diff --git a/core/lib/Drupal/Core/KeyValueStore/AbstractStorage.php b/core/lib/Drupal/Core/KeyValueStore/AbstractStorage.php index 408b9a8..a1ba2e4 100644 --- a/core/lib/Drupal/Core/KeyValueStore/AbstractStorage.php +++ b/core/lib/Drupal/Core/KeyValueStore/AbstractStorage.php @@ -1,7 +1,8 @@ collection = $collection; } /** - * Implements KeyValueStore\Storage\StorageInterface::getCollectionName(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getCollectionName(). */ public function getCollectionName() { return $this->collection; } /** - * Implements KeyValueStore\Storage\StorageInterface::get(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get(). */ public function get($key) { $values = $this->getMultiple(array($key)); @@ -36,7 +37,7 @@ public function get($key) { } /** - * Implements KeyValueStore\Storage\StorageInterface::setMultiple(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::setMultiple(). */ public function setMultiple(array $data) { foreach ($data as $key => $value) { @@ -45,7 +46,7 @@ public function setMultiple(array $data) { } /** - * Implements KeyValueStore\Storage\StorageInterface::delete(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::delete(). */ public function delete($key) { $this->deleteMultiple(array($key)); diff --git a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php index e2d1fd7..4ff63e8 100644 --- a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php +++ b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php @@ -2,7 +2,7 @@ /** * @file - * Contains KeyValueStore\Storage\DatabaseBackend. + * Contains Drupal\Core\KeyValueStore\DatabaseStorage. */ namespace Drupal\Core\KeyValueStore; @@ -10,13 +10,13 @@ /** * Defines a default key/value store implementation. * - * This is Drupal's default key/value store implementation. It uses the database to store - * key/value data. + * This is Drupal's default key/value store implementation. It uses the database + * to store key/value data. */ class DatabaseStorage extends AbstractStorage { /** - * Overrides KeyValueStore\Storage\AbstractStorage::__construct(). + * Overrides Drupal\Core\KeyValueStore\AbstractStorage::__construct(). */ public function __construct($collection, array $options = array()) { parent::__construct($collection, $options); @@ -24,7 +24,7 @@ public function __construct($collection, array $options = array()) { } /** - * Implements KeyValueStore\Storage\StorageInterface::getMultiple(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getMultiple(). */ public function getMultiple(array $keys) { try { @@ -45,7 +45,7 @@ public function getMultiple(array $keys) { } /** - * Implements KeyValueStore\Storage\StorageInterface::getAll(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getAll(). */ public function getAll() { $result = db_query('SELECT name, value FROM {' . db_escape_table($this->table) . '} WHERE collection = :collection', array(':collection' => $this->collection)); @@ -60,7 +60,7 @@ public function getAll() { } /** - * Implements KeyValueStore\Storage\StorageInterface::set(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::set(). */ public function set($key, $value) { db_merge($this->table) @@ -73,7 +73,7 @@ public function set($key, $value) { } /** - * Implements KeyValueStore\Storage\StorageInterface::deleteMultiple(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::deleteMultiple(). */ public function deleteMultiple(array $keys) { // Delete in chunks when a large array is passed. diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php index d4d7b6c..769888b 100644 --- a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php @@ -2,16 +2,16 @@ /** * @file - * Contains KeyValueStore\Storage\StorageInterface. + * Contains Drupal\Core\KeyValueStore\KeyValueStoreInterface. */ namespace Drupal\Core\KeyValueStore; - /** * Defines the interface for key/value store implementations. */ interface KeyValueStoreInterface { + /** * Constructs a new key/value collection. * @@ -33,7 +33,7 @@ public function getCollectionName(); /** * Returns the stored value for a given key. * - * @param $key + * @param string $key * The key of the data to retrieve. * * @return mixed @@ -49,7 +49,8 @@ public function get($key); * * @return array * An associative array of items successfully returned, indexed by key. - * @todo What's returned for non-existing keys? + * + * @todo What's returned for non-existing keys? */ public function getMultiple(array $keys); diff --git a/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php b/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php index b53722b..2e7d9c5 100644 --- a/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php +++ b/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php @@ -2,7 +2,7 @@ /** * @file - * Contains KeyValueStore\Storage\MemoryStorage. + * Contains Drupal\Core\KeyValueStore\MemoryStorage. */ namespace Drupal\Core\KeyValueStore; @@ -10,8 +10,7 @@ /** * Defines a default key/value store implementation. * - * For speed reasons, this class does not use the AbstractStorage generic - * implementation. + * For performance reasons, this implementation is not based on AbstractStorage. */ class MemoryStorage implements KeyValueStoreInterface { @@ -21,28 +20,28 @@ class MemoryStorage implements KeyValueStoreInterface { protected $data = array(); /** - * Implements KeyValueStore\Storage\StorageInterface::__construct(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::__construct(). */ public function __construct($collection, array $options = array()) { $this->collection = $collection; } /** - * Implements KeyValueStore\Storage\StorageInterface::getCollectionName(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getCollectionName(). */ public function getCollectionName() { return $this->collection; } /** - * Implements KeyValueStore\Storage\StorageInterface::get(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get(). */ public function get($key) { return isset($this->data[$key]) ? $this->data[$key] : FALSE; } /** - * Implements KeyValueStore\Storage\StorageInterface::getMultiple(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getMultiple(). */ public function getMultiple(array $keys) { $results = array(); @@ -58,35 +57,35 @@ public function getMultiple(array $keys) { } /** - * Implements KeyValueStore\Storage\StorageInterface::getAll(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::getAll(). */ public function getAll() { return $this->data; } /** - * Implements KeyValueStore\Storage\StorageInterface::set(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::set(). */ public function set($key, $value) { $this->data[$key] = $value; } /** - * Implements KeyValueStore\Storage\StorageInterface::setMultiple(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::setMultiple(). */ public function setMultiple(array $data) { $this->data = $data + $this->data; } /** - * Implements KeyValueStore\Storage\StorageInterface::delete(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::delete(). */ public function delete($key) { unset($this->data[$key]); } /** - * Implements KeyValueStore\Storage\StorageInterface::deleteMultiple(). + * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::deleteMultiple(). */ public function deleteMultiple(array $keys) { foreach ($keys as $key) { diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php index c81f8e9..2994251 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php @@ -1,14 +1,24 @@ 'Database Storage', - 'description' => 'Test the key-value database storage.', - 'group' => 'Key-value store' + 'name' => 'Database storage', + 'description' => 'Tests the key-value database storage.', + 'group' => 'Key-value store', ); } @@ -23,4 +33,5 @@ protected function tearDown() { db_drop_table('keyvalue'); parent::tearDown(); } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php index d3db3bc..f593fd8 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php @@ -1,14 +1,24 @@ 'Memory Storage', - 'description' => 'Test the key-value memory storage.', - 'group' => 'Key-value store' + 'name' => 'Memory storage', + 'description' => 'Tests the key-value memory storage.', + 'group' => 'Key-value store', ); } diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php index 8fb38a0..3e321f3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php @@ -1,41 +1,104 @@ collection = end($class); - $storageClass = '\\Drupal\\Core\\KeyValueStore\\' . substr($this->collection, 0, -4); - $this->kv = new $storageClass($this->collection); + + $this->collection1 = 'first'; + $this->collection2 = 'second'; + + $this->store1 = new $this->storageClass($this->collection1); + $this->store2 = new $this->storageClass($this->collection2); } - public function testSetGet() { - $this->assertEqual($this->kv->getCollectionName(), $this->collection); - // get-set test. - $this->kv->set('foo', 'bar'); - $this->assertEqual('bar', $this->kv->get('foo')); - $this->kv->delete('foo'); - $this->assertFalse($this->kv->get('foo')); - - // multiple test. - $values = array('foo' => 'bar', 'baz' => 'qux'); - $this->kv->setMultiple($values); - $result = $this->kv->getMultiple(array('foo', 'baz')); + /** + * Tests CRUD operations. + */ + public function testCRUD() { + // Verify that each store returns its own collection name. + $this->assertEqual($this->store1->getCollectionName(), $this->collection1); + $this->assertEqual($this->store2->getCollectionName(), $this->collection2); + + // Verify that an item can be stored. + $this->store1->set('foo', 'bar'); + $this->assertEqual('bar', $this->store1->get('foo')); + // Verify that the other collection is not affected. + $this->assertFalse($this->store2->get('foo')); + + // Verify that an item can be updated. + $this->store1->set('foo', 'baz'); + $this->assertEqual('baz', $this->store1->get('foo')); + // Verify that the other collection is still not affected. + $this->assertFalse($this->store2->get('foo')); + + // Verify that a collection/name pair is unique. + $this->store2->set('foo', 'other'); + $this->assertEqual('baz', $this->store1->get('foo')); + $this->assertEqual('other', $this->store2->get('foo')); + + // Verify that an item can be deleted. + $this->store1->delete('foo'); + $this->assertFalse($this->store1->get('foo')); + + // Verify that the other collection is not affected. + $this->assertEqual('other', $this->store2->get('foo')); + $this->store2->delete('foo'); + $this->assertFalse($this->store2->get('foo')); + + // Verify that multiple items can be stored. + $values = array( + 'foo' => 'bar', + 'baz' => 'qux', + ); + $this->store1->setMultiple($values); + + // Verify that multiple items can be retrieved. + $result = $this->store1->getMultiple(array('foo', 'baz')); $this->assertEqual($values, $result); - $result = $this->kv->getAll(); - // $result might not equal to $values, the order is not defined for - // getAll(). + + // Verify that the other collection was not affected. + $this->assertFalse($this->store2->get('foo')); + $this->assertFalse($this->store2->get('baz')); + + // Verify that all items in a collection can be retrieved. + // Ensure that an item with the same name exists in the other collection. + $this->store2->set('foo', 'other'); + $result = $this->store1->getAll(); + // Not using assertIdentical(), since the order is not defined for getAll(). $this->assertEqual(count($result), count($values)); foreach ($result as $key => $value) { $this->assertEqual($values[$key], $value); } - $this->kv->deleteMultiple(array_keys($values)); - $this->assertFalse($this->kv->get('foo')); - $this->assertFalse($this->kv->get('bar')); - $this->assertFalse($this->kv->getMultiple(array('foo', 'baz'))); + // Verify that all items in the other collection are different. + $result = $this->store2->getAll(); + $this->assertEqual($result, array('foo' => 'other')); + + // Verify that multiple items can be deleted. + $this->store1->deleteMultiple(array_keys($values)); + $this->assertFalse($this->store1->get('foo')); + $this->assertFalse($this->store1->get('bar')); + $this->assertFalse($this->store1->getMultiple(array('foo', 'baz'))); + // Verify that the item in the other collection still exists. + $this->assertEqual('other', $this->store2->get('foo')); } } diff --git a/core/modules/system/system.install b/core/modules/system/system.install index dd1503c..085773b 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -549,33 +549,6 @@ function system_schema() { ), 'primary key' => array('name'), ); - $schema['keyvalue'] = array( - 'description' => 'Generic key-value storage table.', - 'fields' => array( - 'name' => array( - 'description' => 'The key.', - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '', - ), - 'collection' => array( - 'description' => 'The collection of the variable.', - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '', - ), - 'value' => array( - 'description' => 'The value.', - 'type' => 'blob', - 'not null' => TRUE, - 'size' => 'big', - 'translatable' => TRUE, - ), - ), - 'primary key' => array('collection', 'name'), - ); $schema['actions'] = array( 'description' => 'Stores action information.', @@ -893,6 +866,34 @@ function system_schema() { ), ); + $schema['keyvalue'] = array( + 'description' => 'Generic key-value storage table.', + 'fields' => array( + 'name' => array( + 'description' => 'The key.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'collection' => array( + 'description' => 'The collection of the variable.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'value' => array( + 'description' => 'The value.', + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + 'translatable' => TRUE, + ), + ), + 'primary key' => array('collection', 'name'), + ); + $schema['menu_router'] = array( 'description' => 'Maps paths to various callbacks (access, page and title)', 'fields' => array(