Commit | Line | Data |
---|---|---|
64c2ecd4 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
64c2ecd4 | 5 | | | |
7d61e75f TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
64c2ecd4 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * Class CRM_Core_BAO_CacheTest | |
14 | * @group headless | |
15 | */ | |
16 | class CRM_Core_BAO_CacheTest extends CiviUnitTestCase { | |
17 | ||
fd5f6b03 SL |
18 | /** |
19 | * @var CRM_Utils_Cache_Interface | |
20 | */ | |
21 | protected $a; | |
22 | ||
23 | public function createSimpleCache() { | |
24 | return new CRM_Utils_Cache_FastArrayDecorator( | |
25 | $this->a = CRM_Utils_Cache::create([ | |
26 | 'name' => 'CRM_Core_BAO_CacheTest', | |
27 | 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], | |
28 | ]) | |
29 | ); | |
30 | } | |
31 | ||
3c643fe7 TO |
32 | public function testMultiVersionDecode() { |
33 | $encoders = ['serialize', ['CRM_Core_BAO_Cache', 'encode']]; | |
34 | $values = [NULL, 0, 1, TRUE, FALSE, [], ['abcd'], 'ab;cd', new stdClass()]; | |
35 | foreach ($encoders as $encoder) { | |
36 | foreach ($values as $value) { | |
37 | $encoded = $encoder($value); | |
38 | $decoded = CRM_Core_BAO_Cache::decode($encoded); | |
39 | $this->assertEquals($value, $decoded, "Failure encoding/decoding value " . var_export($value, 1) . ' with ' . var_export($encoder, 1)); | |
40 | } | |
41 | } | |
42 | } | |
43 | ||
addbe802 TO |
44 | public function exampleValues() { |
45 | $binary = ''; | |
46 | for ($i = 0; $i < 256; $i++) { | |
47 | $binary .= chr($i); | |
48 | } | |
49 | ||
50 | $ex = []; | |
51 | ||
9099cab3 | 52 | $ex[] = [['abc' => 'def']]; |
addbe802 TO |
53 | $ex[] = [0]; |
54 | $ex[] = ['hello world']; | |
55 | $ex[] = ['Scarabée']; | |
56 | $ex[] = ['Iñtërnâtiônàlizætiøn']; | |
57 | $ex[] = ['これは日本語のテキストです。読めますか']; | |
58 | $ex[] = ['देखें हिन्दी कैसी नजर आती है। अरे वाह ये तो नजर आती है।']; | |
59 | $ex[] = [$binary]; | |
60 | ||
61 | return $ex; | |
62 | } | |
63 | ||
64 | /** | |
65 | * @param $originalValue | |
66 | * @dataProvider exampleValues | |
67 | */ | |
68 | public function testSetGetItem($originalValue) { | |
fd5f6b03 SL |
69 | $this->createSimpleCache(); |
70 | $this->a->set('testSetGetItem', $originalValue); | |
64c2ecd4 | 71 | |
fd5f6b03 | 72 | $return_1 = $this->a->get('testSetGetItem'); |
64c2ecd4 TO |
73 | $this->assertEquals($originalValue, $return_1); |
74 | ||
75 | // Wipe out any in-memory copies of the cache. Check to see if the SQL | |
76 | // read is correct. | |
77 | ||
78 | CRM_Core_BAO_Cache::$_cache = NULL; | |
79 | CRM_Utils_Cache::$_singleton = NULL; | |
fd5f6b03 SL |
80 | $this->a->values = []; |
81 | $return_2 = $this->a->get('testSetGetItem'); | |
64c2ecd4 TO |
82 | $this->assertEquals($originalValue, $return_2); |
83 | } | |
84 | ||
51cd6dbd TO |
85 | public function getCleanKeyExamples() { |
86 | $es = []; | |
39b959db SL |
87 | // allowed chars |
88 | $es[] = ['hello_world and/other.planets', 'hello_world-20and-2fother.planets']; | |
89 | // escaped chars | |
90 | $es[] = ['hello/world+-#@{}', 'hello-2fworld-2b-2d-23-40-7b-7d']; | |
91 | // short with emoji | |
92 | $es[] = ["LF-\nTAB-\tCR-\remojiskull💀", 'LF-2d-aTAB-2d-9CR-2d-demojiskull-f0-9f-92-80']; | |
93 | // long with emoji | |
94 | $es[] = ["LF-\nTAB-\tCR-\remojibomb💣emojiskull💀", '-5d9324e052f6e10240dce5029c5e8525']; | |
95 | // spaces are escaped | |
96 | $es[] = ['123456789 123456789 123456789 123456789 123456789 123', '123456789-20123456789-20123456789-20123456789-20123456789-20123']; | |
97 | // long but allowed | |
98 | $es[] = ['123456789_123456789_123456789_123456789_123456789_123456789_123', '123456789_123456789_123456789_123456789_123456789_123456789_123']; | |
99 | // too long, md5 fallback | |
100 | $es[] = ['123456789_123456789_123456789_123456789_123456789_123456789_1234', '-e02b981aff954fdcc9a81c25f5ec9681']; | |
101 | // too long, md5 fallback | |
102 | $es[] = ['123456789-/23456789-+23456789--23456789_123456789_123456789', '-43b6dec1026187ae6f6a8fe4d56ab22e']; | |
51cd6dbd TO |
103 | return $es; |
104 | } | |
105 | ||
106 | /** | |
107 | * @param $inputKey | |
108 | * @param $expectKey | |
109 | * @dataProvider getCleanKeyExamples | |
110 | */ | |
111 | public function testCleanKeys($inputKey, $expectKey) { | |
77f080cb | 112 | $actualKey = CRM_Utils_Cache::cleanKey($inputKey); |
51cd6dbd TO |
113 | $this->assertEquals($expectKey, $actualKey); |
114 | } | |
115 | ||
64c2ecd4 | 116 | } |