3 namespace Friendica\Test\src\Core\Config\Cache;
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;
13 class ConfigCacheLoaderTest extends MockedTest
18 * @var App\Mode|MockInterface
22 protected function setUp()
28 $this->mode = \Mockery::mock(App\Mode::class);
29 $this->mode->shouldReceive('isInstall')->andReturn(true);
33 * Test the loadConfigFiles() method with default values
35 public function testLoadConfigFiles()
37 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
38 $configCache = new ConfigCache();
40 $configCacheLoader->loadConfigFiles($configCache);
42 $this->assertEquals($this->root->url(), $configCache->get('system', 'basepath'));
46 * Test the loadConfigFiles() method with a wrong local.config.php
47 * @expectedException \Exception
48 * @expectedExceptionMessageRegExp /Error loading config file \w+/
50 public function testLoadConfigWrong()
52 $this->delConfigFile('local.config.php');
54 vfsStream::newFile('local.config.php')
55 ->at($this->root->getChild('config'))
56 ->setContent('<?php return true;');
58 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
59 $configCache = new ConfigCache();
61 $configCacheLoader->loadConfigFiles($configCache);
65 * Test the loadConfigFiles() method with a local.config.php file
67 public function testLoadConfigFilesLocal()
69 $this->delConfigFile('local.config.php');
71 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
72 '..' . DIRECTORY_SEPARATOR .
73 '..' . DIRECTORY_SEPARATOR .
74 '..' . DIRECTORY_SEPARATOR .
75 'datasets' . DIRECTORY_SEPARATOR .
76 'config' . DIRECTORY_SEPARATOR .
79 vfsStream::newFile('local.config.php')
80 ->at($this->root->getChild('config'))
81 ->setContent(file_get_contents($file));
83 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
84 $configCache = new ConfigCache();
86 $configCacheLoader->loadConfigFiles($configCache);
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'));
93 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
94 $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
98 * Test the loadConfigFile() method with a local.ini.php file
100 public function testLoadConfigFilesINI()
102 $this->delConfigFile('local.config.php');
104 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
105 '..' . DIRECTORY_SEPARATOR .
106 '..' . DIRECTORY_SEPARATOR .
107 '..' . DIRECTORY_SEPARATOR .
108 'datasets' . DIRECTORY_SEPARATOR .
109 'config' . DIRECTORY_SEPARATOR .
112 vfsStream::newFile('local.ini.php')
113 ->at($this->root->getChild('config'))
114 ->setContent(file_get_contents($file));
116 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
117 $configCache = new ConfigCache();
119 $configCacheLoader->loadConfigFiles($configCache);
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'));
126 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
130 * Test the loadConfigFile() method with a .htconfig.php file
132 public function testLoadConfigFilesHtconfig()
134 $this->delConfigFile('local.config.php');
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';
144 vfsStream::newFile('.htconfig.php')
146 ->setContent(file_get_contents($file));
148 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
149 $configCache = new ConfigCache();
151 $configCacheLoader->loadConfigFiles($configCache);
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 $this->assertEquals('anotherCharset', $configCache->get('database', 'charset'));
159 $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
160 $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
161 $this->assertEquals('fr', $configCache->get('system', 'language'));
163 $this->assertEquals('admin@friendica.local', $configCache->get('config', 'admin_email'));
164 $this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
166 $this->assertEquals('/another/php', $configCache->get('config', 'php_path'));
167 $this->assertEquals('999', $configCache->get('config', 'max_import_size'));
168 $this->assertEquals('666', $configCache->get('system', 'maximagesize'));
170 $this->assertEquals('quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
171 $this->assertEquals('1', $configCache->get('system', 'no_regfullname'));
174 public function testLoadAddonConfig()
184 vfsStream::create($structure, $this->root);
186 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
187 '..' . DIRECTORY_SEPARATOR .
188 '..' . DIRECTORY_SEPARATOR .
189 '..' . DIRECTORY_SEPARATOR .
190 'datasets' . DIRECTORY_SEPARATOR .
191 'config' . DIRECTORY_SEPARATOR .
194 vfsStream::newFile('test.config.php')
195 ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
196 ->setContent(file_get_contents($file));
198 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
200 $conf = $configCacheLoader->loadAddonConfig('test');
202 $this->assertEquals('testhost', $conf['database']['hostname']);
203 $this->assertEquals('testuser', $conf['database']['username']);
204 $this->assertEquals('testpw', $conf['database']['password']);
205 $this->assertEquals('testdb', $conf['database']['database']);
207 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);