MDL-36466 cache: implemented functionality to disable the bulk of the cache API
[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' => -10
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             'invalidationevents' => array(
557                 'crazyevent'
558             )
559         ));
560         $cache = cache::make('phpunit', 'eventinvalidationtest');
561         $this->assertTrue($cache->set('testkey1', 'test data 1'));
562         $this->assertEquals('test data 1', $cache->get('testkey1'));
564         cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
566         $this->assertFalse($cache->get('testkey1'));
568         // OK data added, data invalidated, and invalidation time has been set.
569         // Now we need to manually add back the data and adjust the invalidation time.
570         $timefile = $CFG->dataroot.'/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/a65/a65b1dc524cf6e03c1795197c84d5231eb229b86.cache';
571         $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
572         make_writable_directory(dirname($timefile));
573         file_put_contents($timefile, $timecont);
574         $this->assertTrue(file_exists($timefile));
576         $datafile = $CFG->dataroot.'/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/626/626e9c7a45febd98f064c2b383de8d9d4ebbde7b.cache';
577         $datacont = serialize("test data 1");
578         make_writable_directory(dirname($datafile));
579         file_put_contents($datafile, $datacont);
580         $this->assertTrue(file_exists($datafile));
582         // Test 1: Rebuild without the event and test its there.
583         cache_factory::reset();
584         $instance = cache_config_phpunittest::instance();
585         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
586             'mode' => cache_store::MODE_APPLICATION,
587             'component' => 'phpunit',
588             'area' => 'eventinvalidationtest',
589         ));
590         $cache = cache::make('phpunit', 'eventinvalidationtest');
591         $this->assertEquals('test data 1', $cache->get('testkey1'));
593         // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
594         cache_factory::reset();
595         $instance = cache_config_phpunittest::instance();
596         $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
597             'mode' => cache_store::MODE_APPLICATION,
598             'component' => 'phpunit',
599             'area' => 'eventinvalidationtest',
600             'invalidationevents' => array(
601                 'crazyevent'
602             )
603         ));
604         $cache = cache::make('phpunit', 'eventinvalidationtest');
605         $this->assertFalse($cache->get('testkey1'));
606     }
608     /**
609      * Tests application cache event purge
610      */
611     public function test_application_event_purge() {
612         $instance = cache_config_phpunittest::instance();
613         $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
614             'mode' => cache_store::MODE_APPLICATION,
615             'component' => 'phpunit',
616             'area' => 'eventpurgetest',
617             'invalidationevents' => array(
618                 'crazyevent'
619             )
620         ));
621         $cache = cache::make('phpunit', 'eventpurgetest');
623         $this->assertTrue($cache->set('testkey1', 'test data 1'));
624         $this->assertEquals('test data 1', $cache->get('testkey1'));
625         $this->assertTrue($cache->set('testkey2', 'test data 2'));
626         $this->assertEquals('test data 2', $cache->get('testkey2'));
628         // Purge the event.
629         cache_helper::purge_by_event('crazyevent');
631         // Check things have been removed.
632         $this->assertFalse($cache->get('testkey1'));
633         $this->assertFalse($cache->get('testkey2'));
634     }
636     /**
637      * Tests application cache definition purge
638      */
639     public function test_application_definition_purge() {
640         $instance = cache_config_phpunittest::instance();
641         $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
642             'mode' => cache_store::MODE_APPLICATION,
643             'component' => 'phpunit',
644             'area' => 'definitionpurgetest',
645             'invalidationevents' => array(
646                 'crazyevent'
647             )
648         ));
649         $cache = cache::make('phpunit', 'definitionpurgetest');
651         $this->assertTrue($cache->set('testkey1', 'test data 1'));
652         $this->assertEquals('test data 1', $cache->get('testkey1'));
653         $this->assertTrue($cache->set('testkey2', 'test data 2'));
654         $this->assertEquals('test data 2', $cache->get('testkey2'));
656         // Purge the event.
657         cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
659         // Check things have been removed.
660         $this->assertFalse($cache->get('testkey1'));
661         $this->assertFalse($cache->get('testkey2'));
662     }
664     /**
665      * Test the use of an alt path.
666      * If we can generate a config instance we are done :)
667      */
668     public function test_alt_cache_path() {
669         global $CFG;
670         $this->resetAfterTest();
671         $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
672         $instance = cache_config_phpunittest::instance();
673         $this->assertInstanceOf('cache_config', $instance);
674     }
676     /**
677      * Test disabling the cache stores.
678      */
679     public function test_disable_stores() {
680         $instance = cache_config_phpunittest::instance();
681         $instance->phpunit_add_definition('phpunit/disabletest', array(
682             'mode' => cache_store::MODE_APPLICATION,
683             'component' => 'phpunit',
684             'area' => 'disabletest'
685         ));
686         $cache = cache::make('phpunit', 'disabletest');
687         $this->assertInstanceOf('cache_phpunit_application', $cache);
688         $this->assertEquals('cachestore_file', $cache->phpunit_get_store_class());
690         $this->assertFalse($cache->get('test'));
691         $this->assertTrue($cache->set('test', 'test'));
692         $this->assertEquals('test', $cache->get('test'));
694         cache_factory::disable_stores();
696         $cache = cache::make('phpunit', 'disabletest');
697         $this->assertInstanceOf('cache_phpunit_application', $cache);
698         $this->assertEquals('cachestore_dummy', $cache->phpunit_get_store_class());
700         $this->assertFalse($cache->get('test'));
701         $this->assertTrue($cache->set('test', 'test'));
702         $this->assertEquals('test', $cache->get('test'));
703     }
705     /**
706      * Test disabling the cache.
707      */
708     public function test_disable() {
709         global $CFG;
711         $configfile = $CFG->dataroot.'/muc/config.php';
713         // That's right, we're deleting the config file.
714         $this->assertTrue(@unlink($configfile));
716         // Disable the cache
717         cache_factory::disable();
719         // Check we get the expected disabled factory.
720         $factory = cache_factory::instance();
721         $this->assertInstanceOf('cache_factory_disabled', $factory);
723         // Check we get the expected disabled config.
724         $config = $factory->create_config_instance();
725         $this->assertInstanceOf('cache_config_disabled', $config);
727         // Check we get the expected disabled caches.
728         $cache = cache::make('phpunit', 'disable');
729         $this->assertInstanceOf('cache_disabled', $cache);
731         $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
732         $this->assertInstanceOf('cache_disabled', $cache);
734         $this->assertFalse(file_exists($configfile));
736         $this->assertFalse($cache->get('test'));
737         $this->assertFalse($cache->set('test', 'test'));
738         $this->assertFalse($cache->delete('test'));
739         $this->assertTrue($cache->purge());
741         cache_factory::reset();
743         $factory = cache_factory::instance(true);
744         $config = $factory->create_config_instance();
745         $this->assertEquals('cache_config_phpunittest', get_class($config));
746     }