]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/CacheTest.php
Merge pull request #8269 from MrPetovan/bug/frio-more-actions
[friendica.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);
87                 $configCache->load($override);
88
89                 $this->assertConfigValues($data, $configCache);
90
91                 // override the value
92                 $configCache->load($override, true);
93
94                 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
95                 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
96         }
97
98         /**
99          * Test the loadConfigArray() method with wrong/empty datasets
100          */
101         public function testLoadConfigArrayWrong()
102         {
103                 $configCache = new Cache();
104
105                 // empty dataset
106                 $configCache->load([]);
107                 $this->assertEmpty($configCache->getAll());
108
109                 // wrong dataset
110                 $configCache->load(['system' => 'not_array']);
111                 $this->assertEmpty($configCache->getAll());
112
113                 // incomplete dataset (key is integer ID of the array)
114                 $configCache->load(['system' => ['value']]);
115                 $this->assertEquals('value', $configCache->get('system', 0));
116         }
117
118         /**
119          * Test the getAll() method
120          * @dataProvider dataTests
121          */
122         public function testGetAll($data)
123         {
124                 $configCache = new Cache();
125                 $configCache->load($data);
126
127                 $all = $configCache->getAll();
128
129                 $this->assertContains($data['system'], $all);
130                 $this->assertContains($data['config'], $all);
131         }
132
133         /**
134          * Test the set() and get() method
135          * @dataProvider dataTests
136          */
137         public function testSetGet($data)
138         {
139                 $configCache = new Cache();
140
141                 foreach ($data as $cat => $values) {
142                         foreach ($values as $key => $value) {
143                                 $configCache->set($cat, $key, $value);
144                         }
145                 }
146
147                 $this->assertConfigValues($data, $configCache);
148         }
149
150         /**
151          * Test the get() method without a value
152          */
153         public function testGetEmpty()
154         {
155                 $configCache = new Cache();
156
157                 $this->assertNull($configCache->get('something', 'value'));
158         }
159
160         /**
161          * Test the get() method with a category
162          */
163         public function testGetCat()
164         {
165                 $configCache = new Cache([
166                         'system' => [
167                                 'key1' => 'value1',
168                                 'key2' => 'value2',
169                         ],
170                         'config' => [
171                                 'key3' => 'value3',
172                         ],
173                 ]);
174
175                 $this->assertEquals([
176                         'key1' => 'value1',
177                         'key2' => 'value2',
178                 ], $configCache->get('system'));
179
180                 // explicit null as key
181                 $this->assertEquals([
182                         'key1' => 'value1',
183                         'key2' => 'value2',
184                 ], $configCache->get('system', null));
185         }
186
187         /**
188          * Test the delete() method
189          * @dataProvider dataTests
190          */
191         public function testDelete($data)
192         {
193                 $configCache = new Cache($data);
194
195                 foreach ($data as $cat => $values) {
196                         foreach ($values as $key => $value) {
197                                 $configCache->delete($cat, $key);
198                         }
199                 }
200
201                 $this->assertEmpty($configCache->getAll());
202         }
203
204         /**
205          * Test the keyDiff() method with result
206          * @dataProvider dataTests
207          */
208         public function testKeyDiffWithResult($data)
209         {
210                 $configCache = new Cache($data);
211
212                 $diffConfig = [
213                         'fakeCat' => [
214                                 'fakeKey' => 'value',
215                         ]
216                 ];
217
218                 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
219         }
220
221         /**
222          * Test the keyDiff() method without result
223          * @dataProvider dataTests
224          */
225         public function testKeyDiffWithoutResult($data)
226         {
227                 $configCache = new Cache($data);
228
229                 $diffConfig = $configCache->getAll();
230
231                 $this->assertEmpty($configCache->keyDiff($diffConfig));
232         }
233
234         /**
235          * Test the default hiding of passwords inside the cache
236          */
237         public function testPasswordHide()
238         {
239                 $configCache = new Cache([
240                         'database' => [
241                                 'password' => 'supersecure',
242                                 'username' => 'notsecured',
243                         ],
244                 ]);
245
246                 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
247                 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
248                 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
249         }
250
251         /**
252          * Test disabling the hiding of passwords inside the cache
253          */
254         public function testPasswordShow()
255         {
256                 $configCache = new Cache([
257                         'database' => [
258                                 'password' => 'supersecure',
259                                 'username' => 'notsecured',
260                         ],
261                 ], false);
262
263                 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
264                 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
265                 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
266         }
267
268         /**
269          * Test a empty password
270          */
271         public function testEmptyPassword()
272         {
273                 $configCache = new Cache([
274                         'database' => [
275                                 'password' => '',
276                                 'username' => '',
277                         ]
278                 ]);
279
280                 $this->assertNotEmpty($configCache->get('database', 'password'));
281                 $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
282                 $this->assertEmpty($configCache->get('database', 'username'));
283         }
284
285         public function testWrongTypePassword()
286         {
287                 $configCache = new Cache([
288                         'database' => [
289                                 'password' => new \stdClass(),
290                                 'username' => '',
291                         ]
292                 ]);
293
294                 $this->assertNotEmpty($configCache->get('database', 'password'));
295                 $this->assertEmpty($configCache->get('database', 'username'));
296
297                 $configCache = new Cache([
298                         'database' => [
299                                 'password' => 23,
300                                 'username' => '',
301                         ]
302                 ]);
303
304                 $this->assertEquals(23, $configCache->get('database', 'password'));
305                 $this->assertEmpty($configCache->get('database', 'username'));
306         }
307 }