Mercurial > hg > isophonics-drupal-site
comparison core/modules/config/tests/src/Functional/ConfigEntityTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\config\Functional; | |
4 | |
5 use Drupal\Component\Utility\SafeMarkup; | |
6 use Drupal\Core\Entity\EntityMalformedException; | |
7 use Drupal\Core\Entity\EntityStorageException; | |
8 use Drupal\Core\Config\Entity\ConfigEntityStorage; | |
9 use Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException; | |
10 use Drupal\Core\Url; | |
11 use Drupal\Tests\BrowserTestBase; | |
12 | |
13 /** | |
14 * Tests configuration entities. | |
15 * | |
16 * @group config | |
17 */ | |
18 class ConfigEntityTest extends BrowserTestBase { | |
19 | |
20 /** | |
21 * The maximum length for the entity storage used in this test. | |
22 */ | |
23 const MAX_ID_LENGTH = ConfigEntityStorage::MAX_ID_LENGTH; | |
24 | |
25 /** | |
26 * Modules to enable. | |
27 * | |
28 * @var array | |
29 */ | |
30 public static $modules = ['config_test']; | |
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 = entity_create('config_test'); | |
39 $this->assertTrue($empty->uuid()); | |
40 $this->assertIdentical($empty->label, NULL); | |
41 $this->assertIdentical($empty->style, NULL); | |
42 $this->assertIdentical($empty->language()->getId(), $default_langcode); | |
43 | |
44 // Verify ConfigEntity properties/methods on the newly created empty entity. | |
45 $this->assertIdentical($empty->isNew(), TRUE); | |
46 $this->assertIdentical($empty->getOriginalId(), NULL); | |
47 $this->assertIdentical($empty->bundle(), 'config_test'); | |
48 $this->assertIdentical($empty->id(), NULL); | |
49 $this->assertTrue($empty->uuid()); | |
50 $this->assertIdentical($empty->label(), NULL); | |
51 | |
52 $this->assertIdentical($empty->get('id'), NULL); | |
53 $this->assertTrue($empty->get('uuid')); | |
54 $this->assertIdentical($empty->get('label'), NULL); | |
55 $this->assertIdentical($empty->get('style'), NULL); | |
56 $this->assertIdentical($empty->language()->getId(), $default_langcode); | |
57 | |
58 // Verify Entity properties/methods on the newly created empty entity. | |
59 $this->assertIdentical($empty->getEntityTypeId(), 'config_test'); | |
60 // The URI can only be checked after saving. | |
61 try { | |
62 $empty->urlInfo(); | |
63 $this->fail('EntityMalformedException was thrown.'); | |
64 } | |
65 catch (EntityMalformedException $e) { | |
66 $this->pass('EntityMalformedException was thrown.'); | |
67 } | |
68 | |
69 // Verify that an empty entity cannot be saved. | |
70 try { | |
71 $empty->save(); | |
72 $this->fail('EntityMalformedException was thrown.'); | |
73 } | |
74 catch (EntityMalformedException $e) { | |
75 $this->pass('EntityMalformedException was thrown.'); | |
76 } | |
77 | |
78 // Verify that an entity with an empty ID string is considered empty, too. | |
79 $empty_id = entity_create('config_test', [ | |
80 'id' => '', | |
81 ]); | |
82 $this->assertIdentical($empty_id->isNew(), TRUE); | |
83 try { | |
84 $empty_id->save(); | |
85 $this->fail('EntityMalformedException was thrown.'); | |
86 } | |
87 catch (EntityMalformedException $e) { | |
88 $this->pass('EntityMalformedException was thrown.'); | |
89 } | |
90 | |
91 // Verify properties on a newly created entity. | |
92 $config_test = entity_create('config_test', $expected = [ | |
93 'id' => $this->randomMachineName(), | |
94 'label' => $this->randomString(), | |
95 'style' => $this->randomMachineName(), | |
96 ]); | |
97 $this->assertTrue($config_test->uuid()); | |
98 $this->assertNotEqual($config_test->uuid(), $empty->uuid()); | |
99 $this->assertIdentical($config_test->label, $expected['label']); | |
100 $this->assertIdentical($config_test->style, $expected['style']); | |
101 $this->assertIdentical($config_test->language()->getId(), $default_langcode); | |
102 | |
103 // Verify methods on the newly created entity. | |
104 $this->assertIdentical($config_test->isNew(), TRUE); | |
105 $this->assertIdentical($config_test->getOriginalId(), $expected['id']); | |
106 $this->assertIdentical($config_test->id(), $expected['id']); | |
107 $this->assertTrue($config_test->uuid()); | |
108 $expected['uuid'] = $config_test->uuid(); | |
109 $this->assertIdentical($config_test->label(), $expected['label']); | |
110 | |
111 // Verify that the entity can be saved. | |
112 try { | |
113 $status = $config_test->save(); | |
114 $this->pass('EntityMalformedException was not thrown.'); | |
115 } | |
116 catch (EntityMalformedException $e) { | |
117 $this->fail('EntityMalformedException was not thrown.'); | |
118 } | |
119 | |
120 // The entity path can only be checked after saving. | |
121 $this->assertIdentical($config_test->url(), Url::fromRoute('entity.config_test.edit_form', ['config_test' => $expected['id']])->toString()); | |
122 | |
123 // Verify that the correct status is returned and properties did not change. | |
124 $this->assertIdentical($status, SAVED_NEW); | |
125 $this->assertIdentical($config_test->id(), $expected['id']); | |
126 $this->assertIdentical($config_test->uuid(), $expected['uuid']); | |
127 $this->assertIdentical($config_test->label(), $expected['label']); | |
128 $this->assertIdentical($config_test->isNew(), FALSE); | |
129 $this->assertIdentical($config_test->getOriginalId(), $expected['id']); | |
130 | |
131 // Save again, and verify correct status and properties again. | |
132 $status = $config_test->save(); | |
133 $this->assertIdentical($status, SAVED_UPDATED); | |
134 $this->assertIdentical($config_test->id(), $expected['id']); | |
135 $this->assertIdentical($config_test->uuid(), $expected['uuid']); | |
136 $this->assertIdentical($config_test->label(), $expected['label']); | |
137 $this->assertIdentical($config_test->isNew(), FALSE); | |
138 $this->assertIdentical($config_test->getOriginalId(), $expected['id']); | |
139 | |
140 // Verify that a configuration entity can be saved with an ID of the | |
141 // maximum allowed length, but not longer. | |
142 | |
143 // Test with a short ID. | |
144 $id_length_config_test = entity_create('config_test', [ | |
145 'id' => $this->randomMachineName(8), | |
146 ]); | |
147 try { | |
148 $id_length_config_test->save(); | |
149 $this->pass(SafeMarkup::format("config_test entity with ID length @length was saved.", [ | |
150 '@length' => strlen($id_length_config_test->id()), | |
151 ])); | |
152 } | |
153 catch (ConfigEntityIdLengthException $e) { | |
154 $this->fail($e->getMessage()); | |
155 } | |
156 | |
157 // Test with an ID of the maximum allowed length. | |
158 $id_length_config_test = entity_create('config_test', [ | |
159 'id' => $this->randomMachineName(static::MAX_ID_LENGTH), | |
160 ]); | |
161 try { | |
162 $id_length_config_test->save(); | |
163 $this->pass(SafeMarkup::format("config_test entity with ID length @length was saved.", [ | |
164 '@length' => strlen($id_length_config_test->id()), | |
165 ])); | |
166 } | |
167 catch (ConfigEntityIdLengthException $e) { | |
168 $this->fail($e->getMessage()); | |
169 } | |
170 | |
171 // Test with an ID exceeding the maximum allowed length. | |
172 $id_length_config_test = entity_create('config_test', [ | |
173 'id' => $this->randomMachineName(static::MAX_ID_LENGTH + 1), | |
174 ]); | |
175 try { | |
176 $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 '@length' => strlen($id_length_config_test->id()), | |
179 '@max' => static::MAX_ID_LENGTH, | |
180 ])); | |
181 } | |
182 catch (ConfigEntityIdLengthException $e) { | |
183 $this->pass(SafeMarkup::format("config_test entity with ID length @length exceeding the maximum allowed length of @max failed to save", [ | |
184 '@length' => strlen($id_length_config_test->id()), | |
185 '@max' => static::MAX_ID_LENGTH, | |
186 ])); | |
187 } | |
188 | |
189 // Ensure that creating an entity with the same id as an existing one is not | |
190 // possible. | |
191 $same_id = entity_create('config_test', [ | |
192 'id' => $config_test->id(), | |
193 ]); | |
194 $this->assertIdentical($same_id->isNew(), TRUE); | |
195 try { | |
196 $same_id->save(); | |
197 $this->fail('Not possible to overwrite an entity entity.'); | |
198 } | |
199 catch (EntityStorageException $e) { | |
200 $this->pass('Not possible to overwrite an entity entity.'); | |
201 } | |
202 | |
203 // Verify that renaming the ID returns correct status and properties. | |
204 $ids = [$expected['id'], 'second_' . $this->randomMachineName(4), 'third_' . $this->randomMachineName(4)]; | |
205 for ($i = 1; $i < 3; $i++) { | |
206 $old_id = $ids[$i - 1]; | |
207 $new_id = $ids[$i]; | |
208 // Before renaming, everything should point to the current ID. | |
209 $this->assertIdentical($config_test->id(), $old_id); | |
210 $this->assertIdentical($config_test->getOriginalId(), $old_id); | |
211 | |
212 // Rename. | |
213 $config_test->set('id', $new_id); | |
214 $this->assertIdentical($config_test->id(), $new_id); | |
215 $status = $config_test->save(); | |
216 $this->assertIdentical($status, SAVED_UPDATED); | |
217 $this->assertIdentical($config_test->isNew(), FALSE); | |
218 | |
219 // Verify that originalID points to new ID directly after renaming. | |
220 $this->assertIdentical($config_test->id(), $new_id); | |
221 $this->assertIdentical($config_test->getOriginalId(), $new_id); | |
222 } | |
223 | |
224 // Test config entity prepopulation. | |
225 \Drupal::state()->set('config_test.prepopulate', TRUE); | |
226 $config_test = entity_create('config_test', ['foo' => 'bar']); | |
227 $this->assertEqual($config_test->get('foo'), 'baz', 'Initial value correctly populated'); | |
228 } | |
229 | |
230 /** | |
231 * Tests CRUD operations through the UI. | |
232 */ | |
233 public function testCRUDUI() { | |
234 $this->drupalLogin($this->drupalCreateUser(['administer site configuration'])); | |
235 | |
236 $id = strtolower($this->randomMachineName()); | |
237 $label1 = $this->randomMachineName(); | |
238 $label2 = $this->randomMachineName(); | |
239 $label3 = $this->randomMachineName(); | |
240 $message_insert = format_string('%label configuration has been created.', ['%label' => $label1]); | |
241 $message_update = format_string('%label configuration has been updated.', ['%label' => $label2]); | |
242 $message_delete = format_string('The test configuration %label has been deleted.', ['%label' => $label2]); | |
243 | |
244 // Create a configuration entity. | |
245 $edit = [ | |
246 'id' => $id, | |
247 'label' => $label1, | |
248 ]; | |
249 $this->drupalPostForm('admin/structure/config_test/add', $edit, 'Save'); | |
250 $this->assertUrl('admin/structure/config_test'); | |
251 $this->assertResponse(200); | |
252 $this->assertRaw($message_insert); | |
253 $this->assertNoRaw($message_update); | |
254 $this->assertLinkByHref("admin/structure/config_test/manage/$id"); | |
255 | |
256 // Update the configuration entity. | |
257 $edit = [ | |
258 'label' => $label2, | |
259 ]; | |
260 $this->drupalPostForm("admin/structure/config_test/manage/$id", $edit, 'Save'); | |
261 $this->assertUrl('admin/structure/config_test'); | |
262 $this->assertResponse(200); | |
263 $this->assertNoRaw($message_insert); | |
264 $this->assertRaw($message_update); | |
265 $this->assertLinkByHref("admin/structure/config_test/manage/$id"); | |
266 $this->assertLinkByHref("admin/structure/config_test/manage/$id/delete"); | |
267 | |
268 // Delete the configuration entity. | |
269 $this->drupalGet("admin/structure/config_test/manage/$id"); | |
270 $this->clickLink(t('Delete')); | |
271 $this->assertUrl("admin/structure/config_test/manage/$id/delete"); | |
272 $this->drupalPostForm(NULL, [], 'Delete'); | |
273 $this->assertUrl('admin/structure/config_test'); | |
274 $this->assertResponse(200); | |
275 $this->assertNoRaw($message_update); | |
276 $this->assertRaw($message_delete); | |
277 $this->assertNoText($label1); | |
278 $this->assertNoLinkByHref("admin/structure/config_test/manage/$id"); | |
279 | |
280 // Re-create a configuration entity. | |
281 $edit = [ | |
282 'id' => $id, | |
283 'label' => $label1, | |
284 ]; | |
285 $this->drupalPostForm('admin/structure/config_test/add', $edit, 'Save'); | |
286 $this->assertUrl('admin/structure/config_test'); | |
287 $this->assertResponse(200); | |
288 $this->assertText($label1); | |
289 $this->assertLinkByHref("admin/structure/config_test/manage/$id"); | |
290 | |
291 // Rename the configuration entity's ID/machine name. | |
292 $edit = [ | |
293 'id' => strtolower($this->randomMachineName()), | |
294 'label' => $label3, | |
295 ]; | |
296 $this->drupalPostForm("admin/structure/config_test/manage/$id", $edit, 'Save'); | |
297 $this->assertUrl('admin/structure/config_test'); | |
298 $this->assertResponse(200); | |
299 $this->assertNoText($label1); | |
300 $this->assertNoText($label2); | |
301 $this->assertText($label3); | |
302 $this->assertNoLinkByHref("admin/structure/config_test/manage/$id"); | |
303 $id = $edit['id']; | |
304 $this->assertLinkByHref("admin/structure/config_test/manage/$id"); | |
305 | |
306 // Create a configuration entity with '0' machine name. | |
307 $edit = [ | |
308 'id' => '0', | |
309 'label' => '0', | |
310 ]; | |
311 $this->drupalPostForm('admin/structure/config_test/add', $edit, 'Save'); | |
312 $this->assertResponse(200); | |
313 $message_insert = format_string('%label configuration has been created.', ['%label' => $edit['label']]); | |
314 $this->assertRaw($message_insert); | |
315 $this->assertLinkByHref('admin/structure/config_test/manage/0'); | |
316 $this->assertLinkByHref('admin/structure/config_test/manage/0/delete'); | |
317 $this->drupalPostForm('admin/structure/config_test/manage/0/delete', [], 'Delete'); | |
318 $this->assertFalse(entity_load('config_test', '0'), 'Test entity deleted'); | |
319 | |
320 // Create a configuration entity with a property that uses AJAX to show | |
321 // extra form elements. Test this scenario in a non-JS case by using a | |
322 // 'js-hidden' submit button. | |
323 // @see \Drupal\Tests\config\FunctionalJavascript\ConfigEntityTest::testAjaxOnAddPage() | |
324 $this->drupalGet('admin/structure/config_test/add'); | |
325 | |
326 $id = strtolower($this->randomMachineName()); | |
327 $edit = [ | |
328 'id' => $id, | |
329 'label' => $this->randomString(), | |
330 'size' => 'custom', | |
331 ]; | |
332 | |
333 $this->assertFieldByName('size'); | |
334 $this->assertNoFieldByName('size_value'); | |
335 | |
336 $this->drupalPostForm(NULL, $edit, 'Change size'); | |
337 $this->assertFieldByName('size'); | |
338 $this->assertFieldByName('size_value'); | |
339 | |
340 // Submit the form with the regular 'Save' button and check that the entity | |
341 // values are correct. | |
342 $edit += ['size_value' => 'medium']; | |
343 $this->drupalPostForm(NULL, $edit, 'Save'); | |
344 | |
345 $entity = entity_load('config_test', $id); | |
346 $this->assertEqual($entity->get('size'), 'custom'); | |
347 $this->assertEqual($entity->get('size_value'), 'medium'); | |
348 } | |
349 | |
350 } |