You are here

public function ConfigSchemaTest::testSchemaData in Drupal 8

Same name and namespace in other branches
  1. 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
    ->assertEqual($property
    ->getValue(), '/user/login', 'Got the right value for page.front data.');
  $definition = $property
    ->getDataDefinition();
  $this
    ->assertTrue(empty($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
    ->assertTrue(isset($list['front']) && isset($list['403']) && isset($list['404']), 'Got a list with the right properties for site page data.');
  $this
    ->assertEqual($list['front']
    ->getValue(), '/user/login', '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
    ->assertTrue(!$effect['data']
    ->isEmpty() && $effect['id']
    ->getValue() == 'image_scale', 'Got data for the image scale effect from metadata.');
  $this
    ->assertInstanceOf(IntegerInterface::class, $effect['data']
    ->get('width'));
  $this
    ->assertEqual($effect['data']
    ->get('width')
    ->getValue(), 480, 'Got the right value for the scale effect width.');
}