Merge pull request #14831 from eileenmcnaughton/format
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CacheTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Core_BAO_CacheTest
30 * @group headless
31 */
32 class CRM_Core_BAO_CacheTest extends CiviUnitTestCase {
33
34 /**
35 * @var CRM_Utils_Cache_Interface
36 */
37 protected $a;
38
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'],
44 ])
45 );
46 }
47
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));
56 }
57 }
58 }
59
60 public function exampleValues() {
61 $binary = '';
62 for ($i = 0; $i < 256; $i++) {
63 $binary .= chr($i);
64 }
65
66 $ex = [];
67
68 $ex[] = [array('abc' => 'def')];
69 $ex[] = [0];
70 $ex[] = ['hello world'];
71 $ex[] = ['Scarabée'];
72 $ex[] = ['Iñtërnâtiônàlizætiøn'];
73 $ex[] = ['これは日本語のテキストです。読めますか'];
74 $ex[] = ['देखें हिन्दी कैसी नजर आती है। अरे वाह ये तो नजर आती है।'];
75 $ex[] = [$binary];
76
77 return $ex;
78 }
79
80 /**
81 * @param $originalValue
82 * @dataProvider exampleValues
83 */
84 public function testSetGetItem($originalValue) {
85 $this->createSimpleCache();
86 $this->a->set('testSetGetItem', $originalValue);
87
88 $return_1 = $this->a->get('testSetGetItem');
89 $this->assertEquals($originalValue, $return_1);
90
91 // Wipe out any in-memory copies of the cache. Check to see if the SQL
92 // read is correct.
93
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);
99 }
100
101 public function getCleanKeyExamples() {
102 $es = [];
103 // allowed chars
104 $es[] = ['hello_world and/other.planets', 'hello_world-20and-2fother.planets'];
105 // escaped chars
106 $es[] = ['hello/world+-#@{}', 'hello-2fworld-2b-2d-23-40-7b-7d'];
107 // short with emoji
108 $es[] = ["LF-\nTAB-\tCR-\remojiskull💀", 'LF-2d-aTAB-2d-9CR-2d-demojiskull-f0-9f-92-80'];
109 // long with emoji
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'];
113 // long but allowed
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'];
119 return $es;
120 }
121
122 /**
123 * @param $inputKey
124 * @param $expectKey
125 * @dataProvider getCleanKeyExamples
126 */
127 public function testCleanKeys($inputKey, $expectKey) {
128 $actualKey = CRM_Utils_Cache::cleanKey($inputKey);
129 $this->assertEquals($expectKey, $actualKey);
130 }
131
132 }