View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Config\Schema\Mapping;
use Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter;
use Drupal\Core\TypedData\Plugin\DataType\BooleanData;
use Drupal\Core\TypedData\Plugin\DataType\IntegerData;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\KernelTests\KernelTestBase;
class ConfigEntityAdapterTest extends KernelTestBase {
protected static $modules = [
'config_test',
];
protected $entity;
protected function setUp() : void {
parent::setUp();
$this
->installConfig(static::$modules);
$this->entity = \Drupal::entityTypeManager()
->getStorage('config_test')
->create([
'id' => 'system',
'label' => 'foobar',
'weight' => 1,
]);
}
public function testEntityDeriver() {
$definition = \Drupal::typedDataManager()
->getDefinition('entity:config_test');
$this
->assertEquals(ConfigEntityAdapter::class, $definition['class']);
}
public function testValidate() {
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
$violations = $adapter
->validate();
$this
->assertEmpty($violations);
$this->entity = \Drupal::entityTypeManager()
->getStorage('config_test')
->create([
'id' => 'system',
'label' => 'foobar',
'weight' => 'very heavy',
]);
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
$violations = $adapter
->validate();
$this
->assertCount(1, $violations);
$violation = $violations
->get(0);
$this
->assertEquals('This value should be of the correct primitive type.', $violation
->getMessage());
$this
->assertEquals('weight', $violation
->getPropertyPath());
}
public function testGetProperties() {
$expected_properties = [
'uuid' => StringData::class,
'langcode' => StringData::class,
'status' => BooleanData::class,
'dependencies' => Mapping::class,
'id' => StringData::class,
'label' => StringData::class,
'weight' => IntegerData::class,
'style' => StringData::class,
'size' => StringData::class,
'size_value' => StringData::class,
'protected_property' => StringData::class,
];
$properties = ConfigEntityAdapter::createFromEntity($this->entity)
->getProperties();
$keys = [];
foreach ($properties as $key => $property) {
$keys[] = $key;
$this
->assertInstanceOf($expected_properties[$key], $property);
}
$this
->assertSame(array_keys($expected_properties), $keys);
}
public function testGetValue() {
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
$this
->assertEquals($this->entity->weight, $adapter
->get('weight')
->getValue());
$this
->assertEquals($this->entity
->id(), $adapter
->get('id')
->getValue());
$this
->assertEquals($this->entity->label, $adapter
->get('label')
->getValue());
}
public function testSet() {
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
$this
->assertEquals(1, $adapter
->get('weight')
->getValue());
$return = $adapter
->set('weight', 2);
$this
->assertSame($adapter, $return);
$this
->assertEquals(2, $this->entity->weight);
$this
->assertEquals(2, $adapter
->get('weight')
->getValue());
}
public function testGetString() {
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
$this
->assertEquals('foobar', $adapter
->getString());
}
public function testApplyDefaultValue() {
$this
->expectException(\BadMethodCallException::class);
$this
->expectExceptionMessage('Method not supported');
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
$adapter
->applyDefaultValue();
}
public function testGetIterator() {
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
$iterator = $adapter
->getIterator();
$fields = iterator_to_array($iterator);
$expected_fields = [
'uuid',
'langcode',
'status',
'dependencies',
'id',
'label',
'weight',
'style',
'size',
'size_value',
'protected_property',
];
$this
->assertEquals($expected_fields, array_keys($fields));
$this
->assertEquals($this->entity
->id(), $fields['id']
->getValue());
$adapter
->setValue(NULL);
$this
->assertEquals(new \ArrayIterator([]), $adapter
->getIterator());
}
}