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