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

3
/**
Crell's avatar
Crell committed
4
 * @file
5
 * Contains \Drupal\config\Tests\ConfigFileContentTest.
6 7
 */

8 9
namespace Drupal\config\Tests;

10
use Drupal\Core\Config\FileStorage;
11
use Drupal\simpletest\KernelTestBase;
12

13
/**
14 15 16
 * Tests reading and writing of configuration files.
 *
 * @group config
17
 */
18
class ConfigFileContentTest extends KernelTestBase {
19 20 21 22 23 24 25 26 27 28

  /**
   * Exempt from strict schema checking.
   *
   * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
   *
   * @var bool
   */
  protected $strictConfigSchema = FALSE;

29
  /**
sun's avatar
sun committed
30
   * Tests setting, writing, and reading of a configuration setting.
31
   */
32
  function testReadWriteConfig() {
33
    $storage = $this->container->get('config.storage');
34

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

sun's avatar
sun committed
58
    // Attempt to read non-existing configuration.
59
    $config = $this->config($name);
60

61
    // Verify a configuration object is returned.
62
    $this->assertEqual($config->getName(), $name);
63
    $this->assertTrue($config, 'Config object created.');
64

sun's avatar
sun committed
65
    // Verify the configuration object is empty.
66
    $this->assertEqual($config->get(), array(), 'New config object is empty.');
67

sun's avatar
sun committed
68
    // Verify nothing was saved.
69 70
    $data = $storage->read($name);
    $this->assertIdentical($data, FALSE);
sun's avatar
sun committed
71

72
    // Add a top level value.
73
    $config = $this->config($name);
sun's avatar
sun committed
74
    $config->set($key, $value);
75

76
    // Add a nested value.
77
    $config->set($nested_key, $nested_value);
78

79
    // Add an array.
80 81
    $config->set($array_key, $array_value);

82
    // Add a nested array.
83 84
    $config->set($nested_array_key, $array_value);

85
    // Add a boolean false value. Should get cast to 0.
86 87
    $config->set($false_key, FALSE);

88
    // Add a boolean true value. Should get cast to 1.
89
    $config->set($true_key, TRUE);
90

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

94 95
    // Add an array with a nested boolean false that should get cast to 0.
    $config->set($casting_array_key, $casting_array_value);
96
    $config->save();
97

sun's avatar
sun committed
98
    // Verify the database entry exists.
99 100
    $data = $storage->read($name);
    $this->assertTrue($data);
sun's avatar
sun committed
101

102
    // Read top level value.
103
    $config = $this->config($name);
104
    $this->assertEqual($config->getName(), $name);
105
    $this->assertTrue($config, 'Config object created.');
106
    $this->assertEqual($config->get($key), 'bar', 'Top level configuration value found.');
107

108
    // Read nested value.
109
    $this->assertEqual($config->get($nested_key), $nested_value, 'Nested configuration value found.');
110

111
    // Read array.
112
    $this->assertEqual($config->get($array_key), $array_value, 'Top level array configuration value found.');
113

114
    // Read nested array.
115
    $this->assertEqual($config->get($nested_array_key), $array_value, 'Nested array configuration value found.');
116

117
    // Read a top level value that doesn't exist.
118
    $this->assertNull($config->get('i_dont_exist'), 'Non-existent top level value returned NULL.');
119

120
    // Read a nested value that doesn't exist.
121
    $this->assertNull($config->get('i.dont.exist'), 'Non-existent nested value returned NULL.');
122

123
    // Read false value.
124
    $this->assertEqual($config->get($false_key), '0', "Boolean FALSE value returned the string '0'.");
125

126
    // Read true value.
127
    $this->assertEqual($config->get($true_key), '1', "Boolean TRUE value returned the string '1'.");
sun's avatar
sun committed
128

129
    // Read null value.
130
    $this->assertIdentical($config->get('null'), NULL);
131

132
    // Read false that had been nested in an array value.
133
    $this->assertEqual($config->get($casting_array_false_value_key), '0', "Nested boolean FALSE value returned the string '0'.");
134

135
    // Unset a top level value.
gdd's avatar
gdd committed
136
    $config->clear($key);
137

138
    // Unset a nested value.
gdd's avatar
gdd committed
139 140
    $config->clear($nested_key);
    $config->save();
141
    $config = $this->config($name);
142

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

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

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

154
    // Test chained set()->save().
155
    $chained_name = 'biff.bang';
156
    $config = $this->config($chained_name);
157
    $config->set($key, $value)->save();
158

159
    // Verify the database entry exists from a chained save.
160 161
    $data = $storage->read($chained_name);
    $this->assertEqual($data, $config->get());
162

gdd's avatar
gdd committed
163 164
    // Get file listing for all files starting with 'foo'. Should return
    // two elements.
165
    $files = $storage->listAll('foo');
gdd's avatar
gdd committed
166
    $this->assertEqual(count($files), 2, 'Two files listed with the prefix \'foo\'.');
167

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

    // Get file listing for all files starting with 'foo.bar'. Should return
gdd's avatar
gdd committed
174
    // one element.
175
    $files = $storage->listAll('foo.bar');
gdd's avatar
gdd committed
176 177
    $this->assertEqual(count($files), 1, 'One file listed with the prefix \'foo.bar\'.');

178
    // Get file listing for all files starting with 'bar'. Should return
gdd's avatar
gdd committed
179
    // an empty array.
180
    $files = $storage->listAll('bar');
gdd's avatar
gdd committed
181 182
    $this->assertEqual($files, array(), 'No files listed with the prefix \'bar\'.');

sun's avatar
sun committed
183
    // Delete the configuration.
184
    $config = $this->config($name);
sun's avatar
sun committed
185
    $config->delete();
186

sun's avatar
sun committed
187
    // Verify the database entry no longer exists.
188 189
    $data = $storage->read($name);
    $this->assertIdentical($data, FALSE);
190
  }
191 192 193 194

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

212
    // Encode and write, and reload and decode the configuration data.
213
    $filestorage = new FileStorage($this->configDirectories[CONFIG_SYNC_DIRECTORY]);
214 215
    $filestorage->write($name, $config_data);
    $config_parsed = $filestorage->read($name);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

    $key = 'numeric keys';
    $this->assertIdentical($config_data[$key], $config_parsed[$key]);

    $key = 'nested keys';
    $this->assertIdentical($config_data[$key], $config_parsed[$key]);

    $key = 'HTML';
    $this->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);

    $key = 'UTF-8';
    $this->assertIdentical($config_data['nested keys'][$key], $config_parsed['nested keys'][$key]);

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

    $key = 'invalid xml';
    $this->assertIdentical($config_data[$key], $config_parsed[$key]);
  }
235
}