Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModerationStateAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\workflows\Entity\Workflow;
9
10 /**
11  * Tests the view access control handler for moderation state entities.
12  *
13  * @group content_moderation
14  */
15 class ModerationStateAccessTest extends BrowserTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = [
21     'content_moderation_test_views',
22     'content_moderation',
23   ];
24
25   /**
26    * Test the view operation access handler with the view permission.
27    */
28   public function testViewShowsCorrectStates() {
29     $node_type_id = 'test';
30     $this->createNodeType('Test', $node_type_id);
31
32     $permissions = [
33       'access content',
34       'view all revisions',
35     ];
36     $editor1 = $this->drupalCreateUser($permissions);
37     $this->drupalLogin($editor1);
38
39     $node_1 = Node::create([
40       'type' => $node_type_id,
41       'title' => 'Draft node',
42       'uid' => $editor1->id(),
43     ]);
44     $node_1->moderation_state->value = 'draft';
45     $node_1->save();
46
47     $node_2 = Node::create([
48       'type' => $node_type_id,
49       'title' => 'Published node',
50       'uid' => $editor1->id(),
51     ]);
52     $node_2->moderation_state->value = 'published';
53     $node_2->save();
54
55     // Resave the node with a new state.
56     $node_2->setTitle('Archived node');
57     $node_2->moderation_state->value = 'archived';
58     $node_2->save();
59
60     // Now show the View, and confirm that the state labels are showing.
61     $this->drupalGet('/latest');
62     $page = $this->getSession()->getPage();
63     $this->assertTrue($page->hasContent('Draft'));
64     $this->assertTrue($page->hasContent('Archived'));
65     $this->assertFalse($page->hasContent('Published'));
66
67     // Now log in as an admin and test the same thing.
68     $permissions = [
69       'access content',
70       'view all revisions',
71     ];
72     $admin1 = $this->drupalCreateUser($permissions);
73     $this->drupalLogin($admin1);
74
75     $this->drupalGet('/latest');
76     $page = $this->getSession()->getPage();
77     $this->assertEquals(200, $this->getSession()->getStatusCode());
78     $this->assertTrue($page->hasContent('Draft'));
79     $this->assertTrue($page->hasContent('Archived'));
80     $this->assertFalse($page->hasContent('Published'));
81   }
82
83   /**
84    * Creates a new node type.
85    *
86    * @param string $label
87    *   The human-readable label of the type to create.
88    * @param string $machine_name
89    *   The machine name of the type to create.
90    *
91    * @return \Drupal\node\Entity\NodeType
92    *   The node type just created.
93    */
94   protected function createNodeType($label, $machine_name) {
95     /** @var \Drupal\node\Entity\NodeType $node_type */
96     $node_type = NodeType::create([
97       'type' => $machine_name,
98       'label' => $label,
99     ]);
100     $node_type->save();
101
102     $workflow = Workflow::load('editorial');
103     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', $machine_name);
104     $workflow->save();
105     return $node_type;
106   }
107
108 }