Commit 2c0893e8 authored by catch's avatar catch

Issue #1739900 by alexpott: Added a rename operation to config storage controllers.

parent 3bf6761f
......@@ -317,6 +317,19 @@ public function save() {
return $this;
}
/*
* Renames the configuration object.
*
* @param $new_name
* The new name of the configuration object being constructed.
*/
public function rename($new_name) {
if ($this->storage->rename($this->name, $new_name)) {
$this->name = $new_name;
}
return $this;
}
/**
* Sorts all keys in configuration data.
*
......
......@@ -99,6 +99,20 @@ public function delete($name) {
->execute();
}
/**
* Implements Drupal\Core\Config\StorageInterface::rename().
*
* @throws PDOException
*/
public function rename($name, $new_name) {
$options = array('return' => Database::RETURN_AFFECTED) + $this->options;
return (bool) $this->getConnection()->update('config', $options)
->fields(array('name' => $new_name))
->condition('name', $name)
->execute();
}
/**
* Implements Drupal\Core\Config\StorageInterface::encode().
*/
......
......@@ -107,6 +107,17 @@ public function delete($name) {
return drupal_unlink($this->getFilePath($name));
}
/**
* Implements Drupal\Core\Config\StorageInterface::delete().
*/
public function rename($name, $new_name) {
$status = @rename($this->getFilePath($name), $this->getFilePath($new_name));
if ($status === FALSE) {
throw new StorageException('Failed to rename configuration file from: ' . $this->getFilePath($name) . ' to: ' . $this->getFilePath($new_name));
}
return TRUE;
}
/**
* Implements Drupal\Core\Config\StorageInterface::encode().
*
......
......@@ -49,6 +49,13 @@ public function delete($name) {
return FALSE;
}
/**
* Implements Drupal\Core\Config\StorageInterface::rename().
*/
public function rename($name, $new_name) {
return FALSE;
}
/**
* Implements Drupal\Core\Config\StorageInterface::encode().
*/
......
......@@ -60,6 +60,19 @@ public function write($name, array $data);
*/
public function delete($name);
/**
* Renames a configuration object in the storage.
*
* @param string $name
* The name of a configuration object to rename.
* @param string $new_name
* The new name of a configuration object.
*
* @return bool
* TRUE on success, FALSE otherwise.
*/
public function rename($name, $new_name);
/**
* Encodes configuration data into the storage-specific format.
*
......
......@@ -84,6 +84,13 @@ function testCRUD() {
$new_config = config($name);
$this->assertIdentical($new_config->get(), $config->get());
$this->assertIdentical($config->isNew(), FALSE);
// Rename the configuration object.
$new_name = 'config_test.crud_rename';
$config->rename($new_name);
$renamed_config = config($new_name);
$this->assertIdentical($renamed_config->get(), $config->get());
$this->assertIdentical($renamed_config->isNew(), FALSE);
}
/**
......
......@@ -68,6 +68,14 @@ function testCRUD() {
$this->assertFalse(in_array('system.performance', $names));
$this->assertTrue(in_array($name, $names));
// Rename the configuration storage object.
$new_name = 'config_test.storage_rename';
$this->storage->rename($name, $new_name);
$raw_data = $this->read($new_name);
$this->assertIdentical($raw_data, $data);
// Rename it back so further tests work.
$this->storage->rename($new_name, $name);
// Deleting an existing name returns TRUE.
$result = $this->storage->delete($name);
$this->assertIdentical($result, TRUE);
......@@ -109,6 +117,25 @@ function testCRUD() {
$class = get_class($e);
$this->pass($class . ' thrown upon listing from a non-existing storage bin.');
}
// Test renaming an object that does not exist throws an exception.
try {
$this->storage->rename('config_test.storage_does_not_exist', 'config_test.storage_does_not_exist_rename');
}
catch (\Exception $e) {
$class = get_class($e);
$this->pass($class . ' thrown upon renaming a nonexistent storage bin.');
}
// Test renaming to an object that already exists throws an exception.
try {
$this->storage->rename('system.cron', 'system.performance');
}
catch (\Exception $e) {
$class = get_class($e);
$this->pass($class . ' thrown upon renaming a nonexistent storage bin.');
}
}
abstract protected function read($name);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment