Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / src / Tests / Cache / GenericCacheBackendUnitTestBase.php
1 <?php
2
3 namespace Drupal\system\Tests\Cache;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\simpletest\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  * @deprecated as of Drupal 8.2.x, will be removed before Drupal 9.0.0. Use
20  *    \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase instead.
21  */
22 abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
23
24   /**
25    * Array of objects implementing Drupal\Core\Cache\CacheBackendInterface.
26    *
27    * @var array
28    */
29   protected $cachebackends;
30
31   /**
32    * Cache bin to use for testing.
33    *
34    * @var string
35    */
36   protected $testBin;
37
38   /**
39    * Random value to use in tests.
40    *
41    * @var string
42    */
43   protected $defaultValue;
44
45   /**
46    * Gets the testing bin.
47    *
48    * Override this method if you want to work on a different bin than the
49    * default one.
50    *
51    * @return string
52    *   Bin name.
53    */
54   protected function getTestBin() {
55     if (!isset($this->testBin)) {
56       $this->testBin = 'page';
57     }
58     return $this->testBin;
59   }
60
61   /**
62    * Creates a cache backend to test.
63    *
64    * Override this method to test a CacheBackend.
65    *
66    * @param string $bin
67    *   Bin name to use for this backend instance.
68    *
69    * @return \Drupal\Core\Cache\CacheBackendInterface
70    *   Cache backend to test.
71    */
72   abstract protected function createCacheBackend($bin);
73
74   /**
75    * Allows specific implementation to change the environment before a test run.
76    */
77   public function setUpCacheBackend() {
78   }
79
80   /**
81    * Allows alteration of environment after a test run but before tear down.
82    *
83    * Used before the real tear down because the tear down will change things
84    * such as the database prefix.
85    */
86   public function tearDownCacheBackend() {
87   }
88
89   /**
90    * Gets a backend to test; this will get a shared instance set in the object.
91    *
92    * @return \Drupal\Core\Cache\CacheBackendInterface
93    *   Cache backend to test.
94    */
95   protected function getCacheBackend($bin = NULL) {
96     if (!isset($bin)) {
97       $bin = $this->getTestBin();
98     }
99     if (!isset($this->cachebackends[$bin])) {
100       $this->cachebackends[$bin] = $this->createCacheBackend($bin);
101       // Ensure the backend is empty.
102       $this->cachebackends[$bin]->deleteAll();
103     }
104     return $this->cachebackends[$bin];
105   }
106
107   protected function setUp() {
108     $this->cachebackends = [];
109     $this->defaultValue = $this->randomMachineName(10);
110
111     parent::setUp();
112
113     $this->setUpCacheBackend();
114   }
115
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();
122     }
123     unset($this->cachebackends);
124
125     $this->tearDownCacheBackend();
126
127     parent::tearDown();
128   }
129
130   /**
131    * Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
132    */
133   public function testSetGet() {
134     $backend = $this->getCacheBackend();
135
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.');
146
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.');
155
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.');
163
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.');
173
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.');
183
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);
189
190     // Make sure that a cached object is not affected by changing the original.
191     $data = new \stdClass();
192     $data->value = 1;
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));
208
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);
213
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'));
218
219     // Calling ::set() with invalid cache tags. This should fail an assertion.
220     try {
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.');
223     }
224     catch (\AssertionError $e) {
225       $this->pass('::set() was called with invalid cache tags, runtime assertion failed.');
226     }
227   }
228
229   /**
230    * Tests Drupal\Core\Cache\CacheBackendInterface::delete().
231    */
232   public function testDelete() {
233     $backend = $this->getCacheBackend();
234
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.");
238
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.");
242
243     $backend->delete('test1');
244     $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1 after deletion.");
245
246     $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2.");
247
248     $backend->delete('test2');
249     $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2 after deletion.");
250
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.");
255   }
256
257   /**
258    * Tests data type preservation.
259    */
260   public function testValueTypeIsKept() {
261     $backend = $this->getCacheBackend();
262
263     $variables = [
264       'test1' => 1,
265       'test2' => '0',
266       'test3' => '',
267       'test4' => 12.64,
268       'test5' => FALSE,
269       'test6' => [1, 2, 3],
270     ];
271
272     // Create cache entries.
273     foreach ($variables as $cid => $data) {
274       $backend->set($cid, $data);
275     }
276
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));
282     }
283   }
284
285   /**
286    * Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().
287    */
288   public function testGetMultiple() {
289     $backend = $this->getCacheBackend();
290
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);
301
302     // Mismatch order for harder testing.
303     $reference = [
304       'test3',
305       'test7',
306       // Cid does not exist.
307       'test21',
308       'test6',
309       // Cid does not exist until added before second getMultiple().
310       'test19',
311       'test2',
312     ];
313
314     $cids = $reference;
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.");
328     // Test values.
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.");
341
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);
347
348     $cids = $reference;
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");
358     // Test values.
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.");
370
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));
376   }
377
378   /**
379    * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
380    */
381   public function testSetMultiple() {
382     $backend = $this->getCacheBackend();
383
384     $future_expiration = REQUEST_TIME + 100;
385
386     // Set multiple testing keys.
387     $backend->set('cid_1', 'Some other value');
388     $items = [
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']],
394     ];
395     $backend->setMultiple($items);
396     $cids = array_keys($items);
397     $cached = $backend->getMultiple($cids);
398
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.');
403
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.');
406
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.');
409
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.');
412
413     $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
414
415     // Calling ::setMultiple() with invalid cache tags. This should fail an
416     // assertion.
417     try {
418       $items = [
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]]],
422       ];
423       $backend->setMultiple($items);
424       $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
425     }
426     catch (\AssertionError $e) {
427       $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
428     }
429   }
430
431   /**
432    * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
433    * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
434    */
435   public function testDeleteMultiple() {
436     $backend = $this->getCacheBackend();
437
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);
446
447     $backend->delete('test1');
448     // Nonexistent key should not cause an error.
449     $backend->delete('test23');
450     $backend->deleteMultiple([
451       'test3',
452       'test5',
453       'test7',
454       // Nonexistent key should not cause an error.
455       'test19',
456       // Nonexistent key should not cause an error.
457       'test21',
458     ]);
459
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.");
465
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.");
470
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.");
474
475     // Calling deleteMultiple() with an empty array should not cause an error.
476     $this->assertFalse($backend->deleteMultiple([]));
477   }
478
479   /**
480    * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
481    */
482   public function testDeleteAll() {
483     $backend_a = $this->getCacheBackend();
484     $backend_b = $this->getCacheBackend('bootstrap');
485
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);
490
491     $backend_a->deleteAll();
492
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.');
496   }
497
498   /**
499    * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
500    * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
501    */
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);
508
509     $reference = ['test1', 'test2', 'test3', 'test4'];
510
511     $cids = $reference;
512     $ret = $backend->getMultiple($cids);
513     $this->assertEqual(count($ret), 4, 'Four items returned.');
514
515     $backend->invalidate('test1');
516     $backend->invalidateMultiple(['test2', 'test3']);
517
518     $cids = $reference;
519     $ret = $backend->getMultiple($cids);
520     $this->assertEqual(count($ret), 1, 'Only one item element returned.');
521
522     $cids = $reference;
523     $ret = $backend->getMultiple($cids, TRUE);
524     $this->assertEqual(count($ret), 4, 'Four items returned.');
525
526     // Calling invalidateMultiple() with an empty array should not cause an
527     // error.
528     $this->assertFalse($backend->invalidateMultiple([]));
529   }
530
531   /**
532    * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
533    */
534   public function testInvalidateTags() {
535     $backend = $this->getCacheBackend();
536
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.');
541
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.');
546
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.');
551
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.');
556
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.');
565
566     // Create cache entry in multiple bins. Two cache entries
567     // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
568     // tests.
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.');
574     }
575
576     Cache::invalidateTags(['test_tag:2']);
577
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.');
581     }
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.');
586   }
587
588   /**
589    * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
590    */
591   public function testInvalidateAll() {
592     $backend_a = $this->getCacheBackend();
593     $backend_b = $this->getCacheBackend('bootstrap');
594
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);
599
600     $backend_a->invalidateAll();
601
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.');
607   }
608
609   /**
610    * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin().
611    */
612   public function testRemoveBin() {
613     $backend_a = $this->getCacheBackend();
614     $backend_b = $this->getCacheBackend('bootstrap');
615
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);
620
621     $backend_a->removeBin();
622
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.');
626   }
627
628 }