MDL-52095 themes: Padding to the left on RTL langs
[moodle.git] / cache / tests / cache_test.php
blob6c9f14ccae1ed6594acbfc3135871dfca8f4faba
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * PHPunit tests for the cache API
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 * It contains the components that are requried in order to use caching.
23 * @package core
24 * @category cache
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 // Include the necessary evils.
32 global $CFG;
33 require_once($CFG->dirroot.'/cache/locallib.php');
34 require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
36 /**
37 * PHPunit tests for the cache API
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class core_cache_testcase extends advanced_testcase {
44 /**
45 * Set things back to the default before each test.
47 public function setUp() {
48 parent::setUp();
49 cache_factory::reset();
50 cache_config_testing::create_default_configuration();
53 /**
54 * Final task is to reset the cache system
56 public static function tearDownAfterClass() {
57 parent::tearDownAfterClass();
58 cache_factory::reset();
61 /**
62 * Returns the expected application cache store.
63 * @return string
65 protected function get_expected_application_cache_store() {
66 global $CFG;
67 $expected = 'cachestore_file';
69 // Verify if we are using any of the available ways to use a different application store within tests.
70 if (defined('TEST_CACHE_USING_APPLICATION_STORE') && preg_match('#[a-zA-Z][a-zA-Z0-9_]*#', TEST_CACHE_USING_APPLICATION_STORE)) {
71 // 1st way. Using some of the testing servers.
72 $expected = 'cachestore_'.(string)TEST_CACHE_USING_APPLICATION_STORE;
74 } else if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH && !empty($CFG->altcacheconfigpath)) {
75 // 2nd way. Using an alternative configuration.
76 $defaultstores = cache_helper::get_stores_suitable_for_mode_default();
77 $instance = cache_config::instance();
78 // Iterate over defined mode mappings until we get an application one not being the default.
79 foreach ($instance->get_mode_mappings() as $mapping) {
80 // If the store is not for application mode, ignore.
81 if ($mapping['mode'] !== cache_store::MODE_APPLICATION) {
82 continue;
84 // If the store matches some default mapping store name, ignore.
85 if (array_key_exists($mapping['store'], $defaultstores) && !empty($defaultstores[$mapping['store']]['default'])) {
86 continue;
88 // Arrived here, have found an application mode store not being the default mapped one (file),
89 // that's the one we are using in the configuration for sure.
90 $expected = 'cachestore_'.$mapping['store'];
94 return $expected;
97 /**
98 * Tests cache configuration
100 public function test_cache_config() {
101 global $CFG;
103 if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH &&
104 !empty($CFG->altcacheconfigpath)) {
105 // We need to skip this test - it checks the default config structure, but very likely we arn't using the
106 // default config structure here so theres no point in running the test.
107 $this->markTestSkipped('Skipped testing default cache config structure as alt cache path is being used.');
110 if (defined('TEST_CACHE_USING_APPLICATION_STORE')) {
111 // We need to skip this test - it checks the default config structure, but very likely we arn't using the
112 // default config structure here because we are testing against an alternative application store.
113 $this->markTestSkipped('Skipped testing default cache config structure as alt application store is being used.');
116 $instance = cache_config::instance();
117 $this->assertInstanceOf('cache_config_testing', $instance);
119 $this->assertTrue(cache_config_testing::config_file_exists());
121 $stores = $instance->get_all_stores();
122 $this->assertCount(3, $stores);
123 foreach ($stores as $name => $store) {
124 // Check its an array.
125 $this->assertInternalType('array', $store);
126 // Check the name is the key.
127 $this->assertEquals($name, $store['name']);
128 // Check that it has been declared default.
129 $this->assertTrue($store['default']);
130 // Required attributes = name + plugin + configuration + modes + features.
131 $this->assertArrayHasKey('name', $store);
132 $this->assertArrayHasKey('plugin', $store);
133 $this->assertArrayHasKey('configuration', $store);
134 $this->assertArrayHasKey('modes', $store);
135 $this->assertArrayHasKey('features', $store);
138 $modemappings = $instance->get_mode_mappings();
139 $this->assertCount(3, $modemappings);
140 $modes = array(
141 cache_store::MODE_APPLICATION => false,
142 cache_store::MODE_SESSION => false,
143 cache_store::MODE_REQUEST => false,
145 foreach ($modemappings as $mapping) {
146 // We expect 3 properties.
147 $this->assertCount(3, $mapping);
148 // Required attributes = mode + store.
149 $this->assertArrayHasKey('mode', $mapping);
150 $this->assertArrayHasKey('store', $mapping);
151 // Record the mode.
152 $modes[$mapping['mode']] = true;
155 // Must have the default 3 modes and no more.
156 $this->assertCount(3, $mapping);
157 foreach ($modes as $mode) {
158 $this->assertTrue($mode);
161 $definitions = $instance->get_definitions();
162 // The event invalidation definition is required for the cache API and must be there.
163 $this->assertArrayHasKey('core/eventinvalidation', $definitions);
165 $definitionmappings = $instance->get_definition_mappings();
166 foreach ($definitionmappings as $mapping) {
167 // Required attributes = definition + store.
168 $this->assertArrayHasKey('definition', $mapping);
169 $this->assertArrayHasKey('store', $mapping);
174 * Tests for cache keys that would break on windows.
176 public function test_windows_nasty_keys() {
177 $instance = cache_config_testing::instance();
178 $instance->phpunit_add_definition('phpunit/windowskeytest', array(
179 'mode' => cache_store::MODE_APPLICATION,
180 'component' => 'phpunit',
181 'area' => 'windowskeytest',
182 'simplekeys' => true,
183 'simpledata' => true
185 $cache = cache::make('phpunit', 'windowskeytest');
186 $this->assertTrue($cache->set('contest', 'test data 1'));
187 $this->assertEquals('test data 1', $cache->get('contest'));
191 * Tests the default application cache
193 public function test_default_application_cache() {
194 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest');
195 $this->assertInstanceOf('cache_application', $cache);
196 $this->run_on_cache($cache);
198 $instance = cache_config_testing::instance(true);
199 $instance->phpunit_add_definition('phpunit/test_default_application_cache', array(
200 'mode' => cache_store::MODE_APPLICATION,
201 'component' => 'phpunit',
202 'area' => 'test_default_application_cache',
203 'staticacceleration' => true,
204 'staticaccelerationsize' => 1
206 $cache = cache::make('phpunit', 'test_default_application_cache');
207 $this->assertInstanceOf('cache_application', $cache);
208 $this->run_on_cache($cache);
212 * Tests the default session cache
214 public function test_default_session_cache() {
215 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest');
216 $this->assertInstanceOf('cache_session', $cache);
217 $this->run_on_cache($cache);
221 * Tests the default request cache
223 public function test_default_request_cache() {
224 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest');
225 $this->assertInstanceOf('cache_request', $cache);
226 $this->run_on_cache($cache);
230 * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
232 public function test_on_cache_without_store() {
233 $instance = cache_config_testing::instance(true);
234 $instance->phpunit_add_definition('phpunit/nostoretest1', array(
235 'mode' => cache_store::MODE_APPLICATION,
236 'component' => 'phpunit',
237 'area' => 'nostoretest1',
239 $instance->phpunit_add_definition('phpunit/nostoretest2', array(
240 'mode' => cache_store::MODE_APPLICATION,
241 'component' => 'phpunit',
242 'area' => 'nostoretest2',
243 'staticacceleration' => true
245 $instance->phpunit_remove_stores();
247 $cache = cache::make('phpunit', 'nostoretest1');
248 $this->run_on_cache($cache);
250 $cache = cache::make('phpunit', 'nostoretest2');
251 $this->run_on_cache($cache);
255 * Runs a standard series of access and use tests on a cache instance.
257 * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
259 * @param cache_loader $cache
261 protected function run_on_cache(cache_loader $cache) {
262 $key = 'contestkey';
263 $datascalars = array('test data', null);
264 $dataarray = array('contest' => 'data', 'part' => 'two');
265 $dataobject = (object)$dataarray;
267 foreach ($datascalars as $datascalar) {
268 $this->assertTrue($cache->purge());
270 // Check all read methods.
271 $this->assertFalse($cache->get($key));
272 $this->assertFalse($cache->has($key));
273 $result = $cache->get_many(array($key));
274 $this->assertCount(1, $result);
275 $this->assertFalse(reset($result));
276 $this->assertFalse($cache->has_any(array($key)));
277 $this->assertFalse($cache->has_all(array($key)));
279 // Set the data.
280 $this->assertTrue($cache->set($key, $datascalar));
281 // Setting it more than once should be permitted.
282 $this->assertTrue($cache->set($key, $datascalar));
284 // Recheck the read methods.
285 $this->assertEquals($datascalar, $cache->get($key));
286 $this->assertTrue($cache->has($key));
287 $result = $cache->get_many(array($key));
288 $this->assertCount(1, $result);
289 $this->assertEquals($datascalar, reset($result));
290 $this->assertTrue($cache->has_any(array($key)));
291 $this->assertTrue($cache->has_all(array($key)));
293 // Delete it.
294 $this->assertTrue($cache->delete($key));
296 // Check its gone.
297 $this->assertFalse($cache->get($key));
298 $this->assertFalse($cache->has($key));
301 // Test arrays.
302 $this->assertTrue($cache->set($key, $dataarray));
303 $this->assertEquals($dataarray, $cache->get($key));
305 // Test objects.
306 $this->assertTrue($cache->set($key, $dataobject));
307 $this->assertEquals($dataobject, $cache->get($key));
309 $specobject = new cache_phpunit_dummy_object('red', 'blue');
310 $this->assertTrue($cache->set($key, $specobject));
311 $result = $cache->get($key);
312 $this->assertInstanceOf('cache_phpunit_dummy_object', $result);
313 $this->assertEquals('red_ptc_wfc', $result->property1);
314 $this->assertEquals('blue_ptc_wfc', $result->property2);
316 // Test array of objects.
317 $specobject = new cache_phpunit_dummy_object('red', 'blue');
318 $data = new cacheable_object_array(array(
319 clone($specobject),
320 clone($specobject),
321 clone($specobject))
323 $this->assertTrue($cache->set($key, $data));
324 $result = $cache->get($key);
325 $this->assertInstanceOf('cacheable_object_array', $result);
326 $this->assertCount(3, $data);
327 foreach ($result as $item) {
328 $this->assertInstanceOf('cache_phpunit_dummy_object', $item);
329 $this->assertEquals('red_ptc_wfc', $item->property1);
330 $this->assertEquals('blue_ptc_wfc', $item->property2);
333 // Test set many.
334 $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
335 $this->assertEquals('data1', $cache->get('key1'));
336 $this->assertEquals('data2', $cache->get('key2'));
337 $this->assertEquals(null, $cache->get('key3'));
338 $this->assertTrue($cache->delete('key1'));
339 $this->assertTrue($cache->delete('key2'));
340 $this->assertTrue($cache->delete('key3'));
342 $cache->set_many(array(
343 'key1' => array(1, 2, 3),
344 'key2' => array(3, 2, 1),
346 $this->assertInternalType('array', $cache->get('key1'));
347 $this->assertInternalType('array', $cache->get('key2'));
348 $this->assertCount(3, $cache->get('key1'));
349 $this->assertCount(3, $cache->get('key2'));
350 $this->assertInternalType('array', $cache->get_many(array('key1', 'key2')));
351 $this->assertCount(2, $cache->get_many(array('key1', 'key2')));
352 $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
354 // Test delete many.
355 $this->assertTrue($cache->set('key1', 'data1'));
356 $this->assertTrue($cache->set('key2', 'data2'));
357 $this->assertTrue($cache->set('key3', null));
359 $this->assertEquals('data1', $cache->get('key1'));
360 $this->assertEquals('data2', $cache->get('key2'));
361 $this->assertEquals(null, $cache->get('key3'));
363 $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
365 $this->assertFalse($cache->get('key1'));
366 $this->assertFalse($cache->get('key2'));
367 $this->assertFalse($cache->get('key3'));
369 // Quick reference test.
370 $obj = new stdClass;
371 $obj->key = 'value';
372 $ref =& $obj;
373 $this->assertTrue($cache->set('obj', $obj));
375 $obj->key = 'eulav';
376 $var = $cache->get('obj');
377 $this->assertInstanceOf('stdClass', $var);
378 $this->assertEquals('value', $var->key);
380 $ref->key = 'eulav';
381 $var = $cache->get('obj');
382 $this->assertInstanceOf('stdClass', $var);
383 $this->assertEquals('value', $var->key);
385 $this->assertTrue($cache->delete('obj'));
387 // Deep reference test.
388 $obj1 = new stdClass;
389 $obj1->key = 'value';
390 $obj2 = new stdClass;
391 $obj2->key = 'test';
392 $obj3 = new stdClass;
393 $obj3->key = 'pork';
394 $obj1->subobj =& $obj2;
395 $obj2->subobj =& $obj3;
396 $this->assertTrue($cache->set('obj', $obj1));
398 $obj1->key = 'eulav';
399 $obj2->key = 'tset';
400 $obj3->key = 'krop';
401 $var = $cache->get('obj');
402 $this->assertInstanceOf('stdClass', $var);
403 $this->assertEquals('value', $var->key);
404 $this->assertInstanceOf('stdClass', $var->subobj);
405 $this->assertEquals('test', $var->subobj->key);
406 $this->assertInstanceOf('stdClass', $var->subobj->subobj);
407 $this->assertEquals('pork', $var->subobj->subobj->key);
408 $this->assertTrue($cache->delete('obj'));
410 // Death reference test... basicaly we don't want this to die.
411 $obj = new stdClass;
412 $obj->key = 'value';
413 $obj->self =& $obj;
414 $this->assertTrue($cache->set('obj', $obj));
415 $var = $cache->get('obj');
416 $this->assertInstanceOf('stdClass', $var);
417 $this->assertEquals('value', $var->key);
419 // Reference test after retrieve.
420 $obj = new stdClass;
421 $obj->key = 'value';
422 $this->assertTrue($cache->set('obj', $obj));
424 $var1 = $cache->get('obj');
425 $this->assertInstanceOf('stdClass', $var1);
426 $this->assertEquals('value', $var1->key);
427 $var1->key = 'eulav';
428 $this->assertEquals('eulav', $var1->key);
430 $var2 = $cache->get('obj');
431 $this->assertInstanceOf('stdClass', $var2);
432 $this->assertEquals('value', $var2->key);
434 $this->assertTrue($cache->delete('obj'));
436 // Test strictness exceptions.
437 try {
438 $cache->get('exception', MUST_EXIST);
439 $this->fail('Exception expected from cache::get using MUST_EXIST');
440 } catch (Exception $e) {
441 $this->assertTrue(true);
443 try {
444 $cache->get_many(array('exception1', 'exception2'), MUST_EXIST);
445 $this->fail('Exception expected from cache::get_many using MUST_EXIST');
446 } catch (Exception $e) {
447 $this->assertTrue(true);
449 $cache->set('test', 'test');
450 try {
451 $cache->get_many(array('test', 'exception'), MUST_EXIST);
452 $this->fail('Exception expected from cache::get_many using MUST_EXIST');
453 } catch (Exception $e) {
454 $this->assertTrue(true);
459 * Tests a definition using a data loader
461 public function test_definition_data_loader() {
462 $instance = cache_config_testing::instance(true);
463 $instance->phpunit_add_definition('phpunit/datasourcetest', array(
464 'mode' => cache_store::MODE_APPLICATION,
465 'component' => 'phpunit',
466 'area' => 'datasourcetest',
467 'datasource' => 'cache_phpunit_dummy_datasource',
468 'datasourcefile' => 'cache/tests/fixtures/lib.php'
471 $cache = cache::make('phpunit', 'datasourcetest');
472 $this->assertInstanceOf('cache_application', $cache);
474 // Purge it to be sure.
475 $this->assertTrue($cache->purge());
476 // It won't be there yet.
477 $this->assertFalse($cache->has('Test'));
478 // It should load it ;).
479 $this->assertTrue($cache->has('Test', true));
481 // Purge it to be sure.
482 $this->assertTrue($cache->purge());
483 $this->assertEquals('Test has no value really.', $cache->get('Test'));
485 // Test multiple values.
486 $this->assertTrue($cache->purge());
487 $this->assertTrue($cache->set('b', 'B'));
488 $result = $cache->get_many(array('a', 'b', 'c'));
489 $this->assertInternalType('array', $result);
490 $this->assertCount(3, $result);
491 $this->assertArrayHasKey('a', $result);
492 $this->assertArrayHasKey('b', $result);
493 $this->assertArrayHasKey('c', $result);
494 $this->assertEquals('a has no value really.', $result['a']);
495 $this->assertEquals('B', $result['b']);
496 $this->assertEquals('c has no value really.', $result['c']);
500 * Tests a definition using an overridden loader
502 public function test_definition_overridden_loader() {
503 $instance = cache_config_testing::instance(true);
504 $instance->phpunit_add_definition('phpunit/overridetest', array(
505 'mode' => cache_store::MODE_APPLICATION,
506 'component' => 'phpunit',
507 'area' => 'overridetest',
508 'overrideclass' => 'cache_phpunit_dummy_overrideclass',
509 'overrideclassfile' => 'cache/tests/fixtures/lib.php'
511 $cache = cache::make('phpunit', 'overridetest');
512 $this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
513 $this->assertInstanceOf('cache_application', $cache);
514 // Purge it to be sure.
515 $this->assertTrue($cache->purge());
516 // It won't be there yet.
517 $this->assertFalse($cache->has('Test'));
518 // Add it.
519 $this->assertTrue($cache->set('Test', 'Test has no value really.'));
520 // Check its there.
521 $this->assertEquals('Test has no value really.', $cache->get('Test'));
525 * Test the mappingsonly setting.
527 public function test_definition_mappings_only() {
528 /** @var cache_config_testing $instance */
529 $instance = cache_config_testing::instance(true);
530 $instance->phpunit_add_definition('phpunit/mappingsonly', array(
531 'mode' => cache_store::MODE_APPLICATION,
532 'component' => 'phpunit',
533 'area' => 'mappingsonly',
534 'mappingsonly' => true
535 ), false);
536 $instance->phpunit_add_definition('phpunit/nonmappingsonly', array(
537 'mode' => cache_store::MODE_APPLICATION,
538 'component' => 'phpunit',
539 'area' => 'nonmappingsonly',
540 'mappingsonly' => false
541 ), false);
543 $cacheonly = cache::make('phpunit', 'mappingsonly');
544 $this->assertInstanceOf('cache_application', $cacheonly);
545 $this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class());
547 $expected = $this->get_expected_application_cache_store();
548 $cachenon = cache::make('phpunit', 'nonmappingsonly');
549 $this->assertInstanceOf('cache_application', $cachenon);
550 $this->assertEquals($expected, $cachenon->phpunit_get_store_class());
554 * Test a very basic definition.
556 public function test_definition() {
557 $instance = cache_config_testing::instance();
558 $instance->phpunit_add_definition('phpunit/test', array(
559 'mode' => cache_store::MODE_APPLICATION,
560 'component' => 'phpunit',
561 'area' => 'test',
563 $cache = cache::make('phpunit', 'test');
565 $this->assertTrue($cache->set('testkey1', 'test data 1'));
566 $this->assertEquals('test data 1', $cache->get('testkey1'));
567 $this->assertTrue($cache->set('testkey2', 'test data 2'));
568 $this->assertEquals('test data 2', $cache->get('testkey2'));
572 * Test a definition using the simple keys.
574 public function test_definition_simplekeys() {
575 $instance = cache_config_testing::instance();
576 $instance->phpunit_add_definition('phpunit/simplekeytest', array(
577 'mode' => cache_store::MODE_APPLICATION,
578 'component' => 'phpunit',
579 'area' => 'simplekeytest',
580 'simplekeys' => true
582 $cache = cache::make('phpunit', 'simplekeytest');
584 $this->assertTrue($cache->set('testkey1', 'test data 1'));
585 $this->assertEquals('test data 1', $cache->get('testkey1'));
586 $this->assertTrue($cache->set('testkey2', 'test data 2'));
587 $this->assertEquals('test data 2', $cache->get('testkey2'));
589 $cache->purge();
591 $this->assertTrue($cache->set('1', 'test data 1'));
592 $this->assertEquals('test data 1', $cache->get('1'));
593 $this->assertTrue($cache->set('2', 'test data 2'));
594 $this->assertEquals('test data 2', $cache->get('2'));
598 * Test a negative TTL on an application cache.
600 public function test_application_ttl_negative() {
601 $instance = cache_config_testing::instance(true);
602 $instance->phpunit_add_definition('phpunit/ttltest', array(
603 'mode' => cache_store::MODE_APPLICATION,
604 'component' => 'phpunit',
605 'area' => 'ttltest',
606 'ttl' => -86400 // Set to a day in the past to be extra sure.
608 $cache = cache::make('phpunit', 'ttltest');
609 $this->assertInstanceOf('cache_application', $cache);
611 // Purge it to be sure.
612 $this->assertTrue($cache->purge());
613 // It won't be there yet.
614 $this->assertFalse($cache->has('Test'));
615 // Set it now.
616 $this->assertTrue($cache->set('Test', 'Test'));
617 // Check its not there.
618 $this->assertFalse($cache->has('Test'));
619 // Double check by trying to get it.
620 $this->assertFalse($cache->get('Test'));
622 // Test with multiple keys.
623 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
624 $result = $cache->get_many(array('a', 'b', 'c'));
625 $this->assertInternalType('array', $result);
626 $this->assertCount(3, $result);
627 $this->assertArrayHasKey('a', $result);
628 $this->assertArrayHasKey('b', $result);
629 $this->assertArrayHasKey('c', $result);
630 $this->assertFalse($result['a']);
631 $this->assertFalse($result['b']);
632 $this->assertFalse($result['c']);
634 // Test with multiple keys including missing ones.
635 $result = $cache->get_many(array('a', 'c', 'e'));
636 $this->assertInternalType('array', $result);
637 $this->assertCount(3, $result);
638 $this->assertArrayHasKey('a', $result);
639 $this->assertArrayHasKey('c', $result);
640 $this->assertArrayHasKey('e', $result);
641 $this->assertFalse($result['a']);
642 $this->assertFalse($result['c']);
643 $this->assertFalse($result['e']);
647 * Test a positive TTL on an application cache.
649 public function test_application_ttl_positive() {
650 $instance = cache_config_testing::instance(true);
651 $instance->phpunit_add_definition('phpunit/ttltest', array(
652 'mode' => cache_store::MODE_APPLICATION,
653 'component' => 'phpunit',
654 'area' => 'ttltest',
655 'ttl' => 86400 // Set to a day in the future to be extra sure.
657 $cache = cache::make('phpunit', 'ttltest');
658 $this->assertInstanceOf('cache_application', $cache);
660 // Purge it to be sure.
661 $this->assertTrue($cache->purge());
662 // It won't be there yet.
663 $this->assertFalse($cache->has('Test'));
664 // Set it now.
665 $this->assertTrue($cache->set('Test', 'Test'));
666 // Check its there.
667 $this->assertTrue($cache->has('Test'));
668 // Double check by trying to get it.
669 $this->assertEquals('Test', $cache->get('Test'));
671 // Test with multiple keys.
672 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
673 $result = $cache->get_many(array('a', 'b', 'c'));
674 $this->assertInternalType('array', $result);
675 $this->assertCount(3, $result);
676 $this->assertArrayHasKey('a', $result);
677 $this->assertArrayHasKey('b', $result);
678 $this->assertArrayHasKey('c', $result);
679 $this->assertEquals('A', $result['a']);
680 $this->assertEquals('B', $result['b']);
681 $this->assertEquals('C', $result['c']);
683 // Test with multiple keys including missing ones.
684 $result = $cache->get_many(array('a', 'c', 'e'));
685 $this->assertInternalType('array', $result);
686 $this->assertCount(3, $result);
687 $this->assertArrayHasKey('a', $result);
688 $this->assertArrayHasKey('c', $result);
689 $this->assertArrayHasKey('e', $result);
690 $this->assertEquals('A', $result['a']);
691 $this->assertEquals('C', $result['c']);
692 $this->assertEquals(false, $result['e']);
696 * Test a negative TTL on an session cache.
698 public function test_session_ttl_positive() {
699 $instance = cache_config_testing::instance(true);
700 $instance->phpunit_add_definition('phpunit/ttltest', array(
701 'mode' => cache_store::MODE_SESSION,
702 'component' => 'phpunit',
703 'area' => 'ttltest',
704 'ttl' => 86400 // Set to a day in the future to be extra sure.
706 $cache = cache::make('phpunit', 'ttltest');
707 $this->assertInstanceOf('cache_session', $cache);
709 // Purge it to be sure.
710 $this->assertTrue($cache->purge());
711 // It won't be there yet.
712 $this->assertFalse($cache->has('Test'));
713 // Set it now.
714 $this->assertTrue($cache->set('Test', 'Test'));
715 // Check its there.
716 $this->assertTrue($cache->has('Test'));
717 // Double check by trying to get it.
718 $this->assertEquals('Test', $cache->get('Test'));
720 // Test with multiple keys.
721 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
722 $result = $cache->get_many(array('a', 'b', 'c'));
723 $this->assertInternalType('array', $result);
724 $this->assertCount(3, $result);
725 $this->assertArrayHasKey('a', $result);
726 $this->assertArrayHasKey('b', $result);
727 $this->assertArrayHasKey('c', $result);
728 $this->assertEquals('A', $result['a']);
729 $this->assertEquals('B', $result['b']);
730 $this->assertEquals('C', $result['c']);
732 // Test with multiple keys including missing ones.
733 $result = $cache->get_many(array('a', 'c', 'e'));
734 $this->assertInternalType('array', $result);
735 $this->assertCount(3, $result);
736 $this->assertArrayHasKey('a', $result);
737 $this->assertArrayHasKey('c', $result);
738 $this->assertArrayHasKey('e', $result);
739 $this->assertEquals('A', $result['a']);
740 $this->assertEquals('C', $result['c']);
741 $this->assertEquals(false, $result['e']);
745 * Tests manual locking operations on an application cache
747 public function test_application_manual_locking() {
748 $instance = cache_config_testing::instance();
749 $instance->phpunit_add_definition('phpunit/lockingtest', array(
750 'mode' => cache_store::MODE_APPLICATION,
751 'component' => 'phpunit',
752 'area' => 'lockingtest'
754 $cache1 = cache::make('phpunit', 'lockingtest');
755 $cache2 = clone($cache1);
757 $this->assertTrue($cache1->set('testkey', 'test data'));
758 $this->assertTrue($cache2->set('testkey', 'test data'));
760 $this->assertTrue($cache1->acquire_lock('testkey'));
761 $this->assertFalse($cache2->acquire_lock('testkey'));
763 $this->assertTrue($cache1->check_lock_state('testkey'));
764 $this->assertFalse($cache2->check_lock_state('testkey'));
766 $this->assertTrue($cache1->release_lock('testkey'));
767 $this->assertFalse($cache2->release_lock('testkey'));
769 $this->assertTrue($cache1->set('testkey', 'test data'));
770 $this->assertTrue($cache2->set('testkey', 'test data'));
774 * Tests application cache event invalidation
776 public function test_application_event_invalidation() {
777 $instance = cache_config_testing::instance();
778 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
779 'mode' => cache_store::MODE_APPLICATION,
780 'component' => 'phpunit',
781 'area' => 'eventinvalidationtest',
782 'invalidationevents' => array(
783 'crazyevent'
786 $cache = cache::make('phpunit', 'eventinvalidationtest');
788 $this->assertTrue($cache->set('testkey1', 'test data 1'));
789 $this->assertEquals('test data 1', $cache->get('testkey1'));
790 $this->assertTrue($cache->set('testkey2', 'test data 2'));
791 $this->assertEquals('test data 2', $cache->get('testkey2'));
793 // Test invalidating a single entry.
794 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
796 $this->assertFalse($cache->get('testkey1'));
797 $this->assertEquals('test data 2', $cache->get('testkey2'));
799 $this->assertTrue($cache->set('testkey1', 'test data 1'));
801 // Test invalidating both entries.
802 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
804 $this->assertFalse($cache->get('testkey1'));
805 $this->assertFalse($cache->get('testkey2'));
809 * Tests session cache event invalidation
811 public function test_session_event_invalidation() {
812 $instance = cache_config_testing::instance();
813 $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array(
814 'mode' => cache_store::MODE_SESSION,
815 'component' => 'phpunit',
816 'area' => 'test_session_event_invalidation',
817 'invalidationevents' => array(
818 'crazyevent'
821 $cache = cache::make('phpunit', 'test_session_event_invalidation');
822 $this->assertInstanceOf('cache_session', $cache);
824 $this->assertTrue($cache->set('testkey1', 'test data 1'));
825 $this->assertEquals('test data 1', $cache->get('testkey1'));
826 $this->assertTrue($cache->set('testkey2', 'test data 2'));
827 $this->assertEquals('test data 2', $cache->get('testkey2'));
829 // Test invalidating a single entry.
830 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
832 $this->assertFalse($cache->get('testkey1'));
833 $this->assertEquals('test data 2', $cache->get('testkey2'));
835 $this->assertTrue($cache->set('testkey1', 'test data 1'));
837 // Test invalidating both entries.
838 cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
840 $this->assertFalse($cache->get('testkey1'));
841 $this->assertFalse($cache->get('testkey2'));
845 * Tests application cache definition invalidation
847 public function test_application_definition_invalidation() {
848 $instance = cache_config_testing::instance();
849 $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
850 'mode' => cache_store::MODE_APPLICATION,
851 'component' => 'phpunit',
852 'area' => 'definitioninvalidation'
854 $cache = cache::make('phpunit', 'definitioninvalidation');
855 $this->assertTrue($cache->set('testkey1', 'test data 1'));
856 $this->assertEquals('test data 1', $cache->get('testkey1'));
857 $this->assertTrue($cache->set('testkey2', 'test data 2'));
858 $this->assertEquals('test data 2', $cache->get('testkey2'));
860 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
862 $this->assertFalse($cache->get('testkey1'));
863 $this->assertEquals('test data 2', $cache->get('testkey2'));
865 $this->assertTrue($cache->set('testkey1', 'test data 1'));
867 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
869 $this->assertFalse($cache->get('testkey1'));
870 $this->assertEquals('test data 2', $cache->get('testkey2'));
872 $this->assertTrue($cache->set('testkey1', 'test data 1'));
874 cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
876 $this->assertFalse($cache->get('testkey1'));
877 $this->assertFalse($cache->get('testkey2'));
881 * Tests session cache definition invalidation
883 public function test_session_definition_invalidation() {
884 $instance = cache_config_testing::instance();
885 $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array(
886 'mode' => cache_store::MODE_SESSION,
887 'component' => 'phpunit',
888 'area' => 'test_session_definition_invalidation'
890 $cache = cache::make('phpunit', 'test_session_definition_invalidation');
891 $this->assertInstanceOf('cache_session', $cache);
892 $this->assertTrue($cache->set('testkey1', 'test data 1'));
893 $this->assertEquals('test data 1', $cache->get('testkey1'));
894 $this->assertTrue($cache->set('testkey2', 'test data 2'));
895 $this->assertEquals('test data 2', $cache->get('testkey2'));
897 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 'testkey1');
899 $this->assertFalse($cache->get('testkey1'));
900 $this->assertEquals('test data 2', $cache->get('testkey2'));
902 $this->assertTrue($cache->set('testkey1', 'test data 1'));
904 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
905 array('testkey1'));
907 $this->assertFalse($cache->get('testkey1'));
908 $this->assertEquals('test data 2', $cache->get('testkey2'));
910 $this->assertTrue($cache->set('testkey1', 'test data 1'));
912 cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
913 array('testkey1', 'testkey2'));
915 $this->assertFalse($cache->get('testkey1'));
916 $this->assertFalse($cache->get('testkey2'));
920 * Tests application cache event invalidation over a distributed setup.
922 public function test_distributed_application_event_invalidation() {
923 global $CFG;
924 // This is going to be an intense wee test.
925 // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
926 // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
927 // check that it is not picked up.
928 $instance = cache_config_testing::instance();
929 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
930 'mode' => cache_store::MODE_APPLICATION,
931 'component' => 'phpunit',
932 'area' => 'eventinvalidationtest',
933 'simplekeys' => true,
934 'simpledata' => true,
935 'invalidationevents' => array(
936 'crazyevent'
939 $cache = cache::make('phpunit', 'eventinvalidationtest');
940 $this->assertTrue($cache->set('testkey1', 'test data 1'));
941 $this->assertEquals('test data 1', $cache->get('testkey1'));
943 cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
945 $this->assertFalse($cache->get('testkey1'));
947 // OK data added, data invalidated, and invalidation time has been set.
948 // Now we need to manually add back the data and adjust the invalidation time.
949 $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit');
950 $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
951 // Make sure the file is correct.
952 $this->assertTrue(file_exists($timefile));
953 $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate.
954 make_writable_directory(dirname($timefile));
955 file_put_contents($timefile, $timecont);
956 $this->assertTrue(file_exists($timefile));
958 $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache";
959 $datacont = serialize("test data 1");
960 make_writable_directory(dirname($datafile));
961 file_put_contents($datafile, $datacont);
962 $this->assertTrue(file_exists($datafile));
964 // Test 1: Rebuild without the event and test its there.
965 cache_factory::reset();
966 $instance = cache_config_testing::instance();
967 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
968 'mode' => cache_store::MODE_APPLICATION,
969 'component' => 'phpunit',
970 'area' => 'eventinvalidationtest',
971 'simplekeys' => true,
972 'simpledata' => true,
974 $cache = cache::make('phpunit', 'eventinvalidationtest');
975 $this->assertEquals('test data 1', $cache->get('testkey1'));
977 // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
978 cache_factory::reset();
979 $instance = cache_config_testing::instance();
980 $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
981 'mode' => cache_store::MODE_APPLICATION,
982 'component' => 'phpunit',
983 'area' => 'eventinvalidationtest',
984 'simplekeys' => true,
985 'simpledata' => true,
986 'invalidationevents' => array(
987 'crazyevent'
990 $cache = cache::make('phpunit', 'eventinvalidationtest');
991 $this->assertFalse($cache->get('testkey1'));
993 // Test 3: Verify that an existing lastinvalidation cache file is updated when needed.
995 // Make a new cache class. This should should invalidate testkey2.
996 $cache = cache::make('phpunit', 'eventinvalidationtest');
997 // Timestamp should have updated to cache::now().
998 $this->assertEquals(cache::now(), $cache->get('lastinvalidation'));
1000 // Set testkey2 data.
1001 $cache->set('testkey2', 'test data 2');
1002 // Backdate the event invalidation time by 30 seconds.
1003 $invalidationcache = cache::make('core', 'eventinvalidation');
1004 $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 30));
1005 // Lastinvalidation should already be cache::now().
1006 $this->assertEquals(cache::now(), $cache->get('lastinvalidation'));
1007 // Set it to 15 seconds ago so that we know if it changes.
1008 $cache->set('lastinvalidation', cache::now() - 15);
1009 // Make a new cache class. This should not invalidate anything.
1010 cache_factory::instance()->reset_cache_instances();
1011 $cache = cache::make('phpunit', 'eventinvalidationtest');
1012 // Lastinvalidation shouldn't change since it was already newer than invalidation event.
1013 $this->assertEquals(cache::now() - 15, $cache->get('lastinvalidation'));
1015 // Now set the event invalidation to newer than the lastinvalidation time.
1016 $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 5));
1017 // Make a new cache class. This should should invalidate testkey2.
1018 cache_factory::instance()->reset_cache_instances();
1019 $cache = cache::make('phpunit', 'eventinvalidationtest');
1020 // Lastinvalidation timestamp should have updated to cache::now().
1021 $this->assertEquals(cache::now(), $cache->get('lastinvalidation'));
1023 // Now simulate a purge_by_event 5 seconds ago.
1024 $invalidationcache = cache::make('core', 'eventinvalidation');
1025 $invalidationcache->set('crazyevent', array('purged' => cache::now() - 5));
1026 // Set our lastinvalidation timestamp to 15 seconds ago.
1027 $cache->set('lastinvalidation', cache::now() - 15);
1028 // Make a new cache class. This should invalidate the cache.
1029 cache_factory::instance()->reset_cache_instances();
1030 $cache = cache::make('phpunit', 'eventinvalidationtest');
1031 // Lastinvalidation timestamp should have updated to cache::now().
1032 $this->assertEquals(cache::now(), $cache->get('lastinvalidation'));
1037 * Tests application cache event purge
1039 public function test_application_event_purge() {
1040 $instance = cache_config_testing::instance();
1041 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1042 'mode' => cache_store::MODE_APPLICATION,
1043 'component' => 'phpunit',
1044 'area' => 'eventpurgetest',
1045 'invalidationevents' => array(
1046 'crazyevent'
1049 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1050 'mode' => cache_store::MODE_APPLICATION,
1051 'component' => 'phpunit',
1052 'area' => 'eventpurgetestaccelerated',
1053 'staticacceleration' => true,
1054 'invalidationevents' => array(
1055 'crazyevent'
1058 $cache = cache::make('phpunit', 'eventpurgetest');
1060 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1061 $this->assertEquals('test data 1', $cache->get('testkey1'));
1062 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1063 $this->assertEquals('test data 2', $cache->get('testkey2'));
1065 // Purge the event.
1066 cache_helper::purge_by_event('crazyevent');
1068 // Check things have been removed.
1069 $this->assertFalse($cache->get('testkey1'));
1070 $this->assertFalse($cache->get('testkey2'));
1072 // Now test the static acceleration array.
1073 $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1074 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1075 $this->assertEquals('test data 1', $cache->get('testkey1'));
1076 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1077 $this->assertEquals('test data 2', $cache->get('testkey2'));
1079 // Purge the event.
1080 cache_helper::purge_by_event('crazyevent');
1082 // Check things have been removed.
1083 $this->assertFalse($cache->get('testkey1'));
1084 $this->assertFalse($cache->get('testkey2'));
1088 * Tests session cache event purge
1090 public function test_session_event_purge() {
1091 $instance = cache_config_testing::instance();
1092 $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1093 'mode' => cache_store::MODE_SESSION,
1094 'component' => 'phpunit',
1095 'area' => 'eventpurgetest',
1096 'invalidationevents' => array(
1097 'crazyevent'
1100 $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1101 'mode' => cache_store::MODE_SESSION,
1102 'component' => 'phpunit',
1103 'area' => 'eventpurgetestaccelerated',
1104 'staticacceleration' => true,
1105 'invalidationevents' => array(
1106 'crazyevent'
1109 $cache = cache::make('phpunit', 'eventpurgetest');
1111 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1112 $this->assertEquals('test data 1', $cache->get('testkey1'));
1113 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1114 $this->assertEquals('test data 2', $cache->get('testkey2'));
1116 // Purge the event.
1117 cache_helper::purge_by_event('crazyevent');
1119 // Check things have been removed.
1120 $this->assertFalse($cache->get('testkey1'));
1121 $this->assertFalse($cache->get('testkey2'));
1123 // Now test the static acceleration array.
1124 $cache = cache::make('phpunit', 'eventpurgetestaccelerated');
1125 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1126 $this->assertEquals('test data 1', $cache->get('testkey1'));
1127 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1128 $this->assertEquals('test data 2', $cache->get('testkey2'));
1130 // Purge the event.
1131 cache_helper::purge_by_event('crazyevent');
1133 // Check things have been removed.
1134 $this->assertFalse($cache->get('testkey1'));
1135 $this->assertFalse($cache->get('testkey2'));
1139 * Tests application cache definition purge
1141 public function test_application_definition_purge() {
1142 $instance = cache_config_testing::instance();
1143 $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
1144 'mode' => cache_store::MODE_APPLICATION,
1145 'component' => 'phpunit',
1146 'area' => 'definitionpurgetest',
1147 'invalidationevents' => array(
1148 'crazyevent'
1151 $cache = cache::make('phpunit', 'definitionpurgetest');
1153 $this->assertTrue($cache->set('testkey1', 'test data 1'));
1154 $this->assertEquals('test data 1', $cache->get('testkey1'));
1155 $this->assertTrue($cache->set('testkey2', 'test data 2'));
1156 $this->assertEquals('test data 2', $cache->get('testkey2'));
1158 // Purge the event.
1159 cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
1161 // Check things have been removed.
1162 $this->assertFalse($cache->get('testkey1'));
1163 $this->assertFalse($cache->get('testkey2'));
1167 * Test the use of an alt path.
1168 * If we can generate a config instance we are done :)
1170 public function test_alt_cache_path() {
1171 global $CFG;
1172 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1173 $this->markTestSkipped('Skipped testing alt cache path as it is already being used.');
1175 $this->resetAfterTest();
1176 $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
1177 $instance = cache_config_testing::instance();
1178 $this->assertInstanceOf('cache_config', $instance);
1182 * Test disabling the cache stores.
1184 public function test_disable_stores() {
1185 $instance = cache_config_testing::instance();
1186 $instance->phpunit_add_definition('phpunit/disabletest1', array(
1187 'mode' => cache_store::MODE_APPLICATION,
1188 'component' => 'phpunit',
1189 'area' => 'disabletest1'
1191 $instance->phpunit_add_definition('phpunit/disabletest2', array(
1192 'mode' => cache_store::MODE_SESSION,
1193 'component' => 'phpunit',
1194 'area' => 'disabletest2'
1196 $instance->phpunit_add_definition('phpunit/disabletest3', array(
1197 'mode' => cache_store::MODE_REQUEST,
1198 'component' => 'phpunit',
1199 'area' => 'disabletest3'
1202 $caches = array(
1203 'disabletest1' => cache::make('phpunit', 'disabletest1'),
1204 'disabletest2' => cache::make('phpunit', 'disabletest2'),
1205 'disabletest3' => cache::make('phpunit', 'disabletest3')
1208 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1209 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1210 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1212 $this->assertEquals('cachestore_file', $caches['disabletest1']->phpunit_get_store_class());
1213 $this->assertEquals('cachestore_session', $caches['disabletest2']->phpunit_get_store_class());
1214 $this->assertEquals('cachestore_static', $caches['disabletest3']->phpunit_get_store_class());
1216 foreach ($caches as $cache) {
1217 $this->assertFalse($cache->get('test'));
1218 $this->assertTrue($cache->set('test', 'test'));
1219 $this->assertEquals('test', $cache->get('test'));
1222 cache_factory::disable_stores();
1224 $caches = array(
1225 'disabletest1' => cache::make('phpunit', 'disabletest1'),
1226 'disabletest2' => cache::make('phpunit', 'disabletest2'),
1227 'disabletest3' => cache::make('phpunit', 'disabletest3')
1230 $this->assertInstanceOf('cache_phpunit_application', $caches['disabletest1']);
1231 $this->assertInstanceOf('cache_phpunit_session', $caches['disabletest2']);
1232 $this->assertInstanceOf('cache_phpunit_request', $caches['disabletest3']);
1234 $this->assertEquals('cachestore_dummy', $caches['disabletest1']->phpunit_get_store_class());
1235 $this->assertEquals('cachestore_dummy', $caches['disabletest2']->phpunit_get_store_class());
1236 $this->assertEquals('cachestore_dummy', $caches['disabletest3']->phpunit_get_store_class());
1238 foreach ($caches as $cache) {
1239 $this->assertFalse($cache->get('test'));
1240 $this->assertTrue($cache->set('test', 'test'));
1241 $this->assertEquals('test', $cache->get('test'));
1246 * Test disabling the cache.
1248 public function test_disable() {
1249 global $CFG;
1251 if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
1252 // We can't run this test as it requires us to delete the cache configuration script which we just
1253 // cant do with a custom path in play.
1254 $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
1257 $configfile = $CFG->dataroot.'/muc/config.php';
1259 // That's right, we're deleting the config file.
1260 $this->assertTrue(@unlink($configfile));
1262 // Disable the cache
1263 cache_phpunit_factory::phpunit_disable();
1265 // Check we get the expected disabled factory.
1266 $factory = cache_factory::instance();
1267 $this->assertInstanceOf('cache_factory_disabled', $factory);
1269 // Check we get the expected disabled config.
1270 $config = $factory->create_config_instance();
1271 $this->assertInstanceOf('cache_config_disabled', $config);
1273 // Check we get the expected disabled caches.
1274 $cache = cache::make('phpunit', 'disable');
1275 $this->assertInstanceOf('cache_disabled', $cache);
1277 // Test an application cache.
1278 $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
1279 $this->assertInstanceOf('cache_disabled', $cache);
1281 $this->assertFalse(file_exists($configfile));
1283 $this->assertFalse($cache->get('test'));
1284 $this->assertFalse($cache->set('test', 'test'));
1285 $this->assertFalse($cache->delete('test'));
1286 $this->assertTrue($cache->purge());
1288 // Test a session cache.
1289 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable');
1290 $this->assertInstanceOf('cache_disabled', $cache);
1292 $this->assertFalse(file_exists($configfile));
1294 $this->assertFalse($cache->get('test'));
1295 $this->assertFalse($cache->set('test', 'test'));
1296 $this->assertFalse($cache->delete('test'));
1297 $this->assertTrue($cache->purge());
1299 // Finally test a request cache.
1300 $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable');
1301 $this->assertInstanceOf('cache_disabled', $cache);
1303 $this->assertFalse(file_exists($configfile));
1305 $this->assertFalse($cache->get('test'));
1306 $this->assertFalse($cache->set('test', 'test'));
1307 $this->assertFalse($cache->delete('test'));
1308 $this->assertTrue($cache->purge());
1310 cache_factory::reset();
1312 $factory = cache_factory::instance(true);
1313 $config = $factory->create_config_instance();
1314 $this->assertEquals('cache_config_testing', get_class($config));
1318 * Test that multiple application loaders work ok.
1320 public function test_multiple_application_loaders() {
1321 $instance = cache_config_testing::instance(true);
1322 $instance->phpunit_add_file_store('phpunittest1');
1323 $instance->phpunit_add_file_store('phpunittest2');
1324 $instance->phpunit_add_definition('phpunit/multi_loader', array(
1325 'mode' => cache_store::MODE_APPLICATION,
1326 'component' => 'phpunit',
1327 'area' => 'multi_loader'
1329 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1330 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1332 $cache = cache::make('phpunit', 'multi_loader');
1333 $this->assertInstanceOf('cache_application', $cache);
1334 $this->assertFalse($cache->get('test'));
1335 $this->assertTrue($cache->set('test', 'test'));
1336 $this->assertEquals('test', $cache->get('test'));
1337 $this->assertTrue($cache->delete('test'));
1338 $this->assertFalse($cache->get('test'));
1339 $this->assertTrue($cache->set('test', 'test'));
1340 $this->assertTrue($cache->purge());
1341 $this->assertFalse($cache->get('test'));
1343 // Test the many commands.
1344 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1345 $result = $cache->get_many(array('a', 'b', 'c'));
1346 $this->assertInternalType('array', $result);
1347 $this->assertCount(3, $result);
1348 $this->assertArrayHasKey('a', $result);
1349 $this->assertArrayHasKey('b', $result);
1350 $this->assertArrayHasKey('c', $result);
1351 $this->assertEquals('A', $result['a']);
1352 $this->assertEquals('B', $result['b']);
1353 $this->assertEquals('C', $result['c']);
1354 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1355 $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1356 $result = $cache->get_many(array('a', 'b', 'c'));
1357 $this->assertInternalType('array', $result);
1358 $this->assertCount(3, $result);
1359 $this->assertArrayHasKey('a', $result);
1360 $this->assertArrayHasKey('b', $result);
1361 $this->assertArrayHasKey('c', $result);
1362 $this->assertFalse($result['a']);
1363 $this->assertEquals('B', $result['b']);
1364 $this->assertFalse($result['c']);
1366 // Test non-recursive deletes.
1367 $this->assertTrue($cache->set('test', 'test'));
1368 $this->assertSame('test', $cache->get('test'));
1369 $this->assertTrue($cache->delete('test', false));
1370 // We should still have it on a deeper loader.
1371 $this->assertSame('test', $cache->get('test'));
1372 // Test non-recusive with many functions.
1373 $this->assertSame(3, $cache->set_many(array(
1374 'one' => 'one',
1375 'two' => 'two',
1376 'three' => 'three'
1377 )));
1378 $this->assertSame('one', $cache->get('one'));
1379 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1380 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1381 $this->assertSame('one', $cache->get('one'));
1382 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1386 * Test that multiple application loaders work ok.
1388 public function test_multiple_session_loaders() {
1389 /* @var cache_config_testing $instance */
1390 $instance = cache_config_testing::instance(true);
1391 $instance->phpunit_add_session_store('phpunittest1');
1392 $instance->phpunit_add_session_store('phpunittest2');
1393 $instance->phpunit_add_definition('phpunit/multi_loader', array(
1394 'mode' => cache_store::MODE_SESSION,
1395 'component' => 'phpunit',
1396 'area' => 'multi_loader'
1398 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1399 $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1401 $cache = cache::make('phpunit', 'multi_loader');
1402 $this->assertInstanceOf('cache_session', $cache);
1403 $this->assertFalse($cache->get('test'));
1404 $this->assertTrue($cache->set('test', 'test'));
1405 $this->assertEquals('test', $cache->get('test'));
1406 $this->assertTrue($cache->delete('test'));
1407 $this->assertFalse($cache->get('test'));
1408 $this->assertTrue($cache->set('test', 'test'));
1409 $this->assertTrue($cache->purge());
1410 $this->assertFalse($cache->get('test'));
1412 // Test the many commands.
1413 $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1414 $result = $cache->get_many(array('a', 'b', 'c'));
1415 $this->assertInternalType('array', $result);
1416 $this->assertCount(3, $result);
1417 $this->assertArrayHasKey('a', $result);
1418 $this->assertArrayHasKey('b', $result);
1419 $this->assertArrayHasKey('c', $result);
1420 $this->assertEquals('A', $result['a']);
1421 $this->assertEquals('B', $result['b']);
1422 $this->assertEquals('C', $result['c']);
1423 $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1424 $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1425 $result = $cache->get_many(array('a', 'b', 'c'));
1426 $this->assertInternalType('array', $result);
1427 $this->assertCount(3, $result);
1428 $this->assertArrayHasKey('a', $result);
1429 $this->assertArrayHasKey('b', $result);
1430 $this->assertArrayHasKey('c', $result);
1431 $this->assertFalse($result['a']);
1432 $this->assertEquals('B', $result['b']);
1433 $this->assertFalse($result['c']);
1435 // Test non-recursive deletes.
1436 $this->assertTrue($cache->set('test', 'test'));
1437 $this->assertSame('test', $cache->get('test'));
1438 $this->assertTrue($cache->delete('test', false));
1439 // We should still have it on a deeper loader.
1440 $this->assertSame('test', $cache->get('test'));
1441 // Test non-recusive with many functions.
1442 $this->assertSame(3, $cache->set_many(array(
1443 'one' => 'one',
1444 'two' => 'two',
1445 'three' => 'three'
1446 )));
1447 $this->assertSame('one', $cache->get('one'));
1448 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1449 $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1450 $this->assertSame('one', $cache->get('one'));
1451 $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1455 * Test switching users with session caches.
1457 public function test_session_cache_switch_user() {
1458 $this->resetAfterTest(true);
1459 $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache');
1460 $user1 = $this->getDataGenerator()->create_user();
1461 $user2 = $this->getDataGenerator()->create_user();
1463 // Log in as the first user.
1464 $this->setUser($user1);
1465 $sesskey1 = sesskey();
1467 // Set a basic value in the cache.
1468 $cache->set('var', 1);
1469 $this->assertTrue($cache->has('var'));
1470 $this->assertEquals(1, $cache->get('var'));
1472 // Change to the second user.
1473 $this->setUser($user2);
1474 $sesskey2 = sesskey();
1476 // Make sure the cache doesn't give us the data for the last user.
1477 $this->assertNotEquals($sesskey1, $sesskey2);
1478 $this->assertFalse($cache->has('var'));
1479 $this->assertEquals(false, $cache->get('var'));
1483 * Test switching users with session caches.
1485 public function test_session_cache_switch_user_application_mapping() {
1486 $this->resetAfterTest(true);
1487 $instance = cache_config_testing::instance(true);
1488 $instance->phpunit_add_file_store('testfilestore');
1489 $instance->phpunit_add_definition('phpunit/testappsession', array(
1490 'mode' => cache_store::MODE_SESSION,
1491 'component' => 'phpunit',
1492 'area' => 'testappsession'
1494 $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3);
1495 $cache = cache::make('phpunit', 'testappsession');
1496 $user1 = $this->getDataGenerator()->create_user();
1497 $user2 = $this->getDataGenerator()->create_user();
1499 // Log in as the first user.
1500 $this->setUser($user1);
1501 $sesskey1 = sesskey();
1503 // Set a basic value in the cache.
1504 $cache->set('var', 1);
1505 $this->assertTrue($cache->has('var'));
1506 $this->assertEquals(1, $cache->get('var'));
1508 // Change to the second user.
1509 $this->setUser($user2);
1510 $sesskey2 = sesskey();
1512 // Make sure the cache doesn't give us the data for the last user.
1513 $this->assertNotEquals($sesskey1, $sesskey2);
1514 $this->assertFalse($cache->has('var'));
1515 $this->assertEquals(false, $cache->get('var'));
1519 * Test two session caches being used at once to confirm collisions don't occur.
1521 public function test_dual_session_caches() {
1522 $instance = cache_config_testing::instance(true);
1523 $instance->phpunit_add_definition('phpunit/testsess1', array(
1524 'mode' => cache_store::MODE_SESSION,
1525 'component' => 'phpunit',
1526 'area' => 'testsess1'
1528 $instance->phpunit_add_definition('phpunit/testsess2', array(
1529 'mode' => cache_store::MODE_SESSION,
1530 'component' => 'phpunit',
1531 'area' => 'testsess2'
1533 $cache1 = cache::make('phpunit', 'testsess1');
1534 $cache2 = cache::make('phpunit', 'testsess2');
1536 $this->assertFalse($cache1->has('test'));
1537 $this->assertFalse($cache2->has('test'));
1539 $this->assertTrue($cache1->set('test', '1'));
1541 $this->assertTrue($cache1->has('test'));
1542 $this->assertFalse($cache2->has('test'));
1544 $this->assertTrue($cache2->set('test', '2'));
1546 $this->assertEquals(1, $cache1->get('test'));
1547 $this->assertEquals(2, $cache2->get('test'));
1549 $this->assertTrue($cache1->delete('test'));
1553 * Test multiple session caches when switching user.
1555 public function test_session_cache_switch_user_multiple() {
1556 $this->resetAfterTest(true);
1557 $cache1 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache1');
1558 $cache2 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache2');
1559 $user1 = $this->getDataGenerator()->create_user();
1560 $user2 = $this->getDataGenerator()->create_user();
1562 // Log in as the first user.
1563 $this->setUser($user1);
1564 $sesskey1 = sesskey();
1566 // Set a basic value in the caches.
1567 $cache1->set('var', 1);
1568 $cache2->set('var', 2);
1569 $this->assertEquals(1, $cache1->get('var'));
1570 $this->assertEquals(2, $cache2->get('var'));
1572 // Change to the second user.
1573 $this->setUser($user2);
1574 $sesskey2 = sesskey();
1576 // Make sure the cache doesn't give us the data for the last user.
1577 // Also make sure that switching the user has lead to both caches being purged.
1578 $this->assertNotEquals($sesskey1, $sesskey2);
1579 $this->assertEquals(false, $cache1->get('var'));
1580 $this->assertEquals(false, $cache2->get('var'));
1584 * Test application locking.
1586 public function test_application_locking() {
1587 $instance = cache_config_testing::instance(true);
1588 $instance->phpunit_add_definition('phpunit/test_application_locking', array(
1589 'mode' => cache_store::MODE_APPLICATION,
1590 'component' => 'phpunit',
1591 'area' => 'test_application_locking',
1592 'staticacceleration' => true,
1593 'staticaccelerationsize' => 1,
1594 'requirelockingread' => true,
1595 'requirelockingwrite' => true
1597 $cache = cache::make('phpunit', 'test_application_locking');
1598 $this->assertInstanceOf('cache_application', $cache);
1600 $this->assertTrue($cache->set('a', 'A'));
1601 $this->assertTrue($cache->set('b', 'B'));
1602 $this->assertTrue($cache->set('c', 'C'));
1603 $this->assertEquals('A', $cache->get('a'));
1604 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1605 $this->assertTrue($cache->delete('a'));
1606 $this->assertFalse($cache->has('a'));
1610 * Test the static cache_helper method purge_stores_used_by_definition.
1612 public function test_purge_stores_used_by_definition() {
1613 $instance = cache_config_testing::instance(true);
1614 $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', array(
1615 'mode' => cache_store::MODE_APPLICATION,
1616 'component' => 'phpunit',
1617 'area' => 'test_purge_stores_used_by_definition'
1619 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1620 $this->assertInstanceOf('cache_application', $cache);
1621 $this->assertTrue($cache->set('test', 'test'));
1622 unset($cache);
1624 cache_helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition');
1626 $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
1627 $this->assertInstanceOf('cache_application', $cache);
1628 $this->assertFalse($cache->get('test'));
1632 * Test purge routines.
1634 public function test_purge_routines() {
1635 $instance = cache_config_testing::instance(true);
1636 $instance->phpunit_add_definition('phpunit/purge1', array(
1637 'mode' => cache_store::MODE_APPLICATION,
1638 'component' => 'phpunit',
1639 'area' => 'purge1'
1641 $instance->phpunit_add_definition('phpunit/purge2', array(
1642 'mode' => cache_store::MODE_APPLICATION,
1643 'component' => 'phpunit',
1644 'area' => 'purge2',
1645 'requireidentifiers' => array(
1646 'id'
1650 $factory = cache_factory::instance();
1651 $definition = $factory->create_definition('phpunit', 'purge1');
1652 $this->assertFalse($definition->has_required_identifiers());
1653 $cache = $factory->create_cache($definition);
1654 $this->assertInstanceOf('cache_application', $cache);
1655 $this->assertTrue($cache->set('test', 'test'));
1656 $this->assertTrue($cache->has('test'));
1657 cache_helper::purge_by_definition('phpunit', 'purge1');
1658 $this->assertFalse($cache->has('test'));
1660 $factory = cache_factory::instance();
1661 $definition = $factory->create_definition('phpunit', 'purge2');
1662 $this->assertTrue($definition->has_required_identifiers());
1663 $cache = $factory->create_cache($definition);
1664 $this->assertInstanceOf('cache_application', $cache);
1665 $this->assertTrue($cache->set('test', 'test'));
1666 $this->assertTrue($cache->has('test'));
1667 cache_helper::purge_stores_used_by_definition('phpunit', 'purge2');
1668 $this->assertFalse($cache->has('test'));
1670 try {
1671 cache_helper::purge_by_definition('phpunit', 'purge2');
1672 $this->fail('Should not be able to purge a definition required identifiers without providing them.');
1673 } catch (coding_exception $ex) {
1674 $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage());
1679 * Test that the default stores all support searching.
1681 public function test_defaults_support_searching() {
1682 $instance = cache_config_testing::instance(true);
1683 $instance->phpunit_add_definition('phpunit/search1', array(
1684 'mode' => cache_store::MODE_APPLICATION,
1685 'component' => 'phpunit',
1686 'area' => 'search1',
1687 'requiresearchable' => true
1689 $instance->phpunit_add_definition('phpunit/search2', array(
1690 'mode' => cache_store::MODE_SESSION,
1691 'component' => 'phpunit',
1692 'area' => 'search2',
1693 'requiresearchable' => true
1695 $instance->phpunit_add_definition('phpunit/search3', array(
1696 'mode' => cache_store::MODE_REQUEST,
1697 'component' => 'phpunit',
1698 'area' => 'search3',
1699 'requiresearchable' => true
1701 $factory = cache_factory::instance();
1703 // Test application cache is searchable.
1704 $definition = $factory->create_definition('phpunit', 'search1');
1705 $this->assertInstanceOf('cache_definition', $definition);
1706 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1707 $cache = $factory->create_cache($definition);
1708 $this->assertInstanceOf('cache_application', $cache);
1709 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1711 // Test session cache is searchable.
1712 $definition = $factory->create_definition('phpunit', 'search2');
1713 $this->assertInstanceOf('cache_definition', $definition);
1714 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1715 $cache = $factory->create_cache($definition);
1716 $this->assertInstanceOf('cache_session', $cache);
1717 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1719 // Test request cache is searchable.
1720 $definition = $factory->create_definition('phpunit', 'search3');
1721 $this->assertInstanceOf('cache_definition', $definition);
1722 $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
1723 $cache = $factory->create_cache($definition);
1724 $this->assertInstanceOf('cache_request', $cache);
1725 $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
1728 public function test_static_acceleration() {
1729 $instance = cache_config_testing::instance();
1730 $instance->phpunit_add_definition('phpunit/accelerated', array(
1731 'mode' => cache_store::MODE_APPLICATION,
1732 'component' => 'phpunit',
1733 'area' => 'accelerated',
1734 'staticacceleration' => true,
1735 'staticaccelerationsize' => 3,
1737 $instance->phpunit_add_definition('phpunit/accelerated2', array(
1738 'mode' => cache_store::MODE_APPLICATION,
1739 'component' => 'phpunit',
1740 'area' => 'accelerated2',
1741 'staticacceleration' => true,
1742 'staticaccelerationsize' => 3,
1744 $instance->phpunit_add_definition('phpunit/accelerated3', array(
1745 'mode' => cache_store::MODE_APPLICATION,
1746 'component' => 'phpunit',
1747 'area' => 'accelerated3',
1748 'staticacceleration' => true,
1749 'staticaccelerationsize' => 3,
1751 $instance->phpunit_add_definition('phpunit/accelerated4', array(
1752 'mode' => cache_store::MODE_APPLICATION,
1753 'component' => 'phpunit',
1754 'area' => 'accelerated4',
1755 'staticacceleration' => true,
1756 'staticaccelerationsize' => 4,
1758 $instance->phpunit_add_definition('phpunit/simpledataarea1', array(
1759 'mode' => cache_store::MODE_APPLICATION,
1760 'component' => 'phpunit',
1761 'area' => 'simpledataarea1',
1762 'staticacceleration' => true,
1763 'simpledata' => false
1765 $instance->phpunit_add_definition('phpunit/simpledataarea2', array(
1766 'mode' => cache_store::MODE_APPLICATION,
1767 'component' => 'phpunit',
1768 'area' => 'simpledataarea2',
1769 'staticacceleration' => true,
1770 'simpledata' => true
1773 $cache = cache::make('phpunit', 'accelerated');
1774 $this->assertInstanceOf('cache_phpunit_application', $cache);
1776 // Set and get three elements.
1777 $this->assertTrue($cache->set('a', 'A'));
1778 $this->assertTrue($cache->set('b', 'B'));
1779 $this->assertTrue($cache->set('c', 'C'));
1780 $this->assertEquals('A', $cache->get('a'));
1781 $this->assertEquals(array('b' => 'B', 'c' => 'C'), $cache->get_many(array('b', 'c')));
1783 // Make sure all items are in static acceleration array.
1784 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1785 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1786 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1788 // Add new value and make sure it is in cache and it is in array.
1789 $this->assertTrue($cache->set('d', 'D'));
1790 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1791 $this->assertEquals('D', $cache->get('d'));
1793 // Now the least recent accessed item (a) is no longer in acceleration array.
1794 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1795 $this->assertEquals('B', $cache->phpunit_static_acceleration_get('b'));
1796 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1798 // Adding and deleting element.
1799 $this->assertTrue($cache->set('a', 'A'));
1800 $this->assertTrue($cache->delete('a'));
1801 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1802 $this->assertFalse($cache->has('a'));
1804 // Make sure "purge" deletes from the array as well.
1805 $cache->purge();
1806 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1807 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1808 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1809 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1810 $this->assertFalse($cache->phpunit_static_acceleration_get('e'));
1812 // Check that the array holds the last accessed items by get/set.
1813 $this->assertTrue($cache->set('a', 'A'));
1814 $this->assertTrue($cache->set('b', 'B'));
1815 $this->assertTrue($cache->set('c', 'C'));
1816 $this->assertTrue($cache->set('d', 'D'));
1817 $this->assertTrue($cache->set('e', 'E'));
1818 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1819 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1820 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1821 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1822 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1824 /** @var cache_phpunit_application $cache */
1825 $cache = cache::make('phpunit', 'accelerated2');
1826 $this->assertInstanceOf('cache_phpunit_application', $cache);
1828 // Check that the array holds the last accessed items by get/set.
1829 $this->assertTrue($cache->set('a', 'A'));
1830 $this->assertTrue($cache->set('b', 'B'));
1831 $this->assertTrue($cache->set('c', 'C'));
1832 $this->assertTrue($cache->set('d', 'D'));
1833 $this->assertTrue($cache->set('e', 'E'));
1834 // Current keys in the array: c, d, e.
1835 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1836 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1837 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1838 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1839 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1841 $this->assertEquals('A', $cache->get('a'));
1842 // Current keys in the array: d, e, a.
1843 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1844 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1845 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1846 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1847 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1849 // Current keys in the array: d, e, a.
1850 $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c')));
1851 // Current keys in the array: e, a, c.
1852 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1853 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1854 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1855 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1856 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1859 $cache = cache::make('phpunit', 'accelerated3');
1860 $this->assertInstanceOf('cache_phpunit_application', $cache);
1862 // Check that the array holds the last accessed items by get/set.
1863 $this->assertTrue($cache->set('a', 'A'));
1864 $this->assertTrue($cache->set('b', 'B'));
1865 $this->assertTrue($cache->set('c', 'C'));
1866 $this->assertTrue($cache->set('d', 'D'));
1867 $this->assertTrue($cache->set('e', 'E'));
1868 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1869 $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
1870 $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
1871 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1872 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1874 $this->assertTrue($cache->set('b', 'B2'));
1875 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1876 $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b'));
1877 $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
1878 $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
1879 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1881 $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3')));
1882 $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
1883 $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b'));
1884 $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c'));
1885 $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
1886 $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
1888 $cache = cache::make('phpunit', 'accelerated4');
1889 $this->assertInstanceOf('cache_phpunit_application', $cache);
1890 $this->assertTrue($cache->set('a', 'A'));
1891 $this->assertTrue($cache->set('a', 'A'));
1892 $this->assertTrue($cache->set('a', 'A'));
1893 $this->assertTrue($cache->set('a', 'A'));
1894 $this->assertTrue($cache->set('a', 'A'));
1895 $this->assertTrue($cache->set('a', 'A'));
1896 $this->assertTrue($cache->set('a', 'A'));
1897 $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
1898 $this->assertEquals('A', $cache->get('a'));
1900 // Setting simpledata to false objects are cloned when retrieving data.
1901 $cache = cache::make('phpunit', 'simpledataarea1');
1902 $notreallysimple = new stdClass();
1903 $notreallysimple->name = 'a';
1904 $cache->set('a', $notreallysimple);
1905 $returnedinstance1 = $cache->get('a');
1906 $returnedinstance2 = $cache->get('a');
1907 $returnedinstance1->name = 'b';
1908 $this->assertEquals('a', $returnedinstance2->name);
1910 // Setting simpledata to true we assume that data does not contain references.
1911 $cache = cache::make('phpunit', 'simpledataarea2');
1912 $notreallysimple = new stdClass();
1913 $notreallysimple->name = 'a';
1914 $cache->set('a', $notreallysimple);
1915 $returnedinstance1 = $cache->get('a');
1916 $returnedinstance2 = $cache->get('a');
1917 $returnedinstance1->name = 'b';
1918 $this->assertEquals('b', $returnedinstance2->name);
1921 public function test_performance_debug() {
1922 global $CFG;
1923 $this->resetAfterTest(true);
1924 $CFG->perfdebug = 15;
1926 $instance = cache_config_testing::instance();
1927 $applicationid = 'phpunit/applicationperf';
1928 $instance->phpunit_add_definition($applicationid, array(
1929 'mode' => cache_store::MODE_APPLICATION,
1930 'component' => 'phpunit',
1931 'area' => 'applicationperf'
1933 $sessionid = 'phpunit/sessionperf';
1934 $instance->phpunit_add_definition($sessionid, array(
1935 'mode' => cache_store::MODE_SESSION,
1936 'component' => 'phpunit',
1937 'area' => 'sessionperf'
1939 $requestid = 'phpunit/requestperf';
1940 $instance->phpunit_add_definition($requestid, array(
1941 'mode' => cache_store::MODE_REQUEST,
1942 'component' => 'phpunit',
1943 'area' => 'requestperf'
1946 $application = cache::make('phpunit', 'applicationperf');
1947 $session = cache::make('phpunit', 'sessionperf');
1948 $request = cache::make('phpunit', 'requestperf');
1950 // Check that no stats are recorded for these definitions yet.
1951 $stats = cache_helper::get_stats();
1952 $this->assertArrayNotHasKey($applicationid, $stats);
1953 $this->assertArrayHasKey($sessionid, $stats); // Session cache sets a key on construct.
1954 $this->assertArrayNotHasKey($requestid, $stats);
1956 // Check that stores register misses.
1957 $this->assertFalse($application->get('missMe'));
1958 $this->assertFalse($application->get('missMe'));
1959 $this->assertFalse($session->get('missMe'));
1960 $this->assertFalse($session->get('missMe'));
1961 $this->assertFalse($session->get('missMe'));
1962 $this->assertFalse($request->get('missMe'));
1963 $this->assertFalse($request->get('missMe'));
1964 $this->assertFalse($request->get('missMe'));
1965 $this->assertFalse($request->get('missMe'));
1967 $endstats = cache_helper::get_stats();
1968 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['misses']);
1969 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits']);
1970 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets']);
1971 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['misses']);
1972 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits']);
1973 $this->assertEquals(1, $endstats[$sessionid]['stores']['cachestore_session']['sets']);
1974 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['misses']);
1975 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits']);
1976 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets']);
1978 $startstats = cache_helper::get_stats();
1980 // Check that stores register sets.
1981 $this->assertTrue($application->set('setMe1', 1));
1982 $this->assertTrue($application->set('setMe2', 2));
1983 $this->assertTrue($session->set('setMe1', 1));
1984 $this->assertTrue($session->set('setMe2', 2));
1985 $this->assertTrue($session->set('setMe3', 3));
1986 $this->assertTrue($request->set('setMe1', 1));
1987 $this->assertTrue($request->set('setMe2', 2));
1988 $this->assertTrue($request->set('setMe3', 3));
1989 $this->assertTrue($request->set('setMe4', 4));
1991 $endstats = cache_helper::get_stats();
1992 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
1993 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
1994 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
1995 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
1996 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
1997 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
1998 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
1999 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2000 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2001 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2002 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2003 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2004 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2005 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2006 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2007 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2008 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2009 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2011 $startstats = cache_helper::get_stats();
2013 // Check that stores register hits.
2014 $this->assertEquals($application->get('setMe1'), 1);
2015 $this->assertEquals($application->get('setMe2'), 2);
2016 $this->assertEquals($session->get('setMe1'), 1);
2017 $this->assertEquals($session->get('setMe2'), 2);
2018 $this->assertEquals($session->get('setMe3'), 3);
2019 $this->assertEquals($request->get('setMe1'), 1);
2020 $this->assertEquals($request->get('setMe2'), 2);
2021 $this->assertEquals($request->get('setMe3'), 3);
2022 $this->assertEquals($request->get('setMe4'), 4);
2024 $endstats = cache_helper::get_stats();
2025 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2026 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2027 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2028 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2029 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2030 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2031 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2032 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2033 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2034 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2035 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2036 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2037 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2038 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2039 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2040 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2041 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2042 $startstats[$requestid]['stores']['cachestore_static']['sets']);
2044 $startstats = cache_helper::get_stats();
2046 // Check that stores register through get_many.
2047 $application->get_many(array('setMe1', 'setMe2'));
2048 $session->get_many(array('setMe1', 'setMe2', 'setMe3'));
2049 $request->get_many(array('setMe1', 'setMe2', 'setMe3', 'setMe4'));
2051 $endstats = cache_helper::get_stats();
2052 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['misses'] -
2053 $startstats[$applicationid]['stores']['cachestore_file']['misses']);
2054 $this->assertEquals(2, $endstats[$applicationid]['stores']['cachestore_file']['hits'] -
2055 $startstats[$applicationid]['stores']['cachestore_file']['hits']);
2056 $this->assertEquals(0, $endstats[$applicationid]['stores']['cachestore_file']['sets'] -
2057 $startstats[$applicationid]['stores']['cachestore_file']['sets']);
2058 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['misses'] -
2059 $startstats[$sessionid]['stores']['cachestore_session']['misses']);
2060 $this->assertEquals(3, $endstats[$sessionid]['stores']['cachestore_session']['hits'] -
2061 $startstats[$sessionid]['stores']['cachestore_session']['hits']);
2062 $this->assertEquals(0, $endstats[$sessionid]['stores']['cachestore_session']['sets'] -
2063 $startstats[$sessionid]['stores']['cachestore_session']['sets']);
2064 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['misses'] -
2065 $startstats[$requestid]['stores']['cachestore_static']['misses']);
2066 $this->assertEquals(4, $endstats[$requestid]['stores']['cachestore_static']['hits'] -
2067 $startstats[$requestid]['stores']['cachestore_static']['hits']);
2068 $this->assertEquals(0, $endstats[$requestid]['stores']['cachestore_static']['sets'] -
2069 $startstats[$requestid]['stores']['cachestore_static']['sets']);