Moodle release 3.3.8
[moodle.git] / cache / tests / cache_test.php
blob61205574ebd8f6682abc60971de97e33f83b8be5
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.
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/>.
17 /**
18 * PHPunit tests for the cache API
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.
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
29 defined('MOODLE_INTERNAL') || die();
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');
36 /**
37 * PHPunit tests for the cache API
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class core_cache_testcase extends advanced_testcase {
44 /**
45 * Set things back to the default before each test.
47 public function setUp() {
48 parent::setUp();
49 cache_factory::reset();
50 cache_config_testing::create_default_configuration();
53 /**
54 * Final task is to reset the cache system
56 public static function tearDownAfterClass() {
57 parent::tearDownAfterClass();
58 cache_factory::reset();
61 /**
62 * Returns the expected application cache store.
63 * @return string
65 protected function get_expected_application_cache_store() {
66 global $CFG;
67 $expected = 'cachestore_file';
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;
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;
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;
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'];
94 return $expected;
97 /**
98 * Tests cache configuration
100 public function test_cache_config() {
101 global $CFG;
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.');
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.');
116 $instance = cache_config::instance();
117 $this->assertInstanceOf('cache_config_testing', $instance);
119 $this->assertTrue(cache_config_testing::config_file_exists());
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);
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,
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;
155 // Must have the default 3 modes and no more.
156 $this->assertCount(3, $mapping);
157 foreach ($modes as $mode) {
158 $this->assertTrue($mode);
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);
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);
174 * Tests for cache keys that would break on windows.
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
185 $cache = cache::make('phpunit', 'windowskeytest');
186 $this->assertTrue($cache->set('contest', 'test data 1'));
187 $this->assertEquals('test data 1', $cache->get('contest'));
191 * Tests set_identifiers fails post cache creation.
193 * set_identifiers cannot be called after initial cache instantiation, as you need to create a difference cache.
195 public function test_set_identifiers() {
196 $instance = cache_config_testing::instance();
197 $instance->phpunit_add_definition('phpunit/identifier', array(
198 'mode' => cache_store::MODE_APPLICATION,
199 'component' => 'phpunit',
200 'area' => 'identifier',
201 'simplekeys' => true,
202 'simpledata' => true,
203 'staticacceleration' => true
205 $cache = cache::make('phpunit', 'identifier', array('area'));
206 $this->assertTrue($cache->set('contest', 'test data 1'));
207 $this->assertEquals('test data 1', $cache->get('contest'));
209 $this->expectException('coding_exception');
210 $cache->set_identifiers(array());
214 * Tests the default application cache
216 public function test_default_application_cache() {
217 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest');
218 $this->assertInstanceOf('cache_application', $cache);
219 $this->run_on_cache($cache);
221 $instance = cache_config_testing::instance(true);
222 $instance->phpunit_add_definition('phpunit/test_default_application_cache', array(
223 'mode' => cache_store::MODE_APPLICATION,
224 'component' => 'phpunit',
225 'area' => 'test_default_application_cache',
226 'staticacceleration' => true,
227 'staticaccelerationsize' => 1
229 $cache = cache::make('phpunit', 'test_default_application_cache');
230 $this->assertInstanceOf('cache_application', $cache);
231 $this->run_on_cache($cache);
235 * Tests the default session cache
237 public function test_default_session_cache() {
238 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest');
239 $this->assertInstanceOf('cache_session', $cache);
240 $this->run_on_cache($cache);
244 * Tests the default request cache
246 public function test_default_request_cache() {
247 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest');
248 $this->assertInstanceOf('cache_request', $cache);
249 $this->run_on_cache($cache);
253 * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
255 public function test_on_cache_without_store() {
256 $instance = cache_config_testing::instance(true);
257 $instance->phpunit_add_definition('phpunit/nostoretest1', array(
258 'mode' => cache_store::MODE_APPLICATION,
259 'component' => 'phpunit',
260 'area' => 'nostoretest1',
262 $instance->phpunit_add_definition('phpunit/nostoretest2', array(
263 'mode' => cache_store::MODE_APPLICATION,
264 'component' => 'phpunit',
265 'area' => 'nostoretest2',
266 'staticacceleration' => true
268 $instance->phpunit_remove_stores();
270 $cache = cache::make('phpunit', 'nostoretest1');
271 $this->run_on_cache($cache);
273 $cache = cache::make('phpunit', 'nostoretest2');
274 $this->run_on_cache($cache);
278 * Runs a standard series of access and use tests on a cache instance.
280 * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
282 * @param cache_loader $cache
284 protected function run_on_cache(cache_loader $cache) {
285 $key = 'contestkey';
286 $datascalars = array('test data', null);
287 $dataarray = array('contest' => 'data', 'part' => 'two');
288 $dataobject = (object)$dataarray;
290 foreach ($datascalars as $datascalar) {
291 $this->assertTrue($cache->purge());
293 // Check all read methods.
294 $this->assertFalse($cache->get($key));
295 $this->assertFalse($cache->has($key));
296 $result = $cache->get_many(array($key));
297 $this->assertCount(1, $result);
298 $this->assertFalse(reset($result));
299 $this->assertFalse($cache->has_any(array($key)));
300 $this->assertFalse($cache->has_all(array($key)));
302 // Set the data.
303 $this->assertTrue($cache->set($key, $datascalar));
304 // Setting it more than once should be permitted.
305 $this->assertTrue($cache->set($key, $datascalar));
307 // Recheck the read methods.
308 $this->assertEquals($datascalar, $cache->get($key));
309 $this->assertTrue($cache->has($key));
310 $result = $cache->get_many(array($key));
311 $this->assertCount(1, $result);
312 $this->assertEquals($datascalar, reset($result));
313 $this->assertTrue($cache->has_any(array($key)));
314 $this->assertTrue($cache->has_all(array($key)));
316 // Delete it.
317 $this->assertTrue($cache->delete($key));
319 // Check its gone.
320 $this->assertFalse($cache->get($key));
321 $this->assertFalse($cache->has($key));
324 // Test arrays.
325 $this->assertTrue($cache->set($key, $dataarray));
326 $this->assertEquals($dataarray, $cache->get($key));
328 // Test objects.
329 $this->assertTrue($cache->set($key, $dataobject));
330 $this->assertEquals($dataobject, $cache->get($key));
332 $starttime = microtime(true);
333 $specobject = new cache_phpunit_dummy_object('red', 'blue', $starttime);
334 $this->assertTrue($cache->set($key, $specobject));
335 $result = $cache->get($key);
336 $this->assertInstanceOf('cache_phpunit_dummy_object', $result);
337 $this->assertEquals('red_ptc_wfc', $result->property1);
338 $this->assertEquals('blue_ptc_wfc', $result->property2);
339 $this->assertGreaterThan($starttime, $result->propertytime);
341 // Test array of objects.
342 $specobject = new cache_phpunit_dummy_object('red', 'blue', $starttime);
343 $data = new cacheable_object_array(array(
344 clone($specobject),
345 clone($specobject),
346 clone($specobject))
348 $this->assertTrue($cache->set($key, $data));
349 $result = $cache->get($key);
350 $this->assertInstanceOf('cacheable_object_array', $result);
351 $this->assertCount(3, $data);
352 foreach ($result as $item) {
353 $this->assertInstanceOf('cache_phpunit_dummy_object', $item);
354 $this->assertEquals('red_ptc_wfc', $item->property1);
355 $this->assertEquals('blue_ptc_wfc', $item->property2);
356 // Ensure that wake from cache is called in all cases.
357 $this->assertGreaterThan($starttime, $item->propertytime);
360 // Test set many.
361 $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
362 $this->assertEquals('data1', $cache->get('key1'));
363 $this->assertEquals('data2', $cache->get('key2'));
364 $this->assertEquals(null, $cache->get('key3'));
365 $this->assertTrue($cache->delete('key1'));
366 $this->assertTrue($cache->delete('key2'));
367 $this->assertTrue($cache->delete('key3'));
369 $cache->set_many(array(
370 'key1' => array(1, 2, 3),
371 'key2' => array(3, 2, 1),
373 $this->assertInternalType('array', $cache->get('key1'));
374 $this->assertInternalType('array', $cache->get('key2'));
375 $this->assertCount(3, $cache->get('key1'));
376 $this->assertCount(3, $cache->get('key2'));
377 $this->assertInternalType('array', $cache->get_many(array('key1', 'key2')));
378 $this->assertCount(2, $cache->get_many(array('key1', 'key2')));
379 $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
381 // Test delete many.
382 $this->assertTrue($cache->set('key1', 'data1'));
383 $this->assertTrue($cache->set('key2', 'data2'));
384 $this->assertTrue($cache->set('key3', null));
386 $this->assertEquals('data1', $cache->get('key1'));
387 $this->assertEquals('data2', $cache->get('key2'));
388 $this->assertEquals(null, $cache->get('key3'));
390 $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
392 $this->assertFalse($cache->get('key1'));
393 $this->assertFalse($cache->get('key2'));
394 $this->assertFalse($cache->get('key3'));
396 // Quick reference test.
397 $obj = new stdClass;
398 $obj->key = 'value';
399 $ref =& $obj;
400 $this->assertTrue($cache->set('obj', $obj));
402 $obj->key = 'eulav';
403 $var = $cache->get('obj');
404 $this->assertInstanceOf('stdClass', $var);
405 $this->assertEquals('value', $var->key);
407 $ref->key = 'eulav';
408 $var = $cache->get('obj');
409 $this->assertInstanceOf('stdClass', $var);
410 $this->assertEquals('value', $var->key);
412 $this->assertTrue($cache->delete('obj'));
414 // Deep reference test.
415 $obj1 = new stdClass;
416 $obj1->key = 'value';
417 $obj2 = new stdClass;
418 $obj2->key = 'test';
419 $obj3 = new stdClass;
420 $obj3->key = 'pork';
421 $obj1->subobj =& $obj2;
422 $obj2->subobj =& $obj3;
423 $this->assertTrue($cache->set('obj', $obj1));
425 $obj1->key = 'eulav';
426 $obj2->key = 'tset';
427 $obj3->key = 'krop';
428 $var = $cache->get('obj');
429 $this->assertInstanceOf('stdClass', $var);
430 $this->assertEquals('value', $var->key);
431 $this->assertInstanceOf('stdClass', $var->subobj);
432 $this->assertEquals('test', $var->subobj->key);
433 $this->assertInstanceOf('stdClass', $var->subobj->subobj);
434 $this->assertEquals('pork', $var->subobj->subobj->key);
435 $this->assertTrue($cache->delete('obj'));
437 // Death reference test... basically we don't want this to die.
438 $obj = new stdClass;
439 $obj->key = 'value';
440 $obj->self =& $obj;
441 $this->assertTrue($cache->set('obj', $obj));
442 $var = $cache->get('obj');
443 $this->assertInstanceOf('stdClass', $var);
444 $this->assertEquals('value', $var->key);
446 // Reference test after retrieve.
447 $obj = new stdClass;
448 $obj->key = 'value';
449 $this->assertTrue($cache->set('obj', $obj));
451 $var1 = $cache->get('obj');
452 $this->assertInstanceOf('stdClass', $var1);
453 $this->assertEquals('value', $var1->key);
454 $var1->key = 'eulav';
455 $this->assertEquals('eulav', $var1->key);
457 $var2 = $cache->get('obj');
458 $this->assertInstanceOf('stdClass', $var2);
459 $this->assertEquals('value', $var2->key);
461 $this->assertTrue($cache->delete('obj'));
463 // Death reference test on get_many... basically we don't want this to die.
464 $obj = new stdClass;
465 $obj->key = 'value';
466 $obj->self =& $obj;
467 $this->assertEquals(1, $cache->set_many(array('obj' => $obj)));
468 $var = $cache->get_many(array('obj'));
469 $this->assertInstanceOf('stdClass', $var['obj']);
470 $this->assertEquals('value', $var['obj']->key);
472 // Reference test after retrieve.
473 $obj = new stdClass;
474 $obj->key = 'value';
475 $this->assertEquals(1, $cache->set_many(array('obj' => $obj)));
477 $var1 = $cache->get_many(array('obj'));
478 $this->assertInstanceOf('stdClass', $var1['obj']);
479 $this->assertEquals('value', $var1['obj']->key);
480 $var1['obj']->key = 'eulav';
481 $this->assertEquals('eulav', $var1['obj']->key);
483 $var2 = $cache->get_many(array('obj'));
484 $this->assertInstanceOf('stdClass', $var2['obj']);
485 $this->assertEquals('value', $var2['obj']->key);
487 $this->assertTrue($cache->delete('obj'));
489 // Test strictness exceptions.
490 try {
491 $cache->get('exception', MUST_EXIST);
492 $this->fail('Exception expected from cache::get using MUST_EXIST');
493 } catch (Exception $e) {
494 $this->assertTrue(true);
496 try {
497 $cache->get_many(array('exception1', 'exception2'), MUST_EXIST);
498 $this->fail('Exception expected from cache::get_many using MUST_EXIST');
499 } catch (Exception $e) {
500 $this->assertTrue(true);
502 $cache->set('test', 'test');
503 try {
504 $cache->get_many(array('test', 'exception'), MUST_EXIST);
505 $this->fail('Exception expected from cache::get_many using MUST_EXIST');
506 } catch (Exception $e) {
507 $this->assertTrue(true);
512 * Tests a definition using a data loader
514 public function test_definition_data_loader() {
515 $instance = cache_config_testing::instance(true);
516 $instance->phpunit_add_definition('phpunit/datasourcetest', array(
517 'mode' => cache_store::MODE_APPLICATION,
518 'component' => 'phpunit',
519 'area' => 'datasourcetest',
520 'datasource' => 'cache_phpunit_dummy_datasource',
521 'datasourcefile' => 'cache/tests/fixtures/lib.php'
524 $cache = cache::make('phpunit', 'datasourcetest');
525 $this->assertInstanceOf('cache_application', $cache);
527 // Purge it to be sure.
528 $this->assertTrue($cache->purge());
529 // It won't be there yet.
530 $this->assertFalse($cache->has('Test'));
531 // It should load it ;).
532 $this->assertTrue($cache->has('Test', true));
534 // Purge it to be sure.
535 $this->assertTrue($cache->purge());
536 $this->assertEquals('Test has no value really.', $cache->get('Test'));
538 // Test multiple values.
539 $this->assertTrue($cache->purge());
540 $this->assertTrue($cache->set('b', 'B'));
541 $result = $cache->get_many(array('a', 'b', 'c'));
542 $this->assertInternalType('array', $result);
543 $this->assertCount(3, $result);
544 $this->assertArrayHasKey('a', $result);
545 $this->assertArrayHasKey('b', $result);
546 $this->assertArrayHasKey('c', $result);
547 $this->assertEquals('a has no value really.', $result['a']);
548 $this->assertEquals('B', $result['b']);
549 $this->assertEquals('c has no value really.', $result['c']);
553 * Tests a definition using an overridden loader
555 public function test_definition_overridden_loader() {
556 $instance = cache_config_testing::instance(true);
557 $instance->phpunit_add_definition('phpunit/overridetest', array(
558 'mode' => cache_store::MODE_APPLICATION,
559 'component' => 'phpunit',
560 'area' => 'overridetest',
561 'overrideclass' => 'cache_phpunit_dummy_overrideclass',
562 'overrideclassfile' => 'cache/tests/fixtures/lib.php'
564 $cache = cache::make('phpunit', 'overridetest');
565 $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
566 $this->assertInstanceOf('cache_application', $cache);
567 // Purge it to be sure.
568 $this->assertTrue($cache->purge());
569 // It won't be there yet.
570 $this->assertFalse($cache->has('Test'));
571 // Add it.
572 $this->assertTrue($cache->set('Test', 'Test has no value really.'));
573 // Check its there.
574 $this->assertEquals('Test has no value really.', $cache->get('Test'));
578 * Test the mappingsonly setting.
580 public function test_definition_mappings_only() {
581 /** @var cache_config_testing $instance */
582 $instance = cache_config_testing::instance(true);
583 $instance->phpunit_add_definition('phpunit/mappingsonly', array(
584 'mode' => cache_store::MODE_APPLICATION,
585 'component' => 'phpunit',
586 'area' => 'mappingsonly',
587 'mappingsonly' => true
588 ), false);
589 $instance->phpunit_add_definition('phpunit/nonmappingsonly', array(
590 'mode' => cache_store::MODE_APPLICATION,
591 'component' => 'phpunit',
592 'area' => 'nonmappingsonly',
593 'mappingsonly' => false
594 ), false);
596 $cacheonly = cache::make('phpunit', 'mappingsonly');
597 $this->assertInstanceOf('cache_application', $cacheonly);
598 $this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class());
600 $expected = $this->get_expected_application_cache_store();
601 $cachenon = cache::make('phpunit', 'nonmappingsonly');
602 $this->assertInstanceOf('cache_application', $cachenon);
603 $this->assertEquals($expected, $cachenon->phpunit_get_store_class());
607 * Test a very basic definition.
609 public function test_definition() {
610 $instance = cache_config_testing::instance();
611 $instance->phpunit_add_definition('phpunit/test', array(
612 'mode' => cache_store::MODE_APPLICATION,
613 'component' => 'phpunit',
614 'area' => 'test',
616 $cache = cache::make('phpunit', 'test');
618 $this->assertTrue($cache->set('testkey1', 'test data 1'));
619 $this->assertEquals('test data 1', $cache->get('testkey1'));
620 $this->assertTrue($cache->set('testkey2', 'test data 2'));
621 $this->assertEquals('test data 2', $cache->get('testkey2'));
625 * Test a definition using the simple keys.
627 public function test_definition_simplekeys() {
628 $instance = cache_config_testing::instance();
629 $instance->phpunit_add_definition('phpunit/simplekeytest', array(
630 'mode' => cache_store::MODE_APPLICATION,
631 'component' => 'phpunit',
632 'area' => 'simplekeytest',
633 'simplekeys' => true
635 $cache = cache::make('phpunit', 'simplekeytest');
637 $this->assertTrue($cache->set('testkey1', 'test data 1'));
638 $this->assertEquals('test data 1', $cache->get('testkey1'));
639 $this->assertTrue($cache->set('testkey2', 'test data 2'));
640 $this->assertEquals('test data 2', $cache->get('testkey2'));
642 $cache->purge();
644 $this->assertTrue($cache->set('1', 'test data 1'));
645 $this->assertEquals('test data 1', $cache->get('1'));
646 $this->assertTrue($cache->set('2', 'test data 2'));
647 $this->assertEquals('test data 2', $cache->get('2'));
651 * Test a negative TTL on an application cache.
653 public function test_application_ttl_negative() {
654 $instance = cache_config_testing::instance(true);
655 $instance->phpunit_add_definition('phpunit/ttltest', array(
656 'mode' => cache_store::MODE_APPLICATION,
657 'component' => 'phpunit',
658 'area' => 'ttltest',
659 'ttl' => -86400 // Set to a day in the past to be extra sure.
661 $cache = cache::make('phpunit', 'ttltest');
662 $this->assertInstanceOf('cache_application', $cache);
664 // Purge it to be sure.
665 $this->assertTrue($cache->purge());
666 // It won't be there yet.
667 $this->assertFalse($cache->has('Test'));
668 // Set it now.
669 $this->assertTrue($cache->set('Test', 'Test'));
670 // Check its not there.
671 $this->assertFalse($cache->has('Test'));
672 // Double check by trying to get it.
673 $this->assertFalse($cache->get('Test'));
675 // Test with multiple keys.
676 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
677 $result = $cache->get_many(array('a', 'b', 'c'));
678 $this->assertInternalType('array', $result);
679 $this->assertCount(3, $result);
680 $this->assertArrayHasKey('a', $result);
681 $this->assertArrayHasKey('b', $result);
682 $this->assertArrayHasKey('c', $result);
683 $this->assertFalse($result['a']);
684 $this->assertFalse($result['b']);
685 $this->assertFalse($result['c']);
687 // Test with multiple keys including missing ones.
688 $result = $cache->get_many(array('a', 'c', 'e'));
689 $this->assertInternalType('array', $result);
690 $this->assertCount(3, $result);
691 $this->assertArrayHasKey('a', $result);
692 $this->assertArrayHasKey('c', $result);
693 $this->assertArrayHasKey('e', $result);
694 $this->assertFalse($result['a']);
695 $this->assertFalse($result['c']);
696 $this->assertFalse($result['e']);
700 * Test a positive TTL on an application cache.
702 public function test_application_ttl_positive() {
703 $instance = cache_config_testing::instance(true);
704 $instance->phpunit_add_definition('phpunit/ttltest', array(
705 'mode' => cache_store::MODE_APPLICATION,
706 'component' => 'phpunit',
707 'area' => 'ttltest',
708 'ttl' => 86400 // Set to a day in the future to be extra sure.
710 $cache = cache::make('phpunit', 'ttltest');
711 $this->assertInstanceOf('cache_application', $cache);
713 // Purge it to be sure.
714 $this->assertTrue($cache->purge());
715 // It won't be there yet.
716 $this->assertFalse($cache->has('Test'));
717 // Set it now.
718 $this->assertTrue($cache->set('Test', 'Test'));
719 // Check its there.
720 $this->assertTrue($cache->has('Test'));
721 // Double check by trying to get it.
722 $this->assertEquals('Test', $cache->get('Test'));
724 // Test with multiple keys.
725 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
726 $result = $cache->get_many(array('a', 'b', 'c'));
727 $this->assertInternalType('array', $result);
728 $this->assertCount(3, $result);
729 $this->assertArrayHasKey('a', $result);
730 $this->assertArrayHasKey('b', $result);
731 $this->assertArrayHasKey('c', $result);
732 $this->assertEquals('A', $result['a']);
733 $this->assertEquals('B', $result['b']);
734 $this->assertEquals('C', $result['c']);
736 // Test with multiple keys including missing ones.
737 $result = $cache->get_many(array('a', 'c', 'e'));
738 $this->assertInternalType('array', $result);
739 $this->assertCount(3, $result);
740 $this->assertArrayHasKey('a', $result);
741 $this->assertArrayHasKey('c', $result);
742 $this->assertArrayHasKey('e', $result);
743 $this->assertEquals('A', $result['a']);
744 $this->assertEquals('C', $result['c']);
745 $this->assertEquals(false, $result['e']);
749 * Test a negative TTL on an session cache.
751 public function test_session_ttl_positive() {
752 $instance = cache_config_testing::instance(true);
753 $instance->phpunit_add_definition('phpunit/ttltest', array(
754 'mode' => cache_store::MODE_SESSION,
755 'component' => 'phpunit',
756 'area' => 'ttltest',
757 'ttl' => 86400 // Set to a day in the future to be extra sure.
759 $cache = cache::make('phpunit', 'ttltest');
760 $this->assertInstanceOf('cache_session', $cache);
762 // Purge it to be sure.
763 $this->assertTrue($cache->purge());
764 // It won't be there yet.
765 $this->assertFalse($cache->has('Test'));
766 // Set it now.
767 $this->assertTrue($cache->set('Test', 'Test'));
768 // Check its there.
769 $this->assertTrue($cache->has('Test'));
770 // Double check by trying to get it.
771 $this->assertEquals('Test', $cache->get('Test'));
773 // Test with multiple keys.
774 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
775 $result = $cache->get_many(array('a', 'b', 'c'));
776 $this->assertInternalType('array', $result);
777 $this->assertCount(3, $result);
778 $this->assertArrayHasKey('a', $result);
779 $this->assertArrayHasKey('b', $result);
780 $this->assertArrayHasKey('c', $result);
781 $this->assertEquals('A', $result['a']);
782 $this->assertEquals('B', $result['b']);
783 $this->assertEquals('C', $result['c']);
785 // Test with multiple keys including missing ones.
786 $result = $cache->get_many(array('a', 'c', 'e'));
787 $this->assertInternalType('array', $result);
788 $this->assertCount(3, $result);
789 $this->assertArrayHasKey('a', $result);
790 $this->assertArrayHasKey('c', $result);
791 $this->assertArrayHasKey('e', $result);
792 $this->assertEquals('A', $result['a']);
793 $this->assertEquals('C', $result['c']);
794 $this->assertEquals(false, $result['e']);
798 * Tests manual locking operations on an application cache
800 public function test_application_manual_locking() {
801 $instance = cache_config_testing::instance();
802 $instance->phpunit_add_definition('phpunit/lockingtest', array(
803 'mode' => cache_store::MODE_APPLICATION,
804 'component' => 'phpunit',
805 'area' => 'lockingtest'
807 $cache1 = cache::make('phpunit', 'lockingtest');
808 $cache2 = clone($cache1);
810 $this->assertTrue($cache1->set('testkey', 'test data'));
811 $this->assertTrue($cache2->set('testkey', 'test data'));
813 $this->assertTrue($cache1->acquire_lock('testkey'));
814 $this->assertFalse($cache2->acquire_lock('testkey'));
816 $this->assertTrue($cache1->check_lock_state('testkey'));
817 $this->assertFalse($cache2->check_lock_state('testkey'));
819 $this->assertTrue($cache1->release_lock('testkey'));
820 $this->assertFalse($cache2->release_lock('testkey'));
822 $this->assertTrue($cache1->set('testkey', 'test data'));
823 $this->assertTrue($cache2->set('testkey', 'test data'));
827 * Tests application cache event invalidation
829 public function test_application_event_invalidation() {
830 $instance = cache_config_testing::instance();
831 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
832 'mode' => cache_store::MODE_APPLICATION,
833 'component' => 'phpunit',
834 'area' => 'eventinvalidationtest',
835 'invalidationevents' => array(
836 'crazyevent'
839 $cache = cache::make('phpunit', 'eventinvalidationtest');
841 $this->assertTrue($cache->set('testkey1', 'test data 1'));
842 $this->assertEquals('test data 1', $cache->get('testkey1'));
843 $this->assertTrue($cache->set('testkey2', 'test data 2'));
844 $this->assertEquals('test data 2', $cache->get('testkey2'));
846 // Test invalidating a single entry.
847 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
849 $this->assertFalse($cache->get('testkey1'));
850 $this->assertEquals('test data 2', $cache->get('testkey2'));
852 $this->assertTrue($cache->set('testkey1', 'test data 1'));
854 // Test invalidating both entries.
855 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
857 $this->assertFalse($cache->get('testkey1'));
858 $this->assertFalse($cache->get('testkey2'));
862 * Tests session cache event invalidation
864 public function test_session_event_invalidation() {
865 $instance = cache_config_testing::instance();
866 $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array(
867 'mode' => cache_store::MODE_SESSION,
868 'component' => 'phpunit',
869 'area' => 'test_session_event_invalidation',
870 'invalidationevents' => array(
871 'crazyevent'
874 $cache = cache::make('phpunit', 'test_session_event_invalidation');
875 $this->assertInstanceOf('cache_session', $cache);
877 $this->assertTrue($cache->set('testkey1', 'test data 1'));
878 $this->assertEquals('test data 1', $cache->get('testkey1'));
879 $this->assertTrue($cache->set('testkey2', 'test data 2'));
880 $this->assertEquals('test data 2', $cache->get('testkey2'));
882 // Test invalidating a single entry.
883 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
885 $this->assertFalse($cache->get('testkey1'));
886 $this->assertEquals('test data 2', $cache->get('testkey2'));
888 $this->assertTrue($cache->set('testkey1', 'test data 1'));
890 // Test invalidating both entries.
891 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
893 $this->assertFalse($cache->get('testkey1'));
894 $this->assertFalse($cache->get('testkey2'));
898 * Tests application cache definition invalidation
900 public function test_application_definition_invalidation() {
901 $instance = cache_config_testing::instance();
902 $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
903 'mode' => cache_store::MODE_APPLICATION,
904 'component' => 'phpunit',
905 'area' => 'definitioninvalidation'
907 $cache = cache::make('phpunit', 'definitioninvalidation');
908 $this->assertTrue($cache->set('testkey1', 'test data 1'));
909 $this->assertEquals('test data 1', $cache->get('testkey1'));
910 $this->assertTrue($cache->set('testkey2', 'test data 2'));
911 $this->assertEquals('test data 2', $cache->get('testkey2'));
913 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
915 $this->assertFalse($cache->get('testkey1'));
916 $this->assertEquals('test data 2', $cache->get('testkey2'));
918 $this->assertTrue($cache->set('testkey1', 'test data 1'));
920 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
922 $this->assertFalse($cache->get('testkey1'));
923 $this->assertEquals('test data 2', $cache->get('testkey2'));
925 $this->assertTrue($cache->set('testkey1', 'test data 1'));
927 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
929 $this->assertFalse($cache->get('testkey1'));
930 $this->assertFalse($cache->get('testkey2'));
934 * Tests session cache definition invalidation
936 public function test_session_definition_invalidation() {
937 $instance = cache_config_testing::instance();
938 $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array(
939 'mode' => cache_store::MODE_SESSION,
940 'component' => 'phpunit',
941 'area' => 'test_session_definition_invalidation'
943 $cache = cache::make('phpunit', 'test_session_definition_invalidation');
944 $this->assertInstanceOf('cache_session', $cache);
945 $this->assertTrue($cache->set('testkey1', 'test data 1'));
946 $this->assertEquals('test data 1', $cache->get('testkey1'));
947 $this->assertTrue($cache->set('testkey2', 'test data 2'));
948 $this->assertEquals('test data 2', $cache->get('testkey2'));
950 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 'testkey1');
952 $this->assertFalse($cache->get('testkey1'));
953 $this->assertEquals('test data 2', $cache->get('testkey2'));
955 $this->assertTrue($cache->set('testkey1', 'test data 1'));
957 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
958 array('testkey1'));
960 $this->assertFalse($cache->get('testkey1'));
961 $this->assertEquals('test data 2', $cache->get('testkey2'));
963 $this->assertTrue($cache->set('testkey1', 'test data 1'));
965 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
966 array('testkey1', 'testkey2'));
968 $this->assertFalse($cache->get('testkey1'));
969 $this->assertFalse($cache->get('testkey2'));
973 * Tests application cache event invalidation over a distributed setup.
975 public function test_distributed_application_event_invalidation() {
976 global $CFG;
977 // This is going to be an intense wee test.
978 // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
979 // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
980 // check that it is not picked up.
981 $instance = cache_config_testing::instance();
982 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
983 'mode' => cache_store::MODE_APPLICATION,
984 'component' => 'phpunit',
985 'area' => 'eventinvalidationtest',
986 'simplekeys' => true,
987 'simpledata' => true,
988 'invalidationevents' => array(
989 'crazyevent'
992 $cache = cache::make('phpunit', 'eventinvalidationtest');
993 $this->assertTrue($cache->set('testkey1', 'test data 1'));
994 $this->assertEquals('test data 1', $cache->get('testkey1'));
996 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
998 $this->assertFalse($cache->get('testkey1'));
1000 // OK data added, data invalidated, and invalidation time has been set.
1001 // Now we need to manually add back the data and adjust the invalidation time.
1002 $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit');
1003 $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
1004 // Make sure the file is correct.
1005 $this->assertTrue(file_exists($timefile));
1006 $timecont = serialize(cache::now(true) - 60); // Back 60sec in the past to force it to re-invalidate.
1007 make_writable_directory(dirname($timefile));
1008 file_put_contents($timefile, $timecont);
1009 $this->assertTrue(file_exists($timefile));
1011 $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache";
1012 $datacont = serialize("test data 1");
1013 make_writable_directory(dirname($datafile));
1014 file_put_contents($datafile, $datacont);
1015 $this->assertTrue(file_exists($datafile));
1017 // Test 1: Rebuild without the event and test its there.
1018 cache_factory::reset();
1019 $instance = cache_config_testing::instance();
1020 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
1021 'mode' => cache_store::MODE_APPLICATION,
1022 'component' => 'phpunit',
1023 'area' => 'eventinvalidationtest',
1024 'simplekeys' => true,
1025 'simpledata' => true,
1027 $cache = cache::make('phpunit', 'eventinvalidationtest');
1028 $this->assertEquals('test data 1', $cache->get('testkey1'));
1030 // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
1031 cache_factory::reset();
1033 $instance = cache_config_testing::instance();
1034 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
1035 'mode' => cache_store::MODE_APPLICATION,
1036 'component' => 'phpunit',
1037 'area' => 'eventinvalidationtest',
1038 'simplekeys' => true,
1039 'simpledata' => true,
1040 'invalidationevents' => array(
1041 'crazyevent'
1045 $cache = cache::make('phpunit', 'eventinvalidationtest');
1046 $this->assertFalse($cache->get('testkey1'));
1048 // Test 3: Verify that an existing lastinvalidation cache file is updated when needed.
1050 // Make a new cache class. This should should invalidate testkey2.
1051 $cache = cache::make('phpunit', 'eventinvalidationtest');
1053 // Invalidation token should have been reset.
1054 $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1056 // Set testkey2 data.
1057 $cache->set('testkey2', 'test data 2');
1059 // Backdate the event invalidation time by 30 seconds.
1060 $invalidationcache = cache::make('core', 'eventinvalidation');
1061 $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 30));
1063 // Lastinvalidation should already be cache::now().
1064 $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1066 // Set it to 15 seconds ago so that we know if it changes.
1067 $pasttime = cache::now(true) - 15;
1068 $cache->set('lastinvalidation', $pasttime);
1070 // Make a new cache class. This should not invalidate anything.
1071 cache_factory::instance()->reset_cache_instances();
1072 $cache = cache::make('phpunit', 'eventinvalidationtest');
1074 // Lastinvalidation shouldn't change since it was already newer than invalidation event.
1075 $this->assertEquals($pasttime, $cache->get('lastinvalidation'));
1077 // Now set the event invalidation to newer than the lastinvalidation time.
1078 $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 5));
1079 // Make a new cache class. This should should invalidate testkey2.
1080 cache_factory::instance()->reset_cache_instances();
1081 $cache = cache::make('phpunit', 'eventinvalidationtest');
1082 // Lastinvalidation timestamp should have updated to cache::now().
1083 $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1085 // Now simulate a purge_by_event 5 seconds ago.
1086 $invalidationcache = cache::make('core', 'eventinvalidation');
1087 $invalidationcache->set('crazyevent', array('purged' => cache::now(true) - 5));
1088 // Set our lastinvalidation timestamp to 15 seconds ago.
1089 $cache->set('lastinvalidation', cache::now(true) - 15);
1090 // Make a new cache class. This should invalidate the cache.
1091 cache_factory::instance()->reset_cache_instances();
1092 $cache = cache::make('phpunit', 'eventinvalidationtest');
1093 // Lastinvalidation timestamp should have updated to cache::now().
1094 $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1099 * Tests application cache event purge
1101 public function test_application_event_purge() {
1102 $instance = cache_config_testing::instance();
1103 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1104 'mode' => cache_store::MODE_APPLICATION,
1105 'component' => 'phpunit',
1106 'area' => 'eventpurgetest',
1107 'invalidationevents' => array(
1108 'crazyevent'
1111 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1112 'mode' => cache_store::MODE_APPLICATION,
1113 'component' => 'phpunit',
1114 'area' => 'eventpurgetestaccelerated',
1115 'staticacceleration' => true,
1116 'invalidationevents' => array(
1117 'crazyevent'
1120 $cache = cache::make('phpunit', 'eventpurgetest');
1122 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1123 $this->assertEquals('test data 1', $cache->get('testkey1'));
1124 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1125 $this->assertEquals('test data 2', $cache->get('testkey2'));
1127 // Purge the event.
1128 cache_helper::purge_by_event('crazyevent');
1130 // Check things have been removed.
1131 $this->assertFalse($cache->get('testkey1'));
1132 $this->assertFalse($cache->get('testkey2'));
1134 // Now test the static acceleration array.
1135 $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1136 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1137 $this->assertEquals('test data 1', $cache->get('testkey1'));
1138 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1139 $this->assertEquals('test data 2', $cache->get('testkey2'));
1141 // Purge the event.
1142 cache_helper::purge_by_event('crazyevent');
1144 // Check things have been removed.
1145 $this->assertFalse($cache->get('testkey1'));
1146 $this->assertFalse($cache->get('testkey2'));
1150 * Tests session cache event purge
1152 public function test_session_event_purge() {
1153 $instance = cache_config_testing::instance();
1154 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1155 'mode' => cache_store::MODE_SESSION,
1156 'component' => 'phpunit',
1157 'area' => 'eventpurgetest',
1158 'invalidationevents' => array(
1159 'crazyevent'
1162 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1163 'mode' => cache_store::MODE_SESSION,
1164 'component' => 'phpunit',
1165 'area' => 'eventpurgetestaccelerated',
1166 'staticacceleration' => true,
1167 'invalidationevents' => array(
1168 'crazyevent'
1171 $cache = cache::make('phpunit', 'eventpurgetest');
1173 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1174 $this->assertEquals('test data 1', $cache->get('testkey1'));
1175 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1176 $this->assertEquals('test data 2', $cache->get('testkey2'));
1178 // Purge the event.
1179 cache_helper::purge_by_event('crazyevent');
1181 // Check things have been removed.
1182 $this->assertFalse($cache->get('testkey1'));
1183 $this->assertFalse($cache->get('testkey2'));
1185 // Now test the static acceleration array.
1186 $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1187 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1188 $this->assertEquals('test data 1', $cache->get('testkey1'));
1189 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1190 $this->assertEquals('test data 2', $cache->get('testkey2'));
1192 // Purge the event.
1193 cache_helper::purge_by_event('crazyevent');
1195 // Check things have been removed.
1196 $this->assertFalse($cache->get('testkey1'));
1197 $this->assertFalse($cache->get('testkey2'));
1201 * Tests application cache definition purge
1203 public function test_application_definition_purge() {
1204 $instance = cache_config_testing::instance();
1205 $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
1206 'mode' => cache_store::MODE_APPLICATION,
1207 'component' => 'phpunit',
1208 'area' => 'definitionpurgetest',
1209 'invalidationevents' => array(
1210 'crazyevent'
1213 $cache = cache::make('phpunit', 'definitionpurgetest');
1215 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1216 $this->assertEquals('test data 1', $cache->get('testkey1'));
1217 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1218 $this->assertEquals('test data 2', $cache->get('testkey2'));
1220 // Purge the event.
1221 cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
1223 // Check things have been removed.
1224 $this->assertFalse($cache->get('testkey1'));
1225 $this->assertFalse($cache->get('testkey2'));
1229 * Test the use of an alt path.
1230 * If we can generate a config instance we are done :)
1232 public function test_alt_cache_path() {
1233 global $CFG;
1234 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1235 $this->markTestSkipped('Skipped testing alt cache path as it is already being used.');
1237 $this->resetAfterTest();
1238 $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
1239 $instance = cache_config_testing::instance();
1240 $this->assertInstanceOf('cache_config', $instance);
1244 * Test disabling the cache stores.
1246 public function test_disable_stores() {
1247 $instance = cache_config_testing::instance();
1248 $instance->phpunit_add_definition('phpunit/disabletest1', array(
1249 'mode' => cache_store::MODE_APPLICATION,
1250 'component' => 'phpunit',
1251 'area' => 'disabletest1'
1253 $instance->phpunit_add_definition('phpunit/disabletest2', array(
1254 'mode' => cache_store::MODE_SESSION,
1255 'component' => 'phpunit',
1256 'area' => 'disabletest2'
1258 $instance->phpunit_add_definition('phpunit/disabletest3', array(
1259 'mode' => cache_store::MODE_REQUEST,
1260 'component' => 'phpunit',
1261 'area' => 'disabletest3'
1264 $caches = array(
1265 'disabletest1' => cache::make('phpunit', 'disabletest1'),
1266 'disabletest2' => cache::make('phpunit', 'disabletest2'),
1267 'disabletest3' => cache::make('phpunit', 'disabletest3')
1270 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1271 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1272 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1274 $this->assertEquals('cachestore_file', $caches['disabletest1']->phpunit_get_store_class());
1275 $this->assertEquals('cachestore_session', $caches['disabletest2']->phpunit_get_store_class());
1276 $this->assertEquals('cachestore_static', $caches['disabletest3']->phpunit_get_store_class());
1278 foreach ($caches as $cache) {
1279 $this->assertFalse($cache->get('test'));
1280 $this->assertTrue($cache->set('test', 'test'));
1281 $this->assertEquals('test', $cache->get('test'));
1284 cache_factory::disable_stores();
1286 $caches = array(
1287 'disabletest1' => cache::make('phpunit', 'disabletest1'),
1288 'disabletest2' => cache::make('phpunit', 'disabletest2'),
1289 'disabletest3' => cache::make('phpunit', 'disabletest3')
1292 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1293 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1294 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1296 $this->assertEquals('cachestore_dummy', $caches['disabletest1']->phpunit_get_store_class());
1297 $this->assertEquals('cachestore_dummy', $caches['disabletest2']->phpunit_get_store_class());
1298 $this->assertEquals('cachestore_dummy', $caches['disabletest3']->phpunit_get_store_class());
1300 foreach ($caches as $cache) {
1301 $this->assertFalse($cache->get('test'));
1302 $this->assertTrue($cache->set('test', 'test'));
1303 $this->assertEquals('test', $cache->get('test'));
1308 * Test disabling the cache.
1310 public function test_disable() {
1311 global $CFG;
1313 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1314 // We can't run this test as it requires us to delete the cache configuration script which we just
1315 // cant do with a custom path in play.
1316 $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
1319 $configfile = $CFG->dataroot.'/muc/config.php';
1321 // The config file will not exist yet as we've not done anything with the cache.
1322 // reset_all_data removes the file and without a call to create a configuration it doesn't exist
1323 // as yet.
1324 $this->assertFileNotExists($configfile);
1326 // Disable the cache
1327 cache_phpunit_factory::phpunit_disable();
1329 // Check we get the expected disabled factory.
1330 $factory = cache_factory::instance();
1331 $this->assertInstanceOf('cache_factory_disabled', $factory);
1333 // Check we get the expected disabled config.
1334 $config = $factory->create_config_instance();
1335 $this->assertInstanceOf('cache_config_disabled', $config);
1337 // Check we get the expected disabled caches.
1338 $cache = cache::make('phpunit', 'disable');
1339 $this->assertInstanceOf('cache_disabled', $cache);
1341 // Test an application cache.
1342 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
1343 $this->assertInstanceOf('cache_disabled', $cache);
1345 $this->assertFalse(file_exists($configfile));
1347 $this->assertFalse($cache->get('test'));
1348 $this->assertFalse($cache->set('test', 'test'));
1349 $this->assertFalse($cache->delete('test'));
1350 $this->assertTrue($cache->purge());
1352 // Test a session cache.
1353 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable');
1354 $this->assertInstanceOf('cache_disabled', $cache);
1356 $this->assertFalse(file_exists($configfile));
1358 $this->assertFalse($cache->get('test'));
1359 $this->assertFalse($cache->set('test', 'test'));
1360 $this->assertFalse($cache->delete('test'));
1361 $this->assertTrue($cache->purge());
1363 // Finally test a request cache.
1364 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable');
1365 $this->assertInstanceOf('cache_disabled', $cache);
1367 $this->assertFalse(file_exists($configfile));
1369 $this->assertFalse($cache->get('test'));
1370 $this->assertFalse($cache->set('test', 'test'));
1371 $this->assertFalse($cache->delete('test'));
1372 $this->assertTrue($cache->purge());
1374 cache_factory::reset();
1376 $factory = cache_factory::instance(true);
1377 $config = $factory->create_config_instance();
1378 $this->assertEquals('cache_config_testing', get_class($config));
1382 * Test that multiple application loaders work ok.
1384 public function test_multiple_application_loaders() {
1385 $instance = cache_config_testing::instance(true);
1386 $instance->phpunit_add_file_store('phpunittest1');
1387 $instance->phpunit_add_file_store('phpunittest2');
1388 $instance->phpunit_add_definition('phpunit/multi_loader', array(
1389 'mode' => cache_store::MODE_APPLICATION,
1390 'component' => 'phpunit',
1391 'area' => 'multi_loader'
1393 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1394 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1396 $cache = cache::make('phpunit', 'multi_loader');
1397 $this->assertInstanceOf('cache_application', $cache);
1398 $this->assertFalse($cache->get('test'));
1399 $this->assertTrue($cache->set('test', 'test'));
1400 $this->assertEquals('test', $cache->get('test'));
1401 $this->assertTrue($cache->delete('test'));
1402 $this->assertFalse($cache->get('test'));
1403 $this->assertTrue($cache->set('test', 'test'));
1404 $this->assertTrue($cache->purge());
1405 $this->assertFalse($cache->get('test'));
1407 // Test the many commands.
1408 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1409 $result = $cache->get_many(array('a', 'b', 'c'));
1410 $this->assertInternalType('array', $result);
1411 $this->assertCount(3, $result);
1412 $this->assertArrayHasKey('a', $result);
1413 $this->assertArrayHasKey('b', $result);
1414 $this->assertArrayHasKey('c', $result);
1415 $this->assertEquals('A', $result['a']);
1416 $this->assertEquals('B', $result['b']);
1417 $this->assertEquals('C', $result['c']);
1418 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1419 $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1420 $result = $cache->get_many(array('a', 'b', 'c'));
1421 $this->assertInternalType('array', $result);
1422 $this->assertCount(3, $result);
1423 $this->assertArrayHasKey('a', $result);
1424 $this->assertArrayHasKey('b', $result);
1425 $this->assertArrayHasKey('c', $result);
1426 $this->assertFalse($result['a']);
1427 $this->assertEquals('B', $result['b']);
1428 $this->assertFalse($result['c']);
1430 // Test non-recursive deletes.
1431 $this->assertTrue($cache->set('test', 'test'));
1432 $this->assertSame('test', $cache->get('test'));
1433 $this->assertTrue($cache->delete('test', false));
1434 // We should still have it on a deeper loader.
1435 $this->assertSame('test', $cache->get('test'));
1436 // Test non-recusive with many functions.
1437 $this->assertSame(3, $cache->set_many(array(
1438 'one' => 'one',
1439 'two' => 'two',
1440 'three' => 'three'
1441 )));
1442 $this->assertSame('one', $cache->get('one'));
1443 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1444 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1445 $this->assertSame('one', $cache->get('one'));
1446 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1450 * Test that multiple application loaders work ok.
1452 public function test_multiple_session_loaders() {
1453 /* @var cache_config_testing $instance */
1454 $instance = cache_config_testing::instance(true);
1455 $instance->phpunit_add_session_store('phpunittest1');
1456 $instance->phpunit_add_session_store('phpunittest2');
1457 $instance->phpunit_add_definition('phpunit/multi_loader', array(
1458 'mode' => cache_store::MODE_SESSION,
1459 'component' => 'phpunit',
1460 'area' => 'multi_loader'
1462 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1463 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1465 $cache = cache::make('phpunit', 'multi_loader');
1466 $this->assertInstanceOf('cache_session', $cache);
1467 $this->assertFalse($cache->get('test'));
1468 $this->assertTrue($cache->set('test', 'test'));
1469 $this->assertEquals('test', $cache->get('test'));
1470 $this->assertTrue($cache->delete('test'));
1471 $this->assertFalse($cache->get('test'));
1472 $this->assertTrue($cache->set('test', 'test'));
1473 $this->assertTrue($cache->purge());
1474 $this->assertFalse($cache->get('test'));
1476 // Test the many commands.
1477 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1478 $result = $cache->get_many(array('a', 'b', 'c'));
1479 $this->assertInternalType('array', $result);
1480 $this->assertCount(3, $result);
1481 $this->assertArrayHasKey('a', $result);
1482 $this->assertArrayHasKey('b', $result);
1483 $this->assertArrayHasKey('c', $result);
1484 $this->assertEquals('A', $result['a']);
1485 $this->assertEquals('B', $result['b']);
1486 $this->assertEquals('C', $result['c']);
1487 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1488 $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1489 $result = $cache->get_many(array('a', 'b', 'c'));
1490 $this->assertInternalType('array', $result);
1491 $this->assertCount(3, $result);
1492 $this->assertArrayHasKey('a', $result);
1493 $this->assertArrayHasKey('b', $result);
1494 $this->assertArrayHasKey('c', $result);
1495 $this->assertFalse($result['a']);
1496 $this->assertEquals('B', $result['b']);
1497 $this->assertFalse($result['c']);
1499 // Test non-recursive deletes.
1500 $this->assertTrue($cache->set('test', 'test'));
1501 $this->assertSame('test', $cache->get('test'));
1502 $this->assertTrue($cache->delete('test', false));
1503 // We should still have it on a deeper loader.
1504 $this->assertSame('test', $cache->get('test'));
1505 // Test non-recusive with many functions.
1506 $this->assertSame(3, $cache->set_many(array(
1507 'one' => 'one',
1508 'two' => 'two',
1509 'three' => 'three'
1510 )));
1511 $this->assertSame('one', $cache->get('one'));
1512 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1513 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1514 $this->assertSame('one', $cache->get('one'));
1515 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1519 * Test switching users with session caches.
1521 public function test_session_cache_switch_user() {
1522 $this->resetAfterTest(true);
1523 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache');
1524 $user1 = $this->getDataGenerator()->create_user();
1525 $user2 = $this->getDataGenerator()->create_user();
1527 // Log in as the first user.
1528 $this->setUser($user1);
1529 $sesskey1 = sesskey();
1531 // Set a basic value in the cache.
1532 $cache->set('var', 1);
1533 $this->assertTrue($cache->has('var'));
1534 $this->assertEquals(1, $cache->get('var'));
1536 // Change to the second user.
1537 $this->setUser($user2);
1538 $sesskey2 = sesskey();
1540 // Make sure the cache doesn't give us the data for the last user.
1541 $this->assertNotEquals($sesskey1, $sesskey2);
1542 $this->assertFalse($cache->has('var'));
1543 $this->assertEquals(false, $cache->get('var'));
1547 * Test switching users with session caches.
1549 public function test_session_cache_switch_user_application_mapping() {
1550 $this->resetAfterTest(true);
1551 $instance = cache_config_testing::instance(true);
1552 $instance->phpunit_add_file_store('testfilestore');
1553 $instance->phpunit_add_definition('phpunit/testappsession', array(
1554 'mode' => cache_store::MODE_SESSION,
1555 'component' => 'phpunit',
1556 'area' => 'testappsession'
1558 $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3);
1559 $cache = cache::make('phpunit', 'testappsession');
1560 $user1 = $this->getDataGenerator()->create_user();
1561 $user2 = $this->getDataGenerator()->create_user();
1563 // Log in as the first user.
1564 $this->setUser($user1);
1565 $sesskey1 = sesskey();
1567 // Set a basic value in the cache.
1568 $cache->set('var', 1);
1569 $this->assertTrue($cache->has('var'));
1570 $this->assertEquals(1, $cache->get('var'));
1572 // Change to the second user.
1573 $this->setUser($user2);
1574 $sesskey2 = sesskey();
1576 // Make sure the cache doesn't give us the data for the last user.
1577 $this->assertNotEquals($sesskey1, $sesskey2);
1578 $this->assertFalse($cache->has('var'));
1579 $this->assertEquals(false, $cache->get('var'));
1583 * Test two session caches being used at once to confirm collisions don't occur.
1585 public function test_dual_session_caches() {
1586 $instance = cache_config_testing::instance(true);
1587 $instance->phpunit_add_definition('phpunit/testsess1', array(
1588 'mode' => cache_store::MODE_SESSION,
1589 'component' => 'phpunit',
1590 'area' => 'testsess1'
1592 $instance->phpunit_add_definition('phpunit/testsess2', array(
1593 'mode' => cache_store::MODE_SESSION,
1594 'component' => 'phpunit',
1595 'area' => 'testsess2'
1597 $cache1 = cache::make('phpunit', 'testsess1');
1598 $cache2 = cache::make('phpunit', 'testsess2');
1600 $this->assertFalse($cache1->has('test'));
1601 $this->assertFalse($cache2->has('test'));
1603 $this->assertTrue($cache1->set('test', '1'));
1605 $this->assertTrue($cache1->has('test'));
1606 $this->assertFalse($cache2->has('test'));
1608 $this->assertTrue($cache2->set('test', '2'));
1610 $this->assertEquals(1, $cache1->get('test'));
1611 $this->assertEquals(2, $cache2->get('test'));
1613 $this->assertTrue($cache1->delete('test'));
1617 * Test multiple session caches when switching user.
1619 public function test_session_cache_switch_user_multiple() {
1620 $this->resetAfterTest(true);
1621 $cache1 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache1');
1622 $cache2 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache2');
1623 $user1 = $this->getDataGenerator()->create_user();
1624 $user2 = $this->getDataGenerator()->create_user();
1626 // Log in as the first user.
1627 $this->setUser($user1);
1628 $sesskey1 = sesskey();
1630 // Set a basic value in the caches.
1631 $cache1->set('var', 1);
1632 $cache2->set('var', 2);
1633 $this->assertEquals(1, $cache1->get('var'));
1634 $this->assertEquals(2, $cache2->get('var'));
1636 // Change to the second user.
1637 $this->setUser($user2);
1638 $sesskey2 = sesskey();
1640 // Make sure the cache doesn't give us the data for the last user.
1641 // Also make sure that switching the user has lead to both caches being purged.
1642 $this->assertNotEquals($sesskey1, $sesskey2);
1643 $this->assertEquals(false, $cache1->get('var'));
1644 $this->assertEquals(false, $cache2->get('var'));
1648 * Test application locking.
1650 public function test_application_locking() {
1651 $instance = cache_config_testing::instance(true);
1652 $instance->phpunit_add_definition('phpunit/test_application_locking', array(
1653 'mode' => cache_store::MODE_APPLICATION,
1654 'component' => 'phpunit',
1655 'area' => 'test_application_locking',
1656 'staticacceleration' => true,
1657 'staticaccelerationsize' => 1,
1658 'requirelockingread' => true,
1659 'requirelockingwrite' => true
1661 $cache = cache::make('phpunit', 'test_application_locking');
1662 $this->assertInstanceOf('cache_application', $cache);
1664 $this->assertTrue($cache->set('a', 'A'));
1665 $this->assertTrue($cache->set('b', 'B'));
1666 $this->assertTrue($cache->set('c', 'C'));
1667 $this->assertEquals('A', $cache->get('a'));
1668 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1669 $this->assertTrue($cache->delete('a'));
1670 $this->assertFalse($cache->has('a'));
1674 * Test the static cache_helper method purge_stores_used_by_definition.
1676 public function test_purge_stores_used_by_definition() {
1677 $instance = cache_config_testing::instance(true);
1678 $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', array(
1679 'mode' => cache_store::MODE_APPLICATION,
1680 'component' => 'phpunit',
1681 'area' => 'test_purge_stores_used_by_definition'
1683 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1684 $this->assertInstanceOf('cache_application', $cache);
1685 $this->assertTrue($cache->set('test', 'test'));
1686 unset($cache);
1688 cache_helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition');
1690 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1691 $this->assertInstanceOf('cache_application', $cache);
1692 $this->assertFalse($cache->get('test'));
1696 * Test purge routines.
1698 public function test_purge_routines() {
1699 $instance = cache_config_testing::instance(true);
1700 $instance->phpunit_add_definition('phpunit/purge1', array(
1701 'mode' => cache_store::MODE_APPLICATION,
1702 'component' => 'phpunit',
1703 'area' => 'purge1'
1705 $instance->phpunit_add_definition('phpunit/purge2', array(
1706 'mode' => cache_store::MODE_APPLICATION,
1707 'component' => 'phpunit',
1708 'area' => 'purge2',
1709 'requireidentifiers' => array(
1710 'id'
1714 $factory = cache_factory::instance();
1715 $definition = $factory->create_definition('phpunit', 'purge1');
1716 $this->assertFalse($definition->has_required_identifiers());
1717 $cache = $factory->create_cache($definition);
1718 $this->assertInstanceOf('cache_application', $cache);
1719 $this->assertTrue($cache->set('test', 'test'));
1720 $this->assertTrue($cache->has('test'));
1721 cache_helper::purge_by_definition('phpunit', 'purge1');
1722 $this->assertFalse($cache->has('test'));
1724 $factory = cache_factory::instance();
1725 $definition = $factory->create_definition('phpunit', 'purge2');
1726 $this->assertTrue($definition->has_required_identifiers());
1727 $cache = $factory->create_cache($definition);
1728 $this->assertInstanceOf('cache_application', $cache);
1729 $this->assertTrue($cache->set('test', 'test'));
1730 $this->assertTrue($cache->has('test'));
1731 cache_helper::purge_stores_used_by_definition('phpunit', 'purge2');
1732 $this->assertFalse($cache->has('test'));
1734 try {
1735 cache_helper::purge_by_definition('phpunit', 'purge2');
1736 $this->fail('Should not be able to purge a definition required identifiers without providing them.');
1737 } catch (coding_exception $ex) {
1738 $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage());
1743 * Test that the default stores all support searching.
1745 public function test_defaults_support_searching() {
1746 $instance = cache_config_testing::instance(true);
1747 $instance->phpunit_add_definition('phpunit/search1', array(
1748 'mode' => cache_store::MODE_APPLICATION,
1749 'component' => 'phpunit',
1750 'area' => 'search1',
1751 'requiresearchable' => true
1753 $instance->phpunit_add_definition('phpunit/search2', array(
1754 'mode' => cache_store::MODE_SESSION,
1755 'component' => 'phpunit',
1756 'area' => 'search2',
1757 'requiresearchable' => true
1759 $instance->phpunit_add_definition('phpunit/search3', array(
1760 'mode' => cache_store::MODE_REQUEST,
1761 'component' => 'phpunit',
1762 'area' => 'search3',
1763 'requiresearchable' => true
1765 $factory = cache_factory::instance();
1767 // Test application cache is searchable.
1768 $definition = $factory->create_definition('phpunit', 'search1');
1769 $this->assertInstanceOf('cache_definition', $definition);
1770 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1771 $cache = $factory->create_cache($definition);
1772 $this->assertInstanceOf('cache_application', $cache);
1773 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1775 // Test session cache is searchable.
1776 $definition = $factory->create_definition('phpunit', 'search2');
1777 $this->assertInstanceOf('cache_definition', $definition);
1778 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1779 $cache = $factory->create_cache($definition);
1780 $this->assertInstanceOf('cache_session', $cache);
1781 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1783 // Test request cache is searchable.
1784 $definition = $factory->create_definition('phpunit', 'search3');
1785 $this->assertInstanceOf('cache_definition', $definition);
1786 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1787 $cache = $factory->create_cache($definition);
1788 $this->assertInstanceOf('cache_request', $cache);
1789 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1793 * Test static acceleration
1795 * Note: All the assertGreaterThanOrEqual() in this test should be assertGreaterThan() be because of some microtime()
1796 * resolution problems under some OSs / PHP versions, we are accepting equal as valid outcome. For more info see MDL-57147.
1798 public function test_static_acceleration() {
1799 $instance = cache_config_testing::instance();
1800 $instance->phpunit_add_definition('phpunit/accelerated', array(
1801 'mode' => cache_store::MODE_APPLICATION,
1802 'component' => 'phpunit',
1803 'area' => 'accelerated',
1804 'staticacceleration' => true,
1805 'staticaccelerationsize' => 3,
1807 $instance->phpunit_add_definition('phpunit/accelerated2', array(
1808 'mode' => cache_store::MODE_APPLICATION,
1809 'component' => 'phpunit',
1810 'area' => 'accelerated2',
1811 'staticacceleration' => true,
1812 'staticaccelerationsize' => 3,
1814 $instance->phpunit_add_definition('phpunit/accelerated3', array(
1815 'mode' => cache_store::MODE_APPLICATION,
1816 'component' => 'phpunit',
1817 'area' => 'accelerated3',
1818 'staticacceleration' => true,
1819 'staticaccelerationsize' => 3,
1821 $instance->phpunit_add_definition('phpunit/accelerated4', array(
1822 'mode' => cache_store::MODE_APPLICATION,
1823 'component' => 'phpunit',
1824 'area' => 'accelerated4',
1825 'staticacceleration' => true,
1826 'staticaccelerationsize' => 4,
1828 $instance->phpunit_add_definition('phpunit/simpledataarea1', array(
1829 'mode' => cache_store::MODE_APPLICATION,
1830 'component' => 'phpunit',
1831 'area' => 'simpledataarea1',
1832 'staticacceleration' => true,
1833 'simpledata' => false
1835 $instance->phpunit_add_definition('phpunit/simpledataarea2', array(
1836 'mode' => cache_store::MODE_APPLICATION,
1837 'component' => 'phpunit',
1838 'area' => 'simpledataarea2',
1839 'staticacceleration' => true,
1840 'simpledata' => true
1843 $cache = cache::make('phpunit', 'accelerated');
1844 $this->assertInstanceOf('cache_phpunit_application', $cache);
1846 // Set and get three elements.
1847 $this->assertTrue($cache->set('a', 'A'));
1848 $this->assertTrue($cache->set('b', 'B'));
1849 $this->assertTrue($cache->set('c', 'C'));
1850 $this->assertEquals('A', $cache->get('a'));
1851 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1853 // Make sure all items are in static acceleration array.
1854 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1855 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1856 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1858 // Add new value and make sure it is in cache and it is in array.
1859 $this->assertTrue($cache->set('d', 'D'));
1860 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1861 $this->assertEquals('D', $cache->get('d'));
1863 // Now the least recent accessed item (a) is no longer in acceleration array.
1864 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1865 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1866 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1868 // Adding and deleting element.
1869 $this->assertTrue($cache->set('a', 'A'));
1870 $this->assertTrue($cache->delete('a'));
1871 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1872 $this->assertFalse($cache->has('a'));
1874 // Make sure "purge" deletes from the array as well.
1875 $cache->purge();
1876 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1877 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1878 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1879 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1880 $this->assertFalse($cache->phpunit_static_acceleration_get('e'));
1882 // Check that the array holds the last accessed items by get/set.
1883 $this->assertTrue($cache->set('a', 'A'));
1884 $this->assertTrue($cache->set('b', 'B'));
1885 $this->assertTrue($cache->set('c', 'C'));
1886 $this->assertTrue($cache->set('d', 'D'));
1887 $this->assertTrue($cache->set('e', 'E'));
1888 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1889 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1890 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1891 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1892 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1894 // Store a cacheable_object, get many times and ensure each time wake_for_cache is used.
1895 // Both get and get_many are tested. Two cache entries are used to ensure the times aren't
1896 // confused with multiple calls to get()/get_many().
1897 $startmicrotime = microtime(true);
1898 $cacheableobject = new cache_phpunit_dummy_object(1, 1, $startmicrotime);
1899 $cacheableobject2 = new cache_phpunit_dummy_object(2, 2, $startmicrotime);
1900 $this->assertTrue($cache->set('a', $cacheableobject));
1901 $this->assertTrue($cache->set('b', $cacheableobject2));
1902 $staticaccelerationreturntime = $cache->phpunit_static_acceleration_get('a')->propertytime;
1903 $staticaccelerationreturntimeb = $cache->phpunit_static_acceleration_get('b')->propertytime;
1904 $this->assertGreaterThanOrEqual($startmicrotime, $staticaccelerationreturntime, 'Restore time of static must be newer.');
1906 // Reset the static cache without resetting backing store.
1907 $cache->phpunit_static_acceleration_purge();
1909 // Get the value from the backend store, populating the static cache.
1910 $cachevalue = $cache->get('a');
1911 $this->assertInstanceOf('cache_phpunit_dummy_object', $cachevalue);
1912 $this->assertGreaterThanOrEqual($staticaccelerationreturntime, $cachevalue->propertytime);
1913 $backingstorereturntime = $cachevalue->propertytime;
1915 $results = $cache->get_many(array('b'));
1916 $this->assertInstanceOf('cache_phpunit_dummy_object', $results['b']);
1917 $this->assertGreaterThanOrEqual($staticaccelerationreturntimeb, $results['b']->propertytime);
1918 $backingstorereturntimeb = $results['b']->propertytime;
1920 // Obtain the value again and confirm that static cache is using wake_from_cache.
1921 // Upon failure, the times are not adjusted as wake_from_cache is skipped as the
1922 // value is stored serialized in the static acceleration cache.
1923 $cachevalue = $cache->phpunit_static_acceleration_get('a');
1924 $this->assertInstanceOf('cache_phpunit_dummy_object', $cachevalue);
1925 $this->assertGreaterThanOrEqual($backingstorereturntime, $cachevalue->propertytime);
1927 $results = $cache->get_many(array('b'));
1928 $this->assertInstanceOf('cache_phpunit_dummy_object', $results['b']);
1929 $this->assertGreaterThanOrEqual($backingstorereturntimeb, $results['b']->propertytime);
1931 /** @var cache_phpunit_application $cache */
1932 $cache = cache::make('phpunit', 'accelerated2');
1933 $this->assertInstanceOf('cache_phpunit_application', $cache);
1935 // Check that the array holds the last accessed items by get/set.
1936 $this->assertTrue($cache->set('a', 'A'));
1937 $this->assertTrue($cache->set('b', 'B'));
1938 $this->assertTrue($cache->set('c', 'C'));
1939 $this->assertTrue($cache->set('d', 'D'));
1940 $this->assertTrue($cache->set('e', 'E'));
1941 // Current keys in the array: c, d, e.
1942 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1943 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1944 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1945 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1946 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1948 $this->assertEquals('A', $cache->get('a'));
1949 // Current keys in the array: d, e, a.
1950 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1951 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1952 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1953 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1954 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1956 // Current keys in the array: d, e, a.
1957 $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c')));
1958 // Current keys in the array: e, a, c.
1959 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1960 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1961 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1962 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1963 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1966 $cache = cache::make('phpunit', 'accelerated3');
1967 $this->assertInstanceOf('cache_phpunit_application', $cache);
1969 // Check that the array holds the last accessed items by get/set.
1970 $this->assertTrue($cache->set('a', 'A'));
1971 $this->assertTrue($cache->set('b', 'B'));
1972 $this->assertTrue($cache->set('c', 'C'));
1973 $this->assertTrue($cache->set('d', 'D'));
1974 $this->assertTrue($cache->set('e', 'E'));
1975 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1976 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1977 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1978 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1979 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1981 $this->assertTrue($cache->set('b', 'B2'));
1982 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1983 $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b'));
1984 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1985 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1986 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1988 $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3')));
1989 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1990 $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b'));
1991 $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c'));
1992 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1993 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1995 $cache = cache::make('phpunit', 'accelerated4');
1996 $this->assertInstanceOf('cache_phpunit_application', $cache);
1997 $this->assertTrue($cache->set('a', 'A'));
1998 $this->assertTrue($cache->set('a', 'A'));
1999 $this->assertTrue($cache->set('a', 'A'));
2000 $this->assertTrue($cache->set('a', 'A'));
2001 $this->assertTrue($cache->set('a', 'A'));
2002 $this->assertTrue($cache->set('a', 'A'));
2003 $this->assertTrue($cache->set('a', 'A'));
2004 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
2005 $this->assertEquals('A', $cache->get('a'));
2007 // Setting simpledata to false objects are cloned when retrieving data.
2008 $cache = cache::make('phpunit', 'simpledataarea1');
2009 $notreallysimple = new stdClass();
2010 $notreallysimple->name = 'a';
2011 $cache->set('a', $notreallysimple);
2012 $returnedinstance1 = $cache->get('a');
2013 $returnedinstance2 = $cache->get('a');
2014 $returnedinstance1->name = 'b';
2015 $this->assertEquals('a', $returnedinstance2->name);
2017 // Setting simpledata to true we assume that data does not contain references.
2018 $cache = cache::make('phpunit', 'simpledataarea2');
2019 $notreallysimple = new stdClass();
2020 $notreallysimple->name = 'a';
2021 $cache->set('a', $notreallysimple);
2022 $returnedinstance1 = $cache->get('a');
2023 $returnedinstance2 = $cache->get('a');
2024 $returnedinstance1->name = 'b';
2025 $this->assertEquals('b', $returnedinstance2->name);
2028 public function test_identifiers_have_separate_caches() {
2029 $cachepg = cache::make('core', 'databasemeta', array('dbfamily' => 'pgsql'));
2030 $cachepg->set(1, 'here');
2031 $cachemy = cache::make('core', 'databasemeta', array('dbfamily' => 'mysql'));
2032 $cachemy->set(2, 'there');
2033 $this->assertEquals('here', $cachepg->get(1));
2034 $this->assertEquals('there', $cachemy->get(2));
2035 $this->assertFalse($cachemy->get(1));
2038 public function test_performance_debug() {
2039 global $CFG;
2040 $this->resetAfterTest(true);
2041 $CFG->perfdebug = 15;
2043 $instance = cache_config_testing::instance();
2044 $applicationid = 'phpunit/applicationperf';
2045 $instance->phpunit_add_definition($applicationid, array(
2046 'mode' => cache_store::MODE_APPLICATION,
2047 'component' => 'phpunit',
2048 'area' => 'applicationperf'
2050 $sessionid = 'phpunit/sessionperf';
2051 $instance->phpunit_add_definition($sessionid, array(
2052 'mode' => cache_store::MODE_SESSION,
2053 'component' => 'phpunit',
2054 'area' => 'sessionperf'
2056 $requestid = 'phpunit/requestperf';
2057 $instance->phpunit_add_definition($requestid, array(
2058 'mode' => cache_store::MODE_REQUEST,
2059 'component' => 'phpunit',
2060 'area' => 'requestperf'
2063 $application = cache::make('phpunit', 'applicationperf');
2064 $session = cache::make('phpunit', 'sessionperf');
2065 $request = cache::make('phpunit', 'requestperf');
2067 // Check that no stats are recorded for these definitions yet.
2068 $stats = cache_helper::get_stats();
2069 $this->assertArrayNotHasKey($applicationid, $stats);
2070 $this->assertArrayHasKey($sessionid, $stats); // Session cache sets a key on construct.
2071 $this->assertArrayNotHasKey($requestid, $stats);
2073 // Check that stores register misses.
2074 $this->assertFalse($application->get('missMe'));
2075 $this->assertFalse($application->get('missMe'));
2076 $this->assertFalse($session->get('missMe'));
2077 $this->assertFalse($session->get('missMe'));
2078 $this->assertFalse($session->get('missMe'));
2079 $this->assertFalse($request->get('missMe'));
2080 $this->assertFalse($request->get('missMe'));
2081 $this->assertFalse($request->get('missMe'));
2082 $this->assertFalse($request->get('missMe'));
2084 $endstats = cache_helper::get_stats();
2085 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['misses']);
2086 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits']);
2087 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets']);
2088 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['misses']);
2089 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits']);
2090 $this->assertEquals(1, $endstats[$sessionid]['stores']['cachestore_session']['sets']);
2091 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['misses']);
2092 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits']);
2093 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets']);
2095 $startstats = cache_helper::get_stats();
2097 // Check that stores register sets.
2098 $this->assertTrue($application->set('setMe1', 1));
2099 $this->assertTrue($application->set('setMe2', 2));
2100 $this->assertTrue($session->set('setMe1', 1));
2101 $this->assertTrue($session->set('setMe2', 2));
2102 $this->assertTrue($session->set('setMe3', 3));
2103 $this->assertTrue($request->set('setMe1', 1));
2104 $this->assertTrue($request->set('setMe2', 2));
2105 $this->assertTrue($request->set('setMe3', 3));
2106 $this->assertTrue($request->set('setMe4', 4));
2108 $endstats = cache_helper::get_stats();
2109 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2110 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2111 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2112 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2113 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2114 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2115 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2116 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2117 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2118 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2119 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2120 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2121 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2122 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2123 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2124 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2125 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2126 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2128 $startstats = cache_helper::get_stats();
2130 // Check that stores register hits.
2131 $this->assertEquals($application->get('setMe1'), 1);
2132 $this->assertEquals($application->get('setMe2'), 2);
2133 $this->assertEquals($session->get('setMe1'), 1);
2134 $this->assertEquals($session->get('setMe2'), 2);
2135 $this->assertEquals($session->get('setMe3'), 3);
2136 $this->assertEquals($request->get('setMe1'), 1);
2137 $this->assertEquals($request->get('setMe2'), 2);
2138 $this->assertEquals($request->get('setMe3'), 3);
2139 $this->assertEquals($request->get('setMe4'), 4);
2141 $endstats = cache_helper::get_stats();
2142 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2143 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2144 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2145 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2146 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2147 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2148 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2149 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2150 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2151 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2152 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2153 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2154 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2155 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2156 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2157 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2158 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2159 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2161 $startstats = cache_helper::get_stats();
2163 // Check that stores register through get_many.
2164 $application->get_many(array('setMe1', 'setMe2'));
2165 $session->get_many(array('setMe1', 'setMe2', 'setMe3'));
2166 $request->get_many(array('setMe1', 'setMe2', 'setMe3', 'setMe4'));
2168 $endstats = cache_helper::get_stats();
2169 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2170 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2171 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2172 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2173 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2174 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2175 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2176 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2177 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2178 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2179 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2180 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2181 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2182 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2183 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2184 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2185 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2186 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2189 public function test_static_cache() {
2190 global $CFG;
2191 $this->resetAfterTest(true);
2192 $CFG->perfdebug = 15;
2194 // Create cache store with static acceleration.
2195 $instance = cache_config_testing::instance();
2196 $applicationid = 'phpunit/applicationperf';
2197 $instance->phpunit_add_definition($applicationid, array(
2198 'mode' => cache_store::MODE_APPLICATION,
2199 'component' => 'phpunit',
2200 'area' => 'applicationperf',
2201 'simplekeys' => true,
2202 'staticacceleration' => true,
2203 'staticaccelerationsize' => 3
2206 $application = cache::make('phpunit', 'applicationperf');
2208 // Check that stores register sets.
2209 $this->assertTrue($application->set('setMe1', 1));
2210 $this->assertTrue($application->set('setMe2', 0));
2211 $this->assertTrue($application->set('setMe3', array()));
2212 $this->assertTrue($application->get('setMe1') !== false);
2213 $this->assertTrue($application->get('setMe2') !== false);
2214 $this->assertTrue($application->get('setMe3') !== false);
2216 // Check that the static acceleration worked, even on empty arrays and the number 0.
2217 $endstats = cache_helper::get_stats();
2218 $this->assertEquals(0, $endstats[$applicationid]['stores']['** static acceleration **']['misses']);
2219 $this->assertEquals(3, $endstats[$applicationid]['stores']['** static acceleration **']['hits']);
2222 public function test_performance_debug_off() {
2223 global $CFG;
2224 $this->resetAfterTest(true);
2225 $CFG->perfdebug = 7;
2227 $instance = cache_config_testing::instance();
2228 $applicationid = 'phpunit/applicationperfoff';
2229 $instance->phpunit_add_definition($applicationid, array(
2230 'mode' => cache_store::MODE_APPLICATION,
2231 'component' => 'phpunit',
2232 'area' => 'applicationperfoff'
2234 $sessionid = 'phpunit/sessionperfoff';
2235 $instance->phpunit_add_definition($sessionid, array(
2236 'mode' => cache_store::MODE_SESSION,
2237 'component' => 'phpunit',
2238 'area' => 'sessionperfoff'
2240 $requestid = 'phpunit/requestperfoff';
2241 $instance->phpunit_add_definition($requestid, array(
2242 'mode' => cache_store::MODE_REQUEST,
2243 'component' => 'phpunit',
2244 'area' => 'requestperfoff'
2247 $application = cache::make('phpunit', 'applicationperfoff');
2248 $session = cache::make('phpunit', 'sessionperfoff');
2249 $request = cache::make('phpunit', 'requestperfoff');
2251 // Check that no stats are recorded for these definitions yet.
2252 $stats = cache_helper::get_stats();
2253 $this->assertArrayNotHasKey($applicationid, $stats);
2254 $this->assertArrayNotHasKey($sessionid, $stats);
2255 $this->assertArrayNotHasKey($requestid, $stats);
2257 // Trigger cache misses, cache sets and cache hits.
2258 $this->assertFalse($application->get('missMe'));
2259 $this->assertTrue($application->set('setMe', 1));
2260 $this->assertEquals(1, $application->get('setMe'));
2261 $this->assertFalse($session->get('missMe'));
2262 $this->assertTrue($session->set('setMe', 3));
2263 $this->assertEquals(3, $session->get('setMe'));
2264 $this->assertFalse($request->get('missMe'));
2265 $this->assertTrue($request->set('setMe', 4));
2266 $this->assertEquals(4, $request->get('setMe'));
2268 // Check that no stats are being recorded for these definitions.
2269 $endstats = cache_helper::get_stats();
2270 $this->assertArrayNotHasKey($applicationid, $endstats);
2271 $this->assertArrayNotHasKey($sessionid, $endstats);
2272 $this->assertArrayNotHasKey($requestid, $endstats);
2276 * Tests session cache event purge and subsequent visit in the same request.
2278 * This test simulates a cache being created, a value being set, then the value being purged.
2279 * A subsequent use of the same cache is started in the same request which fills the cache.
2280 * A new request is started a short time later.
2281 * The cache should be filled.
2283 public function test_session_event_purge_same_second() {
2284 $instance = cache_config_testing::instance();
2285 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
2286 'mode' => cache_store::MODE_SESSION,
2287 'component' => 'phpunit',
2288 'area' => 'eventpurgetest',
2289 'invalidationevents' => array(
2290 'crazyevent',
2294 // Create the cache, set a value, and immediately purge it by event.
2295 $cache = cache::make('phpunit', 'eventpurgetest');
2296 $cache->set('testkey1', 'test data 1');
2297 $this->assertEquals('test data 1', $cache->get('testkey1'));
2298 cache_helper::purge_by_event('crazyevent');
2299 $this->assertFalse($cache->get('testkey1'));
2301 // Set up the cache again in the same request and add a new value back in.
2302 $factory = \cache_factory::instance();
2303 $factory->reset_cache_instances();
2304 $cache = cache::make('phpunit', 'eventpurgetest');
2305 $cache->set('testkey1', 'test data 2');
2306 $this->assertEquals('test data 2', $cache->get('testkey1'));
2308 // Trick the cache into thinking that this is a new request.
2309 cache_phpunit_cache::simulate_new_request();
2310 $factory = \cache_factory::instance();
2311 $factory->reset_cache_instances();
2313 // Set up the cache again.
2314 // This is a subsequent request at a new time, so we instead the invalidation time will be checked.
2315 // The invalidation time should match the last purged time and the cache will not be re-purged.
2316 $cache = cache::make('phpunit', 'eventpurgetest');
2317 $this->assertEquals('test data 2', $cache->get('testkey1'));