EntityOperationsTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\KernelTests\KernelTestBase;
  4. use Drupal\node\Entity\Node;
  5. use Drupal\node\Entity\NodeType;
  6. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  7. /**
  8. * @coversDefaultClass \Drupal\content_moderation\EntityOperations
  9. *
  10. * @group content_moderation
  11. */
  12. class EntityOperationsTest extends KernelTestBase {
  13. use ContentModerationTestTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static $modules = [
  18. 'content_moderation',
  19. 'node',
  20. 'user',
  21. 'system',
  22. 'workflows',
  23. ];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp() {
  28. parent::setUp();
  29. $this->installEntitySchema('node');
  30. $this->installSchema('node', 'node_access');
  31. $this->installEntitySchema('user');
  32. $this->installEntitySchema('content_moderation_state');
  33. $this->installConfig('content_moderation');
  34. $this->createNodeType();
  35. }
  36. /**
  37. * Creates a page node type to test with, ensuring that it's moderated.
  38. */
  39. protected function createNodeType() {
  40. $node_type = NodeType::create([
  41. 'type' => 'page',
  42. 'label' => 'Page',
  43. ]);
  44. $node_type->save();
  45. $workflow = $this->createEditorialWorkflow();
  46. $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
  47. $workflow->save();
  48. }
  49. /**
  50. * Verifies that the process of saving pending revisions works as expected.
  51. */
  52. public function testPendingRevisions() {
  53. // Create a new node in draft.
  54. $page = Node::create([
  55. 'type' => 'page',
  56. 'title' => 'A',
  57. ]);
  58. $page->moderation_state->value = 'draft';
  59. $page->save();
  60. $id = $page->id();
  61. // Verify the entity saved correctly, and that the presence of pending
  62. // revisions doesn't affect the default node load.
  63. /** @var \Drupal\node\Entity\Node $page */
  64. $page = Node::load($id);
  65. $this->assertEquals('A', $page->getTitle());
  66. $this->assertTrue($page->isDefaultRevision());
  67. $this->assertFalse($page->isPublished());
  68. // Moderate the entity to published.
  69. $page->setTitle('B');
  70. $page->moderation_state->value = 'published';
  71. $page->save();
  72. // Verify the entity is now published and public.
  73. $page = Node::load($id);
  74. $this->assertEquals('B', $page->getTitle());
  75. $this->assertTrue($page->isDefaultRevision());
  76. $this->assertTrue($page->isPublished());
  77. // Make a new pending revision in Draft.
  78. $page->setTitle('C');
  79. $page->moderation_state->value = 'draft';
  80. $page->save();
  81. // Verify normal loads return the still-default previous version.
  82. $page = Node::load($id);
  83. $this->assertEquals('B', $page->getTitle());
  84. // Verify we can load the pending revision, even if the mechanism is kind
  85. // of gross. Note: revisionIds() is only available on NodeStorageInterface,
  86. // so this won't work for non-nodes. We'd need to use entity queries. This
  87. // is a core bug that should get fixed.
  88. $storage = \Drupal::entityTypeManager()->getStorage('node');
  89. $revision_ids = $storage->revisionIds($page);
  90. sort($revision_ids);
  91. $latest = end($revision_ids);
  92. $page = $storage->loadRevision($latest);
  93. $this->assertEquals('C', $page->getTitle());
  94. $page->setTitle('D');
  95. $page->moderation_state->value = 'published';
  96. $page->save();
  97. // Verify normal loads return the still-default previous version.
  98. $page = Node::load($id);
  99. $this->assertEquals('D', $page->getTitle());
  100. $this->assertTrue($page->isDefaultRevision());
  101. $this->assertTrue($page->isPublished());
  102. // Now check that we can immediately add a new published revision over it.
  103. $page->setTitle('E');
  104. $page->moderation_state->value = 'published';
  105. $page->save();
  106. $page = Node::load($id);
  107. $this->assertEquals('E', $page->getTitle());
  108. $this->assertTrue($page->isDefaultRevision());
  109. $this->assertTrue($page->isPublished());
  110. }
  111. /**
  112. * Verifies that a newly-created node can go straight to published.
  113. */
  114. public function testPublishedCreation() {
  115. // Create a new node in draft.
  116. $page = Node::create([
  117. 'type' => 'page',
  118. 'title' => 'A',
  119. ]);
  120. $page->moderation_state->value = 'published';
  121. $page->save();
  122. $id = $page->id();
  123. // Verify the entity saved correctly.
  124. /** @var \Drupal\node\Entity\Node $page */
  125. $page = Node::load($id);
  126. $this->assertEquals('A', $page->getTitle());
  127. $this->assertTrue($page->isDefaultRevision());
  128. $this->assertTrue($page->isPublished());
  129. }
  130. /**
  131. * Verifies that an unpublished state may be made the default revision.
  132. */
  133. public function testArchive() {
  134. $page = Node::create([
  135. 'type' => 'page',
  136. 'title' => $this->randomString(),
  137. ]);
  138. $page->moderation_state->value = 'published';
  139. $page->save();
  140. $id = $page->id();
  141. // The newly-created page should already be published.
  142. $page = Node::load($id);
  143. $this->assertTrue($page->isPublished());
  144. // When the page is moderated to the archived state, then the latest
  145. // revision should be the default revision, and it should be unpublished.
  146. $page->moderation_state->value = 'archived';
  147. $page->save();
  148. $new_revision_id = $page->getRevisionId();
  149. $storage = \Drupal::entityTypeManager()->getStorage('node');
  150. $new_revision = $storage->loadRevision($new_revision_id);
  151. $this->assertFalse($new_revision->isPublished());
  152. $this->assertTrue($new_revision->isDefaultRevision());
  153. }
  154. }