807152bfedd6d59daace7cbe5f81e44d41df3b4a
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Config / ConfigEntityUnitTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Config;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Unit tests for configuration entity base methods.
9  *
10  * @group config
11  */
12 class ConfigEntityUnitTest extends KernelTestBase {
13
14   /**
15    * Exempt from strict schema checking.
16    *
17    * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
18    *
19    * @var bool
20    */
21   protected $strictConfigSchema = FALSE;
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = ['config_test'];
29
30   /**
31    * The config_test entity storage.
32    *
33    * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
34    */
35   protected $storage;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42     $this->storage = $this->container->get('entity.manager')->getStorage('config_test');
43   }
44
45   /**
46    * Tests storage methods.
47    */
48   public function testStorageMethods() {
49     $entity_type = \Drupal::entityManager()->getDefinition('config_test');
50
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);
56
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(),
63         'style' => $style,
64       ]);
65       $entity->save();
66     }
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),
72     ]);
73     $entity->save();
74
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()));
79     }
80     // Compare UUIDs as the objects are not identical since
81     // $entity->enforceIsNew is FALSE and $entity_loaded_by_uuid->enforceIsNew
82     // is NULL.
83     $this->assertSame($entity->uuid(), $entity_loaded_by_uuid->uuid());
84
85     $entities = $this->storage->loadByProperties();
86     $this->assertEqual(count($entities), 3, 'Three entities are loaded when no properties are specified.');
87
88     $entities = $this->storage->loadByProperties(['style' => $style]);
89     $this->assertEqual(count($entities), 2, 'Two entities are loaded when the style property is specified.');
90
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.');
94     }
95
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(),
100       'style' => 999
101     ]);
102     $entity->save();
103     $this->assertSame('999', $entity->style);
104     $entity->style = 999;
105     $entity->trustData()->save();
106     $this->assertSame(999, $entity->style);
107     $entity->save();
108     $this->assertSame('999', $entity->style);
109   }
110
111 }