Merge branch 'wip-MDL-38110-m25' of git://github.com/samhemelryk/moodle
[moodle.git] / cache / tests / cache_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * PHPunit tests for the cache API
19  *
20  * This file is part of Moodle's cache API, affectionately called MUC.
21  * It contains the components that are requried in order to use caching.
22  *
23  * @package    core
24  * @category   cache
25  * @copyright  2012 Sam Hemelryk
26  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27  */
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
38  *
39  * @copyright  2012 Sam Hemelryk
40  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  */
42 class cache_phpunit_tests extends advanced_testcase {
44     /**
45      * Set things back to the default before each test.
46      */
47     public function setUp() {
48         parent::setUp();
49         cache_factory::reset();
50         cache_config_phpunittest::create_default_configuration();
51     }
53     /**
54      * Final task is to reset the cache system
55      */
56     public static function tearDownAfterClass() {
57         parent::tearDownAfterClass();
58         cache_factory::reset();
59     }
61     /**
62      * Tests cache configuration
63      */
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);
85         }
87         $modemappings = $instance->get_mode_mappings();
88         $this->assertCount(3, $modemappings);
89         $modes = array(
90             cache_store::MODE_APPLICATION => false,
91             cache_store::MODE_SESSION => false,
92             cache_store::MODE_REQUEST => false,
93         );
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);
100             // Record the mode.
101             $modes[$mapping['mode']] = true;
102         }
104         // Must have the default 3 modes and no more.
105         $this->assertCount(3, $mapping);
106         foreach ($modes as $mode) {
107             $this->assertTrue($mode);
108         }
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);
119         }
120     }
122     /**
123      * Tests the default application cache
124      */
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);
129     }
131     /**
132      * Tests the default session cache
133      */
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);
138     }
140     /**
141      * Tests the default request cache
142      */
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);
147     }
149     /**
150      * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
151      */
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',
158         ));
159         $instance->phpunit_add_definition('phpunit/nostoretest2', array(
160             'mode' => cache_store::MODE_APPLICATION,
161             'component' => 'phpunit',
162             'area' => 'nostoretest2',
163             'persistent' => true
164         ));
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);
172     }
174     /**
175      * Runs a standard series of access and use tests on a cache instance.
176      *
177      * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
178      *
179      * @param cache_loader $cache
180      */
181     protected function run_on_cache(cache_loader $cache) {
182         $key = 'testkey';
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)));
198         // Set the data.
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)));
212         // Delete it.
213         $this->assertTrue($cache->delete($key));
215         // Check its gone.
216         $this->assertFalse($cache->get($key));
217         $this->assertFalse($cache->has($key));
219         // Test arrays.
220         $this->assertTrue($cache->set($key, $dataarray));
221         $this->assertEquals($dataarray, $cache->get($key));
223         // Test objects.
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 set many.
235         $cache->set_many(array('key1' => 'data1', 'key2' => 'data2'));
236         $this->assertEquals('data1', $cache->get('key1'));
237         $this->assertEquals('data2', $cache->get('key2'));
238         $this->assertTrue($cache->delete('key1'));
239         $this->assertTrue($cache->delete('key2'));
241         // Test delete many.
242         $this->assertTrue($cache->set('key1', 'data1'));
243         $this->assertTrue($cache->set('key2', 'data2'));
245         $this->assertEquals('data1', $cache->get('key1'));
246         $this->assertEquals('data2', $cache->get('key2'));
248         $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
250         $this->assertFalse($cache->get('key1'));
251         $this->assertFalse($cache->get('key2'));
253         // Quick reference test.
254         $obj = new stdClass;
255         $obj->key = 'value';
256         $ref =& $obj;
257         $this->assertTrue($cache->set('obj', $obj));
259         $obj->key = 'eulav';
260         $var = $cache->get('obj');
261         $this->assertInstanceOf('stdClass', $var);
262         $this->assertEquals('value', $var->key);
264         $ref->key = 'eulav';
265         $var = $cache->get('obj');
266         $this->assertInstanceOf('stdClass', $var);
267         $this->assertEquals('value', $var->key);
269         $this->assertTrue($cache->delete('obj'));
271         // Deep reference test.
272         $obj1 = new stdClass;
273         $obj1->key = 'value';
274         $obj2 = new stdClass;
275         $obj2->key = 'test';
276         $obj3 = new stdClass;
277         $obj3->key = 'pork';
278         $obj1->subobj =& $obj2;
279         $obj2->subobj =& $obj3;
280         $this->assertTrue($cache->set('obj', $obj1));
282         $obj1->key = 'eulav';
283         $obj2->key = 'tset';
284         $obj3->key = 'krop';
285         $var = $cache->get('obj');
286         $this->assertInstanceOf('stdClass', $var);
287         $this->assertEquals('value', $var->key);
288         $this->assertInstanceOf('stdClass', $var->subobj);
289         $this->assertEquals('test', $var->subobj->key);
290         $this->assertInstanceOf('stdClass', $var->subobj->subobj);
291         $this->assertEquals('pork', $var->subobj->subobj->key);
292         $this->assertTrue($cache->delete('obj'));
294         // Death reference test... basicaly we don't want this to die.
295         $obj = new stdClass;
296         $obj->key = 'value';
297         $obj->self =& $obj;
298         $this->assertTrue($cache->set('obj', $obj));
299         $var = $cache->get('obj');
300         $this->assertInstanceOf('stdClass', $var);
301         $this->assertEquals('value', $var->key);
303         // Reference test after retrieve.
304         $obj = new stdClass;
305         $obj->key = 'value';
306         $this->assertTrue($cache->set('obj', $obj));
308         $var1 = $cache->get('obj');
309         $this->assertInstanceOf('stdClass', $var1);
310         $this->assertEquals('value', $var1->key);
311         $var1->key = 'eulav';
312         $this->assertEquals('eulav', $var1->key);
314         $var2 = $cache->get('obj');
315         $this->assertInstanceOf('stdClass', $var2);
316         $this->assertEquals('value', $var2->key);
318         $this->assertTrue($cache->delete('obj'));
319     }
321     /**
322      * Tests a definition using a data loader
323      */
324     public function test_definition_data_loader() {
325         $instance = cache_config_phpunittest::instance(true);
326         $instance->phpunit_add_definition('phpunit/datasourcetest', array(
327             'mode' => cache_store::MODE_APPLICATION,
328             'component' => 'phpunit',
329             'area' => 'datasourcetest',
330             'datasource' => 'cache_phpunit_dummy_datasource',
331             'datasourcefile' => 'cache/tests/fixtures/lib.php'
332         ));
334         $cache = cache::make('phpunit', 'datasourcetest');
335         $this->assertInstanceOf('cache_application', $cache);
337         // Purge it to be sure.
338         $this->assertTrue($cache->purge());
339         // It won't be there yet.
340         $this->assertFalse($cache->has('Test'));
342         // It should load it ;).
343         $this->assertTrue($cache->has('Test', true));
345         // Purge it to be sure.
346         $this->assertTrue($cache->purge());
347         $this->assertEquals('Test has no value really.', $cache->get('Test'));
348     }
350     /**
351      * Tests a definition using an overridden loader
352      */
353     public function test_definition_overridden_loader() {
354         $instance = cache_config_phpunittest::instance(true);
355         $instance->phpunit_add_definition('phpunit/overridetest', array(
356             'mode' => cache_store::MODE_APPLICATION,
357             'component' => 'phpunit',
358             'area' => 'overridetest',
359             'overrideclass' => 'cache_phpunit_dummy_overrideclass',
360             'overrideclassfile' => 'cache/tests/fixtures/lib.php'
361         ));
362         $cache = cache::make('phpunit', 'overridetest');
363         $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
364         $this->assertInstanceOf('cache_application', $cache);
365         // Purge it to be sure.
366         $this->assertTrue($cache->purge());
367         // It won't be there yet.
368         $this->assertFalse($cache->has('Test'));
369         // Add it.
370         $this->assertTrue($cache->set('Test', 'Test has no value really.'));
371         // Check its there.
372         $this->assertEquals('Test has no value really.', $cache->get('Test'));
373     }
375     /**
376      * Test a very basic definition.
377      */
378     public function test_definition() {
379         $instance = cache_config_phpunittest::instance();
380         $instance->phpunit_add_definition('phpunit/test', array(
381             'mode' => cache_store::MODE_APPLICATION,
382             'component' => 'phpunit',
383             'area' => 'test',
384         ));
385         $cache = cache::make('phpunit', 'test');
387         $this->assertTrue($cache->set('testkey1', 'test data 1'));
388         $this->assertEquals('test data 1', $cache->get('testkey1'));
389         $this->assertTrue($cache->set('testkey2', 'test data 2'));
390         $this->assertEquals('test data 2', $cache->get('testkey2'));
391     }
393     /**
394      * Test a definition using the simple keys.
395      */
396     public function test_definition_simplekeys() {
397         $instance = cache_config_phpunittest::instance();
398         $instance->phpunit_add_definition('phpunit/simplekeytest', array(
399             'mode' => cache_store::MODE_APPLICATION,
400             'component' => 'phpunit',
401             'area' => 'simplekeytest',
402             'simplekeys' => true
403         ));
404         $cache = cache::make('phpunit', 'simplekeytest');
406         $this->assertTrue($cache->set('testkey1', 'test data 1'));
407         $this->assertEquals('test data 1', $cache->get('testkey1'));
408         $this->assertTrue($cache->set('testkey2', 'test data 2'));
409         $this->assertEquals('test data 2', $cache->get('testkey2'));
411         $cache->purge();
413         $this->assertTrue($cache->set('1', 'test data 1'));
414         $this->assertEquals('test data 1', $cache->get('1'));
415         $this->assertTrue($cache->set('2', 'test data 2'));
416         $this->assertEquals('test data 2', $cache->get('2'));
417     }
419     public function test_definition_ttl() {
420         $instance = cache_config_phpunittest::instance(true);
421         $instance->phpunit_add_definition('phpunit/ttltest', array(
422             'mode' => cache_store::MODE_APPLICATION,
423             'component' => 'phpunit',
424             'area' => 'ttltest',
425             'ttl' => -86400 // Set to a day in the past to be extra sure.
426         ));
427         $cache = cache::make('phpunit', 'ttltest');
428         $this->assertInstanceOf('cache_application', $cache);
430         // Purge it to be sure.
431         $this->assertTrue($cache->purge());
432         // It won't be there yet.
433         $this->assertFalse($cache->has('Test'));
434         // Set it now.
435         $this->assertTrue($cache->set('Test', 'Test'));
436         // Check its not there.
437         $this->assertFalse($cache->has('Test'));
438         // Double check by trying to get it.
439         $this->assertFalse($cache->get('Test'));
440     }
442     /**
443      * Tests manual locking operations on an application cache
444      */
445     public function test_application_manual_locking() {
446         $instance = cache_config_phpunittest::instance();
447         $instance->phpunit_add_definition('phpunit/lockingtest', array(
448             'mode' => cache_store::MODE_APPLICATION,
449             'component' => 'phpunit',
450             'area' => 'lockingtest'
451         ));
452         $cache1 = cache::make('phpunit', 'lockingtest');
453         $cache2 = clone($cache1);
455         $this->assertTrue($cache1->set('testkey', 'test data'));
456         $this->assertTrue($cache2->set('testkey', 'test data'));
458         $this->assertTrue($cache1->acquire_lock('testkey'));
459         $this->assertFalse($cache2->acquire_lock('testkey'));
461         $this->assertTrue($cache1->check_lock_state('testkey'));
462         $this->assertFalse($cache2->check_lock_state('testkey'));
464         $this->assertTrue($cache1->release_lock('testkey'));
465         $this->assertFalse($cache2->release_lock('testkey'));
467         $this->assertTrue($cache1->set('testkey', 'test data'));
468         $this->assertTrue($cache2->set('testkey', 'test data'));
469     }
471     /**
472      * Tests application cache event invalidation
473      */
474     public function test_application_event_invalidation() {
475         $instance = cache_config_phpunittest::instance();
476         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
477             'mode' => cache_store::MODE_APPLICATION,
478             'component' => 'phpunit',
479             'area' => 'eventinvalidationtest',
480             'invalidationevents' => array(
481                 'crazyevent'
482             )
483         ));
484         $cache = cache::make('phpunit', 'eventinvalidationtest');
486         $this->assertTrue($cache->set('testkey1', 'test data 1'));
487         $this->assertEquals('test data 1', $cache->get('testkey1'));
488         $this->assertTrue($cache->set('testkey2', 'test data 2'));
489         $this->assertEquals('test data 2', $cache->get('testkey2'));
491         // Test invalidating a single entry.
492         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
494         $this->assertFalse($cache->get('testkey1'));
495         $this->assertEquals('test data 2', $cache->get('testkey2'));
497         $this->assertTrue($cache->set('testkey1', 'test data 1'));
499         // Test invalidating both entries.
500         cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
502         $this->assertFalse($cache->get('testkey1'));
503         $this->assertFalse($cache->get('testkey2'));
504     }
506     /**
507      * Tests application cache definition invalidation
508      */
509     public function test_application_definition_invalidation() {
510         $instance = cache_config_phpunittest::instance();
511         $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
512             'mode' => cache_store::MODE_APPLICATION,
513             'component' => 'phpunit',
514             'area' => 'definitioninvalidation'
515         ));
516         $cache = cache::make('phpunit', 'definitioninvalidation');
517         $this->assertTrue($cache->set('testkey1', 'test data 1'));
518         $this->assertEquals('test data 1', $cache->get('testkey1'));
519         $this->assertTrue($cache->set('testkey2', 'test data 2'));
520         $this->assertEquals('test data 2', $cache->get('testkey2'));
522         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
524         $this->assertFalse($cache->get('testkey1'));
525         $this->assertEquals('test data 2', $cache->get('testkey2'));
527         $this->assertTrue($cache->set('testkey1', 'test data 1'));
529         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
531         $this->assertFalse($cache->get('testkey1'));
532         $this->assertEquals('test data 2', $cache->get('testkey2'));
534         $this->assertTrue($cache->set('testkey1', 'test data 1'));
536         cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
538         $this->assertFalse($cache->get('testkey1'));
539         $this->assertFalse($cache->get('testkey2'));
540     }
542     /**
543      * Tests application cache event invalidation over a distributed setup.
544      */
545     public function test_distributed_application_event_invalidation() {
546         global $CFG;
547         // This is going to be an intense wee test.
548         // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
549         // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
550         // check that it is not picked up.
551         $instance = cache_config_phpunittest::instance();
552         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
553             'mode' => cache_store::MODE_APPLICATION,
554             'component' => 'phpunit',
555             'area' => 'eventinvalidationtest',
556             'simplekeys' => true,
557             'simpledata' => true,
558             'invalidationevents' => array(
559                 'crazyevent'
560             )
561         ));
562         $cache = cache::make('phpunit', 'eventinvalidationtest');
563         $this->assertTrue($cache->set('testkey1', 'test data 1'));
564         $this->assertEquals('test data 1', $cache->get('testkey1'));
566         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
568         $this->assertFalse($cache->get('testkey1'));
570         // OK data added, data invalidated, and invalidation time has been set.
571         // Now we need to manually add back the data and adjust the invalidation time.
572         $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit');
573         $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las/lastinvalidation-$hash.cache";
574         // Make sure the file is correct.
575         $this->assertTrue(file_exists($timefile));
576         $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
577         make_writable_directory(dirname($timefile));
578         file_put_contents($timefile, $timecont);
579         $this->assertTrue(file_exists($timefile));
581         $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes/testkey1-$hash.cache";
582         $datacont = serialize("test data 1");
583         make_writable_directory(dirname($datafile));
584         file_put_contents($datafile, $datacont);
585         $this->assertTrue(file_exists($datafile));
587         // Test 1: Rebuild without the event and test its there.
588         cache_factory::reset();
589         $instance = cache_config_phpunittest::instance();
590         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
591             'mode' => cache_store::MODE_APPLICATION,
592             'component' => 'phpunit',
593             'area' => 'eventinvalidationtest',
594             'simplekeys' => true,
595             'simpledata' => true,
596         ));
597         $cache = cache::make('phpunit', 'eventinvalidationtest');
598         $this->assertEquals('test data 1', $cache->get('testkey1'));
600         // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
601         cache_factory::reset();
602         $instance = cache_config_phpunittest::instance();
603         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
604             'mode' => cache_store::MODE_APPLICATION,
605             'component' => 'phpunit',
606             'area' => 'eventinvalidationtest',
607             'simplekeys' => true,
608             'simpledata' => true,
609             'invalidationevents' => array(
610                 'crazyevent'
611             )
612         ));
613         $cache = cache::make('phpunit', 'eventinvalidationtest');
614         $this->assertFalse($cache->get('testkey1'));
615     }
617     /**
618      * Tests application cache event purge
619      */
620     public function test_application_event_purge() {
621         $instance = cache_config_phpunittest::instance();
622         $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
623             'mode' => cache_store::MODE_APPLICATION,
624             'component' => 'phpunit',
625             'area' => 'eventpurgetest',
626             'invalidationevents' => array(
627                 'crazyevent'
628             )
629         ));
630         $cache = cache::make('phpunit', 'eventpurgetest');
632         $this->assertTrue($cache->set('testkey1', 'test data 1'));
633         $this->assertEquals('test data 1', $cache->get('testkey1'));
634         $this->assertTrue($cache->set('testkey2', 'test data 2'));
635         $this->assertEquals('test data 2', $cache->get('testkey2'));
637         // Purge the event.
638         cache_helper::purge_by_event('crazyevent');
640         // Check things have been removed.
641         $this->assertFalse($cache->get('testkey1'));
642         $this->assertFalse($cache->get('testkey2'));
643     }
645     /**
646      * Tests application cache definition purge
647      */
648     public function test_application_definition_purge() {
649         $instance = cache_config_phpunittest::instance();
650         $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
651             'mode' => cache_store::MODE_APPLICATION,
652             'component' => 'phpunit',
653             'area' => 'definitionpurgetest',
654             'invalidationevents' => array(
655                 'crazyevent'
656             )
657         ));
658         $cache = cache::make('phpunit', 'definitionpurgetest');
660         $this->assertTrue($cache->set('testkey1', 'test data 1'));
661         $this->assertEquals('test data 1', $cache->get('testkey1'));
662         $this->assertTrue($cache->set('testkey2', 'test data 2'));
663         $this->assertEquals('test data 2', $cache->get('testkey2'));
665         // Purge the event.
666         cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
668         // Check things have been removed.
669         $this->assertFalse($cache->get('testkey1'));
670         $this->assertFalse($cache->get('testkey2'));
671     }
673     /**
674      * Test the use of an alt path.
675      * If we can generate a config instance we are done :)
676      */
677     public function test_alt_cache_path() {
678         global $CFG;
679         $this->resetAfterTest();
680         $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
681         $instance = cache_config_phpunittest::instance();
682         $this->assertInstanceOf('cache_config', $instance);
683     }
685     /**
686      * Test disabling the cache stores.
687      */
688     public function test_disable_stores() {
689         $instance = cache_config_phpunittest::instance();
690         $instance->phpunit_add_definition('phpunit/disabletest', array(
691             'mode' => cache_store::MODE_APPLICATION,
692             'component' => 'phpunit',
693             'area' => 'disabletest'
694         ));
695         $cache = cache::make('phpunit', 'disabletest');
696         $this->assertInstanceOf('cache_phpunit_application', $cache);
697         $this->assertEquals('cachestore_file', $cache->phpunit_get_store_class());
699         $this->assertFalse($cache->get('test'));
700         $this->assertTrue($cache->set('test', 'test'));
701         $this->assertEquals('test', $cache->get('test'));
703         cache_factory::disable_stores();
705         $cache = cache::make('phpunit', 'disabletest');
706         $this->assertInstanceOf('cache_phpunit_application', $cache);
707         $this->assertEquals('cachestore_dummy', $cache->phpunit_get_store_class());
709         $this->assertFalse($cache->get('test'));
710         $this->assertTrue($cache->set('test', 'test'));
711         $this->assertEquals('test', $cache->get('test'));
712     }
714     /**
715      * Test disabling the cache.
716      */
717     public function test_disable() {
718         global $CFG;
720         $configfile = $CFG->dataroot.'/muc/config.php';
722         // That's right, we're deleting the config file.
723         $this->assertTrue(@unlink($configfile));
725         // Disable the cache
726         cache_phpunit_factory::phpunit_disable();
728         // Check we get the expected disabled factory.
729         $factory = cache_factory::instance();
730         $this->assertInstanceOf('cache_factory_disabled', $factory);
732         // Check we get the expected disabled config.
733         $config = $factory->create_config_instance();
734         $this->assertInstanceOf('cache_config_disabled', $config);
736         // Check we get the expected disabled caches.
737         $cache = cache::make('phpunit', 'disable');
738         $this->assertInstanceOf('cache_disabled', $cache);
740         $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
741         $this->assertInstanceOf('cache_disabled', $cache);
743         $this->assertFalse(file_exists($configfile));
745         $this->assertFalse($cache->get('test'));
746         $this->assertFalse($cache->set('test', 'test'));
747         $this->assertFalse($cache->delete('test'));
748         $this->assertTrue($cache->purge());
750         cache_factory::reset();
752         $factory = cache_factory::instance(true);
753         $config = $factory->create_config_instance();
754         $this->assertEquals('cache_config_phpunittest', get_class($config));
755     }
757     /**
758      * Test that multiple loaders work ok.
759      */
760     public function test_multiple_loaders() {
761         $instance = cache_config_phpunittest::instance(true);
762         $instance->phpunit_add_file_store('phpunittest1');
763         $instance->phpunit_add_file_store('phpunittest2');
764         $instance->phpunit_add_definition('phpunit/multi_loader', array(
765             'mode' => cache_store::MODE_APPLICATION,
766             'component' => 'phpunit',
767             'area' => 'multi_loader'
768         ));
769         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
770         $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
772         $cache = cache::make('phpunit', 'multi_loader');
773         $this->assertInstanceOf('cache_application', $cache);
774         $this->assertFalse($cache->get('test'));
775         $this->assertTrue($cache->set('test', 'test'));
776         $this->assertEquals('test', $cache->get('test'));
777     }