3 namespace Drupal\KernelTests\Core\Config;
5 use Drupal\KernelTests\KernelTestBase;
8 * Unit tests for configuration entity base methods.
12 class ConfigEntityUnitTest extends KernelTestBase {
15 * Exempt from strict schema checking.
17 * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
21 protected $strictConfigSchema = FALSE;
28 public static $modules = ['config_test'];
31 * The config_test entity storage.
33 * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
40 protected function setUp() {
42 $this->storage = $this->container->get('entity.manager')->getStorage('config_test');
46 * Tests storage methods.
48 public function testStorageMethods() {
49 $entity_type = \Drupal::entityManager()->getDefinition('config_test');
51 // Test the static extractID() method.
52 $expected_id = 'test_id';
53 $config_name = $entity_type->getConfigPrefix() . '.' . $expected_id;
54 $storage = $this->storage;
55 $this->assertIdentical($storage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix()), $expected_id);
57 // Create three entities, two with the same style.
58 $style = $this->randomMachineName(8);
59 for ($i = 0; $i < 2; $i++) {
60 $entity = $this->storage->create([
61 'id' => $this->randomMachineName(),
62 'label' => $this->randomString(),
67 $entity = $this->storage->create([
68 'id' => $this->randomMachineName(),
69 'label' => $this->randomString(),
70 // Use a different length for the entity to ensure uniqueness.
71 'style' => $this->randomMachineName(9),
75 // Ensure that the configuration entity can be loaded by UUID.
76 $entity_loaded_by_uuid = \Drupal::entityManager()->loadEntityByUuid($entity_type->id(), $entity->uuid());
77 if (!$entity_loaded_by_uuid) {
78 $this->fail(sprintf("Failed to load '%s' entity ID '%s' by UUID '%s'.", $entity_type->id(), $entity->id(), $entity->uuid()));
80 // Compare UUIDs as the objects are not identical since
81 // $entity->enforceIsNew is FALSE and $entity_loaded_by_uuid->enforceIsNew
83 $this->assertSame($entity->uuid(), $entity_loaded_by_uuid->uuid());
85 $entities = $this->storage->loadByProperties();
86 $this->assertEqual(count($entities), 3, 'Three entities are loaded when no properties are specified.');
88 $entities = $this->storage->loadByProperties(['style' => $style]);
89 $this->assertEqual(count($entities), 2, 'Two entities are loaded when the style property is specified.');
91 // Assert that both returned entities have a matching style property.
92 foreach ($entities as $entity) {
93 $this->assertIdentical($entity->get('style'), $style, 'The loaded entity has the correct style value specified.');
96 // Test that schema type enforcement can be overridden by trusting the data.
97 $entity = $this->storage->create([
98 'id' => $this->randomMachineName(),
99 'label' => $this->randomString(),
103 $this->assertSame('999', $entity->style);
104 $entity->style = 999;
105 $entity->trustData()->save();
106 $this->assertSame(999, $entity->style);
108 $this->assertSame('999', $entity->style);