friendica/tests/src/Core/Config/Cache/ConfigFileManagerTest.php

414 lines
13 KiB
PHP
Raw Normal View History

2019-02-10 13:09:38 +01:00
<?php
2020-02-09 15:45:36 +01:00
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2019-02-10 13:09:38 +01:00
namespace Friendica\Test\src\Core\Config\Cache;
2019-02-10 13:09:38 +01:00
2021-10-26 21:44:29 +02:00
use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Config\ValueObject\Cache;
2019-02-10 13:09:38 +01:00
use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait;
2023-01-01 21:10:41 +01:00
use Friendica\Core\Config\Util\ConfigFileManager;
2019-02-10 13:09:38 +01:00
use org\bovigo\vfs\vfsStream;
2023-01-03 15:36:36 +01:00
class ConfigFileManagerTest extends MockedTest
2019-02-10 13:09:38 +01:00
{
use VFSTrait;
protected function setUp(): void
2019-02-10 13:09:38 +01:00
{
parent::setUp();
$this->setUpVfsDir();
2019-03-14 02:40:16 +01:00
}
/**
* Test the loadConfigFiles() method with default values
*/
public function testLoadConfigFiles()
{
$this->delConfigFile('local.config.php');
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new Cache();
2019-03-14 02:40:16 +01:00
2019-03-24 12:54:26 +01:00
$configFileLoader->setupCache($configCache);
2019-03-14 02:40:16 +01:00
self::assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
2019-02-10 13:09:38 +01:00
}
/**
* Test the loadConfigFiles() method with a wrong local.config.php
*
2019-02-10 13:09:38 +01:00
*/
public function testLoadConfigWrong()
{
$this->expectExceptionMessageMatches("/Error loading config file \w+/");
$this->expectException(\Exception::class);
2019-02-10 13:09:38 +01:00
$this->delConfigFile('local.config.php');
vfsStream::newFile('local.config.php')
->at($this->root->getChild('config'))
->setContent('<?php return true;');
2019-02-10 13:09:38 +01:00
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new Cache();
2019-02-10 13:09:38 +01:00
2019-03-24 12:54:26 +01:00
$configFileLoader->setupCache($configCache);
2019-02-10 13:09:38 +01:00
}
/**
* Test the loadConfigFiles() method with a local.config.php file
*/
public function testLoadConfigFilesLocal()
{
$this->delConfigFile('local.config.php');
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR .
'A.config.php';
2019-02-10 13:09:38 +01:00
vfsStream::newFile('local.config.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($file));
2019-02-10 13:09:38 +01:00
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new Cache();
2019-02-10 13:09:38 +01:00
2019-03-24 12:54:26 +01:00
$configFileLoader->setupCache($configCache);
2019-02-10 13:09:38 +01:00
self::assertEquals('testhost', $configCache->get('database', 'hostname'));
self::assertEquals('testuser', $configCache->get('database', 'username'));
self::assertEquals('testpw', $configCache->get('database', 'password'));
self::assertEquals('testdb', $configCache->get('database', 'database'));
2019-02-10 13:09:38 +01:00
self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
self::assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
2019-02-10 13:09:38 +01:00
}
/**
* Test the loadConfigFile() method with a local.ini.php file
*/
public function testLoadConfigFilesINI()
{
$this->delConfigFile('local.config.php');
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR .
'A.ini.php';
2019-02-10 13:09:38 +01:00
vfsStream::newFile('local.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($file));
2019-02-10 13:09:38 +01:00
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new Cache();
2019-02-10 13:09:38 +01:00
2019-03-24 12:54:26 +01:00
$configFileLoader->setupCache($configCache);
2019-02-10 13:09:38 +01:00
self::assertEquals('testhost', $configCache->get('database', 'hostname'));
self::assertEquals('testuser', $configCache->get('database', 'username'));
self::assertEquals('testpw', $configCache->get('database', 'password'));
self::assertEquals('testdb', $configCache->get('database', 'database'));
2019-02-10 13:09:38 +01:00
self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
2019-02-10 13:09:38 +01:00
}
/**
* Test the loadConfigFile() method with a .htconfig.php file
*/
public function testLoadConfigFilesHtconfig()
{
$this->delConfigFile('local.config.php');
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR .
'.htconfig.php';
2019-02-10 13:09:38 +01:00
vfsStream::newFile('.htconfig.php')
->at($this->root)
->setContent(file_get_contents($file));
2019-02-10 13:09:38 +01:00
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new Cache();
2019-02-10 13:09:38 +01:00
2019-03-24 12:54:26 +01:00
$configFileLoader->setupCache($configCache);
2019-02-10 13:09:38 +01:00
self::assertEquals('testhost', $configCache->get('database', 'hostname'));
self::assertEquals('testuser', $configCache->get('database', 'username'));
self::assertEquals('testpw', $configCache->get('database', 'password'));
self::assertEquals('testdb', $configCache->get('database', 'database'));
self::assertEquals('anotherCharset', $configCache->get('database', 'charset'));
2019-02-10 13:09:38 +01:00
self::assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
self::assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
self::assertEquals('fr', $configCache->get('system', 'language'));
self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
self::assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
self::assertEquals('/another/php', $configCache->get('config', 'php_path'));
self::assertEquals('999', $configCache->get('config', 'max_import_size'));
self::assertEquals('666', $configCache->get('system', 'maximagesize'));
2019-03-16 20:31:10 +01:00
self::assertEquals('frio,vier', $configCache->get('system', 'allowed_themes'));
self::assertEquals('1', $configCache->get('system', 'no_regfullname'));
2019-02-10 13:09:38 +01:00
}
public function testLoadAddonConfig()
{
$structure = [
'addon' => [
'test' => [
'config' => [],
],
],
];
vfsStream::create($structure, $this->root);
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR .
'A.config.php';
2019-02-10 13:09:38 +01:00
vfsStream::newFile('test.config.php')
->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
->setContent(file_get_contents($file));
2019-02-10 13:09:38 +01:00
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
2019-02-10 13:09:38 +01:00
2019-03-24 12:54:26 +01:00
$conf = $configFileLoader->loadAddonConfig('test');
2019-02-10 13:09:38 +01:00
self::assertEquals('testhost', $conf['database']['hostname']);
self::assertEquals('testuser', $conf['database']['username']);
self::assertEquals('testpw', $conf['database']['password']);
self::assertEquals('testdb', $conf['database']['database']);
2019-02-10 13:09:38 +01:00
self::assertEquals('admin@test.it', $conf['config']['admin_email']);
2019-02-10 13:09:38 +01:00
}
/**
* test loading multiple config files - the last config should work
*/
public function testLoadMultipleConfigs()
{
$this->delConfigFile('local.config.php');
$fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR;
vfsStream::newFile('A.config.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'A.config.php'));
vfsStream::newFile('B.config.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.config.php'));
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
self::assertEquals('newValue', $configCache->get('system', 'newKey'));
}
/**
* test loading multiple config files - the last config should work (INI-version)
*/
public function testLoadMultipleInis()
{
$this->delConfigFile('local.config.php');
$fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR;
vfsStream::newFile('A.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'A.ini.php'));
vfsStream::newFile('B.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.ini.php'));
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
self::assertEquals('newValue', $configCache->get('system', 'newKey'));
}
/**
* Test that sample-files (e.g. local-sample.config.php) is never loaded
*/
public function testNotLoadingSamples()
{
$this->delConfigFile('local.config.php');
$fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR;
vfsStream::newFile('A.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'A.ini.php'));
vfsStream::newFile('B-sample.ini.php')
->at($this->root->getChild('config'))
->setContent(file_get_contents($fileDir . 'B.ini.php'));
2023-01-01 21:10:41 +01:00
$configFileLoader = new ConfigFileManager(
$this->root->url(),
2021-10-26 21:44:29 +02:00
$this->root->url() . DIRECTORY_SEPARATOR . Config::CONFIG_DIR,
$this->root->url() . DIRECTORY_SEPARATOR . Config::STATIC_DIR
);
$configCache = new Cache();
$configFileLoader->setupCache($configCache);
self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
self::assertEmpty($configCache->get('system', 'NewKey'));
}
/**
* Test that using a wrong configuration directory leads to the "normal" config path
*/
public function testWrongEnvDir()
{
$this->delConfigFile('local.config.php');
$configFileManager = (new Config())->createConfigFileManager($this->root->url(), ['FRIENDICA_CONFIG_DIR' => '/a/wrong/dir/']);
$configCache = new Cache();
$configFileManager->setupCache($configCache);
self::assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
}
/**
* Test that a different location of the configuration directory produces the expected output
*/
public function testRightEnvDir()
{
$this->delConfigFile('local.config.php');
$fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'..' . DIRECTORY_SEPARATOR .
'datasets' . DIRECTORY_SEPARATOR .
'config' . DIRECTORY_SEPARATOR;
vfsStream::newFile('B.config.php')
->at($this->root->getChild('config2'))
->setContent(file_get_contents($fileDir . 'B.config.php'));
$configFileManager = (new Config())->createConfigFileManager($this->root->url(),
[
'FRIENDICA_CONFIG_DIR' => $this->root->getChild('config2')->url(),
]);
$configCache = new Cache();
$configFileManager->setupCache($configCache);
self::assertEquals('newValue', $configCache->get('system', 'newKey'));
}
2023-01-01 21:10:41 +01:00
2023-01-08 02:04:25 +01:00
/**
* Test for empty node.config.php
*/
public function testEmptyFile()
{
$this->delConfigFile('node.config.php');
vfsStream::newFile('node.config.php')
->at($this->root->getChild('config'))
->setContent('');
$configFileManager = (new Config())->createConfigFileManager($this->root->url());
$configCache = new Cache();
$configFileManager->setupCache($configCache);
self::assertEquals(1,1);
}
2019-02-10 13:09:38 +01:00
}