Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Cache / GenericCacheBackendUnitTestBase.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Cache;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Tests any cache backend.
11  *
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.
15  *
16  * @see DatabaseBackendUnitTestCase
17  *   For a full working implementation.
18  */
19 abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
20
21   /**
22    * Array of objects implementing Drupal\Core\Cache\CacheBackendInterface.
23    *
24    * @var array
25    */
26   protected $cachebackends;
27
28   /**
29    * Cache bin to use for testing.
30    *
31    * @var string
32    */
33   protected $testBin;
34
35   /**
36    * Random value to use in tests.
37    *
38    * @var string
39    */
40   protected $defaultValue;
41
42   /**
43    * Gets the testing bin.
44    *
45    * Override this method if you want to work on a different bin than the
46    * default one.
47    *
48    * @return string
49    *   Bin name.
50    */
51   protected function getTestBin() {
52     if (!isset($this->testBin)) {
53       $this->testBin = 'page';
54     }
55     return $this->testBin;
56   }
57
58   /**
59    * Creates a cache backend to test.
60    *
61    * Override this method to test a CacheBackend.
62    *
63    * @param string $bin
64    *   Bin name to use for this backend instance.
65    *
66    * @return \Drupal\Core\Cache\CacheBackendInterface
67    *   Cache backend to test.
68    */
69   protected abstract function createCacheBackend($bin);
70
71   /**
72    * Allows specific implementation to change the environment before a test run.
73    */
74   public function setUpCacheBackend() {
75   }
76
77   /**
78    * Allows alteration of environment after a test run but before tear down.
79    *
80    * Used before the real tear down because the tear down will change things
81    * such as the database prefix.
82    */
83   public function tearDownCacheBackend() {
84   }
85
86   /**
87    * Gets a backend to test; this will get a shared instance set in the object.
88    *
89    * @return \Drupal\Core\Cache\CacheBackendInterface
90    *   Cache backend to test.
91    */
92   protected function getCacheBackend($bin = NULL) {
93     if (!isset($bin)) {
94       $bin = $this->getTestBin();
95     }
96     if (!isset($this->cachebackends[$bin])) {
97       $this->cachebackends[$bin] = $this->createCacheBackend($bin);
98       // Ensure the backend is empty.
99       $this->cachebackends[$bin]->deleteAll();
100     }
101     return $this->cachebackends[$bin];
102   }
103
104   protected function setUp() {
105     $this->cachebackends = [];
106     $this->defaultValue = $this->randomMachineName(10);
107
108     parent::setUp();
109
110     $this->setUpCacheBackend();
111   }
112
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();
119     }
120     unset($this->cachebackends);
121
122     $this->tearDownCacheBackend();
123
124     parent::tearDown();
125   }
126
127   /**
128    * Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
129    */
130   public function testSetGet() {
131     $backend = $this->getCacheBackend();
132
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.');
143
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.');
152
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.');
160
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.');
170
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.');
180
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);
186
187     // Make sure that a cached object is not affected by changing the original.
188     $data = new \stdClass();
189     $data->value = 1;
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));
205
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);
210
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'));
215
216     // Calling ::set() with invalid cache tags. This should fail an assertion.
217     try {
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.');
220     }
221     catch (\AssertionError $e) {
222       $this->pass('::set() was called with invalid cache tags, runtime assertion failed.');
223     }
224   }
225
226   /**
227    * Tests Drupal\Core\Cache\CacheBackendInterface::delete().
228    */
229   public function testDelete() {
230     $backend = $this->getCacheBackend();
231
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.");
235
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.");
239
240     $backend->delete('test1');
241     $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1 after deletion.");
242
243     $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2.");
244
245     $backend->delete('test2');
246     $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2 after deletion.");
247
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.");
252   }
253
254   /**
255    * Tests data type preservation.
256    */
257   public function testValueTypeIsKept() {
258     $backend = $this->getCacheBackend();
259
260     $variables = [
261       'test1' => 1,
262       'test2' => '0',
263       'test3' => '',
264       'test4' => 12.64,
265       'test5' => FALSE,
266       'test6' => [1, 2, 3],
267     ];
268
269     // Create cache entries.
270     foreach ($variables as $cid => $data) {
271       $backend->set($cid, $data);
272     }
273
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));
279     }
280   }
281
282   /**
283    * Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().
284    */
285   public function testGetMultiple() {
286     $backend = $this->getCacheBackend();
287
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);
298
299     // Mismatch order for harder testing.
300     $reference = [
301       'test3',
302       'test7',
303       'test21', // Cid does not exist.
304       'test6',
305       'test19', // Cid does not exist until added before second getMultiple().
306       'test2',
307     ];
308
309     $cids = $reference;
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.");
323     // Test values.
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.");
336
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);
342
343     $cids = $reference;
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");
353     // Test values.
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.");
365
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));
371   }
372
373   /**
374    * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
375    */
376   public function testSetMultiple() {
377     $backend = $this->getCacheBackend();
378
379     $future_expiration = REQUEST_TIME + 100;
380
381     // Set multiple testing keys.
382     $backend->set('cid_1', 'Some other value');
383     $items = [
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']],
389     ];
390     $backend->setMultiple($items);
391     $cids = array_keys($items);
392     $cached = $backend->getMultiple($cids);
393
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.');
398
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.');
401
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.');
404
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.');
407
408     $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
409
410     // Calling ::setMultiple() with invalid cache tags. This should fail an
411     // assertion.
412     try {
413       $items = [
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]]],
417       ];
418       $backend->setMultiple($items);
419       $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
420     }
421     catch (\AssertionError $e) {
422       $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
423     }
424   }
425
426   /**
427    * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
428    * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
429    */
430   public function testDeleteMultiple() {
431     $backend = $this->getCacheBackend();
432
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);
441
442     $backend->delete('test1');
443     $backend->delete('test23'); // Nonexistent key should not cause an error.
444     $backend->deleteMultiple([
445       'test3',
446       'test5',
447       'test7',
448       'test19', // Nonexistent key should not cause an error.
449       'test21', // Nonexistent key should not cause an error.
450     ]);
451
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.");
457
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.");
462
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.");
466
467     // Calling deleteMultiple() with an empty array should not cause an error.
468     $this->assertFalse($backend->deleteMultiple([]));
469   }
470
471   /**
472    * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
473    */
474   public function testDeleteAll() {
475     $backend_a = $this->getCacheBackend();
476     $backend_b = $this->getCacheBackend('bootstrap');
477
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);
482
483     $backend_a->deleteAll();
484
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.');
488   }
489
490   /**
491    * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
492    * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
493    */
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);
500
501     $reference = ['test1', 'test2', 'test3', 'test4'];
502
503     $cids = $reference;
504     $ret = $backend->getMultiple($cids);
505     $this->assertEqual(count($ret), 4, 'Four items returned.');
506
507     $backend->invalidate('test1');
508     $backend->invalidateMultiple(['test2', 'test3']);
509
510     $cids = $reference;
511     $ret = $backend->getMultiple($cids);
512     $this->assertEqual(count($ret), 1, 'Only one item element returned.');
513
514     $cids = $reference;
515     $ret = $backend->getMultiple($cids, TRUE);
516     $this->assertEqual(count($ret), 4, 'Four items returned.');
517
518     // Calling invalidateMultiple() with an empty array should not cause an
519     // error.
520     $this->assertFalse($backend->invalidateMultiple([]));
521   }
522
523   /**
524    * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
525    */
526   public function testInvalidateTags() {
527     $backend = $this->getCacheBackend();
528
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.');
533
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.');
538
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.');
543
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.');
548
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.');
557
558     // Create cache entry in multiple bins. Two cache entries
559     // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
560     // tests.
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.');
566     }
567
568     Cache::invalidateTags(['test_tag:2']);
569
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.');
573     }
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.');
578   }
579
580   /**
581    * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
582    */
583   public function testInvalidateAll() {
584     $backend_a = $this->getCacheBackend();
585     $backend_b = $this->getCacheBackend('bootstrap');
586
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);
591
592     $backend_a->invalidateAll();
593
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.');
599   }
600
601   /**
602    * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin().
603    */
604   public function testRemoveBin() {
605     $backend_a = $this->getCacheBackend();
606     $backend_b = $this->getCacheBackend('bootstrap');
607
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);
612
613     $backend_a->removeBin();
614
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.');
618   }
619
620 }