]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Config/ConfigFileLoaderTest.php
File test
[friendica.git] / tests / src / Util / Config / ConfigFileLoaderTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util\Config;
23
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;
29
30 class ConfigFileLoaderTest extends MockedTest
31 {
32         use VFSTrait;
33
34         protected function setUp(): void
35         {
36                 parent::setUp();
37
38                 $this->setUpVfsDir();
39         }
40
41         /**
42          * Test the loadConfigFiles() method with default values
43          */
44         public function testLoadConfigFiles()
45         {
46                 $this->delConfigFile('local.config.php');
47
48                 $configFileLoader = new ConfigFileLoader($this->root->url());
49                 $configCache = new Cache();
50
51                 $configFileLoader->setupCache($configCache);
52
53                 self::assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
54         }
55
56         /**
57          * Test the loadConfigFiles() method with a wrong local.config.php
58          *
59          */
60         public function testLoadConfigWrong()
61         {
62                 $this->expectExceptionMessageRegExp("/Error loading config file \w+/");
63                 $this->expectException(\Exception::class);
64                 $this->delConfigFile('local.config.php');
65
66                 vfsStream::newFile('local.config.php')
67                         ->at($this->root->getChild('config'))
68                         ->setContent('<?php return true;');
69
70                 $configFileLoader = new ConfigFileLoader($this->root->url());
71                 $configCache = new Cache();
72
73                 $configFileLoader->setupCache($configCache);
74         }
75
76         /**
77          * Test the loadConfigFiles() method with a local.config.php file
78          */
79         public function testLoadConfigFilesLocal()
80         {
81                 $this->delConfigFile('local.config.php');
82
83                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
84                         '..' . DIRECTORY_SEPARATOR .
85                         '..' . DIRECTORY_SEPARATOR .
86                         'datasets' . DIRECTORY_SEPARATOR .
87                         'config' . DIRECTORY_SEPARATOR .
88                         'A.config.php';
89
90                 vfsStream::newFile('local.config.php')
91                         ->at($this->root->getChild('config'))
92                         ->setContent(file_get_contents($file));
93
94                 $configFileLoader = new ConfigFileLoader($this->root->url());
95                 $configCache = new Cache();
96
97                 $configFileLoader->setupCache($configCache);
98
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'));
103
104                 self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
105                 self::assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
106         }
107
108         /**
109          * Test the loadConfigFile() method with a local.ini.php file
110          */
111         public function testLoadConfigFilesINI()
112         {
113                 $this->delConfigFile('local.config.php');
114
115                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
116                         '..' . DIRECTORY_SEPARATOR .
117                         '..' . DIRECTORY_SEPARATOR .
118                         'datasets' . DIRECTORY_SEPARATOR .
119                         'config' . DIRECTORY_SEPARATOR .
120                         'A.ini.php';
121
122                 vfsStream::newFile('local.ini.php')
123                         ->at($this->root->getChild('config'))
124                         ->setContent(file_get_contents($file));
125
126                 $configFileLoader = new ConfigFileLoader($this->root->url());
127                 $configCache = new Cache();
128
129                 $configFileLoader->setupCache($configCache);
130
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'));
135
136                 self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
137         }
138
139         /**
140          * Test the loadConfigFile() method with a .htconfig.php file
141          */
142         public function testLoadConfigFilesHtconfig()
143         {
144                 $this->delConfigFile('local.config.php');
145
146                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
147                         '..' . DIRECTORY_SEPARATOR .
148                         '..' . DIRECTORY_SEPARATOR .
149                         'datasets' . DIRECTORY_SEPARATOR .
150                         'config' . DIRECTORY_SEPARATOR .
151                         '.htconfig.php';
152
153                 vfsStream::newFile('.htconfig.php')
154                         ->at($this->root)
155                         ->setContent(file_get_contents($file));
156
157                 $configFileLoader = new ConfigFileLoader($this->root->url());
158                 $configCache = new Cache();
159
160                 $configFileLoader->setupCache($configCache);
161
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'));
167
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'));
171
172                 self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
173                 self::assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
174
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'));
178
179                 self::assertEquals('frio,quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
180                 self::assertEquals('1', $configCache->get('system', 'no_regfullname'));
181         }
182
183         public function testLoadAddonConfig()
184         {
185                 $structure = [
186                         'addon' => [
187                                 'test' => [
188                                         'config' => [],
189                                 ],
190                         ],
191                 ];
192
193                 vfsStream::create($structure, $this->root);
194
195                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
196                         '..' . DIRECTORY_SEPARATOR .
197                         '..' . DIRECTORY_SEPARATOR .
198                         'datasets' . DIRECTORY_SEPARATOR .
199                         'config' . DIRECTORY_SEPARATOR .
200                         'A.config.php';
201
202                 vfsStream::newFile('test.config.php')
203                         ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
204                         ->setContent(file_get_contents($file));
205
206                 $configFileLoader = new ConfigFileLoader($this->root->url());
207
208                 $conf = $configFileLoader->loadAddonConfig('test');
209
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']);
214
215                 self::assertEquals('admin@test.it', $conf['config']['admin_email']);
216         }
217
218         /**
219          * test loading multiple config files - the last config should work
220          */
221         public function testLoadMultipleConfigs()
222         {
223                 $this->delConfigFile('local.config.php');
224
225                 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
226                         '..' . DIRECTORY_SEPARATOR .
227                         '..' . DIRECTORY_SEPARATOR .
228                         'datasets' . DIRECTORY_SEPARATOR .
229                         'config' . DIRECTORY_SEPARATOR;
230
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'));
237
238                 $configFileLoader = new ConfigFileLoader($this->root->url());
239                 $configCache = new Cache();
240
241                 $configFileLoader->setupCache($configCache);
242
243                 self::assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
244                 self::assertEquals('newValue', $configCache->get('system', 'newKey'));
245         }
246
247         /**
248          * test loading multiple config files - the last config should work (INI-version)
249          */
250         public function testLoadMultipleInis()
251         {
252                 $this->delConfigFile('local.config.php');
253
254                 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
255                            '..' . DIRECTORY_SEPARATOR .
256                            '..' . DIRECTORY_SEPARATOR .
257                            'datasets' . DIRECTORY_SEPARATOR .
258                            'config' . DIRECTORY_SEPARATOR;
259
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'));
266
267                 $configFileLoader = new ConfigFileLoader($this->root->url());
268                 $configCache = new Cache();
269
270                 $configFileLoader->setupCache($configCache);
271
272                 self::assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email'));
273                 self::assertEquals('newValue', $configCache->get('system', 'newKey'));
274         }
275
276         /**
277          * Test that sample-files (e.g. local-sample.config.php) is never loaded
278          */
279         public function testNotLoadingSamples()
280         {
281                 $this->delConfigFile('local.config.php');
282
283                 $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR .
284                            '..' . DIRECTORY_SEPARATOR .
285                            '..' . DIRECTORY_SEPARATOR .
286                            'datasets' . DIRECTORY_SEPARATOR .
287                            'config' . DIRECTORY_SEPARATOR;
288
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'));
295
296                 $configFileLoader = new ConfigFileLoader($this->root->url());
297                 $configCache = new Cache();
298
299                 $configFileLoader->setupCache($configCache);
300
301                 self::assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
302                 self::assertEmpty($configCache->get('system', 'NewKey'));
303         }
304 }