function ConfigSchemaTest::testConfigSaveWithSchema

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testConfigSaveWithSchema()
  2. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testConfigSaveWithSchema()
  3. 11.x core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php \Drupal\KernelTests\Core\Config\ConfigSchemaTest::testConfigSaveWithSchema()

Tests configuration value data type enforcement using schemas.

File

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

Class

ConfigSchemaTest
Tests schema for configuration objects.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testConfigSaveWithSchema() {
    $untyped_values = [
        // Test a custom type.
'config_schema_test_integer' => '1',
        'config_schema_test_integer_empty_string' => '',
        'integer' => '100',
        'null_integer' => '',
        'float' => '3.14',
        'null_float' => '',
        'string' => 1,
        'null_string' => NULL,
        'empty_string' => '',
        'boolean' => 1,
        // If the config schema doesn't have a type it shouldn't be casted.
'no_type' => 1,
        'mapping' => [
            'string' => 1,
        ],
        'sequence' => [
            1,
            0,
            1,
        ],
        'sequence_bc' => [
            1,
            0,
            1,
        ],
        // Not in schema and therefore should be left untouched.
'not_present_in_schema' => TRUE,
    ];
    $untyped_to_typed = $untyped_values;
    $typed_values = [
        'config_schema_test_integer' => 1,
        'config_schema_test_integer_empty_string' => NULL,
        'integer' => 100,
        'null_integer' => NULL,
        'float' => 3.14,
        'null_float' => NULL,
        'string' => '1',
        'null_string' => NULL,
        'empty_string' => '',
        'boolean' => TRUE,
        'no_type' => 1,
        'mapping' => [
            'string' => '1',
        ],
        'sequence' => [
            TRUE,
            FALSE,
            TRUE,
        ],
        'sequence_bc' => [
            TRUE,
            FALSE,
            TRUE,
        ],
        'not_present_in_schema' => TRUE,
    ];
    // Save config which has a schema that enforces types.
    $this->config('config_schema_test.schema_data_types')
        ->setData($untyped_to_typed)
        ->save();
    $this->assertSame($typed_values, $this->config('config_schema_test.schema_data_types')
        ->get());
    // Save config which does not have a schema that enforces types.
    $this->config('config_schema_test.no_schema_data_types')
        ->setData($untyped_values)
        ->save();
    $this->assertSame($untyped_values, $this->config('config_schema_test.no_schema_data_types')
        ->get());
    // Ensure that configuration objects with keys marked as ignored are not
    // changed when saved. The 'config_schema_test.ignore' will have been saved
    // during the installation of configuration in the setUp method.
    $extension_path = __DIR__ . '/../../../../../modules/config/tests/config_schema_test/';
    $install_storage = new FileStorage($extension_path . InstallStorage::CONFIG_INSTALL_DIRECTORY);
    $original_data = $install_storage->read('config_schema_test.ignore');
    $installed_data = $this->config('config_schema_test.ignore')
        ->get();
    unset($installed_data['_core']);
    $this->assertSame($original_data, $installed_data);
}

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