]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Config/Cache/PConfigCacheTest.php
Merge pull request #7371 from nupplaphil/task/split_cache
[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
84         /**
85          * Test the deleteP() method
86          *
87          * @dataProvider dataTests
88          */
89         public function testDelete($data)
90         {
91                 $configCache = new PConfigCache();
92                 $uid         = 345;
93
94                 foreach ($data as $cat => $values) {
95                         foreach ($values as $key => $value) {
96                                 $configCache->set($uid, $cat, $key, $value);
97                         }
98                 }
99
100                 foreach ($data as $cat => $values) {
101                         foreach ($values as $key => $value) {
102                                 $configCache->delete($uid, $cat, $key);
103                         }
104                 }
105
106                 $this->assertEmpty($configCache->getAll());
107         }
108
109         /**
110          * Test the keyDiff() method with result
111          *
112          * @dataProvider dataTests
113          */
114         public function testKeyDiffWithResult($data)
115         {
116                 $configCache = new PConfigCache($data);
117
118                 $diffConfig = [
119                         'fakeCat' => [
120                                 'fakeKey' => 'value',
121                         ]
122                 ];
123
124                 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
125         }
126
127         /**
128          * Test the keyDiff() method without result
129          *
130          * @dataProvider dataTests
131          */
132         public function testKeyDiffWithoutResult($data)
133         {
134                 $configCache = new PConfigCache();
135
136                 $configCache->load(1, $data);
137
138                 $diffConfig = $configCache->getAll();
139
140                 $this->assertEmpty($configCache->keyDiff($diffConfig));
141         }
142
143         /**
144          * Test the default hiding of passwords inside the cache
145          */
146         public function testPasswordHide()
147         {
148                 $configCache = new PConfigCache();
149
150                 $configCache->load(1, [
151                         'database' => [
152                                 'password' => 'supersecure',
153                                 'username' => 'notsecured',
154                         ]
155                 ]);
156
157                 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
158                 $this->assertNotEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
159                 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
160         }
161
162         /**
163          * Test disabling the hiding of passwords inside the cache
164          */
165         public function testPasswordShow()
166         {
167                 $configCache = new PConfigCache(false);
168
169                 $configCache->load(1, [
170                         'database' => [
171                                 'password' => 'supersecure',
172                                 'username' => 'notsecured',
173                         ]
174                 ]);
175
176                 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
177                 $this->assertEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
178                 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
179         }
180
181         /**
182          * Test a empty password
183          */
184         public function testEmptyPassword()
185         {
186                 $configCache = new PConfigCache();
187
188                 $configCache->load(1, [
189                         'database' => [
190                                 'password' => '',
191                                 'username' => '',
192                         ]
193                 ]);
194
195                 $this->assertEmpty($configCache->get(1, 'database', 'password'));
196                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
197         }
198
199         public function testWrongTypePassword()
200         {
201                 $configCache = new PConfigCache();
202
203                 $configCache->load(1, [
204                         'database' => [
205                                 'password' => new \stdClass(),
206                                 'username' => '',
207                         ]
208                 ]);
209
210                 $this->assertNotEmpty($configCache->get(1, 'database', 'password'));
211                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
212
213                 $configCache = new PConfigCache();
214
215                 $configCache->load(1, [
216                         'database' => [
217                                 'password' => 23,
218                                 'username' => '',
219                         ],
220                 ]);
221
222                 $this->assertEquals(23, $configCache->get(1, 'database', 'password'));
223                 $this->assertEmpty($configCache->get(1, 'database', 'username'));
224         }
225
226         /**
227          * Test two different UID configs and make sure that there is no overlapping possible
228          */
229         public function testTwoUid()
230         {
231                 $configCache = new PConfigCache();
232
233                 $configCache->load(1, [
234                         'cat1' => [
235                                 'key1' => 'value1',
236                         ],
237                 ]);
238
239
240                 $configCache->load(2, [
241                         'cat2' => [
242                                 'key2' => 'value2',
243                         ],
244                 ]);
245
246                 $this->assertEquals('value1', $configCache->get(1, 'cat1', 'key1'));
247                 $this->assertEquals('value2', $configCache->get(2, 'cat2', 'key2'));
248
249                 $this->assertNull($configCache->get(1, 'cat2', 'key2'));
250                 $this->assertNull($configCache->get(2, 'cat1', 'key1'));
251         }
252 }