ContentModerationStateStorageSchemaTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\content_moderation\Entity\ContentModerationState;
  4. use Drupal\KernelTests\KernelTestBase;
  5. use Drupal\node\Entity\Node;
  6. use Drupal\node\Entity\NodeType;
  7. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  8. /**
  9. * Test the ContentModerationState storage schema.
  10. *
  11. * @coversDefaultClass \Drupal\content_moderation\ContentModerationStateStorageSchema
  12. * @group content_moderation
  13. */
  14. class ContentModerationStateStorageSchemaTest extends KernelTestBase {
  15. use ContentModerationTestTrait;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public static $modules = [
  20. 'node',
  21. 'content_moderation',
  22. 'user',
  23. 'system',
  24. 'text',
  25. 'workflows',
  26. 'entity_test',
  27. ];
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->installSchema('node', 'node_access');
  34. $this->installEntitySchema('node');
  35. $this->installEntitySchema('entity_test');
  36. $this->installEntitySchema('user');
  37. $this->installEntitySchema('content_moderation_state');
  38. $this->installConfig('content_moderation');
  39. NodeType::create([
  40. 'type' => 'example',
  41. ])->save();
  42. $workflow = $this->createEditorialWorkflow();
  43. $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
  44. $workflow->save();
  45. }
  46. /**
  47. * Test the ContentModerationState unique keys.
  48. *
  49. * @covers ::getEntitySchema
  50. */
  51. public function testUniqueKeys() {
  52. // Create a node which will create a new ContentModerationState entity.
  53. $node = Node::create([
  54. 'title' => 'Test title',
  55. 'type' => 'example',
  56. 'moderation_state' => 'draft',
  57. ]);
  58. $node->save();
  59. // Ensure an exception when all values match.
  60. $this->assertStorageException([
  61. 'content_entity_type_id' => $node->getEntityTypeId(),
  62. 'content_entity_id' => $node->id(),
  63. 'content_entity_revision_id' => $node->getRevisionId(),
  64. ], TRUE);
  65. // No exception for the same values, with a different langcode.
  66. $this->assertStorageException([
  67. 'content_entity_type_id' => $node->getEntityTypeId(),
  68. 'content_entity_id' => $node->id(),
  69. 'content_entity_revision_id' => $node->getRevisionId(),
  70. 'langcode' => 'de',
  71. ], FALSE);
  72. // A different workflow should not trigger an exception.
  73. $this->assertStorageException([
  74. 'content_entity_type_id' => $node->getEntityTypeId(),
  75. 'content_entity_id' => $node->id(),
  76. 'content_entity_revision_id' => $node->getRevisionId(),
  77. 'workflow' => 'foo',
  78. ], FALSE);
  79. // Different entity types should not trigger an exception.
  80. $this->assertStorageException([
  81. 'content_entity_type_id' => 'entity_test',
  82. 'content_entity_id' => $node->id(),
  83. 'content_entity_revision_id' => $node->getRevisionId(),
  84. ], FALSE);
  85. // Different entity and revision IDs should not trigger an exception.
  86. $this->assertStorageException([
  87. 'content_entity_type_id' => $node->getEntityTypeId(),
  88. 'content_entity_id' => 9999,
  89. 'content_entity_revision_id' => 9999,
  90. ], FALSE);
  91. // Creating a version of the entity with a previously used, but not current
  92. // revision ID should trigger an exception.
  93. $old_revision_id = $node->getRevisionId();
  94. $node->setNewRevision(TRUE);
  95. $node->title = 'Updated title';
  96. $node->moderation_state = 'published';
  97. $node->save();
  98. $this->assertStorageException([
  99. 'content_entity_type_id' => $node->getEntityTypeId(),
  100. 'content_entity_id' => $node->id(),
  101. 'content_entity_revision_id' => $old_revision_id,
  102. ], TRUE);
  103. }
  104. /**
  105. * Assert if a storage exception is triggered when saving a given entity.
  106. *
  107. * @param array $values
  108. * An array of entity values.
  109. * @param bool $has_exception
  110. * If an exception should be triggered when saving the entity.
  111. */
  112. protected function assertStorageException(array $values, $has_exception) {
  113. $defaults = [
  114. 'moderation_state' => 'draft',
  115. 'workflow' => 'editorial',
  116. ];
  117. $entity = ContentModerationState::create($values + $defaults);
  118. $exception_triggered = FALSE;
  119. try {
  120. ContentModerationState::updateOrCreateFromEntity($entity);
  121. }
  122. catch (\Exception $e) {
  123. $exception_triggered = TRUE;
  124. }
  125. $this->assertEquals($has_exception, $exception_triggered);
  126. }
  127. }