You are here

public function ConfigCRUDTest::testDataTypes in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testDataTypes()

Tests data type handling.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php, line 267

Class

ConfigCRUDTest
Tests CRUD operations on configuration objects.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testDataTypes() {
  \Drupal::service('module_installer')
    ->install([
    'config_test',
  ]);
  $storage = new DatabaseStorage($this->container
    ->get('database'), 'config');
  $name = 'config_test.types';
  $config = $this
    ->config($name);

  // Verify variable data types are intact.
  $data = [
    'array' => [],
    'boolean' => TRUE,
    'exp' => 1.2E+34,
    'float' => 3.14159,
    'float_as_integer' => (double) 1,
    'hex' => 0xc,
    'int' => 99,
    // Symfony 5.1's YAML parser issues a deprecation when reading octal with
    // a leading zero, to comply with YAML 1.2. However PECL YAML is still
    // YAML 1.1 compliant.
    // @todo: revisit parsing of octal once PECL YAML supports YAML 1.2.
    // See https://www.drupal.org/project/drupal/issues/3205480
    // 'octal' => 0775,
    'string' => 'string',
    'string_int' => '1',
  ];
  $data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($data));
  $this
    ->assertSame($data, $config
    ->get());

  // Re-set each key using Config::set().
  foreach ($data as $key => $value) {
    $config
      ->set($key, $value);
  }
  $config
    ->save();
  $this
    ->assertSame($data, $config
    ->get());

  // Assert the data against the file storage.
  $this
    ->assertSame($data, $storage
    ->read($name));

  // Set data using config::setData().
  $config
    ->setData($data)
    ->save();
  $this
    ->assertSame($data, $config
    ->get());
  $this
    ->assertSame($data, $storage
    ->read($name));

  // Test that schema type enforcement can be overridden by trusting the data.
  $this
    ->assertSame(99, $config
    ->get('int'));
  $config
    ->set('int', '99')
    ->save(TRUE);
  $this
    ->assertSame('99', $config
    ->get('int'));

  // Test that re-saving without testing the data enforces the schema type.
  $config
    ->save();
  $this
    ->assertSame($data, $config
    ->get());

  // Test that setting an unsupported type for a config object with a schema
  // fails.
  try {
    $config
      ->set('stream', fopen(__FILE__, 'r'))
      ->save();
    $this
      ->fail('No Exception thrown upon saving invalid data type.');
  } catch (UnsupportedDataTypeConfigException $e) {

    // Expected exception; just continue testing.
  }

  // Test that setting an unsupported type for a config object with no schema
  // also fails.
  $typed_config_manager = $this->container
    ->get('config.typed');
  $config_name = 'config_test.no_schema';
  $config = $this
    ->config($config_name);
  $this
    ->assertFalse($typed_config_manager
    ->hasConfigSchema($config_name));
  try {
    $config
      ->set('stream', fopen(__FILE__, 'r'))
      ->save();
    $this
      ->fail('No Exception thrown upon saving invalid data type.');
  } catch (UnsupportedDataTypeConfigException $e) {

    // Expected exception; just continue testing.
  }
}