You are here

public function ConfigSchemaTest::testConfigSaveWithWrappingSchemaDoubleBrackets 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::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 630

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
    ->assertIdentical(\Drupal::config('wrapping.config_schema_test.double_brackets')
    ->get(), $typed_values);
  $tests = \Drupal::service('config.typed')
    ->get('wrapping.config_schema_test.double_brackets')
    ->get('tests')
    ->getElements();
  $definition = $tests[0]
    ->getDataDefinition()
    ->toArray();
  $this
    ->assertEqual($definition['type'], 'wrapping.test.double_brackets.*||test.double_brackets.turtle.horse');
  $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
    ->assertIdentical(\Drupal::config('wrapping.config_schema_test.double_brackets')
    ->get(), $typed_values);
  $tests = \Drupal::service('config.typed')
    ->get('wrapping.config_schema_test.double_brackets')
    ->get('tests')
    ->getElements();
  $definition = $tests[0]
    ->getDataDefinition()
    ->toArray();
  $this
    ->assertEqual($definition['type'], 'wrapping.test.double_brackets.*||test.double_brackets.cat.dog');

  // 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
    ->assertEqual($definition['type'], 'wrapping.test.double_brackets.*||test.double_brackets.cat.dog');
  $definition = $tests[1]
    ->getDataDefinition()
    ->toArray();
  $this
    ->assertEqual($definition['type'], 'wrapping.test.double_brackets.*||test.double_brackets.turtle.horse');
  $typed_values = [
    'tests' => [
      [
        'id' => 'cat:persion.dog',
        'foo' => 'cat',
        'bar' => 'dog',
        'breed' => 'persion',
      ],
    ],
  ];
  \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
    ->assertEqual($definition['type'], 'wrapping.test.other_double_brackets.*||test.double_brackets.cat:*.*');

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