3 namespace Drupal\system\Tests\Cache;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\simpletest\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 * @deprecated as of Drupal 8.2.x, will be removed before Drupal 9.0.0. Use
20 * \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase instead.
22 abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
25 * Array of objects implementing Drupal\Core\Cache\CacheBackendInterface.
29 protected $cachebackends;
32 * Cache bin to use for testing.
39 * Random value to use in tests.
43 protected $defaultValue;
46 * Gets the testing bin.
48 * Override this method if you want to work on a different bin than the
54 protected function getTestBin() {
55 if (!isset($this->testBin)) {
56 $this->testBin = 'page';
58 return $this->testBin;
62 * Creates a cache backend to test.
64 * Override this method to test a CacheBackend.
67 * Bin name to use for this backend instance.
69 * @return \Drupal\Core\Cache\CacheBackendInterface
70 * Cache backend to test.
72 abstract protected function createCacheBackend($bin);
75 * Allows specific implementation to change the environment before a test run.
77 public function setUpCacheBackend() {
81 * Allows alteration of environment after a test run but before tear down.
83 * Used before the real tear down because the tear down will change things
84 * such as the database prefix.
86 public function tearDownCacheBackend() {
90 * Gets a backend to test; this will get a shared instance set in the object.
92 * @return \Drupal\Core\Cache\CacheBackendInterface
93 * Cache backend to test.
95 protected function getCacheBackend($bin = NULL) {
97 $bin = $this->getTestBin();
99 if (!isset($this->cachebackends[$bin])) {
100 $this->cachebackends[$bin] = $this->createCacheBackend($bin);
101 // Ensure the backend is empty.
102 $this->cachebackends[$bin]->deleteAll();
104 return $this->cachebackends[$bin];
107 protected function setUp() {
108 $this->cachebackends = [];
109 $this->defaultValue = $this->randomMachineName(10);
113 $this->setUpCacheBackend();
116 protected function tearDown() {
117 // Destruct the registered backend, each test will get a fresh instance,
118 // properly emptying it here ensure that on persistent data backends they
119 // will come up empty the next test.
120 foreach ($this->cachebackends as $bin => $cachebackend) {
121 $this->cachebackends[$bin]->deleteAll();
123 unset($this->cachebackends);
125 $this->tearDownCacheBackend();
131 * Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
133 public function testSetGet() {
134 $backend = $this->getCacheBackend();
136 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
137 $with_backslash = ['foo' => '\Drupal\foo\Bar'];
138 $backend->set('test1', $with_backslash);
139 $cached = $backend->get('test1');
140 $this->assert(is_object($cached), "Backend returned an object for cache id test1.");
141 $this->assertIdentical($with_backslash, $cached->data);
142 $this->assertTrue($cached->valid, 'Item is marked as valid.');
143 // We need to round because microtime may be rounded up in the backend.
144 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
145 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
147 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
148 $backend->set('test2', ['value' => 3], REQUEST_TIME + 3);
149 $cached = $backend->get('test2');
150 $this->assert(is_object($cached), "Backend returned an object for cache id test2.");
151 $this->assertIdentical(['value' => 3], $cached->data);
152 $this->assertTrue($cached->valid, 'Item is marked as valid.');
153 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
154 $this->assertEqual($cached->expire, REQUEST_TIME + 3, 'Expire time is correct.');
156 $backend->set('test3', 'foobar', REQUEST_TIME - 3);
157 $this->assertFalse($backend->get('test3'), 'Invalid item not returned.');
158 $cached = $backend->get('test3', TRUE);
159 $this->assert(is_object($cached), 'Backend returned an object for cache id test3.');
160 $this->assertFalse($cached->valid, 'Item is marked as valid.');
161 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
162 $this->assertEqual($cached->expire, REQUEST_TIME - 3, 'Expire time is correct.');
164 $this->assertIdentical(FALSE, $backend->get('test4'), "Backend does not contain data for cache id test4.");
165 $with_eof = ['foo' => "\nEOF\ndata"];
166 $backend->set('test4', $with_eof);
167 $cached = $backend->get('test4');
168 $this->assert(is_object($cached), "Backend returned an object for cache id test4.");
169 $this->assertIdentical($with_eof, $cached->data);
170 $this->assertTrue($cached->valid, 'Item is marked as valid.');
171 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
172 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
174 $this->assertIdentical(FALSE, $backend->get('test5'), "Backend does not contain data for cache id test5.");
175 $with_eof_and_semicolon = ['foo' => "\nEOF;\ndata"];
176 $backend->set('test5', $with_eof_and_semicolon);
177 $cached = $backend->get('test5');
178 $this->assert(is_object($cached), "Backend returned an object for cache id test5.");
179 $this->assertIdentical($with_eof_and_semicolon, $cached->data);
180 $this->assertTrue($cached->valid, 'Item is marked as valid.');
181 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
182 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
184 $with_variable = ['foo' => '$bar'];
185 $backend->set('test6', $with_variable);
186 $cached = $backend->get('test6');
187 $this->assert(is_object($cached), "Backend returned an object for cache id test6.");
188 $this->assertIdentical($with_variable, $cached->data);
190 // Make sure that a cached object is not affected by changing the original.
191 $data = new \stdClass();
193 $data->obj = new \stdClass();
194 $data->obj->value = 2;
195 $backend->set('test7', $data);
196 $expected_data = clone $data;
197 // Add a property to the original. It should not appear in the cached data.
198 $data->this_should_not_be_in_the_cache = TRUE;
199 $cached = $backend->get('test7');
200 $this->assert(is_object($cached), "Backend returned an object for cache id test7.");
201 $this->assertEqual($expected_data, $cached->data);
202 $this->assertFalse(isset($cached->data->this_should_not_be_in_the_cache));
203 // Add a property to the cache data. It should not appear when we fetch
204 // the data from cache again.
205 $cached->data->this_should_not_be_in_the_cache = TRUE;
206 $fresh_cached = $backend->get('test7');
207 $this->assertFalse(isset($fresh_cached->data->this_should_not_be_in_the_cache));
209 // Check with a long key.
210 $cid = str_repeat('a', 300);
211 $backend->set($cid, 'test');
212 $this->assertEqual('test', $backend->get($cid)->data);
214 // Check that the cache key is case sensitive.
215 $backend->set('TEST8', 'value');
216 $this->assertEqual('value', $backend->get('TEST8')->data);
217 $this->assertFalse($backend->get('test8'));
219 // Calling ::set() with invalid cache tags. This should fail an assertion.
221 $backend->set('assertion_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]);
222 $this->fail('::set() was called with invalid cache tags, runtime assertion did not fail.');
224 catch (\AssertionError $e) {
225 $this->pass('::set() was called with invalid cache tags, runtime assertion failed.');
230 * Tests Drupal\Core\Cache\CacheBackendInterface::delete().
232 public function testDelete() {
233 $backend = $this->getCacheBackend();
235 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
236 $backend->set('test1', 7);
237 $this->assert(is_object($backend->get('test1')), "Backend returned an object for cache id test1.");
239 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
240 $backend->set('test2', 3);
241 $this->assert(is_object($backend->get('test2')), "Backend returned an object for cache id %cid.");
243 $backend->delete('test1');
244 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1 after deletion.");
246 $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2.");
248 $backend->delete('test2');
249 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2 after deletion.");
251 $long_cid = str_repeat('a', 300);
252 $backend->set($long_cid, 'test');
253 $backend->delete($long_cid);
254 $this->assertIdentical(FALSE, $backend->get($long_cid), "Backend does not contain data for long cache id after deletion.");
258 * Tests data type preservation.
260 public function testValueTypeIsKept() {
261 $backend = $this->getCacheBackend();
269 'test6' => [1, 2, 3],
272 // Create cache entries.
273 foreach ($variables as $cid => $data) {
274 $backend->set($cid, $data);
277 // Retrieve and test cache objects.
278 foreach ($variables as $cid => $value) {
279 $object = $backend->get($cid);
280 $this->assert(is_object($object), sprintf("Backend returned an object for cache id %s.", $cid));
281 $this->assertIdentical($value, $object->data, sprintf("Data of cached id %s kept is identical in type and value", $cid));
286 * Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().
288 public function testGetMultiple() {
289 $backend = $this->getCacheBackend();
291 // Set numerous testing keys.
292 $long_cid = str_repeat('a', 300);
293 $backend->set('test1', 1);
294 $backend->set('test2', 3);
295 $backend->set('test3', 5);
296 $backend->set('test4', 7);
297 $backend->set('test5', 11);
298 $backend->set('test6', 13);
299 $backend->set('test7', 17);
300 $backend->set($long_cid, 300);
302 // Mismatch order for harder testing.
306 // Cid does not exist.
309 // Cid does not exist until added before second getMultiple().
315 $ret = $backend->getMultiple($cids);
316 // Test return - ensure it contains existing cache ids.
317 $this->assert(isset($ret['test2']), "Existing cache id test2 is set.");
318 $this->assert(isset($ret['test3']), "Existing cache id test3 is set.");
319 $this->assert(isset($ret['test6']), "Existing cache id test6 is set.");
320 $this->assert(isset($ret['test7']), "Existing cache id test7 is set.");
321 // Test return - ensure that objects has expected properties.
322 $this->assertTrue($ret['test2']->valid, 'Item is marked as valid.');
323 $this->assertTrue($ret['test2']->created >= REQUEST_TIME && $ret['test2']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
324 $this->assertEqual($ret['test2']->expire, Cache::PERMANENT, 'Expire time is correct.');
325 // Test return - ensure it does not contain nonexistent cache ids.
326 $this->assertFalse(isset($ret['test19']), "Nonexistent cache id test19 is not set.");
327 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set.");
329 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
330 $this->assertIdentical($ret['test3']->data, 5, "Existing cache id test3 has the correct value.");
331 $this->assertIdentical($ret['test6']->data, 13, "Existing cache id test6 has the correct value.");
332 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
333 // Test $cids array - ensure it contains cache id's that do not exist.
334 $this->assert(in_array('test19', $cids), "Nonexistent cache id test19 is in cids array.");
335 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
336 // Test $cids array - ensure it does not contain cache id's that exist.
337 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
338 $this->assertFalse(in_array('test3', $cids), "Existing cache id test3 is not in cids array.");
339 $this->assertFalse(in_array('test6', $cids), "Existing cache id test6 is not in cids array.");
340 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
342 // Test a second time after deleting and setting new keys which ensures that
343 // if the backend uses statics it does not cause unexpected results.
344 $backend->delete('test3');
345 $backend->delete('test6');
346 $backend->set('test19', 57);
349 $ret = $backend->getMultiple($cids);
350 // Test return - ensure it contains existing cache ids.
351 $this->assert(isset($ret['test2']), "Existing cache id test2 is set");
352 $this->assert(isset($ret['test7']), "Existing cache id test7 is set");
353 $this->assert(isset($ret['test19']), "Added cache id test19 is set");
354 // Test return - ensure it does not contain nonexistent cache ids.
355 $this->assertFalse(isset($ret['test3']), "Deleted cache id test3 is not set");
356 $this->assertFalse(isset($ret['test6']), "Deleted cache id test6 is not set");
357 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set");
359 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
360 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
361 $this->assertIdentical($ret['test19']->data, 57, "Added cache id test19 has the correct value.");
362 // Test $cids array - ensure it contains cache id's that do not exist.
363 $this->assert(in_array('test3', $cids), "Deleted cache id test3 is in cids array.");
364 $this->assert(in_array('test6', $cids), "Deleted cache id test6 is in cids array.");
365 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
366 // Test $cids array - ensure it does not contain cache id's that exist.
367 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
368 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
369 $this->assertFalse(in_array('test19', $cids), "Added cache id test19 is not in cids array.");
371 // Test with a long $cid and non-numeric array key.
372 $cids = ['key:key' => $long_cid];
373 $return = $backend->getMultiple($cids);
374 $this->assertEqual(300, $return[$long_cid]->data);
375 $this->assertTrue(empty($cids));
379 * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
381 public function testSetMultiple() {
382 $backend = $this->getCacheBackend();
384 $future_expiration = REQUEST_TIME + 100;
386 // Set multiple testing keys.
387 $backend->set('cid_1', 'Some other value');
389 'cid_1' => ['data' => 1],
390 'cid_2' => ['data' => 2],
391 'cid_3' => ['data' => [1, 2]],
392 'cid_4' => ['data' => 1, 'expire' => $future_expiration],
393 'cid_5' => ['data' => 1, 'tags' => ['test:a', 'test:b']],
395 $backend->setMultiple($items);
396 $cids = array_keys($items);
397 $cached = $backend->getMultiple($cids);
399 $this->assertEqual($cached['cid_1']->data, $items['cid_1']['data'], 'Over-written cache item set correctly.');
400 $this->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.');
401 $this->assertTrue($cached['cid_1']->created >= REQUEST_TIME && $cached['cid_1']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
402 $this->assertEqual($cached['cid_1']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
404 $this->assertEqual($cached['cid_2']->data, $items['cid_2']['data'], 'New cache item set correctly.');
405 $this->assertEqual($cached['cid_2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
407 $this->assertEqual($cached['cid_3']->data, $items['cid_3']['data'], 'New cache item with serialized data set correctly.');
408 $this->assertEqual($cached['cid_3']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
410 $this->assertEqual($cached['cid_4']->data, $items['cid_4']['data'], 'New cache item set correctly.');
411 $this->assertEqual($cached['cid_4']->expire, $future_expiration, 'Cache expiration has been correctly set.');
413 $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
415 // Calling ::setMultiple() with invalid cache tags. This should fail an
419 'exception_test_1' => ['data' => 1, 'tags' => []],
420 'exception_test_2' => ['data' => 2, 'tags' => ['valid']],
421 'exception_test_3' => ['data' => 3, 'tags' => ['node' => [3, 5, 7]]],
423 $backend->setMultiple($items);
424 $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
426 catch (\AssertionError $e) {
427 $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
432 * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
433 * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
435 public function testDeleteMultiple() {
436 $backend = $this->getCacheBackend();
438 // Set numerous testing keys.
439 $backend->set('test1', 1);
440 $backend->set('test2', 3);
441 $backend->set('test3', 5);
442 $backend->set('test4', 7);
443 $backend->set('test5', 11);
444 $backend->set('test6', 13);
445 $backend->set('test7', 17);
447 $backend->delete('test1');
448 // Nonexistent key should not cause an error.
449 $backend->delete('test23');
450 $backend->deleteMultiple([
454 // Nonexistent key should not cause an error.
456 // Nonexistent key should not cause an error.
460 // Test if expected keys have been deleted.
461 $this->assertIdentical(FALSE, $backend->get('test1'), "Cache id test1 deleted.");
462 $this->assertIdentical(FALSE, $backend->get('test3'), "Cache id test3 deleted.");
463 $this->assertIdentical(FALSE, $backend->get('test5'), "Cache id test5 deleted.");
464 $this->assertIdentical(FALSE, $backend->get('test7'), "Cache id test7 deleted.");
466 // Test if expected keys exist.
467 $this->assertNotIdentical(FALSE, $backend->get('test2'), "Cache id test2 exists.");
468 $this->assertNotIdentical(FALSE, $backend->get('test4'), "Cache id test4 exists.");
469 $this->assertNotIdentical(FALSE, $backend->get('test6'), "Cache id test6 exists.");
471 // Test if that expected keys do not exist.
472 $this->assertIdentical(FALSE, $backend->get('test19'), "Cache id test19 does not exist.");
473 $this->assertIdentical(FALSE, $backend->get('test21'), "Cache id test21 does not exist.");
475 // Calling deleteMultiple() with an empty array should not cause an error.
476 $this->assertFalse($backend->deleteMultiple([]));
480 * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
482 public function testDeleteAll() {
483 $backend_a = $this->getCacheBackend();
484 $backend_b = $this->getCacheBackend('bootstrap');
486 // Set both expiring and permanent keys.
487 $backend_a->set('test1', 1, Cache::PERMANENT);
488 $backend_a->set('test2', 3, time() + 1000);
489 $backend_b->set('test3', 4, Cache::PERMANENT);
491 $backend_a->deleteAll();
493 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
494 $this->assertFalse($backend_a->get('test2'), 'Second key has been deleted.');
495 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
499 * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
500 * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
502 public function testInvalidate() {
503 $backend = $this->getCacheBackend();
504 $backend->set('test1', 1);
505 $backend->set('test2', 2);
506 $backend->set('test3', 2);
507 $backend->set('test4', 2);
509 $reference = ['test1', 'test2', 'test3', 'test4'];
512 $ret = $backend->getMultiple($cids);
513 $this->assertEqual(count($ret), 4, 'Four items returned.');
515 $backend->invalidate('test1');
516 $backend->invalidateMultiple(['test2', 'test3']);
519 $ret = $backend->getMultiple($cids);
520 $this->assertEqual(count($ret), 1, 'Only one item element returned.');
523 $ret = $backend->getMultiple($cids, TRUE);
524 $this->assertEqual(count($ret), 4, 'Four items returned.');
526 // Calling invalidateMultiple() with an empty array should not cause an
528 $this->assertFalse($backend->invalidateMultiple([]));
532 * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
534 public function testInvalidateTags() {
535 $backend = $this->getCacheBackend();
537 // Create two cache entries with the same tag and tag value.
538 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
539 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
540 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
542 // Invalidate test_tag of value 1. This should invalidate both entries.
543 Cache::invalidateTags(['test_tag:2']);
544 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after invalidating a cache tag.');
545 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
547 // Create two cache entries with the same tag and an array tag value.
548 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
549 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
550 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
552 // Invalidate test_tag of value 1. This should invalidate both entries.
553 Cache::invalidateTags(['test_tag:1']);
554 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two caches removed after invalidating a cache tag.');
555 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
557 // Create three cache entries with a mix of tags and tag values.
558 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
559 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
560 $backend->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, ['test_tag_foo:3']);
561 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2') && $backend->get('test_cid_invalidate3'), 'Three cached items were created.');
562 Cache::invalidateTags(['test_tag_foo:3']);
563 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Cache items not matching the tag were not invalidated.');
564 $this->assertFalse($backend->get('test_cid_invalidated3'), 'Cached item matching the tag was removed.');
566 // Create cache entry in multiple bins. Two cache entries
567 // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
569 $tags = ['test_tag:1', 'test_tag:2', 'test_tag:3'];
570 $bins = ['path', 'bootstrap', 'page'];
571 foreach ($bins as $bin) {
572 $this->getCacheBackend($bin)->set('test', $this->defaultValue, Cache::PERMANENT, $tags);
573 $this->assertTrue($this->getCacheBackend($bin)->get('test'), 'Cache item was set in bin.');
576 Cache::invalidateTags(['test_tag:2']);
578 // Test that the cache entry has been invalidated in multiple bins.
579 foreach ($bins as $bin) {
580 $this->assertFalse($this->getCacheBackend($bin)->get('test'), 'Tag invalidation affected item in bin.');
582 // Test that the cache entry with a matching tag has been invalidated.
583 $this->assertFalse($this->getCacheBackend($bin)->get('test_cid_invalidate2'), 'Cache items matching tag were invalidated.');
584 // Test that the cache entry with without a matching tag still exists.
585 $this->assertTrue($this->getCacheBackend($bin)->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
589 * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
591 public function testInvalidateAll() {
592 $backend_a = $this->getCacheBackend();
593 $backend_b = $this->getCacheBackend('bootstrap');
595 // Set both expiring and permanent keys.
596 $backend_a->set('test1', 1, Cache::PERMANENT);
597 $backend_a->set('test2', 3, time() + 1000);
598 $backend_b->set('test3', 4, Cache::PERMANENT);
600 $backend_a->invalidateAll();
602 $this->assertFalse($backend_a->get('test1'), 'First key has been invalidated.');
603 $this->assertFalse($backend_a->get('test2'), 'Second key has been invalidated.');
604 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
605 $this->assertTrue($backend_a->get('test1', TRUE), 'First key has not been deleted.');
606 $this->assertTrue($backend_a->get('test2', TRUE), 'Second key has not been deleted.');
610 * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin().
612 public function testRemoveBin() {
613 $backend_a = $this->getCacheBackend();
614 $backend_b = $this->getCacheBackend('bootstrap');
616 // Set both expiring and permanent keys.
617 $backend_a->set('test1', 1, Cache::PERMANENT);
618 $backend_a->set('test2', 3, time() + 1000);
619 $backend_b->set('test3', 4, Cache::PERMANENT);
621 $backend_a->removeBin();
623 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
624 $this->assertFalse($backend_a->get('test2', TRUE), 'Second key has been deleted.');
625 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');