Chris@0: testBin)) { Chris@0: $this->testBin = 'page'; Chris@0: } Chris@0: return $this->testBin; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a cache backend to test. Chris@0: * Chris@0: * Override this method to test a CacheBackend. Chris@0: * Chris@0: * @param string $bin Chris@0: * Bin name to use for this backend instance. Chris@0: * Chris@0: * @return \Drupal\Core\Cache\CacheBackendInterface Chris@0: * Cache backend to test. Chris@0: */ Chris@0: abstract protected function createCacheBackend($bin); Chris@0: Chris@0: /** Chris@0: * Allows specific implementation to change the environment before a test run. Chris@0: */ Chris@0: public function setUpCacheBackend() { Chris@0: } Chris@0: Chris@0: /** Chris@0: * Allows alteration of environment after a test run but before tear down. Chris@0: * Chris@0: * Used before the real tear down because the tear down will change things Chris@0: * such as the database prefix. Chris@0: */ Chris@0: public function tearDownCacheBackend() { Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a backend to test; this will get a shared instance set in the object. Chris@0: * Chris@0: * @return \Drupal\Core\Cache\CacheBackendInterface Chris@0: * Cache backend to test. Chris@0: */ Chris@0: protected function getCacheBackend($bin = NULL) { Chris@0: if (!isset($bin)) { Chris@0: $bin = $this->getTestBin(); Chris@0: } Chris@0: if (!isset($this->cachebackends[$bin])) { Chris@0: $this->cachebackends[$bin] = $this->createCacheBackend($bin); Chris@0: // Ensure the backend is empty. Chris@0: $this->cachebackends[$bin]->deleteAll(); Chris@0: } Chris@0: return $this->cachebackends[$bin]; Chris@0: } Chris@0: Chris@0: protected function setUp() { Chris@0: $this->cachebackends = []; Chris@0: $this->defaultValue = $this->randomMachineName(10); Chris@0: Chris@0: parent::setUp(); Chris@0: Chris@0: $this->setUpCacheBackend(); Chris@0: } Chris@0: Chris@0: protected function tearDown() { Chris@0: // Destruct the registered backend, each test will get a fresh instance, Chris@0: // properly emptying it here ensure that on persistent data backends they Chris@0: // will come up empty the next test. Chris@0: foreach ($this->cachebackends as $bin => $cachebackend) { Chris@0: $this->cachebackends[$bin]->deleteAll(); Chris@0: } Chris@0: unset($this->cachebackends); Chris@0: Chris@0: $this->tearDownCacheBackend(); Chris@0: Chris@0: parent::tearDown(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface. Chris@0: */ Chris@0: public function testSetGet() { Chris@0: $backend = $this->getCacheBackend(); Chris@0: Chris@0: $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1."); Chris@0: $with_backslash = ['foo' => '\Drupal\foo\Bar']; Chris@0: $backend->set('test1', $with_backslash); Chris@0: $cached = $backend->get('test1'); Chris@0: $this->assert(is_object($cached), "Backend returned an object for cache id test1."); Chris@0: $this->assertIdentical($with_backslash, $cached->data); Chris@0: $this->assertTrue($cached->valid, 'Item is marked as valid.'); Chris@0: // We need to round because microtime may be rounded up in the backend. Chris@0: $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.'); Chris@0: $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.'); Chris@0: Chris@0: $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2."); Chris@0: $backend->set('test2', ['value' => 3], REQUEST_TIME + 3); Chris@0: $cached = $backend->get('test2'); Chris@0: $this->assert(is_object($cached), "Backend returned an object for cache id test2."); Chris@0: $this->assertIdentical(['value' => 3], $cached->data); Chris@0: $this->assertTrue($cached->valid, 'Item is marked as valid.'); Chris@0: $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.'); Chris@0: $this->assertEqual($cached->expire, REQUEST_TIME + 3, 'Expire time is correct.'); Chris@0: Chris@0: $backend->set('test3', 'foobar', REQUEST_TIME - 3); Chris@0: $this->assertFalse($backend->get('test3'), 'Invalid item not returned.'); Chris@0: $cached = $backend->get('test3', TRUE); Chris@0: $this->assert(is_object($cached), 'Backend returned an object for cache id test3.'); Chris@0: $this->assertFalse($cached->valid, 'Item is marked as valid.'); Chris@0: $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.'); Chris@0: $this->assertEqual($cached->expire, REQUEST_TIME - 3, 'Expire time is correct.'); Chris@0: Chris@0: $this->assertIdentical(FALSE, $backend->get('test4'), "Backend does not contain data for cache id test4."); Chris@0: $with_eof = ['foo' => "\nEOF\ndata"]; Chris@0: $backend->set('test4', $with_eof); Chris@0: $cached = $backend->get('test4'); Chris@0: $this->assert(is_object($cached), "Backend returned an object for cache id test4."); Chris@0: $this->assertIdentical($with_eof, $cached->data); Chris@0: $this->assertTrue($cached->valid, 'Item is marked as valid.'); Chris@0: $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.'); Chris@0: $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.'); Chris@0: Chris@0: $this->assertIdentical(FALSE, $backend->get('test5'), "Backend does not contain data for cache id test5."); Chris@0: $with_eof_and_semicolon = ['foo' => "\nEOF;\ndata"]; Chris@0: $backend->set('test5', $with_eof_and_semicolon); Chris@0: $cached = $backend->get('test5'); Chris@0: $this->assert(is_object($cached), "Backend returned an object for cache id test5."); Chris@0: $this->assertIdentical($with_eof_and_semicolon, $cached->data); Chris@0: $this->assertTrue($cached->valid, 'Item is marked as valid.'); Chris@0: $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.'); Chris@0: $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.'); Chris@0: Chris@0: $with_variable = ['foo' => '$bar']; Chris@0: $backend->set('test6', $with_variable); Chris@0: $cached = $backend->get('test6'); Chris@0: $this->assert(is_object($cached), "Backend returned an object for cache id test6."); Chris@0: $this->assertIdentical($with_variable, $cached->data); Chris@0: Chris@0: // Make sure that a cached object is not affected by changing the original. Chris@0: $data = new \stdClass(); Chris@0: $data->value = 1; Chris@0: $data->obj = new \stdClass(); Chris@0: $data->obj->value = 2; Chris@0: $backend->set('test7', $data); Chris@0: $expected_data = clone $data; Chris@0: // Add a property to the original. It should not appear in the cached data. Chris@0: $data->this_should_not_be_in_the_cache = TRUE; Chris@0: $cached = $backend->get('test7'); Chris@0: $this->assert(is_object($cached), "Backend returned an object for cache id test7."); Chris@0: $this->assertEqual($expected_data, $cached->data); Chris@0: $this->assertFalse(isset($cached->data->this_should_not_be_in_the_cache)); Chris@0: // Add a property to the cache data. It should not appear when we fetch Chris@0: // the data from cache again. Chris@0: $cached->data->this_should_not_be_in_the_cache = TRUE; Chris@0: $fresh_cached = $backend->get('test7'); Chris@0: $this->assertFalse(isset($fresh_cached->data->this_should_not_be_in_the_cache)); Chris@0: Chris@0: // Check with a long key. Chris@0: $cid = str_repeat('a', 300); Chris@0: $backend->set($cid, 'test'); Chris@0: $this->assertEqual('test', $backend->get($cid)->data); Chris@0: Chris@0: // Check that the cache key is case sensitive. Chris@0: $backend->set('TEST8', 'value'); Chris@0: $this->assertEqual('value', $backend->get('TEST8')->data); Chris@0: $this->assertFalse($backend->get('test8')); Chris@0: Chris@0: // Calling ::set() with invalid cache tags. This should fail an assertion. Chris@0: try { Chris@0: $backend->set('assertion_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]); Chris@0: $this->fail('::set() was called with invalid cache tags, runtime assertion did not fail.'); Chris@0: } Chris@0: catch (\AssertionError $e) { Chris@0: $this->pass('::set() was called with invalid cache tags, runtime assertion failed.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests Drupal\Core\Cache\CacheBackendInterface::delete(). Chris@0: */ Chris@0: public function testDelete() { Chris@0: $backend = $this->getCacheBackend(); Chris@0: Chris@0: $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1."); Chris@0: $backend->set('test1', 7); Chris@0: $this->assert(is_object($backend->get('test1')), "Backend returned an object for cache id test1."); Chris@0: Chris@0: $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2."); Chris@0: $backend->set('test2', 3); Chris@0: $this->assert(is_object($backend->get('test2')), "Backend returned an object for cache id %cid."); Chris@0: Chris@0: $backend->delete('test1'); Chris@0: $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1 after deletion."); Chris@0: Chris@0: $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2."); Chris@0: Chris@0: $backend->delete('test2'); Chris@0: $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2 after deletion."); Chris@0: Chris@0: $long_cid = str_repeat('a', 300); Chris@0: $backend->set($long_cid, 'test'); Chris@0: $backend->delete($long_cid); Chris@0: $this->assertIdentical(FALSE, $backend->get($long_cid), "Backend does not contain data for long cache id after deletion."); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests data type preservation. Chris@0: */ Chris@0: public function testValueTypeIsKept() { Chris@0: $backend = $this->getCacheBackend(); Chris@0: Chris@0: $variables = [ Chris@0: 'test1' => 1, Chris@0: 'test2' => '0', Chris@0: 'test3' => '', Chris@0: 'test4' => 12.64, Chris@0: 'test5' => FALSE, Chris@0: 'test6' => [1, 2, 3], Chris@0: ]; Chris@0: Chris@0: // Create cache entries. Chris@0: foreach ($variables as $cid => $data) { Chris@0: $backend->set($cid, $data); Chris@0: } Chris@0: Chris@0: // Retrieve and test cache objects. Chris@0: foreach ($variables as $cid => $value) { Chris@0: $object = $backend->get($cid); Chris@0: $this->assert(is_object($object), sprintf("Backend returned an object for cache id %s.", $cid)); Chris@0: $this->assertIdentical($value, $object->data, sprintf("Data of cached id %s kept is identical in type and value", $cid)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple(). Chris@0: */ Chris@0: public function testGetMultiple() { Chris@0: $backend = $this->getCacheBackend(); Chris@0: Chris@0: // Set numerous testing keys. Chris@0: $long_cid = str_repeat('a', 300); Chris@0: $backend->set('test1', 1); Chris@0: $backend->set('test2', 3); Chris@0: $backend->set('test3', 5); Chris@0: $backend->set('test4', 7); Chris@0: $backend->set('test5', 11); Chris@0: $backend->set('test6', 13); Chris@0: $backend->set('test7', 17); Chris@0: $backend->set($long_cid, 300); Chris@0: Chris@0: // Mismatch order for harder testing. Chris@0: $reference = [ Chris@0: 'test3', Chris@0: 'test7', Chris@0: // Cid does not exist. Chris@0: 'test21', Chris@0: 'test6', Chris@0: // Cid does not exist until added before second getMultiple(). Chris@0: 'test19', Chris@0: 'test2', Chris@0: ]; Chris@0: Chris@0: $cids = $reference; Chris@0: $ret = $backend->getMultiple($cids); Chris@0: // Test return - ensure it contains existing cache ids. Chris@0: $this->assert(isset($ret['test2']), "Existing cache id test2 is set."); Chris@0: $this->assert(isset($ret['test3']), "Existing cache id test3 is set."); Chris@0: $this->assert(isset($ret['test6']), "Existing cache id test6 is set."); Chris@0: $this->assert(isset($ret['test7']), "Existing cache id test7 is set."); Chris@0: // Test return - ensure that objects has expected properties. Chris@0: $this->assertTrue($ret['test2']->valid, 'Item is marked as valid.'); Chris@0: $this->assertTrue($ret['test2']->created >= REQUEST_TIME && $ret['test2']->created <= round(microtime(TRUE), 3), 'Created time is correct.'); Chris@0: $this->assertEqual($ret['test2']->expire, Cache::PERMANENT, 'Expire time is correct.'); Chris@0: // Test return - ensure it does not contain nonexistent cache ids. Chris@0: $this->assertFalse(isset($ret['test19']), "Nonexistent cache id test19 is not set."); Chris@0: $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set."); Chris@0: // Test values. Chris@0: $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value."); Chris@0: $this->assertIdentical($ret['test3']->data, 5, "Existing cache id test3 has the correct value."); Chris@0: $this->assertIdentical($ret['test6']->data, 13, "Existing cache id test6 has the correct value."); Chris@0: $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value."); Chris@0: // Test $cids array - ensure it contains cache id's that do not exist. Chris@0: $this->assert(in_array('test19', $cids), "Nonexistent cache id test19 is in cids array."); Chris@0: $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array."); Chris@0: // Test $cids array - ensure it does not contain cache id's that exist. Chris@0: $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array."); Chris@0: $this->assertFalse(in_array('test3', $cids), "Existing cache id test3 is not in cids array."); Chris@0: $this->assertFalse(in_array('test6', $cids), "Existing cache id test6 is not in cids array."); Chris@0: $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array."); Chris@0: Chris@0: // Test a second time after deleting and setting new keys which ensures that Chris@0: // if the backend uses statics it does not cause unexpected results. Chris@0: $backend->delete('test3'); Chris@0: $backend->delete('test6'); Chris@0: $backend->set('test19', 57); Chris@0: Chris@0: $cids = $reference; Chris@0: $ret = $backend->getMultiple($cids); Chris@0: // Test return - ensure it contains existing cache ids. Chris@0: $this->assert(isset($ret['test2']), "Existing cache id test2 is set"); Chris@0: $this->assert(isset($ret['test7']), "Existing cache id test7 is set"); Chris@0: $this->assert(isset($ret['test19']), "Added cache id test19 is set"); Chris@0: // Test return - ensure it does not contain nonexistent cache ids. Chris@0: $this->assertFalse(isset($ret['test3']), "Deleted cache id test3 is not set"); Chris@0: $this->assertFalse(isset($ret['test6']), "Deleted cache id test6 is not set"); Chris@0: $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set"); Chris@0: // Test values. Chris@0: $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value."); Chris@0: $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value."); Chris@0: $this->assertIdentical($ret['test19']->data, 57, "Added cache id test19 has the correct value."); Chris@0: // Test $cids array - ensure it contains cache id's that do not exist. Chris@0: $this->assert(in_array('test3', $cids), "Deleted cache id test3 is in cids array."); Chris@0: $this->assert(in_array('test6', $cids), "Deleted cache id test6 is in cids array."); Chris@0: $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array."); Chris@0: // Test $cids array - ensure it does not contain cache id's that exist. Chris@0: $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array."); Chris@0: $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array."); Chris@0: $this->assertFalse(in_array('test19', $cids), "Added cache id test19 is not in cids array."); Chris@0: Chris@0: // Test with a long $cid and non-numeric array key. Chris@0: $cids = ['key:key' => $long_cid]; Chris@0: $return = $backend->getMultiple($cids); Chris@0: $this->assertEqual(300, $return[$long_cid]->data); Chris@0: $this->assertTrue(empty($cids)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple(). Chris@0: */ Chris@0: public function testSetMultiple() { Chris@0: $backend = $this->getCacheBackend(); Chris@0: Chris@0: $future_expiration = REQUEST_TIME + 100; Chris@0: Chris@0: // Set multiple testing keys. Chris@0: $backend->set('cid_1', 'Some other value'); Chris@0: $items = [ Chris@0: 'cid_1' => ['data' => 1], Chris@0: 'cid_2' => ['data' => 2], Chris@0: 'cid_3' => ['data' => [1, 2]], Chris@0: 'cid_4' => ['data' => 1, 'expire' => $future_expiration], Chris@0: 'cid_5' => ['data' => 1, 'tags' => ['test:a', 'test:b']], Chris@0: ]; Chris@0: $backend->setMultiple($items); Chris@0: $cids = array_keys($items); Chris@0: $cached = $backend->getMultiple($cids); Chris@0: Chris@0: $this->assertEqual($cached['cid_1']->data, $items['cid_1']['data'], 'Over-written cache item set correctly.'); Chris@0: $this->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.'); Chris@0: $this->assertTrue($cached['cid_1']->created >= REQUEST_TIME && $cached['cid_1']->created <= round(microtime(TRUE), 3), 'Created time is correct.'); Chris@0: $this->assertEqual($cached['cid_1']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.'); Chris@0: Chris@0: $this->assertEqual($cached['cid_2']->data, $items['cid_2']['data'], 'New cache item set correctly.'); Chris@0: $this->assertEqual($cached['cid_2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.'); Chris@0: Chris@0: $this->assertEqual($cached['cid_3']->data, $items['cid_3']['data'], 'New cache item with serialized data set correctly.'); Chris@0: $this->assertEqual($cached['cid_3']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.'); Chris@0: Chris@0: $this->assertEqual($cached['cid_4']->data, $items['cid_4']['data'], 'New cache item set correctly.'); Chris@0: $this->assertEqual($cached['cid_4']->expire, $future_expiration, 'Cache expiration has been correctly set.'); Chris@0: Chris@0: $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.'); Chris@0: Chris@0: // Calling ::setMultiple() with invalid cache tags. This should fail an Chris@0: // assertion. Chris@0: try { Chris@0: $items = [ Chris@0: 'exception_test_1' => ['data' => 1, 'tags' => []], Chris@0: 'exception_test_2' => ['data' => 2, 'tags' => ['valid']], Chris@0: 'exception_test_3' => ['data' => 3, 'tags' => ['node' => [3, 5, 7]]], Chris@0: ]; Chris@0: $backend->setMultiple($items); Chris@0: $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.'); Chris@0: } Chris@0: catch (\AssertionError $e) { Chris@0: $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test Drupal\Core\Cache\CacheBackendInterface::delete() and Chris@0: * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple(). Chris@0: */ Chris@0: public function testDeleteMultiple() { Chris@0: $backend = $this->getCacheBackend(); Chris@0: Chris@0: // Set numerous testing keys. Chris@0: $backend->set('test1', 1); Chris@0: $backend->set('test2', 3); Chris@0: $backend->set('test3', 5); Chris@0: $backend->set('test4', 7); Chris@0: $backend->set('test5', 11); Chris@0: $backend->set('test6', 13); Chris@0: $backend->set('test7', 17); Chris@0: Chris@0: $backend->delete('test1'); Chris@0: // Nonexistent key should not cause an error. Chris@0: $backend->delete('test23'); Chris@0: $backend->deleteMultiple([ Chris@0: 'test3', Chris@0: 'test5', Chris@0: 'test7', Chris@0: // Nonexistent key should not cause an error. Chris@0: 'test19', Chris@0: // Nonexistent key should not cause an error. Chris@0: 'test21', Chris@0: ]); Chris@0: Chris@0: // Test if expected keys have been deleted. Chris@0: $this->assertIdentical(FALSE, $backend->get('test1'), "Cache id test1 deleted."); Chris@0: $this->assertIdentical(FALSE, $backend->get('test3'), "Cache id test3 deleted."); Chris@0: $this->assertIdentical(FALSE, $backend->get('test5'), "Cache id test5 deleted."); Chris@0: $this->assertIdentical(FALSE, $backend->get('test7'), "Cache id test7 deleted."); Chris@0: Chris@0: // Test if expected keys exist. Chris@0: $this->assertNotIdentical(FALSE, $backend->get('test2'), "Cache id test2 exists."); Chris@0: $this->assertNotIdentical(FALSE, $backend->get('test4'), "Cache id test4 exists."); Chris@0: $this->assertNotIdentical(FALSE, $backend->get('test6'), "Cache id test6 exists."); Chris@0: Chris@0: // Test if that expected keys do not exist. Chris@0: $this->assertIdentical(FALSE, $backend->get('test19'), "Cache id test19 does not exist."); Chris@0: $this->assertIdentical(FALSE, $backend->get('test21'), "Cache id test21 does not exist."); Chris@0: Chris@0: // Calling deleteMultiple() with an empty array should not cause an error. Chris@0: $this->assertFalse($backend->deleteMultiple([])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll(). Chris@0: */ Chris@0: public function testDeleteAll() { Chris@0: $backend_a = $this->getCacheBackend(); Chris@0: $backend_b = $this->getCacheBackend('bootstrap'); Chris@0: Chris@0: // Set both expiring and permanent keys. Chris@0: $backend_a->set('test1', 1, Cache::PERMANENT); Chris@0: $backend_a->set('test2', 3, time() + 1000); Chris@0: $backend_b->set('test3', 4, Cache::PERMANENT); Chris@0: Chris@0: $backend_a->deleteAll(); Chris@0: Chris@0: $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.'); Chris@0: $this->assertFalse($backend_a->get('test2'), 'Second key has been deleted.'); Chris@0: $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and Chris@0: * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple(). Chris@0: */ Chris@0: public function testInvalidate() { Chris@0: $backend = $this->getCacheBackend(); Chris@0: $backend->set('test1', 1); Chris@0: $backend->set('test2', 2); Chris@0: $backend->set('test3', 2); Chris@0: $backend->set('test4', 2); Chris@0: Chris@0: $reference = ['test1', 'test2', 'test3', 'test4']; Chris@0: Chris@0: $cids = $reference; Chris@0: $ret = $backend->getMultiple($cids); Chris@0: $this->assertEqual(count($ret), 4, 'Four items returned.'); Chris@0: Chris@0: $backend->invalidate('test1'); Chris@0: $backend->invalidateMultiple(['test2', 'test3']); Chris@0: Chris@0: $cids = $reference; Chris@0: $ret = $backend->getMultiple($cids); Chris@0: $this->assertEqual(count($ret), 1, 'Only one item element returned.'); Chris@0: Chris@0: $cids = $reference; Chris@0: $ret = $backend->getMultiple($cids, TRUE); Chris@0: $this->assertEqual(count($ret), 4, 'Four items returned.'); Chris@0: Chris@0: // Calling invalidateMultiple() with an empty array should not cause an Chris@0: // error. Chris@0: $this->assertFalse($backend->invalidateMultiple([])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags(). Chris@0: */ Chris@0: public function testInvalidateTags() { Chris@0: $backend = $this->getCacheBackend(); Chris@0: Chris@0: // Create two cache entries with the same tag and tag value. Chris@0: $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']); Chris@0: $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']); Chris@0: $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.'); Chris@0: Chris@0: // Invalidate test_tag of value 1. This should invalidate both entries. Chris@0: Cache::invalidateTags(['test_tag:2']); Chris@0: $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after invalidating a cache tag.'); Chris@0: $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.'); Chris@0: Chris@0: // Create two cache entries with the same tag and an array tag value. Chris@0: $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']); Chris@0: $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']); Chris@0: $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.'); Chris@0: Chris@0: // Invalidate test_tag of value 1. This should invalidate both entries. Chris@0: Cache::invalidateTags(['test_tag:1']); Chris@0: $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two caches removed after invalidating a cache tag.'); Chris@0: $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.'); Chris@0: Chris@0: // Create three cache entries with a mix of tags and tag values. Chris@0: $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']); Chris@0: $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']); Chris@0: $backend->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, ['test_tag_foo:3']); Chris@0: $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2') && $backend->get('test_cid_invalidate3'), 'Three cached items were created.'); Chris@0: Cache::invalidateTags(['test_tag_foo:3']); Chris@0: $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Cache items not matching the tag were not invalidated.'); Chris@0: $this->assertFalse($backend->get('test_cid_invalidated3'), 'Cached item matching the tag was removed.'); Chris@0: Chris@0: // Create cache entry in multiple bins. Two cache entries Chris@0: // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous Chris@0: // tests. Chris@0: $tags = ['test_tag:1', 'test_tag:2', 'test_tag:3']; Chris@0: $bins = ['path', 'bootstrap', 'page']; Chris@0: foreach ($bins as $bin) { Chris@0: $this->getCacheBackend($bin)->set('test', $this->defaultValue, Cache::PERMANENT, $tags); Chris@0: $this->assertTrue($this->getCacheBackend($bin)->get('test'), 'Cache item was set in bin.'); Chris@0: } Chris@0: Chris@0: Cache::invalidateTags(['test_tag:2']); Chris@0: Chris@0: // Test that the cache entry has been invalidated in multiple bins. Chris@0: foreach ($bins as $bin) { Chris@0: $this->assertFalse($this->getCacheBackend($bin)->get('test'), 'Tag invalidation affected item in bin.'); Chris@0: } Chris@0: // Test that the cache entry with a matching tag has been invalidated. Chris@0: $this->assertFalse($this->getCacheBackend($bin)->get('test_cid_invalidate2'), 'Cache items matching tag were invalidated.'); Chris@0: // Test that the cache entry with without a matching tag still exists. Chris@0: $this->assertTrue($this->getCacheBackend($bin)->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll(). Chris@0: */ Chris@0: public function testInvalidateAll() { Chris@0: $backend_a = $this->getCacheBackend(); Chris@0: $backend_b = $this->getCacheBackend('bootstrap'); Chris@0: Chris@0: // Set both expiring and permanent keys. Chris@0: $backend_a->set('test1', 1, Cache::PERMANENT); Chris@0: $backend_a->set('test2', 3, time() + 1000); Chris@0: $backend_b->set('test3', 4, Cache::PERMANENT); Chris@0: Chris@0: $backend_a->invalidateAll(); Chris@0: Chris@0: $this->assertFalse($backend_a->get('test1'), 'First key has been invalidated.'); Chris@0: $this->assertFalse($backend_a->get('test2'), 'Second key has been invalidated.'); Chris@0: $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.'); Chris@0: $this->assertTrue($backend_a->get('test1', TRUE), 'First key has not been deleted.'); Chris@0: $this->assertTrue($backend_a->get('test2', TRUE), 'Second key has not been deleted.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin(). Chris@0: */ Chris@0: public function testRemoveBin() { Chris@0: $backend_a = $this->getCacheBackend(); Chris@0: $backend_b = $this->getCacheBackend('bootstrap'); Chris@0: Chris@0: // Set both expiring and permanent keys. Chris@0: $backend_a->set('test1', 1, Cache::PERMANENT); Chris@0: $backend_a->set('test2', 3, time() + 1000); Chris@0: $backend_b->set('test3', 4, Cache::PERMANENT); Chris@0: Chris@0: $backend_a->removeBin(); Chris@0: Chris@0: $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.'); Chris@0: $this->assertFalse($backend_a->get('test2', TRUE), 'Second key has been deleted.'); Chris@0: $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.'); Chris@0: } Chris@0: Chris@0: }