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 abstract protected 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->assertSame(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->assertSame($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->assertSame(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->assertSame(['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->assertSame(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->assertSame($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->assertSame(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->assertSame($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->assertSame($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->assertSame(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->assertSame(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->assertSame(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->assertSame(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->assertSame(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->assertSame($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 // Cid does not exist.
306 // Cid does not exist until added before second getMultiple().
312 $ret = $backend->getMultiple($cids);
313 // Test return - ensure it contains existing cache ids.
314 $this->assert(isset($ret['test2']), "Existing cache id test2 is set.");
315 $this->assert(isset($ret['test3']), "Existing cache id test3 is set.");
316 $this->assert(isset($ret['test6']), "Existing cache id test6 is set.");
317 $this->assert(isset($ret['test7']), "Existing cache id test7 is set.");
318 // Test return - ensure that objects has expected properties.
319 $this->assertTrue($ret['test2']->valid, 'Item is marked as valid.');
320 $this->assertTrue($ret['test2']->created >= REQUEST_TIME && $ret['test2']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
321 $this->assertEqual($ret['test2']->expire, Cache::PERMANENT, 'Expire time is correct.');
322 // Test return - ensure it does not contain nonexistent cache ids.
323 $this->assertFalse(isset($ret['test19']), "Nonexistent cache id test19 is not set.");
324 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set.");
326 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
327 $this->assertIdentical($ret['test3']->data, 5, "Existing cache id test3 has the correct value.");
328 $this->assertIdentical($ret['test6']->data, 13, "Existing cache id test6 has the correct value.");
329 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
330 // Test $cids array - ensure it contains cache id's that do not exist.
331 $this->assert(in_array('test19', $cids), "Nonexistent cache id test19 is in cids array.");
332 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
333 // Test $cids array - ensure it does not contain cache id's that exist.
334 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
335 $this->assertFalse(in_array('test3', $cids), "Existing cache id test3 is not in cids array.");
336 $this->assertFalse(in_array('test6', $cids), "Existing cache id test6 is not in cids array.");
337 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
339 // Test a second time after deleting and setting new keys which ensures that
340 // if the backend uses statics it does not cause unexpected results.
341 $backend->delete('test3');
342 $backend->delete('test6');
343 $backend->set('test19', 57);
346 $ret = $backend->getMultiple($cids);
347 // Test return - ensure it contains existing cache ids.
348 $this->assert(isset($ret['test2']), "Existing cache id test2 is set");
349 $this->assert(isset($ret['test7']), "Existing cache id test7 is set");
350 $this->assert(isset($ret['test19']), "Added cache id test19 is set");
351 // Test return - ensure it does not contain nonexistent cache ids.
352 $this->assertFalse(isset($ret['test3']), "Deleted cache id test3 is not set");
353 $this->assertFalse(isset($ret['test6']), "Deleted cache id test6 is not set");
354 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set");
356 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
357 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
358 $this->assertIdentical($ret['test19']->data, 57, "Added cache id test19 has the correct value.");
359 // Test $cids array - ensure it contains cache id's that do not exist.
360 $this->assert(in_array('test3', $cids), "Deleted cache id test3 is in cids array.");
361 $this->assert(in_array('test6', $cids), "Deleted cache id test6 is in cids array.");
362 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
363 // Test $cids array - ensure it does not contain cache id's that exist.
364 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
365 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
366 $this->assertFalse(in_array('test19', $cids), "Added cache id test19 is not in cids array.");
368 // Test with a long $cid and non-numeric array key.
369 $cids = ['key:key' => $long_cid];
370 $return = $backend->getMultiple($cids);
371 $this->assertEqual(300, $return[$long_cid]->data);
372 $this->assertTrue(empty($cids));
376 * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
378 public function testSetMultiple() {
379 $backend = $this->getCacheBackend();
381 $future_expiration = REQUEST_TIME + 100;
383 // Set multiple testing keys.
384 $backend->set('cid_1', 'Some other value');
386 'cid_1' => ['data' => 1],
387 'cid_2' => ['data' => 2],
388 'cid_3' => ['data' => [1, 2]],
389 'cid_4' => ['data' => 1, 'expire' => $future_expiration],
390 'cid_5' => ['data' => 1, 'tags' => ['test:a', 'test:b']],
392 $backend->setMultiple($items);
393 $cids = array_keys($items);
394 $cached = $backend->getMultiple($cids);
396 $this->assertEqual($cached['cid_1']->data, $items['cid_1']['data'], 'Over-written cache item set correctly.');
397 $this->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.');
398 $this->assertTrue($cached['cid_1']->created >= REQUEST_TIME && $cached['cid_1']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
399 $this->assertEqual($cached['cid_1']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
401 $this->assertEqual($cached['cid_2']->data, $items['cid_2']['data'], 'New cache item set correctly.');
402 $this->assertEqual($cached['cid_2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
404 $this->assertEqual($cached['cid_3']->data, $items['cid_3']['data'], 'New cache item with serialized data set correctly.');
405 $this->assertEqual($cached['cid_3']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
407 $this->assertEqual($cached['cid_4']->data, $items['cid_4']['data'], 'New cache item set correctly.');
408 $this->assertEqual($cached['cid_4']->expire, $future_expiration, 'Cache expiration has been correctly set.');
410 $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
412 // Calling ::setMultiple() with invalid cache tags. This should fail an
416 'exception_test_1' => ['data' => 1, 'tags' => []],
417 'exception_test_2' => ['data' => 2, 'tags' => ['valid']],
418 'exception_test_3' => ['data' => 3, 'tags' => ['node' => [3, 5, 7]]],
420 $backend->setMultiple($items);
421 $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
423 catch (\AssertionError $e) {
424 $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
429 * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
430 * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
432 public function testDeleteMultiple() {
433 $backend = $this->getCacheBackend();
435 // Set numerous testing keys.
436 $backend->set('test1', 1);
437 $backend->set('test2', 3);
438 $backend->set('test3', 5);
439 $backend->set('test4', 7);
440 $backend->set('test5', 11);
441 $backend->set('test6', 13);
442 $backend->set('test7', 17);
444 $backend->delete('test1');
445 // Nonexistent key should not cause an error.
446 $backend->delete('test23');
447 $backend->deleteMultiple([
451 // Nonexistent key should not cause an error.
453 // Nonexistent key should not cause an error.
457 // Test if expected keys have been deleted.
458 $this->assertSame(FALSE, $backend->get('test1'), "Cache id test1 deleted.");
459 $this->assertSame(FALSE, $backend->get('test3'), "Cache id test3 deleted.");
460 $this->assertSame(FALSE, $backend->get('test5'), "Cache id test5 deleted.");
461 $this->assertSame(FALSE, $backend->get('test7'), "Cache id test7 deleted.");
463 // Test if expected keys exist.
464 $this->assertNotIdentical(FALSE, $backend->get('test2'), "Cache id test2 exists.");
465 $this->assertNotIdentical(FALSE, $backend->get('test4'), "Cache id test4 exists.");
466 $this->assertNotIdentical(FALSE, $backend->get('test6'), "Cache id test6 exists.");
468 // Test if that expected keys do not exist.
469 $this->assertSame(FALSE, $backend->get('test19'), "Cache id test19 does not exist.");
470 $this->assertSame(FALSE, $backend->get('test21'), "Cache id test21 does not exist.");
472 // Calling deleteMultiple() with an empty array should not cause an error.
473 $this->assertFalse($backend->deleteMultiple([]));
477 * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
479 public function testDeleteAll() {
480 $backend_a = $this->getCacheBackend();
481 $backend_b = $this->getCacheBackend('bootstrap');
483 // Set both expiring and permanent keys.
484 $backend_a->set('test1', 1, Cache::PERMANENT);
485 $backend_a->set('test2', 3, time() + 1000);
486 $backend_b->set('test3', 4, Cache::PERMANENT);
488 $backend_a->deleteAll();
490 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
491 $this->assertFalse($backend_a->get('test2'), 'Second key has been deleted.');
492 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
496 * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
497 * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
499 public function testInvalidate() {
500 $backend = $this->getCacheBackend();
501 $backend->set('test1', 1);
502 $backend->set('test2', 2);
503 $backend->set('test3', 2);
504 $backend->set('test4', 2);
506 $reference = ['test1', 'test2', 'test3', 'test4'];
509 $ret = $backend->getMultiple($cids);
510 $this->assertEqual(count($ret), 4, 'Four items returned.');
512 $backend->invalidate('test1');
513 $backend->invalidateMultiple(['test2', 'test3']);
516 $ret = $backend->getMultiple($cids);
517 $this->assertEqual(count($ret), 1, 'Only one item element returned.');
520 $ret = $backend->getMultiple($cids, TRUE);
521 $this->assertEqual(count($ret), 4, 'Four items returned.');
523 // Calling invalidateMultiple() with an empty array should not cause an
525 $this->assertFalse($backend->invalidateMultiple([]));
529 * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
531 public function testInvalidateTags() {
532 $backend = $this->getCacheBackend();
534 // Create two cache entries with the same tag and tag value.
535 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
536 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
537 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
539 // Invalidate test_tag of value 1. This should invalidate both entries.
540 Cache::invalidateTags(['test_tag:2']);
541 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after invalidating a cache tag.');
542 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
544 // Create two cache entries with the same tag and an array tag value.
545 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
546 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
547 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
549 // Invalidate test_tag of value 1. This should invalidate both entries.
550 Cache::invalidateTags(['test_tag:1']);
551 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two caches removed after invalidating a cache tag.');
552 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
554 // Create three cache entries with a mix of tags and tag values.
555 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
556 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
557 $backend->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, ['test_tag_foo:3']);
558 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2') && $backend->get('test_cid_invalidate3'), 'Three cached items were created.');
559 Cache::invalidateTags(['test_tag_foo:3']);
560 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Cache items not matching the tag were not invalidated.');
561 $this->assertFalse($backend->get('test_cid_invalidated3'), 'Cached item matching the tag was removed.');
563 // Create cache entry in multiple bins. Two cache entries
564 // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
566 $tags = ['test_tag:1', 'test_tag:2', 'test_tag:3'];
567 $bins = ['path', 'bootstrap', 'page'];
568 foreach ($bins as $bin) {
569 $this->getCacheBackend($bin)->set('test', $this->defaultValue, Cache::PERMANENT, $tags);
570 $this->assertTrue($this->getCacheBackend($bin)->get('test'), 'Cache item was set in bin.');
573 Cache::invalidateTags(['test_tag:2']);
575 // Test that the cache entry has been invalidated in multiple bins.
576 foreach ($bins as $bin) {
577 $this->assertFalse($this->getCacheBackend($bin)->get('test'), 'Tag invalidation affected item in bin.');
579 // Test that the cache entry with a matching tag has been invalidated.
580 $this->assertFalse($this->getCacheBackend($bin)->get('test_cid_invalidate2'), 'Cache items matching tag were invalidated.');
581 // Test that the cache entry with without a matching tag still exists.
582 $this->assertTrue($this->getCacheBackend($bin)->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
586 * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
588 public function testInvalidateAll() {
589 $backend_a = $this->getCacheBackend();
590 $backend_b = $this->getCacheBackend('bootstrap');
592 // Set both expiring and permanent keys.
593 $backend_a->set('test1', 1, Cache::PERMANENT);
594 $backend_a->set('test2', 3, time() + 1000);
595 $backend_b->set('test3', 4, Cache::PERMANENT);
597 $backend_a->invalidateAll();
599 $this->assertFalse($backend_a->get('test1'), 'First key has been invalidated.');
600 $this->assertFalse($backend_a->get('test2'), 'Second key has been invalidated.');
601 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
602 $this->assertTrue($backend_a->get('test1', TRUE), 'First key has not been deleted.');
603 $this->assertTrue($backend_a->get('test2', TRUE), 'Second key has not been deleted.');
607 * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin().
609 public function testRemoveBin() {
610 $backend_a = $this->getCacheBackend();
611 $backend_b = $this->getCacheBackend('bootstrap');
613 // Set both expiring and permanent keys.
614 $backend_a->set('test1', 1, Cache::PERMANENT);
615 $backend_a->set('test2', 3, time() + 1000);
616 $backend_b->set('test3', 4, Cache::PERMANENT);
618 $backend_a->removeBin();
620 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
621 $this->assertFalse($backend_a->get('test2', TRUE), 'Second key has been deleted.');
622 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');