3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Core\PConfig;
24 use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
25 use Friendica\Core\PConfig\Type\AbstractPConfigValues;
26 use Friendica\Core\PConfig\Repository\PConfig as PConfigModel;
27 use Friendica\Core\PConfig\ValueObject\Cache;
28 use Friendica\Test\MockedTest;
30 use Mockery\MockInterface;
32 abstract class PConfigTest extends MockedTest
34 use ArraySubsetAsserts;
36 /** @var PConfigModel|MockInterface */
37 protected $configModel;
40 protected $configCache;
42 /** @var AbstractPConfigValues */
43 protected $testedConfig;
46 * Assert a config tree
48 * @param int $uid The uid to assert
49 * @param string $cat The category to assert
50 * @param array $data The result data array
52 protected function assertConfig(int $uid, string $cat, array $data)
54 $result = $this->testedConfig->getCache()->getAll();
56 self::assertNotEmpty($result);
57 self::assertArrayHasKey($uid, $result);
58 self::assertArrayHasKey($cat, $result[$uid]);
59 self::assertArraySubset($data, $result[$uid][$cat]);
63 protected function setUp(): void
67 // Create the config model
68 $this->configModel = Mockery::mock(PConfigModel::class);
69 $this->configCache = new Cache();
73 * @return \Friendica\Core\PConfig\Type\AbstractPConfigValues
75 abstract public function getInstance();
77 public function dataTests()
80 'string' => ['uid' => 1, 'data' => 'it'],
81 'boolTrue' => ['uid' => 2, 'data' => true],
82 'boolFalse' => ['uid' => 3, 'data' => false],
83 'integer' => ['uid' => 4, 'data' => 235],
84 'decimal' => ['uid' => 5, 'data' => 2.456],
85 'array' => ['uid' => 6, 'data' => ['1', 2, '3', true, false]],
86 'boolIntTrue' => ['uid' => 7, 'data' => 1],
87 'boolIntFalse' => ['uid' => 8, 'data' => 0],
91 public function dataConfigLoad()
164 * Test the configuration initialization
166 public function testSetUp()
168 $this->testedConfig = $this->getInstance();
169 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
171 self::assertEmpty($this->testedConfig->getCache()->getAll());
175 * Test the configuration load() method
177 public function testLoad(int $uid, array $data, array $possibleCats, array $load)
179 $this->testedConfig = $this->getInstance();
180 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
182 foreach ($load as $loadedCats) {
183 $this->testedConfig->load($uid, $loadedCats);
186 // Assert at least loaded cats are loaded
187 foreach ($load as $loadedCats) {
188 self::assertConfig($uid, $loadedCats, $data[$loadedCats]);
192 public function dataDoubleLoad()
205 'key1' => 'overwritten!',
211 // load should overwrite values everytime!
212 'key1' => 'overwritten!',
232 'key1' => 'overwritten!',
242 // load should overwrite values everytime!
243 'key1' => 'overwritten!',
258 * Test the configuration load() method with overwrite
260 public function testCacheLoadDouble(int $uid, array $data1, array $data2, array $expect)
262 $this->testedConfig = $this->getInstance();
263 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
265 foreach ($data1 as $cat => $data) {
266 $this->testedConfig->load($uid, $cat);
269 // Assert at least loaded cats are loaded
270 foreach ($data1 as $cat => $data) {
271 self::assertConfig($uid, $cat, $data);
274 foreach ($data2 as $cat => $data) {
275 $this->testedConfig->load($uid, $cat);
280 * Test the configuration get() and set() methods without adapter
282 * @dataProvider dataTests
284 public function testSetGetWithoutDB(int $uid, $data)
286 $this->testedConfig = $this->getInstance();
287 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
289 self::assertTrue($this->testedConfig->set($uid, 'test', 'it', $data));
291 self::assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
292 self::assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
296 * Test the configuration get() and set() methods with a model/db
298 * @dataProvider dataTests
300 public function testSetGetWithDB(int $uid, $data)
302 $this->configModel->shouldReceive('set')
303 ->with($uid, 'test', 'it', $data)
307 $this->testedConfig = $this->getInstance();
308 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
310 self::assertTrue($this->testedConfig->set($uid, 'test', 'it', $data));
312 self::assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
313 self::assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
317 * Test the configuration get() method with wrong value and no db
319 public function testGetWrongWithoutDB()
321 $this->testedConfig = $this->getInstance();
322 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
325 self::assertNull($this->testedConfig->get(0, 'test', 'it'));
327 /// beware that the cache returns '!<unset>!' and not null for a non existing value
328 self::assertNull($this->testedConfig->getCache()->get(0, 'test', 'it'));
330 // with default value
331 self::assertEquals('default', $this->testedConfig->get(0, 'test', 'it', 'default'));
333 // with default value and refresh
334 self::assertEquals('default', $this->testedConfig->get(0, 'test', 'it', 'default', true));
338 * Test the configuration get() method with refresh
340 * @dataProvider dataTests
342 public function testGetWithRefresh(int $uid, $data)
344 $this->configCache->load($uid, ['test' => ['it' => 'now']]);
346 $this->testedConfig = $this->getInstance();
347 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
350 self::assertEquals('now', $this->testedConfig->get($uid, 'test', 'it'));
351 self::assertEquals('now', $this->testedConfig->getCache()->get($uid, 'test', 'it'));
354 self::assertEquals($data, $this->testedConfig->get($uid, 'test', 'it', null, true));
355 self::assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
357 // without refresh and wrong value and default
358 self::assertEquals('default', $this->testedConfig->get($uid, 'test', 'not', 'default'));
359 self::assertNull($this->testedConfig->getCache()->get($uid, 'test', 'not'));
363 * Test the configuration delete() method without a model/db
365 * @dataProvider dataTests
367 public function testDeleteWithoutDB(int $uid, $data)
369 $this->configCache->load($uid, ['test' => ['it' => $data]]);
371 $this->testedConfig = $this->getInstance();
372 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
374 self::assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
375 self::assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
377 self::assertTrue($this->testedConfig->delete($uid, 'test', 'it'));
378 self::assertNull($this->testedConfig->get($uid, 'test', 'it'));
379 self::assertNull($this->testedConfig->getCache()->get($uid, 'test', 'it'));
381 self::assertEmpty($this->testedConfig->getCache()->getAll());
385 * Test the configuration delete() method with a model/db
387 public function testDeleteWithDB()
391 $this->configCache->load($uid, ['test' => ['it' => 'now', 'quarter' => 'true']]);
393 $this->configModel->shouldReceive('delete')
394 ->with($uid, 'test', 'it')
397 $this->configModel->shouldReceive('delete')
398 ->with($uid, 'test', 'second')
401 $this->configModel->shouldReceive('delete')
402 ->with($uid, 'test', 'third')
405 $this->configModel->shouldReceive('delete')
406 ->with($uid, 'test', 'quarter')
410 $this->testedConfig = $this->getInstance();
411 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
413 // directly set the value to the cache
414 $this->testedConfig->getCache()->set($uid, 'test', 'it', 'now');
416 self::assertEquals('now', $this->testedConfig->get($uid, 'test', 'it'));
417 self::assertEquals('now', $this->testedConfig->getCache()->get($uid, 'test', 'it'));
419 // delete from cache only
420 self::assertTrue($this->testedConfig->delete($uid, 'test', 'it'));
421 // delete from db only
422 self::assertTrue($this->testedConfig->delete($uid, 'test', 'second'));
424 self::assertFalse($this->testedConfig->delete($uid, 'test', 'third'));
426 self::assertTrue($this->testedConfig->delete($uid, 'test', 'quarter'));
428 self::assertEmpty($this->testedConfig->getCache()->getAll());
431 public function dataMultiUid()
462 * Test if multiple uids for caching are usable without errors
463 * @dataProvider dataMultiUid
465 public function testMultipleUidsWithCache(array $data1, array $data2)
467 $this->configCache->load($data1['uid'], $data1['data']);
468 $this->configCache->load($data2['uid'], $data2['data']);
470 $this->testedConfig = $this->getInstance();
471 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
473 self::assertConfig($data1['uid'], 'cat1', $data1['data']['cat1']);
474 self::assertConfig($data1['uid'], 'cat2', $data1['data']['cat2']);
475 self::assertConfig($data2['uid'], 'cat1', $data2['data']['cat1']);
476 self::assertConfig($data2['uid'], 'cat2', $data2['data']['cat2']);
480 * Test when using an invalid UID
481 * @todo check it the clean way before using the config class
483 public function testInvalidUid()
488 $this->testedConfig = $this->getInstance();
490 self::assertNull($this->testedConfig->get($uid, 'cat1', 'cat2'));
491 self::assertEquals('fallback!', $this->testedConfig->get($uid, 'cat1', 'cat2', 'fallback!'));
493 self::assertFalse($this->testedConfig->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
494 self::assertFalse($this->testedConfig->delete($uid, 'cat1', 'key1'));