You are here

public function ConfigSchemaTest::testConfigSaveWithWrappingSchemaDoubleBrackets in Drupal 9

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

Tests dynamic config schema type with multiple sub-key references.

File

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

Class

ConfigSchemaTest
Tests schema for configuration objects.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testConfigSaveWithWrappingSchemaDoubleBrackets() {
  $untyped_values = [
    'tests' => [
      [
        'wrapper_value' => 'foo',
        'foo' => 'turtle',
        'bar' => 'horse',
        // Converted to a string by 'test.double_brackets.turtle.horse'
        // schema.
        'another_key' => '100',
      ],
    ],
  ];
  $typed_values = [
    'tests' => [
      [
        'wrapper_value' => 'foo',
        'foo' => 'turtle',
        'bar' => 'horse',
        'another_key' => 100,
      ],
    ],
  ];

  // Save config which has a schema that enforces types.
  \Drupal::configFactory()
    ->getEditable('wrapping.config_schema_test.double_brackets')
    ->setData($untyped_values)
    ->save();
  $this
    ->assertSame($typed_values, \Drupal::config('wrapping.config_schema_test.double_brackets')
    ->get());
  $tests = \Drupal::service('config.typed')
    ->get('wrapping.config_schema_test.double_brackets')
    ->get('tests')
    ->getElements();
  $definition = $tests[0]
    ->getDataDefinition()
    ->toArray();
  $this
    ->assertEquals('wrapping.test.double_brackets.*||test.double_brackets.turtle.horse', $definition['type']);
  $untyped_values = [
    'tests' => [
      [
        'wrapper_value' => 'foo',
        'foo' => 'cat',
        'bar' => 'dog',
        // Converted to a string by 'test.double_brackets.cat.dog' schema.
        'another_key' => 100,
      ],
    ],
  ];
  $typed_values = [
    'tests' => [
      [
        'wrapper_value' => 'foo',
        'foo' => 'cat',
        'bar' => 'dog',
        'another_key' => '100',
      ],
    ],
  ];

  // Save config which has a schema that enforces types.
  \Drupal::configFactory()
    ->getEditable('wrapping.config_schema_test.double_brackets')
    ->setData($untyped_values)
    ->save();
  $this
    ->assertSame($typed_values, \Drupal::config('wrapping.config_schema_test.double_brackets')
    ->get());
  $tests = \Drupal::service('config.typed')
    ->get('wrapping.config_schema_test.double_brackets')
    ->get('tests')
    ->getElements();
  $definition = $tests[0]
    ->getDataDefinition()
    ->toArray();
  $this
    ->assertEquals('wrapping.test.double_brackets.*||test.double_brackets.cat.dog', $definition['type']);

  // Combine everything in a single save.
  $typed_values = [
    'tests' => [
      [
        'wrapper_value' => 'foo',
        'foo' => 'cat',
        'bar' => 'dog',
        'another_key' => 100,
      ],
      [
        'wrapper_value' => 'foo',
        'foo' => 'turtle',
        'bar' => 'horse',
        'another_key' => '100',
      ],
    ],
  ];
  \Drupal::configFactory()
    ->getEditable('wrapping.config_schema_test.double_brackets')
    ->setData($typed_values)
    ->save();
  $tests = \Drupal::service('config.typed')
    ->get('wrapping.config_schema_test.double_brackets')
    ->get('tests')
    ->getElements();
  $definition = $tests[0]
    ->getDataDefinition()
    ->toArray();
  $this
    ->assertEquals('wrapping.test.double_brackets.*||test.double_brackets.cat.dog', $definition['type']);
  $definition = $tests[1]
    ->getDataDefinition()
    ->toArray();
  $this
    ->assertEquals('wrapping.test.double_brackets.*||test.double_brackets.turtle.horse', $definition['type']);
  $typed_values = [
    'tests' => [
      [
        'id' => 'cat:persian.dog',
        'foo' => 'cat',
        'bar' => 'dog',
        'breed' => 'persian',
      ],
    ],
  ];
  \Drupal::configFactory()
    ->getEditable('wrapping.config_schema_test.other_double_brackets')
    ->setData($typed_values)
    ->save();
  $tests = \Drupal::service('config.typed')
    ->get('wrapping.config_schema_test.other_double_brackets')
    ->get('tests')
    ->getElements();
  $definition = $tests[0]
    ->getDataDefinition()
    ->toArray();

  // Check that definition type is a merge of the expected types.
  $this
    ->assertEquals('wrapping.test.other_double_brackets.*||test.double_brackets.cat:*.*', $definition['type']);

  // Check that breed was inherited from parent definition.
  $this
    ->assertEquals([
    'type' => 'string',
  ], $definition['mapping']['breed']);
}