3 namespace Drupal\Tests\content_moderation\Kernel;
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;
13 * Tests the views integration of content_moderation.
15 * @group content_moderation
17 class ViewsDataIntegrationTest extends ViewsKernelTestBase {
22 public static $modules = [
23 'content_moderation_test_views',
33 protected function setUp($import_test_views = TRUE) {
34 parent::setUp($import_test_views);
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');
44 $node_type = NodeType::create([
48 $workflow = Workflow::load('editorial');
49 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
50 $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
55 * Tests content_moderation_views_data().
57 * @see content_moderation_views_data()
59 public function testViewsData() {
60 $node = Node::create([
62 'title' => 'Test title first revision',
64 $node->moderation_state->value = 'published';
67 // Create a totally unrelated entity to ensure the extra join information
68 // joins by the correct entity type.
69 $unrelated_entity = EntityTestMulRevPub::create([
72 $unrelated_entity->save();
74 $this->assertEquals($unrelated_entity->id(), $node->id());
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';
83 $view = Views::getView('test_content_moderation_latest_revision');
86 // Ensure that the content_revision_tracker contains the right latest
88 // Also ensure that the relationship back to the revision table contains the
89 // right latest revision.
93 'revision_id' => $revision->getRevisionId(),
94 'title' => $revision->label(),
95 'moderation_state_1' => 'draft',
96 'moderation_state' => 'published',
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']);
103 * Tests the join from the revision data table to the moderation state table.
105 public function testContentModerationStateRevisionJoin() {
106 $node = Node::create([
108 'title' => 'Test title first revision',
110 $node->moderation_state->value = 'published';
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';
120 $view = Views::getView('test_content_moderation_revision_test');
125 'vid' => $node->getRevisionId(),
126 'moderation_state' => 'published',
129 'vid' => $revision->getRevisionId(),
130 'moderation_state' => 'draft',
133 $this->assertIdenticalResultset($view, $expected_result, ['vid' => 'vid', 'moderation_state' => 'moderation_state']);
137 * Tests the join from the data table to the moderation state table.
139 public function testContentModerationStateBaseJoin() {
140 $node = Node::create([
142 'title' => 'Test title first revision',
144 $node->moderation_state->value = 'published';
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';
154 $view = Views::getView('test_content_moderation_base_table_test');
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',
173 $this->assertIdenticalResultset($view, $expected_result, ['nid' => 'nid', 'moderation_state' => 'moderation_state', 'moderation_state_1' => 'moderation_state_1', 'moderation_state_2' => 'moderation_state_2']);