MDL-36466 cache: implemented functionality to disable the bulk of the cache API
[moodle.git] / cache / tests / cache_test.php
CommitLineData
8139ad13
SH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * PHPunit tests for the cache API
19 *
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 * It contains the components that are requried in order to use caching.
22 *
23 * @package core
24 * @category cache
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29defined('MOODLE_INTERNAL') || die();
30
170f821b 31// Include the necessary evils.
8139ad13
SH
32global $CFG;
33require_once($CFG->dirroot.'/cache/locallib.php');
34require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
35
36/**
37 * PHPunit tests for the cache API
38 *
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class cache_phpunit_tests extends advanced_testcase {
43
44 /**
45 * Set things back to the default before each test.
46 */
47 public function setUp() {
48 parent::setUp();
49 cache_factory::reset();
8139ad13
SH
50 cache_config_phpunittest::create_default_configuration();
51 }
52
3680c61a
SH
53 /**
54 * Final task is to reset the cache system
55 */
56 public static function tearDownAfterClass() {
57 parent::tearDownAfterClass();
58 cache_factory::reset();
59 }
60
8139ad13
SH
61 /**
62 * Tests cache configuration
63 */
64 public function test_cache_config() {
65 $instance = cache_config::instance();
66 $this->assertInstanceOf('cache_config_phpunittest', $instance);
67
68 $this->assertTrue(cache_config_phpunittest::config_file_exists());
69
70 $stores = $instance->get_all_stores();
8dbfbd71 71 $this->assertCount(3, $stores);
8139ad13 72 foreach ($stores as $name => $store) {
170f821b 73 // Check its an array.
8139ad13 74 $this->assertInternalType('array', $store);
170f821b 75 // Check the name is the key.
8139ad13 76 $this->assertEquals($name, $store['name']);
170f821b 77 // Check that it has been declared default.
8139ad13 78 $this->assertTrue($store['default']);
170f821b 79 // Required attributes = name + plugin + configuration + modes + features.
8139ad13
SH
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 }
86
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) {
170f821b 95 // We expect 3 properties.
8139ad13 96 $this->assertCount(3, $mapping);
170f821b 97 // Required attributes = mode + store.
8139ad13
SH
98 $this->assertArrayHasKey('mode', $mapping);
99 $this->assertArrayHasKey('store', $mapping);
170f821b 100 // Record the mode.
8139ad13
SH
101 $modes[$mapping['mode']] = true;
102 }
103
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 }
109
110 $definitions = $instance->get_definitions();
8139ad13
SH
111 // The event invalidation definition is required for the cache API and must be there.
112 $this->assertArrayHasKey('core/eventinvalidation', $definitions);
113
114 $definitionmappings = $instance->get_definition_mappings();
8139ad13 115 foreach ($definitionmappings as $mapping) {
170f821b 116 // Required attributes = definition + store.
8139ad13
SH
117 $this->assertArrayHasKey('definition', $mapping);
118 $this->assertArrayHasKey('store', $mapping);
8139ad13 119 }
8139ad13
SH
120 }
121
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 }
130
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 }
139
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 }
148
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();
166
167 $cache = cache::make('phpunit', 'nostoretest1');
168 $this->run_on_cache($cache);
169
170 $cache = cache::make('phpunit', 'nostoretest2');
171 $this->run_on_cache($cache);
172 }
173
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;
186
187 $this->assertTrue($cache->purge());
188
170f821b 189 // Check all read methods.
8139ad13
SH
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)));
197
170f821b 198 // Set the data.
8139ad13 199 $this->assertTrue($cache->set($key, $datascalar));
170f821b 200 // Setting it more than once should be permitted.
8139ad13
SH
201 $this->assertTrue($cache->set($key, $datascalar));
202
170f821b 203 // Recheck the read methods.
8139ad13
SH
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)));
211
170f821b 212 // Delete it.
8139ad13
SH
213 $this->assertTrue($cache->delete($key));
214
170f821b 215 // Check its gone.
8139ad13
SH
216 $this->assertFalse($cache->get($key));
217 $this->assertFalse($cache->has($key));
218
170f821b 219 // Test arrays.
8139ad13
SH
220 $this->assertTrue($cache->set($key, $dataarray));
221 $this->assertEquals($dataarray, $cache->get($key));
222
170f821b 223 // Test objects.
8139ad13
SH
224 $this->assertTrue($cache->set($key, $dataobject));
225 $this->assertEquals($dataobject, $cache->get($key));
226
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);
233
170f821b 234 // Test set many.
8139ad13
SH
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'));
240
170f821b 241 // Test delete many.
8139ad13
SH
242 $this->assertTrue($cache->set('key1', 'data1'));
243 $this->assertTrue($cache->set('key2', 'data2'));
244
245 $this->assertEquals('data1', $cache->get('key1'));
246 $this->assertEquals('data2', $cache->get('key2'));
247
248 $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
249
250 $this->assertFalse($cache->get('key1'));
251 $this->assertFalse($cache->get('key2'));
8dbfbd71
SH
252
253 // Quick reference test.
254 $obj = new stdClass;
255 $obj->key = 'value';
256 $ref =& $obj;
257 $this->assertTrue($cache->set('obj', $obj));
258
259 $obj->key = 'eulav';
260 $var = $cache->get('obj');
261 $this->assertInstanceOf('stdClass', $var);
262 $this->assertEquals('value', $var->key);
263
264 $ref->key = 'eulav';
265 $var = $cache->get('obj');
266 $this->assertInstanceOf('stdClass', $var);
267 $this->assertEquals('value', $var->key);
268
269 $this->assertTrue($cache->delete('obj'));
270
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));
281
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'));
293
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);
267ebe02
SH
302
303 // Reference test after retrieve.
304 $obj = new stdClass;
305 $obj->key = 'value';
306 $this->assertTrue($cache->set('obj', $obj));
307
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);
313
314 $var2 = $cache->get('obj');
315 $this->assertInstanceOf('stdClass', $var2);
316 $this->assertEquals('value', $var2->key);
317
318 $this->assertTrue($cache->delete('obj'));
8139ad13
SH
319 }
320
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',
e0a568e2
SH
330 'datasource' => 'cache_phpunit_dummy_datasource',
331 'datasourcefile' => 'cache/tests/fixtures/lib.php'
8139ad13
SH
332 ));
333
334 $cache = cache::make('phpunit', 'datasourcetest');
335 $this->assertInstanceOf('cache_application', $cache);
336
170f821b 337 // Purge it to be sure.
8139ad13 338 $this->assertTrue($cache->purge());
170f821b 339 // It won't be there yet.
8139ad13
SH
340 $this->assertFalse($cache->has('Test'));
341
170f821b 342 // It should load it ;).
8139ad13
SH
343 $this->assertTrue($cache->has('Test', true));
344
170f821b 345 // Purge it to be sure.
8139ad13
SH
346 $this->assertTrue($cache->purge());
347 $this->assertEquals('Test has no value really.', $cache->get('Test'));
348 }
349
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',
e0a568e2
SH
359 'overrideclass' => 'cache_phpunit_dummy_overrideclass',
360 'overrideclassfile' => 'cache/tests/fixtures/lib.php'
8139ad13
SH
361 ));
362 $cache = cache::make('phpunit', 'overridetest');
363 $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
364 $this->assertInstanceOf('cache_application', $cache);
170f821b 365 // Purge it to be sure.
8139ad13 366 $this->assertTrue($cache->purge());
170f821b 367 // It won't be there yet.
8139ad13 368 $this->assertFalse($cache->has('Test'));
170f821b 369 // Add it.
8139ad13 370 $this->assertTrue($cache->set('Test', 'Test has no value really.'));
170f821b 371 // Check its there.
8139ad13
SH
372 $this->assertEquals('Test has no value really.', $cache->get('Test'));
373 }
374
47834bcd
SH
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');
386
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 }
392
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');
405
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'));
702651c7
SH
410
411 $cache->purge();
412
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'));
47834bcd
SH
417 }
418
42f2c59e
SH
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);
429
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 }
441
8139ad13
SH
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);
454
455 $this->assertTrue($cache1->set('testkey', 'test data'));
456 $this->assertTrue($cache2->set('testkey', 'test data'));
457
458 $this->assertTrue($cache1->acquire_lock('testkey'));
459 $this->assertFalse($cache2->acquire_lock('testkey'));
460
34c84c72
SH
461 $this->assertTrue($cache1->check_lock_state('testkey'));
462 $this->assertFalse($cache2->check_lock_state('testkey'));
8139ad13
SH
463
464 $this->assertTrue($cache1->release_lock('testkey'));
465 $this->assertFalse($cache2->release_lock('testkey'));
466
467 $this->assertTrue($cache1->set('testkey', 'test data'));
468 $this->assertTrue($cache2->set('testkey', 'test data'));
469 }
470
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');
485
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'));
490
170f821b 491 // Test invalidating a single entry.
8139ad13
SH
492 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
493
494 $this->assertFalse($cache->get('testkey1'));
495 $this->assertEquals('test data 2', $cache->get('testkey2'));
496
497 $this->assertTrue($cache->set('testkey1', 'test data 1'));
498
170f821b 499 // Test invalidating both entries.
8139ad13
SH
500 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
501
502 $this->assertFalse($cache->get('testkey1'));
503 $this->assertFalse($cache->get('testkey2'));
504 }
505
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'));
521
522 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
170f821b 523
8139ad13
SH
524 $this->assertFalse($cache->get('testkey1'));
525 $this->assertEquals('test data 2', $cache->get('testkey2'));
526
527 $this->assertTrue($cache->set('testkey1', 'test data 1'));
528
529 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
530
531 $this->assertFalse($cache->get('testkey1'));
532 $this->assertEquals('test data 2', $cache->get('testkey2'));
533
534 $this->assertTrue($cache->set('testkey1', 'test data 1'));
535
536 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
537
538 $this->assertFalse($cache->get('testkey1'));
539 $this->assertFalse($cache->get('testkey2'));
540 }
541
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.
8139ad13
SH
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'));
563
564 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
565
566 $this->assertFalse($cache->get('testkey1'));
567
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.
702651c7 570 $timefile = $CFG->dataroot.'/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/a65/a65b1dc524cf6e03c1795197c84d5231eb229b86.cache';
170f821b 571 $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
47834bcd 572 make_writable_directory(dirname($timefile));
8139ad13
SH
573 file_put_contents($timefile, $timecont);
574 $this->assertTrue(file_exists($timefile));
575
702651c7 576 $datafile = $CFG->dataroot.'/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/626/626e9c7a45febd98f064c2b383de8d9d4ebbde7b.cache';
8139ad13 577 $datacont = serialize("test data 1");
47834bcd 578 make_writable_directory(dirname($datafile));
8139ad13
SH
579 file_put_contents($datafile, $datacont);
580 $this->assertTrue(file_exists($datafile));
581
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'));
592
170f821b 593 // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
8139ad13
SH
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 }
607
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');
622
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'));
627
628 // Purge the event.
629 cache_helper::purge_by_event('crazyevent');
630
631 // Check things have been removed.
632 $this->assertFalse($cache->get('testkey1'));
633 $this->assertFalse($cache->get('testkey2'));
634 }
635
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');
650
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'));
655
656 // Purge the event.
657 cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
658
659 // Check things have been removed.
660 $this->assertFalse($cache->get('testkey1'));
661 $this->assertFalse($cache->get('testkey2'));
662 }
42f2c59e
SH
663
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 }
82afd05c
SH
675
676 /**
3680c61a 677 * Test disabling the cache stores.
82afd05c
SH
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());
689
690 $this->assertFalse($cache->get('test'));
691 $this->assertTrue($cache->set('test', 'test'));
692 $this->assertEquals('test', $cache->get('test'));
693
694 cache_factory::disable_stores();
695
696 $cache = cache::make('phpunit', 'disabletest');
697 $this->assertInstanceOf('cache_phpunit_application', $cache);
698 $this->assertEquals('cachestore_dummy', $cache->phpunit_get_store_class());
699
700 $this->assertFalse($cache->get('test'));
701 $this->assertTrue($cache->set('test', 'test'));
702 $this->assertEquals('test', $cache->get('test'));
703 }
3680c61a
SH
704
705 /**
706 * Test disabling the cache.
707 */
708 public function test_disable() {
709 global $CFG;
710
711 $configfile = $CFG->dataroot.'/muc/config.php';
712
713 // That's right, we're deleting the config file.
714 $this->assertTrue(@unlink($configfile));
715
716 // Disable the cache
717 cache_factory::disable();
718
719 // Check we get the expected disabled factory.
720 $factory = cache_factory::instance();
721 $this->assertInstanceOf('cache_factory_disabled', $factory);
722
723 // Check we get the expected disabled config.
724 $config = $factory->create_config_instance();
725 $this->assertInstanceOf('cache_config_disabled', $config);
726
727 // Check we get the expected disabled caches.
728 $cache = cache::make('phpunit', 'disable');
729 $this->assertInstanceOf('cache_disabled', $cache);
730
731 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
732 $this->assertInstanceOf('cache_disabled', $cache);
733
734 $this->assertFalse(file_exists($configfile));
735
736 $this->assertFalse($cache->get('test'));
737 $this->assertFalse($cache->set('test', 'test'));
738 $this->assertFalse($cache->delete('test'));
739 $this->assertTrue($cache->purge());
740
741 cache_factory::reset();
742
743 $factory = cache_factory::instance(true);
744 $config = $factory->create_config_instance();
745 $this->assertEquals('cache_config_phpunittest', get_class($config));
746 }
8139ad13 747}