Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\Tests\content_moderation\Traits\ContentModerationTestTrait;
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   use ContentModerationTestTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     'content_moderation_test_views',
24     'content_moderation',
25   ];
26
27   /**
28    * Test the view operation access handler with the view permission.
29    */
30   public function testViewShowsCorrectStates() {
31     $node_type_id = 'test';
32     $this->createNodeType('Test', $node_type_id);
33
34     $permissions = [
35       'access content',
36       'view all revisions',
37     ];
38     $editor1 = $this->drupalCreateUser($permissions);
39     $this->drupalLogin($editor1);
40
41     $node_1 = Node::create([
42       'type' => $node_type_id,
43       'title' => 'Draft node',
44       'uid' => $editor1->id(),
45     ]);
46     $node_1->moderation_state->value = 'draft';
47     $node_1->save();
48
49     $node_2 = Node::create([
50       'type' => $node_type_id,
51       'title' => 'Published node',
52       'uid' => $editor1->id(),
53     ]);
54     $node_2->moderation_state->value = 'published';
55     $node_2->save();
56
57     // Resave the node with a new state.
58     $node_2->setTitle('Archived node');
59     $node_2->moderation_state->value = 'archived';
60     $node_2->save();
61
62     // Now show the View, and confirm that the state labels are showing.
63     $this->drupalGet('/latest');
64     $page = $this->getSession()->getPage();
65     $this->assertTrue($page->hasContent('Draft'));
66     $this->assertTrue($page->hasContent('Archived'));
67     $this->assertFalse($page->hasContent('Published'));
68
69     // Now log in as an admin and test the same thing.
70     $permissions = [
71       'access content',
72       'view all revisions',
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 \Drupal\node\Entity\NodeType
94    *   The node type just created.
95    */
96   protected function createNodeType($label, $machine_name) {
97     /** @var \Drupal\node\Entity\NodeType $node_type */
98     $node_type = NodeType::create([
99       'type' => $machine_name,
100       'label' => $label,
101     ]);
102     $node_type->save();
103
104     $workflow = $this->createEditorialWorkflow();
105     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', $machine_name);
106     $workflow->save();
107     return $node_type;
108   }
109
110 }