You are here

public function ConfigSchemaTest::testConfigSaveWithSchema in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/config/src/Tests/ConfigSchemaTest.php \Drupal\config\Tests\ConfigSchemaTest::testConfigSaveWithSchema()

Test configuration value data type enforcement using schemas.

File

core/modules/config/src/Tests/ConfigSchemaTest.php, line 326
Contains \Drupal\config\Tests\ConfigSchemaTest.

Class

ConfigSchemaTest
Tests schema for configuration objects.

Namespace

Drupal\config\Tests

Code

public function testConfigSaveWithSchema() {
  $untyped_values = array(
    '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' => array(
      'string' => 1,
    ),
    'float' => '3.14',
    'null_float' => '',
    'sequence' => array(
      1,
      0,
      1,
    ),
    'sequence_bc' => array(
      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 = array(
    'string' => '1',
    'empty_string' => '',
    'null_string' => NULL,
    'integer' => 100,
    'null_integer' => NULL,
    'boolean' => TRUE,
    'no_type' => 1,
    'mapping' => array(
      'string' => '1',
    ),
    'float' => 3.14,
    'null_float' => NULL,
    'sequence' => array(
      TRUE,
      FALSE,
      TRUE,
    ),
    'sequence_bc' => array(
      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
    ->assertIdentical($this
    ->config('config_schema_test.schema_data_types')
    ->get(), $typed_values);

  // 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
    ->assertIdentical($this
    ->config('config_schema_test.no_schema_data_types')
    ->get(), $untyped_values);

  // 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 = drupal_get_path('module', 'config_schema_test');
  $install_storage = new FileStorage($extension_path . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY);
  $original_data = $install_storage
    ->read('config_schema_test.ignore');
  $this
    ->assertIdentical($this
    ->config('config_schema_test.ignore')
    ->get(), $original_data);
}