annotate core/modules/config/tests/src/Functional/ConfigEntityTest.php @ 19:fa3358dc1485 tip

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