ce5b28c91c174162d06e1a4ad8813b6c1a951db1
[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   abstract protected 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->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.');
143
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.');
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->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.');
170
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.');
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->assertSame($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->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.");
235
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.");
239
240     $backend->delete('test1');
241     $this->assertSame(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->assertSame(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->assertSame(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->assertSame($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       // Cid does not exist.
304       'test21',
305       'test6',
306       // Cid does not exist until added before second getMultiple().
307       'test19',
308       'test2',
309     ];
310
311     $cids = $reference;
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.");
325     // Test values.
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.");
338
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);
344
345     $cids = $reference;
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");
355     // Test values.
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.");
367
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));
373   }
374
375   /**
376    * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
377    */
378   public function testSetMultiple() {
379     $backend = $this->getCacheBackend();
380
381     $future_expiration = REQUEST_TIME + 100;
382
383     // Set multiple testing keys.
384     $backend->set('cid_1', 'Some other value');
385     $items = [
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']],
391     ];
392     $backend->setMultiple($items);
393     $cids = array_keys($items);
394     $cached = $backend->getMultiple($cids);
395
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.');
400
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.');
403
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.');
406
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.');
409
410     $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
411
412     // Calling ::setMultiple() with invalid cache tags. This should fail an
413     // assertion.
414     try {
415       $items = [
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]]],
419       ];
420       $backend->setMultiple($items);
421       $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
422     }
423     catch (\AssertionError $e) {
424       $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
425     }
426   }
427
428   /**
429    * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
430    * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
431    */
432   public function testDeleteMultiple() {
433     $backend = $this->getCacheBackend();
434
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);
443
444     $backend->delete('test1');
445     // Nonexistent key should not cause an error.
446     $backend->delete('test23');
447     $backend->deleteMultiple([
448       'test3',
449       'test5',
450       'test7',
451       // Nonexistent key should not cause an error.
452       'test19',
453       // Nonexistent key should not cause an error.
454       'test21',
455     ]);
456
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.");
462
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.");
467
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.");
471
472     // Calling deleteMultiple() with an empty array should not cause an error.
473     $this->assertFalse($backend->deleteMultiple([]));
474   }
475
476   /**
477    * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
478    */
479   public function testDeleteAll() {
480     $backend_a = $this->getCacheBackend();
481     $backend_b = $this->getCacheBackend('bootstrap');
482
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);
487
488     $backend_a->deleteAll();
489
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.');
493   }
494
495   /**
496    * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
497    * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
498    */
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);
505
506     $reference = ['test1', 'test2', 'test3', 'test4'];
507
508     $cids = $reference;
509     $ret = $backend->getMultiple($cids);
510     $this->assertEqual(count($ret), 4, 'Four items returned.');
511
512     $backend->invalidate('test1');
513     $backend->invalidateMultiple(['test2', 'test3']);
514
515     $cids = $reference;
516     $ret = $backend->getMultiple($cids);
517     $this->assertEqual(count($ret), 1, 'Only one item element returned.');
518
519     $cids = $reference;
520     $ret = $backend->getMultiple($cids, TRUE);
521     $this->assertEqual(count($ret), 4, 'Four items returned.');
522
523     // Calling invalidateMultiple() with an empty array should not cause an
524     // error.
525     $this->assertFalse($backend->invalidateMultiple([]));
526   }
527
528   /**
529    * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
530    */
531   public function testInvalidateTags() {
532     $backend = $this->getCacheBackend();
533
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.');
538
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.');
543
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.');
548
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.');
553
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.');
562
563     // Create cache entry in multiple bins. Two cache entries
564     // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
565     // tests.
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.');
571     }
572
573     Cache::invalidateTags(['test_tag:2']);
574
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.');
578     }
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.');
583   }
584
585   /**
586    * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
587    */
588   public function testInvalidateAll() {
589     $backend_a = $this->getCacheBackend();
590     $backend_b = $this->getCacheBackend('bootstrap');
591
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);
596
597     $backend_a->invalidateAll();
598
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.');
604   }
605
606   /**
607    * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin().
608    */
609   public function testRemoveBin() {
610     $backend_a = $this->getCacheBackend();
611     $backend_b = $this->getCacheBackend('bootstrap');
612
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);
617
618     $backend_a->removeBin();
619
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.');
623   }
624
625 }