3 namespace Drupal\KernelTests\Core\Entity;
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;
13 * Tests entity adapter for configuration entities.
15 * @see \Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter
19 * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter
21 class ConfigEntityAdapterTest extends KernelTestBase {
26 public static $modules = ['config_test'];
31 * @var \Drupal\config_test\Entity\ConfigTest
38 protected function setUp() {
40 $this->installConfig(static::$modules);
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([
52 * @covers \Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver::getDerivativeDefinitions
54 public function testEntityDeriver() {
55 $definition = \Drupal::typedDataManager()->getDefinition('entity:config_test');
56 $this->assertEquals(ConfigEntityAdapter::class, $definition['class']);
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([
69 // Set weight to be a string which should not validate.
70 'weight' => 'very heavy',
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());
81 * @covers ::getProperties
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,
97 $properties = ConfigEntityAdapter::createFromEntity($this->entity)->getProperties();
99 foreach ($properties as $key => $property) {
101 $this->assertInstanceOf($expected_properties[$key], $property);
103 $this->assertSame(array_keys($expected_properties), $keys);
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());
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());
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());
133 * @covers ::getString
135 public function testGetString() {
136 $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
137 $this->assertEquals('foobar', $adapter->getString());
141 * @covers ::applyDefaultValue
143 public function testApplyDefaultValue() {
144 $this->setExpectedException(\BadMethodCallException::class, 'Method not supported');
145 $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
146 $adapter->applyDefaultValue();
150 * @covers ::getIterator
152 public function testGetIterator() {
153 $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
154 $iterator = $adapter->getIterator();
155 $fields = iterator_to_array($iterator);
167 'protected_property',
169 $this->assertEquals($expected_fields, array_keys($fields));
170 $this->assertEquals($this->entity->id(), $fields['id']->getValue());
172 $adapter->setValue(NULL);
173 $this->assertEquals(new \ArrayIterator([]), $adapter->getIterator());