function ConfigCRUDTest::testDataTypes

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testDataTypes()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testDataTypes()
  3. 10 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 266

Class

ConfigCRUDTest
Tests CRUD operations on configuration objects.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testDataTypes() : void {
    \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.
        //   https://www.drupal.org/project/drupal/issues/3205480
        //   'octal' => 0775,
'string' => 'string',
        'string_int' => '1',
        'nullable_array' => NULL,
        'nullable_boolean' => NULL,
        'nullable_exp' => NULL,
        'nullable_float' => NULL,
        'nullable_float_as_integer' => NULL,
        'nullable_hex' => NULL,
        'nullable_int' => NULL,
        'nullable_octal' => NULL,
        'nullable_string' => NULL,
        'nullable_string_int' => NULL,
        'mapping_with_only_required_keys' => [],
        'mapping_with_some_required_keys' => [],
        'mapping_with_only_optional_keys' => [],
    ];
    $data = [
        '_core' => [
            'default_config_hash' => Crypt::hashBase64(serialize($data)),
        ],
    ] + $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.
    }
}

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