3 namespace Drupal\Tests\content_moderation\Kernel;
5 use Drupal\content_moderation\Entity\ContentModerationState;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
12 * Test the ContentModerationState storage schema.
14 * @coversDefaultClass \Drupal\content_moderation\ContentModerationStateStorageSchema
15 * @group content_moderation
17 class ContentModerationStateStorageSchemaTest extends KernelTestBase {
19 use ContentModerationTestTrait;
24 public static $modules = [
37 protected function setUp() {
40 $this->installSchema('node', 'node_access');
41 $this->installEntitySchema('node');
42 $this->installEntitySchema('entity_test');
43 $this->installEntitySchema('user');
44 $this->installEntitySchema('content_moderation_state');
45 $this->installConfig('content_moderation');
50 $workflow = $this->createEditorialWorkflow();
51 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
56 * Test the ContentModerationState unique keys.
58 * @covers ::getEntitySchema
60 public function testUniqueKeys() {
61 // Create a node which will create a new ContentModerationState entity.
62 $node = Node::create([
63 'title' => 'Test title',
65 'moderation_state' => 'draft',
69 // Ensure an exception when all values match.
70 $this->assertStorageException([
71 'content_entity_type_id' => $node->getEntityTypeId(),
72 'content_entity_id' => $node->id(),
73 'content_entity_revision_id' => $node->getRevisionId(),
76 // No exception for the same values, with a different langcode.
77 $this->assertStorageException([
78 'content_entity_type_id' => $node->getEntityTypeId(),
79 'content_entity_id' => $node->id(),
80 'content_entity_revision_id' => $node->getRevisionId(),
84 // A different workflow should not trigger an exception.
85 $this->assertStorageException([
86 'content_entity_type_id' => $node->getEntityTypeId(),
87 'content_entity_id' => $node->id(),
88 'content_entity_revision_id' => $node->getRevisionId(),
92 // Different entity types should not trigger an exception.
93 $this->assertStorageException([
94 'content_entity_type_id' => 'entity_test',
95 'content_entity_id' => $node->id(),
96 'content_entity_revision_id' => $node->getRevisionId(),
99 // Different entity and revision IDs should not trigger an exception.
100 $this->assertStorageException([
101 'content_entity_type_id' => $node->getEntityTypeId(),
102 'content_entity_id' => 9999,
103 'content_entity_revision_id' => 9999,
106 // Creating a version of the entity with a previously used, but not current
107 // revision ID should trigger an exception.
108 $old_revision_id = $node->getRevisionId();
109 $node->setNewRevision(TRUE);
110 $node->title = 'Updated title';
111 $node->moderation_state = 'published';
113 $this->assertStorageException([
114 'content_entity_type_id' => $node->getEntityTypeId(),
115 'content_entity_id' => $node->id(),
116 'content_entity_revision_id' => $old_revision_id,
121 * Assert if a storage exception is triggered when saving a given entity.
123 * @param array $values
124 * An array of entity values.
125 * @param bool $has_exception
126 * If an exception should be triggered when saving the entity.
128 protected function assertStorageException(array $values, $has_exception) {
130 'moderation_state' => 'draft',
131 'workflow' => 'editorial',
133 $entity = ContentModerationState::create($values + $defaults);
134 $exception_triggered = FALSE;
136 ContentModerationState::updateOrCreateFromEntity($entity);
138 catch (\Exception $e) {
139 $exception_triggered = TRUE;
141 $this->assertEquals($has_exception, $exception_triggered);