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