annotate core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\Cache;
Chris@0 4
Chris@18 5 @trigger_error(__NAMESPACE__ . '\GenericCacheBackendUnitTestBase is deprecated for removal before Drupal 9.0.0. Use \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase instead.', E_USER_DEPRECATED);
Chris@18 6
Chris@0 7 use Drupal\Core\Cache\Cache;
Chris@0 8 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 9 use Drupal\simpletest\KernelTestBase;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Tests any cache backend.
Chris@0 13 *
Chris@0 14 * Full generic unit test suite for any cache backend. In order to use it for a
Chris@0 15 * cache backend implementation, extend this class and override the
Chris@0 16 * createBackendInstance() method to return an object.
Chris@0 17 *
Chris@0 18 * @see DatabaseBackendUnitTestCase
Chris@0 19 * For a full working implementation.
Chris@0 20 *
Chris@0 21 * @deprecated as of Drupal 8.2.x, will be removed before Drupal 9.0.0. Use
Chris@0 22 * \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase instead.
Chris@0 23 */
Chris@0 24 abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Array of objects implementing Drupal\Core\Cache\CacheBackendInterface.
Chris@0 28 *
Chris@0 29 * @var array
Chris@0 30 */
Chris@0 31 protected $cachebackends;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Cache bin to use for testing.
Chris@0 35 *
Chris@0 36 * @var string
Chris@0 37 */
Chris@0 38 protected $testBin;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Random value to use in tests.
Chris@0 42 *
Chris@0 43 * @var string
Chris@0 44 */
Chris@0 45 protected $defaultValue;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Gets the testing bin.
Chris@0 49 *
Chris@0 50 * Override this method if you want to work on a different bin than the
Chris@0 51 * default one.
Chris@0 52 *
Chris@0 53 * @return string
Chris@0 54 * Bin name.
Chris@0 55 */
Chris@0 56 protected function getTestBin() {
Chris@0 57 if (!isset($this->testBin)) {
Chris@0 58 $this->testBin = 'page';
Chris@0 59 }
Chris@0 60 return $this->testBin;
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@0 64 * Creates a cache backend to test.
Chris@0 65 *
Chris@0 66 * Override this method to test a CacheBackend.
Chris@0 67 *
Chris@0 68 * @param string $bin
Chris@0 69 * Bin name to use for this backend instance.
Chris@0 70 *
Chris@0 71 * @return \Drupal\Core\Cache\CacheBackendInterface
Chris@0 72 * Cache backend to test.
Chris@0 73 */
Chris@0 74 abstract protected function createCacheBackend($bin);
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Allows specific implementation to change the environment before a test run.
Chris@0 78 */
Chris@0 79 public function setUpCacheBackend() {
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Allows alteration of environment after a test run but before tear down.
Chris@0 84 *
Chris@0 85 * Used before the real tear down because the tear down will change things
Chris@0 86 * such as the database prefix.
Chris@0 87 */
Chris@0 88 public function tearDownCacheBackend() {
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * Gets a backend to test; this will get a shared instance set in the object.
Chris@0 93 *
Chris@0 94 * @return \Drupal\Core\Cache\CacheBackendInterface
Chris@0 95 * Cache backend to test.
Chris@0 96 */
Chris@0 97 protected function getCacheBackend($bin = NULL) {
Chris@0 98 if (!isset($bin)) {
Chris@0 99 $bin = $this->getTestBin();
Chris@0 100 }
Chris@0 101 if (!isset($this->cachebackends[$bin])) {
Chris@0 102 $this->cachebackends[$bin] = $this->createCacheBackend($bin);
Chris@0 103 // Ensure the backend is empty.
Chris@0 104 $this->cachebackends[$bin]->deleteAll();
Chris@0 105 }
Chris@0 106 return $this->cachebackends[$bin];
Chris@0 107 }
Chris@0 108
Chris@0 109 protected function setUp() {
Chris@0 110 $this->cachebackends = [];
Chris@0 111 $this->defaultValue = $this->randomMachineName(10);
Chris@0 112
Chris@0 113 parent::setUp();
Chris@0 114
Chris@0 115 $this->setUpCacheBackend();
Chris@0 116 }
Chris@0 117
Chris@0 118 protected function tearDown() {
Chris@0 119 // Destruct the registered backend, each test will get a fresh instance,
Chris@0 120 // properly emptying it here ensure that on persistent data backends they
Chris@0 121 // will come up empty the next test.
Chris@0 122 foreach ($this->cachebackends as $bin => $cachebackend) {
Chris@0 123 $this->cachebackends[$bin]->deleteAll();
Chris@0 124 }
Chris@0 125 unset($this->cachebackends);
Chris@0 126
Chris@0 127 $this->tearDownCacheBackend();
Chris@0 128
Chris@0 129 parent::tearDown();
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
Chris@0 134 */
Chris@0 135 public function testSetGet() {
Chris@0 136 $backend = $this->getCacheBackend();
Chris@0 137
Chris@0 138 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
Chris@0 139 $with_backslash = ['foo' => '\Drupal\foo\Bar'];
Chris@0 140 $backend->set('test1', $with_backslash);
Chris@0 141 $cached = $backend->get('test1');
Chris@0 142 $this->assert(is_object($cached), "Backend returned an object for cache id test1.");
Chris@0 143 $this->assertIdentical($with_backslash, $cached->data);
Chris@0 144 $this->assertTrue($cached->valid, 'Item is marked as valid.');
Chris@0 145 // We need to round because microtime may be rounded up in the backend.
Chris@0 146 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
Chris@0 147 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
Chris@0 148
Chris@0 149 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
Chris@0 150 $backend->set('test2', ['value' => 3], REQUEST_TIME + 3);
Chris@0 151 $cached = $backend->get('test2');
Chris@0 152 $this->assert(is_object($cached), "Backend returned an object for cache id test2.");
Chris@0 153 $this->assertIdentical(['value' => 3], $cached->data);
Chris@0 154 $this->assertTrue($cached->valid, 'Item is marked as valid.');
Chris@0 155 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
Chris@0 156 $this->assertEqual($cached->expire, REQUEST_TIME + 3, 'Expire time is correct.');
Chris@0 157
Chris@0 158 $backend->set('test3', 'foobar', REQUEST_TIME - 3);
Chris@0 159 $this->assertFalse($backend->get('test3'), 'Invalid item not returned.');
Chris@0 160 $cached = $backend->get('test3', TRUE);
Chris@0 161 $this->assert(is_object($cached), 'Backend returned an object for cache id test3.');
Chris@0 162 $this->assertFalse($cached->valid, 'Item is marked as valid.');
Chris@0 163 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
Chris@0 164 $this->assertEqual($cached->expire, REQUEST_TIME - 3, 'Expire time is correct.');
Chris@0 165
Chris@0 166 $this->assertIdentical(FALSE, $backend->get('test4'), "Backend does not contain data for cache id test4.");
Chris@0 167 $with_eof = ['foo' => "\nEOF\ndata"];
Chris@0 168 $backend->set('test4', $with_eof);
Chris@0 169 $cached = $backend->get('test4');
Chris@0 170 $this->assert(is_object($cached), "Backend returned an object for cache id test4.");
Chris@0 171 $this->assertIdentical($with_eof, $cached->data);
Chris@0 172 $this->assertTrue($cached->valid, 'Item is marked as valid.');
Chris@0 173 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
Chris@0 174 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
Chris@0 175
Chris@0 176 $this->assertIdentical(FALSE, $backend->get('test5'), "Backend does not contain data for cache id test5.");
Chris@0 177 $with_eof_and_semicolon = ['foo' => "\nEOF;\ndata"];
Chris@0 178 $backend->set('test5', $with_eof_and_semicolon);
Chris@0 179 $cached = $backend->get('test5');
Chris@0 180 $this->assert(is_object($cached), "Backend returned an object for cache id test5.");
Chris@0 181 $this->assertIdentical($with_eof_and_semicolon, $cached->data);
Chris@0 182 $this->assertTrue($cached->valid, 'Item is marked as valid.');
Chris@0 183 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
Chris@0 184 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
Chris@0 185
Chris@0 186 $with_variable = ['foo' => '$bar'];
Chris@0 187 $backend->set('test6', $with_variable);
Chris@0 188 $cached = $backend->get('test6');
Chris@0 189 $this->assert(is_object($cached), "Backend returned an object for cache id test6.");
Chris@0 190 $this->assertIdentical($with_variable, $cached->data);
Chris@0 191
Chris@0 192 // Make sure that a cached object is not affected by changing the original.
Chris@0 193 $data = new \stdClass();
Chris@0 194 $data->value = 1;
Chris@0 195 $data->obj = new \stdClass();
Chris@0 196 $data->obj->value = 2;
Chris@0 197 $backend->set('test7', $data);
Chris@0 198 $expected_data = clone $data;
Chris@0 199 // Add a property to the original. It should not appear in the cached data.
Chris@0 200 $data->this_should_not_be_in_the_cache = TRUE;
Chris@0 201 $cached = $backend->get('test7');
Chris@0 202 $this->assert(is_object($cached), "Backend returned an object for cache id test7.");
Chris@0 203 $this->assertEqual($expected_data, $cached->data);
Chris@0 204 $this->assertFalse(isset($cached->data->this_should_not_be_in_the_cache));
Chris@0 205 // Add a property to the cache data. It should not appear when we fetch
Chris@0 206 // the data from cache again.
Chris@0 207 $cached->data->this_should_not_be_in_the_cache = TRUE;
Chris@0 208 $fresh_cached = $backend->get('test7');
Chris@0 209 $this->assertFalse(isset($fresh_cached->data->this_should_not_be_in_the_cache));
Chris@0 210
Chris@0 211 // Check with a long key.
Chris@0 212 $cid = str_repeat('a', 300);
Chris@0 213 $backend->set($cid, 'test');
Chris@0 214 $this->assertEqual('test', $backend->get($cid)->data);
Chris@0 215
Chris@0 216 // Check that the cache key is case sensitive.
Chris@0 217 $backend->set('TEST8', 'value');
Chris@0 218 $this->assertEqual('value', $backend->get('TEST8')->data);
Chris@0 219 $this->assertFalse($backend->get('test8'));
Chris@0 220
Chris@0 221 // Calling ::set() with invalid cache tags. This should fail an assertion.
Chris@0 222 try {
Chris@0 223 $backend->set('assertion_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]);
Chris@0 224 $this->fail('::set() was called with invalid cache tags, runtime assertion did not fail.');
Chris@0 225 }
Chris@0 226 catch (\AssertionError $e) {
Chris@0 227 $this->pass('::set() was called with invalid cache tags, runtime assertion failed.');
Chris@0 228 }
Chris@0 229 }
Chris@0 230
Chris@0 231 /**
Chris@0 232 * Tests Drupal\Core\Cache\CacheBackendInterface::delete().
Chris@0 233 */
Chris@0 234 public function testDelete() {
Chris@0 235 $backend = $this->getCacheBackend();
Chris@0 236
Chris@0 237 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
Chris@0 238 $backend->set('test1', 7);
Chris@0 239 $this->assert(is_object($backend->get('test1')), "Backend returned an object for cache id test1.");
Chris@0 240
Chris@0 241 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
Chris@0 242 $backend->set('test2', 3);
Chris@0 243 $this->assert(is_object($backend->get('test2')), "Backend returned an object for cache id %cid.");
Chris@0 244
Chris@0 245 $backend->delete('test1');
Chris@0 246 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1 after deletion.");
Chris@0 247
Chris@0 248 $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2.");
Chris@0 249
Chris@0 250 $backend->delete('test2');
Chris@0 251 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2 after deletion.");
Chris@0 252
Chris@0 253 $long_cid = str_repeat('a', 300);
Chris@0 254 $backend->set($long_cid, 'test');
Chris@0 255 $backend->delete($long_cid);
Chris@0 256 $this->assertIdentical(FALSE, $backend->get($long_cid), "Backend does not contain data for long cache id after deletion.");
Chris@0 257 }
Chris@0 258
Chris@0 259 /**
Chris@0 260 * Tests data type preservation.
Chris@0 261 */
Chris@0 262 public function testValueTypeIsKept() {
Chris@0 263 $backend = $this->getCacheBackend();
Chris@0 264
Chris@0 265 $variables = [
Chris@0 266 'test1' => 1,
Chris@0 267 'test2' => '0',
Chris@0 268 'test3' => '',
Chris@0 269 'test4' => 12.64,
Chris@0 270 'test5' => FALSE,
Chris@0 271 'test6' => [1, 2, 3],
Chris@0 272 ];
Chris@0 273
Chris@0 274 // Create cache entries.
Chris@0 275 foreach ($variables as $cid => $data) {
Chris@0 276 $backend->set($cid, $data);
Chris@0 277 }
Chris@0 278
Chris@0 279 // Retrieve and test cache objects.
Chris@0 280 foreach ($variables as $cid => $value) {
Chris@0 281 $object = $backend->get($cid);
Chris@0 282 $this->assert(is_object($object), sprintf("Backend returned an object for cache id %s.", $cid));
Chris@0 283 $this->assertIdentical($value, $object->data, sprintf("Data of cached id %s kept is identical in type and value", $cid));
Chris@0 284 }
Chris@0 285 }
Chris@0 286
Chris@0 287 /**
Chris@0 288 * Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().
Chris@0 289 */
Chris@0 290 public function testGetMultiple() {
Chris@0 291 $backend = $this->getCacheBackend();
Chris@0 292
Chris@0 293 // Set numerous testing keys.
Chris@0 294 $long_cid = str_repeat('a', 300);
Chris@0 295 $backend->set('test1', 1);
Chris@0 296 $backend->set('test2', 3);
Chris@0 297 $backend->set('test3', 5);
Chris@0 298 $backend->set('test4', 7);
Chris@0 299 $backend->set('test5', 11);
Chris@0 300 $backend->set('test6', 13);
Chris@0 301 $backend->set('test7', 17);
Chris@0 302 $backend->set($long_cid, 300);
Chris@0 303
Chris@0 304 // Mismatch order for harder testing.
Chris@0 305 $reference = [
Chris@0 306 'test3',
Chris@0 307 'test7',
Chris@0 308 // Cid does not exist.
Chris@0 309 'test21',
Chris@0 310 'test6',
Chris@0 311 // Cid does not exist until added before second getMultiple().
Chris@0 312 'test19',
Chris@0 313 'test2',
Chris@0 314 ];
Chris@0 315
Chris@0 316 $cids = $reference;
Chris@0 317 $ret = $backend->getMultiple($cids);
Chris@0 318 // Test return - ensure it contains existing cache ids.
Chris@0 319 $this->assert(isset($ret['test2']), "Existing cache id test2 is set.");
Chris@0 320 $this->assert(isset($ret['test3']), "Existing cache id test3 is set.");
Chris@0 321 $this->assert(isset($ret['test6']), "Existing cache id test6 is set.");
Chris@0 322 $this->assert(isset($ret['test7']), "Existing cache id test7 is set.");
Chris@0 323 // Test return - ensure that objects has expected properties.
Chris@0 324 $this->assertTrue($ret['test2']->valid, 'Item is marked as valid.');
Chris@0 325 $this->assertTrue($ret['test2']->created >= REQUEST_TIME && $ret['test2']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
Chris@0 326 $this->assertEqual($ret['test2']->expire, Cache::PERMANENT, 'Expire time is correct.');
Chris@0 327 // Test return - ensure it does not contain nonexistent cache ids.
Chris@0 328 $this->assertFalse(isset($ret['test19']), "Nonexistent cache id test19 is not set.");
Chris@0 329 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set.");
Chris@0 330 // Test values.
Chris@0 331 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
Chris@0 332 $this->assertIdentical($ret['test3']->data, 5, "Existing cache id test3 has the correct value.");
Chris@0 333 $this->assertIdentical($ret['test6']->data, 13, "Existing cache id test6 has the correct value.");
Chris@0 334 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
Chris@0 335 // Test $cids array - ensure it contains cache id's that do not exist.
Chris@0 336 $this->assert(in_array('test19', $cids), "Nonexistent cache id test19 is in cids array.");
Chris@0 337 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
Chris@0 338 // Test $cids array - ensure it does not contain cache id's that exist.
Chris@0 339 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
Chris@0 340 $this->assertFalse(in_array('test3', $cids), "Existing cache id test3 is not in cids array.");
Chris@0 341 $this->assertFalse(in_array('test6', $cids), "Existing cache id test6 is not in cids array.");
Chris@0 342 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
Chris@0 343
Chris@0 344 // Test a second time after deleting and setting new keys which ensures that
Chris@0 345 // if the backend uses statics it does not cause unexpected results.
Chris@0 346 $backend->delete('test3');
Chris@0 347 $backend->delete('test6');
Chris@0 348 $backend->set('test19', 57);
Chris@0 349
Chris@0 350 $cids = $reference;
Chris@0 351 $ret = $backend->getMultiple($cids);
Chris@0 352 // Test return - ensure it contains existing cache ids.
Chris@0 353 $this->assert(isset($ret['test2']), "Existing cache id test2 is set");
Chris@0 354 $this->assert(isset($ret['test7']), "Existing cache id test7 is set");
Chris@0 355 $this->assert(isset($ret['test19']), "Added cache id test19 is set");
Chris@0 356 // Test return - ensure it does not contain nonexistent cache ids.
Chris@0 357 $this->assertFalse(isset($ret['test3']), "Deleted cache id test3 is not set");
Chris@0 358 $this->assertFalse(isset($ret['test6']), "Deleted cache id test6 is not set");
Chris@0 359 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set");
Chris@0 360 // Test values.
Chris@0 361 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
Chris@0 362 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
Chris@0 363 $this->assertIdentical($ret['test19']->data, 57, "Added cache id test19 has the correct value.");
Chris@0 364 // Test $cids array - ensure it contains cache id's that do not exist.
Chris@0 365 $this->assert(in_array('test3', $cids), "Deleted cache id test3 is in cids array.");
Chris@0 366 $this->assert(in_array('test6', $cids), "Deleted cache id test6 is in cids array.");
Chris@0 367 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
Chris@0 368 // Test $cids array - ensure it does not contain cache id's that exist.
Chris@0 369 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
Chris@0 370 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
Chris@0 371 $this->assertFalse(in_array('test19', $cids), "Added cache id test19 is not in cids array.");
Chris@0 372
Chris@0 373 // Test with a long $cid and non-numeric array key.
Chris@0 374 $cids = ['key:key' => $long_cid];
Chris@0 375 $return = $backend->getMultiple($cids);
Chris@0 376 $this->assertEqual(300, $return[$long_cid]->data);
Chris@0 377 $this->assertTrue(empty($cids));
Chris@0 378 }
Chris@0 379
Chris@0 380 /**
Chris@0 381 * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
Chris@0 382 */
Chris@0 383 public function testSetMultiple() {
Chris@0 384 $backend = $this->getCacheBackend();
Chris@0 385
Chris@0 386 $future_expiration = REQUEST_TIME + 100;
Chris@0 387
Chris@0 388 // Set multiple testing keys.
Chris@0 389 $backend->set('cid_1', 'Some other value');
Chris@0 390 $items = [
Chris@0 391 'cid_1' => ['data' => 1],
Chris@0 392 'cid_2' => ['data' => 2],
Chris@0 393 'cid_3' => ['data' => [1, 2]],
Chris@0 394 'cid_4' => ['data' => 1, 'expire' => $future_expiration],
Chris@0 395 'cid_5' => ['data' => 1, 'tags' => ['test:a', 'test:b']],
Chris@0 396 ];
Chris@0 397 $backend->setMultiple($items);
Chris@0 398 $cids = array_keys($items);
Chris@0 399 $cached = $backend->getMultiple($cids);
Chris@0 400
Chris@0 401 $this->assertEqual($cached['cid_1']->data, $items['cid_1']['data'], 'Over-written cache item set correctly.');
Chris@0 402 $this->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.');
Chris@0 403 $this->assertTrue($cached['cid_1']->created >= REQUEST_TIME && $cached['cid_1']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
Chris@0 404 $this->assertEqual($cached['cid_1']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
Chris@0 405
Chris@0 406 $this->assertEqual($cached['cid_2']->data, $items['cid_2']['data'], 'New cache item set correctly.');
Chris@0 407 $this->assertEqual($cached['cid_2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
Chris@0 408
Chris@0 409 $this->assertEqual($cached['cid_3']->data, $items['cid_3']['data'], 'New cache item with serialized data set correctly.');
Chris@0 410 $this->assertEqual($cached['cid_3']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
Chris@0 411
Chris@0 412 $this->assertEqual($cached['cid_4']->data, $items['cid_4']['data'], 'New cache item set correctly.');
Chris@0 413 $this->assertEqual($cached['cid_4']->expire, $future_expiration, 'Cache expiration has been correctly set.');
Chris@0 414
Chris@0 415 $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
Chris@0 416
Chris@0 417 // Calling ::setMultiple() with invalid cache tags. This should fail an
Chris@0 418 // assertion.
Chris@0 419 try {
Chris@0 420 $items = [
Chris@0 421 'exception_test_1' => ['data' => 1, 'tags' => []],
Chris@0 422 'exception_test_2' => ['data' => 2, 'tags' => ['valid']],
Chris@0 423 'exception_test_3' => ['data' => 3, 'tags' => ['node' => [3, 5, 7]]],
Chris@0 424 ];
Chris@0 425 $backend->setMultiple($items);
Chris@0 426 $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
Chris@0 427 }
Chris@0 428 catch (\AssertionError $e) {
Chris@0 429 $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
Chris@0 430 }
Chris@0 431 }
Chris@0 432
Chris@0 433 /**
Chris@0 434 * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
Chris@0 435 * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
Chris@0 436 */
Chris@0 437 public function testDeleteMultiple() {
Chris@0 438 $backend = $this->getCacheBackend();
Chris@0 439
Chris@0 440 // Set numerous testing keys.
Chris@0 441 $backend->set('test1', 1);
Chris@0 442 $backend->set('test2', 3);
Chris@0 443 $backend->set('test3', 5);
Chris@0 444 $backend->set('test4', 7);
Chris@0 445 $backend->set('test5', 11);
Chris@0 446 $backend->set('test6', 13);
Chris@0 447 $backend->set('test7', 17);
Chris@0 448
Chris@0 449 $backend->delete('test1');
Chris@0 450 // Nonexistent key should not cause an error.
Chris@0 451 $backend->delete('test23');
Chris@0 452 $backend->deleteMultiple([
Chris@0 453 'test3',
Chris@0 454 'test5',
Chris@0 455 'test7',
Chris@0 456 // Nonexistent key should not cause an error.
Chris@0 457 'test19',
Chris@0 458 // Nonexistent key should not cause an error.
Chris@0 459 'test21',
Chris@0 460 ]);
Chris@0 461
Chris@0 462 // Test if expected keys have been deleted.
Chris@0 463 $this->assertIdentical(FALSE, $backend->get('test1'), "Cache id test1 deleted.");
Chris@0 464 $this->assertIdentical(FALSE, $backend->get('test3'), "Cache id test3 deleted.");
Chris@0 465 $this->assertIdentical(FALSE, $backend->get('test5'), "Cache id test5 deleted.");
Chris@0 466 $this->assertIdentical(FALSE, $backend->get('test7'), "Cache id test7 deleted.");
Chris@0 467
Chris@0 468 // Test if expected keys exist.
Chris@0 469 $this->assertNotIdentical(FALSE, $backend->get('test2'), "Cache id test2 exists.");
Chris@0 470 $this->assertNotIdentical(FALSE, $backend->get('test4'), "Cache id test4 exists.");
Chris@0 471 $this->assertNotIdentical(FALSE, $backend->get('test6'), "Cache id test6 exists.");
Chris@0 472
Chris@0 473 // Test if that expected keys do not exist.
Chris@0 474 $this->assertIdentical(FALSE, $backend->get('test19'), "Cache id test19 does not exist.");
Chris@0 475 $this->assertIdentical(FALSE, $backend->get('test21'), "Cache id test21 does not exist.");
Chris@0 476
Chris@0 477 // Calling deleteMultiple() with an empty array should not cause an error.
Chris@0 478 $this->assertFalse($backend->deleteMultiple([]));
Chris@0 479 }
Chris@0 480
Chris@0 481 /**
Chris@0 482 * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
Chris@0 483 */
Chris@0 484 public function testDeleteAll() {
Chris@0 485 $backend_a = $this->getCacheBackend();
Chris@0 486 $backend_b = $this->getCacheBackend('bootstrap');
Chris@0 487
Chris@0 488 // Set both expiring and permanent keys.
Chris@0 489 $backend_a->set('test1', 1, Cache::PERMANENT);
Chris@0 490 $backend_a->set('test2', 3, time() + 1000);
Chris@0 491 $backend_b->set('test3', 4, Cache::PERMANENT);
Chris@0 492
Chris@0 493 $backend_a->deleteAll();
Chris@0 494
Chris@0 495 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
Chris@0 496 $this->assertFalse($backend_a->get('test2'), 'Second key has been deleted.');
Chris@0 497 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
Chris@0 498 }
Chris@0 499
Chris@0 500 /**
Chris@0 501 * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
Chris@0 502 * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
Chris@0 503 */
Chris@0 504 public function testInvalidate() {
Chris@0 505 $backend = $this->getCacheBackend();
Chris@0 506 $backend->set('test1', 1);
Chris@0 507 $backend->set('test2', 2);
Chris@0 508 $backend->set('test3', 2);
Chris@0 509 $backend->set('test4', 2);
Chris@0 510
Chris@0 511 $reference = ['test1', 'test2', 'test3', 'test4'];
Chris@0 512
Chris@0 513 $cids = $reference;
Chris@0 514 $ret = $backend->getMultiple($cids);
Chris@0 515 $this->assertEqual(count($ret), 4, 'Four items returned.');
Chris@0 516
Chris@0 517 $backend->invalidate('test1');
Chris@0 518 $backend->invalidateMultiple(['test2', 'test3']);
Chris@0 519
Chris@0 520 $cids = $reference;
Chris@0 521 $ret = $backend->getMultiple($cids);
Chris@0 522 $this->assertEqual(count($ret), 1, 'Only one item element returned.');
Chris@0 523
Chris@0 524 $cids = $reference;
Chris@0 525 $ret = $backend->getMultiple($cids, TRUE);
Chris@0 526 $this->assertEqual(count($ret), 4, 'Four items returned.');
Chris@0 527
Chris@0 528 // Calling invalidateMultiple() with an empty array should not cause an
Chris@0 529 // error.
Chris@0 530 $this->assertFalse($backend->invalidateMultiple([]));
Chris@0 531 }
Chris@0 532
Chris@0 533 /**
Chris@0 534 * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
Chris@0 535 */
Chris@0 536 public function testInvalidateTags() {
Chris@0 537 $backend = $this->getCacheBackend();
Chris@0 538
Chris@0 539 // Create two cache entries with the same tag and tag value.
Chris@0 540 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
Chris@0 541 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
Chris@0 542 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
Chris@0 543
Chris@0 544 // Invalidate test_tag of value 1. This should invalidate both entries.
Chris@0 545 Cache::invalidateTags(['test_tag:2']);
Chris@0 546 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after invalidating a cache tag.');
Chris@0 547 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
Chris@0 548
Chris@0 549 // Create two cache entries with the same tag and an array tag value.
Chris@0 550 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
Chris@0 551 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
Chris@0 552 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
Chris@0 553
Chris@0 554 // Invalidate test_tag of value 1. This should invalidate both entries.
Chris@0 555 Cache::invalidateTags(['test_tag:1']);
Chris@0 556 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two caches removed after invalidating a cache tag.');
Chris@0 557 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
Chris@0 558
Chris@0 559 // Create three cache entries with a mix of tags and tag values.
Chris@0 560 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
Chris@0 561 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
Chris@0 562 $backend->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, ['test_tag_foo:3']);
Chris@0 563 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2') && $backend->get('test_cid_invalidate3'), 'Three cached items were created.');
Chris@0 564 Cache::invalidateTags(['test_tag_foo:3']);
Chris@0 565 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Cache items not matching the tag were not invalidated.');
Chris@0 566 $this->assertFalse($backend->get('test_cid_invalidated3'), 'Cached item matching the tag was removed.');
Chris@0 567
Chris@0 568 // Create cache entry in multiple bins. Two cache entries
Chris@0 569 // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
Chris@0 570 // tests.
Chris@0 571 $tags = ['test_tag:1', 'test_tag:2', 'test_tag:3'];
Chris@0 572 $bins = ['path', 'bootstrap', 'page'];
Chris@0 573 foreach ($bins as $bin) {
Chris@0 574 $this->getCacheBackend($bin)->set('test', $this->defaultValue, Cache::PERMANENT, $tags);
Chris@0 575 $this->assertTrue($this->getCacheBackend($bin)->get('test'), 'Cache item was set in bin.');
Chris@0 576 }
Chris@0 577
Chris@0 578 Cache::invalidateTags(['test_tag:2']);
Chris@0 579
Chris@0 580 // Test that the cache entry has been invalidated in multiple bins.
Chris@0 581 foreach ($bins as $bin) {
Chris@0 582 $this->assertFalse($this->getCacheBackend($bin)->get('test'), 'Tag invalidation affected item in bin.');
Chris@0 583 }
Chris@0 584 // Test that the cache entry with a matching tag has been invalidated.
Chris@0 585 $this->assertFalse($this->getCacheBackend($bin)->get('test_cid_invalidate2'), 'Cache items matching tag were invalidated.');
Chris@0 586 // Test that the cache entry with without a matching tag still exists.
Chris@0 587 $this->assertTrue($this->getCacheBackend($bin)->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
Chris@0 588 }
Chris@0 589
Chris@0 590 /**
Chris@0 591 * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
Chris@0 592 */
Chris@0 593 public function testInvalidateAll() {
Chris@0 594 $backend_a = $this->getCacheBackend();
Chris@0 595 $backend_b = $this->getCacheBackend('bootstrap');
Chris@0 596
Chris@0 597 // Set both expiring and permanent keys.
Chris@0 598 $backend_a->set('test1', 1, Cache::PERMANENT);
Chris@0 599 $backend_a->set('test2', 3, time() + 1000);
Chris@0 600 $backend_b->set('test3', 4, Cache::PERMANENT);
Chris@0 601
Chris@0 602 $backend_a->invalidateAll();
Chris@0 603
Chris@0 604 $this->assertFalse($backend_a->get('test1'), 'First key has been invalidated.');
Chris@0 605 $this->assertFalse($backend_a->get('test2'), 'Second key has been invalidated.');
Chris@0 606 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
Chris@0 607 $this->assertTrue($backend_a->get('test1', TRUE), 'First key has not been deleted.');
Chris@0 608 $this->assertTrue($backend_a->get('test2', TRUE), 'Second key has not been deleted.');
Chris@0 609 }
Chris@0 610
Chris@0 611 /**
Chris@0 612 * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin().
Chris@0 613 */
Chris@0 614 public function testRemoveBin() {
Chris@0 615 $backend_a = $this->getCacheBackend();
Chris@0 616 $backend_b = $this->getCacheBackend('bootstrap');
Chris@0 617
Chris@0 618 // Set both expiring and permanent keys.
Chris@0 619 $backend_a->set('test1', 1, Cache::PERMANENT);
Chris@0 620 $backend_a->set('test2', 3, time() + 1000);
Chris@0 621 $backend_b->set('test3', 4, Cache::PERMANENT);
Chris@0 622
Chris@0 623 $backend_a->removeBin();
Chris@0 624
Chris@0 625 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
Chris@0 626 $this->assertFalse($backend_a->get('test2', TRUE), 'Second key has been deleted.');
Chris@0 627 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
Chris@0 628 }
Chris@0 629
Chris@0 630 }