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 public function testCRUD() {
36 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
37 // Verify default properties on a newly created empty entity.
38 $empty = EntityTestLabel::create();
39 $this->assertIdentical($empty->id->value, NULL);
40 $this->assertIdentical($empty->name->value, NULL);
41 $this->assertTrue($empty->uuid->value);
42 $this->assertIdentical($empty->langcode->value, $default_langcode);
44 // Verify ConfigEntity properties/methods on the newly created empty entity.
45 $this->assertIdentical($empty->isNew(), TRUE);
46 $this->assertIdentical($empty->bundle(), 'entity_test_label');
47 $this->assertIdentical($empty->id(), NULL);
48 $this->assertTrue($empty->uuid());
49 $this->assertIdentical($empty->label(), NULL);
51 // Verify Entity properties/methods on the newly created empty entity.
52 $this->assertIdentical($empty->getEntityTypeId(), 'entity_test_label');
53 // The URI can only be checked after saving.
56 $this->fail('EntityMalformedException was thrown.');
58 catch (EntityMalformedException $e) {
59 $this->pass('EntityMalformedException was thrown.');
62 // Verify that an empty entity cannot be saved.
65 $this->fail('EntityMalformedException was thrown.');
67 catch (EntityMalformedException $e) {
68 $this->pass('EntityMalformedException was thrown.');
71 // Verify that an entity with an empty ID string is considered empty, too.
72 $empty_id = EntityTestLabel::create([
75 $this->assertIdentical($empty_id->isNew(), TRUE);
78 $this->fail('EntityMalformedException was thrown.');
80 catch (EntityMalformedException $e) {
81 $this->pass('EntityMalformedException was thrown.');
84 // Verify properties on a newly created entity.
85 $entity_test = EntityTestLabel::create($expected = [
86 'id' => $this->randomMachineName(),
87 'name' => $this->randomString(),
89 $this->assertIdentical($entity_test->id->value, $expected['id']);
90 $this->assertTrue($entity_test->uuid->value);
91 $this->assertNotEqual($entity_test->uuid->value, $empty->uuid->value);
92 $this->assertIdentical($entity_test->name->value, $expected['name']);
93 $this->assertIdentical($entity_test->langcode->value, $default_langcode);
95 // Verify methods on the newly created entity.
96 $this->assertIdentical($entity_test->isNew(), TRUE);
97 $this->assertIdentical($entity_test->id(), $expected['id']);
98 $this->assertTrue($entity_test->uuid());
99 $expected['uuid'] = $entity_test->uuid();
100 $this->assertIdentical($entity_test->label(), $expected['name']);
102 // Verify that the entity can be saved.
104 $status = $entity_test->save();
105 $this->pass('EntityMalformedException was not thrown.');
107 catch (EntityMalformedException $e) {
108 $this->fail('EntityMalformedException was not thrown.');
111 // Verify that the correct status is returned and properties did not change.
112 $this->assertIdentical($status, SAVED_NEW);
113 $this->assertIdentical($entity_test->id(), $expected['id']);
114 $this->assertIdentical($entity_test->uuid(), $expected['uuid']);
115 $this->assertIdentical($entity_test->label(), $expected['name']);
116 $this->assertIdentical($entity_test->isNew(), FALSE);
118 // Save again, and verify correct status and properties again.
119 $status = $entity_test->save();
120 $this->assertIdentical($status, SAVED_UPDATED);
121 $this->assertIdentical($entity_test->id(), $expected['id']);
122 $this->assertIdentical($entity_test->uuid(), $expected['uuid']);
123 $this->assertIdentical($entity_test->label(), $expected['name']);
124 $this->assertIdentical($entity_test->isNew(), FALSE);
126 // Ensure that creating an entity with the same id as an existing one is not
128 $same_id = EntityTestLabel::create([
129 'id' => $entity_test->id(),
131 $this->assertIdentical($same_id->isNew(), TRUE);
134 $this->fail('Not possible to overwrite an entity entity.');
136 catch (EntityStorageException $e) {
137 $this->pass('Not possible to overwrite an entity entity.');
140 // Verify that renaming the ID returns correct status and properties.
141 $ids = [$expected['id'], 'second_' . $this->randomMachineName(4), 'third_' . $this->randomMachineName(4)];
142 for ($i = 1; $i < 3; $i++) {
143 $old_id = $ids[$i - 1];
145 // Before renaming, everything should point to the current ID.
146 $this->assertIdentical($entity_test->id(), $old_id);
149 $entity_test->id = $new_id;
150 $this->assertIdentical($entity_test->id(), $new_id);
151 $status = $entity_test->save();
152 $this->assertIdentical($status, SAVED_UPDATED);
153 $this->assertIdentical($entity_test->isNew(), FALSE);
155 // Verify that originalID points to new ID directly after renaming.
156 $this->assertIdentical($entity_test->id(), $new_id);