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