Upgraded drupal core with security updates
[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       'view content moderation',
36     ];
37     $editor1 = $this->drupalCreateUser($permissions);
38     $this->drupalLogin($editor1);
39
40     $node_1 = Node::create([
41       'type' => $node_type_id,
42       'title' => 'Draft node',
43       'uid' => $editor1->id(),
44     ]);
45     $node_1->moderation_state->value = 'draft';
46     $node_1->save();
47
48     $node_2 = Node::create([
49       'type' => $node_type_id,
50       'title' => 'Published node',
51       'uid' => $editor1->id(),
52     ]);
53     $node_2->moderation_state->value = 'published';
54     $node_2->save();
55
56     // Resave the node with a new state.
57     $node_2->setTitle('Archived node');
58     $node_2->moderation_state->value = 'archived';
59     $node_2->save();
60
61     // Now show the View, and confirm that the state labels are showing.
62     $this->drupalGet('/latest');
63     $page = $this->getSession()->getPage();
64     $this->assertTrue($page->hasContent('Draft'));
65     $this->assertTrue($page->hasContent('Archived'));
66     $this->assertFalse($page->hasContent('Published'));
67
68     // Now log in as an admin and test the same thing.
69     $permissions = [
70       'access content',
71       'view all revisions',
72       'administer content moderation',
73     ];
74     $admin1 = $this->drupalCreateUser($permissions);
75     $this->drupalLogin($admin1);
76
77     $this->drupalGet('/latest');
78     $page = $this->getSession()->getPage();
79     $this->assertEquals(200, $this->getSession()->getStatusCode());
80     $this->assertTrue($page->hasContent('Draft'));
81     $this->assertTrue($page->hasContent('Archived'));
82     $this->assertFalse($page->hasContent('Published'));
83   }
84
85   /**
86    * Creates a new node type.
87    *
88    * @param string $label
89    *   The human-readable label of the type to create.
90    * @param string $machine_name
91    *   The machine name of the type to create.
92    *
93    * @return NodeType
94    *   The node type just created.
95    */
96   protected function createNodeType($label, $machine_name) {
97     /** @var NodeType $node_type */
98     $node_type = NodeType::create([
99       'type' => $machine_name,
100       'label' => $label,
101     ]);
102     $node_type->save();
103
104     $workflow = Workflow::load('editorial');
105     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', $machine_name);
106     $workflow->save();
107     return $node_type;
108   }
109
110 }