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\Config;
24 use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\Config\Model\DatabaseConfig;
27 use Friendica\Core\Config\Model\ReadOnlyFileConfig;
28 use Friendica\Core\Config\Util\ConfigFileManager;
29 use Friendica\Core\Config\ValueObject\Cache;
30 use Friendica\Test\DatabaseTest;
31 use Friendica\Test\Util\CreateDatabaseTrait;
32 use Friendica\Test\Util\VFSTrait;
33 use org\bovigo\vfs\vfsStream;
35 class ConfigTest extends DatabaseTest
37 use ArraySubsetAsserts;
39 use CreateDatabaseTrait;
42 protected $configCache;
44 /** @var ConfigFileManager */
45 protected $configFileManager;
47 /** @var IManageConfigValues */
48 protected $testedConfig;
51 * Assert a config tree
53 * @param string $cat The category to assert
54 * @param array $data The result data array
56 protected function assertConfig(string $cat, array $data)
58 $result = $this->testedConfig->getCache()->getAll();
60 self::assertNotEmpty($result);
61 self::assertArrayHasKey($cat, $result);
62 self::assertArraySubset($data, $result[$cat]);
66 protected function setUp(): void
72 $this->configCache = new Cache();
73 $this->configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
77 * @return IManageConfigValues
79 public function getInstance()
81 $this->configFileManager->setupCache($this->configCache);
82 return new DatabaseConfig($this->getDbInstance(), $this->configCache);
85 public function dataTests()
88 'string' => ['data' => 'it'],
89 'boolTrue' => ['data' => true],
90 'boolFalse' => ['data' => false],
91 'integer' => ['data' => 235],
92 'decimal' => ['data' => 2.456],
93 'array' => ['data' => ['1', 2, '3', true, false]],
94 'boolIntTrue' => ['data' => 1],
95 'boolIntFalse' => ['Data' => 0],
99 public function dataConfigLoad()
167 public function configToDbArray(array $config): array
171 foreach ($config as $category => $data) {
172 foreach ($data as $key => $value) {
181 return ['config' => $dbarray];
185 * Test the configuration initialization
186 * @dataProvider dataConfigLoad
188 public function testSetUp(array $data)
190 $this->loadDirectFixture($this->configToDbArray($data) , $this->getDbInstance());
192 $this->testedConfig = $this->getInstance();
193 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
195 // assert config is loaded everytime
196 self::assertConfig('config', $data['config']);
200 * Test the configuration reload() method
205 * @dataProvider dataConfigLoad
207 public function testReload(array $data, array $load)
209 $this->loadDirectFixture($this->configToDbArray($data), $this->getDbInstance());
211 $this->testedConfig = $this->getInstance();
212 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
214 $this->testedConfig->reload();
216 // Assert at least loaded cats are loaded
217 foreach ($load as $loadedCats) {
218 self::assertConfig($loadedCats, $data[$loadedCats]);
222 public function dataDoubleLoad()
234 'key1' => 'overwritten!',
240 // load should overwrite values everytime!
241 'key1' => 'overwritten!',
260 'key1' => 'overwritten!',
270 // load should overwrite values everytime!
271 'key1' => 'overwritten!',
286 * Test the configuration load() method with overwrite
288 * @dataProvider dataDoubleLoad
290 public function testCacheLoadDouble(array $data1, array $data2, array $expect = [])
292 $this->loadDirectFixture($this->configToDbArray($data1), $this->getDbInstance());
294 $this->testedConfig = $this->getInstance();
295 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
297 // Assert at least loaded cats are loaded
298 foreach ($data1 as $cat => $data) {
299 self::assertConfig($cat, $data);
302 $this->loadDirectFixture($this->configToDbArray($data2), $this->getDbInstance());
304 $this->testedConfig->reload();
306 foreach ($data2 as $cat => $data) {
307 self::assertConfig($cat, $data);
312 * Test the configuration load without result
314 public function testLoadWrong()
316 $this->testedConfig = new ReadOnlyFileConfig(new Cache());
317 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
319 self::assertEmpty($this->testedConfig->getCache()->getAll());
323 * Test the configuration get() and set() methods
325 * @dataProvider dataTests
327 public function testSetGet($data)
329 $this->testedConfig = $this->getInstance();
330 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
332 self::assertTrue($this->testedConfig->set('test', 'it', $data));
334 self::assertEquals($data, $this->testedConfig->get('test', 'it'));
335 self::assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
339 * Test the configuration get() method with wrong value and no db
341 public function testGetWrongWithoutDB()
343 $this->testedConfig = $this->getInstance();
344 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
347 self::assertNull($this->testedConfig->get('test', 'it'));
349 /// beware that the cache returns '!<unset>!' and not null for a nonexistent value
350 self::assertNull($this->testedConfig->getCache()->get('test', 'it'));
352 // with default value
353 self::assertEquals('default', $this->testedConfig->get('test', 'it', 'default'));
355 // with default value and refresh
356 self::assertEquals('default', $this->testedConfig->get('test', 'it', 'default', true));
360 * Test the configuration delete() method without a model/db
362 * @dataProvider dataTests
364 public function testDelete($data)
366 $this->configCache->load(['test' => ['it' => $data]], Cache::SOURCE_FILE);
368 $this->testedConfig = new DatabaseConfig($this->getDbInstance(), $this->configCache);
369 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
371 self::assertEquals($data, $this->testedConfig->get('test', 'it'));
372 self::assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
374 self::assertTrue($this->testedConfig->delete('test', 'it'));
375 self::assertNull($this->testedConfig->get('test', 'it'));
376 self::assertNull($this->testedConfig->getCache()->get('test', 'it'));
380 * Test the configuration get() and set() method where the db value has a higher prio than the config file
382 public function testSetGetHighPrio()
384 $this->testedConfig = $this->getInstance();
385 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
387 $this->testedConfig->getCache()->set('config', 'test', 'prio', Cache::SOURCE_FILE);
388 self::assertEquals('prio', $this->testedConfig->get('config', 'test'));
390 // now you have to get the new variable entry because of the new set the get refresh succeed as well
391 self::assertTrue($this->testedConfig->set('config', 'test', '123'));
392 self::assertEquals('123', $this->testedConfig->get('config', 'test', '', true));
396 * Test the configuration get() and set() method where the db value has a lower prio than the env
398 public function testSetGetLowPrio()
400 $this->loadDirectFixture(['config' => [['cat' => 'config', 'k' => 'test', 'v' => 'it']]], $this->getDbInstance());
402 $this->testedConfig = $this->getInstance();
403 self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
404 self::assertEquals('it', $this->testedConfig->get('config', 'test'));
406 $this->testedConfig->getCache()->set('config', 'test', 'prio', Cache::SOURCE_ENV);
407 // You can set a config value, but if there's a value with a higher priority (environment), this value will persist when retrieving
408 self::assertTrue($this->testedConfig->set('config', 'test', '123'));
409 self::assertEquals('prio', $this->testedConfig->get('config', 'test', '', true));
413 public function dataTestCat()
416 'test_with_hashmap' => [
418 'test_with_hashmap' => [
420 'last_update' => 1671051565,
424 'last_update' => 1658952852,
429 'register_policy' => 2,
430 'register_text' => '',
431 'sitename' => 'Friendica Social Network23',
432 'hostname' => 'friendica.local',
433 'private_addons' => false,
436 'dbclean_expire_conversation' => 90,
439 'cat' => 'test_with_hashmap',
442 'last_update' => 1671051565,
446 'last_update' => 1658952852,
451 'test_with_keys' => [
453 'test_with_keys' => [
455 'last_update' => 1671051565,
459 'last_update' => 1658952852,
464 'register_policy' => 2,
465 'register_text' => '',
466 'sitename' => 'Friendica Social Network23',
467 'hostname' => 'friendica.local',
468 'private_addons' => false,
471 'dbclean_expire_conversation' => 90,
474 'cat' => 'test_with_keys',
477 'last_update' => 1671051565,
481 'last_update' => 1658952852,
486 'test_with_inner_array' => [
488 'test_with_inner_array' => [
490 'last_update' => 1671051565,
497 'last_update' => 1658952852,
502 'register_policy' => 2,
503 'register_text' => '',
504 'sitename' => 'Friendica Social Network23',
505 'hostname' => 'friendica.local',
506 'private_addons' => false,
509 'dbclean_expire_conversation' => 90,
512 'cat' => 'test_with_inner_array',
515 'last_update' => 1671051565,
522 'last_update' => 1658952852,
531 * @dataProvider dataTestCat
533 public function testGetCategory(array $data, string $category, array $assertion)
535 $this->configCache = new Cache($data);
536 $config = new ReadOnlyFileConfig($this->configCache);
538 self::assertEquals($assertion, $config->get($category));
541 public function dataSerialized(): array
545 'value' => ['test' => ['array']],
546 'assertion' => ['test' => ['array']],
549 'value' => 's:48:"s:40:"s:32:"https://punkrock-underground.com";";";',
550 'assertion' => 'https://punkrock-underground.com',
552 'double-serialized-array' => [
553 'value' => 's:53:"a:1:{s:9:"testArray";a:1:{s:4:"with";s:7:"entries";}}";',
554 'assertion' => ['testArray' => ['with' => 'entries']],
560 * @dataProvider dataSerialized
562 public function testSerializedValues($value, $assertion)
564 $config = $this->getInstance();
566 $config->set('test', 'it', $value);
567 self:self::assertEquals($assertion, $config->get('test', 'it'));