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