]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/PConfig/CacheTest.php
Add license info at Friendica classes
[friendica.git] / tests / src / Core / PConfig / CacheTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\PConfig;
23
24 use Friendica\Core\PConfig\Cache;
25 use Friendica\Test\MockedTest;
26
27 class CacheTest extends MockedTest
28 {
29         public function dataTests()
30         {
31                 return [
32                         'normal' => [
33                                 'data' => [
34                                         'system' => [
35                                                 'test'      => 'it',
36                                                 'boolTrue'  => true,
37                                                 'boolFalse' => false,
38                                                 'int'       => 235,
39                                                 'dec'       => 2.456,
40                                                 'array'     => ['1', 2, '3', true, false],
41                                         ],
42                                         'config' => [
43                                                 'a' => 'value',
44                                         ],
45                                 ]
46                         ]
47                 ];
48         }
49
50         private function assertConfigValues($data, Cache $configCache, $uid)
51         {
52                 foreach ($data as $cat => $values) {
53                         foreach ($values as $key => $value) {
54                                 $this->assertEquals($data[$cat][$key], $configCache->get($uid, $cat, $key));
55                         }
56                 }
57         }
58
59         /**
60          * Test the setP() and getP() methods
61          *
62          * @dataProvider dataTests
63          */
64         public function testSetGet($data)
65         {
66                 $configCache = new Cache();
67                 $uid         = 345;
68
69                 foreach ($data as $cat => $values) {
70                         foreach ($values as $key => $value) {
71                                 $configCache->set($uid, $cat, $key, $value);
72                         }
73                 }
74
75                 $this->assertConfigValues($data, $configCache, $uid);
76         }
77
78
79         /**
80          * Test the getP() method with a category
81          */
82         public function testGetCat()
83         {
84                 $configCache = new Cache();
85                 $uid         = 345;
86
87                 $configCache->load($uid, [
88                         'system' => [
89                                 'key1' => 'value1',
90                                 'key2' => 'value2',
91                         ],
92                         'config' => [
93                                 'key3' => 'value3',
94                         ],
95                 ]);
96
97                 $this->assertEquals([
98                         'key1' => 'value1',
99                         'key2' => 'value2',
100                 ], $configCache->get($uid, 'system'));
101
102                 // test explicit cat with null as key
103                 $this->assertEquals([
104                         'key1' => 'value1',
105                         'key2' => 'value2',
106                 ], $configCache->get($uid, 'system', null));
107         }
108
109         /**
110          * Test the deleteP() method
111          *
112          * @dataProvider dataTests
113          */
114         public function testDelete($data)
115         {
116                 $configCache = new Cache();
117                 $uid         = 345;
118
119                 foreach ($data as $cat => $values) {
120                         foreach ($values as $key => $value) {
121                                 $configCache->set($uid, $cat, $key, $value);
122                         }
123                 }
124
125                 foreach ($data as $cat => $values) {
126                         foreach ($values as $key => $value) {
127                                 $configCache->delete($uid, $cat, $key);
128                         }
129                 }
130
131                 $this->assertEmpty($configCache->getAll());
132         }
133
134         /**
135          * Test the keyDiff() method with result
136          *
137          * @dataProvider dataTests
138          */
139         public function testKeyDiffWithResult($data)
140         {
141                 $configCache = new Cache();
142
143                 $diffConfig = [
144                         'fakeCat' => [
145                                 'fakeKey' => 'value',
146                         ]
147                 ];
148
149                 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
150         }
151
152         /**
153          * Test the keyDiff() method without result
154          *
155          * @dataProvider dataTests
156          */
157         public function testKeyDiffWithoutResult($data)
158         {
159                 $configCache = new Cache();
160
161                 $configCache->load(1, $data);
162
163                 $diffConfig = $configCache->getAll();
164
165                 $this->assertEmpty($configCache->keyDiff($diffConfig));
166         }
167
168         /**
169          * Test the default hiding of passwords inside the cache
170          */
171         public function testPasswordHide()
172         {
173                 $configCache = new Cache();
174
175                 $configCache->load(1, [
176                         'database' => [
177                                 'password' => 'supersecure',
178                                 'username' => 'notsecured',
179                         ]
180                 ]);
181
182                 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
183                 $this->assertNotEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
184                 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
185         }
186
187         /**
188          * Test disabling the hiding of passwords inside the cache
189          */
190         public function testPasswordShow()
191         {
192                 $configCache = new Cache(false);
193
194                 $configCache->load(1, [
195                         'database' => [
196                                 'password' => 'supersecure',
197                                 'username' => 'notsecured',
198                         ]
199                 ]);
200
201                 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
202                 $this->assertEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
203                 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
204         }
205
206         /**
207          * Test a empty password
208          */
209         public function testEmptyPassword()
210         {
211                 $configCache = new Cache();
212
213                 $configCache->load(1, [
214                         'database' => [
215                                 'password' => '',
216                                 'username' => '',
217                         ]
218                 ]);
219
220                 $this->assertEmpty($configCache->get(1, 'database', 'password'));
221                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
222         }
223
224         public function testWrongTypePassword()
225         {
226                 $configCache = new Cache();
227
228                 $configCache->load(1, [
229                         'database' => [
230                                 'password' => new \stdClass(),
231                                 'username' => '',
232                         ]
233                 ]);
234
235                 $this->assertNotEmpty($configCache->get(1, 'database', 'password'));
236                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
237
238                 $configCache = new Cache();
239
240                 $configCache->load(1, [
241                         'database' => [
242                                 'password' => 23,
243                                 'username' => '',
244                         ],
245                 ]);
246
247                 $this->assertEquals(23, $configCache->get(1, 'database', 'password'));
248                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
249         }
250
251         /**
252          * Test two different UID configs and make sure that there is no overlapping possible
253          */
254         public function testTwoUid()
255         {
256                 $configCache = new Cache();
257
258                 $configCache->load(1, [
259                         'cat1' => [
260                                 'key1' => 'value1',
261                         ],
262                 ]);
263
264
265                 $configCache->load(2, [
266                         'cat2' => [
267                                 'key2' => 'value2',
268                         ],
269                 ]);
270
271                 $this->assertEquals('value1', $configCache->get(1, 'cat1', 'key1'));
272                 $this->assertEquals('value2', $configCache->get(2, 'cat2', 'key2'));
273
274                 $this->assertNull($configCache->get(1, 'cat2', 'key2'));
275                 $this->assertNull($configCache->get(2, 'cat1', 'key1'));
276         }
277
278         /**
279          * Test when using an invalid UID
280          * @todo check it the clean way before using the config class
281          */
282         public function testInvalidUid()
283         {
284                 // bad UID!
285                 $uid = null;
286
287                 $configCache = new Cache();
288
289                 $this->assertNull($configCache->get($uid, 'cat1', 'cat2'));
290
291                 $this->assertFalse($configCache->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
292                 $this->assertFalse($configCache->delete($uid, 'cat1', 'key1'));
293         }
294 }