function TypedConfigTest::testTypedDataAPI

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Config/TypedConfigTest.php \Drupal\KernelTests\Config\TypedConfigTest::testTypedDataAPI()
  2. 10 core/tests/Drupal/KernelTests/Config/TypedConfigTest.php \Drupal\KernelTests\Config\TypedConfigTest::testTypedDataAPI()
  3. 11.x core/tests/Drupal/KernelTests/Config/TypedConfigTest.php \Drupal\KernelTests\Config\TypedConfigTest::testTypedDataAPI()

Verifies that the Typed Data API is implemented correctly.

File

core/tests/Drupal/KernelTests/Config/TypedConfigTest.php, line 38

Class

TypedConfigTest
Tests config validation mechanism.

Namespace

Drupal\KernelTests\Config

Code

public function testTypedDataAPI() {
    
    /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager */
    $typed_config_manager = \Drupal::service('config.typed');
    
    /** @var \Drupal\Core\Config\Schema\TypedConfigInterface $typed_config */
    $typed_config = $typed_config_manager->get('config_test.validation');
    // Test a primitive.
    $string_data = $typed_config->get('llama');
    $this->assertInstanceOf(StringInterface::class, $string_data);
    $this->assertEquals('llama', $string_data->getValue());
    // Test complex data.
    $mapping = $typed_config->get('cat');
    
    /** @var \Drupal\Core\TypedData\ComplexDataInterface $mapping */
    $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());
    // Verify the item metadata is available.
    $this->assertInstanceOf(ComplexDataDefinitionInterface::class, $mapping->getDataDefinition());
    $this->assertArrayHasKey('type', $mapping->getProperties());
    $this->assertArrayHasKey('count', $mapping->getProperties());
    // Test accessing sequences.
    $sequence = $typed_config->get('giraffe');
    
    /** @var \Drupal\Core\TypedData\ListInterface $sequence */
    $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());
    // Verify the item metadata is available.
    $this->assertInstanceOf(SequenceDataDefinition::class, $sequence->getDataDefinition());
    // Test accessing typed config objects for simple config and config
    // entities.
    $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()));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.