ConfigFileContentTest.php 7.47 KB
Newer Older
Crell's avatar
Crell committed
1 2
<?php

3
namespace Drupal\KernelTests\Core\Config;
4

5
use Drupal\Core\Config\FileStorage;
6
use Drupal\Core\Site\Settings;
7
use Drupal\KernelTests\KernelTestBase;
8

9
/**
10 11 12
 * Tests reading and writing of configuration files.
 *
 * @group config
13
 */
14
class ConfigFileContentTest extends KernelTestBase {
15 16 17 18

  /**
   * Exempt from strict schema checking.
   *
19
   * @see \Drupal\Core\Config\Development\ConfigSchemaChecker
20 21 22 23 24
   *
   * @var bool
   */
  protected $strictConfigSchema = FALSE;

25
  /**
sun's avatar
sun committed
26
   * Tests setting, writing, and reading of a configuration setting.
27
   */
28
  public function testReadWriteConfig() {
29
    $storage = $this->container->get('config.storage');
30

sun's avatar
sun committed
31 32 33
    $name = 'foo.bar';
    $key = 'foo';
    $value = 'bar';
34 35 36
    $nested_key = 'biff.bang';
    $nested_value = 'pow';
    $array_key = 'array';
37
    $array_value = [
38
      'foo' => 'bar',
39
      'biff' => [
gdd's avatar
gdd committed
40
        'bang' => 'pow',
41 42
      ],
    ];
gdd's avatar
gdd committed
43 44
    $casting_array_key = 'casting_array';
    $casting_array_false_value_key = 'casting_array.cast.false';
45 46
    $casting_array_value = [
      'cast' => [
gdd's avatar
gdd committed
47
        'false' => FALSE,
48 49
      ],
    ];
50 51 52
    $nested_array_key = 'nested.array';
    $true_key = 'true';
    $false_key = 'false';
53

sun's avatar
sun committed
54
    // Attempt to read non-existing configuration.
55
    $config = $this->config($name);
56

57
    // Verify a configuration object is returned.
58
    $this->assertEqual($config->getName(), $name);
59
    $this->assertNotEmpty($config, 'Config object created.');
60

sun's avatar
sun committed
61
    // Verify the configuration object is empty.
62
    $this->assertEqual($config->get(), [], 'New config object is empty.');
63

sun's avatar
sun committed
64
    // Verify nothing was saved.
65
    $data = $storage->read($name);
66
    $this->assertFalse($data);
sun's avatar
sun committed
67

68
    // Add a top level value.
69
    $config = $this->config($name);
sun's avatar
sun committed
70
    $config->set($key, $value);
71

72
    // Add a nested value.
73
    $config->set($nested_key, $nested_value);
74

75
    // Add an array.
76 77
    $config->set($array_key, $array_value);

78
    // Add a nested array.
79 80
    $config->set($nested_array_key, $array_value);

81
    // Add a boolean false value. Should get cast to 0.
82 83
    $config->set($false_key, FALSE);

84
    // Add a boolean true value. Should get cast to 1.
85
    $config->set($true_key, TRUE);
gdd's avatar
gdd committed
86

87 88 89
    // Add a null value. Should get cast to an empty string.
    $config->set('null', NULL);

gdd's avatar
gdd committed
90 91
    // Add an array with a nested boolean false that should get cast to 0.
    $config->set($casting_array_key, $casting_array_value);
92
    $config->save();
93

sun's avatar
sun committed
94
    // Verify the database entry exists.
95
    $data = $storage->read($name);
96
    $this->assertNotEmpty($data);
sun's avatar
sun committed
97

98
    // Read top level value.
99
    $config = $this->config($name);
100
    $this->assertEqual($config->getName(), $name);
101
    $this->assertNotEmpty($config, 'Config object created.');
102
    $this->assertEqual($config->get($key), 'bar', 'Top level configuration value found.');
103

104
    // Read nested value.
105
    $this->assertEqual($config->get($nested_key), $nested_value, 'Nested configuration value found.');
106

107
    // Read array.
108
    $this->assertEqual($config->get($array_key), $array_value, 'Top level array configuration value found.');
109

110
    // Read nested array.
111
    $this->assertEqual($config->get($nested_array_key), $array_value, 'Nested array configuration value found.');
112

113
    // Read a top level value that doesn't exist.
114
    $this->assertNull($config->get('i_do_not_exist'), 'Non-existent top level value returned NULL.');
115

116
    // Read a nested value that doesn't exist.
117
    $this->assertNull($config->get('i.do.not.exist'), 'Non-existent nested value returned NULL.');
118

119
    // Read false value.
120
    $this->assertFalse($config->get($false_key), "Boolean FALSE value returned the FALSE.");
121

122
    // Read true value.
123
    $this->assertTrue($config->get($true_key), "Boolean TRUE value returned the TRUE.");
sun's avatar
sun committed
124

125
    // Read null value.
126
    $this->assertIdentical($config->get('null'), NULL);
127

128
    // Read false that had been nested in an array value.
129
    $this->assertFalse($config->get($casting_array_false_value_key), "Nested boolean FALSE value returned FALSE.");
gdd's avatar
gdd committed
130

131
    // Unset a top level value.
gdd's avatar
gdd committed
132
    $config->clear($key);
133

134
    // Unset a nested value.
gdd's avatar
gdd committed
135 136
    $config->clear($nested_key);
    $config->save();
137
    $config = $this->config($name);
138

139
    // Read unset top level value.
140
    $this->assertNull($config->get($key), 'Top level value unset.');
gdd's avatar
gdd committed
141

142
    // Read unset nested value.
143
    $this->assertNull($config->get($nested_key), 'Nested value unset.');
gdd's avatar
gdd committed
144

145
    // Create two new configuration files to test listing.
146
    $config = $this->config('foo.baz');
gdd's avatar
gdd committed
147 148
    $config->set($key, $value);
    $config->save();
149

150
    // Test chained set()->save().
gdd's avatar
gdd committed
151
    $chained_name = 'biff.bang';
152
    $config = $this->config($chained_name);
gdd's avatar
gdd committed
153
    $config->set($key, $value)->save();
154

gdd's avatar
gdd committed
155
    // Verify the database entry exists from a chained save.
156 157
    $data = $storage->read($chained_name);
    $this->assertEqual($data, $config->get());
gdd's avatar
gdd committed
158

gdd's avatar
gdd committed
159 160
    // Get file listing for all files starting with 'foo'. Should return
    // two elements.
161
    $files = $storage->listAll('foo');
162
    $this->assertCount(2, $files, 'Two files listed with the prefix \'foo\'.');
163

gdd's avatar
gdd committed
164 165
    // Get file listing for all files starting with 'biff'. Should return
    // one element.
166
    $files = $storage->listAll('biff');
167
    $this->assertCount(1, $files, 'One file listed with the prefix \'biff\'.');
168 169

    // Get file listing for all files starting with 'foo.bar'. Should return
gdd's avatar
gdd committed
170
    // one element.
171
    $files = $storage->listAll('foo.bar');
172
    $this->assertCount(1, $files, 'One file listed with the prefix \'foo.bar\'.');
gdd's avatar
gdd committed
173

174
    // Get file listing for all files starting with 'bar'. Should return
gdd's avatar
gdd committed
175
    // an empty array.
176
    $files = $storage->listAll('bar');
177
    $this->assertEqual($files, [], 'No files listed with the prefix \'bar\'.');
gdd's avatar
gdd committed
178

sun's avatar
sun committed
179
    // Delete the configuration.
180
    $config = $this->config($name);
sun's avatar
sun committed
181
    $config->delete();
182

sun's avatar
sun committed
183
    // Verify the database entry no longer exists.
184
    $data = $storage->read($name);
185
    $this->assertFalse($data);
186
  }
187 188 189 190

