af59651410f9dc9c9724e0bc7562b9d118fd35c6
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ContentModerationStateStorageSchemaTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
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;
10
11 /**
12  * Test the ContentModerationState storage schema.
13  *
14  * @coversDefaultClass \Drupal\content_moderation\ContentModerationStateStorageSchema
15  * @group content_moderation
16  */
17 class ContentModerationStateStorageSchemaTest extends KernelTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     'node',
24     'content_moderation',
25     'user',
26     'system',
27     'text',
28     'workflows',
29     'entity_test',
30   ];
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37
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');
44
45     NodeType::create([
46       'type' => 'example',
47     ])->save();
48     $workflow = Workflow::load('editorial');
49     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
50     $workflow->save();
51   }
52
53   /**
54    * Test the ContentModerationState unique keys.
55    *
56    * @covers ::getEntitySchema
57    */
58   public function testUniqueKeys() {
59     // Create a node which will create a new ContentModerationState entity.
60     $node = Node::create([
61       'title' => 'Test title',
62       'type' => 'example',
63       'moderation_state' => 'draft',
64     ]);
65     $node->save();
66
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(),
72     ], TRUE);
73
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(),
79       'langcode' => 'de',
80     ], FALSE);
81
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(),
87       'workflow' => 'foo',
88     ], FALSE);
89
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(),
95     ], FALSE);
96
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,
102     ], FALSE);
103
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';
110     $node->save();
111     $this->assertStorageException([
112       'content_entity_type_id' => $node->getEntityTypeId(),
113       'content_entity_id' => $node->id(),
114       'content_entity_revision_id' => $old_revision_id,
115     ], TRUE);
116   }
117
118   /**
119    * Assert if a storage exception is triggered when saving a given entity.
120    *
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.
125    */
126   protected function assertStorageException(array $values, $has_exception) {
127     $defaults = [
128       'moderation_state' => 'draft',
129       'workflow' => 'editorial',
130     ];
131     $entity = ContentModerationState::create($values + $defaults);
132     $exception_triggered = FALSE;
133     try {
134       ContentModerationState::updateOrCreateFromEntity($entity);
135     }
136     catch (\Exception $e) {
137       $exception_triggered = TRUE;
138     }
139     $this->assertEquals($has_exception, $exception_triggered);
140   }
141
142 }