3 namespace Drupal\Tests\content_moderation\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\workflows\Entity\Workflow;
9 * Tests moderated content administration page functionality.
11 * @group content_moderation
13 class ModeratedContentViewTest extends BrowserTestBase {
16 * A user with permission to bypass access content.
18 * @var \Drupal\Core\Session\AccountInterface
25 public static $modules = ['content_moderation', 'node', 'views'];
30 public function setUp() {
33 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page'])->save();
34 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article'])->save();
35 $this->drupalCreateContentType(['type' => 'unmoderated_type', 'name' => 'Unmoderated type'])->save();
37 $workflow = Workflow::load('editorial');
38 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
39 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'article');
42 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view any unpublished content', 'administer nodes', 'bypass node access']);
46 * Tests the moderated content page.
48 public function testModeratedContentPage() {
49 $assert_sesison = $this->assertSession();
50 $this->drupalLogin($this->adminUser);
52 // Use an explicit changed time to ensure the expected order in the content
53 // admin listing. We want these to appear in the table in the same order as
54 // they appear in the following code, and the 'moderated_content' view has a
55 // table style configuration with a default sort on the 'changed' field
57 $time = \Drupal::time()->getRequestTime();
58 $excluded_nodes['published_page'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time--, 'moderation_state' => 'published']);
59 $excluded_nodes['published_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published']);
61 $excluded_nodes['unmoderated_type'] = $this->drupalCreateNode(['type' => 'unmoderated_type', 'changed' => $time--]);
62 $excluded_nodes['unmoderated_type']->setNewRevision(TRUE);
63 $excluded_nodes['unmoderated_type']->isDefaultRevision(FALSE);
64 $excluded_nodes['unmoderated_type']->changed->value = $time--;
65 $excluded_nodes['unmoderated_type']->save();
67 $nodes['published_then_draft_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published', 'title' => 'first article - published']);
68 $nodes['published_then_draft_article']->setNewRevision(TRUE);
69 $nodes['published_then_draft_article']->setTitle('first article - draft');
70 $nodes['published_then_draft_article']->moderation_state->value = 'draft';
71 $nodes['published_then_draft_article']->changed->value = $time--;
72 $nodes['published_then_draft_article']->save();
74 $nodes['published_then_archived_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published']);
75 $nodes['published_then_archived_article']->setNewRevision(TRUE);
76 $nodes['published_then_archived_article']->moderation_state->value = 'archived';
77 $nodes['published_then_archived_article']->changed->value = $time--;
78 $nodes['published_then_archived_article']->save();
80 $nodes['draft_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'draft']);
81 $nodes['draft_page_1'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time--, 'moderation_state' => 'draft']);
82 $nodes['draft_page_2'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time, 'moderation_state' => 'draft']);
84 // Verify view, edit, and delete links for any content.
85 $this->drupalGet('admin/content/moderated');
86 $assert_sesison->statusCodeEquals(200);
88 // Check that nodes with pending revisions appear in the view.
89 $node_type_labels = $this->xpath('//td[contains(@class, "views-field-type")]');
91 foreach ($nodes as $node) {
92 $assert_sesison->linkByHrefExists('node/' . $node->id());
93 $assert_sesison->linkByHrefExists('node/' . $node->id() . '/edit');
94 $assert_sesison->linkByHrefExists('node/' . $node->id() . '/delete');
95 // Verify that we can see the content type label.
96 $this->assertEquals($node->type->entity->label(), trim($node_type_labels[$delta]->getText()));
100 // Check that nodes that are not moderated or do not have a pending revision
101 // do not appear in the view.
102 foreach ($excluded_nodes as $node) {
103 $assert_sesison->linkByHrefNotExists('node/' . $node->id());
106 // Check that the latest revision is displayed.
107 $assert_sesison->pageTextContains('first article - draft');
108 $assert_sesison->pageTextNotContains('first article - published');
110 // Verify filtering by moderation state.
111 $this->drupalGet('admin/content/moderated', ['query' => ['moderation_state' => 'editorial-draft']]);
113 $assert_sesison->linkByHrefExists('node/' . $nodes['published_then_draft_article']->id() . '/edit');
114 $assert_sesison->linkByHrefExists('node/' . $nodes['draft_article']->id() . '/edit');
115 $assert_sesison->linkByHrefExists('node/' . $nodes['draft_page_1']->id() . '/edit');
116 $assert_sesison->linkByHrefExists('node/' . $nodes['draft_page_1']->id() . '/edit');
117 $assert_sesison->linkByHrefNotExists('node/' . $nodes['published_then_archived_article']->id() . '/edit');
119 // Verify filtering by moderation state and content type.
120 $this->drupalGet('admin/content/moderated', ['query' => ['moderation_state' => 'editorial-draft', 'type' => 'page']]);
122 $assert_sesison->linkByHrefExists('node/' . $nodes['draft_page_1']->id() . '/edit');
123 $assert_sesison->linkByHrefExists('node/' . $nodes['draft_page_2']->id() . '/edit');
124 $assert_sesison->linkByHrefNotExists('node/' . $nodes['published_then_draft_article']->id() . '/edit');
125 $assert_sesison->linkByHrefNotExists('node/' . $nodes['published_then_archived_article']->id() . '/edit');
126 $assert_sesison->linkByHrefNotExists('node/' . $nodes['draft_article']->id() . '/edit');