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