0f89125bb6a0236a1dd4feedf7703ed3c89db992
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ViewsDataIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
8 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
9 use Drupal\views\Views;
10
11 /**
12  * Tests the views integration of content_moderation.
13  *
14  * @group content_moderation
15  */
16 class ViewsDataIntegrationTest extends ViewsKernelTestBase {
17
18   use ContentModerationTestTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = [
24     'content_moderation_test_views',
25     'node',
26     'content_moderation',
27     'workflows',
28     'entity_test',
29   ];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp($import_test_views = TRUE) {
35     parent::setUp($import_test_views);
36
37     $this->installEntitySchema('node');
38     $this->installEntitySchema('entity_test_mulrevpub');
39     $this->installEntitySchema('user');
40     $this->installEntitySchema('content_moderation_state');
41     $this->installSchema('node', 'node_access');
42     $this->installConfig('content_moderation_test_views');
43     $this->installConfig('content_moderation');
44
45     $node_type = NodeType::create([
46       'type' => 'page',
47     ]);
48     $node_type->save();
49     $workflow = $this->createEditorialWorkflow();
50     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
51     $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
52     $workflow->save();
53   }
54
55   /**
56    * Tests the join from the revision data table to the moderation state table.
57    */
58   public function testContentModerationStateRevisionJoin() {
59     $node = Node::create([
60       'type' => 'page',
61       'title' => 'Test title first revision',
62     ]);
63     $node->moderation_state->value = 'published';
64     $node->save();
65
66     $revision = clone $node;
67     $revision->setNewRevision(TRUE);
68     $revision->isDefaultRevision(FALSE);
69     $revision->title->value = 'Test title second revision';
70     $revision->moderation_state->value = 'draft';
71     $revision->save();
72
73     $view = Views::getView('test_content_moderation_revision_test');
74     $view->execute();
75
76     $expected_result = [
77       [
78         'vid' => $node->getRevisionId(),
79         'moderation_state' => 'published',
80       ],
81       [
82         'vid' => $revision->getRevisionId(),
83         'moderation_state' => 'draft',
84       ],
85     ];
86     $this->assertIdenticalResultset($view, $expected_result, ['vid' => 'vid', 'moderation_state' => 'moderation_state']);
87   }
88
89   /**
90    * Tests the join from the data table to the moderation state table.
91    */
92   public function testContentModerationStateBaseJoin() {
93     $node = Node::create([
94       'type' => 'page',
95       'title' => 'Test title first revision',
96     ]);
97     $node->moderation_state->value = 'published';
98     $node->save();
99
100     $revision = clone $node;
101     $revision->setNewRevision(TRUE);
102     $revision->isDefaultRevision(FALSE);
103     $revision->title->value = 'Test title second revision';
104     $revision->moderation_state->value = 'draft';
105     $revision->save();
106
107     $view = Views::getView('test_content_moderation_base_table_test');
108     $view->execute();
109
110     $expected_result = [
111       [
112         'nid' => $node->id(),
113         // Joins from the base table to the default revision of the
114         // content_moderation.
115         'moderation_state' => 'published',
116         // Joins from the revision table to the default revision of the
117         // content_moderation.
118         'moderation_state_1' => 'published',
119         // Joins from the revision table to the revision of the
120         // content_moderation.
121         'moderation_state_2' => 'published',
122       ],
123     ];
124     $this->assertIdenticalResultset($view, $expected_result, ['nid' => 'nid', 'moderation_state' => 'moderation_state', 'moderation_state_1' => 'moderation_state_1', 'moderation_state_2' => 'moderation_state_2']);
125   }
126
127   /**
128    * Tests the content moderation state views field.
129    */
130   public function testContentModerationStateField() {
131     $node = Node::create([
132       'type' => 'page',
133       'title' => 'Test title',
134     ]);
135     $node->moderation_state->value = 'published';
136     $node->save();
137
138     $view = Views::getView('test_content_moderation_field_state_test');
139     $view->execute();
140
141     $expected_result = [
142       [
143         'title' => 'Test title',
144         'moderation_state' => 'published',
145       ],
146     ];
147     $this->assertIdenticalResultset($view, $expected_result, ['title' => 'title', 'moderation_state' => 'moderation_state']);
148   }
149
150 }