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

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

8 9
namespace Drupal\config\Tests;

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

13
/**
14
 * Tests reading and writing file contents.
15
 */
16
class ConfigFileContentTest extends WebTestBase {
17 18
  public static function getInfo() {
    return array(
19 20
      'name' => 'File content',
      'description' => 'Tests reading and writing of configuration files.',
21 22 23
      'group' => 'Configuration',
    );
  }
24 25

  /**
sun's avatar
sun committed
26
   * Tests setting, writing, and reading of a configuration setting.
27
   */
28
  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 37 38 39
    $nested_key = 'biff.bang';
    $nested_value = 'pow';
    $array_key = 'array';
    $array_value = array(
      'foo' => 'bar',
      'biff' => array(
gdd's avatar
gdd committed
40
        'bang' => 'pow',
41 42
      )
    );
gdd's avatar
gdd committed
43 44 45 46 47
    $casting_array_key = 'casting_array';
    $casting_array_false_value_key = 'casting_array.cast.false';
    $casting_array_value = array(
      'cast' => array(
        '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 = config($name);
56

sun's avatar
sun committed
57
    // Verify an configuration object is returned.
58
    $this->assertEqual($config->getName(), $name);
59
    $this->assertTrue($config, 'Config object created.');
60

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

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

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

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

75 76 77 78 79 80 81 82 83 84 85
    // Add an array
    $config->set($array_key, $array_value);

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

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

    // Add a boolean true value. Should get cast to 1
    $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 96
    $data = $storage->read($name);
    $this->assertTrue($data);
sun's avatar
sun committed
97

98
    // Read top level value
sun's avatar
sun committed
99
    $config = config($name);
100
    $this->assertEqual($config->getName(), $name);
101
    $this->assertTrue($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_dont_exist'), 'Non-existent top level value returned NULL.');
115 116

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

    // Read false value
120
    $this->assertEqual($config->get($false_key), '0', format_string("Boolean FALSE value returned the string '0'."));
121 122

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

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

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

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

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

gdd's avatar
gdd committed
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

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

gdd's avatar
gdd committed
150 151 152 153
    // Test chained set()->save()
    $chained_name = 'biff.bang';
    $config = config($chained_name);
    $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');
gdd's avatar
gdd committed
162
    $this->assertEqual(count($files), 2, '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');
gdd's avatar
gdd committed
167
    $this->assertEqual(count($files), 1, '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');
gdd's avatar
gdd committed
172 173
    $this->assertEqual(count($files), 1, 'One file listed with the prefix \'foo.bar\'.');

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');
gdd's avatar
gdd committed
177 178
    $this->assertEqual($files, array(), 'No files listed with the prefix \'bar\'.');

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

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

  /**
   * Tests serialization of configuration to file.
   */
191
  function testSerialization() {
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    $name = $this->randomName(10) . '.' . $this->randomName(10);
    $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> & < > " \' ',
    );

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

    $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]);
  }
231
}