Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Site / SettingsRewriteTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Site;
4
5 use Drupal\Core\Site\Settings;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests the drupal_rewrite_settings() function.
10  *
11  * @group system
12  */
13 class SettingsRewriteTest extends KernelTestBase {
14
15   /**
16    * Tests the drupal_rewrite_settings() function.
17    */
18   public function testDrupalRewriteSettings() {
19     include_once $this->root . '/core/includes/install.inc';
20     $site_path = $this->container->get('site.path');
21     $tests = [
22       [
23         'original' => '$no_index_value_scalar = TRUE;',
24         'settings' => [
25           'no_index_value_scalar' => (object) [
26             'value' => FALSE,
27             'comment' => 'comment',
28           ],
29         ],
30         'expected' => '$no_index_value_scalar = false; // comment',
31       ],
32       [
33         'original' => '$no_index_value_scalar = TRUE;',
34         'settings' => [
35           'no_index_value_foo' => [
36             'foo' => [
37               'value' => (object) [
38                 'value' => NULL,
39                 'required' => TRUE,
40                 'comment' => 'comment',
41               ],
42             ],
43           ],
44         ],
45         'expected' => <<<'EXPECTED'
46 $no_index_value_scalar = TRUE;
47 $no_index_value_foo['foo']['value'] = NULL; // comment
48 EXPECTED
49       ],
50       [
51         'original' => '$no_index_value_array = array("old" => "value");',
52         'settings' => [
53           'no_index_value_array' => (object) [
54             'value' => FALSE,
55             'required' => TRUE,
56             'comment' => 'comment',
57           ],
58         ],
59         'expected' => '$no_index_value_array = array("old" => "value");
60 $no_index_value_array = false; // comment',
61       ],
62       [
63         'original' => '$has_index_value_scalar["foo"]["bar"] = NULL;',
64         'settings' => [
65           'has_index_value_scalar' => [
66             'foo' => [
67               'bar' => (object) [
68                 'value' => FALSE,
69                 'required' => TRUE,
70                 'comment' => 'comment',
71               ],
72             ],
73           ],
74         ],
75         'expected' => '$has_index_value_scalar["foo"]["bar"] = false; // comment',
76       ],
77       [
78         'original' => '$has_index_value_scalar["foo"]["bar"] = "foo";',
79         'settings' => [
80           'has_index_value_scalar' => [
81             'foo' => [
82               'value' => (object) [
83                 'value' => ['value' => 2],
84                 'required' => TRUE,
85                 'comment' => 'comment',
86               ],
87             ],
88           ],
89         ],
90         'expected' => <<<'EXPECTED'
91 $has_index_value_scalar["foo"]["bar"] = "foo";
92 $has_index_value_scalar['foo']['value'] = array (
93   'value' => 2,
94 ); // comment
95 EXPECTED
96       ],
97     ];
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");
103     }
104
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.
107     $test = [
108       'settings' => [
109         'no_index' => (object) [
110           'value' => TRUE,
111           'required' => TRUE,
112         ],
113       ],
114       'expected' => '$no_index = true;',
115     ];
116     // Make an empty file.
117     $filename = Settings::get('file_public_path', $site_path . '/files') . '/mock_settings.php';
118     file_put_contents($filename, "");
119
120     // Write the setting to the file.
121     drupal_rewrite_settings($test['settings'], $filename);
122
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");
125   }
126
127 }