7891b60f58755166441afc9b3169765ecd65b150
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModeratedContentViewTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\workflows\Entity\Workflow;
7
8 /**
9  * Tests moderated content administration page functionality.
10  *
11  * @group content_moderation
12  */
13 class ModeratedContentViewTest extends BrowserTestBase {
14
15   /**
16    * A user with permission to bypass access content.
17    *
18    * @var \Drupal\Core\Session\AccountInterface
19    */
20   protected $adminUser;
21
22   /**
23    * {@inheritdoc}
24    */
25   public static $modules = ['content_moderation', 'node', 'views'];
26
27   /**
28    * {@inheritdoc}
29    */
30   public function setUp() {
31     parent::setUp();
32
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();
36
37     $workflow = Workflow::load('editorial');
38     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
39     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'article');
40     $workflow->save();
41
42     $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view any unpublished content', 'administer nodes', 'bypass node access']);
43   }
44
45   /**
46    * Tests the moderated content page.
47    */
48   public function testModeratedContentPage() {
49     $assert_sesison = $this->assertSession();
50     $this->drupalLogin($this->adminUser);
51
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
56     // descending.
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']);
60
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();
66
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();
73
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();
79
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']);
83
84     // Verify view, edit, and delete links for any content.
85     $this->drupalGet('admin/content/moderated');
86     $assert_sesison->statusCodeEquals(200);
87
88     // Check that nodes with pending revisions appear in the view.
89     $node_type_labels = $this->xpath('//td[contains(@class, "views-field-type")]');
90     $delta = 0;
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()));
97       $delta++;
98     }
99
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());
104     }
105
106     // Check that the latest revision is displayed.
107     $assert_sesison->pageTextContains('first article - draft');
108     $assert_sesison->pageTextNotContains('first article - published');
109
110     // Verify filtering by moderation state.
111     $this->drupalGet('admin/content/moderated', ['query' => ['moderation_state' => 'editorial-draft']]);
112
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');
118
119     // Verify filtering by moderation state and content type.
120     $this->drupalGet('admin/content/moderated', ['query' => ['moderation_state' => 'editorial-draft', 'type' => 'page']]);
121
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');
127   }
128
129 }