]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/PConfig/PConfigTest.php
Fix tests
[friendica.git] / tests / src / Core / PConfig / PConfigTest.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\Core\BasePConfig;
26 use Friendica\Model\Config\PConfig as PConfigModel;
27 use Friendica\Test\MockedTest;
28 use Mockery;
29 use Mockery\MockInterface;
30
31 abstract class PConfigTest extends MockedTest
32 {
33         /** @var PConfigModel|MockInterface */
34         protected $configModel;
35
36         /** @var Cache */
37         protected $configCache;
38
39         /** @var BasePConfig */
40         protected $testedConfig;
41
42         /**
43          * Assert a config tree
44          *
45          * @param int    $uid  The uid to assert
46          * @param string $cat  The category to assert
47          * @param array  $data The result data array
48          */
49         protected function assertConfig(int $uid, string $cat, array $data)
50         {
51                 $result = $this->testedConfig->getCache()->getAll();
52
53                 self::assertNotEmpty($result);
54                 self::assertArrayHasKey($uid, $result);
55                 self::assertArrayHasKey($cat, $result[$uid]);
56                 self::assertArraySubset($data, $result[$uid][$cat]);
57         }
58
59
60         protected function setUp(): void
61         {
62                 parent::setUp();
63
64                 // Create the config model
65                 $this->configModel = Mockery::mock(PConfigModel::class);
66                 $this->configCache = new Cache();
67         }
68
69         /**
70          * @return BasePConfig
71          */
72         abstract public function getInstance();
73
74         public function dataTests()
75         {
76                 return [
77                         'string'       => ['uid' => 1, 'data' => 'it'],
78                         'boolTrue'     => ['uid' => 2, 'data' => true],
79                         'boolFalse'    => ['uid' => 3, 'data' => false],
80                         'integer'      => ['uid' => 4, 'data' => 235],
81                         'decimal'      => ['uid' => 5, 'data' => 2.456],
82                         'array'        => ['uid' => 6, 'data' => ['1', 2, '3', true, false]],
83                         'boolIntTrue'  => ['uid' => 7, 'data' => 1],
84                         'boolIntFalse' => ['uid' => 8, 'data' => 0],
85                 ];
86         }
87
88         public function dataConfigLoad()
89         {
90                 $data = [
91                         'system' => [
92                                 'key1' => 'value1',
93                                 'key2' => 'value2',
94                                 'key3' => 'value3',
95                         ],
96                         'config' => [
97                                 'key1' => 'value1a',
98                                 'key4' => 'value4',
99                         ],
100                         'other'  => [
101                                 'key5' => 'value5',
102                                 'key6' => 'value6',
103                         ],
104                 ];
105
106                 return [
107                         'system' => [
108                                 'uid' => 1,
109                                 'data'         => $data,
110                                 'possibleCats' => [
111                                         'system',
112                                         'config',
113                                         'other'
114                                 ],
115                                 'load'         => [
116                                         'system',
117                                 ],
118                         ],
119                         'other'  => [
120                                 'uid' => 2,
121                                 'data'         => $data,
122                                 'possibleCats' => [
123                                         'system',
124                                         'config',
125                                         'other'
126                                 ],
127                                 'load'         => [
128                                         'other',
129                                 ],
130                         ],
131                         'config' => [
132                                 'uid' => 3,
133                                 'data'         => $data,
134                                 'possibleCats' => [
135                                         'system',
136                                         'config',
137                                         'other'
138                                 ],
139                                 'load'         => [
140                                         'config',
141                                 ],
142                         ],
143                         'all'    => [
144                                 'uid' => 4,
145                                 'data'         => $data,
146                                 'possibleCats' => [
147                                         'system',
148                                         'config',
149                                         'other'
150                                 ],
151                                 'load'         => [
152                                         'system',
153                                         'config',
154                                         'other'
155                                 ],
156                         ],
157                 ];
158         }
159
160         /**
161          * Test the configuration initialization
162          */
163         public function testSetUp()
164         {
165                 $this->testedConfig = $this->getInstance();
166                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
167
168                 self::assertEmpty($this->testedConfig->getCache()->getAll());
169         }
170
171         /**
172          * Test the configuration load() method
173          */
174         public function testLoad(int $uid, array $data, array $possibleCats, array $load)
175         {
176                 $this->testedConfig = $this->getInstance();
177                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
178
179                 foreach ($load as $loadedCats) {
180                         $this->testedConfig->load($uid, $loadedCats);
181                 }
182
183                 // Assert at least loaded cats are loaded
184                 foreach ($load as $loadedCats) {
185                         self::assertConfig($uid, $loadedCats, $data[$loadedCats]);
186                 }
187         }
188
189         public function dataDoubleLoad()
190         {
191                 return [
192                         'config' => [
193                                 'uid' => 1,
194                                 'data1'  => [
195                                         'config' => [
196                                                 'key1' => 'value1',
197                                                 'key2' => 'value2',
198                                         ],
199                                 ],
200                                 'data2'  => [
201                                         'config' => [
202                                                 'key1' => 'overwritten!',
203                                                 'key3' => 'value3',
204                                         ],
205                                 ],
206                                 'expect' => [
207                                         'config' => [
208                                                 // load should overwrite values everytime!
209                                                 'key1' => 'overwritten!',
210                                                 'key2' => 'value2',
211                                                 'key3' => 'value3',
212                                         ],
213                                 ],
214                         ],
215                         'other'  => [
216                                 'uid' => 1,
217                                 'data1'  => [
218                                         'config' => [
219                                                 'key12' => 'data4',
220                                                 'key45' => 7,
221                                         ],
222                                         'other'  => [
223                                                 'key1' => 'value1',
224                                                 'key2' => 'value2',
225                                         ],
226                                 ],
227                                 'data2'  => [
228                                         'other'  => [
229                                                 'key1' => 'overwritten!',
230                                                 'key3' => 'value3',
231                                         ],
232                                         'config' => [
233                                                 'key45' => 45,
234                                                 'key52' => true,
235                                         ]
236                                 ],
237                                 'expect' => [
238                                         'other'  => [
239                                                 // load should overwrite values everytime!
240                                                 'key1' => 'overwritten!',
241                                                 'key2' => 'value2',
242                                                 'key3' => 'value3',
243                                         ],
244                                         'config' => [
245                                                 'key12' => 'data4',
246                                                 'key45' => 45,
247                                                 'key52' => true,
248                                         ],
249                                 ],
250                         ],
251                 ];
252         }
253
254         /**
255          * Test the configuration load() method with overwrite
256          */
257         public function testCacheLoadDouble(int $uid, array $data1, array $data2, array $expect)
258         {
259                 $this->testedConfig = $this->getInstance();
260                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
261
262                 foreach ($data1 as $cat => $data) {
263                         $this->testedConfig->load($uid, $cat);
264                 }
265
266                 // Assert at least loaded cats are loaded
267                 foreach ($data1 as $cat => $data) {
268                         self::assertConfig($uid, $cat, $data);
269                 }
270
271                 foreach ($data2 as $cat => $data) {
272                         $this->testedConfig->load($uid, $cat);
273                 }
274         }
275
276         /**
277          * Test the configuration get() and set() methods without adapter
278          *
279          * @dataProvider dataTests
280          */
281         public function testSetGetWithoutDB(int $uid, $data)
282         {
283                 $this->testedConfig = $this->getInstance();
284                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
285
286                 self::assertTrue($this->testedConfig->set($uid, 'test', 'it', $data));
287
288                 self::assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
289                 self::assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
290         }
291
292         /**
293          * Test the configuration get() and set() methods with a model/db
294          *
295          * @dataProvider dataTests
296          */
297         public function testSetGetWithDB(int $uid, $data)
298         {
299                 $this->configModel->shouldReceive('set')
300                                   ->with($uid, 'test', 'it', $data)
301                                   ->andReturn(true)
302                                   ->once();
303
304                 $this->testedConfig = $this->getInstance();
305                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
306
307                 self::assertTrue($this->testedConfig->set($uid, 'test', 'it', $data));
308
309                 self::assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
310                 self::assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
311         }
312
313         /**
314          * Test the configuration get() method with wrong value and no db
315          */
316         public function testGetWrongWithoutDB()
317         {
318                 $this->testedConfig = $this->getInstance();
319                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
320
321                 // without refresh
322                 self::assertNull($this->testedConfig->get(0, 'test', 'it'));
323
324                 /// beware that the cache returns '!<unset>!' and not null for a non existing value
325                 self::assertNull($this->testedConfig->getCache()->get(0, 'test', 'it'));
326
327                 // with default value
328                 self::assertEquals('default', $this->testedConfig->get(0, 'test', 'it', 'default'));
329
330                 // with default value and refresh
331                 self::assertEquals('default', $this->testedConfig->get(0, 'test', 'it', 'default', true));
332         }
333
334         /**
335          * Test the configuration get() method with refresh
336          *
337          * @dataProvider dataTests
338          */
339         public function testGetWithRefresh(int $uid, $data)
340         {
341                 $this->configCache->load($uid, ['test' => ['it' => 'now']]);
342
343                 $this->testedConfig = $this->getInstance();
344                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
345
346                 // without refresh
347                 self::assertEquals('now', $this->testedConfig->get($uid, 'test', 'it'));
348                 self::assertEquals('now', $this->testedConfig->getCache()->get($uid, 'test', 'it'));
349
350                 // with refresh
351                 self::assertEquals($data, $this->testedConfig->get($uid, 'test', 'it', null, true));
352                 self::assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
353
354                 // without refresh and wrong value and default
355                 self::assertEquals('default', $this->testedConfig->get($uid, 'test', 'not', 'default'));
356                 self::assertNull($this->testedConfig->getCache()->get($uid, 'test', 'not'));
357         }
358
359         /**
360          * Test the configuration delete() method without a model/db
361          *
362          * @dataProvider dataTests
363          */
364         public function testDeleteWithoutDB(int $uid, $data)
365         {
366                 $this->configCache->load($uid, ['test' => ['it' => $data]]);
367
368                 $this->testedConfig = $this->getInstance();
369                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
370
371                 self::assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
372                 self::assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
373
374                 self::assertTrue($this->testedConfig->delete($uid, 'test', 'it'));
375                 self::assertNull($this->testedConfig->get($uid, 'test', 'it'));
376                 self::assertNull($this->testedConfig->getCache()->get($uid, 'test', 'it'));
377
378                 self::assertEmpty($this->testedConfig->getCache()->getAll());
379         }
380
381         /**
382          * Test the configuration delete() method with a model/db
383          */
384         public function testDeleteWithDB()
385         {
386                 $uid = 42;
387
388                 $this->configCache->load($uid, ['test' => ['it' => 'now', 'quarter' => 'true']]);
389
390                 $this->configModel->shouldReceive('delete')
391                                   ->with($uid, 'test', 'it')
392                                   ->andReturn(false)
393                                   ->once();
394                 $this->configModel->shouldReceive('delete')
395                                   ->with($uid, 'test', 'second')
396                                   ->andReturn(true)
397                                   ->once();
398                 $this->configModel->shouldReceive('delete')
399                                   ->with($uid, 'test', 'third')
400                                   ->andReturn(false)
401                                   ->once();
402                 $this->configModel->shouldReceive('delete')
403                                   ->with($uid, 'test', 'quarter')
404                                   ->andReturn(true)
405                                   ->once();
406
407                 $this->testedConfig = $this->getInstance();
408                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
409
410                 // directly set the value to the cache
411                 $this->testedConfig->getCache()->set($uid, 'test', 'it', 'now');
412
413                 self::assertEquals('now', $this->testedConfig->get($uid, 'test', 'it'));
414                 self::assertEquals('now', $this->testedConfig->getCache()->get($uid, 'test', 'it'));
415
416                 // delete from cache only
417                 self::assertTrue($this->testedConfig->delete($uid, 'test', 'it'));
418                 // delete from db only
419                 self::assertTrue($this->testedConfig->delete($uid, 'test', 'second'));
420                 // no delete
421                 self::assertFalse($this->testedConfig->delete($uid, 'test', 'third'));
422                 // delete both
423                 self::assertTrue($this->testedConfig->delete($uid, 'test', 'quarter'));
424
425                 self::assertEmpty($this->testedConfig->getCache()->getAll());
426         }
427
428         public function dataMultiUid()
429         {
430                 return [
431                         'normal' => [
432                                 'data1' => [
433                                         'uid'  => 1,
434                                         'data' => [
435                                                 'cat1' => [
436                                                         'key1' => 'value1',
437                                                 ],
438                                                 'cat2' => [
439                                                         'key2' => 'value2',
440                                                 ]
441                                         ],
442                                 ],
443                                 'data2' => [
444                                         'uid' => 2,
445                                         'data' => [
446                                                 'cat1' => [
447                                                         'key1' => 'value1a',
448                                                 ],
449                                                 'cat2' => [
450                                                         'key2' => 'value2',
451                                                 ],
452                                         ],
453                                 ],
454                         ],
455                 ];
456         }
457
458         /**
459          * Test if multiple uids for caching are usable without errors
460          * @dataProvider dataMultiUid
461          */
462         public function testMultipleUidsWithCache(array $data1, array $data2)
463         {
464                 $this->configCache->load($data1['uid'], $data1['data']);
465                 $this->configCache->load($data2['uid'], $data2['data']);
466
467                 $this->testedConfig = $this->getInstance();
468                 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
469
470                 self::assertConfig($data1['uid'], 'cat1', $data1['data']['cat1']);
471                 self::assertConfig($data1['uid'], 'cat2', $data1['data']['cat2']);
472                 self::assertConfig($data2['uid'], 'cat1', $data2['data']['cat1']);
473                 self::assertConfig($data2['uid'], 'cat2', $data2['data']['cat2']);
474         }
475
476         /**
477          * Test when using an invalid UID
478          * @todo check it the clean way before using the config class
479          */
480         public function testInvalidUid()
481         {
482                 // bad UID!
483                 $uid = 0;
484
485                 $this->testedConfig = $this->getInstance();
486
487                 self::assertNull($this->testedConfig->get($uid, 'cat1', 'cat2'));
488                 self::assertEquals('fallback!', $this->testedConfig->get($uid, 'cat1', 'cat2', 'fallback!'));
489
490                 self::assertFalse($this->testedConfig->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
491                 self::assertFalse($this->testedConfig->delete($uid, 'cat1', 'key1'));
492         }
493 }