GenericCacheBackendUnitTestBase.php 30.5 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase.
6 7 8 9
 */

namespace Drupal\system\Tests\Cache;

10
use Drupal\Core\Cache\Cache;
11
use Drupal\Core\Cache\CacheBackendInterface;
12
use Drupal\simpletest\KernelTestBase;
13 14

/**
15 16
 * Tests any cache backend.
 *
17
 * Full generic unit test suite for any cache backend. In order to use it for a
18
 * cache backend implementation, extend this class and override the
19 20 21 22 23
 * createBackendInstace() method to return an object.
 *
 * @see DatabaseBackendUnitTestCase
 *   For a full working implementation.
 */
24
abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

  /**
   * Array of objects implementing Drupal\Core\Cache\CacheBackendInterface.
   *
   * @var array
   */
  protected $cachebackends;

  /**
   * Cache bin to use for testing.
   *
   * @var string
   */
  protected $testBin;

  /**
   * Random value to use in tests.
   *
   * @var string
   */
  protected $defaultValue;

  /**
48
   * Gets the testing bin.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
   *
   * Override this method if you want to work on a different bin than the
   * default one.
   *
   * @return string
   *   Bin name.
   */
  protected function getTestBin() {
    if (!isset($this->testBin)) {
      $this->testBin = 'page';
    }
    return $this->testBin;
  }

  /**
64
   * Creates a cache backend to test.
65 66 67 68 69 70
   *
   * Override this method to test a CacheBackend.
   *
   * @param string $bin
   *   Bin name to use for this backend instance.
   *
71
   * @return \Drupal\Core\Cache\CacheBackendInterface
72 73 74 75 76
   *   Cache backend to test.
   */
  protected abstract function createCacheBackend($bin);

  /**
77
   * Allows specific implementation to change the environment before a test run.
78 79 80 81 82
   */
  public function setUpCacheBackend() {
  }

  /**
83 84 85 86
   * Allows alteration of environment after a test run but before tear down.
   *
   * Used before the real tear down because the tear down will change things
   * such as the database prefix.
87 88 89 90 91
   */
  public function tearDownCacheBackend() {
  }

  /**
92
   * Gets a backend to test; this will get a shared instance set in the object.
93
   *
94
   * @return \Drupal\Core\Cache\CacheBackendInterface
95 96
   *   Cache backend to test.
   */
97
  protected function getCacheBackend($bin = null) {
98 99 100 101 102 103
    if (!isset($bin)) {
      $bin = $this->getTestBin();
    }
    if (!isset($this->cachebackends[$bin])) {
      $this->cachebackends[$bin] = $this->createCacheBackend($bin);
      // Ensure the backend is empty.
104
      $this->cachebackends[$bin]->deleteAll();
105 106 107 108
    }
    return $this->cachebackends[$bin];
  }

109
  protected function setUp() {
110
    $this->cachebackends = array();
111
    $this->defaultValue = $this->randomMachineName(10);
112 113 114 115 116 117

    parent::setUp();

    $this->setUpCacheBackend();
  }

