View source
<?php
namespace Drupal\KernelTests\Config;
use Drupal\Core\Config\Schema\SequenceDataDefinition;
use Drupal\Core\Config\Schema\TypedConfigInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\Type\IntegerInterface;
use Drupal\Core\TypedData\Type\StringInterface;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\Validator\ConstraintViolationListInterface;
class TypedConfigTest extends KernelTestBase {
protected static $modules = [
'config_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig('config_test');
}
public function testTypedDataAPI() {
$typed_config_manager = \Drupal::service('config.typed');
$typed_config = $typed_config_manager
->get('config_test.validation');
$string_data = $typed_config
->get('llama');
$this
->assertInstanceOf(StringInterface::class, $string_data);
$this
->assertEquals('llama', $string_data
->getValue());
$mapping = $typed_config
->get('cat');
$this
->assertInstanceOf(ComplexDataInterface::class, $mapping);
$this
->assertInstanceOf(StringInterface::class, $mapping
->get('type'));
$this
->assertEquals('kitten', $mapping
->get('type')
->getValue());
$this
->assertInstanceOf(IntegerInterface::class, $mapping
->get('count'));
$this
->assertEquals(2, $mapping
->get('count')
->getValue());
$this
->assertInstanceOf(ComplexDataDefinitionInterface::class, $mapping
->getDataDefinition());
$this
->assertArrayHasKey('type', $mapping
->getProperties());
$this
->assertArrayHasKey('count', $mapping
->getProperties());
$sequence = $typed_config
->get('giraffe');
$this
->assertInstanceOf(ComplexDataInterface::class, $sequence);
$this
->assertInstanceOf(StringInterface::class, $sequence
->get('hum1'));
$this
->assertEquals('hum1', $sequence
->get('hum1')
->getValue());
$this
->assertEquals('hum2', $sequence
->get('hum2')
->getValue());
$this
->assertCount(2, $sequence
->getIterator());
$this
->assertInstanceOf(SequenceDataDefinition::class, $sequence
->getDataDefinition());
$typed_config_manager = \Drupal::service('config.typed');
$typed_config = $typed_config_manager
->createFromNameAndData('config_test.validation', \Drupal::configFactory()
->get('config_test.validation')
->get());
$this
->assertInstanceOf(TypedConfigInterface::class, $typed_config);
$this
->assertEquals([
'llama',
'cat',
'giraffe',
'uuid',
'_core',
], array_keys($typed_config
->getElements()));
$config_test_entity = \Drupal::entityTypeManager()
->getStorage('config_test')
->create([
'id' => 'asterix',
'label' => 'Asterix',
'weight' => 11,
'style' => 'test_style',
]);
$typed_config = $typed_config_manager
->createFromNameAndData($config_test_entity
->getConfigDependencyName(), $config_test_entity
->toArray());
$this
->assertInstanceOf(TypedConfigInterface::class, $typed_config);
$this
->assertEquals([
'uuid',
'langcode',
'status',
'dependencies',
'id',
'label',
'weight',
'style',
'size',
'size_value',
'protected_property',
], array_keys($typed_config
->getElements()));
}
public function testSimpleConfigValidation() {
$config = \Drupal::configFactory()
->getEditable('config_test.validation');
$typed_config_manager = \Drupal::service('config.typed');
$typed_config = $typed_config_manager
->get('config_test.validation');
$result = $typed_config
->validate();
$this
->assertInstanceOf(ConstraintViolationListInterface::class, $result);
$this
->assertEmpty($result);
$config
->set('llama', 'elephant');
$config
->save();
$typed_config = $typed_config_manager
->get('config_test.validation');
$result = $typed_config
->validate();
$this
->assertCount(1, $result);
$this
->assertEquals('no valid llama', $result
->get(0)
->getMessage());
$config
->set('llama', 'llama');
$config
->set('cat.type', 'nyans');
$config
->save();
$typed_config = $typed_config_manager
->get('config_test.validation');
$result = $typed_config
->validate();
$this
->assertEmpty($result);
$config
->set('cat.type', 'miaus');
$config
->save();
$typed_config = $typed_config_manager
->get('config_test.validation');
$result = $typed_config
->validate();
$this
->assertCount(1, $result);
$this
->assertEquals('no valid cat', $result
->get(0)
->getMessage());
$config
->set('cat.type', 'nyans');
$config
->set('giraffe', [
'muh',
'hum2',
]);
$config
->save();
$typed_config = $typed_config_manager
->get('config_test.validation');
$result = $typed_config
->validate();
$this
->assertCount(1, $result);
$this
->assertEquals('Giraffes just hum', $result
->get(0)
->getMessage());
$config
->set('giraffe', [
'hum',
'hum2',
'invalid-key' => 'hum',
]);
$config
->save();
$typed_config = $typed_config_manager
->get('config_test.validation');
$result = $typed_config
->validate();
$this
->assertCount(1, $result);
$this
->assertEquals('giraffe', $result
->get(0)
->getPropertyPath());
$this
->assertEquals('Invalid giraffe key.', $result
->get(0)
->getMessage());
$typed_config = $typed_config_manager
->get('config_test.validation');
$value = $typed_config
->getValue();
unset($value['giraffe']);
$value['elephant'] = 'foo';
$value['zebra'] = 'foo';
$typed_config
->setValue($value);
$result = $typed_config
->validate();
$this
->assertCount(1, $result);
$this
->assertEquals('', $result
->get(0)
->getPropertyPath());
$this
->assertEquals('Unexpected keys: elephant, zebra', $result
->get(0)
->getMessage());
}
}