986fe125db740335c205098573ddef2063c46a32
[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\Tests\content_moderation\Traits\ContentModerationTestTrait;
7
8 /**
9  * Tests moderated content administration page functionality.
10  *
11  * @group content_moderation
12  */
13 class ModeratedContentViewTest extends BrowserTestBase {
14
15   use ContentModerationTestTrait;
16
17   /**
18    * A user with permission to bypass access content.
19    *
20    * @var \Drupal\Core\Session\AccountInterface
21    */
22   protected $adminUser;
23
24   /**
25    * {@inheritdoc}
26    */
27   public static $modules = ['content_moderation', 'node', 'views'];
28
29   /**
30    * {@inheritdoc}
31    */
32   public function setUp() {
33     parent::setUp();
34
35     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page'])->save();
36     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article'])->save();
37     $this->drupalCreateContentType(['type' => 'unmoderated_type', 'name' => 'Unmoderated type'])->save();
38
39     $workflow = $this->createEditorialWorkflow();
40     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
41     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'article');
42     $workflow->save();
43
44     $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view any unpublished content', 'administer nodes', 'bypass node access']);
45   }
46
47   /**
48    * Tests the moderated content page.
49    */
50   public function testModeratedContentPage() {
51     $assert_sesison = $this->assertSession();
52     $this->drupalLogin($this->adminUser);
53
54     // Use an explicit changed time to ensure the expected order in the content
55     // admin listing. We want these to appear in the table in the same order as
56     // they appear in the following code, and the 'moderated_content' view has a
57     // table style configuration with a default sort on the 'changed' field
58     // descending.
59     $time = \Drupal::time()->getRequestTime();
60     $excluded_nodes['published_page'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time--, 'moderation_state' => 'published']);
61     $excluded_nodes['published_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published']);
62
63     $excluded_nodes['unmoderated_type'] = $this->drupalCreateNode(['type' => 'unmoderated_type', 'changed' => $time--]);
64     $excluded_nodes['unmoderated_type']->setNewRevision(TRUE);
65     $excluded_nodes['unmoderated_type']->isDefaultRevision(FALSE);
66     $excluded_nodes['unmoderated_type']->changed->value = $time--;
67     $excluded_nodes['unmoderated_type']->save();
68
69     $nodes['published_then_draft_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published', 'title' => 'first article - published']);
70     $nodes['published_then_draft_article']->setNewRevision(TRUE);
71     $nodes['published_then_draft_article']->setTitle('first article - draft');
72     $nodes['published_then_draft_article']->moderation_state->value = 'draft';
73     $nodes['published_then_draft_article']->changed->value = $time--;
74     $nodes['published_then_draft_article']->save();
75
76     $nodes['published_then_archived_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published']);
77     $nodes['published_then_archived_article']->setNewRevision(TRUE);
78     $nodes['published_then_archived_article']->moderation_state->value = 'archived';
79     $nodes['published_then_archived_article']->changed->value = $time--;
80     $nodes['published_then_archived_article']->save();
81
82     $nodes['draft_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'draft']);
83     $nodes['draft_page_1'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time--, 'moderation_state' => 'draft']);
84     $nodes['draft_page_2'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time, 'moderation_state' => 'draft']);
85
86     // Verify view, edit, and delete links for any content.
87     $this->drupalGet('admin/content/moderated');
88     $assert_sesison->statusCodeEquals(200);
89
90     // Check that nodes with pending revisions appear in the view.
91     $node_type_labels = $this->xpath('//td[contains(@class, "views-field-type")]');
92     $delta = 0;
93     foreach ($nodes as $node) {
94       $assert_sesison->linkByHrefExists('node/' . $node->id());
95       $assert_sesison->linkByHrefExists('node/' . $node->id() . '/edit');
96       $assert_sesison->linkByHrefExists('node/' . $node->id() . '/delete');
97       // Verify that we can see the content type label.
98       $this->assertEquals($node->type->entity->label(), trim($node_type_labels[$delta]->getText()));
99       $delta++;
100     }
101
102     // Check that nodes that are not moderated or do not have a pending revision
103     // do not appear in the view.
104     foreach ($excluded_nodes as $node) {
105       $assert_sesison->linkByHrefNotExists('node/' . $node->id());
106     }
107
108     // Check that the latest revision is displayed.
109     $assert_sesison->pageTextContains('first article - draft');
110     $assert_sesison->pageTextNotContains('first article - published');
111
112     // Verify filtering by moderation state.
113     $this->drupalGet('admin/content/moderated', ['query' => ['moderation_state' => 'editorial-draft']]);
114
115     $assert_sesison->linkByHrefExists('node/' . $nodes['published_then_draft_article']->id() . '/edit');
116     $assert_sesison->linkByHrefExists('node/' . $nodes['draft_article']->id() . '/edit');
117     $assert_sesison->linkByHrefExists('node/' . $nodes['draft_page_1']->id() . '/edit');
118     $assert_sesison->linkByHrefExists('node/' . $nodes['draft_page_1']->id() . '/edit');
119     $assert_sesison->linkByHrefNotExists('node/' . $nodes['published_then_archived_article']->id() . '/edit');
120
121     // Verify filtering by moderation state and content type.
122     $this->drupalGet('admin/content/moderated', ['query' => ['moderation_state' => 'editorial-draft', 'type' => 'page']]);
123
124     $assert_sesison->linkByHrefExists('node/' . $nodes['draft_page_1']->id() . '/edit');
125     $assert_sesison->linkByHrefExists('node/' . $nodes['draft_page_2']->id() . '/edit');
126     $assert_sesison->linkByHrefNotExists('node/' . $nodes['published_then_draft_article']->id() . '/edit');
127     $assert_sesison->linkByHrefNotExists('node/' . $nodes['published_then_archived_article']->id() . '/edit');
128     $assert_sesison->linkByHrefNotExists('node/' . $nodes['draft_article']->id() . '/edit');
129   }
130
131 }