3 namespace Drupal\KernelTests\Core\KeyValueStore;
5 use Drupal\Core\Entity\EntityMalformedException;
6 use Drupal\Core\Entity\EntityStorageException;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\entity_test\Entity\EntityTestLabel;
11 * Tests KeyValueEntityStorage for content entities.
13 * @group KeyValueStore
15 class KeyValueContentEntityStorageTest extends KernelTestBase {
22 public static $modules = ['user', 'entity_test', 'keyvalue_test'];
27 protected function setUp() {
29 $this->installEntitySchema('user');
33 * Tests CRUD operations.
35 * @covers \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::hasData
37 public function testCRUD() {
38 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
40 $storage = \Drupal::entityTypeManager()->getStorage('entity_test_label');
41 $this->assertFalse($storage->hasData());
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);
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);
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.
62 $this->fail('EntityMalformedException was thrown.');
64 catch (EntityMalformedException $e) {
65 $this->pass('EntityMalformedException was thrown.');
68 // Verify that an empty entity cannot be saved.
71 $this->fail('EntityMalformedException was thrown.');
73 catch (EntityMalformedException $e) {
74 $this->pass('EntityMalformedException was thrown.');
77 // Verify that an entity with an empty ID string is considered empty, too.
78 $empty_id = EntityTestLabel::create([
81 $this->assertIdentical($empty_id->isNew(), TRUE);
84 $this->fail('EntityMalformedException was thrown.');
86 catch (EntityMalformedException $e) {
87 $this->pass('EntityMalformedException was thrown.');
90 // Verify properties on a newly created entity.
91 $entity_test = EntityTestLabel::create($expected = [
92 'id' => $this->randomMachineName(),
93 'name' => $this->randomString(),
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);
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']);
108 // Verify that the entity can be saved.
110 $status = $entity_test->save();
111 $this->pass('EntityMalformedException was not thrown.');
113 catch (EntityMalformedException $e) {
114 $this->fail('EntityMalformedException was not thrown.');
117 // Verify that hasData() returns the expected result.
118 $this->assertTrue($storage->hasData());
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);
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);
135 // Ensure that creating an entity with the same id as an existing one is not
137 $same_id = EntityTestLabel::create([
138 'id' => $entity_test->id(),
140 $this->assertIdentical($same_id->isNew(), TRUE);
143 $this->fail('Not possible to overwrite an entity entity.');
145 catch (EntityStorageException $e) {
146 $this->pass('Not possible to overwrite an entity entity.');
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];
154 // Before renaming, everything should point to the current ID.
155 $this->assertIdentical($entity_test->id(), $old_id);
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);
164 // Verify that originalID points to new ID directly after renaming.
165 $this->assertIdentical($entity_test->id(), $new_id);
170 * Tests uninstallation of a module that does not use the SQL entity storage.
172 public function testUninstall() {
173 $uninstall_validator_reasons = \Drupal::service('content_uninstall_validator')->validate('keyvalue_test');
174 $this->assertEmpty($uninstall_validator_reasons);