config.test 11.6 KB
Newer Older
Crell's avatar
Crell committed
1 2
<?php

3
/**
Crell's avatar
Crell committed
4
 * @file
5
 * Tests for the Configuration module.
6 7
 */

8 9
use Drupal\Core\Config\SignedFileStorage;

10
/**
11
 * Tests the secure file writer.
12
 */
13
class ConfigFileSecurityTestCase extends DrupalWebTestCase {
14 15
  protected $profile = 'testing';

16 17 18 19
  protected $filename = 'foo.bar';

  protected $testContent = 'Good morning, Denver!';

20 21
  public static function getInfo() {
    return array(
22 23
      'name' => 'File security',
      'description' => 'Tests security of saved configuration files.',
24 25 26 27 28 29 30
      'group' => 'Configuration',
    );
  }

  /**
   * Tests that a file written by this system has a valid signature.
   */
31
  function testFileVerify() {
32
    $file = new SignedFileStorage($this->filename);
33
    $file->write($this->testContent);
34

35
    $this->assertTrue($file->verify(), 'A file verifies after being written.');
36

37
    unset($file);
38

39
    // Load the file again, so that there is no stale data from the old object.
40
    $file = new SignedFileStorage($this->filename);
41
    $this->assertTrue($file->verify(), 'A file verifies after being written and reloaded.');
42 43 44 45 46
  }

  /**
   * Tests that a file written by this system can be successfully read back.
   */
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  function testFilePersist() {
    $file = new SignedFileStorage($this->filename);
    $file->write($this->testContent);

    unset($file);

    // Reading should throw an exception in case of bad validation.
    // Note that if any other exception is thrown, we let the test system
    // handle catching and reporting it.
    try {
      $file = new SignedFileStorage($this->filename);
      $saved_content = $file->read();

      $this->assertEqual($saved_content, $this->testContent, 'A file can be read back successfully.');
    }
    catch (Exception $e) {
      $this->fail('File failed verification when being read.');
    }
  }

  /**
   * Tests that a file fails validation if it's been monkeyed with.
   */
  function testFileNotVerify() {
    $file = new SignedFileStorage($this->filename);
    $file->write($this->testContent);

    // Manually overwrite the body of the secure file. Note that we skip the
    // first line, which is reserved for the signature and such, to overwrite
    // just the payload.
    $raw_file = new SplFileObject($file->getFilePath(), 'a+');
    $raw_file->fwrite('Good morning, Detroit!');
    $raw_file->fflush();
    unset($raw_file);

    unset($file);

    $file = new SignedFileStorage($this->filename);
    $this->assertFalse($file->verify(), 'Corrupted file does not verify.');
  }
87
}
88

89
/**
90
 * Tests reading and writing file contents.
91
 */
92
class ConfigFileContentTestCase extends DrupalWebTestCase {
93 94
  protected $profile = 'testing';

sun's avatar
sun committed
95 96
  protected $fileExtension = 'xml';

97 98
  public static function getInfo() {
    return array(
99 100
      'name' => 'File content',
      'description' => 'Tests reading and writing of configuration files.',
101 102 103
      'group' => 'Configuration',
    );
  }
104 105

