ConfigFileContentTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Core\Config\FileStorage;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Tests reading and writing of configuration files.
  7. *
  8. * @group config
  9. */
  10. class ConfigFileContentTest extends KernelTestBase {
  11. /**
  12. * Exempt from strict schema checking.
  13. *
  14. * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
  15. *
  16. * @var bool
  17. */
  18. protected $strictConfigSchema = FALSE;
  19. /**
  20. * Tests setting, writing, and reading of a configuration setting.
  21. */
  22. public function testReadWriteConfig() {
  23. $storage = $this->container->get('config.storage');
  24. $name = 'foo.bar';
  25. $key = 'foo';
  26. $value = 'bar';
  27. $nested_key = 'biff.bang';
  28. $nested_value = 'pow';
  29. $array_key = 'array';
  30. $array_value = [
  31. 'foo' => 'bar',
  32. 'biff' => [
  33. 'bang' => 'pow',
  34. ],
  35. ];
  36. $casting_array_key = 'casting_array';
  37. $casting_array_false_value_key = 'casting_array.cast.false';
  38. $casting_array_value = [
  39. 'cast' => [
  40. 'false' => FALSE,
  41. ],
  42. ];
  43. $nested_array_key = 'nested.array';
  44. $true_key = 'true';
  45. $false_key = 'false';
  46. // Attempt to read non-existing configuration.
  47. $config = $this->config($name);
  48. // Verify a configuration object is returned.
  49. $this->assertEqual($config->getName(), $name);
  50. $this->assertTrue($config, 'Config object created.');
  51. // Verify the configuration object is empty.
  52. $this->assertEqual($config->get(), [], 'New config object is empty.');
  53. // Verify nothing was saved.
  54. $data = $storage->read($name);
  55. $this->assertIdentical($data, FALSE);
  56. // Add a top level value.
  57. $config = $this->config($name);
  58. $config->set($key, $value);
  59. // Add a nested value.
  60. $config->set($nested_key, $nested_value);
  61. // Add an array.
  62. $config->set($array_key, $array_value);
  63. // Add a nested array.
  64. $config->set($nested_array_key, $array_value);
  65. // Add a boolean false value. Should get cast to 0.
  66. $config->set($false_key, FALSE);
  67. // Add a boolean true value. Should get cast to 1.
  68. $config->set($true_key, TRUE);
  69. // Add a null value. Should get cast to an empty string.
  70. $config->set('null', NULL);
  71. // Add an array with a nested boolean false that should get cast to 0.
  72. $config->set($casting_array_key, $casting_array_value);
  73. $config->save();
  74. // Verify the database entry exists.
  75. $data = $storage->read($name);
  76. $this->assertTrue($data);
  77. // Read top level value.
  78. $config = $this->config($name);
  79. $this->assertEqual($config->getName(), $name);
  80. $this->assertTrue($config, 'Config object created.');
  81. $this->assertEqual($config->get($key), 'bar', 'Top level configuration value found.');
  82. // Read nested value.
  83. $this->assertEqual($config->get($nested_key), $nested_value, 'Nested configuration value found.');
  84. // Read array.
  85. $this->assertEqual($config->get($array_key), $array_value, 'Top level array configuration value found.');
  86. // Read nested array.
  87. $this->assertEqual($config->get($nested_array_key), $array_value, 'Nested array configuration value found.');
  88. // Read a top level value that doesn't exist.
  89. $this->assertNull($config->get('i_dont_exist'), 'Non-existent top level value returned NULL.');
  90. // Read a nested value that doesn't exist.
  91. $this->assertNull($config->get('i.dont.exist'), 'Non-existent nested value returned NULL.');
  92. // Read false value.
  93. $this->assertFalse($config->get($false_key), "Boolean FALSE value returned the FALSE.");
  94. // Read true value.
  95. $this->assertTrue($config->get($true_key), "Boolean TRUE value returned the TRUE.");
  96. // Read null value.
  97. $this->assertIdentical($config->get('null'), NULL);
  98. // Read false that had been nested in an array value.
  99. $this->assertSame($config->get($casting_array_false_value_key), FALSE, "Nested boolean FALSE value returned FALSE.");
  100. // Unset a top level value.
  101. $config->clear($key);
  102. // Unset a nested value.
  103. $config->clear($nested_key);
  104. $config->save();
  105. $config = $this->config($name);
  106. // Read unset top level value.
  107. $this->assertNull($config->get($key), 'Top level value unset.');
  108. // Read unset nested value.
  109. $this->assertNull($config->get($nested_key), 'Nested value unset.');
  110. // Create two new configuration files to test listing.
  111. $config = $this->config('foo.baz');
  112. $config->set($key, $value);
  113. $config->save();
  114. // Test chained set()->save().
  115. $chained_name = 'biff.bang';
  116. $config = $this->config($chained_name);
  117. $config->set($key, $value)->save();
  118. // Verify the database entry exists from a chained save.
  119. $data = $storage->read($chained_name);
  120. $this->assertEqual($data, $config->get());
  121. // Get file listing for all files starting with 'foo'. Should return
  122. // two elements.
  123. $files = $storage->listAll('foo');
  124. $this->assertEqual(count($files), 2, 'Two files listed with the prefix \'foo\'.');
  125. // Get file listing for all files starting with 'biff'. Should return
  126. // one element.
  127. $files = $storage->listAll('biff');
  128. $this->assertEqual(count($files), 1, 'One file listed with the prefix \'biff\'.');
  129. // Get file listing for all files starting with 'foo.bar'. Should return
  130. // one element.
  131. $files = $storage->listAll('foo.bar');
  132. $this->assertEqual(count($files), 1, 'One file listed with the prefix \'foo.bar\'.');
  133. // Get file listing for all files starting with 'bar'. Should return
  134. // an empty array.
  135. $files = $storage->listAll('bar');
  136. $this->assertEqual($files, [], 'No files listed with the prefix \'bar\'.');
  137. // Delete the configuration.
  138. $config = $this->config($name);
  139. $config->delete();
  140. // Verify the database entry no longer exists.
  141. $data = $storage->read($name);
  142. $this->assertIdentical($data, FALSE);
  143. }
  144. /**
  145. * Tests serialization of configuration to file.
  146. */
  147. public function testSerialization() {
  148. $name = $this->randomMachineName(10) . '.' . $this->randomMachineName(10);
  149. $config_data = [
  150. // Indexed arrays; the order of elements is essential.
  151. 'numeric keys' => ['i', 'n', 'd', 'e', 'x', 'e', 'd'],
  152. // Infinitely nested keys using arbitrary element names.
  153. 'nested keys' => [
  154. // HTML/XML in values.
  155. 'HTML' => '<strong> <bold> <em> <blockquote>',
  156. // UTF-8 in values.
  157. 'UTF-8' => 'FrançAIS is ÜBER-åwesome',
  158. // Unicode in keys and values.
  159. 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ' => 'αβγδεζηθικλμνξοσὠ',
  160. ],
  161. 'invalid xml' => '</title><script type="text/javascript">alert("Title XSS!");</script> & < > " \' ',
  162. ];
  163. // Encode and write, and reload and decode the configuration data.
  164. $filestorage = new FileStorage(config_get_config_directory(CONFIG_SYNC_DIRECTORY));
  165. $filestorage->write($name, $config_data);
  166. $config_parsed = $filestorage->read($name);
  167. $key = 'numeric keys';
  168. $this->assertSame($config_data[$key], $config_parsed[$key]);
  169. $key = 'nested keys';
  170. $this->assertSame($config_data[$key], $config_parsed[$key]);
  171. $key = 'HTML';
  172. $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
  173. $key = 'UTF-8';
  174. $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
  175. $key = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
  176. $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
  177. $key = 'invalid xml';
  178. $this->assertSame($config_data[$key], $config_parsed[$key]);
  179. }
  180. }