DefaultRevisionStateTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\KernelTests\KernelTestBase;
  4. use Drupal\language\Entity\ConfigurableLanguage;
  5. use Drupal\node\Entity\Node;
  6. use Drupal\node\Entity\NodeType;
  7. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  8. /**
  9. * Tests the correct default revision is set.
  10. *
  11. * @group content_moderation
  12. */
  13. class DefaultRevisionStateTest extends KernelTestBase {
  14. use ContentModerationTestTrait;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static $modules = [
  19. 'entity_test',
  20. 'node',
  21. 'block_content',
  22. 'content_moderation',
  23. 'user',
  24. 'system',
  25. 'language',
  26. 'content_translation',
  27. 'text',
  28. 'workflows',
  29. ];
  30. /**
  31. * @var \Drupal\Core\Entity\EntityTypeManager
  32. */
  33. protected $entityTypeManager;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function setUp() {
  38. parent::setUp();
  39. $this->installSchema('node', 'node_access');
  40. $this->installEntitySchema('node');
  41. $this->installEntitySchema('user');
  42. $this->installEntitySchema('entity_test_with_bundle');
  43. $this->installEntitySchema('entity_test_rev');
  44. $this->installEntitySchema('entity_test_mulrevpub');
  45. $this->installEntitySchema('block_content');
  46. $this->installEntitySchema('content_moderation_state');
  47. $this->installConfig('content_moderation');
  48. $this->entityTypeManager = $this->container->get('entity_type.manager');
  49. }
  50. /**
  51. * Tests a translatable Node.
  52. */
  53. public function testMultilingual() {
  54. // Enable French.
  55. ConfigurableLanguage::createFromLangcode('fr')->save();
  56. $node_type = NodeType::create([
  57. 'type' => 'example',
  58. ]);
  59. $node_type->save();
  60. $this->container->get('content_translation.manager')->setEnabled('node', 'example', TRUE);
  61. $workflow = $this->createEditorialWorkflow();
  62. $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
  63. $workflow->save();
  64. $english_node = Node::create([
  65. 'type' => 'example',
  66. 'title' => 'Test title',
  67. ]);
  68. // Revision 1 (en).
  69. $english_node
  70. ->setUnpublished()
  71. ->save();
  72. $this->assertEquals('draft', $english_node->moderation_state->value);
  73. $this->assertFalse($english_node->isPublished());
  74. $this->assertTrue($english_node->isDefaultRevision());
  75. $this->assertModerationState($english_node->getRevisionId(), $english_node->language()->getId(), 'draft');
  76. // Revision 2 (fr)
  77. $french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
  78. $french_node->moderation_state->value = 'published';
  79. $french_node->save();
  80. $this->assertTrue($french_node->isPublished());
  81. $this->assertTrue($french_node->isDefaultRevision());
  82. $this->assertModerationState($french_node->getRevisionId(), $french_node->language()->getId(), 'published');
  83. // Revision 3 (fr)
  84. $node = Node::load($english_node->id())->getTranslation('fr');
  85. $node->moderation_state->value = 'draft';
  86. $node->save();
  87. $this->assertFalse($node->isPublished());
  88. $this->assertFalse($node->isDefaultRevision());
  89. $this->assertModerationState($node->getRevisionId(), $node->language()->getId(), 'draft');
  90. // Revision 4 (en)
  91. $latest_revision = $this->entityTypeManager->getStorage('node')->loadRevision(3);
  92. $latest_revision->moderation_state->value = 'draft';
  93. $latest_revision->save();
  94. $this->assertFalse($latest_revision->isPublished());
  95. $this->assertFalse($latest_revision->isDefaultRevision());
  96. $this->assertModerationState($latest_revision->getRevisionId(), $latest_revision->language()->getId(), 'draft');
  97. }
  98. /**
  99. * Verifies the expected moderation state revision exists.
  100. *
  101. * @param int $revision_id
  102. * The revision ID of the host entity.
  103. * @param string $langcode
  104. * The language code of the host entity to check.
  105. * @param string $expected_state
  106. * The state the content moderation state revision should be in.
  107. * @param string $expected_workflow
  108. * The workflow the content moderation state revision should be using.
  109. */
  110. protected function assertModerationState($revision_id, $langcode, $expected_state, $expected_workflow = 'editorial') {
  111. $moderation_state_storage = $this->entityTypeManager->getStorage('content_moderation_state');
  112. $query = $moderation_state_storage->getQuery();
  113. $results = $query->allRevisions()
  114. ->condition('content_entity_revision_id', $revision_id)
  115. ->condition('langcode', $langcode)
  116. ->execute();
  117. $this->assertCount(1, $results);
  118. $moderation_state = $moderation_state_storage
  119. ->loadRevision(key($results))
  120. ->getTranslation($langcode);
  121. $this->assertEquals($expected_state, $moderation_state->get('moderation_state')->value);
  122. $this->assertEquals($expected_workflow, $moderation_state->get('workflow')->target_id);
  123. }
  124. }