3 namespace Friendica\Test\src\Util\Config;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Test\MockedTest;
7 use Friendica\Test\Util\VFSTrait;
8 use Friendica\Util\ConfigFileLoader;
9 use org\bovigo\vfs\vfsStream;
11 class ConfigFileLoaderTest extends MockedTest
15 protected function setUp()
23 * Test the loadConfigFiles() method with default values
25 public function testLoadConfigFiles()
27 $this->delConfigFile('local.config.php');
29 $configFileLoader = new ConfigFileLoader($this->root->url());
30 $configCache = new ConfigCache();
32 $configFileLoader->setupCache($configCache);
34 $this->assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
38 * Test the loadConfigFiles() method with a wrong local.config.php
39 * @expectedException \Exception
40 * @expectedExceptionMessageRegExp /Error loading config file \w+/
42 public function testLoadConfigWrong()
44 $this->delConfigFile('local.config.php');
46 vfsStream::newFile('local.config.php')
47 ->at($this->root->getChild('config'))
48 ->setContent('<?php return true;');
50 $configFileLoader = new ConfigFileLoader($this->root->url());
51 $configCache = new ConfigCache();
53 $configFileLoader->setupCache($configCache);
57 * Test the loadConfigFiles() method with a local.config.php file
59 public function testLoadConfigFilesLocal()
61 $this->delConfigFile('local.config.php');
63 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
64 '..' . DIRECTORY_SEPARATOR .
65 '..' . DIRECTORY_SEPARATOR .
66 'datasets' . DIRECTORY_SEPARATOR .
67 'config' . DIRECTORY_SEPARATOR .
70 vfsStream::newFile('local.config.php')
71 ->at($this->root->getChild('config'))
72 ->setContent(file_get_contents($file));
74 $configFileLoader = new ConfigFileLoader($this->root->url());
75 $configCache = new ConfigCache();
77 $configFileLoader->setupCache($configCache);
79 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
80 $this->assertEquals('testuser', $configCache->get('database', 'username'));
81 $this->assertEquals('testpw', $configCache->get('database', 'password'));
82 $this->assertEquals('testdb', $configCache->get('database', 'database'));
84 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
85 $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
89 * Test the loadConfigFile() method with a local.ini.php file
91 public function testLoadConfigFilesINI()
93 $this->delConfigFile('local.config.php');
95 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
96 '..' . DIRECTORY_SEPARATOR .
97 '..' . DIRECTORY_SEPARATOR .
98 'datasets' . DIRECTORY_SEPARATOR .
99 'config' . DIRECTORY_SEPARATOR .
102 vfsStream::newFile('local.ini.php')
103 ->at($this->root->getChild('config'))
104 ->setContent(file_get_contents($file));
106 $configFileLoader = new ConfigFileLoader($this->root->url());
107 $configCache = new ConfigCache();
109 $configFileLoader->setupCache($configCache);
111 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
112 $this->assertEquals('testuser', $configCache->get('database', 'username'));
113 $this->assertEquals('testpw', $configCache->get('database', 'password'));
114 $this->assertEquals('testdb', $configCache->get('database', 'database'));
116 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
120 * Test the loadConfigFile() method with a .htconfig.php file
122 public function testLoadConfigFilesHtconfig()
124 $this->delConfigFile('local.config.php');
126 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
127 '..' . DIRECTORY_SEPARATOR .
128 '..' . DIRECTORY_SEPARATOR .
129 'datasets' . DIRECTORY_SEPARATOR .
130 'config' . DIRECTORY_SEPARATOR .
133 vfsStream::newFile('.htconfig.php')
135 ->setContent(file_get_contents($file));
137 $configFileLoader = new ConfigFileLoader($this->root->url());
138 $configCache = new ConfigCache();
140 $configFileLoader->setupCache($configCache);
142 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
143 $this->assertEquals('testuser', $configCache->get('database', 'username'));
144 $this->assertEquals('testpw', $configCache->get('database', 'password'));
145 $this->assertEquals('testdb', $configCache->get('database', 'database'));
146 $this->assertEquals('anotherCharset', $configCache->get('database', 'charset'));
148 $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
149 $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
150 $this->assertEquals('fr', $configCache->get('system', 'language'));
152 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
153 $this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
155 $this->assertEquals('/another/php', $configCache->get('config', 'php_path'));
156 $this->assertEquals('999', $configCache->get('config', 'max_import_size'));
157 $this->assertEquals('666', $configCache->get('system', 'maximagesize'));
159 $this->assertEquals('quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
160 $this->assertEquals('1', $configCache->get('system', 'no_regfullname'));
163 public function testLoadAddonConfig()
173 vfsStream::create($structure, $this->root);
175 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
176 '..' . DIRECTORY_SEPARATOR .
177 '..' . DIRECTORY_SEPARATOR .
178 'datasets' . DIRECTORY_SEPARATOR .
179 'config' . DIRECTORY_SEPARATOR .
182 vfsStream::newFile('test.config.php')
183 ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
184 ->setContent(file_get_contents($file));
186 $configFileLoader = new ConfigFileLoader($this->root->url());
188 $conf = $configFileLoader->loadAddonConfig('test');
190 $this->assertEquals('testhost', $conf['database']['hostname']);
191 $this->assertEquals('testuser', $conf['database']['username']);
192 $this->assertEquals('testpw', $conf['database']['password']);
193 $this->assertEquals('testdb', $conf['database']['database']);
195 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);
199 * test loading multiple config files - the last config should work
201 public function testLoadMultipleConfigs()
203 $this->delConfigFile('local.config.php');
205 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
206 '..' . DIRECTORY_SEPARATOR .
207 '..' . DIRECTORY_SEPARATOR .
208 'datasets' . DIRECTORY_SEPARATOR .
209 'config' . DIRECTORY_SEPARATOR;
211 vfsStream::newFile('A.config.php')
212 ->at($this->root->getChild('config'))
213 ->setContent(file_get_contents($fileDir . 'A.config.php'));
214 vfsStream::newFile('B.config.php')
215 ->at($this->root->getChild('config'))
216 ->setContent(file_get_contents($fileDir . 'B.config.php'));
218 $configFileLoader = new ConfigFileLoader($this->root->url());
219 $configCache = new ConfigCache();
221 $configFileLoader->setupCache($configCache);
223 $this->assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
224 $this->assertEquals('newValue', $configCache->get('system', 'newKey'));
228 * test loading multiple config files - the last config should work (INI-version)
230 public function testLoadMultipleInis()
232 $this->delConfigFile('local.config.php');
234 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
235 '..' . DIRECTORY_SEPARATOR .
236 '..' . DIRECTORY_SEPARATOR .
237 'datasets' . DIRECTORY_SEPARATOR .
238 'config' . DIRECTORY_SEPARATOR;
240 vfsStream::newFile('A.ini.php')
241 ->at($this->root->getChild('config'))
242 ->setContent(file_get_contents($fileDir . 'A.ini.php'));
243 vfsStream::newFile('B.ini.php')
244 ->at($this->root->getChild('config'))
245 ->setContent(file_get_contents($fileDir . 'B.ini.php'));
247 $configFileLoader = new ConfigFileLoader($this->root->url());
248 $configCache = new ConfigCache();
250 $configFileLoader->setupCache($configCache);
252 $this->assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
253 $this->assertEquals('newValue', $configCache->get('system', 'newKey'));
257 * Test that sample-files (e.g. local-sample.config.php) is never loaded
259 public function testNotLoadingSamples()
261 $this->delConfigFile('local.config.php');
263 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
264 '..' . DIRECTORY_SEPARATOR .
265 '..' . DIRECTORY_SEPARATOR .
266 'datasets' . DIRECTORY_SEPARATOR .
267 'config' . DIRECTORY_SEPARATOR;
269 vfsStream::newFile('A.ini.php')
270 ->at($this->root->getChild('config'))
271 ->setContent(file_get_contents($fileDir . 'A.ini.php'));
272 vfsStream::newFile('B-sample.ini.php')
273 ->at($this->root->getChild('config'))
274 ->setContent(file_get_contents($fileDir . 'B.ini.php'));
276 $configFileLoader = new ConfigFileLoader($this->root->url());
277 $configCache = new ConfigCache();
279 $configFileLoader->setupCache($configCache);
281 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
282 $this->assertEmpty($configCache->get('system', 'NewKey'));