SettingsRewriteTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Drupal\KernelTests\Core\Site;
  3. use Drupal\Core\Site\Settings;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Tests the drupal_rewrite_settings() function.
  7. *
  8. * @group system
  9. */
  10. class SettingsRewriteTest extends KernelTestBase {
  11. /**
  12. * Tests the drupal_rewrite_settings() function.
  13. */
  14. public function testDrupalRewriteSettings() {
  15. include_once $this->root . '/core/includes/install.inc';
  16. $site_path = $this->container->get('site.path');
  17. $tests = [
  18. [
  19. 'original' => '$no_index_value_scalar = TRUE;',
  20. 'settings' => [
  21. 'no_index_value_scalar' => (object) [
  22. 'value' => FALSE,
  23. 'comment' => 'comment',
  24. ],
  25. ],
  26. 'expected' => '$no_index_value_scalar = false; // comment',
  27. ],
  28. [
  29. 'original' => '$no_index_value_scalar = TRUE;',
  30. 'settings' => [
  31. 'no_index_value_foo' => [
  32. 'foo' => [
  33. 'value' => (object) [
  34. 'value' => NULL,
  35. 'required' => TRUE,
  36. 'comment' => 'comment',
  37. ],
  38. ],
  39. ],
  40. ],
  41. 'expected' => <<<'EXPECTED'
  42. $no_index_value_scalar = TRUE;
  43. $no_index_value_foo['foo']['value'] = NULL; // comment
  44. EXPECTED
  45. ],
  46. [
  47. 'original' => '$no_index_value_array = array("old" => "value");',
  48. 'settings' => [
  49. 'no_index_value_array' => (object) [
  50. 'value' => FALSE,
  51. 'required' => TRUE,
  52. 'comment' => 'comment',
  53. ],
  54. ],
  55. 'expected' => '$no_index_value_array = array("old" => "value");
  56. $no_index_value_array = false; // comment',
  57. ],
  58. [
  59. 'original' => '$has_index_value_scalar["foo"]["bar"] = NULL;',
  60. 'settings' => [
  61. 'has_index_value_scalar' => [
  62. 'foo' => [
  63. 'bar' => (object) [
  64. 'value' => FALSE,
  65. 'required' => TRUE,
  66. 'comment' => 'comment',
  67. ],
  68. ],
  69. ],
  70. ],
  71. 'expected' => '$has_index_value_scalar["foo"]["bar"] = false; // comment',
  72. ],
  73. [
  74. 'original' => '$has_index_value_scalar["foo"]["bar"] = "foo";',
  75. 'settings' => [
  76. 'has_index_value_scalar' => [
  77. 'foo' => [
  78. 'value' => (object) [
  79. 'value' => ['value' => 2],
  80. 'required' => TRUE,
  81. 'comment' => 'comment',
  82. ],
  83. ],
  84. ],
  85. ],
  86. 'expected' => <<<'EXPECTED'
  87. $has_index_value_scalar["foo"]["bar"] = "foo";
  88. $has_index_value_scalar['foo']['value'] = array (
  89. 'value' => 2,
  90. ); // comment
  91. EXPECTED
  92. ],
  93. ];
  94. foreach ($tests as $test) {
  95. $filename = Settings::get('file_public_path', $site_path . '/files') . '/mock_settings.php';
  96. file_put_contents($filename, "<?php\n" . $test['original'] . "\n");
  97. drupal_rewrite_settings($test['settings'], $filename);
  98. $this->assertEqual(file_get_contents($filename), "<?php\n" . $test['expected'] . "\n");
  99. }
  100. // Test that <?php gets added to the start of an empty settings file.
  101. // Set the array of settings that will be written to the file.
  102. $test = [
  103. 'settings' => [
  104. 'no_index' => (object) [
  105. 'value' => TRUE,
  106. 'required' => TRUE,
  107. ],
  108. ],
  109. 'expected' => '$no_index = true;',
  110. ];
  111. // Make an empty file.
  112. $filename = Settings::get('file_public_path', $site_path . '/files') . '/mock_settings.php';
  113. file_put_contents($filename, "");
  114. // Write the setting to the file.
  115. drupal_rewrite_settings($test['settings'], $filename);
  116. // Check that the result is just the php opening tag and the settings.
  117. $this->assertEqual(file_get_contents($filename), "<?php\n" . $test['expected'] . "\n");
  118. }
  119. }