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 1eec831..2fa841c 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php @@ -21,6 +21,13 @@ */ protected $storageClass; + /** + * An array of random stdClass objects. + * + * @var array + */ + protected $objects = array(); + protected function setUp() { parent::setUp(); @@ -29,6 +36,11 @@ protected function setUp() { $this->store1 = new $this->storageClass($this->collection1); $this->store2 = new $this->storageClass($this->collection2); + + // Create several objects for testing. + for ($i = 0; $i <= 5; $i++) { + $this->objects[$i] = $this->randomObject(); + } } /** @@ -40,49 +52,51 @@ public function testCRUD() { $this->assertIdentical($this->store2->getCollectionName(), $this->collection2); // Verify that an item can be stored. - $this->store1->set('foo', 'bar'); - $this->assertIdentical('bar', $this->store1->get('foo')); + $this->store1->set('foo', $this->objects[0]); + $this->assertIdenticalObject($this->objects[0], $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->assertIdentical('baz', $this->store1->get('foo')); + $this->store1->set('foo', $this->objects[1]); + $this->assertIdenticalObject($this->objects[1], $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->assertIdentical('baz', $this->store1->get('foo')); - $this->assertIdentical('other', $this->store2->get('foo')); + $this->store2->set('foo', $this->objects[2]); + $this->assertIdenticalObject($this->objects[1], $this->store1->get('foo')); + $this->assertIdenticalObject($this->objects[2], $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->assertIdentical('other', $this->store2->get('foo')); + $this->assertIdenticalObject($this->objects[2], $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', + 'foo' => $this->objects[3], + 'bar' => $this->objects[4], ); $this->store1->setMultiple($values); // Verify that multiple items can be retrieved. - $result = $this->store1->getMultiple(array('foo', 'baz')); - $this->assertIdentical($values, $result); + $result = $this->store1->getMultiple(array('foo', 'bar')); + foreach ($values as $j => $value) { + $this->assertIdenticalObject($value, $result[$j]); + } // Verify that the other collection was not affected. $this->assertFalse($this->store2->get('foo')); - $this->assertFalse($this->store2->get('baz')); + $this->assertFalse($this->store2->get('bar')); // 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'); + $this->store2->set('foo', $this->objects[5]); $result = $this->store1->getAll(); // Not using assertIdentical(), since the order is not defined for getAll(). $this->assertEqual(count($result), count($values)); @@ -91,15 +105,15 @@ public function testCRUD() { } // Verify that all items in the other collection are different. $result = $this->store2->getAll(); - $this->assertEqual($result, array('foo' => 'other')); + $this->assertEqual($result, array('foo' => $this->objects[5])); // 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'))); + $this->assertFalse($this->store1->getMultiple(array('foo', 'bar'))); // Verify that the item in the other collection still exists. - $this->assertIdentical('other', $this->store2->get('foo')); + $this->assertIdenticalObject($this->objects[5], $this->store2->get('foo')); } /** @@ -123,4 +137,30 @@ public function testNonExistingKeys() { $this->assertFalse(isset($values['foo']), "Key 'foo' not found."); $this->assertIdentical($values['bar'], 'baz'); } + + /** + * Tests the setIfNotExists() method. + */ + public function testSetIfNotExists() { + + $key = $this->randomName(); + // Test that setIfNotExists() succeeds only the first time. + for ($i = 0; $i <= 1; $i++) { + // setIfNotExists() should fail the second time ($i = 1). + $this->assertEqual(!$i, $this->store1->setIfNotExists($key, $this->objects[$i])); + $this->assertIdenticalObject($this->objects[0], $this->store1->get($key)); + // Verify that the other collection is not affected. + $this->assertFalse($this->store2->get($key)); + } + + // Remove the item and try to set it again. + $this->store1->delete($key); + $this->store1->setIfNotExists($key, $this->objects[1]); + // This time it should succeed. + $this->assertIdenticalObject($this->objects[1], $this->store1->get($key)); + // Verify that the other collection is still not affected. + $this->assertFalse($this->store2->get($key)); + + } + }