function ConfigSchemaTest::testConfigSaveWithWrappingSchemaDoubleBrackets

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testConfigSaveWithWrappingSchemaDoubleBrackets()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testConfigSaveWithWrappingSchemaDoubleBrackets()
  3. 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 714

Class

ConfigSchemaTest
Tests schema for configuration objects.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testConfigSaveWithWrappingSchemaDoubleBrackets() : void {
    $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' => [
            [
                'another_key' => 100,
                'foo' => 'turtle',
                'bar' => 'horse',
                'wrapper_value' => 'foo',
            ],
        ],
    ];
    // Save config which has a schema that enforces types.
    \Drupal::configFactory()->getEditable('wrapping.config_schema_test.double_brackets')
        ->setData($untyped_values)
        ->save();
    // TRICKY: https://www.drupal.org/project/drupal/issues/2663410 introduced a
    // bug that made TypedConfigManager sensitive to cache pollution. Saving
    // config triggers validation, which in turn triggers that cache pollution
    // bug. This is a work-around.
    // @todo Remove in https://www.drupal.org/project/drupal/issues/3400181
    \Drupal::service('config.typed')->clearCachedDefinitions();
    $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' => [
            [
                'another_key' => '100',
                'foo' => 'cat',
                'bar' => 'dog',
                'wrapper_value' => 'foo',
            ],
        ],
    ];
    // Save config which has a schema that enforces types.
    \Drupal::configFactory()->getEditable('wrapping.config_schema_test.double_brackets')
        ->setData($untyped_values)
        ->save();
    // TRICKY: https://www.drupal.org/project/drupal/issues/2663410 introduced a
    // bug that made TypedConfigManager sensitive to cache pollution. Saving
    // config in a test triggers the schema checking and validation logic from
    // \Drupal\Core\Config\Development\ConfigSchemaChecker , which in turn
    // triggers that cache pollution bug. This is a work-around.
    // @todo Remove in https://www.drupal.org/project/drupal/issues/3400181
    \Drupal::service('config.typed')->clearCachedDefinitions();
    $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' => [
            [
                'another_key' => 100,
                'foo' => 'cat',
                'bar' => 'dog',
                'wrapper_value' => 'foo',
            ],
            [
                'another_key' => '100',
                'foo' => 'turtle',
                'bar' => 'horse',
                'wrapper_value' => 'foo',
            ],
        ],
    ];
    \Drupal::configFactory()->getEditable('wrapping.config_schema_test.double_brackets')
        ->setData($typed_values)
        ->save();
    // TRICKY: https://www.drupal.org/project/drupal/issues/2663410 introduced a
    // bug that made TypedConfigManager sensitive to cache pollution. Saving
    // config in a test triggers the schema checking and validation logic from
    // \Drupal\Core\Config\Development\ConfigSchemaChecker , which in turn
    // triggers that cache pollution bug. This is a work-around.
    // @todo Remove in https://www.drupal.org/project/drupal/issues/3400181
    \Drupal::service('config.typed')->clearCachedDefinitions();
    $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();
    // TRICKY: https://www.drupal.org/project/drupal/issues/2663410 introduced a
    // bug that made TypedConfigManager sensitive to cache pollution. Saving
    // config in a test triggers the schema checking and validation logic from
    // \Drupal\Core\Config\Development\ConfigSchemaChecker , which in turn
    // triggers that cache pollution bug. This is a work-around.
    // @todo Remove in https://www.drupal.org/project/drupal/issues/3400181
    \Drupal::service('config.typed')->clearCachedDefinitions();
    $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',
        'requiredKey' => TRUE,
    ], $definition['mapping']['breed']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.