remove basepath and hostname from admin panel and add update path
[friendica.git/.git] / tests / src / Util / Config / ConfigCacheLoaderTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util\Config;
4
5 use Friendica\App;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Test\MockedTest;
8 use Friendica\Test\Util\VFSTrait;
9 use Friendica\Util\Config\ConfigCacheLoader;
10 use Mockery\MockInterface;
11 use org\bovigo\vfs\vfsStream;
12
13 class ConfigCacheLoaderTest extends MockedTest
14 {
15         use VFSTrait;
16
17         /**
18          * @var App\Mode|MockInterface
19          */
20         private $mode;
21
22         protected function setUp()
23         {
24                 parent::setUp();
25
26                 $this->setUpVfsDir();
27
28                 $this->mode = \Mockery::mock(App\Mode::class);
29                 $this->mode->shouldReceive('isInstall')->andReturn(true);
30         }
31
32         /**
33          * Test the loadConfigFiles() method with default values
34          */
35         public function testLoadConfigFiles()
36         {
37                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
38                 $configCache = new ConfigCache();
39
40                 $configCacheLoader->loadConfigFiles($configCache);
41
42                 $this->assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
43         }
44
45         /**
46          * Test the loadConfigFiles() method with a wrong local.config.php
47          * @expectedException \Exception
48          * @expectedExceptionMessageRegExp /Error loading config file \w+/
49          */
50         public function testLoadConfigWrong()
51         {
52                 $this->delConfigFile('local.config.php');
53
54                 vfsStream::newFile('local.config.php')
55                         ->at($this->root->getChild('config'))
56                         ->setContent('<?php return true;');
57
58                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
59                 $configCache = new ConfigCache();
60
61                 $configCacheLoader->loadConfigFiles($configCache);
62         }
63
64         /**
65          * Test the loadConfigFiles() method with a local.config.php file
66          */
67         public function testLoadConfigFilesLocal()
68         {
69                 $this->delConfigFile('local.config.php');
70
71                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
72                         '..' . DIRECTORY_SEPARATOR .
73                         '..' . DIRECTORY_SEPARATOR .
74                         'datasets' . DIRECTORY_SEPARATOR .
75                         'config' . DIRECTORY_SEPARATOR .
76                         'local.config.php';
77
78                 vfsStream::newFile('local.config.php')
79                         ->at($this->root->getChild('config'))
80                         ->setContent(file_get_contents($file));
81
82                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
83                 $configCache = new ConfigCache();
84
85                 $configCacheLoader->loadConfigFiles($configCache);
86
87                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
88                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
89                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
90                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
91
92                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
93                 $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
94         }
95
96         /**
97          * Test the loadConfigFile() method with a local.ini.php file
98          */
99         public function testLoadConfigFilesINI()
100         {
101                 $this->delConfigFile('local.config.php');
102
103                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
104                         '..' . DIRECTORY_SEPARATOR .
105                         '..' . DIRECTORY_SEPARATOR .
106                         'datasets' . DIRECTORY_SEPARATOR .
107                         'config' . DIRECTORY_SEPARATOR .
108                         'local.ini.php';
109
110                 vfsStream::newFile('local.ini.php')
111                         ->at($this->root->getChild('config'))
112                         ->setContent(file_get_contents($file));
113
114                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
115                 $configCache = new ConfigCache();
116
117                 $configCacheLoader->loadConfigFiles($configCache);
118
119                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
120                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
121                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
122                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
123
124                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
125         }
126
127         /**
128          * Test the loadConfigFile() method with a .htconfig.php file
129          */
130         public function testLoadConfigFilesHtconfig()
131         {
132                 $this->delConfigFile('local.config.php');
133
134                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
135                         '..' . DIRECTORY_SEPARATOR .
136                         '..' . DIRECTORY_SEPARATOR .
137                         'datasets' . DIRECTORY_SEPARATOR .
138                         'config' . DIRECTORY_SEPARATOR .
139                         '.htconfig.test.php';
140
141                 vfsStream::newFile('.htconfig.php')
142                         ->at($this->root)
143                         ->setContent(file_get_contents($file));
144
145                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
146                 $configCache = new ConfigCache();
147
148                 $configCacheLoader->loadConfigFiles($configCache);
149
150                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
151                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
152                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
153                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
154                 $this->assertEquals('anotherCharset', $configCache->get('database', 'charset'));
155
156                 $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
157                 $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
158                 $this->assertEquals('fr', $configCache->get('system', 'language'));
159
160                 $this->assertEquals('admin@friendica.local', $configCache->get('config', 'admin_email'));
161                 $this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
162
163                 $this->assertEquals('/another/php', $configCache->get('config', 'php_path'));
164                 $this->assertEquals('999', $configCache->get('config', 'max_import_size'));
165                 $this->assertEquals('666', $configCache->get('system', 'maximagesize'));
166
167                 $this->assertEquals('quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
168                 $this->assertEquals('1', $configCache->get('system', 'no_regfullname'));
169         }
170
171         public function testLoadAddonConfig()
172         {
173                 $structure = [
174                         'addon' => [
175                                 'test' => [
176                                         'config' => [],
177                                 ],
178                         ],
179                 ];
180
181                 vfsStream::create($structure, $this->root);
182
183                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
184                         '..' . DIRECTORY_SEPARATOR .
185                         '..' . DIRECTORY_SEPARATOR .
186                         'datasets' . DIRECTORY_SEPARATOR .
187                         'config' . DIRECTORY_SEPARATOR .
188                         'local.config.php';
189
190                 vfsStream::newFile('test.config.php')
191                         ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
192                         ->setContent(file_get_contents($file));
193
194                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
195
196                 $conf = $configCacheLoader->loadAddonConfig('test');
197
198                 $this->assertEquals('testhost', $conf['database']['hostname']);
199                 $this->assertEquals('testuser', $conf['database']['username']);
200                 $this->assertEquals('testpw', $conf['database']['password']);
201                 $this->assertEquals('testdb', $conf['database']['database']);
202
203                 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);
204         }
205 }