d4c4aa37cdecd401841203a4aea1b376688d178b
[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\entity_test\Entity\EntityTestMulRevPub;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
9 use Drupal\views\Views;
10 use Drupal\workflows\Entity\Workflow;
11
12 /**
13  * Tests the views integration of content_moderation.
14  *
15  * @group content_moderation
16  */
17 class ViewsDataIntegrationTest extends ViewsKernelTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     'content_moderation_test_views',
24     'node',
25     'content_moderation',
26     'workflows',
27     'entity_test',
28   ];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp($import_test_views = TRUE) {
34     parent::setUp($import_test_views);
35
36     $this->installEntitySchema('node');
37     $this->installEntitySchema('entity_test_mulrevpub');
38     $this->installEntitySchema('user');
39     $this->installEntitySchema('content_moderation_state');
40     $this->installSchema('node', 'node_access');
41     $this->installConfig('content_moderation_test_views');
42     $this->installConfig('content_moderation');
43
44     $node_type = NodeType::create([
45       'type' => 'page',
46     ]);
47     $node_type->save();
48     $workflow = Workflow::load('editorial');
49     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
50     $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
51     $workflow->save();
52   }
53
54   /**
55    * Tests content_moderation_views_data().
56    *
57    * @see content_moderation_views_data()
58    */
59   public function testViewsData() {
60     $node = Node::create([
61       'type' => 'page',
62       'title' => 'Test title first revision',
63     ]);
64     $node->moderation_state->value = 'published';
65     $node->save();
66
67     // Create a totally unrelated entity to ensure the extra join information
68     // joins by the correct entity type.
69     $unrelated_entity = EntityTestMulRevPub::create([
70       'id' => $node->id(),
71     ]);
72     $unrelated_entity->save();
73
74     $this->assertEquals($unrelated_entity->id(), $node->id());
75
76     $revision = clone $node;
77     $revision->setNewRevision(TRUE);
78     $revision->isDefaultRevision(FALSE);
79     $revision->title->value = 'Test title second revision';
80     $revision->moderation_state->value = 'draft';
81     $revision->save();
82
83     $view = Views::getView('test_content_moderation_latest_revision');
84     $view->execute();
85
86     // Ensure that the content_revision_tracker contains the right latest
87     // revision ID.
88     // Also ensure that the relationship back to the revision table contains the
89     // right latest revision.
90     $expected_result = [
91       [
92         'nid' => $node->id(),
93         'revision_id' => $revision->getRevisionId(),
94         'title' => $revision->label(),
95         'moderation_state_1' => 'draft',
96         'moderation_state' => 'published',
97       ],
98     ];
99     $this->assertIdenticalResultset($view, $expected_result, ['nid' => 'nid', 'content_revision_tracker_revision_id' => 'revision_id', 'moderation_state' => 'moderation_state', 'moderation_state_1' => 'moderation_state_1']);
100   }
101
102   /**
103    * Tests the join from the revision data table to the moderation state table.
104    */
105   public function testContentModerationStateRevisionJoin() {
106     $node = Node::create([
107       'type' => 'page',
108       'title' => 'Test title first revision',
109     ]);
110     $node->moderation_state->value = 'published';
111     $node->save();
112
113     $revision = clone $node;
114     $revision->setNewRevision(TRUE);
115     $revision->isDefaultRevision(FALSE);
116     $revision->title->value = 'Test title second revision';
117     $revision->moderation_state->value = 'draft';
118     $revision->save();
119
120     $view = Views::getView('test_content_moderation_revision_test');
121     $view->execute();
122
123     $expected_result = [
124       [
125         'vid' => $node->getRevisionId(),
126         'moderation_state' => 'published',
127       ],
128       [
129         'vid' => $revision->getRevisionId(),
130         'moderation_state' => 'draft',
131       ],
132     ];
133     $this->assertIdenticalResultset($view, $expected_result, ['vid' => 'vid', 'moderation_state' => 'moderation_state']);
134   }
135
136   /**
137    * Tests the join from the data table to the moderation state table.
138    */
139   public function testContentModerationStateBaseJoin() {
140     $node = Node::create([
141       'type' => 'page',
142       'title' => 'Test title first revision',
143     ]);
144     $node->moderation_state->value = 'published';
145     $node->save();
146
147     $revision = clone $node;
148     $revision->setNewRevision(TRUE);
149     $revision->isDefaultRevision(FALSE);
150     $revision->title->value = 'Test title second revision';
151     $revision->moderation_state->value = 'draft';
152     $revision->save();
153
154     $view = Views::getView('test_content_moderation_base_table_test');
155     $view->execute();
156
157     $expected_result = [
158       [
159         'nid' => $node->id(),
160         // @todo I would have expected that the content_moderation_state default
161         //   revision is the same one as in the node, but it isn't.
162         // Joins from the base table to the default revision of the
163         // content_moderation.
164         'moderation_state' => 'draft',
165         // Joins from the revision table to the default revision of the
166         // content_moderation.
167         'moderation_state_1' => 'draft',
168         // Joins from the revision table to the revision of the
169         // content_moderation.
170         'moderation_state_2' => 'published',
171       ],
172     ];
173     $this->assertIdenticalResultset($view, $expected_result, ['nid' => 'nid', 'moderation_state' => 'moderation_state', 'moderation_state_1' => 'moderation_state_1', 'moderation_state_2' => 'moderation_state_2']);
174   }
175
176 }