ViewsDataIntegrationTest.php 4.4 KB

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