Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\Tests\content_moderation\Traits\ContentModerationTestTrait;
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   use ContentModerationTestTrait;
20
21   /**
22    * {@inheritdoc}
23    */
24   public static $modules = [
25     'node',
26     'content_moderation',
27     'user',
28     'system',
29     'text',
30     'workflows',
31     'entity_test',
32   ];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
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');
46
47     NodeType::create([
48       'type' => 'example',
49     ])->save();
50     $workflow = $this->createEditorialWorkflow();
51     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
52     $workflow->save();
53   }
54
55   /**
56    * Test the ContentModerationState unique keys.
57    *
58    * @covers ::getEntitySchema
59    */
60   public function testUniqueKeys() {
61     // Create a node which will create a new ContentModerationState entity.
62     $node = Node::create([
63       'title' => 'Test title',
64       'type' => 'example',
65       'moderation_state' => 'draft',
66     ]);
67     $node->save();
68
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(),
74     ], TRUE);
75
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(),
81       'langcode' => 'de',
82     ], FALSE);
83
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(),
89       'workflow' => 'foo',
90     ], FALSE);
91
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(),
97     ], FALSE);
98
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,
104     ], FALSE);
105
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';
112     $node->save();
113     $this->assertStorageException([
114       'content_entity_type_id' => $node->getEntityTypeId(),
115       'content_entity_id' => $node->id(),
116       'content_entity_revision_id' => $old_revision_id,
117     ], TRUE);
118   }
119
120   /**
121    * Assert if a storage exception is triggered when saving a given entity.
122    *
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.
127    */
128   protected function assertStorageException(array $values, $has_exception) {
129     $defaults = [
130       'moderation_state' => 'draft',
131       'workflow' => 'editorial',
132     ];
133     $entity = ContentModerationState::create($values + $defaults);
134     $exception_triggered = FALSE;
135     try {
136       ContentModerationState::updateOrCreateFromEntity($entity);
137     }
138     catch (\Exception $e) {
139       $exception_triggered = TRUE;
140     }
141     $this->assertEquals($has_exception, $exception_triggered);
142   }
143
144 }