2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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.
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.
33 require_once($CFG->dirroot
.'/cache/locallib.php');
34 require_once($CFG->dirroot
.'/cache/tests/fixtures/lib.php');
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 cache_phpunit_tests
extends advanced_testcase
{
45 * Set things back to the default before each test.
47 public function setUp() {
49 cache_factory
::reset();
50 cache_config_phpunittest
::create_default_configuration();
54 * Final task is to reset the cache system
56 public static function tearDownAfterClass() {
57 parent
::tearDownAfterClass();
58 cache_factory
::reset();
62 * Tests cache configuration
64 public function test_cache_config() {
65 $instance = cache_config
::instance();
66 $this->assertInstanceOf('cache_config_phpunittest', $instance);
68 $this->assertTrue(cache_config_phpunittest
::config_file_exists());
70 $stores = $instance->get_all_stores();
71 $this->assertCount(3, $stores);
72 foreach ($stores as $name => $store) {
73 // Check its an array.
74 $this->assertInternalType('array', $store);
75 // Check the name is the key.
76 $this->assertEquals($name, $store['name']);
77 // Check that it has been declared default.
78 $this->assertTrue($store['default']);
79 // Required attributes = name + plugin + configuration + modes + features.
80 $this->assertArrayHasKey('name', $store);
81 $this->assertArrayHasKey('plugin', $store);
82 $this->assertArrayHasKey('configuration', $store);
83 $this->assertArrayHasKey('modes', $store);
84 $this->assertArrayHasKey('features', $store);
87 $modemappings = $instance->get_mode_mappings();
88 $this->assertCount(3, $modemappings);
90 cache_store
::MODE_APPLICATION
=> false,
91 cache_store
::MODE_SESSION
=> false,
92 cache_store
::MODE_REQUEST
=> false,
94 foreach ($modemappings as $mapping) {
95 // We expect 3 properties.
96 $this->assertCount(3, $mapping);
97 // Required attributes = mode + store.
98 $this->assertArrayHasKey('mode', $mapping);
99 $this->assertArrayHasKey('store', $mapping);
101 $modes[$mapping['mode']] = true;
104 // Must have the default 3 modes and no more.
105 $this->assertCount(3, $mapping);
106 foreach ($modes as $mode) {
107 $this->assertTrue($mode);
110 $definitions = $instance->get_definitions();
111 // The event invalidation definition is required for the cache API and must be there.
112 $this->assertArrayHasKey('core/eventinvalidation', $definitions);
114 $definitionmappings = $instance->get_definition_mappings();
115 foreach ($definitionmappings as $mapping) {
116 // Required attributes = definition + store.
117 $this->assertArrayHasKey('definition', $mapping);
118 $this->assertArrayHasKey('store', $mapping);
123 * Tests the default application cache
125 public function test_default_application_cache() {
126 $cache = cache
::make_from_params(cache_store
::MODE_APPLICATION
, 'phpunit', 'applicationtest');
127 $this->assertInstanceOf('cache_application', $cache);
128 $this->run_on_cache($cache);
132 * Tests the default session cache
134 public function test_default_session_cache() {
135 $cache = cache
::make_from_params(cache_store
::MODE_SESSION
, 'phpunit', 'applicationtest');
136 $this->assertInstanceOf('cache_session', $cache);
137 $this->run_on_cache($cache);
141 * Tests the default request cache
143 public function test_default_request_cache() {
144 $cache = cache
::make_from_params(cache_store
::MODE_REQUEST
, 'phpunit', 'applicationtest');
145 $this->assertInstanceOf('cache_request', $cache);
146 $this->run_on_cache($cache);
150 * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
152 public function test_on_cache_without_store() {
153 $instance = cache_config_phpunittest
::instance(true);
154 $instance->phpunit_add_definition('phpunit/nostoretest1', array(
155 'mode' => cache_store
::MODE_APPLICATION
,
156 'component' => 'phpunit',
157 'area' => 'nostoretest1',
159 $instance->phpunit_add_definition('phpunit/nostoretest2', array(
160 'mode' => cache_store
::MODE_APPLICATION
,
161 'component' => 'phpunit',
162 'area' => 'nostoretest2',
165 $instance->phpunit_remove_stores();
167 $cache = cache
::make('phpunit', 'nostoretest1');
168 $this->run_on_cache($cache);
170 $cache = cache
::make('phpunit', 'nostoretest2');
171 $this->run_on_cache($cache);
175 * Runs a standard series of access and use tests on a cache instance.
177 * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
179 * @param cache_loader $cache
181 protected function run_on_cache(cache_loader
$cache) {
183 $datascalar = 'test data';
184 $dataarray = array('test' => 'data', 'part' => 'two');
185 $dataobject = (object)$dataarray;
187 $this->assertTrue($cache->purge());
189 // Check all read methods.
190 $this->assertFalse($cache->get($key));
191 $this->assertFalse($cache->has($key));
192 $result = $cache->get_many(array($key));
193 $this->assertCount(1, $result);
194 $this->assertFalse(reset($result));
195 $this->assertFalse($cache->has_any(array($key)));
196 $this->assertFalse($cache->has_all(array($key)));
199 $this->assertTrue($cache->set($key, $datascalar));
200 // Setting it more than once should be permitted.
201 $this->assertTrue($cache->set($key, $datascalar));
203 // Recheck the read methods.
204 $this->assertEquals($datascalar, $cache->get($key));
205 $this->assertTrue($cache->has($key));
206 $result = $cache->get_many(array($key));
207 $this->assertCount(1, $result);
208 $this->assertEquals($datascalar, reset($result));
209 $this->assertTrue($cache->has_any(array($key)));
210 $this->assertTrue($cache->has_all(array($key)));
213 $this->assertTrue($cache->delete($key));
216 $this->assertFalse($cache->get($key));
217 $this->assertFalse($cache->has($key));
220 $this->assertTrue($cache->set($key, $dataarray));
221 $this->assertEquals($dataarray, $cache->get($key));
224 $this->assertTrue($cache->set($key, $dataobject));
225 $this->assertEquals($dataobject, $cache->get($key));
227 $specobject = new cache_phpunit_dummy_object('red', 'blue');
228 $this->assertTrue($cache->set($key, $specobject));
229 $result = $cache->get($key);
230 $this->assertInstanceOf('cache_phpunit_dummy_object', $result);
231 $this->assertEquals('red_ptc_wfc', $result->property1
);
232 $this->assertEquals('blue_ptc_wfc', $result->property2
);
234 // Test array of objects
235 $specobject = new cache_phpunit_dummy_object('red', 'blue');
236 $data = new cacheable_object_array(array(
241 $this->assertTrue($cache->set($key, $data));
242 $result = $cache->get($key);
243 $this->assertInstanceOf('cacheable_object_array', $result);
244 $this->assertCount(3, $data);
245 foreach ($result as $item) {
246 $this->assertInstanceOf('cache_phpunit_dummy_object', $item);
247 $this->assertEquals('red_ptc_wfc', $item->property1
);
248 $this->assertEquals('blue_ptc_wfc', $item->property2
);
252 $cache->set_many(array('key1' => 'data1', 'key2' => 'data2'));
253 $this->assertEquals('data1', $cache->get('key1'));
254 $this->assertEquals('data2', $cache->get('key2'));
255 $this->assertTrue($cache->delete('key1'));
256 $this->assertTrue($cache->delete('key2'));
259 $this->assertTrue($cache->set('key1', 'data1'));
260 $this->assertTrue($cache->set('key2', 'data2'));
262 $this->assertEquals('data1', $cache->get('key1'));
263 $this->assertEquals('data2', $cache->get('key2'));
265 $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
267 $this->assertFalse($cache->get('key1'));
268 $this->assertFalse($cache->get('key2'));
270 // Quick reference test.
274 $this->assertTrue($cache->set('obj', $obj));
277 $var = $cache->get('obj');
278 $this->assertInstanceOf('stdClass', $var);
279 $this->assertEquals('value', $var->key
);
282 $var = $cache->get('obj');
283 $this->assertInstanceOf('stdClass', $var);
284 $this->assertEquals('value', $var->key
);
286 $this->assertTrue($cache->delete('obj'));
288 // Deep reference test.
289 $obj1 = new stdClass
;
290 $obj1->key
= 'value';
291 $obj2 = new stdClass
;
293 $obj3 = new stdClass
;
295 $obj1->subobj
=& $obj2;
296 $obj2->subobj
=& $obj3;
297 $this->assertTrue($cache->set('obj', $obj1));
299 $obj1->key
= 'eulav';
302 $var = $cache->get('obj');
303 $this->assertInstanceOf('stdClass', $var);
304 $this->assertEquals('value', $var->key
);
305 $this->assertInstanceOf('stdClass', $var->subobj
);
306 $this->assertEquals('test', $var->subobj
->key
);
307 $this->assertInstanceOf('stdClass', $var->subobj
->subobj
);
308 $this->assertEquals('pork', $var->subobj
->subobj
->key
);
309 $this->assertTrue($cache->delete('obj'));
311 // Death reference test... basicaly we don't want this to die.
315 $this->assertTrue($cache->set('obj', $obj));
316 $var = $cache->get('obj');
317 $this->assertInstanceOf('stdClass', $var);
318 $this->assertEquals('value', $var->key
);
320 // Reference test after retrieve.
323 $this->assertTrue($cache->set('obj', $obj));
325 $var1 = $cache->get('obj');
326 $this->assertInstanceOf('stdClass', $var1);
327 $this->assertEquals('value', $var1->key
);
328 $var1->key
= 'eulav';
329 $this->assertEquals('eulav', $var1->key
);
331 $var2 = $cache->get('obj');
332 $this->assertInstanceOf('stdClass', $var2);
333 $this->assertEquals('value', $var2->key
);
335 $this->assertTrue($cache->delete('obj'));
339 * Tests a definition using a data loader
341 public function test_definition_data_loader() {
342 $instance = cache_config_phpunittest
::instance(true);
343 $instance->phpunit_add_definition('phpunit/datasourcetest', array(
344 'mode' => cache_store
::MODE_APPLICATION
,
345 'component' => 'phpunit',
346 'area' => 'datasourcetest',
347 'datasource' => 'cache_phpunit_dummy_datasource',
348 'datasourcefile' => 'cache/tests/fixtures/lib.php'
351 $cache = cache
::make('phpunit', 'datasourcetest');
352 $this->assertInstanceOf('cache_application', $cache);
354 // Purge it to be sure.
355 $this->assertTrue($cache->purge());
356 // It won't be there yet.
357 $this->assertFalse($cache->has('Test'));
359 // It should load it ;).
360 $this->assertTrue($cache->has('Test', true));
362 // Purge it to be sure.
363 $this->assertTrue($cache->purge());
364 $this->assertEquals('Test has no value really.', $cache->get('Test'));
368 * Tests a definition using an overridden loader
370 public function test_definition_overridden_loader() {
371 $instance = cache_config_phpunittest
::instance(true);
372 $instance->phpunit_add_definition('phpunit/overridetest', array(
373 'mode' => cache_store
::MODE_APPLICATION
,
374 'component' => 'phpunit',
375 'area' => 'overridetest',
376 'overrideclass' => 'cache_phpunit_dummy_overrideclass',
377 'overrideclassfile' => 'cache/tests/fixtures/lib.php'
379 $cache = cache
::make('phpunit', 'overridetest');
380 $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
381 $this->assertInstanceOf('cache_application', $cache);
382 // Purge it to be sure.
383 $this->assertTrue($cache->purge());
384 // It won't be there yet.
385 $this->assertFalse($cache->has('Test'));
387 $this->assertTrue($cache->set('Test', 'Test has no value really.'));
389 $this->assertEquals('Test has no value really.', $cache->get('Test'));
393 * Test a very basic definition.
395 public function test_definition() {
396 $instance = cache_config_phpunittest
::instance();
397 $instance->phpunit_add_definition('phpunit/test', array(
398 'mode' => cache_store
::MODE_APPLICATION
,
399 'component' => 'phpunit',
402 $cache = cache
::make('phpunit', 'test');
404 $this->assertTrue($cache->set('testkey1', 'test data 1'));
405 $this->assertEquals('test data 1', $cache->get('testkey1'));
406 $this->assertTrue($cache->set('testkey2', 'test data 2'));
407 $this->assertEquals('test data 2', $cache->get('testkey2'));
411 * Test a definition using the simple keys.
413 public function test_definition_simplekeys() {
414 $instance = cache_config_phpunittest
::instance();
415 $instance->phpunit_add_definition('phpunit/simplekeytest', array(
416 'mode' => cache_store
::MODE_APPLICATION
,
417 'component' => 'phpunit',
418 'area' => 'simplekeytest',
421 $cache = cache
::make('phpunit', 'simplekeytest');
423 $this->assertTrue($cache->set('testkey1', 'test data 1'));
424 $this->assertEquals('test data 1', $cache->get('testkey1'));
425 $this->assertTrue($cache->set('testkey2', 'test data 2'));
426 $this->assertEquals('test data 2', $cache->get('testkey2'));
430 $this->assertTrue($cache->set('1', 'test data 1'));
431 $this->assertEquals('test data 1', $cache->get('1'));
432 $this->assertTrue($cache->set('2', 'test data 2'));
433 $this->assertEquals('test data 2', $cache->get('2'));
436 public function test_definition_ttl() {
437 $instance = cache_config_phpunittest
::instance(true);
438 $instance->phpunit_add_definition('phpunit/ttltest', array(
439 'mode' => cache_store
::MODE_APPLICATION
,
440 'component' => 'phpunit',
442 'ttl' => -86400 // Set to a day in the past to be extra sure.
444 $cache = cache
::make('phpunit', 'ttltest');
445 $this->assertInstanceOf('cache_application', $cache);
447 // Purge it to be sure.
448 $this->assertTrue($cache->purge());
449 // It won't be there yet.
450 $this->assertFalse($cache->has('Test'));
452 $this->assertTrue($cache->set('Test', 'Test'));
453 // Check its not there.
454 $this->assertFalse($cache->has('Test'));
455 // Double check by trying to get it.
456 $this->assertFalse($cache->get('Test'));
460 * Tests manual locking operations on an application cache
462 public function test_application_manual_locking() {
463 $instance = cache_config_phpunittest
::instance();
464 $instance->phpunit_add_definition('phpunit/lockingtest', array(
465 'mode' => cache_store
::MODE_APPLICATION
,
466 'component' => 'phpunit',
467 'area' => 'lockingtest'
469 $cache1 = cache
::make('phpunit', 'lockingtest');
470 $cache2 = clone($cache1);
472 $this->assertTrue($cache1->set('testkey', 'test data'));
473 $this->assertTrue($cache2->set('testkey', 'test data'));
475 $this->assertTrue($cache1->acquire_lock('testkey'));
476 $this->assertFalse($cache2->acquire_lock('testkey'));
478 $this->assertTrue($cache1->check_lock_state('testkey'));
479 $this->assertFalse($cache2->check_lock_state('testkey'));
481 $this->assertTrue($cache1->release_lock('testkey'));
482 $this->assertFalse($cache2->release_lock('testkey'));
484 $this->assertTrue($cache1->set('testkey', 'test data'));
485 $this->assertTrue($cache2->set('testkey', 'test data'));
489 * Tests application cache event invalidation
491 public function test_application_event_invalidation() {
492 $instance = cache_config_phpunittest
::instance();
493 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
494 'mode' => cache_store
::MODE_APPLICATION
,
495 'component' => 'phpunit',
496 'area' => 'eventinvalidationtest',
497 'invalidationevents' => array(
501 $cache = cache
::make('phpunit', 'eventinvalidationtest');
503 $this->assertTrue($cache->set('testkey1', 'test data 1'));
504 $this->assertEquals('test data 1', $cache->get('testkey1'));
505 $this->assertTrue($cache->set('testkey2', 'test data 2'));
506 $this->assertEquals('test data 2', $cache->get('testkey2'));
508 // Test invalidating a single entry.
509 cache_helper
::invalidate_by_event('crazyevent', array('testkey1'));
511 $this->assertFalse($cache->get('testkey1'));
512 $this->assertEquals('test data 2', $cache->get('testkey2'));
514 $this->assertTrue($cache->set('testkey1', 'test data 1'));
516 // Test invalidating both entries.
517 cache_helper
::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
519 $this->assertFalse($cache->get('testkey1'));
520 $this->assertFalse($cache->get('testkey2'));
524 * Tests application cache definition invalidation
526 public function test_application_definition_invalidation() {
527 $instance = cache_config_phpunittest
::instance();
528 $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
529 'mode' => cache_store
::MODE_APPLICATION
,
530 'component' => 'phpunit',
531 'area' => 'definitioninvalidation'
533 $cache = cache
::make('phpunit', 'definitioninvalidation');
534 $this->assertTrue($cache->set('testkey1', 'test data 1'));
535 $this->assertEquals('test data 1', $cache->get('testkey1'));
536 $this->assertTrue($cache->set('testkey2', 'test data 2'));
537 $this->assertEquals('test data 2', $cache->get('testkey2'));
539 cache_helper
::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
541 $this->assertFalse($cache->get('testkey1'));
542 $this->assertEquals('test data 2', $cache->get('testkey2'));
544 $this->assertTrue($cache->set('testkey1', 'test data 1'));
546 cache_helper
::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
548 $this->assertFalse($cache->get('testkey1'));
549 $this->assertEquals('test data 2', $cache->get('testkey2'));
551 $this->assertTrue($cache->set('testkey1', 'test data 1'));
553 cache_helper
::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
555 $this->assertFalse($cache->get('testkey1'));
556 $this->assertFalse($cache->get('testkey2'));
560 * Tests application cache event invalidation over a distributed setup.
562 public function test_distributed_application_event_invalidation() {
564 // This is going to be an intense wee test.
565 // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
566 // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
567 // check that it is not picked up.
568 $instance = cache_config_phpunittest
::instance();
569 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
570 'mode' => cache_store
::MODE_APPLICATION
,
571 'component' => 'phpunit',
572 'area' => 'eventinvalidationtest',
573 'simplekeys' => true,
574 'simpledata' => true,
575 'invalidationevents' => array(
579 $cache = cache
::make('phpunit', 'eventinvalidationtest');
580 $this->assertTrue($cache->set('testkey1', 'test data 1'));
581 $this->assertEquals('test data 1', $cache->get('testkey1'));
583 cache_helper
::invalidate_by_event('crazyevent', array('testkey1'));
585 $this->assertFalse($cache->get('testkey1'));
587 // OK data added, data invalidated, and invalidation time has been set.
588 // Now we need to manually add back the data and adjust the invalidation time.
589 $hash = md5(cache_store
::MODE_APPLICATION
.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot
.'phpunit');
590 $timefile = $CFG->dataroot
."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las/lastinvalidation-$hash.cache";
591 // Make sure the file is correct.
592 $this->assertTrue(file_exists($timefile));
593 $timecont = serialize(cache
::now() - 60); // Back 60sec in the past to force it to re-invalidate.
594 make_writable_directory(dirname($timefile));
595 file_put_contents($timefile, $timecont);
596 $this->assertTrue(file_exists($timefile));
598 $datafile = $CFG->dataroot
."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes/testkey1-$hash.cache";
599 $datacont = serialize("test data 1");
600 make_writable_directory(dirname($datafile));
601 file_put_contents($datafile, $datacont);
602 $this->assertTrue(file_exists($datafile));
604 // Test 1: Rebuild without the event and test its there.
605 cache_factory
::reset();
606 $instance = cache_config_phpunittest
::instance();
607 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
608 'mode' => cache_store
::MODE_APPLICATION
,
609 'component' => 'phpunit',
610 'area' => 'eventinvalidationtest',
611 'simplekeys' => true,
612 'simpledata' => true,
614 $cache = cache
::make('phpunit', 'eventinvalidationtest');
615 $this->assertEquals('test data 1', $cache->get('testkey1'));
617 // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
618 cache_factory
::reset();
619 $instance = cache_config_phpunittest
::instance();
620 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
621 'mode' => cache_store
::MODE_APPLICATION
,
622 'component' => 'phpunit',
623 'area' => 'eventinvalidationtest',
624 'simplekeys' => true,
625 'simpledata' => true,
626 'invalidationevents' => array(
630 $cache = cache
::make('phpunit', 'eventinvalidationtest');
631 $this->assertFalse($cache->get('testkey1'));
635 * Tests application cache event purge
637 public function test_application_event_purge() {
638 $instance = cache_config_phpunittest
::instance();
639 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
640 'mode' => cache_store
::MODE_APPLICATION
,
641 'component' => 'phpunit',
642 'area' => 'eventpurgetest',
643 'invalidationevents' => array(
647 $instance->phpunit_add_definition('phpunit/eventpurgetestpersistent', array(
648 'mode' => cache_store
::MODE_APPLICATION
,
649 'component' => 'phpunit',
650 'area' => 'eventpurgetestpersistent',
651 'persistent' => true,
652 'invalidationevents' => array(
656 $cache = cache
::make('phpunit', 'eventpurgetest');
658 $this->assertTrue($cache->set('testkey1', 'test data 1'));
659 $this->assertEquals('test data 1', $cache->get('testkey1'));
660 $this->assertTrue($cache->set('testkey2', 'test data 2'));
661 $this->assertEquals('test data 2', $cache->get('testkey2'));
664 cache_helper
::purge_by_event('crazyevent');
666 // Check things have been removed.
667 $this->assertFalse($cache->get('testkey1'));
668 $this->assertFalse($cache->get('testkey2'));
670 // Now test the persistent cache.
671 $cache = cache
::make('phpunit', 'eventpurgetestpersistent');
672 $this->assertTrue($cache->set('testkey1', 'test data 1'));
673 $this->assertEquals('test data 1', $cache->get('testkey1'));
674 $this->assertTrue($cache->set('testkey2', 'test data 2'));
675 $this->assertEquals('test data 2', $cache->get('testkey2'));
678 cache_helper
::purge_by_event('crazyevent');
680 // Check things have been removed.
681 $this->assertFalse($cache->get('testkey1'));
682 $this->assertFalse($cache->get('testkey2'));
686 * Tests session cache event purge
688 public function test_session_event_purge() {
689 $instance = cache_config_phpunittest
::instance();
690 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
691 'mode' => cache_store
::MODE_SESSION
,
692 'component' => 'phpunit',
693 'area' => 'eventpurgetest',
694 'invalidationevents' => array(
698 $instance->phpunit_add_definition('phpunit/eventpurgetestpersistent', array(
699 'mode' => cache_store
::MODE_SESSION
,
700 'component' => 'phpunit',
701 'area' => 'eventpurgetestpersistent',
702 'persistent' => true,
703 'invalidationevents' => array(
707 $cache = cache
::make('phpunit', 'eventpurgetest');
709 $this->assertTrue($cache->set('testkey1', 'test data 1'));
710 $this->assertEquals('test data 1', $cache->get('testkey1'));
711 $this->assertTrue($cache->set('testkey2', 'test data 2'));
712 $this->assertEquals('test data 2', $cache->get('testkey2'));
715 cache_helper
::purge_by_event('crazyevent');
717 // Check things have been removed.
718 $this->assertFalse($cache->get('testkey1'));
719 $this->assertFalse($cache->get('testkey2'));
721 // Now test the persistent cache.
722 $cache = cache
::make('phpunit', 'eventpurgetestpersistent');
723 $this->assertTrue($cache->set('testkey1', 'test data 1'));
724 $this->assertEquals('test data 1', $cache->get('testkey1'));
725 $this->assertTrue($cache->set('testkey2', 'test data 2'));
726 $this->assertEquals('test data 2', $cache->get('testkey2'));
729 cache_helper
::purge_by_event('crazyevent');
731 // Check things have been removed.
732 $this->assertFalse($cache->get('testkey1'));
733 $this->assertFalse($cache->get('testkey2'));
737 * Tests application cache definition purge
739 public function test_application_definition_purge() {
740 $instance = cache_config_phpunittest
::instance();
741 $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
742 'mode' => cache_store
::MODE_APPLICATION
,
743 'component' => 'phpunit',
744 'area' => 'definitionpurgetest',
745 'invalidationevents' => array(
749 $cache = cache
::make('phpunit', 'definitionpurgetest');
751 $this->assertTrue($cache->set('testkey1', 'test data 1'));
752 $this->assertEquals('test data 1', $cache->get('testkey1'));
753 $this->assertTrue($cache->set('testkey2', 'test data 2'));
754 $this->assertEquals('test data 2', $cache->get('testkey2'));
757 cache_helper
::purge_by_definition('phpunit', 'definitionpurgetest');
759 // Check things have been removed.
760 $this->assertFalse($cache->get('testkey1'));
761 $this->assertFalse($cache->get('testkey2'));
765 * Test the use of an alt path.
766 * If we can generate a config instance we are done :)
768 public function test_alt_cache_path() {
770 $this->resetAfterTest();
771 $CFG->altcacheconfigpath
= $CFG->dataroot
.'/cache/altcacheconfigpath';
772 $instance = cache_config_phpunittest
::instance();
773 $this->assertInstanceOf('cache_config', $instance);
777 * Test disabling the cache stores.
779 public function test_disable_stores() {
780 $instance = cache_config_phpunittest
::instance();
781 $instance->phpunit_add_definition('phpunit/disabletest', array(
782 'mode' => cache_store
::MODE_APPLICATION
,
783 'component' => 'phpunit',
784 'area' => 'disabletest'
786 $cache = cache
::make('phpunit', 'disabletest');
787 $this->assertInstanceOf('cache_phpunit_application', $cache);
788 $this->assertEquals('cachestore_file', $cache->phpunit_get_store_class());
790 $this->assertFalse($cache->get('test'));
791 $this->assertTrue($cache->set('test', 'test'));
792 $this->assertEquals('test', $cache->get('test'));
794 cache_factory
::disable_stores();
796 $cache = cache
::make('phpunit', 'disabletest');
797 $this->assertInstanceOf('cache_phpunit_application', $cache);
798 $this->assertEquals('cachestore_dummy', $cache->phpunit_get_store_class());
800 $this->assertFalse($cache->get('test'));
801 $this->assertTrue($cache->set('test', 'test'));
802 $this->assertEquals('test', $cache->get('test'));
806 * Test disabling the cache.
808 public function test_disable() {
811 $configfile = $CFG->dataroot
.'/muc/config.php';
813 // That's right, we're deleting the config file.
814 $this->assertTrue(@unlink
($configfile));
817 cache_phpunit_factory
::phpunit_disable();
819 // Check we get the expected disabled factory.
820 $factory = cache_factory
::instance();
821 $this->assertInstanceOf('cache_factory_disabled', $factory);
823 // Check we get the expected disabled config.
824 $config = $factory->create_config_instance();
825 $this->assertInstanceOf('cache_config_disabled', $config);
827 // Check we get the expected disabled caches.
828 $cache = cache
::make('phpunit', 'disable');
829 $this->assertInstanceOf('cache_disabled', $cache);
831 $cache = cache
::make_from_params(cache_store
::MODE_APPLICATION
, 'phpunit', 'disable');
832 $this->assertInstanceOf('cache_disabled', $cache);
834 $this->assertFalse(file_exists($configfile));
836 $this->assertFalse($cache->get('test'));
837 $this->assertFalse($cache->set('test', 'test'));
838 $this->assertFalse($cache->delete('test'));
839 $this->assertTrue($cache->purge());
841 cache_factory
::reset();
843 $factory = cache_factory
::instance(true);
844 $config = $factory->create_config_instance();
845 $this->assertEquals('cache_config_phpunittest', get_class($config));
849 * Test that multiple loaders work ok.
851 public function test_multiple_loaders() {
852 $instance = cache_config_phpunittest
::instance(true);
853 $instance->phpunit_add_file_store('phpunittest1');
854 $instance->phpunit_add_file_store('phpunittest2');
855 $instance->phpunit_add_definition('phpunit/multi_loader', array(
856 'mode' => cache_store
::MODE_APPLICATION
,
857 'component' => 'phpunit',
858 'area' => 'multi_loader'
860 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
861 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
863 $cache = cache
::make('phpunit', 'multi_loader');
864 $this->assertInstanceOf('cache_application', $cache);
865 $this->assertFalse($cache->get('test'));
866 $this->assertTrue($cache->set('test', 'test'));
867 $this->assertEquals('test', $cache->get('test'));
871 * Test switching users with session caches.
873 public function test_session_cache_switch_user() {
874 $this->resetAfterTest(true);
875 $cache = cache
::make_from_params(cache_store
::MODE_SESSION
, 'phpunit', 'sessioncache');
876 $user1 = $this->getDataGenerator()->create_user();
877 $user2 = $this->getDataGenerator()->create_user();
879 // Log in as the first user.
880 $this->setUser($user1);
881 $sesskey1 = sesskey();
883 // Set a basic value in the cache.
884 $cache->set('var', 1);
885 $this->assertTrue($cache->has('var'));
886 $this->assertEquals(1, $cache->get('var'));
888 // Change to the second user.
889 $this->setUser($user2);
890 $sesskey2 = sesskey();
892 // Make sure the cache doesn't give us the data for the last user.
893 $this->assertNotEquals($sesskey1, $sesskey2);
894 $this->assertFalse($cache->has('var'));
895 $this->assertEquals(false, $cache->get('var'));
899 * Test multiple session caches when switching user.
901 public function test_session_cache_switch_user_multiple() {
902 $this->resetAfterTest(true);
903 $cache1 = cache
::make_from_params(cache_store
::MODE_SESSION
, 'phpunit', 'sessioncache1');
904 $cache2 = cache
::make_from_params(cache_store
::MODE_SESSION
, 'phpunit', 'sessioncache2');
905 $user1 = $this->getDataGenerator()->create_user();
906 $user2 = $this->getDataGenerator()->create_user();
908 // Log in as the first user.
909 $this->setUser($user1);
910 $sesskey1 = sesskey();
912 // Set a basic value in the caches.
913 $cache1->set('var', 1);
914 $cache2->set('var', 2);
915 $this->assertEquals(1, $cache1->get('var'));
916 $this->assertEquals(2, $cache2->get('var'));
918 // Change to the second user.
919 $this->setUser($user2);
920 $sesskey2 = sesskey();
922 // Make sure the cache doesn't give us the data for the last user.
923 // Also make sure that switching the user has lead to both caches being purged.
924 $this->assertNotEquals($sesskey1, $sesskey2);
925 $this->assertEquals(false, $cache1->get('var'));
926 $this->assertEquals(false, $cache2->get('var'));