]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/Cache/ConfigCacheTest.php
Merge pull request #7358 from nupplaphil/bug/installer_db
[friendica.git] / tests / src / Core / Config / Cache / ConfigCacheTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Config\Cache;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Test\MockedTest;
7 use ParagonIE\HiddenString\HiddenString;
8
9 class ConfigCacheTest extends MockedTest
10 {
11         public function dataTests()
12         {
13                 return [
14                         'normal' => [
15                                 'data' => [
16                                         'system' => [
17                                                 'test' => 'it',
18                                                 'boolTrue' => true,
19                                                 'boolFalse' => false,
20                                                 'int' => 235,
21                                                 'dec' => 2.456,
22                                                 'array' => ['1', 2, '3', true, false],
23                                         ],
24                                         'config' => [
25                                                 'a' => 'value',
26                                         ],
27                                 ]
28                         ]
29                 ];
30         }
31
32         private function assertConfigValues($data, ConfigCache $configCache, $uid = null)
33         {
34                 foreach ($data as $cat => $values) {
35                         foreach ($values as $key => $value) {
36                                 if (isset($uid)) {
37                                         $this->assertEquals($data[$cat][$key], $configCache->getP($uid, $cat, $key));
38                                 } else {
39                                         $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
40                                 }
41                         }
42                 }
43         }
44
45         /**
46          * Test the loadConfigArray() method without override
47          * @dataProvider dataTests
48          */
49         public function testLoadConfigArray($data)
50         {
51                 $configCache = new ConfigCache();
52                 $configCache->load($data);
53
54                 $this->assertConfigValues($data, $configCache);
55         }
56
57         /**
58          * Test the loadConfigArray() method with overrides
59          * @dataProvider dataTests
60          */
61         public function testLoadConfigArrayOverride($data)
62         {
63                 $override = [
64                         'system' => [
65                                 'test' => 'not',
66                                 'boolTrue' => false,
67                         ]
68                 ];
69
70                 $configCache = new ConfigCache();
71                 $configCache->load($data);
72                 $configCache->load($override);
73
74                 $this->assertConfigValues($data, $configCache);
75
76                 // override the value
77                 $configCache->load($override, true);
78
79                 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
80                 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
81         }
82
83         /**
84          * Test the loadConfigArray() method with wrong/empty datasets
85          */
86         public function testLoadConfigArrayWrong()
87         {
88                 $configCache = new ConfigCache();
89
90                 // empty dataset
91                 $configCache->load([]);
92                 $this->assertEmpty($configCache->getAll());
93
94                 // wrong dataset
95                 $configCache->load(['system' => 'not_array']);
96                 $this->assertEmpty($configCache->getAll());
97
98                 // incomplete dataset (key is integer ID of the array)
99                 $configCache->load(['system' => ['value']]);
100                 $this->assertEquals('value', $configCache->get('system', 0));
101         }
102
103         /**
104          * Test the getAll() method
105          * @dataProvider dataTests
106          */
107         public function testGetAll($data)
108         {
109                 $configCache = new ConfigCache();
110                 $configCache->load($data);
111
112                 $all = $configCache->getAll();
113
114                 $this->assertContains($data['system'], $all);
115                 $this->assertContains($data['config'], $all);
116         }
117
118         /**
119          * Test the set() and get() method
120          * @dataProvider dataTests
121          */
122         public function testSetGet($data)
123         {
124                 $configCache = new ConfigCache();
125
126                 foreach ($data as $cat => $values) {
127                         foreach ($values as $key => $value) {
128                                 $configCache->set($cat, $key, $value);
129                         }
130                 }
131
132                 $this->assertConfigValues($data, $configCache);
133         }
134
135         /**
136          * Test the get() method without a value
137          */
138         public function testGetEmpty()
139         {
140                 $configCache = new ConfigCache();
141
142                 $this->assertNull($configCache->get('something', 'value'));
143         }
144
145         /**
146          * Test the get() method with a category
147          */
148         public function testGetCat()
149         {
150                 $configCache = new ConfigCache([
151                         'system' => [
152                                 'key1' => 'value1',
153                                 'key2' => 'value2',
154                         ],
155                         'config' => [
156                                 'key3' => 'value3',
157                         ],
158                 ]);
159
160                 $this->assertEquals([
161                         'key1' => 'value1',
162                         'key2' => 'value2',
163                 ], $configCache->get('system'));
164         }
165
166         /**
167          * Test the delete() method
168          * @dataProvider dataTests
169          */
170         public function testDelete($data)
171         {
172                 $configCache = new ConfigCache($data);
173
174                 foreach ($data as $cat => $values) {
175                         foreach ($values as $key => $value) {
176                                 $configCache->delete($cat, $key);
177                         }
178                 }
179
180                 $this->assertEmpty($configCache->getAll());
181         }
182
183         /**
184          * Test the setP() and getP() methods
185          * @dataProvider dataTests
186          */
187         public function testSetGetP($data)
188         {
189                 $configCache = new ConfigCache();
190                 $uid = 345;
191
192                 foreach ($data as $cat => $values) {
193                         foreach ($values as $key => $value) {
194                                 $configCache->setP($uid, $cat, $key, $value);
195                         }
196                 }
197
198                 $this->assertConfigValues($data, $configCache, $uid);
199         }
200
201
202         /**
203          * Test the getP() method with a category
204          */
205         public function testGetPCat()
206         {
207                 $configCache = new ConfigCache();
208                 $uid = 345;
209
210                 $configCache->loadP($uid, [
211                         'system' => [
212                                 'key1' => 'value1',
213                                 'key2' => 'value2',
214                         ],
215                         'config' => [
216                                 'key3' => 'value3',
217                         ],
218                 ]);
219
220                 $this->assertEquals([
221                         'key1' => 'value1',
222                         'key2' => 'value2',
223                 ], $configCache->get($uid, 'system'));
224         }
225
226         /**
227          * Test the deleteP() method
228          * @dataProvider dataTests
229          */
230         public function testDeleteP($data)
231         {
232                 $configCache = new ConfigCache();
233                 $uid = 345;
234
235                 foreach ($data as $cat => $values) {
236                         foreach ($values as $key => $value) {
237                                 $configCache->setP($uid, $cat, $key, $value);
238                         }
239                 }
240
241                 foreach ($data as $cat => $values) {
242                         foreach ($values as $key => $value) {
243                                 $configCache->deleteP($uid, $cat, $key);
244                         }
245                 }
246
247                 $this->assertEmpty($configCache->getAll());
248         }
249
250         /**
251          * Test the keyDiff() method with result
252          * @dataProvider dataTests
253          */
254         public function testKeyDiffWithResult($data)
255         {
256                 $configCache = new ConfigCache($data);
257
258                 $diffConfig = [
259                         'fakeCat' => [
260                                 'fakeKey' => 'value',
261                         ]
262                 ];
263
264                 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
265         }
266
267         /**
268          * Test the keyDiff() method without result
269          * @dataProvider dataTests
270          */
271         public function testKeyDiffWithoutResult($data)
272         {
273                 $configCache = new ConfigCache($data);
274
275                 $diffConfig = $configCache->getAll();
276
277                 $this->assertEmpty($configCache->keyDiff($diffConfig));
278         }
279
280         /**
281          * Test the default hiding of passwords inside the cache
282          */
283         public function testPasswordHide()
284         {
285                 $configCache = new ConfigCache([
286                         'database' => [
287                                 'password' => 'supersecure',
288                                 'username' => 'notsecured',
289                         ],
290                 ]);
291
292                 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
293                 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
294                 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
295         }
296
297         /**
298          * Test disabling the hiding of passwords inside the cache
299          */
300         public function testPasswordShow()
301         {
302                 $configCache = new ConfigCache([
303                         'database' => [
304                                 'password' => 'supersecure',
305                                 'username' => 'notsecured',
306                         ],
307                 ], false);
308
309                 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
310                 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
311                 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
312         }
313
314         /**
315          * Test a empty password
316          */
317         public function testEmptyPassword()
318         {
319                 $configCache = new ConfigCache([
320                         'database' => [
321                                 'password' => '',
322                                 'username' => '',
323                         ]
324                 ]);
325
326                 $this->assertNotEmpty($configCache->get('database', 'password'));
327                 $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
328                 $this->assertEmpty($configCache->get('database', 'username'));
329         }
330
331         public function testWrongTypePassword()
332         {
333                 $configCache = new ConfigCache([
334                         'database' => [
335                                 'password' => new \stdClass(),
336                                 'username' => '',
337                         ]
338                 ]);
339
340                 $this->assertNotEmpty($configCache->get('database', 'password'));
341                 $this->assertEmpty($configCache->get('database', 'username'));
342
343                 $configCache = new ConfigCache([
344                         'database' => [
345                                 'password' => 23,
346                                 'username' => '',
347                         ]
348                 ]);
349
350                 $this->assertEquals(23, $configCache->get('database', 'password'));
351                 $this->assertEmpty($configCache->get('database', 'username'));
352         }
353 }