118
  protected function tearDown() {
119
    // Destruct the registered backend, each test will get a fresh instance,
120
    // properly emptying it here ensure that on persistent data backends they
121 122
    // will come up empty the next test.
    foreach ($this->cachebackends as $bin => $cachebackend) {
123
      $this->cachebackends[$bin]->deleteAll();
124 125 126 127 128 129 130 131 132
    }
    unset($this->cachebackends);

    $this->tearDownCacheBackend();

    parent::tearDown();
  }

  /**
133
   * Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
134 135 136 137 138
   */
  public function testSetGet() {
    $backend = $this->getCacheBackend();

    $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
139 140
    $with_backslash = array('foo' => '\Drupal\foo\Bar');
    $backend->set('test1', $with_backslash);
141 142
    $cached = $backend->get('test1');
    $this->assert(is_object($cached), "Backend returned an object for cache id test1.");
143
    $this->assertIdentical($with_backslash, $cached->data);
144
    $this->assertTrue($cached->valid, 'Item is marked as valid.');
145 146
    // We need to round because microtime may be rounded up in the backend.
    $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
147
    $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
148 149

    $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
150
    $backend->set('test2', array('value' => 3), REQUEST_TIME + 3);
151 152
    $cached = $backend->get('test2');
    $this->assert(is_object($cached), "Backend returned an object for cache id test2.");
153 154
    $this->assertIdentical(array('value' => 3), $cached->data);
    $this->assertTrue($cached->valid, 'Item is marked as valid.');
155
    $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
156 157 158 159 160 161 162
    $this->assertEqual($cached->expire, REQUEST_TIME + 3, 'Expire time is correct.');

    $backend->set('test3', 'foobar', REQUEST_TIME - 3);
    $this->assertFalse($backend->get('test3'), 'Invalid item not returned.');
    $cached = $backend->get('test3', TRUE);
    $this->assert(is_object($cached), 'Backend returned an object for cache id test3.');
    $this->assertFalse($cached->valid, 'Item is marked as valid.');
163
    $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
164
    $this->assertEqual($cached->expire, REQUEST_TIME - 3, 'Expire time is correct.');
165 166 167 168 169 170 171 172

    $this->assertIdentical(FALSE, $backend->get('test4'), "Backend does not contain data for cache id test4.");
    $with_eof = array('foo' => "\nEOF\ndata");
    $backend->set('test4', $with_eof);
    $cached = $backend->get('test4');
    $this->assert(is_object($cached), "Backend returned an object for cache id test4.");
    $this->assertIdentical($with_eof, $cached->data);
    $this->assertTrue($cached->valid, 'Item is marked as valid.');
173
    $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
174 175 176 177 178 179 180 181 182
    $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');

    $this->assertIdentical(FALSE, $backend->get('test5'), "Backend does not contain data for cache id test5.");
    $with_eof_and_semicolon = array('foo' => "\nEOF;\ndata");
    $backend->set('test5', $with_eof_and_semicolon);
    $cached = $backend->get('test5');
    $this->assert(is_object($cached), "Backend returned an object for cache id test5.");
    $this->assertIdentical($with_eof_and_semicolon, $cached->data);
    $this->assertTrue($cached->valid, 'Item is marked as valid.');
183
    $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
184
    $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
185 186 187 188 189 190

    $with_variable = array('foo' => '$bar');
    $backend->set('test6', $with_variable);
    $cached = $backend->get('test6');
    $this->assert(is_object($cached), "Backend returned an object for cache id test6.");
    $this->assertIdentical($with_variable, $cached->data);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    // Make sure that a cached object is not affected by changing the original.
    $data = new \stdClass();
    $data->value = 1;
    $data->obj = new \stdClass();
    $data->obj->value = 2;
    $backend->set('test7', $data);
    $expected_data = clone $data;
    // Add a property to the original. It should not appear in the cached data.
    $data->this_should_not_be_in_the_cache = TRUE;
    $cached = $backend->get('test7');
    $this->assert(is_object($cached), "Backend returned an object for cache id test7.");
    $this->assertEqual($expected_data, $cached->data);
    $this->assertFalse(isset($cached->data->this_should_not_be_in_the_cache));
    // Add a property to the cache data. It should not appear when we fetch
    // the data from cache again.
    $cached->data->this_should_not_be_in_the_cache = TRUE;
    $fresh_cached = $backend->get('test7');
    $this->assertFalse(isset($fresh_cached->data->this_should_not_be_in_the_cache));
210 211 212 213 214

    // Check with a long key.
    $cid = str_repeat('a', 300);
    $backend->set($cid, 'test');
    $this->assertEqual('test', $backend->get($cid)->data);
215

216 217 218 219 220
    // Check that the cache key is case sensitive.
    $backend->set('TEST8', 'value');
    $this->assertEqual('value', $backend->get('TEST8')->data);
    $this->assertFalse($backend->get('test8'));

221 222 223 224 225 226 227 228
    // Calling ::set() with invalid cache tags.
    try {
      $backend->set('exception_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]);
      $this->fail('::set() was called with invalid cache tags, no exception was thrown.');
    }
    catch (\LogicException $e) {
      $this->pass('::set() was called with invalid cache tags, an exception was thrown.');
    }
229 230 231
  }

  /**
232
   * Tests Drupal\Core\Cache\CacheBackendInterface::delete().
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
   */
  public function testDelete() {
    $backend = $this->getCacheBackend();

    $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
    $backend->set('test1', 7);
    $this->assert(is_object($backend->get('test1')), "Backend returned an object for cache id test1.");

    $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
    $backend->set('test2', 3);
    $this->assert(is_object($backend->get('test2')), "Backend returned an object for cache id %cid.");

    $backend->delete('test1');
    $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1 after deletion.");

    $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2.");

    $backend->delete('test2');
    $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2 after deletion.");
252 253 254 255 256

    $long_cid = str_repeat('a', 300);
    $backend->set($long_cid, 'test');
    $backend->delete($long_cid);
    $this->assertIdentical(FALSE, $backend->get($long_cid), "Backend does not contain data for long cache id after deletion.");
257 258 259
  }

  /**
260
   * Tests data type preservation.
261 262 263 264 265 266 267 268 269
   */
  public function testValueTypeIsKept() {
    $backend = $this->getCacheBackend();

    $variables = array(
      'test1' => 1,
      'test2' => '0',
      'test3' => '',
      'test4' => 12.64,
270
      'test5' => FALSE,
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
      'test6' => array(1,2,3),
    );

    // Create cache entries.
    foreach ($variables as $cid => $data) {
      $backend->set($cid, $data);
    }

    // Retrieve and test cache objects.
    foreach ($variables as $cid => $value) {
      $object = $backend->get($cid);
      $this->assert(is_object($object), sprintf("Backend returned an object for cache id %s.", $cid));
      $this->assertIdentical($value, $object->data, sprintf("Data of cached id %s kept is identical in type and value", $cid));
    }
  }

  /**
288
   * Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().
289 290 291 292 293
   */
  public function testGetMultiple() {
    $backend = $this->getCacheBackend();

    // Set numerous testing keys.
294
    $long_cid = str_repeat('a', 300);
295 296 297 298 299 300 301
    $backend->set('test1', 1);
    $backend->set('test2', 3);
    $backend->set('test3', 5);
    $backend->set('test4', 7);
    $backend->set('test5', 11);
    $backend->set('test6', 13);
    $backend->set('test7', 17);
302
    $backend->set($long_cid, 300);
303 304 305 306 307 308 309

    // Mismatch order for harder testing.
    $reference = array(
      'test3',
      'test7',
      'test21', // Cid does not exist.
      'test6',
310
      'test19', // Cid does not exist until added before second getMultiple().
311 312 313 314 315 316 317 318 319 320
      'test2',
    );

    $cids = $reference;
    $ret = $backend->getMultiple($cids);
    // Test return - ensure it contains existing cache ids.
    $this->assert(isset($ret['test2']), "Existing cache id test2 is set.");
    $this->assert(isset($ret['test3']), "Existing cache id test3 is set.");
    $this->assert(isset($ret['test6']), "Existing cache id test6 is set.");
    $this->assert(isset($ret['test7']), "Existing cache id test7 is set.");
321 322
    // Test return - ensure that objects has expected properties.
    $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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    // Test return - ensure it does not contain nonexistent cache ids.
    $this->assertFalse(isset($ret['test19']), "Nonexistent cache id test19 is not set.");
    $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set.");
    // Test values.
    $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
    $this->assertIdentical($ret['test3']->data, 5, "Existing cache id test3 has the correct value.");
    $this->assertIdentical($ret['test6']->data, 13, "Existing cache id test6 has the correct value.");
    $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
    // Test $cids array - ensure it contains cache id's that do not exist.
    $this->assert(in_array('test19', $cids), "Nonexistent cache id test19 is in cids array.");
    $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
    // Test $cids array - ensure it does not contain cache id's that exist.
    $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
    $this->assertFalse(in_array('test3', $cids), "Existing cache id test3 is not in cids array.");
    $this->assertFalse(in_array('test6', $cids), "Existing cache id test6 is not in cids array.");
    $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");

    // Test a second time after deleting and setting new keys which ensures that
    // if the backend uses statics it does not cause unexpected results.
    $backend->delete('test3');
    $backend->delete('test6');
    $backend->set('test19', 57);

    $cids = $reference;
    $ret = $backend->getMultiple($cids);
    // Test return - ensure it contains existing cache ids.
    $this->assert(isset($ret['test2']), "Existing cache id test2 is set");
    $this->assert(isset($ret['test7']), "Existing cache id test7 is set");
    $this->assert(isset($ret['test19']), "Added cache id test19 is set");
    // Test return - ensure it does not contain nonexistent cache ids.
    $this->assertFalse(isset($ret['test3']), "Deleted cache id test3 is not set");
    $this->assertFalse(isset($ret['test6']), "Deleted cache id test6 is not set");
    $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set");
    // Test values.
    $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
    $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
    $this->assertIdentical($ret['test19']->data, 57, "Added cache id test19 has the correct value.");
    // Test $cids array - ensure it contains cache id's that do not exist.
    $this->assert(in_array('test3', $cids), "Deleted cache id test3 is in cids array.");
    $this->assert(in_array('test6', $cids), "Deleted cache id test6 is in cids array.");
    $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
    // Test $cids array - ensure it does not contain cache id's that exist.
    $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
    $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
    $this->assertFalse(in_array('test19', $cids), "Added cache id test19 is not in cids array.");
370 371 372 373 374 375

    // Test with a long $cid and non-numeric array key.
    $cids = array('key:key' => $long_cid);
    $return = $backend->getMultiple($cids);
    $this->assertEqual(300, $return[$long_cid]->data);
    $this->assertTrue(empty($cids));
376 377
  }

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  /**
   * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
   */
  public function testSetMultiple() {
    $backend = $this->getCacheBackend();

    $future_expiration = REQUEST_TIME + 100;

    // Set multiple testing keys.
    $backend->set('cid_1', 'Some other value');
    $items = array(
      'cid_1' => array('data' => 1),
      'cid_2' => array('data' => 2),
      'cid_3' => array('data' => array(1, 2)),
      'cid_4' => array('data' => 1, 'expire' => $future_expiration),
393
      'cid_5' => array('data' => 1, 'tags' => array('test:a', 'test:b')),
394 395 396 397 398 399
    );
    $backend->setMultiple($items);
    $cids = array_keys($items);
    $cached = $backend->getMultiple($cids);

    $this->assertEqual($cached['cid_1']->data, $items['cid_1']['data'], 'Over-written cache item set correctly.');
400 401
    $this->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.');
    $this->assertTrue($cached['cid_1']->created >= REQUEST_TIME && $cached['cid_1']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
402 403 404 405 406 407 408 409 410 411 412 413 414
    $this->assertEqual($cached['cid_1']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');

    $this->assertEqual($cached['cid_2']->data, $items['cid_2']['data'], 'New cache item set correctly.');
    $this->assertEqual($cached['cid_2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');

    $this->assertEqual($cached['cid_3']->data, $items['cid_3']['data'], 'New cache item with serialized data set correctly.');
    $this->assertEqual($cached['cid_3']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');

    $this->assertEqual($cached['cid_4']->data, $items['cid_4']['data'], 'New cache item set correctly.');
    $this->assertEqual($cached['cid_4']->expire, $future_expiration, 'Cache expiration has been correctly set.');

    $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
    $this->assertEqual($cached['cid_5']->tags, array('test:a', 'test:b'));
415 416 417 418 419 420 421 422 423 424 425 426 427 428

    // Calling ::setMultiple() with invalid cache tags.
    try {
      $items = [
        'exception_test_1' => array('data' => 1, 'tags' => []),
        'exception_test_2' => array('data' => 2, 'tags' => ['valid']),
        'exception_test_3' => array('data' => 3, 'tags' => ['node' => [3, 5, 7]]),
      ];
      $backend->setMultiple($items);
      $this->fail('::setMultiple() was called with invalid cache tags, no exception was thrown.');
    }
    catch (\LogicException $e) {
      $this->pass('::setMultiple() was called with invalid cache tags, an exception was thrown.');
    }
429 430
  }

431
  /**
432 433
   * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
   * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
434 435 436 437 438 439 440 441 442 443 444 445 446
   */
  public function testDeleteMultiple() {
    $backend = $this->getCacheBackend();

    // Set numerous testing keys.
    $backend->set('test1', 1);
    $backend->set('test2', 3);
    $backend->set('test3', 5);
    $backend->set('test4', 7);
    $backend->set('test5', 11);
    $backend->set('test6', 13);
    $backend->set('test7', 17);

447 448
    $backend->delete('test1');
    $backend->delete('test23'); // Nonexistent key should not cause an error.
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    $backend->deleteMultiple(array(
      'test3',
      'test5',
      'test7',
      'test19', // Nonexistent key should not cause an error.
      'test21', // Nonexistent key should not cause an error.
    ));

    // Test if expected keys have been deleted.
    $this->assertIdentical(FALSE, $backend->get('test1'), "Cache id test1 deleted.");
    $this->assertIdentical(FALSE, $backend->get('test3'), "Cache id test3 deleted.");
    $this->assertIdentical(FALSE, $backend->get('test5'), "Cache id test5 deleted.");
    $this->assertIdentical(FALSE, $backend->get('test7'), "Cache id test7 deleted.");

    // Test if expected keys exist.
    $this->assertNotIdentical(FALSE, $backend->get('test2'), "Cache id test2 exists.");
    $this->assertNotIdentical(FALSE, $backend->get('test4'), "Cache id test4 exists.");
    $this->assertNotIdentical(FALSE, $backend->get('test6'), "Cache id test6 exists.");

    // Test if that expected keys do not exist.
    $this->assertIdentical(FALSE, $backend->get('test19'), "Cache id test19 does not exist.");
    $this->assertIdentical(FALSE, $backend->get('test21'), "Cache id test21 does not exist.");
471 472 473

    // Calling deleteMultiple() with an empty array should not cause an error.
    $this->assertFalse($backend->deleteMultiple(array()));
474 475 476
  }

  /**
477
   * Tests Drupal\Core\Cache\CacheBackendInterface::deleteTags().
478
   */
479 480 481 482
  function testDeleteTags() {
    $backend = $this->getCacheBackend();

    // Create two cache entries with the same tag and tag value.
483 484
    $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, array('test_tag:2'));
    $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array('test_tag:2'));
485 486 487
    $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');

    // Delete test_tag of value 1. This should delete both entries.
488
    $backend->deleteTags(array('test_tag:2'));
489 490 491 492
    $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after deleting a cache tag.');
    $this->assertFalse($backend->get('test_cid_invalidate1', TRUE) || $backend->get('test_cid_invalidate2', TRUE), 'Two cache items deleted after deleting a cache tag.');

    // Create two cache entries with the same tag and an array tag value.
493 494
    $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, array('test_tag:1'));
    $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array('test_tag:1'));
495 496 497
    $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');

    // Delete test_tag of value 1. This should delete both entries.
498
    $backend->deleteTags(array('test_tag:1'));
499 500 501 502
    $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after deleted a cache tag.');
    $this->assertFalse($backend->get('test_cid_invalidate1', TRUE) || $backend->get('test_cid_invalidate2', TRUE), 'Two cache items deleted after deleting a cache tag.');

    // Create three cache entries with a mix of tags and tag values.
503 504 505
    $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, array('test_tag:1'));
    $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array('test_tag:2'));
    $backend->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, array('test_tag_foo:3'));
