Bugfix ConfigCacheLoaderTest
[friendica.git/.git] / tests / src / Core / Config / Cache / ConfigCacheLoaderTest.php
1 <?php
2
3 namespace Friendica\Test\Core\Config\Cache;
4
5 use Friendica\App;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Core\Config\Cache\ConfigCacheLoader;
8 use Friendica\Test\MockedTest;
9 use Friendica\Test\Util\VFSTrait;
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                         '..' . DIRECTORY_SEPARATOR .
75                         'datasets' . DIRECTORY_SEPARATOR .
76                         'config' . DIRECTORY_SEPARATOR .
77                         'local.config.php';
78
79                 vfsStream::newFile('local.config.php')
80                         ->at($this->root->getChild('config'))
81                         ->setContent(file_get_contents($file));
82
83                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
84                 $configCache = new ConfigCache();
85
86                 $configCacheLoader->loadConfigFiles($configCache);
87
88                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
89                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
90                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
91                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
92
93                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
94                 $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
95         }
96
97         /**
98          * Test the loadConfigFile() method with a local.ini.php file
99          */
100         public function testLoadConfigFilesINI()
101         {
102                 $this->delConfigFile('local.config.php');
103
104                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
105                         '..' . DIRECTORY_SEPARATOR .
106                         '..' . DIRECTORY_SEPARATOR .
107                         '..' . DIRECTORY_SEPARATOR .
108                         'datasets' . DIRECTORY_SEPARATOR .
109                         'config' . DIRECTORY_SEPARATOR .
110                         'local.ini.php';
111
112                 vfsStream::newFile('local.ini.php')
113                         ->at($this->root->getChild('config'))
114                         ->setContent(file_get_contents($file));
115
116                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
117                 $configCache = new ConfigCache();
118
119                 $configCacheLoader->loadConfigFiles($configCache);
120
121                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
122                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
123                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
124                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
125
126                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
127         }
128
129         /**
130          * Test the loadConfigFile() method with a .htconfig.php file
131          */
132         public function testLoadConfigFilesHtconfig()
133         {
134                 $this->delConfigFile('local.config.php');
135
136                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
137                         '..' . DIRECTORY_SEPARATOR .
138                         '..' . DIRECTORY_SEPARATOR .
139                         '..' . DIRECTORY_SEPARATOR .
140                         'datasets' . DIRECTORY_SEPARATOR .
141                         'config' . DIRECTORY_SEPARATOR .
142                         '.htconfig.test.php';
143
144                 vfsStream::newFile('.htconfig.php')
145                         ->at($this->root)
146                         ->setContent(file_get_contents($file));
147
148                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
149                 $configCache = new ConfigCache();
150
151                 $configCacheLoader->loadConfigFiles($configCache);
152
153                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
154                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
155                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
156                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
157
158                 $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
159                 $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
160                 $this->assertEquals('fr', $configCache->get('system', 'language'));
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                         '..' . DIRECTORY_SEPARATOR .
179                         'datasets' . DIRECTORY_SEPARATOR .
180                         'config' . DIRECTORY_SEPARATOR .
181                         'local.config.php';
182
183                 vfsStream::newFile('test.config.php')
184                         ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
185                         ->setContent(file_get_contents($file));
186
187                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
188
189                 $conf = $configCacheLoader->loadAddonConfig('test');
190
191                 $this->assertEquals('testhost', $conf['database']['hostname']);
192                 $this->assertEquals('testuser', $conf['database']['username']);
193                 $this->assertEquals('testpw', $conf['database']['password']);
194                 $this->assertEquals('testdb', $conf['database']['database']);
195
196                 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);
197         }
198 }