Version 1
[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   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);
43
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);
50
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.
54     try {
55       $empty->urlInfo();
56       $this->fail('EntityMalformedException was thrown.');
57     }
58     catch (EntityMalformedException $e) {
59       $this->pass('EntityMalformedException was thrown.');
60     }
61
62     // Verify that an empty entity cannot be saved.
63     try {
64       $empty->save();
65       $this->fail('EntityMalformedException was thrown.');
66     }
67     catch (EntityMalformedException $e) {
68       $this->pass('EntityMalformedException was thrown.');
69     }
70
71     // Verify that an entity with an empty ID string is considered empty, too.
72     $empty_id = EntityTestLabel::create([
73       'id' => '',
74     ]);
75     $this->assertIdentical($empty_id->isNew(), TRUE);
76     try {
77       $empty_id->save();
78       $this->fail('EntityMalformedException was thrown.');
79     }
80     catch (EntityMalformedException $e) {
81       $this->pass('EntityMalformedException was thrown.');
82     }
83
84     // Verify properties on a newly created entity.
85     $entity_test = EntityTestLabel::create($expected = [
86       'id' => $this->randomMachineName(),
87       'name' => $this->randomString(),
88     ]);
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);
94
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']);
101
102     // Verify that the entity can be saved.
103     try {
104       $status = $entity_test->save();
105       $this->pass('EntityMalformedException was not thrown.');
106     }
107     catch (EntityMalformedException $e) {
108       $this->fail('EntityMalformedException was not thrown.');
109     }
110
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);
117
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);
125
126     // Ensure that creating an entity with the same id as an existing one is not
127     // possible.
128     $same_id = EntityTestLabel::create([
129       'id' => $entity_test->id(),
130     ]);
131     $this->assertIdentical($same_id->isNew(), TRUE);
132     try {
133       $same_id->save();
134       $this->fail('Not possible to overwrite an entity entity.');
135     }
136     catch (EntityStorageException $e) {
137       $this->pass('Not possible to overwrite an entity entity.');
138     }
139
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];
144       $new_id = $ids[$i];
145       // Before renaming, everything should point to the current ID.
146       $this->assertIdentical($entity_test->id(), $old_id);
147
148       // Rename.
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);
154
155       // Verify that originalID points to new ID directly after renaming.
156       $this->assertIdentical($entity_test->id(), $new_id);
157     }
158   }
159
160 }