ConfigEntityUnitTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\KernelTests\KernelTestBase;
  4. /**
  5. * Unit tests for configuration entity base methods.
  6. *
  7. * @group config
  8. */
  9. class ConfigEntityUnitTest extends KernelTestBase {
  10. /**
  11. * Exempt from strict schema checking.
  12. *
  13. * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
  14. *
  15. * @var bool
  16. */
  17. protected $strictConfigSchema = FALSE;
  18. /**
  19. * Modules to enable.
  20. *
  21. * @var array
  22. */
  23. public static $modules = ['config_test'];
  24. /**
  25. * The config_test entity storage.
  26. *
  27. * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
  28. */
  29. protected $storage;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function setUp() {
  34. parent::setUp();
  35. $this->storage = $this->container->get('entity.manager')->getStorage('config_test');
  36. }
  37. /**
  38. * Tests storage methods.
  39. */
  40. public function testStorageMethods() {
  41. $entity_type = \Drupal::entityManager()->getDefinition('config_test');
  42. // Test the static extractID() method.
  43. $expected_id = 'test_id';
  44. $config_name = $entity_type->getConfigPrefix() . '.' . $expected_id;
  45. $storage = $this->storage;
  46. $this->assertIdentical($storage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix()), $expected_id);
  47. // Create three entities, two with the same style.
  48. $style = $this->randomMachineName(8);
  49. for ($i = 0; $i < 2; $i++) {
  50. $entity = $this->storage->create([
  51. 'id' => $this->randomMachineName(),
  52. 'label' => $this->randomString(),
  53. 'style' => $style,
  54. ]);
  55. $entity->save();
  56. }
  57. $entity = $this->storage->create([
  58. 'id' => $this->randomMachineName(),
  59. 'label' => $this->randomString(),
  60. // Use a different length for the entity to ensure uniqueness.
  61. 'style' => $this->randomMachineName(9),
  62. ]);
  63. $entity->save();
  64. // Ensure that the configuration entity can be loaded by UUID.
  65. $entity_loaded_by_uuid = \Drupal::entityManager()->loadEntityByUuid($entity_type->id(), $entity->uuid());
  66. if (!$entity_loaded_by_uuid) {
  67. $this->fail(sprintf("Failed to load '%s' entity ID '%s' by UUID '%s'.", $entity_type->id(), $entity->id(), $entity->uuid()));
  68. }
  69. // Compare UUIDs as the objects are not identical since
  70. // $entity->enforceIsNew is FALSE and $entity_loaded_by_uuid->enforceIsNew
  71. // is NULL.
  72. $this->assertSame($entity->uuid(), $entity_loaded_by_uuid->uuid());
  73. $entities = $this->storage->loadByProperties();
  74. $this->assertEqual(count($entities), 3, 'Three entities are loaded when no properties are specified.');
  75. $entities = $this->storage->loadByProperties(['style' => $style]);
  76. $this->assertEqual(count($entities), 2, 'Two entities are loaded when the style property is specified.');
  77. // Assert that both returned entities have a matching style property.
  78. foreach ($entities as $entity) {
  79. $this->assertIdentical($entity->get('style'), $style, 'The loaded entity has the correct style value specified.');
  80. }
  81. // Test that schema type enforcement can be overridden by trusting the data.
  82. $entity = $this->storage->create([
  83. 'id' => $this->randomMachineName(),
  84. 'label' => $this->randomString(),
  85. 'style' => 999,
  86. ]);
  87. $entity->save();
  88. $this->assertSame('999', $entity->style);
  89. $entity->style = 999;
  90. $entity->trustData()->save();
  91. $this->assertSame(999, $entity->style);
  92. $entity->save();
  93. $this->assertSame('999', $entity->style);
  94. }
  95. }