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