function ConfigFileContentTest::testSerialization

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigFileContentTest.php \Drupal\KernelTests\Core\Config\ConfigFileContentTest::testSerialization()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigFileContentTest.php \Drupal\KernelTests\Core\Config\ConfigFileContentTest::testSerialization()
  3. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigFileContentTest.php \Drupal\KernelTests\Core\Config\ConfigFileContentTest::testSerialization()

Tests serialization of configuration to file.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigFileContentTest.php, line 193

Class

ConfigFileContentTest
Tests reading and writing of configuration files.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testSerialization() : void {
    $name = $this->randomMachineName(10) . '.' . $this->randomMachineName(10);
    $config_data = [
        // Indexed arrays; the order of elements is essential.
'numeric keys' => [
            'i',
            'n',
            'd',
            'e',
            'x',
            'e',
            'd',
        ],
        // Infinitely nested keys using arbitrary element names.
'nested keys' => [
            // HTML/XML in values.
'HTML' => '<strong> <bold> <em> <blockquote>',
            // UTF-8 in values.
            // cspell:disable-next-line
'UTF-8' => 'FrançAIS is ÜBER-åwesome',
            // Unicode in keys and values.
            // cSpell:disable-next-line
'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ' => 'αβγδεζηθικλμνξοσὠ',
        ],
        'invalid xml' => '</title><script type="text/javascript">alert("Title XSS!");</script> & < > " \' ',
    ];
    // Encode and write, and reload and decode the configuration data.
    $file_storage = new FileStorage(Settings::get('config_sync_directory'));
    $file_storage->write($name, $config_data);
    $config_parsed = $file_storage->read($name);
    $key = 'numeric keys';
    $this->assertSame($config_data[$key], $config_parsed[$key]);
    $key = 'nested keys';
    $this->assertSame($config_data[$key], $config_parsed[$key]);
    $key = 'HTML';
    $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
    $key = 'UTF-8';
    $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
    // cSpell:disable-next-line
    $key = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
    $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
    $key = 'invalid xml';
    $this->assertSame($config_data[$key], $config_parsed[$key]);
}

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