ConfigCRUDTest.php 9.06 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\config\Tests\ConfigCRUDTest.
 */

namespace Drupal\config\Tests;

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\Config\ConfigNameException;
12
use Drupal\simpletest\DrupalUnitTestBase;
13
use Drupal\Core\Config\FileStorage;
14
use Drupal\Core\Config\DatabaseStorage;
15
use Drupal\Core\Config\UnsupportedDataTypeConfigException;
16 17 18 19

/**
 * Tests CRUD operations on configuration objects.
 */
20
class ConfigCRUDTest extends DrupalUnitTestBase {
21 22 23 24 25 26 27 28

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('system');

29 30 31 32 33 34 35 36 37 38 39 40
  public static function getInfo() {
    return array(
      'name' => 'CRUD operations',
      'description' => 'Tests CRUD operations on configuration objects.',
      'group' => 'Configuration',
    );
  }

  /**
   * Tests CRUD operations.
   */
  function testCRUD() {
41
    $storage = $this->container->get('config.storage');
42 43
    $name = 'config_test.crud';

44
    $config = \Drupal::config($name);
45 46 47
    $this->assertIdentical($config->isNew(), TRUE);

    // Create a new configuration object.
48 49
    $config->set('value', 'initial');
    $config->save();
50
    $this->assertIdentical($config->isNew(), FALSE);
51

52
    // Verify the active configuration contains the saved value.
53 54 55 56 57 58
    $actual_data = $storage->read($name);
    $this->assertIdentical($actual_data, array('value' => 'initial'));

    // Update the configuration object instance.
    $config->set('value', 'instance-update');
    $config->save();
59
    $this->assertIdentical($config->isNew(), FALSE);
60

61
    // Verify the active configuration contains the updated value.
62 63 64
    $actual_data = $storage->read($name);
    $this->assertIdentical($actual_data, array('value' => 'instance-update'));

65 66
    // Verify a call to \Drupal::config() immediately returns the updated value.
    $new_config = \Drupal::config($name);
67
    $this->assertIdentical($new_config->get(), $config->get());
68
    $this->assertIdentical($config->isNew(), FALSE);
69 70 71 72 73 74

    // Delete the configuration object.
    $config->delete();

    // Verify the configuration object is empty.
    $this->assertIdentical($config->get(), array());
75
    $this->assertIdentical($config->isNew(), TRUE);
76

77
    // Verify the active configuration contains no value.
78
    $actual_data = $storage->read($name);
79
    $this->assertIdentical($actual_data, FALSE);
80

81 82
    // Verify \Drupal::config() returns no data.
    $new_config = \Drupal::config($name);
83
    $this->assertIdentical($new_config->get(), $config->get());
84
    $this->assertIdentical($config->isNew(), TRUE);
85 86 87 88

    // Re-create the configuration object.
    $config->set('value', 're-created');
    $config->save();
89
    $this->assertIdentical($config->isNew(), FALSE);
90

91
    // Verify the active configuration contains the updated value.
92 93 94
    $actual_data = $storage->read($name);
    $this->assertIdentical($actual_data, array('value' => 're-created'));

95 96
    // Verify a call to \Drupal::config() immediately returns the updated value.
    $new_config = \Drupal::config($name);
97
    $this->assertIdentical($new_config->get(), $config->get());
98
    $this->assertIdentical($config->isNew(), FALSE);
99 100 101

    // Rename the configuration object.
    $new_name = 'config_test.crud_rename';
102
    $this->container->get('config.factory')->rename($name, $new_name);
103
    $renamed_config = \Drupal::config($new_name);
104 105 106 107 108
    $this->assertIdentical($renamed_config->get(), $config->get());
    $this->assertIdentical($renamed_config->isNew(), FALSE);

    // Ensure that the old configuration object is removed from both the cache
    // and the configuration storage.
109
    $config = \Drupal::config($name);
110 111 112 113 114 115
    $this->assertIdentical($config->get(), array());
    $this->assertIdentical($config->isNew(), TRUE);

    // Test renaming when config.factory does not have the object in its static
    // cache.
    $name = 'config_test.crud_rename';
116
    $config = \Drupal::config($name);
117 118
    $new_name = 'config_test.crud_rename_no_cache';
    $this->container->get('config.factory')->clearStaticCache()->rename($name, $new_name);
119
    $renamed_config = \Drupal::config($new_name);
120 121
    $this->assertIdentical($renamed_config->get(), $config->get());
    $this->assertIdentical($renamed_config->isNew(), FALSE);
122 123

    // Merge data into the configuration object.
124
    $new_config = \Drupal::config($new_name);
125 126 127 128 129 130 131 132
    $expected_values = array(
      'value' => 'herp',
      '404' => 'derp',
    );
    $new_config->merge($expected_values);
    $new_config->save();
    $this->assertIdentical($new_config->get('value'), $expected_values['value']);
    $this->assertIdentical($new_config->get('404'), $expected_values['404']);
133 134
  }

135 136 137 138 139 140 141 142
  /**
   * Tests the validation of configuration object names.
   */
  function testNameValidation() {
    // Verify that an object name without namespace causes an exception.
    $name = 'nonamespace';
    $message = 'Expected ConfigNameException was thrown for a name without a namespace.';
    try {
143
      \Drupal::config($name)->save();
144 145 146 147 148 149 150 151 152 153
      $this->fail($message);
    }
    catch (ConfigNameException $e) {
      $this->pass($message);
    }

    // Verify that a name longer than the maximum length causes an exception.
    $name = 'config_test.herman_melville.moby_dick_or_the_whale.harper_1851.now_small_fowls_flew_screaming_over_the_yet_yawning_gulf_a_sullen_white_surf_beat_against_its_steep_sides_then_all_collapsed_and_the_great_shroud_of_the_sea_rolled_on_as_it_rolled_five_thousand_years_ago';
    $message = 'Expected ConfigNameException was thrown for a name longer than Config::MAX_NAME_LENGTH.';
    try {
154
      \Drupal::config($name)->save();
155 156 157 158 159 160 161 162 163 164 165
      $this->fail($message);
    }
    catch (ConfigNameException $e) {
      $this->pass($message);
    }

    // Verify that disallowed characters in the name cause an exception.
    $characters = $test_characters = array(':', '?', '*', '<', '>', '"', '\'', '/', '\\');
    foreach ($test_characters as $i => $c) {
      try {
        $name = 'namespace.object' . $c;
166
        $config = \Drupal::config($name);
167 168 169 170 171 172 173 174 175 176 177 178 179 180
        $config->save();
      }
      catch (ConfigNameException $e) {
        unset($test_characters[$i]);
      }
    }
    $this->assertTrue(empty($test_characters), format_string('Expected ConfigNameException was thrown for all invalid name characters: @characters', array(
      '@characters' => implode(' ', $characters),
    )));

    // Verify that a valid config object name can be saved.
    $name = 'namespace.object';
    $message = 'ConfigNameException was not thrown for a valid object name.';
    try {
181
      $config = \Drupal::config($name);
182 183 184 185 186 187 188 189
      $config->save();
      $this->pass($message);
    }
    catch (\Exception $e) {
      $this->fail($message);
    }

  }
190

191 192 193 194 195
  /**
   * Tests data type handling.
   */
  public function testDataTypes() {
    \Drupal::moduleHandler()->install(array('config_test'));
196
    $storage = new DatabaseStorage($this->container->get('database'), 'config');
197 198
    $name = 'config_test.types';
    $config = $this->container->get('config.factory')->get($name);
199
    $original_content = file_get_contents(drupal_get_path('module', 'config_test') . "/config/$name.yml");
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    $this->verbose('<pre>' . $original_content . "\n" . var_export($storage->read($name), TRUE));

    // Verify variable data types are intact.
    $data = array(
      'array' => array(),
      'boolean' => TRUE,
      'exp' => 1.2e+34,
      'float' => 3.14159,
      'hex' => 0xC,
      'int' => 99,
      'octal' => 0775,
      'string' => 'string',
      'string_int' => '1',
    );
    $this->assertIdentical($config->get(), $data);

    // Re-set each key using Config::set().
    foreach($data as $key => $value) {
      $config->set($key, $value);
    }
    $config->save();
    $this->assertIdentical($config->get(), $data);
    // Assert the data against the file storage.
    $this->assertIdentical($storage->read($name), $data);
224
    $this->verbose('<pre>' . $name . var_export($storage->read($name), TRUE));
225 226 227 228 229 230

    // Set data using config::setData().
    $config->setData($data)->save();
    $this->assertIdentical($config->get(), $data);
    $this->assertIdentical($storage->read($name), $data);

231 232
    // Test that setting an unsupported type for a config object with a schema
    // fails.
233 234 235 236
    try {
      $config->set('stream', fopen(__FILE__, 'r'))->save();
      $this->fail('No Exception thrown upon saving invalid data type.');
    }
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    catch (UnsupportedDataTypeConfigException $e) {
      $this->pass(String::format('%class thrown upon saving invalid data type.', array(
        '%class' => get_class($e),
      )));
    }

    // Test that setting an unsupported type for a config object with no schema
    // also fails.
    $typed_config_manager = $this->container->get('config.typed');
    $config_name = 'config_test.no_schema';
    $config = $this->container->get('config.factory')->get($config_name);
    $this->assertFalse($typed_config_manager->hasConfigSchema($config_name));

    try {
      $config->set('stream', fopen(__FILE__, 'r'))->save();
      $this->fail('No Exception thrown upon saving invalid data type.');
    }
    catch (UnsupportedDataTypeConfigException $e) {
      $this->pass(String::format('%class thrown upon saving invalid data type.', array(
256 257 258 259 260 261
        '%class' => get_class($e),
      )));
    }
  }

}