comparison core/modules/config/tests/src/Functional/ConfigEntityTest.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
33 * Tests CRUD operations. 33 * Tests CRUD operations.
34 */ 34 */
35 public function testCRUD() { 35 public function testCRUD() {
36 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId(); 36 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
37 // Verify default properties on a newly created empty entity. 37 // Verify default properties on a newly created empty entity.
38 $empty = entity_create('config_test'); 38 $storage = \Drupal::entityTypeManager()->getStorage('config_test');
39 $empty = $storage->create();
39 $this->assertTrue($empty->uuid()); 40 $this->assertTrue($empty->uuid());
40 $this->assertIdentical($empty->label, NULL); 41 $this->assertIdentical($empty->label, NULL);
41 $this->assertIdentical($empty->style, NULL); 42 $this->assertIdentical($empty->style, NULL);
42 $this->assertIdentical($empty->language()->getId(), $default_langcode); 43 $this->assertIdentical($empty->language()->getId(), $default_langcode);
43 44
74 catch (EntityMalformedException $e) { 75 catch (EntityMalformedException $e) {
75 $this->pass('EntityMalformedException was thrown.'); 76 $this->pass('EntityMalformedException was thrown.');
76 } 77 }
77 78
78 // Verify that an entity with an empty ID string is considered empty, too. 79 // Verify that an entity with an empty ID string is considered empty, too.
79 $empty_id = entity_create('config_test', [ 80 $empty_id = $storage->create([
80 'id' => '', 81 'id' => '',
81 ]); 82 ]);
82 $this->assertIdentical($empty_id->isNew(), TRUE); 83 $this->assertIdentical($empty_id->isNew(), TRUE);
83 try { 84 try {
84 $empty_id->save(); 85 $empty_id->save();
87 catch (EntityMalformedException $e) { 88 catch (EntityMalformedException $e) {
88 $this->pass('EntityMalformedException was thrown.'); 89 $this->pass('EntityMalformedException was thrown.');
89 } 90 }
90 91
91 // Verify properties on a newly created entity. 92 // Verify properties on a newly created entity.
92 $config_test = entity_create('config_test', $expected = [ 93 $config_test = $storage->create($expected = [
93 'id' => $this->randomMachineName(), 94 'id' => $this->randomMachineName(),
94 'label' => $this->randomString(), 95 'label' => $this->randomString(),
95 'style' => $this->randomMachineName(), 96 'style' => $this->randomMachineName(),
96 ]); 97 ]);
97 $this->assertTrue($config_test->uuid()); 98 $this->assertTrue($config_test->uuid());
139 140
140 // Verify that a configuration entity can be saved with an ID of the 141 // Verify that a configuration entity can be saved with an ID of the
141 // maximum allowed length, but not longer. 142 // maximum allowed length, but not longer.
142 143
143 // Test with a short ID. 144 // Test with a short ID.
144 $id_length_config_test = entity_create('config_test', [ 145 $id_length_config_test = $storage->create([
145 'id' => $this->randomMachineName(8), 146 'id' => $this->randomMachineName(8),
146 ]); 147 ]);
147 try { 148 try {
148 $id_length_config_test->save(); 149 $id_length_config_test->save();
149 $this->pass(SafeMarkup::format("config_test entity with ID length @length was saved.", [ 150 $this->pass(SafeMarkup::format("config_test entity with ID length @length was saved.", [
153 catch (ConfigEntityIdLengthException $e) { 154 catch (ConfigEntityIdLengthException $e) {
154 $this->fail($e->getMessage()); 155 $this->fail($e->getMessage());
155 } 156 }
156 157
157 // Test with an ID of the maximum allowed length. 158 // Test with an ID of the maximum allowed length.
158 $id_length_config_test = entity_create('config_test', [ 159 $id_length_config_test = $storage->create([
159 'id' => $this->randomMachineName(static::MAX_ID_LENGTH), 160 'id' => $this->randomMachineName(static::MAX_ID_LENGTH),
160 ]); 161 ]);
161 try { 162 try {
162 $id_length_config_test->save(); 163 $id_length_config_test->save();
163 $this->pass(SafeMarkup::format("config_test entity with ID length @length was saved.", [ 164 $this->pass(SafeMarkup::format("config_test entity with ID length @length was saved.", [
167 catch (ConfigEntityIdLengthException $e) { 168 catch (ConfigEntityIdLengthException $e) {
168 $this->fail($e->getMessage()); 169 $this->fail($e->getMessage());
169 } 170 }
170 171
171 // Test with an ID exceeding the maximum allowed length. 172 // Test with an ID exceeding the maximum allowed length.
172 $id_length_config_test = entity_create('config_test', [ 173 $id_length_config_test = $storage->create([
173 'id' => $this->randomMachineName(static::MAX_ID_LENGTH + 1), 174 'id' => $this->randomMachineName(static::MAX_ID_LENGTH + 1),
174 ]); 175 ]);
175 try { 176 try {
176 $status = $id_length_config_test->save(); 177 $status = $id_length_config_test->save();
177 $this->fail(SafeMarkup::format("config_test entity with ID length @length exceeding the maximum allowed length of @max saved successfully", [ 178 $this->fail(SafeMarkup::format("config_test entity with ID length @length exceeding the maximum allowed length of @max saved successfully", [
186 ])); 187 ]));
187 } 188 }
188 189
189 // Ensure that creating an entity with the same id as an existing one is not 190 // Ensure that creating an entity with the same id as an existing one is not
190 // possible. 191 // possible.
191 $same_id = entity_create('config_test', [ 192 $same_id = $storage->create([
192 'id' => $config_test->id(), 193 'id' => $config_test->id(),
193 ]); 194 ]);
194 $this->assertIdentical($same_id->isNew(), TRUE); 195 $this->assertIdentical($same_id->isNew(), TRUE);
195 try { 196 try {
196 $same_id->save(); 197 $same_id->save();
221 $this->assertIdentical($config_test->getOriginalId(), $new_id); 222 $this->assertIdentical($config_test->getOriginalId(), $new_id);
222 } 223 }
223 224
224 // Test config entity prepopulation. 225 // Test config entity prepopulation.
225 \Drupal::state()->set('config_test.prepopulate', TRUE); 226 \Drupal::state()->set('config_test.prepopulate', TRUE);
226 $config_test = entity_create('config_test', ['foo' => 'bar']); 227 $config_test = $storage->create(['foo' => 'bar']);
227 $this->assertEqual($config_test->get('foo'), 'baz', 'Initial value correctly populated'); 228 $this->assertEqual($config_test->get('foo'), 'baz', 'Initial value correctly populated');
228 } 229 }
229 230
230 /** 231 /**
231 * Tests CRUD operations through the UI. 232 * Tests CRUD operations through the UI.