3 namespace Drupal\KernelTests\Core\Cache;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\KernelTests\KernelTestBase;
10 * Tests any cache backend.
12 * Full generic unit test suite for any cache backend. In order to use it for a
13 * cache backend implementation, extend this class and override the
14 * createBackendInstance() method to return an object.
16 * @see DatabaseBackendUnitTestCase
17 * For a full working implementation.
19 abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
22 * Array of objects implementing Drupal\Core\Cache\CacheBackendInterface.
26 protected $cachebackends;
29 * Cache bin to use for testing.
36 * Random value to use in tests.
40 protected $defaultValue;
43 * Gets the testing bin.
45 * Override this method if you want to work on a different bin than the
51 protected function getTestBin() {
52 if (!isset($this->testBin)) {
53 $this->testBin = 'page';
55 return $this->testBin;
59 * Creates a cache backend to test.
61 * Override this method to test a CacheBackend.
64 * Bin name to use for this backend instance.
66 * @return \Drupal\Core\Cache\CacheBackendInterface
67 * Cache backend to test.
69 protected abstract function createCacheBackend($bin);
72 * Allows specific implementation to change the environment before a test run.
74 public function setUpCacheBackend() {
78 * Allows alteration of environment after a test run but before tear down.
80 * Used before the real tear down because the tear down will change things
81 * such as the database prefix.
83 public function tearDownCacheBackend() {
87 * Gets a backend to test; this will get a shared instance set in the object.
89 * @return \Drupal\Core\Cache\CacheBackendInterface
90 * Cache backend to test.
92 protected function getCacheBackend($bin = NULL) {
94 $bin = $this->getTestBin();
96 if (!isset($this->cachebackends[$bin])) {
97 $this->cachebackends[$bin] = $this->createCacheBackend($bin);
98 // Ensure the backend is empty.
99 $this->cachebackends[$bin]->deleteAll();
101 return $this->cachebackends[$bin];
104 protected function setUp() {
105 $this->cachebackends = [];
106 $this->defaultValue = $this->randomMachineName(10);
110 $this->setUpCacheBackend();
113 protected function tearDown() {
114 // Destruct the registered backend, each test will get a fresh instance,
115 // properly emptying it here ensure that on persistent data backends they
116 // will come up empty the next test.
117 foreach ($this->cachebackends as $bin => $cachebackend) {
118 $this->cachebackends[$bin]->deleteAll();
120 unset($this->cachebackends);
122 $this->tearDownCacheBackend();
128 * Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
130 public function testSetGet() {
131 $backend = $this->getCacheBackend();
133 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
134 $with_backslash = ['foo' => '\Drupal\foo\Bar'];
135 $backend->set('test1', $with_backslash);
136 $cached = $backend->get('test1');
137 $this->assert(is_object($cached), "Backend returned an object for cache id test1.");
138 $this->assertIdentical($with_backslash, $cached->data);
139 $this->assertTrue($cached->valid, 'Item is marked as valid.');
140 // We need to round because microtime may be rounded up in the backend.
141 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
142 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
144 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
145 $backend->set('test2', ['value' => 3], REQUEST_TIME + 3);
146 $cached = $backend->get('test2');
147 $this->assert(is_object($cached), "Backend returned an object for cache id test2.");
148 $this->assertIdentical(['value' => 3], $cached->data);
149 $this->assertTrue($cached->valid, 'Item is marked as valid.');
150 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
151 $this->assertEqual($cached->expire, REQUEST_TIME + 3, 'Expire time is correct.');
153 $backend->set('test3', 'foobar', REQUEST_TIME - 3);
154 $this->assertFalse($backend->get('test3'), 'Invalid item not returned.');
155 $cached = $backend->get('test3', TRUE);
156 $this->assert(is_object($cached), 'Backend returned an object for cache id test3.');
157 $this->assertFalse($cached->valid, 'Item is marked as valid.');
158 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
159 $this->assertEqual($cached->expire, REQUEST_TIME - 3, 'Expire time is correct.');
161 $this->assertIdentical(FALSE, $backend->get('test4'), "Backend does not contain data for cache id test4.");
162 $with_eof = ['foo' => "\nEOF\ndata"];
163 $backend->set('test4', $with_eof);
164 $cached = $backend->get('test4');
165 $this->assert(is_object($cached), "Backend returned an object for cache id test4.");
166 $this->assertIdentical($with_eof, $cached->data);
167 $this->assertTrue($cached->valid, 'Item is marked as valid.');
168 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
169 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
171 $this->assertIdentical(FALSE, $backend->get('test5'), "Backend does not contain data for cache id test5.");
172 $with_eof_and_semicolon = ['foo' => "\nEOF;\ndata"];
173 $backend->set('test5', $with_eof_and_semicolon);
174 $cached = $backend->get('test5');
175 $this->assert(is_object($cached), "Backend returned an object for cache id test5.");
176 $this->assertIdentical($with_eof_and_semicolon, $cached->data);
177 $this->assertTrue($cached->valid, 'Item is marked as valid.');
178 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
179 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
181 $with_variable = ['foo' => '$bar'];
182 $backend->set('test6', $with_variable);
183 $cached = $backend->get('test6');
184 $this->assert(is_object($cached), "Backend returned an object for cache id test6.");
185 $this->assertIdentical($with_variable, $cached->data);
187 // Make sure that a cached object is not affected by changing the original.
188 $data = new \stdClass();
190 $data->obj = new \stdClass();
191 $data->obj->value = 2;
192 $backend->set('test7', $data);
193 $expected_data = clone $data;
194 // Add a property to the original. It should not appear in the cached data.
195 $data->this_should_not_be_in_the_cache = TRUE;
196 $cached = $backend->get('test7');
197 $this->assert(is_object($cached), "Backend returned an object for cache id test7.");
198 $this->assertEqual($expected_data, $cached->data);
199 $this->assertFalse(isset($cached->data->this_should_not_be_in_the_cache));
200 // Add a property to the cache data. It should not appear when we fetch
201 // the data from cache again.
202 $cached->data->this_should_not_be_in_the_cache = TRUE;
203 $fresh_cached = $backend->get('test7');
204 $this->assertFalse(isset($fresh_cached->data->this_should_not_be_in_the_cache));
206 // Check with a long key.
207 $cid = str_repeat('a', 300);
208 $backend->set($cid, 'test');
209 $this->assertEqual('test', $backend->get($cid)->data);
211 // Check that the cache key is case sensitive.
212 $backend->set('TEST8', 'value');
213 $this->assertEqual('value', $backend->get('TEST8')->data);
214 $this->assertFalse($backend->get('test8'));
216 // Calling ::set() with invalid cache tags. This should fail an assertion.
218 $backend->set('assertion_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]);
219 $this->fail('::set() was called with invalid cache tags, runtime assertion did not fail.');
221 catch (\AssertionError $e) {
222 $this->pass('::set() was called with invalid cache tags, runtime assertion failed.');
227 * Tests Drupal\Core\Cache\CacheBackendInterface::delete().
229 public function testDelete() {
230 $backend = $this->getCacheBackend();
232 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
233 $backend->set('test1', 7);
234 $this->assert(is_object($backend->get('test1')), "Backend returned an object for cache id test1.");
236 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
237 $backend->set('test2', 3);
238 $this->assert(is_object($backend->get('test2')), "Backend returned an object for cache id %cid.");
240 $backend->delete('test1');
241 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1 after deletion.");
243 $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2.");
245 $backend->delete('test2');
246 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2 after deletion.");
248 $long_cid = str_repeat('a', 300);
249 $backend->set($long_cid, 'test');
250 $backend->delete($long_cid);
251 $this->assertIdentical(FALSE, $backend->get($long_cid), "Backend does not contain data for long cache id after deletion.");
255 * Tests data type preservation.
257 public function testValueTypeIsKept() {
258 $backend = $this->getCacheBackend();
266 'test6' => [1, 2, 3],
269 // Create cache entries.
270 foreach ($variables as $cid => $data) {
271 $backend->set($cid, $data);
274 // Retrieve and test cache objects.
275 foreach ($variables as $cid => $value) {
276 $object = $backend->get($cid);
277 $this->assert(is_object($object), sprintf("Backend returned an object for cache id %s.", $cid));
278 $this->assertIdentical($value, $object->data, sprintf("Data of cached id %s kept is identical in type and value", $cid));
283 * Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().
285 public function testGetMultiple() {
286 $backend = $this->getCacheBackend();
288 // Set numerous testing keys.
289 $long_cid = str_repeat('a', 300);
290 $backend->set('test1', 1);
291 $backend->set('test2', 3);
292 $backend->set('test3', 5);
293 $backend->set('test4', 7);
294 $backend->set('test5', 11);
295 $backend->set('test6', 13);
296 $backend->set('test7', 17);
297 $backend->set($long_cid, 300);
299 // Mismatch order for harder testing.
303 'test21', // Cid does not exist.
305 'test19', // Cid does not exist until added before second getMultiple().
310 $ret = $backend->getMultiple($cids);
311 // Test return - ensure it contains existing cache ids.
312 $this->assert(isset($ret['test2']), "Existing cache id test2 is set.");
313 $this->assert(isset($ret['test3']), "Existing cache id test3 is set.");
314 $this->assert(isset($ret['test6']), "Existing cache id test6 is set.");
315 $this->assert(isset($ret['test7']), "Existing cache id test7 is set.");
316 // Test return - ensure that objects has expected properties.
317 $this->assertTrue($ret['test2']->valid, 'Item is marked as valid.');
318 $this->assertTrue($ret['test2']->created >= REQUEST_TIME && $ret['test2']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
319 $this->assertEqual($ret['test2']->expire, Cache::PERMANENT, 'Expire time is correct.');
320 // Test return - ensure it does not contain nonexistent cache ids.
321 $this->assertFalse(isset($ret['test19']), "Nonexistent cache id test19 is not set.");
322 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set.");
324 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
325 $this->assertIdentical($ret['test3']->data, 5, "Existing cache id test3 has the correct value.");
326 $this->assertIdentical($ret['test6']->data, 13, "Existing cache id test6 has the correct value.");
327 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
328 // Test $cids array - ensure it contains cache id's that do not exist.
329 $this->assert(in_array('test19', $cids), "Nonexistent cache id test19 is in cids array.");
330 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
331 // Test $cids array - ensure it does not contain cache id's that exist.
332 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
333 $this->assertFalse(in_array('test3', $cids), "Existing cache id test3 is not in cids array.");
334 $this->assertFalse(in_array('test6', $cids), "Existing cache id test6 is not in cids array.");
335 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
337 // Test a second time after deleting and setting new keys which ensures that
338 // if the backend uses statics it does not cause unexpected results.
339 $backend->delete('test3');
340 $backend->delete('test6');
341 $backend->set('test19', 57);
344 $ret = $backend->getMultiple($cids);
345 // Test return - ensure it contains existing cache ids.
346 $this->assert(isset($ret['test2']), "Existing cache id test2 is set");
347 $this->assert(isset($ret['test7']), "Existing cache id test7 is set");
348 $this->assert(isset($ret['test19']), "Added cache id test19 is set");
349 // Test return - ensure it does not contain nonexistent cache ids.
350 $this->assertFalse(isset($ret['test3']), "Deleted cache id test3 is not set");
351 $this->assertFalse(isset($ret['test6']), "Deleted cache id test6 is not set");
352 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set");
354 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
355 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
356 $this->assertIdentical($ret['test19']->data, 57, "Added cache id test19 has the correct value.");
357 // Test $cids array - ensure it contains cache id's that do not exist.
358 $this->assert(in_array('test3', $cids), "Deleted cache id test3 is in cids array.");
359 $this->assert(in_array('test6', $cids), "Deleted cache id test6 is in cids array.");
360 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
361 // Test $cids array - ensure it does not contain cache id's that exist.
362 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
363 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
364 $this->assertFalse(in_array('test19', $cids), "Added cache id test19 is not in cids array.");
366 // Test with a long $cid and non-numeric array key.
367 $cids = ['key:key' => $long_cid];
368 $return = $backend->getMultiple($cids);
369 $this->assertEqual(300, $return[$long_cid]->data);
370 $this->assertTrue(empty($cids));
374 * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
376 public function testSetMultiple() {
377 $backend = $this->getCacheBackend();
379 $future_expiration = REQUEST_TIME + 100;
381 // Set multiple testing keys.
382 $backend->set('cid_1', 'Some other value');
384 'cid_1' => ['data' => 1],
385 'cid_2' => ['data' => 2],
386 'cid_3' => ['data' => [1, 2]],
387 'cid_4' => ['data' => 1, 'expire' => $future_expiration],
388 'cid_5' => ['data' => 1, 'tags' => ['test:a', 'test:b']],
390 $backend->setMultiple($items);
391 $cids = array_keys($items);
392 $cached = $backend->getMultiple($cids);
394 $this->assertEqual($cached['cid_1']->data, $items['cid_1']['data'], 'Over-written cache item set correctly.');
395 $this->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.');
396 $this->assertTrue($cached['cid_1']->created >= REQUEST_TIME && $cached['cid_1']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
397 $this->assertEqual($cached['cid_1']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
399 $this->assertEqual($cached['cid_2']->data, $items['cid_2']['data'], 'New cache item set correctly.');
400 $this->assertEqual($cached['cid_2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
402 $this->assertEqual($cached['cid_3']->data, $items['cid_3']['data'], 'New cache item with serialized data set correctly.');
403 $this->assertEqual($cached['cid_3']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
405 $this->assertEqual($cached['cid_4']->data, $items['cid_4']['data'], 'New cache item set correctly.');
406 $this->assertEqual($cached['cid_4']->expire, $future_expiration, 'Cache expiration has been correctly set.');
408 $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
410 // Calling ::setMultiple() with invalid cache tags. This should fail an
414 'exception_test_1' => ['data' => 1, 'tags' => []],
415 'exception_test_2' => ['data' => 2, 'tags' => ['valid']],
416 'exception_test_3' => ['data' => 3, 'tags' => ['node' => [3, 5, 7]]],
418 $backend->setMultiple($items);
419 $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
421 catch (\AssertionError $e) {
422 $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
427 * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
428 * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
430 public function testDeleteMultiple() {
431 $backend = $this->getCacheBackend();
433 // Set numerous testing keys.
434 $backend->set('test1', 1);
435 $backend->set('test2', 3);
436 $backend->set('test3', 5);
437 $backend->set('test4', 7);
438 $backend->set('test5', 11);
439 $backend->set('test6', 13);
440 $backend->set('test7', 17);
442 $backend->delete('test1');
443 $backend->delete('test23'); // Nonexistent key should not cause an error.
444 $backend->deleteMultiple([
448 'test19', // Nonexistent key should not cause an error.
449 'test21', // Nonexistent key should not cause an error.
452 // Test if expected keys have been deleted.
453 $this->assertIdentical(FALSE, $backend->get('test1'), "Cache id test1 deleted.");
454 $this->assertIdentical(FALSE, $backend->get('test3'), "Cache id test3 deleted.");
455 $this->assertIdentical(FALSE, $backend->get('test5'), "Cache id test5 deleted.");
456 $this->assertIdentical(FALSE, $backend->get('test7'), "Cache id test7 deleted.");
458 // Test if expected keys exist.
459 $this->assertNotIdentical(FALSE, $backend->get('test2'), "Cache id test2 exists.");
460 $this->assertNotIdentical(FALSE, $backend->get('test4'), "Cache id test4 exists.");
461 $this->assertNotIdentical(FALSE, $backend->get('test6'), "Cache id test6 exists.");
463 // Test if that expected keys do not exist.
464 $this->assertIdentical(FALSE, $backend->get('test19'), "Cache id test19 does not exist.");
465 $this->assertIdentical(FALSE, $backend->get('test21'), "Cache id test21 does not exist.");
467 // Calling deleteMultiple() with an empty array should not cause an error.
468 $this->assertFalse($backend->deleteMultiple([]));
472 * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
474 public function testDeleteAll() {
475 $backend_a = $this->getCacheBackend();
476 $backend_b = $this->getCacheBackend('bootstrap');
478 // Set both expiring and permanent keys.
479 $backend_a->set('test1', 1, Cache::PERMANENT);
480 $backend_a->set('test2', 3, time() + 1000);
481 $backend_b->set('test3', 4, Cache::PERMANENT);
483 $backend_a->deleteAll();
485 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
486 $this->assertFalse($backend_a->get('test2'), 'Second key has been deleted.');
487 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
491 * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
492 * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
494 public function testInvalidate() {
495 $backend = $this->getCacheBackend();
496 $backend->set('test1', 1);
497 $backend->set('test2', 2);
498 $backend->set('test3', 2);
499 $backend->set('test4', 2);
501 $reference = ['test1', 'test2', 'test3', 'test4'];
504 $ret = $backend->getMultiple($cids);
505 $this->assertEqual(count($ret), 4, 'Four items returned.');
507 $backend->invalidate('test1');
508 $backend->invalidateMultiple(['test2', 'test3']);
511 $ret = $backend->getMultiple($cids);
512 $this->assertEqual(count($ret), 1, 'Only one item element returned.');
515 $ret = $backend->getMultiple($cids, TRUE);
516 $this->assertEqual(count($ret), 4, 'Four items returned.');
518 // Calling invalidateMultiple() with an empty array should not cause an
520 $this->assertFalse($backend->invalidateMultiple([]));
524 * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
526 public function testInvalidateTags() {
527 $backend = $this->getCacheBackend();
529 // Create two cache entries with the same tag and tag value.
530 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
531 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
532 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
534 // Invalidate test_tag of value 1. This should invalidate both entries.
535 Cache::invalidateTags(['test_tag:2']);
536 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after invalidating a cache tag.');
537 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
539 // Create two cache entries with the same tag and an array tag value.
540 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
541 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
542 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
544 // Invalidate test_tag of value 1. This should invalidate both entries.
545 Cache::invalidateTags(['test_tag:1']);
546 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two caches removed after invalidating a cache tag.');
547 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
549 // Create three cache entries with a mix of tags and tag values.
550 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
551 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
552 $backend->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, ['test_tag_foo:3']);
553 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2') && $backend->get('test_cid_invalidate3'), 'Three cached items were created.');
554 Cache::invalidateTags(['test_tag_foo:3']);
555 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Cache items not matching the tag were not invalidated.');
556 $this->assertFalse($backend->get('test_cid_invalidated3'), 'Cached item matching the tag was removed.');
558 // Create cache entry in multiple bins. Two cache entries
559 // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
561 $tags = ['test_tag:1', 'test_tag:2', 'test_tag:3'];
562 $bins = ['path', 'bootstrap', 'page'];
563 foreach ($bins as $bin) {
564 $this->getCacheBackend($bin)->set('test', $this->defaultValue, Cache::PERMANENT, $tags);
565 $this->assertTrue($this->getCacheBackend($bin)->get('test'), 'Cache item was set in bin.');
568 Cache::invalidateTags(['test_tag:2']);
570 // Test that the cache entry has been invalidated in multiple bins.
571 foreach ($bins as $bin) {
572 $this->assertFalse($this->getCacheBackend($bin)->get('test'), 'Tag invalidation affected item in bin.');
574 // Test that the cache entry with a matching tag has been invalidated.
575 $this->assertFalse($this->getCacheBackend($bin)->get('test_cid_invalidate2'), 'Cache items matching tag were invalidated.');
576 // Test that the cache entry with without a matching tag still exists.
577 $this->assertTrue($this->getCacheBackend($bin)->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
581 * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
583 public function testInvalidateAll() {
584 $backend_a = $this->getCacheBackend();
585 $backend_b = $this->getCacheBackend('bootstrap');
587 // Set both expiring and permanent keys.
588 $backend_a->set('test1', 1, Cache::PERMANENT);
589 $backend_a->set('test2', 3, time() + 1000);
590 $backend_b->set('test3', 4, Cache::PERMANENT);
592 $backend_a->invalidateAll();
594 $this->assertFalse($backend_a->get('test1'), 'First key has been invalidated.');
595 $this->assertFalse($backend_a->get('test2'), 'Second key has been invalidated.');
596 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
597 $this->assertTrue($backend_a->get('test1', TRUE), 'First key has not been deleted.');
598 $this->assertTrue($backend_a->get('test2', TRUE), 'Second key has not been deleted.');
602 * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin().
604 public function testRemoveBin() {
605 $backend_a = $this->getCacheBackend();
606 $backend_b = $this->getCacheBackend('bootstrap');
608 // Set both expiring and permanent keys.
609 $backend_a->set('test1', 1, Cache::PERMANENT);
610 $backend_a->set('test2', 3, time() + 1000);
611 $backend_b->set('test3', 4, Cache::PERMANENT);
613 $backend_a->removeBin();
615 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
616 $this->assertFalse($backend_a->get('test2', TRUE), 'Second key has been deleted.');
617 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');