3 namespace Drupal\Tests\config\Functional;
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\Tests\BrowserTestBase;
14 * Tests configuration entities.
18 class ConfigEntityTest extends BrowserTestBase {
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 $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);
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);
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);
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.
64 $this->fail('EntityMalformedException was thrown.');
66 catch (EntityMalformedException $e) {
67 $this->pass('EntityMalformedException was thrown.');
70 // Verify that an empty entity cannot be saved.
73 $this->fail('EntityMalformedException was thrown.');
75 catch (EntityMalformedException $e) {
76 $this->pass('EntityMalformedException was thrown.');
79 // Verify that an entity with an empty ID string is considered empty, too.
80 $empty_id = $storage->create([
83 $this->assertIdentical($empty_id->isNew(), TRUE);
86 $this->fail('EntityMalformedException was thrown.');
88 catch (EntityMalformedException $e) {
89 $this->pass('EntityMalformedException was thrown.');
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(),
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);
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']);
112 // Verify that the entity can be saved.
114 $status = $config_test->save();
115 $this->pass('EntityMalformedException was not thrown.');
117 catch (EntityMalformedException $e) {
118 $this->fail('EntityMalformedException was not thrown.');
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());
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']);
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']);
141 // Verify that a configuration entity can be saved with an ID of the
142 // maximum allowed length, but not longer.
144 // Test with a short ID.
145 $id_length_config_test = $storage->create([
146 'id' => $this->randomMachineName(8),
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()),
154 catch (ConfigEntityIdLengthException $e) {
155 $this->fail($e->getMessage());
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),
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()),
168 catch (ConfigEntityIdLengthException $e) {
169 $this->fail($e->getMessage());
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),
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,
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,
190 // Ensure that creating an entity with the same id as an existing one is not
192 $same_id = $storage->create([
193 'id' => $config_test->id(),
195 $this->assertIdentical($same_id->isNew(), TRUE);
198 $this->fail('Not possible to overwrite an entity entity.');
200 catch (EntityStorageException $e) {
201 $this->pass('Not possible to overwrite an entity entity.');
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];
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);
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);
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);
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');
232 * Tests CRUD operations through the UI.
234 public function testCRUDUI() {
235 $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
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]);
245 // Create a configuration entity.
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");
257 // Update the configuration entity.
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");
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");
281 // Re-create a configuration entity.
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");
292 // Rename the configuration entity's ID/machine name.
294 'id' => strtolower($this->randomMachineName()),
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");
305 $this->assertLinkByHref("admin/structure/config_test/manage/$id");
307 // Create a configuration entity with '0' machine name.
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');
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');
327 $id = strtolower($this->randomMachineName());
330 'label' => $this->randomString(),
334 $this->assertFieldByName('size');
335 $this->assertNoFieldByName('size_value');
337 $this->drupalPostForm(NULL, $edit, 'Change size');
338 $this->assertFieldByName('size');
339 $this->assertFieldByName('size_value');
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');
346 $entity = entity_load('config_test', $id);
347 $this->assertEqual($entity->get('size'), 'custom');
348 $this->assertEqual($entity->get('size_value'), 'medium');