forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.php
2377 lines (2065 loc) · 105 KB
/
cache_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* PHPunit tests for the cache API
*
* This file is part of Moodle's cache API, affectionately called MUC.
* It contains the components that are requried in order to use caching.
*
* @package core
* @category cache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Include the necessary evils.
global $CFG;
require_once($CFG->dirroot.'/cache/locallib.php');
require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
/**
* PHPunit tests for the cache API
*
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_cache_testcase extends advanced_testcase {
/**
* Set things back to the default before each test.
*/
public function setUp() {
parent::setUp();
cache_factory::reset();
cache_config_testing::create_default_configuration();
}
/**
* Final task is to reset the cache system
*/
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
cache_factory::reset();
}
/**
* Returns the expected application cache store.
* @return string
*/
protected function get_expected_application_cache_store() {
global $CFG;
$expected = 'cachestore_file';
// Verify if we are using any of the available ways to use a different application store within tests.
if (defined('TEST_CACHE_USING_APPLICATION_STORE') && preg_match('#[a-zA-Z][a-zA-Z0-9_]*#', TEST_CACHE_USING_APPLICATION_STORE)) {
// 1st way. Using some of the testing servers.
$expected = 'cachestore_'.(string)TEST_CACHE_USING_APPLICATION_STORE;
} else if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH && !empty($CFG->altcacheconfigpath)) {
// 2nd way. Using an alternative configuration.
$defaultstores = cache_helper::get_stores_suitable_for_mode_default();
$instance = cache_config::instance();
// Iterate over defined mode mappings until we get an application one not being the default.
foreach ($instance->get_mode_mappings() as $mapping) {
// If the store is not for application mode, ignore.
if ($mapping['mode'] !== cache_store::MODE_APPLICATION) {
continue;
}
// If the store matches some default mapping store name, ignore.
if (array_key_exists($mapping['store'], $defaultstores) && !empty($defaultstores[$mapping['store']]['default'])) {
continue;
}
// Arrived here, have found an application mode store not being the default mapped one (file),
// that's the one we are using in the configuration for sure.
$expected = 'cachestore_'.$mapping['store'];
}
}
return $expected;
}
/**
* Tests cache configuration
*/
public function test_cache_config() {
global $CFG;
if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH &&
!empty($CFG->altcacheconfigpath)) {
// We need to skip this test - it checks the default config structure, but very likely we arn't using the
// default config structure here so theres no point in running the test.
$this->markTestSkipped('Skipped testing default cache config structure as alt cache path is being used.');
}
if (defined('TEST_CACHE_USING_APPLICATION_STORE')) {
// We need to skip this test - it checks the default config structure, but very likely we arn't using the
// default config structure here because we are testing against an alternative application store.
$this->markTestSkipped('Skipped testing default cache config structure as alt application store is being used.');
}
$instance = cache_config::instance();
$this->assertInstanceOf('cache_config_testing', $instance);
$this->assertTrue(cache_config_testing::config_file_exists());
$stores = $instance->get_all_stores();
$this->assertCount(3, $stores);
foreach ($stores as $name => $store) {
// Check its an array.
$this->assertInternalType('array', $store);
// Check the name is the key.
$this->assertEquals($name, $store['name']);
// Check that it has been declared default.
$this->assertTrue($store['default']);
// Required attributes = name + plugin + configuration + modes + features.
$this->assertArrayHasKey('name', $store);
$this->assertArrayHasKey('plugin', $store);
$this->assertArrayHasKey('configuration', $store);
$this->assertArrayHasKey('modes', $store);
$this->assertArrayHasKey('features', $store);
}
$modemappings = $instance->get_mode_mappings();
$this->assertCount(3, $modemappings);
$modes = array(
cache_store::MODE_APPLICATION => false,
cache_store::MODE_SESSION => false,
cache_store::MODE_REQUEST => false,
);
foreach ($modemappings as $mapping) {
// We expect 3 properties.
$this->assertCount(3, $mapping);
// Required attributes = mode + store.
$this->assertArrayHasKey('mode', $mapping);
$this->assertArrayHasKey('store', $mapping);
// Record the mode.
$modes[$mapping['mode']] = true;
}
// Must have the default 3 modes and no more.
$this->assertCount(3, $mapping);
foreach ($modes as $mode) {
$this->assertTrue($mode);
}
$definitions = $instance->get_definitions();
// The event invalidation definition is required for the cache API and must be there.
$this->assertArrayHasKey('core/eventinvalidation', $definitions);
$definitionmappings = $instance->get_definition_mappings();
foreach ($definitionmappings as $mapping) {
// Required attributes = definition + store.
$this->assertArrayHasKey('definition', $mapping);
$this->assertArrayHasKey('store', $mapping);
}
}
/**
* Tests for cache keys that would break on windows.
*/
public function test_windows_nasty_keys() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/windowskeytest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'windowskeytest',
'simplekeys' => true,
'simpledata' => true
));
$cache = cache::make('phpunit', 'windowskeytest');
$this->assertTrue($cache->set('contest', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('contest'));
}
/**
* Tests set_identifiers fails post cache creation.
*
* set_identifiers cannot be called after initial cache instantiation, as you need to create a difference cache.
*/
public function test_set_identifiers() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/identifier', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'identifier',
'simplekeys' => true,
'simpledata' => true,
'staticacceleration' => true
));
$cache = cache::make('phpunit', 'identifier', array('area'));
$this->assertTrue($cache->set('contest', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('contest'));
$this->expectException('coding_exception');
$cache->set_identifiers(array());
}
/**
* Tests the default application cache
*/
public function test_default_application_cache() {
$cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest');
$this->assertInstanceOf('cache_application', $cache);
$this->run_on_cache($cache);
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/test_default_application_cache', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'test_default_application_cache',
'staticacceleration' => true,
'staticaccelerationsize' => 1
));
$cache = cache::make('phpunit', 'test_default_application_cache');
$this->assertInstanceOf('cache_application', $cache);
$this->run_on_cache($cache);
}
/**
* Tests the default session cache
*/
public function test_default_session_cache() {
$cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest');
$this->assertInstanceOf('cache_session', $cache);
$this->run_on_cache($cache);
}
/**
* Tests the default request cache
*/
public function test_default_request_cache() {
$cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest');
$this->assertInstanceOf('cache_request', $cache);
$this->run_on_cache($cache);
}
/**
* Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
*/
public function test_on_cache_without_store() {
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/nostoretest1', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'nostoretest1',
));
$instance->phpunit_add_definition('phpunit/nostoretest2', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'nostoretest2',
'staticacceleration' => true
));
$instance->phpunit_remove_stores();
$cache = cache::make('phpunit', 'nostoretest1');
$this->run_on_cache($cache);
$cache = cache::make('phpunit', 'nostoretest2');
$this->run_on_cache($cache);
}
/**
* Runs a standard series of access and use tests on a cache instance.
*
* This function is great because we can use it to ensure all of the loaders perform exactly the same way.
*
* @param cache_loader $cache
*/
protected function run_on_cache(cache_loader $cache) {
$key = 'contestkey';
$datascalars = array('test data', null);
$dataarray = array('contest' => 'data', 'part' => 'two');
$dataobject = (object)$dataarray;
foreach ($datascalars as $datascalar) {
$this->assertTrue($cache->purge());
// Check all read methods.
$this->assertFalse($cache->get($key));
$this->assertFalse($cache->has($key));
$result = $cache->get_many(array($key));
$this->assertCount(1, $result);
$this->assertFalse(reset($result));
$this->assertFalse($cache->has_any(array($key)));
$this->assertFalse($cache->has_all(array($key)));
// Set the data.
$this->assertTrue($cache->set($key, $datascalar));
// Setting it more than once should be permitted.
$this->assertTrue($cache->set($key, $datascalar));
// Recheck the read methods.
$this->assertEquals($datascalar, $cache->get($key));
$this->assertTrue($cache->has($key));
$result = $cache->get_many(array($key));
$this->assertCount(1, $result);
$this->assertEquals($datascalar, reset($result));
$this->assertTrue($cache->has_any(array($key)));
$this->assertTrue($cache->has_all(array($key)));
// Delete it.
$this->assertTrue($cache->delete($key));
// Check its gone.
$this->assertFalse($cache->get($key));
$this->assertFalse($cache->has($key));
}
// Test arrays.
$this->assertTrue($cache->set($key, $dataarray));
$this->assertEquals($dataarray, $cache->get($key));
// Test objects.
$this->assertTrue($cache->set($key, $dataobject));
$this->assertEquals($dataobject, $cache->get($key));
$starttime = microtime(true);
$specobject = new cache_phpunit_dummy_object('red', 'blue', $starttime);
$this->assertTrue($cache->set($key, $specobject));
$result = $cache->get($key);
$this->assertInstanceOf('cache_phpunit_dummy_object', $result);
$this->assertEquals('red_ptc_wfc', $result->property1);
$this->assertEquals('blue_ptc_wfc', $result->property2);
$this->assertGreaterThan($starttime, $result->propertytime);
// Test array of objects.
$specobject = new cache_phpunit_dummy_object('red', 'blue', $starttime);
$data = new cacheable_object_array(array(
clone($specobject),
clone($specobject),
clone($specobject))
);
$this->assertTrue($cache->set($key, $data));
$result = $cache->get($key);
$this->assertInstanceOf('cacheable_object_array', $result);
$this->assertCount(3, $data);
foreach ($result as $item) {
$this->assertInstanceOf('cache_phpunit_dummy_object', $item);
$this->assertEquals('red_ptc_wfc', $item->property1);
$this->assertEquals('blue_ptc_wfc', $item->property2);
// Ensure that wake from cache is called in all cases.
$this->assertGreaterThan($starttime, $item->propertytime);
}
// Test set many.
$cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
$this->assertEquals('data1', $cache->get('key1'));
$this->assertEquals('data2', $cache->get('key2'));
$this->assertEquals(null, $cache->get('key3'));
$this->assertTrue($cache->delete('key1'));
$this->assertTrue($cache->delete('key2'));
$this->assertTrue($cache->delete('key3'));
$cache->set_many(array(
'key1' => array(1, 2, 3),
'key2' => array(3, 2, 1),
));
$this->assertInternalType('array', $cache->get('key1'));
$this->assertInternalType('array', $cache->get('key2'));
$this->assertCount(3, $cache->get('key1'));
$this->assertCount(3, $cache->get('key2'));
$this->assertInternalType('array', $cache->get_many(array('key1', 'key2')));
$this->assertCount(2, $cache->get_many(array('key1', 'key2')));
$this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
// Test delete many.
$this->assertTrue($cache->set('key1', 'data1'));
$this->assertTrue($cache->set('key2', 'data2'));
$this->assertTrue($cache->set('key3', null));
$this->assertEquals('data1', $cache->get('key1'));
$this->assertEquals('data2', $cache->get('key2'));
$this->assertEquals(null, $cache->get('key3'));
$this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
$this->assertFalse($cache->get('key1'));
$this->assertFalse($cache->get('key2'));
$this->assertFalse($cache->get('key3'));
// Quick reference test.
$obj = new stdClass;
$obj->key = 'value';
$ref =& $obj;
$this->assertTrue($cache->set('obj', $obj));
$obj->key = 'eulav';
$var = $cache->get('obj');
$this->assertInstanceOf('stdClass', $var);
$this->assertEquals('value', $var->key);
$ref->key = 'eulav';
$var = $cache->get('obj');
$this->assertInstanceOf('stdClass', $var);
$this->assertEquals('value', $var->key);
$this->assertTrue($cache->delete('obj'));
// Deep reference test.
$obj1 = new stdClass;
$obj1->key = 'value';
$obj2 = new stdClass;
$obj2->key = 'test';
$obj3 = new stdClass;
$obj3->key = 'pork';
$obj1->subobj =& $obj2;
$obj2->subobj =& $obj3;
$this->assertTrue($cache->set('obj', $obj1));
$obj1->key = 'eulav';
$obj2->key = 'tset';
$obj3->key = 'krop';
$var = $cache->get('obj');
$this->assertInstanceOf('stdClass', $var);
$this->assertEquals('value', $var->key);
$this->assertInstanceOf('stdClass', $var->subobj);
$this->assertEquals('test', $var->subobj->key);
$this->assertInstanceOf('stdClass', $var->subobj->subobj);
$this->assertEquals('pork', $var->subobj->subobj->key);
$this->assertTrue($cache->delete('obj'));
// Death reference test... basically we don't want this to die.
$obj = new stdClass;
$obj->key = 'value';
$obj->self =& $obj;
$this->assertTrue($cache->set('obj', $obj));
$var = $cache->get('obj');
$this->assertInstanceOf('stdClass', $var);
$this->assertEquals('value', $var->key);
// Reference test after retrieve.
$obj = new stdClass;
$obj->key = 'value';
$this->assertTrue($cache->set('obj', $obj));
$var1 = $cache->get('obj');
$this->assertInstanceOf('stdClass', $var1);
$this->assertEquals('value', $var1->key);
$var1->key = 'eulav';
$this->assertEquals('eulav', $var1->key);
$var2 = $cache->get('obj');
$this->assertInstanceOf('stdClass', $var2);
$this->assertEquals('value', $var2->key);
$this->assertTrue($cache->delete('obj'));
// Death reference test on get_many... basically we don't want this to die.
$obj = new stdClass;
$obj->key = 'value';
$obj->self =& $obj;
$this->assertEquals(1, $cache->set_many(array('obj' => $obj)));
$var = $cache->get_many(array('obj'));
$this->assertInstanceOf('stdClass', $var['obj']);
$this->assertEquals('value', $var['obj']->key);
// Reference test after retrieve.
$obj = new stdClass;
$obj->key = 'value';
$this->assertEquals(1, $cache->set_many(array('obj' => $obj)));
$var1 = $cache->get_many(array('obj'));
$this->assertInstanceOf('stdClass', $var1['obj']);
$this->assertEquals('value', $var1['obj']->key);
$var1['obj']->key = 'eulav';
$this->assertEquals('eulav', $var1['obj']->key);
$var2 = $cache->get_many(array('obj'));
$this->assertInstanceOf('stdClass', $var2['obj']);
$this->assertEquals('value', $var2['obj']->key);
$this->assertTrue($cache->delete('obj'));
// Test strictness exceptions.
try {
$cache->get('exception', MUST_EXIST);
$this->fail('Exception expected from cache::get using MUST_EXIST');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
$cache->get_many(array('exception1', 'exception2'), MUST_EXIST);
$this->fail('Exception expected from cache::get_many using MUST_EXIST');
} catch (Exception $e) {
$this->assertTrue(true);
}
$cache->set('test', 'test');
try {
$cache->get_many(array('test', 'exception'), MUST_EXIST);
$this->fail('Exception expected from cache::get_many using MUST_EXIST');
} catch (Exception $e) {
$this->assertTrue(true);
}
}
/**
* Tests a definition using a data loader
*/
public function test_definition_data_loader() {
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/datasourcetest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'datasourcetest',
'datasource' => 'cache_phpunit_dummy_datasource',
'datasourcefile' => 'cache/tests/fixtures/lib.php'
));
$cache = cache::make('phpunit', 'datasourcetest');
$this->assertInstanceOf('cache_application', $cache);
// Purge it to be sure.
$this->assertTrue($cache->purge());
// It won't be there yet.
$this->assertFalse($cache->has('Test'));
// It should load it ;).
$this->assertTrue($cache->has('Test', true));
// Purge it to be sure.
$this->assertTrue($cache->purge());
$this->assertEquals('Test has no value really.', $cache->get('Test'));
// Test multiple values.
$this->assertTrue($cache->purge());
$this->assertTrue($cache->set('b', 'B'));
$result = $cache->get_many(array('a', 'b', 'c'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('b', $result);
$this->assertArrayHasKey('c', $result);
$this->assertEquals('a has no value really.', $result['a']);
$this->assertEquals('B', $result['b']);
$this->assertEquals('c has no value really.', $result['c']);
}
/**
* Tests a definition using an overridden loader
*/
public function test_definition_overridden_loader() {
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/overridetest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'overridetest',
'overrideclass' => 'cache_phpunit_dummy_overrideclass',
'overrideclassfile' => 'cache/tests/fixtures/lib.php'
));
$cache = cache::make('phpunit', 'overridetest');
$this->assertInstanceOf('cache_phpunit_dummy_overrideclass', $cache);
$this->assertInstanceOf('cache_application', $cache);
// Purge it to be sure.
$this->assertTrue($cache->purge());
// It won't be there yet.
$this->assertFalse($cache->has('Test'));
// Add it.
$this->assertTrue($cache->set('Test', 'Test has no value really.'));
// Check its there.
$this->assertEquals('Test has no value really.', $cache->get('Test'));
}
/**
* Test the mappingsonly setting.
*/
public function test_definition_mappings_only() {
/** @var cache_config_testing $instance */
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/mappingsonly', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'mappingsonly',
'mappingsonly' => true
), false);
$instance->phpunit_add_definition('phpunit/nonmappingsonly', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'nonmappingsonly',
'mappingsonly' => false
), false);
$cacheonly = cache::make('phpunit', 'mappingsonly');
$this->assertInstanceOf('cache_application', $cacheonly);
$this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class());
$expected = $this->get_expected_application_cache_store();
$cachenon = cache::make('phpunit', 'nonmappingsonly');
$this->assertInstanceOf('cache_application', $cachenon);
$this->assertEquals($expected, $cachenon->phpunit_get_store_class());
}
/**
* Test a very basic definition.
*/
public function test_definition() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/test', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'test',
));
$cache = cache::make('phpunit', 'test');
$this->assertTrue($cache->set('testkey1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('testkey1'));
$this->assertTrue($cache->set('testkey2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
}
/**
* Test a definition using the simple keys.
*/
public function test_definition_simplekeys() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/simplekeytest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'simplekeytest',
'simplekeys' => true
));
$cache = cache::make('phpunit', 'simplekeytest');
$this->assertTrue($cache->set('testkey1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('testkey1'));
$this->assertTrue($cache->set('testkey2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
$cache->purge();
$this->assertTrue($cache->set('1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('1'));
$this->assertTrue($cache->set('2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('2'));
}
/**
* Test a negative TTL on an application cache.
*/
public function test_application_ttl_negative() {
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/ttltest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'ttltest',
'ttl' => -86400 // Set to a day in the past to be extra sure.
));
$cache = cache::make('phpunit', 'ttltest');
$this->assertInstanceOf('cache_application', $cache);
// Purge it to be sure.
$this->assertTrue($cache->purge());
// It won't be there yet.
$this->assertFalse($cache->has('Test'));
// Set it now.
$this->assertTrue($cache->set('Test', 'Test'));
// Check its not there.
$this->assertFalse($cache->has('Test'));
// Double check by trying to get it.
$this->assertFalse($cache->get('Test'));
// Test with multiple keys.
$this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
$result = $cache->get_many(array('a', 'b', 'c'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('b', $result);
$this->assertArrayHasKey('c', $result);
$this->assertFalse($result['a']);
$this->assertFalse($result['b']);
$this->assertFalse($result['c']);
// Test with multiple keys including missing ones.
$result = $cache->get_many(array('a', 'c', 'e'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('c', $result);
$this->assertArrayHasKey('e', $result);
$this->assertFalse($result['a']);
$this->assertFalse($result['c']);
$this->assertFalse($result['e']);
}
/**
* Test a positive TTL on an application cache.
*/
public function test_application_ttl_positive() {
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/ttltest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'ttltest',
'ttl' => 86400 // Set to a day in the future to be extra sure.
));
$cache = cache::make('phpunit', 'ttltest');
$this->assertInstanceOf('cache_application', $cache);
// Purge it to be sure.
$this->assertTrue($cache->purge());
// It won't be there yet.
$this->assertFalse($cache->has('Test'));
// Set it now.
$this->assertTrue($cache->set('Test', 'Test'));
// Check its there.
$this->assertTrue($cache->has('Test'));
// Double check by trying to get it.
$this->assertEquals('Test', $cache->get('Test'));
// Test with multiple keys.
$this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
$result = $cache->get_many(array('a', 'b', 'c'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('b', $result);
$this->assertArrayHasKey('c', $result);
$this->assertEquals('A', $result['a']);
$this->assertEquals('B', $result['b']);
$this->assertEquals('C', $result['c']);
// Test with multiple keys including missing ones.
$result = $cache->get_many(array('a', 'c', 'e'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('c', $result);
$this->assertArrayHasKey('e', $result);
$this->assertEquals('A', $result['a']);
$this->assertEquals('C', $result['c']);
$this->assertEquals(false, $result['e']);
}
/**
* Test a negative TTL on an session cache.
*/
public function test_session_ttl_positive() {
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/ttltest', array(
'mode' => cache_store::MODE_SESSION,
'component' => 'phpunit',
'area' => 'ttltest',
'ttl' => 86400 // Set to a day in the future to be extra sure.
));
$cache = cache::make('phpunit', 'ttltest');
$this->assertInstanceOf('cache_session', $cache);
// Purge it to be sure.
$this->assertTrue($cache->purge());
// It won't be there yet.
$this->assertFalse($cache->has('Test'));
// Set it now.
$this->assertTrue($cache->set('Test', 'Test'));
// Check its there.
$this->assertTrue($cache->has('Test'));
// Double check by trying to get it.
$this->assertEquals('Test', $cache->get('Test'));
// Test with multiple keys.
$this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
$result = $cache->get_many(array('a', 'b', 'c'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('b', $result);
$this->assertArrayHasKey('c', $result);
$this->assertEquals('A', $result['a']);
$this->assertEquals('B', $result['b']);
$this->assertEquals('C', $result['c']);
// Test with multiple keys including missing ones.
$result = $cache->get_many(array('a', 'c', 'e'));
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertArrayHasKey('a', $result);
$this->assertArrayHasKey('c', $result);
$this->assertArrayHasKey('e', $result);
$this->assertEquals('A', $result['a']);
$this->assertEquals('C', $result['c']);
$this->assertEquals(false, $result['e']);
}
/**
* Tests manual locking operations on an application cache
*/
public function test_application_manual_locking() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/lockingtest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'lockingtest'
));
$cache1 = cache::make('phpunit', 'lockingtest');
$cache2 = clone($cache1);
$this->assertTrue($cache1->set('testkey', 'test data'));
$this->assertTrue($cache2->set('testkey', 'test data'));
$this->assertTrue($cache1->acquire_lock('testkey'));
$this->assertFalse($cache2->acquire_lock('testkey'));
$this->assertTrue($cache1->check_lock_state('testkey'));
$this->assertFalse($cache2->check_lock_state('testkey'));
$this->assertTrue($cache1->release_lock('testkey'));
$this->assertFalse($cache2->release_lock('testkey'));
$this->assertTrue($cache1->set('testkey', 'test data'));
$this->assertTrue($cache2->set('testkey', 'test data'));
}
/**
* Tests application cache event invalidation
*/
public function test_application_event_invalidation() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'eventinvalidationtest',
'invalidationevents' => array(
'crazyevent'
)
));
$cache = cache::make('phpunit', 'eventinvalidationtest');
$this->assertTrue($cache->set('testkey1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('testkey1'));
$this->assertTrue($cache->set('testkey2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
// Test invalidating a single entry.
cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
$this->assertFalse($cache->get('testkey1'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
$this->assertTrue($cache->set('testkey1', 'test data 1'));
// Test invalidating both entries.
cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
$this->assertFalse($cache->get('testkey1'));
$this->assertFalse($cache->get('testkey2'));
}
/**
* Tests session cache event invalidation
*/
public function test_session_event_invalidation() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array(
'mode' => cache_store::MODE_SESSION,
'component' => 'phpunit',
'area' => 'test_session_event_invalidation',
'invalidationevents' => array(
'crazyevent'
)
));
$cache = cache::make('phpunit', 'test_session_event_invalidation');
$this->assertInstanceOf('cache_session', $cache);
$this->assertTrue($cache->set('testkey1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('testkey1'));
$this->assertTrue($cache->set('testkey2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
// Test invalidating a single entry.
cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
$this->assertFalse($cache->get('testkey1'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
$this->assertTrue($cache->set('testkey1', 'test data 1'));
// Test invalidating both entries.
cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
$this->assertFalse($cache->get('testkey1'));
$this->assertFalse($cache->get('testkey2'));
}
/**
* Tests application cache definition invalidation
*/
public function test_application_definition_invalidation() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'definitioninvalidation'
));
$cache = cache::make('phpunit', 'definitioninvalidation');
$this->assertTrue($cache->set('testkey1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('testkey1'));
$this->assertTrue($cache->set('testkey2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
$this->assertFalse($cache->get('testkey1'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
$this->assertTrue($cache->set('testkey1', 'test data 1'));
cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
$this->assertFalse($cache->get('testkey1'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
$this->assertTrue($cache->set('testkey1', 'test data 1'));
cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
$this->assertFalse($cache->get('testkey1'));
$this->assertFalse($cache->get('testkey2'));
}
/**
* Tests session cache definition invalidation
*/
public function test_session_definition_invalidation() {
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array(
'mode' => cache_store::MODE_SESSION,
'component' => 'phpunit',
'area' => 'test_session_definition_invalidation'
));
$cache = cache::make('phpunit', 'test_session_definition_invalidation');
$this->assertInstanceOf('cache_session', $cache);
$this->assertTrue($cache->set('testkey1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('testkey1'));
$this->assertTrue($cache->set('testkey2', 'test data 2'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(), 'testkey1');
$this->assertFalse($cache->get('testkey1'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
$this->assertTrue($cache->set('testkey1', 'test data 1'));
cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
array('testkey1'));
$this->assertFalse($cache->get('testkey1'));
$this->assertEquals('test data 2', $cache->get('testkey2'));
$this->assertTrue($cache->set('testkey1', 'test data 1'));
cache_helper::invalidate_by_definition('phpunit', 'test_session_definition_invalidation', array(),
array('testkey1', 'testkey2'));
$this->assertFalse($cache->get('testkey1'));
$this->assertFalse($cache->get('testkey2'));
}
/**
* Tests application cache event invalidation over a distributed setup.
*/
public function test_distributed_application_event_invalidation() {
global $CFG;
// This is going to be an intense wee test.
// We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
// disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
// check that it is not picked up.
$instance = cache_config_testing::instance();
$instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'eventinvalidationtest',
'simplekeys' => true,
'simpledata' => true,
'invalidationevents' => array(
'crazyevent'
)
));
$cache = cache::make('phpunit', 'eventinvalidationtest');
$this->assertTrue($cache->set('testkey1', 'test data 1'));
$this->assertEquals('test data 1', $cache->get('testkey1'));
cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
$this->assertFalse($cache->get('testkey1'));
// OK data added, data invalidated, and invalidation time has been set.