You are here

public function ConfigSchemaTest::testSchemaData in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testSchemaData()
  2. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testSchemaData()

Tests metadata applied to configuration objects.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php, line 315

Class

ConfigSchemaTest
Tests schema for configuration objects.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testSchemaData() {

  // Try a simple property.
  $meta = \Drupal::service('config.typed')
    ->get('system.site');
  $property = $meta
    ->get('page')
    ->get('front');
  $this
    ->assertInstanceOf(StringInterface::class, $property);
  $this
    ->assertEquals('/user/login', $property
    ->getValue(), 'Got the right value for page.front data.');
  $definition = $property
    ->getDataDefinition();
  $this
    ->assertEmpty($definition['translatable'], 'Got the right translatability setting for page.front data.');

  // Check nested array of properties.
  $list = $meta
    ->get('page')
    ->getElements();
  $this
    ->assertCount(3, $list, 'Got a list with the right number of properties for site page data');
  $this
    ->assertArrayHasKey('front', $list);
  $this
    ->assertArrayHasKey('403', $list);
  $this
    ->assertArrayHasKey('404', $list);
  $this
    ->assertEquals('/user/login', $list['front']
    ->getValue(), 'Got the right value for page.front data from the list.');

  // And test some TypedConfigInterface methods.
  $properties = $list;
  $this
    ->assertCount(3, $properties, 'Got the right number of properties for site page.');
  $this
    ->assertSame($list['front'], $properties['front']);
  $values = $meta
    ->get('page')
    ->toArray();
  $this
    ->assertCount(3, $values, 'Got the right number of property values for site page.');
  $this
    ->assertSame($values['front'], '/user/login');

  // Now let's try something more complex, with nested objects.
  $wrapper = \Drupal::service('config.typed')
    ->get('image.style.large');
  $effects = $wrapper
    ->get('effects');
  $this
    ->assertCount(1, $effects
    ->toArray(), 'Got an array with effects for image.style.large data');
  $uuid = key($effects
    ->getValue());
  $effect = $effects
    ->get($uuid)
    ->getElements();
  $this
    ->assertFalse($effect['data']
    ->isEmpty(), 'Got data for the image scale effect from metadata.');
  $this
    ->assertSame('image_scale', $effect['id']
    ->getValue(), 'Got data for the image scale effect from metadata.');
  $this
    ->assertInstanceOf(IntegerInterface::class, $effect['data']
    ->get('width'));
  $this
    ->assertEquals(480, $effect['data']
    ->get('width')
    ->getValue(), 'Got the right value for the scale effect width.');
}