Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / KeyValueStore / KeyValueContentEntityStorageTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\KeyValueStore;
4
5 use Drupal\Core\Entity\EntityMalformedException;
6 use Drupal\Core\Entity\EntityStorageException;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\entity_test\Entity\EntityTestLabel;
9
10 /**
11  * Tests KeyValueEntityStorage for content entities.
12  *
13  * @group KeyValueStore
14  */
15 class KeyValueContentEntityStorageTest extends KernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['user', 'entity_test', 'keyvalue_test'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29     $this->installEntitySchema('user');
30   }
31
32   /**
33    * Tests CRUD operations.
34    *
35    * @covers \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::hasData
36    */
37   public function testCRUD() {
38     $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
39
40     $storage = \Drupal::entityTypeManager()->getStorage('entity_test_label');
41     $this->assertFalse($storage->hasData());
42
43     // Verify default properties on a newly created empty entity.
44     $empty = EntityTestLabel::create();
45     $this->assertIdentical($empty->id->value, NULL);
46     $this->assertIdentical($empty->name->value, NULL);
47     $this->assertTrue($empty->uuid->value);
48     $this->assertIdentical($empty->langcode->value, $default_langcode);
49
50     // Verify ConfigEntity properties/methods on the newly created empty entity.
51     $this->assertIdentical($empty->isNew(), TRUE);
52     $this->assertIdentical($empty->bundle(), 'entity_test_label');
53     $this->assertIdentical($empty->id(), NULL);
54     $this->assertTrue($empty->uuid());
55     $this->assertIdentical($empty->label(), NULL);
56
57     // Verify Entity properties/methods on the newly created empty entity.
58     $this->assertIdentical($empty->getEntityTypeId(), 'entity_test_label');
59     // The URI can only be checked after saving.
60     try {
61       $empty->urlInfo();
62       $this->fail('EntityMalformedException was thrown.');
63     }
64     catch (EntityMalformedException $e) {
65       $this->pass('EntityMalformedException was thrown.');
66     }
67
68     // Verify that an empty entity cannot be saved.
69     try {
70       $empty->save();
71       $this->fail('EntityMalformedException was thrown.');
72     }
73     catch (EntityMalformedException $e) {
74       $this->pass('EntityMalformedException was thrown.');
75     }
76
77     // Verify that an entity with an empty ID string is considered empty, too.
78     $empty_id = EntityTestLabel::create([
79       'id' => '',
80     ]);
81     $this->assertIdentical($empty_id->isNew(), TRUE);
82     try {
83       $empty_id->save();
84       $this->fail('EntityMalformedException was thrown.');
85     }
86     catch (EntityMalformedException $e) {
87       $this->pass('EntityMalformedException was thrown.');
88     }
89
90     // Verify properties on a newly created entity.
91     $entity_test = EntityTestLabel::create($expected = [
92       'id' => $this->randomMachineName(),
93       'name' => $this->randomString(),
94     ]);
95     $this->assertIdentical($entity_test->id->value, $expected['id']);
96     $this->assertTrue($entity_test->uuid->value);
97     $this->assertNotEqual($entity_test->uuid->value, $empty->uuid->value);
98     $this->assertIdentical($entity_test->name->value, $expected['name']);
99     $this->assertIdentical($entity_test->langcode->value, $default_langcode);
100
101     // Verify methods on the newly created entity.
102     $this->assertIdentical($entity_test->isNew(), TRUE);
103     $this->assertIdentical($entity_test->id(), $expected['id']);
104     $this->assertTrue($entity_test->uuid());
105     $expected['uuid'] = $entity_test->uuid();
106     $this->assertIdentical($entity_test->label(), $expected['name']);
107
108     // Verify that the entity can be saved.
109     try {
110       $status = $entity_test->save();
111       $this->pass('EntityMalformedException was not thrown.');
112     }
113     catch (EntityMalformedException $e) {
114       $this->fail('EntityMalformedException was not thrown.');
115     }
116
117     // Verify that hasData() returns the expected result.
118     $this->assertTrue($storage->hasData());
119
120     // Verify that the correct status is returned and properties did not change.
121     $this->assertIdentical($status, SAVED_NEW);
122     $this->assertIdentical($entity_test->id(), $expected['id']);
123     $this->assertIdentical($entity_test->uuid(), $expected['uuid']);
124     $this->assertIdentical($entity_test->label(), $expected['name']);
125     $this->assertIdentical($entity_test->isNew(), FALSE);
126
127     // Save again, and verify correct status and properties again.
128     $status = $entity_test->save();
129     $this->assertIdentical($status, SAVED_UPDATED);
130     $this->assertIdentical($entity_test->id(), $expected['id']);
131     $this->assertIdentical($entity_test->uuid(), $expected['uuid']);
132     $this->assertIdentical($entity_test->label(), $expected['name']);
133     $this->assertIdentical($entity_test->isNew(), FALSE);
134
135     // Ensure that creating an entity with the same id as an existing one is not
136     // possible.
137     $same_id = EntityTestLabel::create([
138       'id' => $entity_test->id(),
139     ]);
140     $this->assertIdentical($same_id->isNew(), TRUE);
141     try {
142       $same_id->save();
143       $this->fail('Not possible to overwrite an entity entity.');
144     }
145     catch (EntityStorageException $e) {
146       $this->pass('Not possible to overwrite an entity entity.');
147     }
148
149     // Verify that renaming the ID returns correct status and properties.
150     $ids = [$expected['id'], 'second_' . $this->randomMachineName(4), 'third_' . $this->randomMachineName(4)];
151     for ($i = 1; $i < 3; $i++) {
152       $old_id = $ids[$i - 1];
153       $new_id = $ids[$i];
154       // Before renaming, everything should point to the current ID.
155       $this->assertIdentical($entity_test->id(), $old_id);
156
157       // Rename.
158       $entity_test->id = $new_id;
159       $this->assertIdentical($entity_test->id(), $new_id);
160       $status = $entity_test->save();
161       $this->assertIdentical($status, SAVED_UPDATED);
162       $this->assertIdentical($entity_test->isNew(), FALSE);
163
164       // Verify that originalID points to new ID directly after renaming.
165       $this->assertIdentical($entity_test->id(), $new_id);
166     }
167   }
168
169   /**
170    * Tests uninstallation of a module that does not use the SQL entity storage.
171    */
172   public function testUninstall() {
173     $uninstall_validator_reasons = \Drupal::service('content_uninstall_validator')->validate('keyvalue_test');
174     $this->assertEmpty($uninstall_validator_reasons);
175   }
176
177 }