f91f267c71b9b52f5f1058bd0aec0312c8917c75
[friendica.git/.git] / tests / src / Core / Config / Cache / ConfigCacheLoaderTest.php
1 <?php
2
3 namespace Friendica\Test\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 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                         '..' . DIRECTORY_SEPARATOR .
65                         'datasets' . DIRECTORY_SEPARATOR .
66                         'config' . DIRECTORY_SEPARATOR .
67                         'local.config.php';
68
69                 vfsStream::newFile('local.config.php')
70                         ->at($this->root->getChild('config'))
71                         ->setContent(file_get_contents($file));
72
73                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
74                 $configCache = new ConfigCache();
75
76                 $configCacheLoader->loadConfigFiles($configCache);
77
78                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
79                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
80                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
81                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
82
83                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
84                 $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
85         }
86
87         /**
88          * Test the loadConfigFile() method with a local.ini.php file
89          */
90         public function testLoadConfigFilesINI()
91         {
92                 $this->delConfigFile('local.config.php');
93
94                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
95                         '..' . DIRECTORY_SEPARATOR .
96                         '..' . DIRECTORY_SEPARATOR .
97                         '..' . DIRECTORY_SEPARATOR .
98                         'datasets' . DIRECTORY_SEPARATOR .
99                         'config' . DIRECTORY_SEPARATOR .
100                         'local.ini.php';
101
102                 vfsStream::newFile('local.ini.php')
103                         ->at($this->root->getChild('config'))
104                         ->setContent(file_get_contents($file));
105
106                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
107                 $configCache = new ConfigCache();
108
109                 $configCacheLoader->loadConfigFiles($configCache);
110
111                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
112                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
113                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
114                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
115
116                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
117         }
118
119         /**
120          * Test the loadConfigFile() method with a .htconfig.php file
121          */
122         public function testLoadConfigFilesHtconfig()
123         {
124                 $this->delConfigFile('local.config.php');
125
126                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
127                         '..' . DIRECTORY_SEPARATOR .
128                         '..' . DIRECTORY_SEPARATOR .
129                         '..' . DIRECTORY_SEPARATOR .
130                         'datasets' . DIRECTORY_SEPARATOR .
131                         'config' . DIRECTORY_SEPARATOR .
132                         '.htconfig.test.php';
133
134                 vfsStream::newFile('.htconfig.php')
135                         ->at($this->root)
136                         ->setContent(file_get_contents($file));
137
138                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
139                 $configCache = new ConfigCache();
140
141                 $configCacheLoader->loadConfigFiles($configCache);
142
143                 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
144                 $this->assertEquals('testuser', $configCache->get('database', 'username'));
145                 $this->assertEquals('testpw', $configCache->get('database', 'password'));
146                 $this->assertEquals('testdb', $configCache->get('database', 'database'));
147
148                 $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
149                 $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
150                 $this->assertEquals('fr', $configCache->get('system', 'language'));
151         }
152
153         public function testLoadAddonConfig()
154         {
155                 $structure = [
156                         'addon' => [
157                                 'test' => [
158                                         'config' => [],
159                                 ],
160                         ],
161                 ];
162
163                 vfsStream::create($structure, $this->root);
164
165                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
166                         '..' . DIRECTORY_SEPARATOR .
167                         '..' . DIRECTORY_SEPARATOR .
168                         '..' . DIRECTORY_SEPARATOR .
169                         'datasets' . DIRECTORY_SEPARATOR .
170                         'config' . DIRECTORY_SEPARATOR .
171                         'local.config.php';
172
173                 vfsStream::newFile('test.config.php')
174                         ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
175                         ->setContent(file_get_contents($file));
176
177                 $configCacheLoader = new ConfigCacheLoader($this->root->url());
178
179                 $conf = $configCacheLoader->loadAddonConfig('test');
180
181                 $this->assertEquals('testhost', $conf['database']['hostname']);
182                 $this->assertEquals('testuser', $conf['database']['username']);
183                 $this->assertEquals('testpw', $conf['database']['password']);
184                 $this->assertEquals('testdb', $conf['database']['database']);
185
186                 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);
187         }
188 }