af9403c320aa5ad02aeabc41d17c8f88d7d6cbc4
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / ConfigEntityAdapterTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\Core\Config\Schema\Mapping;
6 use Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter;
7 use Drupal\Core\TypedData\Plugin\DataType\BooleanData;
8 use Drupal\Core\TypedData\Plugin\DataType\IntegerData;
9 use Drupal\Core\TypedData\Plugin\DataType\StringData;
10 use Drupal\KernelTests\KernelTestBase;
11
12 /**
13  * Tests entity adapter for configuration entities.
14  *
15  * @see \Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter
16  *
17  * @group Entity
18  *
19  * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter
20  */
21 class ConfigEntityAdapterTest extends KernelTestBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public static $modules = ['config_test'];
27
28   /**
29    * The config entity.
30    *
31    * @var \Drupal\config_test\Entity\ConfigTest
32    */
33   protected $entity;
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40     $this->installConfig(static::$modules);
41
42     // ConfigTest::create doesn't work with the following exception:
43     // "Multiple entity types found for Drupal\config_test\Entity\ConfigTest."
44     $this->entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
45       'id' => 'system',
46       'label' => 'foobar',
47       'weight' => 1,
48     ]);
49   }
50
51   /**
52    * @covers \Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver::getDerivativeDefinitions
53    */
54   public function testEntityDeriver() {
55     $definition = \Drupal::typedDataManager()->getDefinition('entity:config_test');
56     $this->assertEquals(ConfigEntityAdapter::class, $definition['class']);
57   }
58
59   /**
60    * @covers ::validate
61    */
62   public function testValidate() {
63     $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
64     $violations = $adapter->validate();
65     $this->assertEmpty($violations);
66     $this->entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
67       'id' => 'system',
68       'label' => 'foobar',
69       // Set weight to be a string which should not validate.
70       'weight' => 'very heavy',
71     ]);
72     $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
73     $violations = $adapter->validate();
74     $this->assertCount(1, $violations);
75     $violation = $violations->get(0);
76     $this->assertEquals('This value should be of the correct primitive type.', $violation->getMessage());
77     $this->assertEquals('weight', $violation->getPropertyPath());
78   }
79
80   /**
81    * @covers ::getProperties
82    */
83   public function testGetProperties() {
84     $expected_properties = [
85       'uuid' => StringData::class,
86       'langcode' => StringData::class,
87       'status' => BooleanData::class,
88       'dependencies' => Mapping::class,
89       'id' => StringData::class,
90       'label' => StringData::class,
91       'weight' => IntegerData::class,
92       'style' => StringData::class,
93       'size' => StringData::class,
94       'size_value' => StringData::class,
95       'protected_property' => StringData::class,
96     ];
97     $properties = ConfigEntityAdapter::createFromEntity($this->entity)->getProperties();
98     $keys = [];
99     foreach ($properties as $key => $property) {
100       $keys[] = $key;
101       $this->assertInstanceOf($expected_properties[$key], $property);
102     }
103     $this->assertSame(array_keys($expected_properties), $keys);
104   }
105
106   /**
107    * @covers ::getValue
108    */
109   public function testGetValue() {
110     $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
111     $this->assertEquals($this->entity->weight, $adapter->get('weight')->getValue());
112     $this->assertEquals($this->entity->id(), $adapter->get('id')->getValue());
113     $this->assertEquals($this->entity->label, $adapter->get('label')->getValue());
114   }
115
116   /**
117    * @covers ::set
118    */
119   public function testSet() {
120     $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
121     // Get the value via typed data to ensure that the typed representation is
122     // updated correctly when the value is set.
123     $this->assertEquals(1, $adapter->get('weight')->getValue());
124
125     $return = $adapter->set('weight', 2);
126     $this->assertSame($adapter, $return);
127     $this->assertEquals(2, $this->entity->weight);
128     // Ensure the typed data is updated via the set too.
129     $this->assertEquals(2, $adapter->get('weight')->getValue());
130   }
131
132   /**
133    * @covers ::getString
134    */
135   public function testGetString() {
136     $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
137     $this->assertEquals('foobar', $adapter->getString());
138   }
139
140   /**
141    * @covers ::applyDefaultValue
142    */
143   public function testApplyDefaultValue() {
144     $this->setExpectedException(\BadMethodCallException::class, 'Method not supported');
145     $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
146     $adapter->applyDefaultValue();
147   }
148
149   /**
150    * @covers ::getIterator
151    */
152   public function testGetIterator() {
153     $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
154     $iterator = $adapter->getIterator();
155     $fields = iterator_to_array($iterator);
156     $expected_fields = [
157       'uuid',
158       'langcode',
159       'status',
160       'dependencies',
161       'id',
162       'label',
163       'weight',
164       'style',
165       'size',
166       'size_value',
167       'protected_property',
168     ];
169     $this->assertEquals($expected_fields, array_keys($fields));
170     $this->assertEquals($this->entity->id(), $fields['id']->getValue());
171
172     $adapter->setValue(NULL);
173     $this->assertEquals(new \ArrayIterator([]), $adapter->getIterator());
174   }
175
176 }