function ConfigSchemaTest::testSchemaMapping

Tests the basic metadata retrieval layer.

File

drupal/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTest.php, line 43
Contains \Drupal\config\Tests\ConfigSchemaTest.

Class

ConfigSchemaTest
Tests schema for configuration objects.

Namespace

Drupal\config\Tests

Code

function testSchemaMapping() {

  // Nonexistent configuration key will have Unknown as metadata.
  $definition = config_typed()
    ->getDefinition('config_test.no_such_key');
  $expected = array();
  $expected['label'] = 'Unknown';
  $expected['class'] = '\\Drupal\\Core\\Config\\Schema\\Property';
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for nonexistent configuration.');

  // Configuration file without schema will return Unknown as well.
  $definition = config_typed()
    ->getDefinition('config_test.noschema');
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for configuration with no schema.');

  // Configuration file with only some schema.
  $definition = config_typed()
    ->getDefinition('config_test.someschema');
  $expected = array();
  $expected['label'] = 'Schema test data';
  $expected['class'] = '\\Drupal\\Core\\Config\\Schema\\Mapping';
  $expected['mapping']['testitem'] = array(
    'label' => 'Test item',
  );
  $expected['mapping']['testlist'] = array(
    'label' => 'Test list',
  );
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for configuration with only some schema.');

  // Check type detection on elements with undefined types.
  $config = config_typed()
    ->get('config_test.someschema');
  $definition = $config['testitem']
    ->getDefinition();
  $expected = array();
  $expected['label'] = 'Test item';
  $expected['class'] = '\\Drupal\\Core\\TypedData\\Type\\String';
  $expected['type'] = 'string';
  $this
    ->assertEqual($definition, $expected, 'Automatic type detection on string item worked.');
  $definition = $config['testlist']
    ->getDefinition();
  $expected = array();
  $expected['label'] = 'Test list';
  $expected['class'] = '\\Drupal\\Core\\Config\\Schema\\Property';
  $expected['type'] = 'undefined';
  $this
    ->assertEqual($definition, $expected, 'Automatic type fallback on non-string item worked.');

  // Simple case, straight metadata.
  $definition = config_typed()
    ->getDefinition('system.maintenance');
  $expected = array();
  $expected['label'] = 'Maintenance mode';
  $expected['class'] = '\\Drupal\\Core\\Config\\Schema\\Mapping';
  $expected['mapping']['enabled'] = array(
    'label' => 'Put site into maintenance mode',
    'type' => 'boolean',
  );
  $expected['mapping']['message'] = array(
    'label' => 'Message to display when in maintenance mode',
    'type' => 'text',
  );
  $expected['mapping']['langcode'] = array(
    'label' => 'Default language',
    'type' => 'string',
  );
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for system.maintenance');

  // More complex case, generic type. Metadata for image style.
  $definition = config_typed()
    ->getDefinition('image.style.large');
  $expected = array();
  $expected['label'] = 'Image style';
  $expected['class'] = '\\Drupal\\Core\\Config\\Schema\\Mapping';
  $expected['mapping']['name']['type'] = 'string';
  $expected['mapping']['label']['type'] = 'label';
  $expected['mapping']['effects']['type'] = 'sequence';
  $expected['mapping']['effects']['sequence'][0]['type'] = 'mapping';
  $expected['mapping']['effects']['sequence'][0]['mapping']['name']['type'] = 'string';
  $expected['mapping']['effects']['sequence'][0]['mapping']['data']['type'] = 'image.effect.[%parent.name]';
  $expected['mapping']['effects']['sequence'][0]['mapping']['weight']['type'] = 'integer';
  $expected['mapping']['effects']['sequence'][0]['mapping']['ieid']['type'] = 'string';
  $expected['mapping']['langcode']['label'] = 'Default language';
  $expected['mapping']['langcode']['type'] = 'string';
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for image.style.large');

  // More complex, type based on a complex one.
  $definition = config_typed()
    ->getDefinition('image.effect.image_scale');

  // This should be the schema for image.effect.image_scale.
  $expected = array();
  $expected['label'] = 'Image scale';
  $expected['class'] = '\\Drupal\\Core\\Config\\Schema\\Mapping';
  $expected['mapping']['width']['type'] = 'integer';
  $expected['mapping']['width']['label'] = 'Width';
  $expected['mapping']['height']['type'] = 'integer';
  $expected['mapping']['height']['label'] = 'Height';
  $expected['mapping']['upscale']['type'] = 'boolean';
  $expected['mapping']['upscale']['label'] = 'Upscale';
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for image.effect.image_scale');

  // Most complex case, get metadata for actual configuration element.
  $effects = config_typed()
    ->get('image.style.medium')
    ->get('effects');
  $definition = $effects['bddf0d06-42f9-4c75-a700-a33cafa25ea0']['data']
    ->getDefinition();

  // This should be the schema for image.effect.image_scale, reuse previous one.
  $expected['type'] = 'image.effect.image_scale';
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for the first effect of image.style.medium');

  // More complex, multiple filesystem marker test.
  $definition = config_typed()
    ->getDefinition('config_test.someschema.somemodule.section_one.subsection');

  // This should be the schema of config_test.someschema.somemodule.*.*.
  $expected = array();
  $expected['label'] = 'Schema multiple filesytem marker test';
  $expected['class'] = '\\Drupal\\Core\\Config\\Schema\\Mapping';
  $expected['mapping']['id']['type'] = 'string';
  $expected['mapping']['id']['label'] = 'ID';
  $expected['mapping']['description']['type'] = 'text';
  $expected['mapping']['description']['label'] = 'Description';
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for config_test.someschema.somemodule.section_one.subsection');
  $definition = config_typed()
    ->getDefinition('config_test.someschema.somemodule.section_two.subsection');

  // The other file should have the same schema.
  $this
    ->assertEqual($definition, $expected, 'Retrieved the right metadata for config_test.someschema.somemodule.section_two.subsection');
}