- Adding additional legacy .htconfig information
[friendica.git/.git] / tests / src / Core / Config / Cache / ConfigCacheLoaderTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Config\Cache;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\Cache\ConfigCacheLoader;
7 use Friendica\Test\MockedTest;
8 use Friendica\Test\Util\VFSTrait;
9 use org\bovigo\vfs\vfsStream;
10
11 class ConfigCacheLoaderTest 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 a wrong local.config.php
24          * @expectedException \Exception
25          * @expectedExceptionMessageRegExp /Error loading config file \w+/
26          */
27         public function testLoadConfigWrong()
28         {
29                 $this->delConfigFile('local.config.php');
30
31                 vfsStream::newFile('local.config.php')
32                         ->at($this->root->getChild('config'))
33                         ->setContent('<?php return true;');
34
35                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
36                 $configCache = new ConfigCache();
37
38                 $configCacheLoader->loadConfigFiles($configCache);
39         }
40
41         /**
42          * Test the loadConfigFiles() method with a local.config.php file
43          */
44         public function testLoadConfigFilesLocal()
45         {
46                 $this->delConfigFile('local.config.php');
47
48                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
49                         '..' . DIRECTORY_SEPARATOR .
50                         '..' . DIRECTORY_SEPARATOR .
51                         '..' . DIRECTORY_SEPARATOR .
52                         'datasets' . DIRECTORY_SEPARATOR .
53                         'config' . DIRECTORY_SEPARATOR .
54                         'local.config.php';
55
56                 vfsStream::newFile('local.config.php')
57                         ->at($this->root->getChild('config'))
58                         ->setContent(file_get_contents($file));
59
60                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
61                 $configCache = new ConfigCache();
62
63                 $configCacheLoader->loadConfigFiles($configCache);
64
65                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
66                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
67                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
68                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
69
70                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
71                 $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
72         }
73
74         /**
75          * Test the loadConfigFile() method with a local.ini.php file
76          */
77         public function testLoadConfigFilesINI()
78         {
79                 $this->delConfigFile('local.config.php');
80
81                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
82                         '..' . DIRECTORY_SEPARATOR .
83                         '..' . DIRECTORY_SEPARATOR .
84                         '..' . DIRECTORY_SEPARATOR .
85                         'datasets' . DIRECTORY_SEPARATOR .
86                         'config' . DIRECTORY_SEPARATOR .
87                         'local.ini.php';
88
89                 vfsStream::newFile('local.ini.php')
90                         ->at($this->root->getChild('config'))
91                         ->setContent(file_get_contents($file));
92
93                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
94                 $configCache = new ConfigCache();
95
96                 $configCacheLoader->loadConfigFiles($configCache);
97
98                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
99                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
100                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
101                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
102
103                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
104         }
105
106         /**
107          * Test the loadConfigFile() method with a .htconfig.php file
108          */
109         public function testLoadConfigFilesHtconfig()
110         {
111                 $this->delConfigFile('local.config.php');
112
113                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
114                         '..' . DIRECTORY_SEPARATOR .
115                         '..' . DIRECTORY_SEPARATOR .
116                         '..' . DIRECTORY_SEPARATOR .
117                         'datasets' . DIRECTORY_SEPARATOR .
118                         'config' . DIRECTORY_SEPARATOR .
119                         '.htconfig.test.php';
120
121                 vfsStream::newFile('.htconfig.php')
122                         ->at($this->root)
123                         ->setContent(file_get_contents($file));
124
125                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
126                 $configCache = new ConfigCache();
127
128                 $configCacheLoader->loadConfigFiles($configCache);
129
130                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
131                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
132                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
133                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
134
135                 $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
136                 $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
137                 $this->assertEquals('fr', $configCache->get('system', 'language'));
138
139                 $this->assertEquals('admin@friendica.local', $configCache->get('config', 'admin_email'));
140                 $this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
141
142                 $this->assertEquals('/another/php', $configCache->get('config', 'php_path'));
143                 $this->assertEquals('999', $configCache->get('config', 'max_import_size'));
144         }
145
146         public function testLoadAddonConfig()
147         {
148                 $structure = [
149                         'addon' => [
150                                 'test' => [
151                                         'config' => [],
152                                 ],
153                         ],
154                 ];
155
156                 vfsStream::create($structure, $this->root);
157
158                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
159                         '..' . DIRECTORY_SEPARATOR .
160                         '..' . DIRECTORY_SEPARATOR .
161                         '..' . DIRECTORY_SEPARATOR .
162                         'datasets' . DIRECTORY_SEPARATOR .
163                         'config' . DIRECTORY_SEPARATOR .
164                         'local.config.php';
165
166                 vfsStream::newFile('test.config.php')
167                         ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
168                         ->setContent(file_get_contents($file));
169
170                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
171
172                 $conf = $configCacheLoader->loadAddonConfig('test');
173
174                 $this->assertEquals('testhost', $conf['database']['hostname']);
175                 $this->assertEquals('testuser', $conf['database']['username']);
176                 $this->assertEquals('testpw', $conf['database']['password']);
177                 $this->assertEquals('testdb', $conf['database']['database']);
178
179                 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);
180         }
181 }