506
    $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2') && $backend->get('test_cid_invalidate3'), 'Three cached items were created.');
507
    $backend->deleteTags(array('test_tag_foo:3'));
508 509 510 511 512 513
    $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Cached items not matching the tag were not deleted.');
    $this->assertFalse($backend->get('test_cid_invalidated3', TRUE), 'Cache item matching the tag was deleted.');

    // Create cache entry in multiple bins. Two cache entries
    // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
    // tests.
514
    $tags = array('test_tag:1', 'test_tag:2', 'test_tag:3');
515 516
    $bins = array('path', 'bootstrap', 'page');
    foreach ($bins as $bin) {
517
      $this->getCacheBackend($bin)->set('test', $this->defaultValue, Cache::PERMANENT, $tags);
518 519 520 521 522
      $this->assertTrue($this->getCacheBackend($bin)->get('test'), 'Cache item was set in bin.');
    }

    // Delete tag in mulitple bins.
    foreach ($bins as $bin) {
523
      $this->getCacheBackend($bin)->deleteTags(array('test_tag:2'));
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    }

    // Test that cache entry has been deleted in multple bins.
    foreach ($bins as $bin) {
      $this->assertFalse($this->getCacheBackend($bin)->get('test', TRUE), 'Tag deletion affected item in bin.');
    }
    // Test that the cache entry with a matching tag has been invalidated.
    $this->assertFalse($this->getCacheBackend($bin)->get('test_cid_invalidate2', TRUE), 'Cache items matching tag were deleted.');
    // Test that the cache entry with without a matching tag still exists.
    $this->assertTrue($this->getCacheBackend($bin)->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
  }

  /**
   * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
   */
  public function testDeleteAll() {
540
    $backend = $this->getCacheBackend();
541
    $unrelated = $this->getCacheBackend('bootstrap');
542 543

    // Set both expiring and permanent keys.
544
    $backend->set('test1', 1, Cache::PERMANENT);
545
    $backend->set('test2', 3, time() + 1000);
546
    $unrelated->set('test3', 4, Cache::PERMANENT);
547

548
    $backend->deleteAll();
549

550 551
    $this->assertFalse($backend->get('test1'), 'First key has been deleted.');
    $this->assertFalse($backend->get('test2'), 'Second key has been deleted.');
552
    $this->assertTrue($unrelated->get('test3'), 'Item in other bin is preserved.');
553 554 555
  }

  /**
556 557
   * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
   * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
558
   */
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
  function testInvalidate() {
    $backend = $this->getCacheBackend();
    $backend->set('test1', 1);
    $backend->set('test2', 2);
    $backend->set('test3', 2);
    $backend->set('test4', 2);

    $reference = array('test1', 'test2', 'test3', 'test4');

    $cids = $reference;
    $ret = $backend->getMultiple($cids);
    $this->assertEqual(count($ret), 4, 'Four items returned.');

    $backend->invalidate('test1');
    $backend->invalidateMultiple(array('test2', 'test3'));

    $cids = $reference;
    $ret = $backend->getMultiple($cids);
    $this->assertEqual(count($ret), 1, 'Only one item element returned.');

    $cids = $reference;
    $ret = $backend->getMultiple($cids, TRUE);
    $this->assertEqual(count($ret), 4, 'Four items returned.');
582 583 584 585

    // Calling invalidateMultiple() with an empty array should not cause an
    // error.
    $this->assertFalse($backend->invalidateMultiple(array()));
586 587 588
  }

  /**
589
   * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
590
   */
