ConfigEntityAdapterTest.php 5.3 KB

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