3 namespace Drupal\config\Tests;
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;
11 use Drupal\simpletest\WebTestBase;
14 * Tests configuration entities.
18 class ConfigEntityTest extends WebTestBase {
21 * The maximum length for the entity storage used in this test.
23 const MAX_ID_LENGTH = ConfigEntityStorage::MAX_ID_LENGTH;
30 public static $modules = ['config_test'];
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 = 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);
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);
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);
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.
63 $this->fail('EntityMalformedException was thrown.');
65 catch (EntityMalformedException $e) {
66 $this->pass('EntityMalformedException was thrown.');
69 // Verify that an empty entity cannot be saved.
72 $this->fail('EntityMalformedException was thrown.');
74 catch (EntityMalformedException $e) {
75 $this->pass('EntityMalformedException was thrown.');
78 // Verify that an entity with an empty ID string is considered empty, too.
79 $empty_id = entity_create('config_test', [
82 $this->assertIdentical($empty_id->isNew(), TRUE);
85 $this->fail('EntityMalformedException was thrown.');
87 catch (EntityMalformedException $e) {
88 $this->pass('EntityMalformedException was thrown.');
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(),
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);
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']);
111 // Verify that the entity can be saved.
113 $status = $config_test->save();
114 $this->pass('EntityMalformedException was not thrown.');
116 catch (EntityMalformedException $e) {
117 $this->fail('EntityMalformedException was not thrown.');
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());
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']);
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']);
140 // Verify that a configuration entity can be saved with an ID of the
141 // maximum allowed length, but not longer.
143 // Test with a short ID.
144 $id_length_config_test = entity_create('config_test', [
145 'id' => $this->randomMachineName(8),
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())]
153 catch (ConfigEntityIdLengthException $e) {
154 $this->fail($e->getMessage());
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),
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()),
167 catch (ConfigEntityIdLengthException $e) {
168 $this->fail($e->getMessage());
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),
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,
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,
189 // Ensure that creating an entity with the same id as an existing one is not
191 $same_id = entity_create('config_test', [
192 'id' => $config_test->id(),
194 $this->assertIdentical($same_id->isNew(), TRUE);
197 $this->fail('Not possible to overwrite an entity entity.');
199 catch (EntityStorageException $e) {
200 $this->pass('Not possible to overwrite an entity entity.');
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];
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);
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);
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);
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');
231 * Tests CRUD operations through the UI.
233 public function testCRUDUI() {
234 $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
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]);
244 // Create a configuration entity.
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");
256 // Update the configuration entity.
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");
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");
280 // Re-create a configuration entity.
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");
291 // Rename the configuration entity's ID/machine name.
293 'id' => strtolower($this->randomMachineName()),
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");
304 $this->assertLinkByHref("admin/structure/config_test/manage/$id");
306 // Create a configuration entity with '0' machine name.
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');
320 // Create a configuration entity with a property that uses AJAX to show
321 // extra form elements.
322 $this->drupalGet('admin/structure/config_test/add');
324 // Test that the dependent element is not shown initially.
325 $this->assertFieldByName('size');
326 $this->assertNoFieldByName('size_value');
328 $id = strtolower($this->randomMachineName());
331 'label' => $this->randomString(),
334 $this->drupalPostAjaxForm(NULL, $edit, 'size');
336 // Check that the dependent element is shown after selecting a 'size' value.
337 $this->assertFieldByName('size');
338 $this->assertFieldByName('size_value');
340 // Test the same scenario but it in a non-JS case by using a 'js-hidden'
342 $this->drupalGet('admin/structure/config_test/add');
343 $this->assertFieldByName('size');
344 $this->assertNoFieldByName('size_value');
346 $this->drupalPostForm(NULL, $edit, 'Change size');
347 $this->assertFieldByName('size');
348 $this->assertFieldByName('size_value');
350 // Submit the form with the regular 'Save' button and check that the entity
351 // values are correct.
352 $edit += ['size_value' => 'medium'];
353 $this->drupalPostForm(NULL, $edit, 'Save');
355 $entity = entity_load('config_test', $id);
356 $this->assertEqual($entity->get('size'), 'custom');
357 $this->assertEqual($entity->get('size_value'), 'medium');