3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * Class CRM_Core_BAO_CacheTest
16 class CRM_Core_BAO_CacheTest
extends CiviUnitTestCase
{
19 * @var CRM_Utils_Cache_Interface
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'],
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));
44 public function exampleValues() {
46 for ($i = 0; $i < 256; $i++
) {
52 $ex[] = [['abc' => 'def']];
54 $ex[] = ['hello world'];
56 $ex[] = ['Iñtërnâtiônàlizætiøn'];
57 $ex[] = ['これは日本語のテキストです。読めますか'];
58 $ex[] = ['देखें हिन्दी कैसी नजर आती है। अरे वाह ये तो नजर आती है।'];
65 * @param $originalValue
66 * @dataProvider exampleValues
68 public function testSetGetItem($originalValue) {
69 $this->createSimpleCache();
70 $this->a
->set('testSetGetItem', $originalValue);
72 $return_1 = $this->a
->get('testSetGetItem');
73 $this->assertEquals($originalValue, $return_1);
75 // Wipe out any in-memory copies of the cache. Check to see if the SQL
78 CRM_Utils_Cache
::$_singleton = NULL;
79 $this->a
->values
= [];
80 $return_2 = $this->a
->get('testSetGetItem');
81 $this->assertEquals($originalValue, $return_2);
84 public function getCleanKeyExamples() {
87 $es[] = ['hello_world and/other.planets', 'hello_world-20and-2fother.planets'];
89 $es[] = ['hello/world+-#@{}', 'hello-2fworld-2b-2d-23-40-7b-7d'];
91 $es[] = ["LF-\nTAB-\tCR-\remojiskull💀", 'LF-2d-aTAB-2d-9CR-2d-demojiskull-f0-9f-92-80'];
93 $es[] = ["LF-\nTAB-\tCR-\remojibomb💣emojiskull💀", '-5d9324e052f6e10240dce5029c5e8525'];
95 $es[] = ['123456789 123456789 123456789 123456789 123456789 123', '123456789-20123456789-20123456789-20123456789-20123456789-20123'];
97 $es[] = ['123456789_123456789_123456789_123456789_123456789_123456789_123', '123456789_123456789_123456789_123456789_123456789_123456789_123'];
98 // too long, md5 fallback
99 $es[] = ['123456789_123456789_123456789_123456789_123456789_123456789_1234', '-e02b981aff954fdcc9a81c25f5ec9681'];
100 // too long, md5 fallback
101 $es[] = ['123456789-/23456789-+23456789--23456789_123456789_123456789', '-43b6dec1026187ae6f6a8fe4d56ab22e'];
108 * @dataProvider getCleanKeyExamples
110 public function testCleanKeys($inputKey, $expectKey) {
111 $actualKey = CRM_Utils_Cache
::cleanKey($inputKey);
112 $this->assertEquals($expectKey, $actualKey);