]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/Cache/ConfigCacheTest.php
Merge pull request #7371 from nupplaphil/task/split_cache
[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
162         /**
163          * Test the delete() method
164          * @dataProvider dataTests
165          */
166         public function testDelete($data)
167         {
168                 $configCache = new ConfigCache($data);
169
170                 foreach ($data as $cat => $values) {
171                         foreach ($values as $key => $value) {
172                                 $configCache->delete($cat, $key);
173                         }
174                 }
175
176                 $this->assertEmpty($configCache->getAll());
177         }
178
179         /**
180          * Test the keyDiff() method with result
181          * @dataProvider dataTests
182          */
183         public function testKeyDiffWithResult($data)
184         {
185                 $configCache = new ConfigCache($data);
186
187                 $diffConfig = [
188                         'fakeCat' => [
189                                 'fakeKey' => 'value',
190                         ]
191                 ];
192
193                 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
194         }
195
196         /**
197          * Test the keyDiff() method without result
198          * @dataProvider dataTests
199          */
200         public function testKeyDiffWithoutResult($data)
201         {
202                 $configCache = new ConfigCache($data);
203
204                 $diffConfig = $configCache->getAll();
205
206                 $this->assertEmpty($configCache->keyDiff($diffConfig));
207         }
208
209         /**
210          * Test the default hiding of passwords inside the cache
211          */
212         public function testPasswordHide()
213         {
214                 $configCache = new ConfigCache([
215                         'database' => [
216                                 'password' => 'supersecure',
217                                 'username' => 'notsecured',
218                         ],
219                 ]);
220
221                 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
222                 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
223                 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
224         }
225
226         /**
227          * Test disabling the hiding of passwords inside the cache
228          */
229         public function testPasswordShow()
230         {
231                 $configCache = new ConfigCache([
232                         'database' => [
233                                 'password' => 'supersecure',
234                                 'username' => 'notsecured',
235                         ],
236                 ], false);
237
238                 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
239                 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
240                 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
241         }
242
243         /**
244          * Test a empty password
245          */
246         public function testEmptyPassword()
247         {
248                 $configCache = new ConfigCache([
249                         'database' => [
250                                 'password' => '',
251                                 'username' => '',
252                         ]
253                 ]);
254
255                 $this->assertNotEmpty($configCache->get('database', 'password'));
256                 $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
257                 $this->assertEmpty($configCache->get('database', 'username'));
258         }
259
260         public function testWrongTypePassword()
261         {
262                 $configCache = new ConfigCache([
263                         'database' => [
264                                 'password' => new \stdClass(),
265                                 'username' => '',
266                         ]
267                 ]);
268
269                 $this->assertNotEmpty($configCache->get('database', 'password'));
270                 $this->assertEmpty($configCache->get('database', 'username'));
271
272                 $configCache = new ConfigCache([
273                         'database' => [
274                                 'password' => 23,
275                                 'username' => '',
276                         ]
277                 ]);
278
279                 $this->assertEquals(23, $configCache->get('database', 'password'));
280                 $this->assertEmpty($configCache->get('database', 'username'));
281         }
282 }