db backup prior to drupal security update
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / LatestRevisionViewsFilterTest.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 "Latest Revision" views filter.
12  *
13  * @group content_moderation
14  */
15 class LatestRevisionViewsFilterTest extends BrowserTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = [
21     'content_moderation_test_views',
22     'content_moderation',
23   ];
24
25   /**
26    * Tests view shows the correct node IDs.
27    */
28   public function testViewShowsCorrectNids() {
29     $this->createNodeType('Test', 'test');
30
31     $permissions = [
32       'access content',
33       'view all revisions',
34     ];
35     $editor1 = $this->drupalCreateUser($permissions);
36
37     $this->drupalLogin($editor1);
38
39     // Make a pre-moderation node.
40     /** @var Node $node_0 */
41     $node_0 = Node::create([
42       'type' => 'test',
43       'title' => 'Node 0 - Rev 1',
44       'uid' => $editor1->id(),
45     ]);
46     $node_0->save();
47
48     // Now enable moderation for subsequent nodes.
49     $workflow = Workflow::load('editorial');
50     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'test');
51     $workflow->save();
52
53     // Make a node that is only ever in Draft.
54     /** @var Node $node_1 */
55     $node_1 = Node::create([
56       'type' => 'test',
57       'title' => 'Node 1 - Rev 1',
58       'uid' => $editor1->id(),
59     ]);
60     $node_1->moderation_state->value = 'draft';
61     $node_1->save();
62
63     // Make a node that is in Draft, then Published.
64     /** @var Node $node_2 */
65     $node_2 = Node::create([
66       'type' => 'test',
67       'title' => 'Node 2 - Rev 1',
68       'uid' => $editor1->id(),
69     ]);
70     $node_2->moderation_state->value = 'draft';
71     $node_2->save();
72
73     $node_2->setTitle('Node 2 - Rev 2');
74     $node_2->moderation_state->value = 'published';
75     $node_2->save();
76
77     // Make a node that is in Draft, then Published, then Draft.
78     /** @var Node $node_3 */
79     $node_3 = Node::create([
80       'type' => 'test',
81       'title' => 'Node 3 - Rev 1',
82       'uid' => $editor1->id(),
83     ]);
84     $node_3->moderation_state->value = 'draft';
85     $node_3->save();
86
87     $node_3->setTitle('Node 3 - Rev 2');
88     $node_3->moderation_state->value = 'published';
89     $node_3->save();
90
91     $node_3->setTitle('Node 3 - Rev 3');
92     $node_3->moderation_state->value = 'draft';
93     $node_3->save();
94
95     // Now show the View, and confirm that only the correct titles are showing.
96     $this->drupalGet('/latest');
97     $page = $this->getSession()->getPage();
98     $this->assertEquals(200, $this->getSession()->getStatusCode());
99     $this->assertTrue($page->hasContent('Node 1 - Rev 1'));
100     $this->assertTrue($page->hasContent('Node 2 - Rev 2'));
101     $this->assertTrue($page->hasContent('Node 3 - Rev 3'));
102     $this->assertFalse($page->hasContent('Node 2 - Rev 1'));
103     $this->assertFalse($page->hasContent('Node 3 - Rev 1'));
104     $this->assertFalse($page->hasContent('Node 3 - Rev 2'));
105     $this->assertFalse($page->hasContent('Node 0 - Rev 1'));
106   }
107
108   /**
109    * Creates a new node type.
110    *
111    * @param string $label
112    *   The human-readable label of the type to create.
113    * @param string $machine_name
114    *   The machine name of the type to create.
115    *
116    * @return NodeType
117    *   The node type just created.
118    */
119   protected function createNodeType($label, $machine_name) {
120     /** @var NodeType $node_type */
121     $node_type = NodeType::create([
122       'type' => $machine_name,
123       'label' => $label,
124     ]);
125     $node_type->save();
126
127     return $node_type;
128   }
129
130 }