3 namespace Drupal\KernelTests\Core\Site;
5 use Drupal\Core\Site\Settings;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests the drupal_rewrite_settings() function.
13 class SettingsRewriteTest extends KernelTestBase {
16 * Tests the drupal_rewrite_settings() function.
18 public function testDrupalRewriteSettings() {
19 include_once $this->root . '/core/includes/install.inc';
20 $site_path = $this->container->get('site.path');
23 'original' => '$no_index_value_scalar = TRUE;',
25 'no_index_value_scalar' => (object) [
27 'comment' => 'comment',
30 'expected' => '$no_index_value_scalar = false; // comment',
33 'original' => '$no_index_value_scalar = TRUE;',
35 'no_index_value_foo' => [
40 'comment' => 'comment',
45 'expected' => <<<'EXPECTED'
46 $no_index_value_scalar = TRUE;
47 $no_index_value_foo['foo']['value'] = NULL; // comment
51 'original' => '$no_index_value_array = array("old" => "value");',
53 'no_index_value_array' => (object) [
56 'comment' => 'comment',
59 'expected' => '$no_index_value_array = array("old" => "value");
60 $no_index_value_array = false; // comment',
63 'original' => '$has_index_value_scalar["foo"]["bar"] = NULL;',
65 'has_index_value_scalar' => [
70 'comment' => 'comment',
75 'expected' => '$has_index_value_scalar["foo"]["bar"] = false; // comment',
78 'original' => '$has_index_value_scalar["foo"]["bar"] = "foo";',
80 'has_index_value_scalar' => [
83 'value' => ['value' => 2],
85 'comment' => 'comment',
90 'expected' => <<<'EXPECTED'
91 $has_index_value_scalar["foo"]["bar"] = "foo";
92 $has_index_value_scalar['foo']['value'] = array (
98 foreach ($tests as $test) {
99 $filename = Settings::get('file_public_path', $site_path . '/files') . '/mock_settings.php';
100 file_put_contents($filename, "<?php\n" . $test['original'] . "\n");
101 drupal_rewrite_settings($test['settings'], $filename);
102 $this->assertEqual(file_get_contents($filename), "<?php\n" . $test['expected'] . "\n");
105 // Test that <?php gets added to the start of an empty settings file.
106 // Set the array of settings that will be written to the file.
109 'no_index' => (object) [
114 'expected' => '$no_index = true;',
116 // Make an empty file.
117 $filename = Settings::get('file_public_path', $site_path . '/files') . '/mock_settings.php';
118 file_put_contents($filename, "");
120 // Write the setting to the file.
121 drupal_rewrite_settings($test['settings'], $filename);
123 // Check that the result is just the php opening tag and the settings.
124 $this->assertEqual(file_get_contents($filename), "<?php\n" . $test['expected'] . "\n");