]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/Cache/PConfigCacheTest.php
wrapping up 2019.12
[friendica.git] / tests / src / Core / Config / Cache / PConfigCacheTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Config\Cache;
4
5 use Friendica\Core\Config\Cache\PConfigCache;
6 use Friendica\Test\MockedTest;
7
8 class PConfigCacheTest extends MockedTest
9 {
10         public function dataTests()
11         {
12                 return [
13                         'normal' => [
14                                 'data' => [
15                                         'system' => [
16                                                 'test'      => 'it',
17                                                 'boolTrue'  => true,
18                                                 'boolFalse' => false,
19                                                 'int'       => 235,
20                                                 'dec'       => 2.456,
21                                                 'array'     => ['1', 2, '3', true, false],
22                                         ],
23                                         'config' => [
24                                                 'a' => 'value',
25                                         ],
26                                 ]
27                         ]
28                 ];
29         }
30
31         private function assertConfigValues($data, PConfigCache $configCache, $uid)
32         {
33                 foreach ($data as $cat => $values) {
34                         foreach ($values as $key => $value) {
35                                 $this->assertEquals($data[$cat][$key], $configCache->get($uid, $cat, $key));
36                         }
37                 }
38         }
39
40         /**
41          * Test the setP() and getP() methods
42          *
43          * @dataProvider dataTests
44          */
45         public function testSetGet($data)
46         {
47                 $configCache = new PConfigCache();
48                 $uid         = 345;
49
50                 foreach ($data as $cat => $values) {
51                         foreach ($values as $key => $value) {
52                                 $configCache->set($uid, $cat, $key, $value);
53                         }
54                 }
55
56                 $this->assertConfigValues($data, $configCache, $uid);
57         }
58
59
60         /**
61          * Test the getP() method with a category
62          */
63         public function testGetCat()
64         {
65                 $configCache = new PConfigCache();
66                 $uid         = 345;
67
68                 $configCache->load($uid, [
69                         'system' => [
70                                 'key1' => 'value1',
71                                 'key2' => 'value2',
72                         ],
73                         'config' => [
74                                 'key3' => 'value3',
75                         ],
76                 ]);
77
78                 $this->assertEquals([
79                         'key1' => 'value1',
80                         'key2' => 'value2',
81                 ], $configCache->get($uid, 'system'));
82
83                 // test explicit cat with null as key
84                 $this->assertEquals([
85                         'key1' => 'value1',
86                         'key2' => 'value2',
87                 ], $configCache->get($uid, 'system', null));
88         }
89
90         /**
91          * Test the deleteP() method
92          *
93          * @dataProvider dataTests
94          */
95         public function testDelete($data)
96         {
97                 $configCache = new PConfigCache();
98                 $uid         = 345;
99
100                 foreach ($data as $cat => $values) {
101                         foreach ($values as $key => $value) {
102                                 $configCache->set($uid, $cat, $key, $value);
103                         }
104                 }
105
106                 foreach ($data as $cat => $values) {
107                         foreach ($values as $key => $value) {
108                                 $configCache->delete($uid, $cat, $key);
109                         }
110                 }
111
112                 $this->assertEmpty($configCache->getAll());
113         }
114
115         /**
116          * Test the keyDiff() method with result
117          *
118          * @dataProvider dataTests
119          */
120         public function testKeyDiffWithResult($data)
121         {
122                 $configCache = new PConfigCache();
123
124                 $diffConfig = [
125                         'fakeCat' => [
126                                 'fakeKey' => 'value',
127                         ]
128                 ];
129
130                 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
131         }
132
133         /**
134          * Test the keyDiff() method without result
135          *
136          * @dataProvider dataTests
137          */
138         public function testKeyDiffWithoutResult($data)
139         {
140                 $configCache = new PConfigCache();
141
142                 $configCache->load(1, $data);
143
144                 $diffConfig = $configCache->getAll();
145
146                 $this->assertEmpty($configCache->keyDiff($diffConfig));
147         }
148
149         /**
150          * Test the default hiding of passwords inside the cache
151          */
152         public function testPasswordHide()
153         {
154                 $configCache = new PConfigCache();
155
156                 $configCache->load(1, [
157                         'database' => [
158                                 'password' => 'supersecure',
159                                 'username' => 'notsecured',
160                         ]
161                 ]);
162
163                 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
164                 $this->assertNotEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
165                 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
166         }
167
168         /**
169          * Test disabling the hiding of passwords inside the cache
170          */
171         public function testPasswordShow()
172         {
173                 $configCache = new PConfigCache(false);
174
175                 $configCache->load(1, [
176                         'database' => [
177                                 'password' => 'supersecure',
178                                 'username' => 'notsecured',
179                         ]
180                 ]);
181
182                 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
183                 $this->assertEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
184                 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
185         }
186
187         /**
188          * Test a empty password
189          */
190         public function testEmptyPassword()
191         {
192                 $configCache = new PConfigCache();
193
194                 $configCache->load(1, [
195                         'database' => [
196                                 'password' => '',
197                                 'username' => '',
198                         ]
199                 ]);
200
201                 $this->assertEmpty($configCache->get(1, 'database', 'password'));
202                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
203         }
204
205         public function testWrongTypePassword()
206         {
207                 $configCache = new PConfigCache();
208
209                 $configCache->load(1, [
210                         'database' => [
211                                 'password' => new \stdClass(),
212                                 'username' => '',
213                         ]
214                 ]);
215
216                 $this->assertNotEmpty($configCache->get(1, 'database', 'password'));
217                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
218
219                 $configCache = new PConfigCache();
220
221                 $configCache->load(1, [
222                         'database' => [
223                                 'password' => 23,
224                                 'username' => '',
225                         ],
226                 ]);
227
228                 $this->assertEquals(23, $configCache->get(1, 'database', 'password'));
229                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
230         }
231
232         /**
233          * Test two different UID configs and make sure that there is no overlapping possible
234          */
235         public function testTwoUid()
236         {
237                 $configCache = new PConfigCache();
238
239                 $configCache->load(1, [
240                         'cat1' => [
241                                 'key1' => 'value1',
242                         ],
243                 ]);
244
245
246                 $configCache->load(2, [
247                         'cat2' => [
248                                 'key2' => 'value2',
249                         ],
250                 ]);
251
252                 $this->assertEquals('value1', $configCache->get(1, 'cat1', 'key1'));
253                 $this->assertEquals('value2', $configCache->get(2, 'cat2', 'key2'));
254
255                 $this->assertNull($configCache->get(1, 'cat2', 'key2'));
256                 $this->assertNull($configCache->get(2, 'cat1', 'key1'));
257         }
258
259         /**
260          * Test when using an invalid UID
261          * @todo check it the clean way before using the config class
262          */
263         public function testInvalidUid()
264         {
265                 // bad UID!
266                 $uid = null;
267
268                 $configCache = new PConfigCache();
269
270                 $this->assertNull($configCache->get($uid, 'cat1', 'cat2'));
271
272                 $this->assertFalse($configCache->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
273                 $this->assertFalse($configCache->delete($uid, 'cat1', 'key1'));
274         }
275 }