You are here

public function ConfigSchemaTest::testConfigSaveWithSchema 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::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 = [
    'string' => 1,
    'empty_string' => '',
    'null_string' => NULL,
    'integer' => '100',
    'null_integer' => '',
    'boolean' => 1,
    // If the config schema doesn't have a type it shouldn't be casted.
    'no_type' => 1,
    'mapping' => [
      'string' => 1,
    ],
    'float' => '3.14',
    'null_float' => '',
    'sequence' => [
      1,
      0,
      1,
    ],
    'sequence_bc' => [
      1,
      0,
      1,
    ],
    // Not in schema and therefore should be left untouched.
    'not_present_in_schema' => TRUE,
    // Test a custom type.
    'config_schema_test_integer' => '1',
    'config_schema_test_integer_empty_string' => '',
  ];
  $untyped_to_typed = $untyped_values;
  $typed_values = [
    'string' => '1',
    'empty_string' => '',
    'null_string' => NULL,
    'integer' => 100,
    'null_integer' => NULL,
    'boolean' => TRUE,
    'no_type' => 1,
    'mapping' => [
      'string' => '1',
    ],
    'float' => 3.14,
    'null_float' => NULL,
    'sequence' => [
      TRUE,
      FALSE,
      TRUE,
    ],
    'sequence_bc' => [
      TRUE,
      FALSE,
      TRUE,
    ],
    'not_present_in_schema' => TRUE,
    'config_schema_test_integer' => 1,
    'config_schema_test_integer_empty_string' => NULL,
  ];

  // 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);
}