  /**
   * Tests serialization of configuration to file.
   */
191
  public function testSerialization() {
192
    $name = $this->randomMachineName(10) . '.' . $this->randomMachineName(10);
193
    $config_data = [
194
      // Indexed arrays; the order of elements is essential.
195
      'numeric keys' => ['i', 'n', 'd', 'e', 'x', 'e', 'd'],
196
      // Infinitely nested keys using arbitrary element names.
197
      'nested keys' => [
198 199 200 201 202 203
        // 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.
        'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ' => 'αβγδεζηθικλμνξοσὠ',
204
      ],
205
      'invalid xml' => '</title><script type="text/javascript">alert("Title XSS!");</script> & < > " \' ',
206
    ];
207

208
    // Encode and write, and reload and decode the configuration data.
209
    $filestorage = new FileStorage(Settings::get('config_sync_directory'));
210 211
    $filestorage->write($name, $config_data);
    $config_parsed = $filestorage->read($name);
212 213

    $key = 'numeric keys';
214
    $this->assertSame($config_data[$key], $config_parsed[$key]);
215 216

    $key = 'nested keys';
217
    $this->assertSame($config_data[$key], $config_parsed[$key]);
218 219

    $key = 'HTML';
220
    $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
221 222

    $key = 'UTF-8';
223
    $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
224 225

    $key = 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ';
226
    $this->assertSame($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);
227 228

    $key = 'invalid xml';
229
    $this->assertSame($config_data[$key], $config_parsed[$key]);
230
  }
231

232
}