diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index 7ad1aee..82b4101 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -86,7 +86,7 @@ class Config extends StorableConfigBase { * @param \Drupal\Core\Language\Language $language * The language object used to override configuration data. */ - public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManager $typed_config, Language $language = NULL) { + public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config, Language $language = NULL) { $this->name = $name; $this->storage = $storage; $this->eventDispatcher = $event_dispatcher; @@ -276,6 +276,9 @@ public function delete() { $this->data = array(); $this->storage->delete($this->name); $this->isNew = TRUE; + $this->settingsOverrides = array(); + $this->languageOverrides = array(); + $this->moduleOverrides = array(); $this->resetOverriddenData(); $this->eventDispatcher->dispatch(ConfigEvents::DELETE, new ConfigCrudEvent($this)); $this->originalData = $this->data; @@ -347,4 +350,4 @@ public function getOriginal($key = '', $apply_overrides = TRUE) { } } } -} +} \ No newline at end of file diff --git a/core/tests/Drupal/Tests/Core/Config/ConfigTest.php b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php new file mode 100644 index 0000000..cf7618f --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php @@ -0,0 +1,337 @@ + 'Config test', + 'description' => 'Tests Config.', + 'group' => 'Configuration' + ); + } + + /** + * Setup. + */ + public function setUp() { + $this->storage = $this->getMock('Drupal\Core\Config\StorageInterface'); + $this->event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->typed_config = $this->getMock('\Drupal\Core\Config\TypedConfigManagerInterface'); + $this->language = $this->getMock('\Drupal\Core\Language\Language'); + $this->config = new Config('config.test', $this->storage, $this->event_dispatcher, $this->typed_config, $this->language); + } + + /** + * Check that the config name is set correctly. + */ + public function testSetName() { + // Valid name with dot. + $testName = 'test.name'; + + // Set the name. + $this->config->setName($testName); + + // Check that the name has been set correctly. + $this->assertEquals($testName, $this->config->getName()); + + // Check that the name validates. + // Should throw \Drupal\Core\Config\ConfigNameException if invalid. + $this->config->validateName($testName); + } + + /** + * Check that isNew is set correctly. + */ + public function testIsNew() { + // Config should be new by default. + $this->assertTrue($this->config->isNew()); + + // Config is no longer new once saved. + $this->config->save(); + $this->assertFalse($this->config->isNew()); + } + + /** + * Check that data is set correctly. + */ + public function testSetData() { + $data = array('a' => 1, 'b' => 2, 'c' => array('d' => 3)); + $this->config->setData($data); + $this->assertEquals($data, $this->config->getRawData()); + $this->assertEquals(1, $this->config->get('a')); + $this->assertEquals(3, $this->config->get('c.d')); + } + + /** + * Check that original data is set when config is saved. + */ + public function testSave() { + // Set initial data. + $this->config->setData(array('a' => 'testVal')); + + // Check that original data has not been set yet. + $this->assertNull($this->config->getOriginal('a', FALSE)); + + // Save so that the original data is set. + $config = $this->config->save(); + + // Check that returned $config is instance of Config. + $this->assertInstanceOf('\Drupal\Core\Config\Config', $config); + + // Check that the original data it saved. + $this->assertEquals($this->config->getOriginal('a', FALSE), 'testVal'); + } + + /** + * Check that overrides are returned by get method and original data is maintained. + */ + public function testOverrideData() { + // Set initial data. + $this->config->setData(array('a' => 1)); + + // Check original data was set correctly. + $this->assertEquals($this->config->get('a'), 1); + + // Save so that the original data is stored. + $this->config->save(); + + // Set language override data and check value before and after save. + $this->config->setLanguageOverride(array('a' => 2)); + $this->assertEquals($this->config->get('a'), 2); + $this->config->save(); + $this->assertEquals($this->config->get('a'), 2); + + // Set module override data and check value before and after save. + $this->config->setModuleOverride(array('a' => 3)); + $this->assertEquals($this->config->get('a'), 3); + $this->config->save(); + $this->assertEquals($this->config->get('a'), 3); + + // Set settings override data and check value before and after save. + $this->config->setSettingsOverride(array('a' => 4)); + $this->assertEquals($this->config->get('a'), 4); + $this->config->save(); + $this->assertEquals($this->config->get('a'), 4); + + // Set module and language again to ensure override order is correct. + $this->config->setLanguageOverride(array('a' => 2)); + $this->config->setModuleOverride(array('a' => 3)); + + // 'a' should still be '4' after setting module and language overrides. + $this->assertEquals($this->config->get('a'), 4); + $this->config->save(); + $this->assertEquals($this->config->get('a'), 4); + + // Check original data has not changed. + $this->assertEquals($this->config->getOriginal('a', FALSE), 1); + + // Check correct override value '4' is returned with $apply_overrides = TRUE. + $this->assertEquals($this->config->getOriginal('a', TRUE), 4); + + // Check $apply_overrides defaults to TRUE. + $this->assertEquals($this->config->getOriginal('a'), 4); + } + + /** + * Check that data is set correctly. + */ + public function testSetValue() { + // Check single value. + $this->config->set('testData', 'testDataValue'); + $this->assertEquals('testDataValue', $this->config->get('testData')); + + // Check nested value. + $this->config->set('nested.testData', 'nestedDataValue'); + $this->assertEquals('nestedDataValue', $this->config->get('nested.testData')); + } + + /** + * Check that single value cannot be overwritten with a nested value. + * + * @expectedException PHPUnit_Framework_Error_Warning + */ + public function testSetIllegalOffsetValue() { + // Set a single value. + $this->config->set('testData', 'testDataValue'); + + // Attempt to treat the single value as a nested item. + $this->config->set('testData.illegalOffset', 'testDataValue'); + } + + /** + * Check that config can be initialized with data. + */ + public function testInitWithData() { + $config = $this->config->initWithData(array('a' => 'b')); + + // Should return the Config object. + $this->assertInstanceOf('\Drupal\Core\Config\Config', $config); + + // Check config is not new. + $this->assertEquals(FALSE, $this->config->isNew()); + + // Check that data value was set correctly. + $this->assertEquals('b', $this->config->get('a')); + + // Check that original data was set. + $this->assertEquals('b', $this->config->getOriginal('a')); + } + + /** + * Check clear. + */ + public function testClear() { + // Check that value is cleared. + $this->config->set('a', 'testVal'); + $this->assertEquals('testVal', $this->config->get('a')); + $this->config->clear('a'); + $this->assertNull($this->config->get('a')); + + // Check that nested value is cleared. + $this->config->set('a', array('b' => 'testVal')); + $this->assertEquals('testVal', $this->config->get('a.b')); + $this->config->clear('a.b'); + $this->assertNull($this->config->get('a.b')); + } + + /** + * Check that config delete is working correctly. + */ + public function testDelete() { + // Set initial data. + $this->config->set('a', 'testVal'); + $this->config->setModuleOverride(array('a' => 'overrideVal')); + + // Save. + $this->config->save(); + + // Check that values have been set correctly. + $this->assertEquals('overrideVal', $this->config->get('a')); + $this->assertEquals('overrideVal', $this->config->getOriginal('a', TRUE)); + $this->assertEquals('testVal', $this->config->getOriginal('a', FALSE)); + $this->assertFalse($this->config->isNew()); + + // Delete. + $this->config->delete(); + + // Check object properties have been reset. + $this->assertTrue($this->config->isNew()); + $this->assertEmpty($this->config->get('a')); + $this->assertEmpty($this->config->getOriginal('a', TRUE)); + $this->assertEmpty($this->config->getOriginal('a', FALSE)); + } + + /** + * Check that data merges correctly. + */ + public function testMerge() { + // Set initial data. + $data = array('a' => 1, 'b' => 2, 'c' => array('d' => 3)); + $this->config->setData($data); + + // Data to merge. + $dataToMerge = array('a' => 2, 'e' => 4, 'c' => array('f' => 5)); + $this->config->merge($dataToMerge); + + // Check that data has merged correctly. + $mergedData = array('a' => 2, 'b' => 2, 'c' => array('d' => 3, 'f' => 5), 'e' => 4); + $this->assertEquals($mergedData, $this->config->getRawData()); + } + + /** + * Check language is set. + */ + public function testGetLanguage() { + $this->assertInstanceOf('\Drupal\Core\Language\Language', $this->config->getLanguage()); + } + + /** + * Checks that name validation exception are thrown. + * + * @expectedException \Drupal\Core\Config\ConfigNameException + * @dataProvider validateNameProvider + */ + public function testValidateNameException($name, $exception_message) { + $this->setExpectedException('\Drupal\Core\Config\ConfigNameException', $exception_message); + $this->config->validateName($name); + } + + /** + * Provides data to test name validation. + */ + public function validateNameProvider() { + $return = array( + // Name missing namespace (dot). + array( + 'MissingNamespace', + String::format('Missing namespace in Config object name MissingNamespace.', array( + '@name' => 'MissingNamespace', + )), + ), + // Exceeds length (max length plus an extra dot). + array( + str_repeat('a', Config::MAX_NAME_LENGTH) . ".", + String::format('Config object name @name exceeds maximum allowed length of @length characters.', array( + '@name' => str_repeat('a', Config::MAX_NAME_LENGTH) . ".", + '@length' => Config::MAX_NAME_LENGTH, + )), + ), + ); + // Name must not contain : ? * < > " ' / \ + foreach (array(':', '?', '*', '<', '>', '"',"'",'/','\\') as $char) { + $name = 'name.' . $char; + $return[] = array( + $name, + String::format('Invalid character in Config object name @name.', array( + '@name' => $name, + )) + ); + } + return $return; + } +}