591
  function testInvalidateTags() {
592 593 594
    $backend = $this->getCacheBackend();

    // Create two cache entries with the same tag and tag value.
595 596
    $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, array('test_tag:2'));
    $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array('test_tag:2'));
597 598
    $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');

599
    // Invalidate test_tag of value 1. This should invalidate both entries.
600
    $backend->invalidateTags(array('test_tag:2'));
601 602
    $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after invalidating a cache tag.');
    $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
603 604

    // Create two cache entries with the same tag and an array tag value.
605 606
    $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, array('test_tag:1'));
    $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array('test_tag:1'));
607 608
    $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');

609
    // Invalidate test_tag of value 1. This should invalidate both entries.
610
    $backend->invalidateTags(array('test_tag:1'));
611 612
    $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two caches removed after invalidating a cache tag.');
    $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
613 614

    // Create three cache entries with a mix of tags and tag values.
615 616 617
    $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, array('test_tag:1'));
    $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, array('test_tag:2'));
    $backend->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, array('test_tag_foo:3'));
618
    $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2') && $backend->get('test_cid_invalidate3'), 'Three cached items were created.');
619
    $backend->invalidateTags(array('test_tag_foo:3'));
620 621
    $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Cache items not matching the tag were not invalidated.');
    $this->assertFalse($backend->get('test_cid_invalidated3'), 'Cached item matching the tag was removed.');