  /**
sun's avatar
sun committed
106
   * Tests setting, writing, and reading of a configuration setting.
107
   */
108
  function testReadWriteConfig() {
sun's avatar
sun committed
109
    $config_dir = config_get_config_directory();
sun's avatar
sun committed
110 111 112
    $name = 'foo.bar';
    $key = 'foo';
    $value = 'bar';
113 114 115 116 117 118
    $nested_key = 'biff.bang';
    $nested_value = 'pow';
    $array_key = 'array';
    $array_value = array(
      'foo' => 'bar',
      'biff' => array(
gdd's avatar
gdd committed
119
        'bang' => 'pow',
120 121
      )
    );
gdd's avatar
gdd committed
122 123 124 125 126
    $casting_array_key = 'casting_array';
    $casting_array_false_value_key = 'casting_array.cast.false';
    $casting_array_value = array(
      'cast' => array(
        'false' => FALSE,
127 128 129 130 131
      )
    );
    $nested_array_key = 'nested.array';
    $true_key = 'true';
    $false_key = 'false';
132

sun's avatar
sun committed
133
    // Attempt to read non-existing configuration.
134
    $config = config($name);
135

sun's avatar
sun committed
136 137
    // Verify an configuration object is returned.
//    $this->assertEqual($config->name, $name);
138 139
    $this->assertTrue($config, t('Config object created.'));

sun's avatar
sun committed
140
    // Verify the configuration object is empty.
141 142
    $this->assertEqual($config->get(), array(), t('New config object is empty.'));

sun's avatar
sun committed
143
    // Verify nothing was saved.
144
    $db_config = db_query('SELECT * FROM {config} WHERE name = :name', array(':name' => $name))->fetch();
145 146
    $this->assertIdentical($db_config, FALSE, t('Active store does not have a record for %name', array('%name' => $name)));
    $this->assertFalse(file_exists($config_dir . '/' . $name . '.' . $this->fileExtension), 'Configuration file does not exist.');
sun's avatar
sun committed
147

148
    // Add a top level value
sun's avatar
sun committed
149 150
    $config = config($name);
    $config->set($key, $value);
151 152 153

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

155 156 157 158 159 160 161 162 163 164 165
    // 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
166 167 168

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

sun's avatar
sun committed
171 172
    // Verify the database entry exists.
    $db_config = db_query('SELECT * FROM {config} WHERE name = :name', array(':name' => $name))->fetch();
173 174
    $this->assertEqual($db_config->name, $name, t('After saving configuration, active store has a record for %name', array('%name' => $name)));

sun's avatar
sun committed
175
    // Verify the file exists.
176
    $this->assertTrue(file_exists($config_dir . '/' . $name . '.' . $this->fileExtension), t('After saving configuration, config file exists.'));
sun's avatar
sun committed
177

178
    // Read top level value
sun's avatar
sun committed
179 180
    $config = config($name);
//    $this->assertEqual($config->name, $name);
181 182 183 184 185 186 187
    $this->assertTrue($config, 'Config object created.');
    $this->assertEqual($config->get($key), 'bar', t('Top level configuration value found.'));

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

    // Read array
gdd's avatar
gdd committed
188
    $this->assertEqual($config->get($array_key), $array_value, t('Top level array configuration value found.'));
189

190
    // Read nested array
gdd's avatar
gdd committed
191
    $this->assertEqual($config->get($nested_array_key), $array_value, t('Nested array configuration value found.'));
192 193 194 195 196 197 198 199 200 201 202 203

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

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

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

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

gdd's avatar
gdd committed
205 206 207
    // Read false that had been nested in an array value
    $this->assertEqual($config->get($casting_array_false_value_key), '0', t('Nested boolean FALSE value returned the string \'0\'.'));

gdd's avatar
gdd committed
208 209
    // Unset a top level value
    $config->clear($key);
210

gdd's avatar
gdd committed
211 212 213 214
    // Unset a nested value
    $config->clear($nested_key);
    $config->save();
    $config = config($name);
215

gdd's avatar
gdd committed
216 217 218 219 220 221
    // Read unset top level value
    $this->assertNull($config->get($key), t('Top level value unset.'));

    // Read unset nested value
    $this->assertNull($config->get($nested_key), t('Nested value unset.'));

gdd's avatar
gdd committed
222 223 224 225
    // Create two new configuration files to test listing
    $config = config('foo.baz');
    $config->set($key, $value);
    $config->save();
226

gdd's avatar
gdd committed
227 228 229 230
    // Test chained set()->save()
    $chained_name = 'biff.bang';
    $config = config($chained_name);
    $config->set($key, $value)->save();
231

gdd's avatar
gdd committed
232 233 234 235 236 237 238
    // Verify the database entry exists from a chained save.
    $db_config = db_query('SELECT * FROM {config} WHERE name = :name', array(':name' => $chained_name))->fetch();
    $this->assertEqual($db_config->name, $chained_name, t('After saving configuration by chaining through set(), active store has a record for %name', array('%name' => $chained_name)));

    // Verify the file exists from a chained save.
    $this->assertTrue(file_exists($config_dir . '/' . $chained_name . '.' . $this->fileExtension), t('After saving configuration by chaining through set(), config file exists.'));

gdd's avatar
gdd committed
239 240 241 242
    // Get file listing for all files starting with 'foo'. Should return
    // two elements.
    $files = config_get_signed_file_storage_names_with_prefix('foo');
    $this->assertEqual(count($files), 2, 'Two files listed with the prefix \'foo\'.');
243

gdd's avatar
gdd committed
244 245 246 247
    // Get file listing for all files starting with 'biff'. Should return
    // one element.
    $files = config_get_signed_file_storage_names_with_prefix('biff');
    $this->assertEqual(count($files), 1, 'One file listed with the prefix \'biff\'.');
248 249

    // Get file listing for all files starting with 'foo.bar'. Should return
gdd's avatar
gdd committed
250 251 252 253
    // one element.
    $files = config_get_signed_file_storage_names_with_prefix('foo.bar');
    $this->assertEqual(count($files), 1, 'One file listed with the prefix \'foo.bar\'.');

254
    // Get file listing for all files starting with 'bar'. Should return
gdd's avatar
gdd committed
255 256 257 258
    // an empty array.
    $files = config_get_signed_file_storage_names_with_prefix('bar');
    $this->assertEqual($files, array(), 'No files listed with the prefix \'bar\'.');

sun's avatar
sun committed
259 260 261
    // Delete the configuration.
    $config = config($name);
    $config->delete();
262

sun's avatar
sun committed
263 264 265 266
    // Verify the database entry no longer exists.
    $db_config = db_query('SELECT * FROM {config} WHERE name = :name', array(':name' => $name))->fetch();
    $this->assertIdentical($db_config, FALSE);
    $this->assertFalse(file_exists($config_dir . '/' . $name . '.' . $this->fileExtension));
267

sun's avatar
sun committed
268
    // Attempt to delete non-existing configuration.
269 270
  }
}
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

  /**
   * Tests configuration overriding from settings.php.
   */
