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\workflows\Entity\Workflow;
12 * Test the ContentModerationState storage schema.
14 * @coversDefaultClass \Drupal\content_moderation\ContentModerationStateStorageSchema
15 * @group content_moderation
17 class ContentModerationStateStorageSchemaTest extends KernelTestBase {
22 public static $modules = [
35 protected function setUp() {
38 $this->installSchema('node', 'node_access');
39 $this->installEntitySchema('node');
40 $this->installEntitySchema('entity_test');
41 $this->installEntitySchema('user');
42 $this->installEntitySchema('content_moderation_state');
43 $this->installConfig('content_moderation');
48 $workflow = Workflow::load('editorial');
49 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
54 * Test the ContentModerationState unique keys.
56 * @covers ::getEntitySchema
58 public function testUniqueKeys() {
59 // Create a node which will create a new ContentModerationState entity.
60 $node = Node::create([
61 'title' => 'Test title',
63 'moderation_state' => 'draft',
67 // Ensure an exception when all values match.
68 $this->assertStorageException([
69 'content_entity_type_id' => $node->getEntityTypeId(),
70 'content_entity_id' => $node->id(),
71 'content_entity_revision_id' => $node->getRevisionId(),
74 // No exception for the same values, with a different langcode.
75 $this->assertStorageException([
76 'content_entity_type_id' => $node->getEntityTypeId(),
77 'content_entity_id' => $node->id(),
78 'content_entity_revision_id' => $node->getRevisionId(),
82 // A different workflow should not trigger an exception.
83 $this->assertStorageException([
84 'content_entity_type_id' => $node->getEntityTypeId(),
85 'content_entity_id' => $node->id(),
86 'content_entity_revision_id' => $node->getRevisionId(),
90 // Different entity types should not trigger an exception.
91 $this->assertStorageException([
92 'content_entity_type_id' => 'entity_test',
93 'content_entity_id' => $node->id(),
94 'content_entity_revision_id' => $node->getRevisionId(),
97 // Different entity and revision IDs should not trigger an exception.
98 $this->assertStorageException([
99 'content_entity_type_id' => $node->getEntityTypeId(),
100 'content_entity_id' => 9999,
101 'content_entity_revision_id' => 9999,
104 // Creating a version of the entity with a previously used, but not current
105 // revision ID should trigger an exception.
106 $old_revision_id = $node->getRevisionId();
107 $node->setNewRevision(TRUE);
108 $node->title = 'Updated title';
109 $node->moderation_state = 'published';
111 $this->assertStorageException([
112 'content_entity_type_id' => $node->getEntityTypeId(),
113 'content_entity_id' => $node->id(),
114 'content_entity_revision_id' => $old_revision_id,
119 * Assert if a storage exception is triggered when saving a given entity.
121 * @param array $values
122 * An array of entity values.
123 * @param bool $has_exception
124 * If an exception should be triggered when saving the entity.
126 protected function assertStorageException(array $values, $has_exception) {
128 'moderation_state' => 'draft',
129 'workflow' => 'editorial',
131 $entity = ContentModerationState::create($values + $defaults);
132 $exception_triggered = FALSE;
134 ContentModerationState::updateOrCreateFromEntity($entity);
136 catch (\Exception $e) {
137 $exception_triggered = TRUE;
139 $this->assertEquals($has_exception, $exception_triggered);