622

623 624 625
    // Create cache entry in multiple bins. Two cache entries
    // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
    // tests.
626
    $tags = array('test_tag:1', 'test_tag:2', 'test_tag:3');
627 628
    $bins = array('path', 'bootstrap', 'page');
    foreach ($bins as $bin) {
629
      $this->getCacheBackend($bin)->set('test', $this->defaultValue, Cache::PERMANENT, $tags);
630
      $this->assertTrue($this->getCacheBackend($bin)->get('test'), 'Cache item was set in bin.');
631 632 633 634
    }

    // Invalidate tag in mulitple bins.
    foreach ($bins as $bin) {
635
      $this->getCacheBackend($bin)->invalidateTags(array('test_tag:2'));
636 637 638 639
    }

    // Test that cache entry has been invalidated in multple bins.
    foreach ($bins as $bin) {
640
      $this->assertFalse($this->getCacheBackend($bin)->get('test'), 'Tag invalidation affected item in bin.');
641 642
    }
    // Test that the cache entry with a matching tag has been invalidated.
643
    $this->assertFalse($this->getCacheBackend($bin)->get('test_cid_invalidate2'), 'Cache items matching tag were invalidated.');
644
    // Test that the cache entry with without a matching tag still exists.
645 646 647 648 649 650 651 652
    $this->assertTrue($this->getCacheBackend($bin)->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
  }

  /**
   * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
   */
  public function testInvalidateAll() {
    $backend = $this->getCacheBackend();
653
    $unrelated = $this->getCacheBackend('bootstrap');
654 655

    // Set both expiring and permanent keys.
656
    $backend->set('test1', 1, Cache::PERMANENT);
657
    $backend->set('test2', 3, time() + 1000);
658
    $unrelated->set('test3', 4, Cache::PERMANENT);
659 660 661 662 663

    $backend->invalidateAll();

    $this->assertFalse($backend->get('test1'), 'First key has been invalidated.');
    $this->assertFalse($backend->get('test2'), 'Second key has been invalidated.');
664
    $this->assertTrue($unrelated->get('test3'), 'Item in other bin is preserved.');
665 666
    $this->assertTrue($backend->get('test1', TRUE), 'First key has not been deleted.');
    $this->assertTrue($backend->get('test2', TRUE), 'Second key has not been deleted.');
667
  }
668

669
}