[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * PHPunit tests for the cache API 19 * 20 * This file is part of Moodle's cache API, affectionately called MUC. 21 * It contains the components that are requried in order to use caching. 22 * 23 * @package core 24 * @category cache 25 * @copyright 2012 Sam Hemelryk 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 // Include the necessary evils. 32 global $CFG; 33 require_once($CFG->dirroot.'/cache/locallib.php'); 34 require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php'); 35 36 /** 37 * PHPunit tests for the cache API 38 * 39 * @copyright 2012 Sam Hemelryk 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 */ 42 class core_cache_testcase extends advanced_testcase { 43 44 /** 45 * Set things back to the default before each test. 46 */ 47 public function setUp() { 48 parent::setUp(); 49 cache_factory::reset(); 50 cache_config_testing::create_default_configuration(); 51 } 52 53 /** 54 * Final task is to reset the cache system 55 */ 56 public static function tearDownAfterClass() { 57 parent::tearDownAfterClass(); 58 cache_factory::reset(); 59 } 60 61 /** 62 * Returns the expected application cache store. 63 * @return string 64 */ 65 protected function get_expected_application_cache_store() { 66 global $CFG; 67 $expected = 'cachestore_file'; 68 69 // Verify if we are using any of the available ways to use a different application store within tests. 70 if (defined('TEST_CACHE_USING_APPLICATION_STORE') && preg_match('#[a-zA-Z][a-zA-Z0-9_]*#', TEST_CACHE_USING_APPLICATION_STORE)) { 71 // 1st way. Using some of the testing servers. 72 $expected = 'cachestore_'.(string)TEST_CACHE_USING_APPLICATION_STORE; 73 74 } else if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH && !empty($CFG->altcacheconfigpath)) { 75 // 2nd way. Using an alternative configuration. 76 $defaultstores = cache_helper::get_stores_suitable_for_mode_default(); 77 $instance = cache_config::instance(); 78 // Iterate over defined mode mappings until we get an application one not being the default. 79 foreach ($instance->get_mode_mappings() as $mapping) { 80 // If the store is not for application mode, ignore. 81 if ($mapping['mode'] !== cache_store::MODE_APPLICATION) { 82 continue; 83 } 84 // If the store matches some default mapping store name, ignore. 85 if (array_key_exists($mapping['store'], $defaultstores) && !empty($defaultstores[$mapping['store']]['default'])) { 86 continue; 87 } 88 // Arrived here, have found an application mode store not being the default mapped one (file), 89 // that's the one we are using in the configuration for sure. 90 $expected = 'cachestore_'.$mapping['store']; 91 } 92 } 93 94 return $expected; 95 } 96 97 /** 98 * Tests cache configuration 99 */ 100 public function test_cache_config() { 101 global $CFG; 102 103 if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH && 104 !empty($CFG->altcacheconfigpath)) { 105 // We need to skip this test - it checks the default config structure, but very likely we arn't using the 106 // default config structure here so theres no point in running the test. 107 $this->markTestSkipped('Skipped testing default cache config structure as alt cache path is being used.'); 108 } 109 110 if (defined('TEST_CACHE_USING_APPLICATION_STORE')) { 111 // We need to skip this test - it checks the default config structure, but very likely we arn't using the 112 // default config structure here because we are testing against an alternative application store. 113 $this->markTestSkipped('Skipped testing default cache config structure as alt application store is being used.'); 114 } 115 116 $instance = cache_config::instance(); 117 $this->assertInstanceOf('cache_config_testing', $instance); 118 119 $this->assertTrue(cache_config_testing::config_file_exists()); 120 121 $stores = $instance->get_all_stores(); 122 $this->assertCount(3, $stores); 123 foreach ($stores as $name => $store) { 124 // Check its an array. 125 $this->assertInternalType('array', $store); 126 // Check the name is the key. 127 $this->assertEquals($name, $store['name']); 128 // Check that it has been declared default. 129 $this->assertTrue($store['default']); 130 // Required attributes = name + plugin + configuration + modes + features. 131 $this->assertArrayHasKey('name', $store); 132 $this->assertArrayHasKey('plugin', $store); 133 $this->assertArrayHasKey('configuration', $store); 134 $this->assertArrayHasKey('modes', $store); 135 $this->assertArrayHasKey('features', $store); 136 } 137 138 $modemappings = $instance->get_mode_mappings(); 139 $this->assertCount(3, $modemappings); 140 $modes = array( 141 cache_store::MODE_APPLICATION => false, 142 cache_store::MODE_SESSION => false, 143 cache_store::MODE_REQUEST => false, 144 ); 145 foreach ($modemappings as $mapping) { 146 // We expect 3 properties. 147 $this->assertCount(3, $mapping); 148 // Required attributes = mode + store. 149 $this->assertArrayHasKey('mode', $mapping); 150 $this->assertArrayHasKey('store', $mapping); 151 // Record the mode. 152 $modes[$mapping['mode']] = true; 153 } 154 155 // Must have the default 3 modes and no more. 156 $this->assertCount(3, $mapping); 157 foreach ($modes as $mode) { 158 $this->assertTrue($mode); 159 } 160 161 $definitions = $instance->get_definitions(); 162 // The event invalidation definition is required for the cache API and must be there. 163 $this->assertArrayHasKey('core/eventinvalidation', $definitions); 164 165 $definitionmappings = $instance->get_definition_mappings(); 166 foreach ($definitionmappings as $mapping) { 167 // Required attributes = definition + store. 168 $this->assertArrayHasKey('definition', $mapping); 169 $this->assertArrayHasKey('store', $mapping); 170 } 171 } 172 173 /** 174 * Tests for cache keys that would break on windows. 175 */ 176 public function test_windows_nasty_keys() { 177 $instance = cache_config_testing::instance(); 178 $instance->phpunit_add_definition('phpunit/windowskeytest', array( 179 'mode' => cache_store::MODE_APPLICATION, 180 'component' => 'phpunit', 181 'area' => 'windowskeytest', 182 'simplekeys' => true, 183 'simpledata' => true 184 )); 185 $cache = cache::make('phpunit', 'windowskeytest'); 186 $this->assertTrue($cache->set('contest', 'test data 1')); 187 $this->assertEquals('test data 1', $cache->get('contest')); 188 } 189 190 /** 191 * Tests the default application cache 192 */ 193 public function test_default_application_cache() { 194 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest'); 195 $this->assertInstanceOf('cache_application', $cache); 196 $this->run_on_cache($cache); 197 198 $instance = cache_config_testing::instance(true); 199 $instance->phpunit_add_definition('phpunit/test_default_application_cache', array( 200 'mode' => cache_store::MODE_APPLICATION, 201 'component' => 'phpunit', 202 'area' => 'test_default_application_cache', 203 'staticacceleration' => true, 204 'staticaccelerationsize' => 1 205 )); 206 $cache = cache::make('phpunit', 'test_default_application_cache'); 207 $this->assertInstanceOf('cache_application', $cache); 208 $this->run_on_cache($cache); 209 } 210 211 /** 212 * Tests the default session cache 213 */ 214 public function test_default_session_cache() { 215 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest'); 216 $this->assertInstanceOf('cache_session', $cache); 217 $this->run_on_cache($cache); 218 } 219 220 /** 221 * Tests the default request cache 222 */ 223 public function test_default_request_cache() { 224 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest'); 225 $this->assertInstanceOf('cache_request', $cache); 226 $this->run_on_cache($cache); 227 } 228 229 /** 230 * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this). 231 */ 232 public function test_on_cache_without_store() { 233 $instance = cache_config_testing::instance(true); 234 $instance->phpunit_add_definition('phpunit/nostoretest1', array( 235 'mode' => cache_store::MODE_APPLICATION, 236 'component' => 'phpunit', 237 'area' => 'nostoretest1', 238 )); 239 $instance->phpunit_add_definition('phpunit/nostoretest2', array( 240 'mode' => cache_store::MODE_APPLICATION, 241 'component' => 'phpunit', 242 'area' => 'nostoretest2', 243 'staticacceleration' => true 244 )); 245 $instance->phpunit_remove_stores(); 246 247 $cache = cache::make('phpunit', 'nostoretest1'); 248 $this->run_on_cache($cache); 249 250 $cache = cache::make('phpunit', 'nostoretest2'); 251 $this->run_on_cache($cache); 252 } 253 254 /** 255 * Runs a standard series of access and use tests on a cache instance. 256 * 257 * This function is great because we can use it to ensure all of the loaders perform exactly the same way. 258 * 259 * @param cache_loader $cache 260 */ 261 protected function run_on_cache(cache_loader $cache) { 262 $key = 'contestkey'; 263 $datascalars = array('test data', null); 264 $dataarray = array('contest' => 'data', 'part' => 'two'); 265 $dataobject = (object)$dataarray; 266 267 foreach ($datascalars as $datascalar) { 268 $this->assertTrue($cache->purge()); 269 270 // Check all read methods. 271 $this->assertFalse($cache->get($key)); 272 $this->assertFalse($cache->has($key)); 273 $result = $cache->get_many(array($key)); 274 $this->assertCount(1, $result); 275 $this->assertFalse(reset($result)); 276 $this->assertFalse($cache->has_any(array($key))); 277 $this->assertFalse($cache->has_all(array($key))); 278 279 // Set the data. 280 $this->assertTrue($cache->set($key, $datascalar)); 281 // Setting it more than once should be permitted. 282 $this->assertTrue($cache->set($key, $datascalar)); 283 284 // Recheck the read methods. 285 $this->assertEquals($datascalar, $cache->get($key)); 286 $this->assertTrue($cache->has($key)); 287 $result = $cache->get_many(array($key)); 288 $this->assertCount(1, $result); 289 $this->assertEquals($datascalar, reset($result)); 290 $this->assertTrue($cache->has_any(array($key))); 291 $this->assertTrue($cache->has_all(array($key))); 292 293 // Delete it. 294 $this->assertTrue($cache->delete($key)); 295 296 // Check its gone. 297 $this->assertFalse($cache->get($key)); 298 $this->assertFalse($cache->has($key)); 299 } 300 301 // Test arrays. 302 $this->assertTrue($cache->set($key, $dataarray)); 303 $this->assertEquals($dataarray, $cache->get($key)); 304 305 // Test objects. 306 $this->assertTrue($cache->set($key, $dataobject)); 307 $this->assertEquals($dataobject, $cache->get($key)); 308 309 $specobject = new cache_phpunit_dummy_object('red', 'blue'); 310 $this->assertTrue($cache->set($key, $specobject)); 311 $result = $cache->get($key); 312 $this->assertInstanceOf('cache_phpunit_dummy_object', $result); 313 $this->assertEquals('red_ptc_wfc', $result->property1); 314 $this->assertEquals('blue_ptc_wfc', $result->property2); 315 316 // Test array of objects. 317 $specobject = new cache_phpunit_dummy_object('red', 'blue'); 318 $data = new cacheable_object_array(array( 319 clone($specobject), 320 clone($specobject), 321 clone($specobject)) 322 ); 323 $this->assertTrue($cache->set($key, $data)); 324 $result = $cache->get($key); 325 $this->assertInstanceOf('cacheable_object_array', $result); 326 $this->assertCount(3, $data); 327 foreach ($result as $item) { 328 $this->assertInstanceOf('cache_phpunit_dummy_object', $item); 329 $this->assertEquals('red_ptc_wfc', $item->property1); 330 $this->assertEquals('blue_ptc_wfc', $item->property2); 331 } 332 333 // Test set many. 334 $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null)); 335 $this->assertEquals('data1', $cache->get('key1')); 336 $this->assertEquals('data2', $cache->get('key2')); 337 $this->assertEquals(null, $cache->get('key3')); 338 $this->assertTrue($cache->delete('key1')); 339 $this->assertTrue($cache->delete('key2')); 340 $this->assertTrue($cache->delete('key3')); 341 342 $cache->set_many(array( 343 'key1' => array(1, 2, 3), 344 'key2' => array(3, 2, 1), 345 )); 346 $this->assertInternalType('array', $cache->get('key1')); 347 $this->assertInternalType('array', $cache->get('key2')); 348 $this->assertCount(3, $cache->get('key1')); 349 $this->assertCount(3, $cache->get('key2')); 350 $this->assertInternalType('array', $cache->get_many(array('key1', 'key2'))); 351 $this->assertCount(2, $cache->get_many(array('key1', 'key2'))); 352 $this->assertEquals(2, $cache->delete_many(array('key1', 'key2'))); 353 354 // Test delete many. 355 $this->assertTrue($cache->set('key1', 'data1')); 356 $this->assertTrue($cache->set('key2', 'data2')); 357 $this->assertTrue($cache->set('key3', null)); 358 359 $this->assertEquals('data1', $cache->get('key1')); 360 $this->assertEquals('data2', $cache->get('key2')); 361 $this->assertEquals(null, $cache->get('key3')); 362 363 $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3'))); 364 365 $this->assertFalse($cache->get('key1')); 366 $this->assertFalse($cache->get('key2')); 367 $this->assertFalse($cache->get('key3')); 368 369 // Quick reference test. 370 $obj = new stdClass; 371 $obj->key = 'value'; 372 $ref =& $obj; 373 $this->assertTrue($cache->set('obj', $obj)); 374 375 $obj->key = 'eulav'; 376 $var = $cache->get('obj'); 377 $this->assertInstanceOf('stdClass', $var); 378 $this->assertEquals('value', $var->key); 379 380 $ref->key = 'eulav'; 381 $var = $cache->get('obj'); 382 $this->assertInstanceOf('stdClass', $var); 383 $this->assertEquals('value', $var->key); 384 385 $this->assertTrue($cache->delete('obj')); 386 387 // Deep reference test. 388 $obj1 = new stdClass; 389 $obj1->key = 'value'; 390 $obj2 = new stdClass; 391 $obj2->key = 'test'; 392 $obj3 = new stdClass; 393 $obj3->key = 'pork'; 394 $obj1->subobj =& $obj2; 395 $obj2->subobj =& $obj3; 396 $this->assertTrue($cache->set('obj', $obj1)); 397 398 $obj1->key = 'eulav'; 399 $obj2->key = 'tset'; 400 $obj3->key = 'krop'; 401 $var = $cache->get('obj'); 402 $this->assertInstanceOf('stdClass', $var); 403 $this->assertEquals('value', $var->key); 404 $this->assertInstanceOf('stdClass', $var->subobj); 405 $this->assertEquals('test', $var->subobj->key); 406 $this->assertInstanceOf('stdClass', $var->subobj->subobj); 407 $this->assertEquals('pork', $var->subobj->subobj->key); 408 $this->assertTrue($cache->delete('obj')); 409 410 // Death reference test... basically we don't want this to die. 411 $obj = new stdClass; 412 $obj->key = 'value'; 413 $obj->self =& $obj; 414 $this->assertTrue($cache->set('obj', $obj)); 415 $var = $cache->get('obj'); 416 $this->assertInstanceOf('stdClass', $var); 417 $this->assertEquals('value', $var->key); 418 419 // Reference test after retrieve. 420 $obj = new stdClass; 421 $obj->key = 'value'; 422 $this->assertTrue($cache->set('obj', $obj)); 423 424 $var1 = $cache->get('obj'); 425 $this->assertInstanceOf('stdClass', $var1); 426 $this->assertEquals('value', $var1->key); 427 $var1->key = 'eulav'; 428 $this->assertEquals('eulav', $var1->key); 429 430 $var2 = $cache->get('obj'); 431 $this->assertInstanceOf('stdClass', $var2); 432 $this->assertEquals('value', $var2->key); 433 434 $this->assertTrue($cache->delete('obj')); 435 436 // Death reference test on get_many... basically we don't want this to die. 437 $obj = new stdClass; 438 $obj->key = 'value'; 439 $obj->self =& $obj; 440 $this->assertEquals(1, $cache->set_many(array('obj' => $obj))); 441 $var = $cache->get_many(array('obj')); 442 $this->assertInstanceOf('stdClass', $var['obj']); 443 $this->assertEquals('value', $var['obj']->key); 444 445 // Reference test after retrieve. 446 $obj = new stdClass; 447 $obj->key = 'value'; 448 $this->assertEquals(1, $cache->set_many(array('obj' => $obj))); 449 450 $var1 = $cache->get_many(array('obj')); 451 $this->assertInstanceOf('stdClass', $var1['obj']); 452 $this->assertEquals('value', $var1['obj']->key); 453 $var1['obj']->key = 'eulav'; 454 $this->assertEquals('eulav', $var1['obj']->key); 455 456 $var2 = $cache->get_many(array('obj')); 457 $this->assertInstanceOf('stdClass', $var2['obj']); 458 $this->assertEquals('value', $var2['obj']->key); 459 460 $this->assertTrue($cache->delete('obj')); 461 462 // Test strictness exceptions. 463 try { 464 $cache->get('exception', MUST_EXIST); 465 $this->fail('Exception expected from cache::get using MUST_EXIST'); 466 } catch (Exception $e) { 467 $this->assertTrue(true); 468 } 469 try { 470 $cache->get_many(array('exception1', 'exception2'), MUST_EXIST); 471 $this->fail('Exception expected from cache::get_many using MUST_EXIST'); 472 } catch (Exception $e) { 473 $this->assertTrue(true); 474 } 475 $cache->set('test', 'test'); 476 try { 477 $cache->get_many(array('test', 'exception'), MUST_EXIST); 478 $this->fail('Exception expected from cache::get_many using MUST_EXIST'); 479 } catch (Exception $e) { 480 $this->assertTrue(true); 481 } 482 } 483 484 /** 485 * Tests a definition using a data loader 486 */ 487 public function test_definition_data_loader() { 488 $instance = cache_config_testing::instance(true); 489 $instance->phpunit_add_definition('phpunit/datasourcetest', array( 490 'mode' => cache_store::MODE_APPLICATION, 491 'component' => 'phpunit', 492 'area' => 'datasourcetest', 493 'datasource' => 'cache_phpunit_dummy_datasource', 494 'datasourcefile' => 'cache/tests/fixtures/lib.php' 495 )); 496 497 $cache = cache::make('phpunit', 'datasourcetest'); 498 $this->assertInstanceOf('cache_application', $cache); 499 500 // Purge it to be sure. 501 $this->assertTrue($cache->purge()); 502 // It won't be there yet. 503 $this->assertFalse($cache->has('Test')); 504 // It should load it ;). 505 $this->assertTrue($cache->has('Test', true)); 506 507 // Purge it to be sure. 508 $this->assertTrue($cache->purge()); 509 $this->assertEquals('Test has no value really.', $cache->get('Test')); 510 511 // Test multiple values. 512 $this->assertTrue($cache->purge()); 513 $this->assertTrue($cache->set('b', 'B')); 514 $result = $cache->get_many(array('a', 'b', 'c')); 515 $this->assertInternalType('array', $result); 516 $this->assertCount(3, $result); 517 $this->assertArrayHasKey('a', $result); 518 $this->assertArrayHasKey('b', $result); 519 $this->assertArrayHasKey('c', $result); 520 $this->assertEquals('a has no value really.', $result['a']); 521 $this->assertEquals('B', $result['b']); 522 $this->assertEquals('c has no value really.', $result['c']); 523 } 524 525 /** 526 * Tests a definition using an overridden loader 527 */ 528 public function test_definition_overridden_loader() { 529 $instance = cache_config_testing::instance(true); 530 $instance->phpunit_add_definition('phpunit/overridetest', array( 531 'mode' => cache_store::MODE_APPLICATION, 532 'component' => 'phpunit', 533 'area' => 'overridetest', 534 'overrideclass' => 'cache_phpunit_dummy_overrideclass', 535 'overrideclassfile' => 'cache/tests/fixtures/lib.php' 536 )); 537 $cache = cache::make('phpunit', 'overridetest'); 538 $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache); 539 $this->assertInstanceOf('cache_application', $cache); 540 // Purge it to be sure. 541 $this->assertTrue($cache->purge()); 542 // It won't be there yet. 543 $this->assertFalse($cache->has('Test')); 544 // Add it. 545 $this->assertTrue($cache->set('Test', 'Test has no value really.')); 546 // Check its there. 547 $this->assertEquals('Test has no value really.', $cache->get('Test')); 548 } 549 550 /** 551 * Test the mappingsonly setting. 552 */ 553 public function test_definition_mappings_only() { 554 /** @var cache_config_testing $instance */ 555 $instance = cache_config_testing::instance(true); 556 $instance->phpunit_add_definition('phpunit/mappingsonly', array( 557 'mode' => cache_store::MODE_APPLICATION, 558 'component' => 'phpunit', 559 'area' => 'mappingsonly', 560 'mappingsonly' => true 561 ), false); 562 $instance->phpunit_add_definition('phpunit/nonmappingsonly', array( 563 'mode' => cache_store::MODE_APPLICATION, 564 'component' => 'phpunit', 565 'area' => 'nonmappingsonly', 566 'mappingsonly' => false 567 ), false); 568 569 $cacheonly = cache::make('phpunit', 'mappingsonly'); 570 $this->assertInstanceOf('cache_application', $cacheonly); 571 $this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class()); 572 573 $expected = $this->get_expected_application_cache_store(); 574 $cachenon = cache::make('phpunit', 'nonmappingsonly'); 575 $this->assertInstanceOf('cache_application', $cachenon); 576 $this->assertEquals($expected, $cachenon->phpunit_get_store_class()); 577 } 578 579 /** 580 * Test a very basic definition. 581 */ 582 public function test_definition() { 583 $instance = cache_config_testing::instance(); 584 $instance->phpunit_add_definition('phpunit/test', array( 585 'mode' => cache_store::MODE_APPLICATION, 586 'component' => 'phpunit', 587 'area' => 'test', 588 )); 589 $cache = cache::make('phpunit', 'test'); 590 591 $this->assertTrue($cache->set('testkey1', 'test data 1')); 592 $this->assertEquals('test data 1', $cache->get('testkey1')); 593 $this->assertTrue($cache->set('testkey2', 'test data 2')); 594 $this->assertEquals('test data 2', $cache->get('testkey2')); 595 } 596 597 /** 598 * Test a definition using the simple keys. 599 */ 600 public function test_definition_simplekeys() { 601 $instance = cache_config_testing::instance(); 602 $instance->phpunit_add_definition('phpunit/simplekeytest', array( 603 'mode' => cache_store::MODE_APPLICATION, 604 'component' => 'phpunit', 605 'area' => 'simplekeytest', 606 'simplekeys' => true 607 )); 608 $cache = cache::make('phpunit', 'simplekeytest'); 609 610 $this->assertTrue($cache->set('testkey1', 'test data 1')); 611 $this->assertEquals('test data 1', $cache->get('testkey1')); 612 $this->assertTrue($cache->set('testkey2', 'test data 2')); 613 $this->assertEquals('test data 2', $cache->get('testkey2')); 614 615 $cache->purge(); 616 617 $this->assertTrue($cache->set('1', 'test data 1')); 618 $this->assertEquals('test data 1', $cache->get('1')); 619 $this->assertTrue($cache->set('2', 'test data 2')); 620 $this->assertEquals('test data 2', $cache->get('2')); 621 } 622 623 /** 624 * Test a negative TTL on an application cache. 625 */ 626 public function test_application_ttl_negative() { 627 $instance = cache_config_testing::instance(true); 628 $instance->phpunit_add_definition('phpunit/ttltest', array( 629 'mode' => cache_store::MODE_APPLICATION, 630 'component' => 'phpunit', 631 'area' => 'ttltest', 632 'ttl' => -86400 // Set to a day in the past to be extra sure. 633 )); 634 $cache = cache::make('phpunit', 'ttltest'); 635 $this->assertInstanceOf('cache_application', $cache); 636 637 // Purge it to be sure. 638 $this->assertTrue($cache->purge()); 639 // It won't be there yet. 640 $this->assertFalse($cache->has('Test')); 641 // Set it now. 642 $this->assertTrue($cache->set('Test', 'Test')); 643 // Check its not there. 644 $this->assertFalse($cache->has('Test')); 645 // Double check by trying to get it. 646 $this->assertFalse($cache->get('Test')); 647 648 // Test with multiple keys. 649 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C'))); 650 $result = $cache->get_many(array('a', 'b', 'c')); 651 $this->assertInternalType('array', $result); 652 $this->assertCount(3, $result); 653 $this->assertArrayHasKey('a', $result); 654 $this->assertArrayHasKey('b', $result); 655 $this->assertArrayHasKey('c', $result); 656 $this->assertFalse($result['a']); 657 $this->assertFalse($result['b']); 658 $this->assertFalse($result['c']); 659 660 // Test with multiple keys including missing ones. 661 $result = $cache->get_many(array('a', 'c', 'e')); 662 $this->assertInternalType('array', $result); 663 $this->assertCount(3, $result); 664 $this->assertArrayHasKey('a', $result); 665 $this->assertArrayHasKey('c', $result); 666 $this->assertArrayHasKey('e', $result); 667 $this->assertFalse($result['a']); 668 $this->assertFalse($result['c']); 669 $this->assertFalse($result['e']); 670 } 671 672 /** 673 * Test a positive TTL on an application cache. 674 */ 675 public function test_application_ttl_positive() { 676 $instance = cache_config_testing::instance(true); 677 $instance->phpunit_add_definition('phpunit/ttltest', array( 678 'mode' => cache_store::MODE_APPLICATION, 679 'component' => 'phpunit', 680 'area' => 'ttltest', 681 'ttl' => 86400 // Set to a day in the future to be extra sure. 682 )); 683 $cache = cache::make('phpunit', 'ttltest'); 684 $this->assertInstanceOf('cache_application', $cache); 685 686 // Purge it to be sure. 687 $this->assertTrue($cache->purge()); 688 // It won't be there yet. 689 $this->assertFalse($cache->has('Test')); 690 // Set it now. 691 $this->assertTrue($cache->set('Test', 'Test')); 692 // Check its there. 693 $this->assertTrue($cache->has('Test')); 694 // Double check by trying to get it. 695 $this->assertEquals('Test', $cache->get('Test')); 696 697 // Test with multiple keys. 698 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C'))); 699 $result = $cache->get_many(array('a', 'b', 'c')); 700 $this->assertInternalType('array', $result); 701 $this->assertCount(3, $result); 702 $this->assertArrayHasKey('a', $result); 703 $this->assertArrayHasKey('b', $result); 704 $this->assertArrayHasKey('c', $result); 705 $this->assertEquals('A', $result['a']); 706 $this->assertEquals('B', $result['b']); 707 $this->assertEquals('C', $result['c']); 708 709 // Test with multiple keys including missing ones. 710 $result = $cache->get_many(array('a', 'c', 'e')); 711 $this->assertInternalType('array', $result); 712 $this->assertCount(3, $result); 713 $this->assertArrayHasKey('a', $result); 714 $this->assertArrayHasKey('c', $result); 715 $this->assertArrayHasKey('e', $result); 716 $this->assertEquals('A', $result['a']); 717 $this->assertEquals('C', $result['c']); 718 $this->assertEquals(false, $result['e']); 719 } 720 721 /** 722 * Test a negative TTL on an session cache. 723 */ 724 public function test_session_ttl_positive() { 725 $instance = cache_config_testing::instance(true); 726 $instance->phpunit_add_definition('phpunit/ttltest', array( 727 'mode' => cache_store::MODE_SESSION, 728 'component' => 'phpunit', 729 'area' => 'ttltest', 730 'ttl' => 86400 // Set to a day in the future to be extra sure. 731 )); 732 $cache = cache::make('phpunit', 'ttltest'); 733 $this->assertInstanceOf('cache_session', $cache); 734 735 // Purge it to be sure. 736 $this->assertTrue($cache->purge()); 737 // It won't be there yet. 738 $this->assertFalse($cache->has('Test')); 739 // Set it now. 740 $this->assertTrue($cache->set('Test', 'Test')); 741 // Check its there. 742 $this->assertTrue($cache->has('Test')); 743 // Double check by trying to get it. 744 $this->assertEquals('Test', $cache->get('Test')); 745 746 // Test with multiple keys. 747 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C'))); 748 $result = $cache->get_many(array('a', 'b', 'c')); 749 $this->assertInternalType('array', $result); 750 $this->assertCount(3, $result); 751 $this->assertArrayHasKey('a', $result); 752 $this->assertArrayHasKey('b', $result); 753 $this->assertArrayHasKey('c', $result); 754 $this->assertEquals('A', $result['a']); 755 $this->assertEquals('B', $result['b']); 756 $this->assertEquals('C', $result['c']); 757 758 // Test with multiple keys including missing ones. 759 $result = $cache->get_many(array('a', 'c', 'e')); 760 $this->assertInternalType('array', $result); 761 $this->assertCount(3, $result); 762 $this->assertArrayHasKey('a', $result); 763 $this->assertArrayHasKey('c', $result); 764 $this->assertArrayHasKey('e', $result); 765 $this->assertEquals('A', $result['a']); 766 $this->assertEquals('C', $result['c']); 767 $this->assertEquals(false, $result['e']); 768 } 769 770 /** 771 * Tests manual locking operations on an application cache 772 */ 773 public function test_application_manual_locking() { 774 $instance = cache_config_testing::instance(); 775 $instance->phpunit_add_definition('phpunit/lockingtest', array( 776 'mode' => cache_store::MODE_APPLICATION, 777 'component' => 'phpunit', 778 'area' => 'lockingtest' 779 )); 780 $cache1 = cache::make('phpunit', 'lockingtest'); 781 $cache2 = clone($cache1); 782 783 $this->assertTrue($cache1->set('testkey', 'test data')); 784 $this->assertTrue($cache2->set('testkey', 'test data')); 785 786 $this->assertTrue($cache1->acquire_lock('testkey')); 787 $this->assertFalse($cache2->acquire_lock('testkey')); 788 789 $this->assertTrue($cache1->check_lock_state('testkey')); 790 $this->assertFalse($cache2->check_lock_state('testkey')); 791 792 $this->assertTrue($cache1->release_lock('testkey')); 793 $this->assertFalse($cache2->release_lock('testkey')); 794 795 $this->assertTrue($cache1->set('testkey', 'test data')); 796 $this->assertTrue($cache2->set('testkey', 'test data')); 797 } 798 799 /** 800 * Tests application cache event invalidation 801 */ 802 public function test_application_event_invalidation() { 803 $instance = cache_config_testing::instance(); 804 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array( 805 'mode' => cache_store::MODE_APPLICATION, 806 'component' => 'phpunit', 807 'area' => 'eventinvalidationtest', 808 'invalidationevents' => array( 809 'crazyevent' 810 ) 811 )); 812 $cache = cache::make('phpunit', 'eventinvalidationtest'); 813 814 $this->assertTrue($cache->set('testkey1', 'test data 1')); 815 $this->assertEquals('test data 1', $cache->get('testkey1')); 816 $this->assertTrue($cache->set('testkey2', 'test data 2')); 817 $this->assertEquals('test data 2', $cache->get('testkey2')); 818 819 // Test invalidating a single entry. 820 cache_helper::invalidate_by_event('crazyevent', array('testkey1')); 821 822 $this->assertFalse($cache->get('testkey1')); 823 $this->assertEquals('test data 2', $cache->get('testkey2')); 824 825 $this->assertTrue($cache->set('testkey1', 'test data 1')); 826 827 // Test invalidating both entries. 828 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2')); 829 830 $this->assertFalse($cache->get('testkey1')); 831 $this->assertFalse($cache->get('testkey2')); 832 } 833 834 /** 835 * Tests session cache event invalidation 836 */ 837 public function test_session_event_invalidation() { 838 $instance = cache_config_testing::instance(); 839 $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array( 840 'mode' => cache_store::MODE_SESSION, 841 'component' => 'phpunit', 842 'area' => 'test_session_event_invalidation', 843 'invalidationevents' => array( 844 'crazyevent' 845 ) 846 )); 847 $cache = cache::make('phpunit', 'test_session_event_invalidation'); 848 $this->assertInstanceOf('cache_session', $cache); 849 850 $this->assertTrue($cache->set('testkey1', 'test data 1')); 851 $this->assertEquals('test data 1', $cache->get('testkey1')); 852 $this->assertTrue($cache->set('testkey2', 'test data 2')); 853 $this->assertEquals('test data 2', $cache->get('testkey2')); 854 855 // Test invalidating a single entry. 856 cache_helper::invalidate_by_event('crazyevent', array('testkey1')); 857 858 $this->assertFalse($cache->get('testkey1')); 859 $this->assertEquals('test data 2', $cache->get('testkey2')); 860 861 $this->assertTrue($cache->set('testkey1', 'test data 1')); 862 863 // Test invalidating both entries. 864 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2')); 865 866 $this->assertFalse($cache->get('testkey1')); 867 $this->assertFalse($cache->get('testkey2')); 868 } 869 870 /** 871 * Tests application cache definition invalidation 872 */ 873 public function test_application_definition_invalidation() { 874 $instance = cache_config_testing::instance(); 875 $instance->phpunit_add_definition('phpunit/definitioninvalidation', array( 876 'mode' => cache_store::MODE_APPLICATION, 877 'component' => 'phpunit', 878 'area' => 'definitioninvalidation' 879 )); 880 $cache = cache::make('phpunit', 'definitioninvalidation'); 881 $this->assertTrue($cache->set('testkey1', 'test data 1')); 882 $this->assertEquals('test data 1', $cache->get('testkey1')); 883 $this->assertTrue($cache->set('testkey2', 'test data 2')); 884 $this->assertEquals('test data 2', $cache->get('testkey2')); 885 886 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1'); 887 888 $this->assertFalse($cache->get('testkey1')); 889 $this->assertEquals('test data 2', $cache->get('testkey2')); 890 891 $this->assertTrue($cache->set('testkey1', 'test data 1')); 892 893 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1')); 894 895 $this->assertFalse($cache->get('testkey1')); 896 $this->assertEquals('test data 2', $cache->get('testkey2')); 897 898 $this->assertTrue($cache->set('testkey1', 'test data 1')); 899 900 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2')); 901 902 $this->assertFalse($cache->get('testkey1')); 903 $this->assertFalse($cache->get('testkey2')); 904 } 905 906 /** 907 * Tests session cache definition invalidation 908 */ 909 public function test_session_definition_invalidation() { 910 $instance = cache_config_testing::instance(); 911 $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array( 912 'mode' => cache_store::MODE_SESSION, 913 'component' => 'phpunit', 914 'area' => 'test_session_definition_invalidation' 915 )); 916 $cache = cache::make('phpunit', 'test_session_definition_invalidation'); 917 $this->assertInstanceOf('cache_session', $cache); 918 $this->assertTrue($cache->set('testkey1', 'test data 1')); 919 $this->assertEquals('test data 1', $cache->get('testkey1')); 920 $this->assertTrue($cache->set('testkey2', 'test data 2')); 921 $this->assertEquals('test data 2', $cache->get('testkey2')); 922 923 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 'testkey1'); 924 925 $this->assertFalse($cache->get('testkey1')); 926 $this->assertEquals('test data 2', $cache->get('testkey2')); 927 928 $this->assertTrue($cache->set('testkey1', 'test data 1')); 929 930 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 931 array('testkey1')); 932 933 $this->assertFalse($cache->get('testkey1')); 934 $this->assertEquals('test data 2', $cache->get('testkey2')); 935 936 $this->assertTrue($cache->set('testkey1', 'test data 1')); 937 938 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 939 array('testkey1', 'testkey2')); 940 941 $this->assertFalse($cache->get('testkey1')); 942 $this->assertFalse($cache->get('testkey2')); 943 } 944 945 /** 946 * Tests application cache event invalidation over a distributed setup. 947 */ 948 public function test_distributed_application_event_invalidation() { 949 global $CFG; 950 // This is going to be an intense wee test. 951 // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a 952 // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally 953 // check that it is not picked up. 954 $instance = cache_config_testing::instance(); 955 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array( 956 'mode' => cache_store::MODE_APPLICATION, 957 'component' => 'phpunit', 958 'area' => 'eventinvalidationtest', 959 'simplekeys' => true, 960 'simpledata' => true, 961 'invalidationevents' => array( 962 'crazyevent' 963 ) 964 )); 965 $cache = cache::make('phpunit', 'eventinvalidationtest'); 966 $this->assertTrue($cache->set('testkey1', 'test data 1')); 967 $this->assertEquals('test data 1', $cache->get('testkey1')); 968 969 cache_helper::invalidate_by_event('crazyevent', array('testkey1')); 970 971 $this->assertFalse($cache->get('testkey1')); 972 973 // OK data added, data invalidated, and invalidation time has been set. 974 // Now we need to manually add back the data and adjust the invalidation time. 975 $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit'); 976 $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache"; 977 // Make sure the file is correct. 978 $this->assertTrue(file_exists($timefile)); 979 $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate. 980 make_writable_directory(dirname($timefile)); 981 file_put_contents($timefile, $timecont); 982 $this->assertTrue(file_exists($timefile)); 983 984 $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache"; 985 $datacont = serialize("test data 1"); 986 make_writable_directory(dirname($datafile)); 987 file_put_contents($datafile, $datacont); 988 $this->assertTrue(file_exists($datafile)); 989 990 // Test 1: Rebuild without the event and test its there. 991 cache_factory::reset(); 992 $instance = cache_config_testing::instance(); 993 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array( 994 'mode' => cache_store::MODE_APPLICATION, 995 'component' => 'phpunit', 996 'area' => 'eventinvalidationtest', 997 'simplekeys' => true, 998 'simpledata' => true, 999 )); 1000 $cache = cache::make('phpunit', 'eventinvalidationtest'); 1001 $this->assertEquals('test data 1', $cache->get('testkey1')); 1002 1003 // Test 2: Rebuild and test the invalidation of the event via the invalidation cache. 1004 cache_factory::reset(); 1005 $instance = cache_config_testing::instance(); 1006 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array( 1007 'mode' => cache_store::MODE_APPLICATION, 1008 'component' => 'phpunit', 1009 'area' => 'eventinvalidationtest', 1010 'simplekeys' => true, 1011 'simpledata' => true, 1012 'invalidationevents' => array( 1013 'crazyevent' 1014 ) 1015 )); 1016 $cache = cache::make('phpunit', 'eventinvalidationtest'); 1017 $this->assertFalse($cache->get('testkey1')); 1018 1019 // Test 3: Verify that an existing lastinvalidation cache file is updated when needed. 1020 1021 // Make a new cache class. This should should invalidate testkey2. 1022 $cache = cache::make('phpunit', 'eventinvalidationtest'); 1023 // Timestamp should have updated to cache::now(). 1024 $this->assertEquals(cache::now(), $cache->get('lastinvalidation')); 1025 1026 // Set testkey2 data. 1027 $cache->set('testkey2', 'test data 2'); 1028 // Backdate the event invalidation time by 30 seconds. 1029 $invalidationcache = cache::make('core', 'eventinvalidation'); 1030 $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 30)); 1031 // Lastinvalidation should already be cache::now(). 1032 $this->assertEquals(cache::now(), $cache->get('lastinvalidation')); 1033 // Set it to 15 seconds ago so that we know if it changes. 1034 $cache->set('lastinvalidation', cache::now() - 15); 1035 // Make a new cache class. This should not invalidate anything. 1036 cache_factory::instance()->reset_cache_instances(); 1037 $cache = cache::make('phpunit', 'eventinvalidationtest'); 1038 // Lastinvalidation shouldn't change since it was already newer than invalidation event. 1039 $this->assertEquals(cache::now() - 15, $cache->get('lastinvalidation')); 1040 1041 // Now set the event invalidation to newer than the lastinvalidation time. 1042 $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 5)); 1043 // Make a new cache class. This should should invalidate testkey2. 1044 cache_factory::instance()->reset_cache_instances(); 1045 $cache = cache::make('phpunit', 'eventinvalidationtest'); 1046 // Lastinvalidation timestamp should have updated to cache::now(). 1047 $this->assertEquals(cache::now(), $cache->get('lastinvalidation')); 1048 1049 // Now simulate a purge_by_event 5 seconds ago. 1050 $invalidationcache = cache::make('core', 'eventinvalidation'); 1051 $invalidationcache->set('crazyevent', array('purged' => cache::now() - 5)); 1052 // Set our lastinvalidation timestamp to 15 seconds ago. 1053 $cache->set('lastinvalidation', cache::now() - 15); 1054 // Make a new cache class. This should invalidate the cache. 1055 cache_factory::instance()->reset_cache_instances(); 1056 $cache = cache::make('phpunit', 'eventinvalidationtest'); 1057 // Lastinvalidation timestamp should have updated to cache::now(). 1058 $this->assertEquals(cache::now(), $cache->get('lastinvalidation')); 1059 1060 } 1061 1062 /** 1063 * Tests application cache event purge 1064 */ 1065 public function test_application_event_purge() { 1066 $instance = cache_config_testing::instance(); 1067 $instance->phpunit_add_definition('phpunit/eventpurgetest', array( 1068 'mode' => cache_store::MODE_APPLICATION, 1069 'component' => 'phpunit', 1070 'area' => 'eventpurgetest', 1071 'invalidationevents' => array( 1072 'crazyevent' 1073 ) 1074 )); 1075 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array( 1076 'mode' => cache_store::MODE_APPLICATION, 1077 'component' => 'phpunit', 1078 'area' => 'eventpurgetestaccelerated', 1079 'staticacceleration' => true, 1080 'invalidationevents' => array( 1081 'crazyevent' 1082 ) 1083 )); 1084 $cache = cache::make('phpunit', 'eventpurgetest'); 1085 1086 $this->assertTrue($cache->set('testkey1', 'test data 1')); 1087 $this->assertEquals('test data 1', $cache->get('testkey1')); 1088 $this->assertTrue($cache->set('testkey2', 'test data 2')); 1089 $this->assertEquals('test data 2', $cache->get('testkey2')); 1090 1091 // Purge the event. 1092 cache_helper::purge_by_event('crazyevent'); 1093 1094 // Check things have been removed. 1095 $this->assertFalse($cache->get('testkey1')); 1096 $this->assertFalse($cache->get('testkey2')); 1097 1098 // Now test the static acceleration array. 1099 $cache = cache::make('phpunit', 'eventpurgetestaccelerated'); 1100 $this->assertTrue($cache->set('testkey1', 'test data 1')); 1101 $this->assertEquals('test data 1', $cache->get('testkey1')); 1102 $this->assertTrue($cache->set('testkey2', 'test data 2')); 1103 $this->assertEquals('test data 2', $cache->get('testkey2')); 1104 1105 // Purge the event. 1106 cache_helper::purge_by_event('crazyevent'); 1107 1108 // Check things have been removed. 1109 $this->assertFalse($cache->get('testkey1')); 1110 $this->assertFalse($cache->get('testkey2')); 1111 } 1112 1113 /** 1114 * Tests session cache event purge 1115 */ 1116 public function test_session_event_purge() { 1117 $instance = cache_config_testing::instance(); 1118 $instance->phpunit_add_definition('phpunit/eventpurgetest', array( 1119 'mode' => cache_store::MODE_SESSION, 1120 'component' => 'phpunit', 1121 'area' => 'eventpurgetest', 1122 'invalidationevents' => array( 1123 'crazyevent' 1124 ) 1125 )); 1126 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array( 1127 'mode' => cache_store::MODE_SESSION, 1128 'component' => 'phpunit', 1129 'area' => 'eventpurgetestaccelerated', 1130 'staticacceleration' => true, 1131 'invalidationevents' => array( 1132 'crazyevent' 1133 ) 1134 )); 1135 $cache = cache::make('phpunit', 'eventpurgetest'); 1136 1137 $this->assertTrue($cache->set('testkey1', 'test data 1')); 1138 $this->assertEquals('test data 1', $cache->get('testkey1')); 1139 $this->assertTrue($cache->set('testkey2', 'test data 2')); 1140 $this->assertEquals('test data 2', $cache->get('testkey2')); 1141 1142 // Purge the event. 1143 cache_helper::purge_by_event('crazyevent'); 1144 1145 // Check things have been removed. 1146 $this->assertFalse($cache->get('testkey1')); 1147 $this->assertFalse($cache->get('testkey2')); 1148 1149 // Now test the static acceleration array. 1150 $cache = cache::make('phpunit', 'eventpurgetestaccelerated'); 1151 $this->assertTrue($cache->set('testkey1', 'test data 1')); 1152 $this->assertEquals('test data 1', $cache->get('testkey1')); 1153 $this->assertTrue($cache->set('testkey2', 'test data 2')); 1154 $this->assertEquals('test data 2', $cache->get('testkey2')); 1155 1156 // Purge the event. 1157 cache_helper::purge_by_event('crazyevent'); 1158 1159 // Check things have been removed. 1160 $this->assertFalse($cache->get('testkey1')); 1161 $this->assertFalse($cache->get('testkey2')); 1162 } 1163 1164 /** 1165 * Tests application cache definition purge 1166 */ 1167 public function test_application_definition_purge() { 1168 $instance = cache_config_testing::instance(); 1169 $instance->phpunit_add_definition('phpunit/definitionpurgetest', array( 1170 'mode' => cache_store::MODE_APPLICATION, 1171 'component' => 'phpunit', 1172 'area' => 'definitionpurgetest', 1173 'invalidationevents' => array( 1174 'crazyevent' 1175 ) 1176 )); 1177 $cache = cache::make('phpunit', 'definitionpurgetest'); 1178 1179 $this->assertTrue($cache->set('testkey1', 'test data 1')); 1180 $this->assertEquals('test data 1', $cache->get('testkey1')); 1181 $this->assertTrue($cache->set('testkey2', 'test data 2')); 1182 $this->assertEquals('test data 2', $cache->get('testkey2')); 1183 1184 // Purge the event. 1185 cache_helper::purge_by_definition('phpunit', 'definitionpurgetest'); 1186 1187 // Check things have been removed. 1188 $this->assertFalse($cache->get('testkey1')); 1189 $this->assertFalse($cache->get('testkey2')); 1190 } 1191 1192 /** 1193 * Test the use of an alt path. 1194 * If we can generate a config instance we are done :) 1195 */ 1196 public function test_alt_cache_path() { 1197 global $CFG; 1198 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) { 1199 $this->markTestSkipped('Skipped testing alt cache path as it is already being used.'); 1200 } 1201 $this->resetAfterTest(); 1202 $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath'; 1203 $instance = cache_config_testing::instance(); 1204 $this->assertInstanceOf('cache_config', $instance); 1205 } 1206 1207 /** 1208 * Test disabling the cache stores. 1209 */ 1210 public function test_disable_stores() { 1211 $instance = cache_config_testing::instance(); 1212 $instance->phpunit_add_definition('phpunit/disabletest1', array( 1213 'mode' => cache_store::MODE_APPLICATION, 1214 'component' => 'phpunit', 1215 'area' => 'disabletest1' 1216 )); 1217 $instance->phpunit_add_definition('phpunit/disabletest2', array( 1218 'mode' => cache_store::MODE_SESSION, 1219 'component' => 'phpunit', 1220 'area' => 'disabletest2' 1221 )); 1222 $instance->phpunit_add_definition('phpunit/disabletest3', array( 1223 'mode' => cache_store::MODE_REQUEST, 1224 'component' => 'phpunit', 1225 'area' => 'disabletest3' 1226 )); 1227 1228 $caches = array( 1229 'disabletest1' => cache::make('phpunit', 'disabletest1'), 1230 'disabletest2' => cache::make('phpunit', 'disabletest2'), 1231 'disabletest3' => cache::make('phpunit', 'disabletest3') 1232 ); 1233 1234 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']); 1235 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']); 1236 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']); 1237 1238 $this->assertEquals('cachestore_file', $caches['disabletest1']->phpunit_get_store_class()); 1239 $this->assertEquals('cachestore_session', $caches['disabletest2']->phpunit_get_store_class()); 1240 $this->assertEquals('cachestore_static', $caches['disabletest3']->phpunit_get_store_class()); 1241 1242 foreach ($caches as $cache) { 1243 $this->assertFalse($cache->get('test')); 1244 $this->assertTrue($cache->set('test', 'test')); 1245 $this->assertEquals('test', $cache->get('test')); 1246 } 1247 1248 cache_factory::disable_stores(); 1249 1250 $caches = array( 1251 'disabletest1' => cache::make('phpunit', 'disabletest1'), 1252 'disabletest2' => cache::make('phpunit', 'disabletest2'), 1253 'disabletest3' => cache::make('phpunit', 'disabletest3') 1254 ); 1255 1256 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']); 1257 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']); 1258 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']); 1259 1260 $this->assertEquals('cachestore_dummy', $caches['disabletest1']->phpunit_get_store_class()); 1261 $this->assertEquals('cachestore_dummy', $caches['disabletest2']->phpunit_get_store_class()); 1262 $this->assertEquals('cachestore_dummy', $caches['disabletest3']->phpunit_get_store_class()); 1263 1264 foreach ($caches as $cache) { 1265 $this->assertFalse($cache->get('test')); 1266 $this->assertTrue($cache->set('test', 'test')); 1267 $this->assertEquals('test', $cache->get('test')); 1268 } 1269 } 1270 1271 /** 1272 * Test disabling the cache. 1273 */ 1274 public function test_disable() { 1275 global $CFG; 1276 1277 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) { 1278 // We can't run this test as it requires us to delete the cache configuration script which we just 1279 // cant do with a custom path in play. 1280 $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.'); 1281 } 1282 1283 $configfile = $CFG->dataroot.'/muc/config.php'; 1284 1285 // That's right, we're deleting the config file. 1286 $this->assertTrue(@unlink($configfile)); 1287 1288 // Disable the cache 1289 cache_phpunit_factory::phpunit_disable(); 1290 1291 // Check we get the expected disabled factory. 1292 $factory = cache_factory::instance(); 1293 $this->assertInstanceOf('cache_factory_disabled', $factory); 1294 1295 // Check we get the expected disabled config. 1296 $config = $factory->create_config_instance(); 1297 $this->assertInstanceOf('cache_config_disabled', $config); 1298 1299 // Check we get the expected disabled caches. 1300 $cache = cache::make('phpunit', 'disable'); 1301 $this->assertInstanceOf('cache_disabled', $cache); 1302 1303 // Test an application cache. 1304 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable'); 1305 $this->assertInstanceOf('cache_disabled', $cache); 1306 1307 $this->assertFalse(file_exists($configfile)); 1308 1309 $this->assertFalse($cache->get('test')); 1310 $this->assertFalse($cache->set('test', 'test')); 1311 $this->assertFalse($cache->delete('test')); 1312 $this->assertTrue($cache->purge()); 1313 1314 // Test a session cache. 1315 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable'); 1316 $this->assertInstanceOf('cache_disabled', $cache); 1317 1318 $this->assertFalse(file_exists($configfile)); 1319 1320 $this->assertFalse($cache->get('test')); 1321 $this->assertFalse($cache->set('test', 'test')); 1322 $this->assertFalse($cache->delete('test')); 1323 $this->assertTrue($cache->purge()); 1324 1325 // Finally test a request cache. 1326 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable'); 1327 $this->assertInstanceOf('cache_disabled', $cache); 1328 1329 $this->assertFalse(file_exists($configfile)); 1330 1331 $this->assertFalse($cache->get('test')); 1332 $this->assertFalse($cache->set('test', 'test')); 1333 $this->assertFalse($cache->delete('test')); 1334 $this->assertTrue($cache->purge()); 1335 1336 cache_factory::reset(); 1337 1338 $factory = cache_factory::instance(true); 1339 $config = $factory->create_config_instance(); 1340 $this->assertEquals('cache_config_testing', get_class($config)); 1341 } 1342 1343 /** 1344 * Test that multiple application loaders work ok. 1345 */ 1346 public function test_multiple_application_loaders() { 1347 $instance = cache_config_testing::instance(true); 1348 $instance->phpunit_add_file_store('phpunittest1'); 1349 $instance->phpunit_add_file_store('phpunittest2'); 1350 $instance->phpunit_add_definition('phpunit/multi_loader', array( 1351 'mode' => cache_store::MODE_APPLICATION, 1352 'component' => 'phpunit', 1353 'area' => 'multi_loader' 1354 )); 1355 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3); 1356 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2); 1357 1358 $cache = cache::make('phpunit', 'multi_loader'); 1359 $this->assertInstanceOf('cache_application', $cache); 1360 $this->assertFalse($cache->get('test')); 1361 $this->assertTrue($cache->set('test', 'test')); 1362 $this->assertEquals('test', $cache->get('test')); 1363 $this->assertTrue($cache->delete('test')); 1364 $this->assertFalse($cache->get('test')); 1365 $this->assertTrue($cache->set('test', 'test')); 1366 $this->assertTrue($cache->purge()); 1367 $this->assertFalse($cache->get('test')); 1368 1369 // Test the many commands. 1370 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C'))); 1371 $result = $cache->get_many(array('a', 'b', 'c')); 1372 $this->assertInternalType('array', $result); 1373 $this->assertCount(3, $result); 1374 $this->assertArrayHasKey('a', $result); 1375 $this->assertArrayHasKey('b', $result); 1376 $this->assertArrayHasKey('c', $result); 1377 $this->assertEquals('A', $result['a']); 1378 $this->assertEquals('B', $result['b']); 1379 $this->assertEquals('C', $result['c']); 1380 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c'))); 1381 $this->assertEquals(2, $cache->delete_many(array('a', 'c'))); 1382 $result = $cache->get_many(array('a', 'b', 'c')); 1383 $this->assertInternalType('array', $result); 1384 $this->assertCount(3, $result); 1385 $this->assertArrayHasKey('a', $result); 1386 $this->assertArrayHasKey('b', $result); 1387 $this->assertArrayHasKey('c', $result); 1388 $this->assertFalse($result['a']); 1389 $this->assertEquals('B', $result['b']); 1390 $this->assertFalse($result['c']); 1391 1392 // Test non-recursive deletes. 1393 $this->assertTrue($cache->set('test', 'test')); 1394 $this->assertSame('test', $cache->get('test')); 1395 $this->assertTrue($cache->delete('test', false)); 1396 // We should still have it on a deeper loader. 1397 $this->assertSame('test', $cache->get('test')); 1398 // Test non-recusive with many functions. 1399 $this->assertSame(3, $cache->set_many(array( 1400 'one' => 'one', 1401 'two' => 'two', 1402 'three' => 'three' 1403 ))); 1404 $this->assertSame('one', $cache->get('one')); 1405 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three'))); 1406 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false)); 1407 $this->assertSame('one', $cache->get('one')); 1408 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three'))); 1409 } 1410 1411 /** 1412 * Test that multiple application loaders work ok. 1413 */ 1414 public function test_multiple_session_loaders() { 1415 /* @var cache_config_testing $instance */ 1416 $instance = cache_config_testing::instance(true); 1417 $instance->phpunit_add_session_store('phpunittest1'); 1418 $instance->phpunit_add_session_store('phpunittest2'); 1419 $instance->phpunit_add_definition('phpunit/multi_loader', array( 1420 'mode' => cache_store::MODE_SESSION, 1421 'component' => 'phpunit', 1422 'area' => 'multi_loader' 1423 )); 1424 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3); 1425 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2); 1426 1427 $cache = cache::make('phpunit', 'multi_loader'); 1428 $this->assertInstanceOf('cache_session', $cache); 1429 $this->assertFalse($cache->get('test')); 1430 $this->assertTrue($cache->set('test', 'test')); 1431 $this->assertEquals('test', $cache->get('test')); 1432 $this->assertTrue($cache->delete('test')); 1433 $this->assertFalse($cache->get('test')); 1434 $this->assertTrue($cache->set('test', 'test')); 1435 $this->assertTrue($cache->purge()); 1436 $this->assertFalse($cache->get('test')); 1437 1438 // Test the many commands. 1439 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C'))); 1440 $result = $cache->get_many(array('a', 'b', 'c')); 1441 $this->assertInternalType('array', $result); 1442 $this->assertCount(3, $result); 1443 $this->assertArrayHasKey('a', $result); 1444 $this->assertArrayHasKey('b', $result); 1445 $this->assertArrayHasKey('c', $result); 1446 $this->assertEquals('A', $result['a']); 1447 $this->assertEquals('B', $result['b']); 1448 $this->assertEquals('C', $result['c']); 1449 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c'))); 1450 $this->assertEquals(2, $cache->delete_many(array('a', 'c'))); 1451 $result = $cache->get_many(array('a', 'b', 'c')); 1452 $this->assertInternalType('array', $result); 1453 $this->assertCount(3, $result); 1454 $this->assertArrayHasKey('a', $result); 1455 $this->assertArrayHasKey('b', $result); 1456 $this->assertArrayHasKey('c', $result); 1457 $this->assertFalse($result['a']); 1458 $this->assertEquals('B', $result['b']); 1459 $this->assertFalse($result['c']); 1460 1461 // Test non-recursive deletes. 1462 $this->assertTrue($cache->set('test', 'test')); 1463 $this->assertSame('test', $cache->get('test')); 1464 $this->assertTrue($cache->delete('test', false)); 1465 // We should still have it on a deeper loader. 1466 $this->assertSame('test', $cache->get('test')); 1467 // Test non-recusive with many functions. 1468 $this->assertSame(3, $cache->set_many(array( 1469 'one' => 'one', 1470 'two' => 'two', 1471 'three' => 'three' 1472 ))); 1473 $this->assertSame('one', $cache->get('one')); 1474 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three'))); 1475 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false)); 1476 $this->assertSame('one', $cache->get('one')); 1477 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three'))); 1478 } 1479 1480 /** 1481 * Test switching users with session caches. 1482 */ 1483 public function test_session_cache_switch_user() { 1484 $this->resetAfterTest(true); 1485 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache'); 1486 $user1 = $this->getDataGenerator()->create_user(); 1487 $user2 = $this->getDataGenerator()->create_user(); 1488 1489 // Log in as the first user. 1490 $this->setUser($user1); 1491 $sesskey1 = sesskey(); 1492 1493 // Set a basic value in the cache. 1494 $cache->set('var', 1); 1495 $this->assertTrue($cache->has('var')); 1496 $this->assertEquals(1, $cache->get('var')); 1497 1498 // Change to the second user. 1499 $this->setUser($user2); 1500 $sesskey2 = sesskey(); 1501 1502 // Make sure the cache doesn't give us the data for the last user. 1503 $this->assertNotEquals($sesskey1, $sesskey2); 1504 $this->assertFalse($cache->has('var')); 1505 $this->assertEquals(false, $cache->get('var')); 1506 } 1507 1508 /** 1509 * Test switching users with session caches. 1510 */ 1511 public function test_session_cache_switch_user_application_mapping() { 1512 $this->resetAfterTest(true); 1513 $instance = cache_config_testing::instance(true); 1514 $instance->phpunit_add_file_store('testfilestore'); 1515 $instance->phpunit_add_definition('phpunit/testappsession', array( 1516 'mode' => cache_store::MODE_SESSION, 1517 'component' => 'phpunit', 1518 'area' => 'testappsession' 1519 )); 1520 $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3); 1521 $cache = cache::make('phpunit', 'testappsession'); 1522 $user1 = $this->getDataGenerator()->create_user(); 1523 $user2 = $this->getDataGenerator()->create_user(); 1524 1525 // Log in as the first user. 1526 $this->setUser($user1); 1527 $sesskey1 = sesskey(); 1528 1529 // Set a basic value in the cache. 1530 $cache->set('var', 1); 1531 $this->assertTrue($cache->has('var')); 1532 $this->assertEquals(1, $cache->get('var')); 1533 1534 // Change to the second user. 1535 $this->setUser($user2); 1536 $sesskey2 = sesskey(); 1537 1538 // Make sure the cache doesn't give us the data for the last user. 1539 $this->assertNotEquals($sesskey1, $sesskey2); 1540 $this->assertFalse($cache->has('var')); 1541 $this->assertEquals(false, $cache->get('var')); 1542 } 1543 1544 /** 1545 * Test two session caches being used at once to confirm collisions don't occur. 1546 */ 1547 public function test_dual_session_caches() { 1548 $instance = cache_config_testing::instance(true); 1549 $instance->phpunit_add_definition('phpunit/testsess1', array( 1550 'mode' => cache_store::MODE_SESSION, 1551 'component' => 'phpunit', 1552 'area' => 'testsess1' 1553 )); 1554 $instance->phpunit_add_definition('phpunit/testsess2', array( 1555 'mode' => cache_store::MODE_SESSION, 1556 'component' => 'phpunit', 1557 'area' => 'testsess2' 1558 )); 1559 $cache1 = cache::make('phpunit', 'testsess1'); 1560 $cache2 = cache::make('phpunit', 'testsess2'); 1561 1562 $this->assertFalse($cache1->has('test')); 1563 $this->assertFalse($cache2->has('test')); 1564 1565 $this->assertTrue($cache1->set('test', '1')); 1566 1567 $this->assertTrue($cache1->has('test')); 1568 $this->assertFalse($cache2->has('test')); 1569 1570 $this->assertTrue($cache2->set('test', '2')); 1571 1572 $this->assertEquals(1, $cache1->get('test')); 1573 $this->assertEquals(2, $cache2->get('test')); 1574 1575 $this->assertTrue($cache1->delete('test')); 1576 } 1577 1578 /** 1579 * Test multiple session caches when switching user. 1580 */ 1581 public function test_session_cache_switch_user_multiple() { 1582 $this->resetAfterTest(true); 1583 $cache1 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache1'); 1584 $cache2 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache2'); 1585 $user1 = $this->getDataGenerator()->create_user(); 1586 $user2 = $this->getDataGenerator()->create_user(); 1587 1588 // Log in as the first user. 1589 $this->setUser($user1); 1590 $sesskey1 = sesskey(); 1591 1592 // Set a basic value in the caches. 1593 $cache1->set('var', 1); 1594 $cache2->set('var', 2); 1595 $this->assertEquals(1, $cache1->get('var')); 1596 $this->assertEquals(2, $cache2->get('var')); 1597 1598 // Change to the second user. 1599 $this->setUser($user2); 1600 $sesskey2 = sesskey(); 1601 1602 // Make sure the cache doesn't give us the data for the last user. 1603 // Also make sure that switching the user has lead to both caches being purged. 1604 $this->assertNotEquals($sesskey1, $sesskey2); 1605 $this->assertEquals(false, $cache1->get('var')); 1606 $this->assertEquals(false, $cache2->get('var')); 1607 } 1608 1609 /** 1610 * Test application locking. 1611 */ 1612 public function test_application_locking() { 1613 $instance = cache_config_testing::instance(true); 1614 $instance->phpunit_add_definition('phpunit/test_application_locking', array( 1615 'mode' => cache_store::MODE_APPLICATION, 1616 'component' => 'phpunit', 1617 'area' => 'test_application_locking', 1618 'staticacceleration' => true, 1619 'staticaccelerationsize' => 1, 1620 'requirelockingread' => true, 1621 'requirelockingwrite' => true 1622 )); 1623 $cache = cache::make('phpunit', 'test_application_locking'); 1624 $this->assertInstanceOf('cache_application', $cache); 1625 1626 $this->assertTrue($cache->set('a', 'A')); 1627 $this->assertTrue($cache->set('b', 'B')); 1628 $this->assertTrue($cache->set('c', 'C')); 1629 $this->assertEquals('A', $cache->get('a')); 1630 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c'))); 1631 $this->assertTrue($cache->delete('a')); 1632 $this->assertFalse($cache->has('a')); 1633 } 1634 1635 /** 1636 * Test the static cache_helper method purge_stores_used_by_definition. 1637 */ 1638 public function test_purge_stores_used_by_definition() { 1639 $instance = cache_config_testing::instance(true); 1640 $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', array( 1641 'mode' => cache_store::MODE_APPLICATION, 1642 'component' => 'phpunit', 1643 'area' => 'test_purge_stores_used_by_definition' 1644 )); 1645 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition'); 1646 $this->assertInstanceOf('cache_application', $cache); 1647 $this->assertTrue($cache->set('test', 'test')); 1648 unset($cache); 1649 1650 cache_helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition'); 1651 1652 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition'); 1653 $this->assertInstanceOf('cache_application', $cache); 1654 $this->assertFalse($cache->get('test')); 1655 } 1656 1657 /** 1658 * Test purge routines. 1659 */ 1660 public function test_purge_routines() { 1661 $instance = cache_config_testing::instance(true); 1662 $instance->phpunit_add_definition('phpunit/purge1', array( 1663 'mode' => cache_store::MODE_APPLICATION, 1664 'component' => 'phpunit', 1665 'area' => 'purge1' 1666 )); 1667 $instance->phpunit_add_definition('phpunit/purge2', array( 1668 'mode' => cache_store::MODE_APPLICATION, 1669 'component' => 'phpunit', 1670 'area' => 'purge2', 1671 'requireidentifiers' => array( 1672 'id' 1673 ) 1674 )); 1675 1676 $factory = cache_factory::instance(); 1677 $definition = $factory->create_definition('phpunit', 'purge1'); 1678 $this->assertFalse($definition->has_required_identifiers()); 1679 $cache = $factory->create_cache($definition); 1680 $this->assertInstanceOf('cache_application', $cache); 1681 $this->assertTrue($cache->set('test', 'test')); 1682 $this->assertTrue($cache->has('test')); 1683 cache_helper::purge_by_definition('phpunit', 'purge1'); 1684 $this->assertFalse($cache->has('test')); 1685 1686 $factory = cache_factory::instance(); 1687 $definition = $factory->create_definition('phpunit', 'purge2'); 1688 $this->assertTrue($definition->has_required_identifiers()); 1689 $cache = $factory->create_cache($definition); 1690 $this->assertInstanceOf('cache_application', $cache); 1691 $this->assertTrue($cache->set('test', 'test')); 1692 $this->assertTrue($cache->has('test')); 1693 cache_helper::purge_stores_used_by_definition('phpunit', 'purge2'); 1694 $this->assertFalse($cache->has('test')); 1695 1696 try { 1697 cache_helper::purge_by_definition('phpunit', 'purge2'); 1698 $this->fail('Should not be able to purge a definition required identifiers without providing them.'); 1699 } catch (coding_exception $ex) { 1700 $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage()); 1701 } 1702 } 1703 1704 /** 1705 * Test that the default stores all support searching. 1706 */ 1707 public function test_defaults_support_searching() { 1708 $instance = cache_config_testing::instance(true); 1709 $instance->phpunit_add_definition('phpunit/search1', array( 1710 'mode' => cache_store::MODE_APPLICATION, 1711 'component' => 'phpunit', 1712 'area' => 'search1', 1713 'requiresearchable' => true 1714 )); 1715 $instance->phpunit_add_definition('phpunit/search2', array( 1716 'mode' => cache_store::MODE_SESSION, 1717 'component' => 'phpunit', 1718 'area' => 'search2', 1719 'requiresearchable' => true 1720 )); 1721 $instance->phpunit_add_definition('phpunit/search3', array( 1722 'mode' => cache_store::MODE_REQUEST, 1723 'component' => 'phpunit', 1724 'area' => 'search3', 1725 'requiresearchable' => true 1726 )); 1727 $factory = cache_factory::instance(); 1728 1729 // Test application cache is searchable. 1730 $definition = $factory->create_definition('phpunit', 'search1'); 1731 $this->assertInstanceOf('cache_definition', $definition); 1732 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE); 1733 $cache = $factory->create_cache($definition); 1734 $this->assertInstanceOf('cache_application', $cache); 1735 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements()); 1736 1737 // Test session cache is searchable. 1738 $definition = $factory->create_definition('phpunit', 'search2'); 1739 $this->assertInstanceOf('cache_definition', $definition); 1740 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE); 1741 $cache = $factory->create_cache($definition); 1742 $this->assertInstanceOf('cache_session', $cache); 1743 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements()); 1744 1745 // Test request cache is searchable. 1746 $definition = $factory->create_definition('phpunit', 'search3'); 1747 $this->assertInstanceOf('cache_definition', $definition); 1748 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE); 1749 $cache = $factory->create_cache($definition); 1750 $this->assertInstanceOf('cache_request', $cache); 1751 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements()); 1752 } 1753 1754 public function test_static_acceleration() { 1755 $instance = cache_config_testing::instance(); 1756 $instance->phpunit_add_definition('phpunit/accelerated', array( 1757 'mode' => cache_store::MODE_APPLICATION, 1758 'component' => 'phpunit', 1759 'area' => 'accelerated', 1760 'staticacceleration' => true, 1761 'staticaccelerationsize' => 3, 1762 )); 1763 $instance->phpunit_add_definition('phpunit/accelerated2', array( 1764 'mode' => cache_store::MODE_APPLICATION, 1765 'component' => 'phpunit', 1766 'area' => 'accelerated2', 1767 'staticacceleration' => true, 1768 'staticaccelerationsize' => 3, 1769 )); 1770 $instance->phpunit_add_definition('phpunit/accelerated3', array( 1771 'mode' => cache_store::MODE_APPLICATION, 1772 'component' => 'phpunit', 1773 'area' => 'accelerated3', 1774 'staticacceleration' => true, 1775 'staticaccelerationsize' => 3, 1776 )); 1777 $instance->phpunit_add_definition('phpunit/accelerated4', array( 1778 'mode' => cache_store::MODE_APPLICATION, 1779 'component' => 'phpunit', 1780 'area' => 'accelerated4', 1781 'staticacceleration' => true, 1782 'staticaccelerationsize' => 4, 1783 )); 1784 $instance->phpunit_add_definition('phpunit/simpledataarea1', array( 1785 'mode' => cache_store::MODE_APPLICATION, 1786 'component' => 'phpunit', 1787 'area' => 'simpledataarea1', 1788 'staticacceleration' => true, 1789 'simpledata' => false 1790 )); 1791 $instance->phpunit_add_definition('phpunit/simpledataarea2', array( 1792 'mode' => cache_store::MODE_APPLICATION, 1793 'component' => 'phpunit', 1794 'area' => 'simpledataarea2', 1795 'staticacceleration' => true, 1796 'simpledata' => true 1797 )); 1798 1799 $cache = cache::make('phpunit', 'accelerated'); 1800 $this->assertInstanceOf('cache_phpunit_application', $cache); 1801 1802 // Set and get three elements. 1803 $this->assertTrue($cache->set('a', 'A')); 1804 $this->assertTrue($cache->set('b', 'B')); 1805 $this->assertTrue($cache->set('c', 'C')); 1806 $this->assertEquals('A', $cache->get('a')); 1807 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c'))); 1808 1809 // Make sure all items are in static acceleration array. 1810 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); 1811 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b')); 1812 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); 1813 1814 // Add new value and make sure it is in cache and it is in array. 1815 $this->assertTrue($cache->set('d', 'D')); 1816 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); 1817 $this->assertEquals('D', $cache->get('d')); 1818 1819 // Now the least recent accessed item (a) is no longer in acceleration array. 1820 $this->assertFalse($cache->phpunit_static_acceleration_get('a')); 1821 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b')); 1822 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); 1823 1824 // Adding and deleting element. 1825 $this->assertTrue($cache->set('a', 'A')); 1826 $this->assertTrue($cache->delete('a')); 1827 $this->assertFalse($cache->phpunit_static_acceleration_get('a')); 1828 $this->assertFalse($cache->has('a')); 1829 1830 // Make sure "purge" deletes from the array as well. 1831 $cache->purge(); 1832 $this->assertFalse($cache->phpunit_static_acceleration_get('a')); 1833 $this->assertFalse($cache->phpunit_static_acceleration_get('b')); 1834 $this->assertFalse($cache->phpunit_static_acceleration_get('c')); 1835 $this->assertFalse($cache->phpunit_static_acceleration_get('d')); 1836 $this->assertFalse($cache->phpunit_static_acceleration_get('e')); 1837 1838 // Check that the array holds the last accessed items by get/set. 1839 $this->assertTrue($cache->set('a', 'A')); 1840 $this->assertTrue($cache->set('b', 'B')); 1841 $this->assertTrue($cache->set('c', 'C')); 1842 $this->assertTrue($cache->set('d', 'D')); 1843 $this->assertTrue($cache->set('e', 'E')); 1844 $this->assertFalse($cache->phpunit_static_acceleration_get('a')); 1845 $this->assertFalse($cache->phpunit_static_acceleration_get('b')); 1846 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); 1847 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); 1848 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); 1849 1850 /** @var cache_phpunit_application $cache */ 1851 $cache = cache::make('phpunit', 'accelerated2'); 1852 $this->assertInstanceOf('cache_phpunit_application', $cache); 1853 1854 // Check that the array holds the last accessed items by get/set. 1855 $this->assertTrue($cache->set('a', 'A')); 1856 $this->assertTrue($cache->set('b', 'B')); 1857 $this->assertTrue($cache->set('c', 'C')); 1858 $this->assertTrue($cache->set('d', 'D')); 1859 $this->assertTrue($cache->set('e', 'E')); 1860 // Current keys in the array: c, d, e. 1861 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); 1862 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); 1863 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); 1864 $this->assertFalse($cache->phpunit_static_acceleration_get('a')); 1865 $this->assertFalse($cache->phpunit_static_acceleration_get('b')); 1866 1867 $this->assertEquals('A', $cache->get('a')); 1868 // Current keys in the array: d, e, a. 1869 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); 1870 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); 1871 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); 1872 $this->assertFalse($cache->phpunit_static_acceleration_get('b')); 1873 $this->assertFalse($cache->phpunit_static_acceleration_get('c')); 1874 1875 // Current keys in the array: d, e, a. 1876 $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c'))); 1877 // Current keys in the array: e, a, c. 1878 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); 1879 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); 1880 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); 1881 $this->assertFalse($cache->phpunit_static_acceleration_get('b')); 1882 $this->assertFalse($cache->phpunit_static_acceleration_get('d')); 1883 1884 1885 $cache = cache::make('phpunit', 'accelerated3'); 1886 $this->assertInstanceOf('cache_phpunit_application', $cache); 1887 1888 // Check that the array holds the last accessed items by get/set. 1889 $this->assertTrue($cache->set('a', 'A')); 1890 $this->assertTrue($cache->set('b', 'B')); 1891 $this->assertTrue($cache->set('c', 'C')); 1892 $this->assertTrue($cache->set('d', 'D')); 1893 $this->assertTrue($cache->set('e', 'E')); 1894 $this->assertFalse($cache->phpunit_static_acceleration_get('a')); 1895 $this->assertFalse($cache->phpunit_static_acceleration_get('b')); 1896 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c')); 1897 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); 1898 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); 1899 1900 $this->assertTrue($cache->set('b', 'B2')); 1901 $this->assertFalse($cache->phpunit_static_acceleration_get('a')); 1902 $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b')); 1903 $this->assertFalse($cache->phpunit_static_acceleration_get('c')); 1904 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d')); 1905 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); 1906 1907 $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3'))); 1908 $this->assertFalse($cache->phpunit_static_acceleration_get('a')); 1909 $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b')); 1910 $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c')); 1911 $this->assertFalse($cache->phpunit_static_acceleration_get('d')); 1912 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e')); 1913 1914 $cache = cache::make('phpunit', 'accelerated4'); 1915 $this->assertInstanceOf('cache_phpunit_application', $cache); 1916 $this->assertTrue($cache->set('a', 'A')); 1917 $this->assertTrue($cache->set('a', 'A')); 1918 $this->assertTrue($cache->set('a', 'A')); 1919 $this->assertTrue($cache->set('a', 'A')); 1920 $this->assertTrue($cache->set('a', 'A')); 1921 $this->assertTrue($cache->set('a', 'A')); 1922 $this->assertTrue($cache->set('a', 'A')); 1923 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a')); 1924 $this->assertEquals('A', $cache->get('a')); 1925 1926 // Setting simpledata to false objects are cloned when retrieving data. 1927 $cache = cache::make('phpunit', 'simpledataarea1'); 1928 $notreallysimple = new stdClass(); 1929 $notreallysimple->name = 'a'; 1930 $cache->set('a', $notreallysimple); 1931 $returnedinstance1 = $cache->get('a'); 1932 $returnedinstance2 = $cache->get('a'); 1933 $returnedinstance1->name = 'b'; 1934 $this->assertEquals('a', $returnedinstance2->name); 1935 1936 // Setting simpledata to true we assume that data does not contain references. 1937 $cache = cache::make('phpunit', 'simpledataarea2'); 1938 $notreallysimple = new stdClass(); 1939 $notreallysimple->name = 'a'; 1940 $cache->set('a', $notreallysimple); 1941 $returnedinstance1 = $cache->get('a'); 1942 $returnedinstance2 = $cache->get('a'); 1943 $returnedinstance1->name = 'b'; 1944 $this->assertEquals('b', $returnedinstance2->name); 1945 } 1946 1947 public function test_performance_debug() { 1948 global $CFG; 1949 $this->resetAfterTest(true); 1950 $CFG->perfdebug = 15; 1951 1952 $instance = cache_config_testing::instance(); 1953 $applicationid = 'phpunit/applicationperf'; 1954 $instance->phpunit_add_definition($applicationid, array( 1955 'mode' => cache_store::MODE_APPLICATION, 1956 'component' => 'phpunit', 1957 'area' => 'applicationperf' 1958 )); 1959 $sessionid = 'phpunit/sessionperf'; 1960 $instance->phpunit_add_definition($sessionid, array( 1961 'mode' => cache_store::MODE_SESSION, 1962 'component' => 'phpunit', 1963 'area' => 'sessionperf' 1964 )); 1965 $requestid = 'phpunit/requestperf'; 1966 $instance->phpunit_add_definition($requestid, array( 1967 'mode' => cache_store::MODE_REQUEST, 1968 'component' => 'phpunit', 1969 'area' => 'requestperf' 1970 )); 1971 1972 $application = cache::make('phpunit', 'applicationperf'); 1973 $session = cache::make('phpunit', 'sessionperf'); 1974 $request = cache::make('phpunit', 'requestperf'); 1975 1976 // Check that no stats are recorded for these definitions yet. 1977 $stats = cache_helper::get_stats(); 1978 $this->assertArrayNotHasKey($applicationid, $stats); 1979 $this->assertArrayHasKey($sessionid, $stats); // Session cache sets a key on construct. 1980 $this->assertArrayNotHasKey($requestid, $stats); 1981 1982 // Check that stores register misses. 1983 $this->assertFalse($application->get('missMe')); 1984 $this->assertFalse($application->get('missMe')); 1985 $this->assertFalse($session->get('missMe')); 1986 $this->assertFalse($session->get('missMe')); 1987 $this->assertFalse($session->get('missMe')); 1988 $this->assertFalse($request->get('missMe')); 1989 $this->assertFalse($request->get('missMe')); 1990 $this->assertFalse($request->get('missMe')); 1991 $this->assertFalse($request->get('missMe')); 1992 1993 $endstats = cache_helper::get_stats(); 1994 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['misses']); 1995 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits']); 1996 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets']); 1997 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['misses']); 1998 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits']); 1999 $this->assertEquals(1, $endstats[$sessionid]['stores']['cachestore_session']['sets']); 2000 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['misses']); 2001 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits']); 2002 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets']); 2003 2004 $startstats = cache_helper::get_stats(); 2005 2006 // Check that stores register sets. 2007 $this->assertTrue($application->set('setMe1', 1)); 2008 $this->assertTrue($application->set('setMe2', 2)); 2009 $this->assertTrue($session->set('setMe1', 1)); 2010 $this->assertTrue($session->set('setMe2', 2)); 2011 $this->assertTrue($session->set('setMe3', 3)); 2012 $this->assertTrue($request->set('setMe1', 1)); 2013 $this->assertTrue($request->set('setMe2', 2)); 2014 $this->assertTrue($request->set('setMe3', 3)); 2015 $this->assertTrue($request->set('setMe4', 4)); 2016 2017 $endstats = cache_helper::get_stats(); 2018 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] - 2019 $startstats[$applicationid]['stores']['cachestore_file']['misses']); 2020 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits'] - 2021 $startstats[$applicationid]['stores']['cachestore_file']['hits']); 2022 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['sets'] - 2023 $startstats[$applicationid]['stores']['cachestore_file']['sets']); 2024 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] - 2025 $startstats[$sessionid]['stores']['cachestore_session']['misses']); 2026 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits'] - 2027 $startstats[$sessionid]['stores']['cachestore_session']['hits']); 2028 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['sets'] - 2029 $startstats[$sessionid]['stores']['cachestore_session']['sets']); 2030 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] - 2031 $startstats[$requestid]['stores']['cachestore_static']['misses']); 2032 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits'] - 2033 $startstats[$requestid]['stores']['cachestore_static']['hits']); 2034 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['sets'] - 2035 $startstats[$requestid]['stores']['cachestore_static']['sets']); 2036 2037 $startstats = cache_helper::get_stats(); 2038 2039 // Check that stores register hits. 2040 $this->assertEquals($application->get('setMe1'), 1); 2041 $this->assertEquals($application->get('setMe2'), 2); 2042 $this->assertEquals($session->get('setMe1'), 1); 2043 $this->assertEquals($session->get('setMe2'), 2); 2044 $this->assertEquals($session->get('setMe3'), 3); 2045 $this->assertEquals($request->get('setMe1'), 1); 2046 $this->assertEquals($request->get('setMe2'), 2); 2047 $this->assertEquals($request->get('setMe3'), 3); 2048 $this->assertEquals($request->get('setMe4'), 4); 2049 2050 $endstats = cache_helper::get_stats(); 2051 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] - 2052 $startstats[$applicationid]['stores']['cachestore_file']['misses']); 2053 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] - 2054 $startstats[$applicationid]['stores']['cachestore_file']['hits']); 2055 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] - 2056 $startstats[$applicationid]['stores']['cachestore_file']['sets']); 2057 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] - 2058 $startstats[$sessionid]['stores']['cachestore_session']['misses']); 2059 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] - 2060 $startstats[$sessionid]['stores']['cachestore_session']['hits']); 2061 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] - 2062 $startstats[$sessionid]['stores']['cachestore_session']['sets']); 2063 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] - 2064 $startstats[$requestid]['stores']['cachestore_static']['misses']); 2065 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] - 2066 $startstats[$requestid]['stores']['cachestore_static']['hits']); 2067 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] - 2068 $startstats[$requestid]['stores']['cachestore_static']['sets']); 2069 2070 $startstats = cache_helper::get_stats(); 2071 2072 // Check that stores register through get_many. 2073 $application->get_many(array('setMe1', 'setMe2')); 2074 $session->get_many(array('setMe1', 'setMe2', 'setMe3')); 2075 $request->get_many(array('setMe1', 'setMe2', 'setMe3', 'setMe4')); 2076 2077 $endstats = cache_helper::get_stats(); 2078 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] - 2079 $startstats[$applicationid]['stores']['cachestore_file']['misses']); 2080 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] - 2081 $startstats[$applicationid]['stores']['cachestore_file']['hits']); 2082 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] - 2083 $startstats[$applicationid]['stores']['cachestore_file']['sets']); 2084 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] - 2085 $startstats[$sessionid]['stores']['cachestore_session']['misses']); 2086 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] - 2087 $startstats[$sessionid]['stores']['cachestore_session']['hits']); 2088 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] - 2089 $startstats[$sessionid]['stores']['cachestore_session']['sets']); 2090 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] - 2091 $startstats[$requestid]['stores']['cachestore_static']['misses']); 2092 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] - 2093 $startstats[$requestid]['stores']['cachestore_static']['hits']); 2094 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] - 2095 $startstats[$requestid]['stores']['cachestore_static']['sets']); 2096 } 2097 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |