3 * @copyright Copyright (C) 2010-2021, 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\Util\Config;
24 use Friendica\Core\Config\Cache;
25 use Friendica\Test\MockedTest;
26 use Friendica\Test\Util\VFSTrait;
27 use Friendica\Util\ConfigFileLoader;
28 use org\bovigo\vfs\vfsStream;
30 class ConfigFileLoaderTest extends MockedTest
34 protected function setUp(): void
42 * Test the loadConfigFiles() method with default values
44 public function testLoadConfigFiles()
46 $this->delConfigFile('local.config.php');
48 $configFileLoader = new ConfigFileLoader($this->root->url());
49 $configCache = new Cache();
51 $configFileLoader->setupCache($configCache);
53 self::assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
57 * Test the loadConfigFiles() method with a wrong local.config.php
60 public function testLoadConfigWrong()
62 $this->expectExceptionMessageMatches("/Error loading config file \w+/");
63 $this->expectException(\Exception::class);
64 $this->delConfigFile('local.config.php');
66 vfsStream::newFile('local.config.php')
67 ->at($this->root->getChild('config'))
68 ->setContent('<?php return true;');
70 $configFileLoader = new ConfigFileLoader($this->root->url());
71 $configCache = new Cache();
73 $configFileLoader->setupCache($configCache);
77 * Test the loadConfigFiles() method with a local.config.php file
79 public function testLoadConfigFilesLocal()
81 $this->delConfigFile('local.config.php');
83 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
84 '..' . DIRECTORY_SEPARATOR .
85 '..' . DIRECTORY_SEPARATOR .
86 'datasets' . DIRECTORY_SEPARATOR .
87 'config' . DIRECTORY_SEPARATOR .
90 vfsStream::newFile('local.config.php')
91 ->at($this->root->getChild('config'))
92 ->setContent(file_get_contents($file));
94 $configFileLoader = new ConfigFileLoader($this->root->url());
95 $configCache = new Cache();
97 $configFileLoader->setupCache($configCache);
99 self::assertEquals('testhost', $configCache->get('database', 'hostname'));
100 self::assertEquals('testuser', $configCache->get('database', 'username'));
101 self::assertEquals('testpw', $configCache->get('database', 'password'));
102 self::assertEquals('testdb', $configCache->get('database', 'database'));
104 self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
105 self::assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
109 * Test the loadConfigFile() method with a local.ini.php file
111 public function testLoadConfigFilesINI()
113 $this->delConfigFile('local.config.php');
115 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
116 '..' . DIRECTORY_SEPARATOR .
117 '..' . DIRECTORY_SEPARATOR .
118 'datasets' . DIRECTORY_SEPARATOR .
119 'config' . DIRECTORY_SEPARATOR .
122 vfsStream::newFile('local.ini.php')
123 ->at($this->root->getChild('config'))
124 ->setContent(file_get_contents($file));
126 $configFileLoader = new ConfigFileLoader($this->root->url());
127 $configCache = new Cache();
129 $configFileLoader->setupCache($configCache);
131 self::assertEquals('testhost', $configCache->get('database', 'hostname'));
132 self::assertEquals('testuser', $configCache->get('database', 'username'));
133 self::assertEquals('testpw', $configCache->get('database', 'password'));
134 self::assertEquals('testdb', $configCache->get('database', 'database'));
136 self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
140 * Test the loadConfigFile() method with a .htconfig.php file
142 public function testLoadConfigFilesHtconfig()
144 $this->delConfigFile('local.config.php');
146 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
147 '..' . DIRECTORY_SEPARATOR .
148 '..' . DIRECTORY_SEPARATOR .
149 'datasets' . DIRECTORY_SEPARATOR .
150 'config' . DIRECTORY_SEPARATOR .
153 vfsStream::newFile('.htconfig.php')
155 ->setContent(file_get_contents($file));
157 $configFileLoader = new ConfigFileLoader($this->root->url());
158 $configCache = new Cache();
160 $configFileLoader->setupCache($configCache);
162 self::assertEquals('testhost', $configCache->get('database', 'hostname'));
163 self::assertEquals('testuser', $configCache->get('database', 'username'));
164 self::assertEquals('testpw', $configCache->get('database', 'password'));
165 self::assertEquals('testdb', $configCache->get('database', 'database'));
166 self::assertEquals('anotherCharset', $configCache->get('database', 'charset'));
168 self::assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
169 self::assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
170 self::assertEquals('fr', $configCache->get('system', 'language'));
172 self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
173 self::assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
175 self::assertEquals('/another/php', $configCache->get('config', 'php_path'));
176 self::assertEquals('999', $configCache->get('config', 'max_import_size'));
177 self::assertEquals('666', $configCache->get('system', 'maximagesize'));
179 self::assertEquals('frio,quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
180 self::assertEquals('1', $configCache->get('system', 'no_regfullname'));
183 public function testLoadAddonConfig()
193 vfsStream::create($structure, $this->root);
195 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
196 '..' . DIRECTORY_SEPARATOR .
197 '..' . DIRECTORY_SEPARATOR .
198 'datasets' . DIRECTORY_SEPARATOR .
199 'config' . DIRECTORY_SEPARATOR .
202 vfsStream::newFile('test.config.php')
203 ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
204 ->setContent(file_get_contents($file));
206 $configFileLoader = new ConfigFileLoader($this->root->url());
208 $conf = $configFileLoader->loadAddonConfig('test');
210 self::assertEquals('testhost', $conf['database']['hostname']);
211 self::assertEquals('testuser', $conf['database']['username']);
212 self::assertEquals('testpw', $conf['database']['password']);
213 self::assertEquals('testdb', $conf['database']['database']);
215 self::assertEquals('admin@test.it', $conf['config']['admin_email']);
219 * test loading multiple config files - the last config should work
221 public function testLoadMultipleConfigs()
223 $this->delConfigFile('local.config.php');
225 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
226 '..' . DIRECTORY_SEPARATOR .
227 '..' . DIRECTORY_SEPARATOR .
228 'datasets' . DIRECTORY_SEPARATOR .
229 'config' . DIRECTORY_SEPARATOR;
231 vfsStream::newFile('A.config.php')
232 ->at($this->root->getChild('config'))
233 ->setContent(file_get_contents($fileDir . 'A.config.php'));
234 vfsStream::newFile('B.config.php')
235 ->at($this->root->getChild('config'))
236 ->setContent(file_get_contents($fileDir . 'B.config.php'));
238 $configFileLoader = new ConfigFileLoader($this->root->url());
239 $configCache = new Cache();
241 $configFileLoader->setupCache($configCache);
243 self::assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
244 self::assertEquals('newValue', $configCache->get('system', 'newKey'));
248 * test loading multiple config files - the last config should work (INI-version)
250 public function testLoadMultipleInis()
252 $this->delConfigFile('local.config.php');
254 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
255 '..' . DIRECTORY_SEPARATOR .
256 '..' . DIRECTORY_SEPARATOR .
257 'datasets' . DIRECTORY_SEPARATOR .
258 'config' . DIRECTORY_SEPARATOR;
260 vfsStream::newFile('A.ini.php')
261 ->at($this->root->getChild('config'))
262 ->setContent(file_get_contents($fileDir . 'A.ini.php'));
263 vfsStream::newFile('B.ini.php')
264 ->at($this->root->getChild('config'))
265 ->setContent(file_get_contents($fileDir . 'B.ini.php'));
267 $configFileLoader = new ConfigFileLoader($this->root->url());
268 $configCache = new Cache();
270 $configFileLoader->setupCache($configCache);
272 self::assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
273 self::assertEquals('newValue', $configCache->get('system', 'newKey'));
277 * Test that sample-files (e.g. local-sample.config.php) is never loaded
279 public function testNotLoadingSamples()
281 $this->delConfigFile('local.config.php');
283 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
284 '..' . DIRECTORY_SEPARATOR .
285 '..' . DIRECTORY_SEPARATOR .
286 'datasets' . DIRECTORY_SEPARATOR .
287 'config' . DIRECTORY_SEPARATOR;
289 vfsStream::newFile('A.ini.php')
290 ->at($this->root->getChild('config'))
291 ->setContent(file_get_contents($fileDir . 'A.ini.php'));
292 vfsStream::newFile('B-sample.ini.php')
293 ->at($this->root->getChild('config'))
294 ->setContent(file_get_contents($fileDir . 'B.ini.php'));
296 $configFileLoader = new ConfigFileLoader($this->root->url());
297 $configCache = new Cache();
299 $configFileLoader->setupCache($configCache);
301 self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
302 self::assertEmpty($configCache->get('system', 'NewKey'));