You are here

public function ConfigFileContentTest::testSerialization in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigFileContentTest.php \Drupal\KernelTests\Core\Config\ConfigFileContentTest::testSerialization()
  2. 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 191

Class

ConfigFileContentTest
Tests reading and writing of configuration files.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testSerialization() {
  $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.
      'UTF-8' => 'FrançAIS is ÜBER-åwesome',
      // Unicode in keys and values.
      'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ' => 'αβγδεζηθικλμνξοσὠ',
    ],
    'invalid xml' => '</title><script type="text/javascript">alert("Title XSS!");</script> & < > " \' ',
  ];

  // Encode and write, and reload and decode the configuration data.
  $filestorage = new FileStorage(Settings::get('config_sync_directory'));
  $filestorage
    ->write($name, $config_data);
  $config_parsed = $filestorage
    ->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]);
  $key = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
  $this
    ->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
  $key = 'invalid xml';
  $this
    ->assertSame($config_data[$key], $config_parsed[$key]);
}