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