class ConfOverrideTestCase extends DrupalWebTestCase {
  protected $testContent = 'Good morning, Denver!';

  public static function getInfo() {
    return array(
      'name' => 'Configuration overrides',
      'description' => 'Tests configuration overrides through settings.php.',
      'group' => 'Configuration',
    );
  }

  /**
   * Test configuration override.
   */
  function testConfigurationOverride() {
    global $conf;
    $config = config('system.performance');
    $this->assertNotEqual($config->get('cache'), $this->testContent);

    $conf['system.performance']['cache'] = $this->testContent;
    $config = config('system.performance');
    $this->assertEqual($config->get('cache'), $conf['system.performance']['cache']);
  }
}
299 300 301 302 303 304 305 306 307 308

/**
 * Tests function providing configuration upgrade from Drupal 7 to 8.
 */
class ConfUpdate7to8TestCase extends DrupalWebTestCase {
  protected $testContent = 'Olá, Sao Paulo!';

  public static function getInfo() {
    return array(
      'name' => 'Configuration update from Drupal 7 to 8',
309
      'description' => 'Tests the ability to update Drupal 7 variables to the
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        configuration management system.',
      'group' => 'Configuration',
    );
  }

  function setUp() {
    parent::setUp('config_upgrade');
    require_once DRUPAL_ROOT . '/core/includes/update.inc';
  }

  /**
   * Test configuration update function.
   */
  function testConfigurationUpdate() {
    // Ensure that the variable table has the object. The variable table will
    // remain in place for Drupal 8 to provide an upgrade path for overridden
    // variables.
    db_merge('variable')->key(array('name' => 'config_test_foo'))->fields(array('value' => serialize($this->testContent)))->execute();
    db_merge('variable')->key(array('name' => 'config_bar'))->fields(array('value' => serialize($this->testContent)))->execute();

    update_variables_to_config('config.test', array('config_test_bar' => 'config_bar'));
    $config = config('config.test');
332 333
    $this->assertEqual($config->get('config_test_foo'), $this->testContent);
    $this->assertEqual($config->get('config_test_bar'), $this->testContent);
334 335
  }
}