]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Config/ConfigFileLoaderTest.php
Merge pull request #7785 from nupplaphil/bug/7676-is_file_warning
[friendica.git] / tests / src / Util / Config / ConfigFileLoaderTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util\Config;
4
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;
10
11 class ConfigFileLoaderTest extends MockedTest
12 {
13         use VFSTrait;
14
15         protected function setUp()
16         {
17                 parent::setUp();
18
19                 $this->setUpVfsDir();
20         }
21
22         /**
23          * Test the loadConfigFiles() method with default values
24          */
25         public function testLoadConfigFiles()
26         {
27                 $this->delConfigFile('local.config.php');
28
29                 $configFileLoader = new ConfigFileLoader($this->root->url());
30                 $configCache = new ConfigCache();
31
32                 $configFileLoader->setupCache($configCache);
33
34                 $this->assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
35         }
36
37         /**
38          * Test the loadConfigFiles() method with a wrong local.config.php
39          * @expectedException \Exception
40          * @expectedExceptionMessageRegExp /Error loading config file \w+/
41          */
42         public function testLoadConfigWrong()
43         {
44                 $this->delConfigFile('local.config.php');
45
46                 vfsStream::newFile('local.config.php')
47                         ->at($this->root->getChild('config'))
48                         ->setContent('<?php return true;');
49
50                 $configFileLoader = new ConfigFileLoader($this->root->url());
51                 $configCache = new ConfigCache();
52
53                 $configFileLoader->setupCache($configCache);
54         }
55
56         /**
57          * Test the loadConfigFiles() method with a local.config.php file
58          */
59         public function testLoadConfigFilesLocal()
60         {
61                 $this->delConfigFile('local.config.php');
62
63                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
64                         '..' . DIRECTORY_SEPARATOR .
65                         '..' . DIRECTORY_SEPARATOR .
66                         'datasets' . DIRECTORY_SEPARATOR .
67                         'config' . DIRECTORY_SEPARATOR .
68                         'A.config.php';
69
70                 vfsStream::newFile('local.config.php')
71                         ->at($this->root->getChild('config'))
72                         ->setContent(file_get_contents($file));
73
74                 $configFileLoader = new ConfigFileLoader($this->root->url());
75                 $configCache = new ConfigCache();
76
77                 $configFileLoader->setupCache($configCache);
78
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'));
83
84                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
85                 $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
86         }
87
88         /**
89          * Test the loadConfigFile() method with a local.ini.php file
90          */
91         public function testLoadConfigFilesINI()
92         {
93                 $this->delConfigFile('local.config.php');
94
95                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
96                         '..' . DIRECTORY_SEPARATOR .
97                         '..' . DIRECTORY_SEPARATOR .
98                         'datasets' . DIRECTORY_SEPARATOR .
99                         'config' . DIRECTORY_SEPARATOR .
100                         'A.ini.php';
101
102                 vfsStream::newFile('local.ini.php')
103                         ->at($this->root->getChild('config'))
104                         ->setContent(file_get_contents($file));
105
106                 $configFileLoader = new ConfigFileLoader($this->root->url());
107                 $configCache = new ConfigCache();
108
109                 $configFileLoader->setupCache($configCache);
110
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'));
115
116                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
117         }
118
119         /**
120          * Test the loadConfigFile() method with a .htconfig.php file
121          */
122         public function testLoadConfigFilesHtconfig()
123         {
124                 $this->delConfigFile('local.config.php');
125
126                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
127                         '..' . DIRECTORY_SEPARATOR .
128                         '..' . DIRECTORY_SEPARATOR .
129                         'datasets' . DIRECTORY_SEPARATOR .
130                         'config' . DIRECTORY_SEPARATOR .
131                         '.htconfig.php';
132
133                 vfsStream::newFile('.htconfig.php')
134                         ->at($this->root)
135                         ->setContent(file_get_contents($file));
136
137                 $configFileLoader = new ConfigFileLoader($this->root->url());
138                 $configCache = new ConfigCache();
139
140                 $configFileLoader->setupCache($configCache);
141
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'));
147
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'));
151
152                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
153                 $this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
154
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'));
158
159                 $this->assertEquals('quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
160                 $this->assertEquals('1', $configCache->get('system', 'no_regfullname'));
161         }
162
163         public function testLoadAddonConfig()
164         {
165                 $structure = [
166                         'addon' => [
167                                 'test' => [
168                                         'config' => [],
169                                 ],
170                         ],
171                 ];
172
173                 vfsStream::create($structure, $this->root);
174
175                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
176                         '..' . DIRECTORY_SEPARATOR .
177                         '..' . DIRECTORY_SEPARATOR .
178                         'datasets' . DIRECTORY_SEPARATOR .
179                         'config' . DIRECTORY_SEPARATOR .
180                         'A.config.php';
181
182                 vfsStream::newFile('test.config.php')
183                         ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
184                         ->setContent(file_get_contents($file));
185
186                 $configFileLoader = new ConfigFileLoader($this->root->url());
187
188                 $conf = $configFileLoader->loadAddonConfig('test');
189
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']);
194
195                 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);
196         }
197
198         /**
199          * test loading multiple config files - the last config should work
200          */
201         public function testLoadMultipleConfigs()
202         {
203                 $this->delConfigFile('local.config.php');
204
205                 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
206                         '..' . DIRECTORY_SEPARATOR .
207                         '..' . DIRECTORY_SEPARATOR .
208                         'datasets' . DIRECTORY_SEPARATOR .
209                         'config' . DIRECTORY_SEPARATOR;
210
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'));
217
218                 $configFileLoader = new ConfigFileLoader($this->root->url());
219                 $configCache = new ConfigCache();
220
221                 $configFileLoader->setupCache($configCache);
222
223                 $this->assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
224                 $this->assertEquals('newValue', $configCache->get('system', 'newKey'));
225         }
226
227         /**
228          * test loading multiple config files - the last config should work (INI-version)
229          */
230         public function testLoadMultipleInis()
231         {
232                 $this->delConfigFile('local.config.php');
233
234                 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
235                            '..' . DIRECTORY_SEPARATOR .
236                            '..' . DIRECTORY_SEPARATOR .
237                            'datasets' . DIRECTORY_SEPARATOR .
238                            'config' . DIRECTORY_SEPARATOR;
239
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'));
246
247                 $configFileLoader = new ConfigFileLoader($this->root->url());
248                 $configCache = new ConfigCache();
249
250                 $configFileLoader->setupCache($configCache);
251
252                 $this->assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
253                 $this->assertEquals('newValue', $configCache->get('system', 'newKey'));
254         }
255
256         /**
257          * Test that sample-files (e.g. local-sample.config.php) is never loaded
258          */
259         public function testNotLoadingSamples()
260         {
261                 $this->delConfigFile('local.config.php');
262
263                 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
264                            '..' . DIRECTORY_SEPARATOR .
265                            '..' . DIRECTORY_SEPARATOR .
266                            'datasets' . DIRECTORY_SEPARATOR .
267                            'config' . DIRECTORY_SEPARATOR;
268
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'));
275
276                 $configFileLoader = new ConfigFileLoader($this->root->url());
277                 $configCache = new ConfigCache();
278
279                 $configFileLoader->setupCache($configCache);
280
281                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
282                 $this->assertEmpty($configCache->get('system', 'NewKey'));
283         }
284 }