ConfigEntityStorageTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Core\Config\ConfigDuplicateUUIDException;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Tests sync and importing config entities with IDs and UUIDs that match
  7. * existing config.
  8. *
  9. * @group config
  10. */
  11. class ConfigEntityStorageTest extends KernelTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['config_test'];
  18. /**
  19. * Tests creating configuration entities with changed UUIDs.
  20. */
  21. public function testUUIDConflict() {
  22. $entity_type = 'config_test';
  23. $id = 'test_1';
  24. // Load the original configuration entity.
  25. $storage = $this->container->get('entity_type.manager')
  26. ->getStorage($entity_type);
  27. $storage->create(['id' => $id])->save();
  28. $entity = $storage->load($id);
  29. $original_properties = $entity->toArray();
  30. // Override with a new UUID and try to save.
  31. $new_uuid = $this->container->get('uuid')->generate();
  32. $entity->set('uuid', $new_uuid);
  33. try {
  34. $entity->save();
  35. $this->fail('Exception thrown when attempting to save a configuration entity with a UUID that does not match the existing UUID.');
  36. }
  37. catch (ConfigDuplicateUUIDException $e) {
  38. $this->pass(format_string('Exception thrown when attempting to save a configuration entity with a UUID that does not match existing data: %e.', ['%e' => $e]));
  39. }
  40. // Ensure that the config entity was not corrupted.
  41. $entity = entity_load('config_test', $entity->id(), TRUE);
  42. $this->assertIdentical($entity->toArray(), $original_properties);
  43. }
  44. /**
  45. * Tests the hasData() method for config entity storage.
  46. *
  47. * @covers \Drupal\Core\Config\Entity\ConfigEntityStorage::hasData
  48. */
  49. public function testHasData() {
  50. $storage = \Drupal::entityTypeManager()->getStorage('config_test');
  51. $this->assertFalse($storage->hasData());
  52. // Add a test config entity and check again.
  53. $storage->create(['id' => $this->randomMachineName()])->save();
  54. $this->assertTrue($storage->hasData());
  55. }
  56. }