3 * @copyright Copyright (C) 2020, Friendica
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 Friendica\Core\PConfig\Cache;
25 use Friendica\Core\BasePConfig;
26 use Friendica\Model\Config\PConfig as PConfigModel;
27 use Friendica\Test\MockedTest;
29 use Mockery\MockInterface;
31 abstract class PConfigTest extends MockedTest
33 /** @var PConfigModel|MockInterface */
34 protected $configModel;
37 protected $configCache;
39 /** @var BasePConfig */
40 protected $testedConfig;
43 * Assert a config tree
45 * @param int $uid The uid to assert
46 * @param string $cat The category to assert
47 * @param array $data The result data array
49 protected function assertConfig(int $uid, string $cat, array $data)
51 $result = $this->testedConfig->getCache()->getAll();
53 $this->assertNotEmpty($result);
54 $this->assertArrayHasKey($uid, $result);
55 $this->assertArrayHasKey($cat, $result[$uid]);
56 $this->assertArraySubset($data, $result[$uid][$cat]);
60 protected function setUp()
64 // Create the config model
65 $this->configModel = Mockery::mock(PConfigModel::class);
66 $this->configCache = new Cache();
72 public abstract function getInstance();
74 public function dataTests()
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],
88 public function dataConfigLoad()
161 * Test the configuration initialization
162 * @dataProvider dataConfigLoad
164 public function testSetUp(int $uid, array $data)
166 $this->testedConfig = $this->getInstance();
167 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
169 $this->assertEmpty($this->testedConfig->getCache()->getAll());
173 * Test the configuration load() method
175 public function testLoad(int $uid, array $data, array $possibleCats, array $load)
177 $this->testedConfig = $this->getInstance();
178 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
180 foreach ($load as $loadedCats) {
181 $this->testedConfig->load($uid, $loadedCats);
184 // Assert at least loaded cats are loaded
185 foreach ($load as $loadedCats) {
186 $this->assertConfig($uid, $loadedCats, $data[$loadedCats]);
190 public function dataDoubleLoad()
203 'key1' => 'overwritten!',
209 // load should overwrite values everytime!
210 'key1' => 'overwritten!',
230 'key1' => 'overwritten!',
240 // load should overwrite values everytime!
241 'key1' => 'overwritten!',
256 * Test the configuration load() method with overwrite
258 public function testCacheLoadDouble(int $uid, array $data1, array $data2, array $expect)
260 $this->testedConfig = $this->getInstance();
261 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
263 foreach ($data1 as $cat => $data) {
264 $this->testedConfig->load($uid, $cat);
267 // Assert at least loaded cats are loaded
268 foreach ($data1 as $cat => $data) {
269 $this->assertConfig($uid, $cat, $data);
272 foreach ($data2 as $cat => $data) {
273 $this->testedConfig->load($uid, $cat);
278 * Test the configuration get() and set() methods without adapter
280 * @dataProvider dataTests
282 public function testSetGetWithoutDB(int $uid, $data)
284 $this->testedConfig = $this->getInstance();
285 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
287 $this->assertTrue($this->testedConfig->set($uid, 'test', 'it', $data));
289 $this->assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
290 $this->assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
294 * Test the configuration get() and set() methods with a model/db
296 * @dataProvider dataTests
298 public function testSetGetWithDB(int $uid, $data)
300 $this->configModel->shouldReceive('set')
301 ->with($uid, 'test', 'it', $data)
305 $this->testedConfig = $this->getInstance();
306 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
308 $this->assertTrue($this->testedConfig->set($uid, 'test', 'it', $data));
310 $this->assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
311 $this->assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
315 * Test the configuration get() method with wrong value and no db
317 public function testGetWrongWithoutDB()
319 $this->testedConfig = $this->getInstance();
320 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
323 $this->assertNull($this->testedConfig->get(0, 'test', 'it'));
325 /// beware that the cache returns '!<unset>!' and not null for a non existing value
326 $this->assertNull($this->testedConfig->getCache()->get(0, 'test', 'it'));
328 // with default value
329 $this->assertEquals('default', $this->testedConfig->get(0, 'test', 'it', 'default'));
331 // with default value and refresh
332 $this->assertEquals('default', $this->testedConfig->get(0, 'test', 'it', 'default', true));
336 * Test the configuration get() method with refresh
338 * @dataProvider dataTests
340 public function testGetWithRefresh(int $uid, $data)
342 $this->configCache->load($uid, ['test' => ['it' => 'now']]);
344 $this->testedConfig = $this->getInstance();
345 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
348 $this->assertEquals('now', $this->testedConfig->get($uid, 'test', 'it'));
349 $this->assertEquals('now', $this->testedConfig->getCache()->get($uid, 'test', 'it'));
352 $this->assertEquals($data, $this->testedConfig->get($uid, 'test', 'it', null, true));
353 $this->assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
355 // without refresh and wrong value and default
356 $this->assertEquals('default', $this->testedConfig->get($uid, 'test', 'not', 'default'));
357 $this->assertNull($this->testedConfig->getCache()->get($uid, 'test', 'not'));
361 * Test the configuration delete() method without a model/db
363 * @dataProvider dataTests
365 public function testDeleteWithoutDB(int $uid, $data)
367 $this->configCache->load($uid, ['test' => ['it' => $data]]);
369 $this->testedConfig = $this->getInstance();
370 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
372 $this->assertEquals($data, $this->testedConfig->get($uid, 'test', 'it'));
373 $this->assertEquals($data, $this->testedConfig->getCache()->get($uid, 'test', 'it'));
375 $this->assertTrue($this->testedConfig->delete($uid, 'test', 'it'));
376 $this->assertNull($this->testedConfig->get($uid, 'test', 'it'));
377 $this->assertNull($this->testedConfig->getCache()->get($uid, 'test', 'it'));
379 $this->assertEmpty($this->testedConfig->getCache()->getAll());
383 * Test the configuration delete() method with a model/db
385 public function testDeleteWithDB()
389 $this->configCache->load($uid, ['test' => ['it' => 'now', 'quarter' => 'true']]);
391 $this->configModel->shouldReceive('delete')
392 ->with($uid, 'test', 'it')
395 $this->configModel->shouldReceive('delete')
396 ->with($uid, 'test', 'second')
399 $this->configModel->shouldReceive('delete')
400 ->with($uid, 'test', 'third')
403 $this->configModel->shouldReceive('delete')
404 ->with($uid, 'test', 'quarter')
408 $this->testedConfig = $this->getInstance();
409 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
411 // directly set the value to the cache
412 $this->testedConfig->getCache()->set($uid, 'test', 'it', 'now');
414 $this->assertEquals('now', $this->testedConfig->get($uid, 'test', 'it'));
415 $this->assertEquals('now', $this->testedConfig->getCache()->get($uid, 'test', 'it'));
417 // delete from cache only
418 $this->assertTrue($this->testedConfig->delete($uid, 'test', 'it'));
419 // delete from db only
420 $this->assertTrue($this->testedConfig->delete($uid, 'test', 'second'));
422 $this->assertFalse($this->testedConfig->delete($uid, 'test', 'third'));
424 $this->assertTrue($this->testedConfig->delete($uid, 'test', 'quarter'));
426 $this->assertEmpty($this->testedConfig->getCache()->getAll());
429 public function dataMultiUid()
460 * Test if multiple uids for caching are usable without errors
461 * @dataProvider dataMultiUid
463 public function testMultipleUidsWithCache(array $data1, array $data2)
465 $this->configCache->load($data1['uid'], $data1['data']);
466 $this->configCache->load($data2['uid'], $data2['data']);
468 $this->testedConfig = $this->getInstance();
469 $this->assertInstanceOf(Cache::class, $this->testedConfig->getCache());
471 $this->assertConfig($data1['uid'], 'cat1', $data1['data']['cat1']);
472 $this->assertConfig($data1['uid'], 'cat2', $data1['data']['cat2']);
473 $this->assertConfig($data2['uid'], 'cat1', $data2['data']['cat1']);
474 $this->assertConfig($data2['uid'], 'cat2', $data2['data']['cat2']);
478 * Test when using an invalid UID
479 * @todo check it the clean way before using the config class
481 public function testInvalidUid()
486 $this->testedConfig = $this->getInstance();
488 $this->assertNull($this->testedConfig->get($uid, 'cat1', 'cat2'));
489 $this->assertEquals('fallback!', $this->testedConfig->get($uid, 'cat1', 'cat2', 'fallback!'));
491 $this->assertFalse($this->testedConfig->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
492 $this->assertFalse($this->testedConfig->delete($uid, 'cat1', 'key1'));