3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Class CRM_Core_BAO_CacheTest
32 class CRM_Core_BAO_CacheTest
extends CiviUnitTestCase
{
35 * @var CRM_Utils_Cache_Interface
39 public function createSimpleCache() {
40 return new CRM_Utils_Cache_FastArrayDecorator(
41 $this->a
= CRM_Utils_Cache
::create([
42 'name' => 'CRM_Core_BAO_CacheTest',
43 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'],
48 public function testMultiVersionDecode() {
49 $encoders = ['serialize', ['CRM_Core_BAO_Cache', 'encode']];
50 $values = [NULL, 0, 1, TRUE, FALSE, [], ['abcd'], 'ab;cd', new stdClass()];
51 foreach ($encoders as $encoder) {
52 foreach ($values as $value) {
53 $encoded = $encoder($value);
54 $decoded = CRM_Core_BAO_Cache
::decode($encoded);
55 $this->assertEquals($value, $decoded, "Failure encoding/decoding value " . var_export($value, 1) . ' with ' . var_export($encoder, 1));
60 public function exampleValues() {
62 for ($i = 0; $i < 256; $i++
) {
68 $ex[] = [['abc' => 'def']];
70 $ex[] = ['hello world'];
72 $ex[] = ['Iñtërnâtiônàlizætiøn'];
73 $ex[] = ['これは日本語のテキストです。読めますか'];
74 $ex[] = ['देखें हिन्दी कैसी नजर आती है। अरे वाह ये तो नजर आती है।'];
81 * @param $originalValue
82 * @dataProvider exampleValues
84 public function testSetGetItem($originalValue) {
85 $this->createSimpleCache();
86 $this->a
->set('testSetGetItem', $originalValue);
88 $return_1 = $this->a
->get('testSetGetItem');
89 $this->assertEquals($originalValue, $return_1);
91 // Wipe out any in-memory copies of the cache. Check to see if the SQL
94 CRM_Core_BAO_Cache
::$_cache = NULL;
95 CRM_Utils_Cache
::$_singleton = NULL;
96 $this->a
->values
= [];
97 $return_2 = $this->a
->get('testSetGetItem');
98 $this->assertEquals($originalValue, $return_2);
101 public function getCleanKeyExamples() {
104 $es[] = ['hello_world and/other.planets', 'hello_world-20and-2fother.planets'];
106 $es[] = ['hello/world+-#@{}', 'hello-2fworld-2b-2d-23-40-7b-7d'];
108 $es[] = ["LF-\nTAB-\tCR-\remojiskull💀", 'LF-2d-aTAB-2d-9CR-2d-demojiskull-f0-9f-92-80'];
110 $es[] = ["LF-\nTAB-\tCR-\remojibomb💣emojiskull💀", '-5d9324e052f6e10240dce5029c5e8525'];
111 // spaces are escaped
112 $es[] = ['123456789 123456789 123456789 123456789 123456789 123', '123456789-20123456789-20123456789-20123456789-20123456789-20123'];
114 $es[] = ['123456789_123456789_123456789_123456789_123456789_123456789_123', '123456789_123456789_123456789_123456789_123456789_123456789_123'];
115 // too long, md5 fallback
116 $es[] = ['123456789_123456789_123456789_123456789_123456789_123456789_1234', '-e02b981aff954fdcc9a81c25f5ec9681'];
117 // too long, md5 fallback
118 $es[] = ['123456789-/23456789-+23456789--23456789_123456789_123456789', '-43b6dec1026187ae6f6a8fe4d56ab22e'];
125 * @dataProvider getCleanKeyExamples
127 public function testCleanKeys($inputKey, $expectKey) {
128 $actualKey = CRM_Utils_Cache
::cleanKey($inputKey);
129 $this->assertEquals($expectKey, $actualKey);