3 namespace Friendica\Test\src\Util\Config;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Test\MockedTest;
8 use Friendica\Test\Util\VFSTrait;
9 use Friendica\Util\Config\ConfigCacheLoader;
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 'datasets' . DIRECTORY_SEPARATOR .
75 'config' . DIRECTORY_SEPARATOR .
78 vfsStream::newFile('local.config.php')
79 ->at($this->root->getChild('config'))
80 ->setContent(file_get_contents($file));
82 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
83 $configCache = new ConfigCache();
85 $configCacheLoader->loadConfigFiles($configCache);
87 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
88 $this->assertEquals('testuser', $configCache->get('database', 'username'));
89 $this->assertEquals('testpw', $configCache->get('database', 'password'));
90 $this->assertEquals('testdb', $configCache->get('database', 'database'));
92 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
93 $this->assertEquals('Friendica Social Network', $configCache->get('config', 'sitename'));
97 * Test the loadConfigFile() method with a local.ini.php file
99 public function testLoadConfigFilesINI()
101 $this->delConfigFile('local.config.php');
103 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
104 '..' . DIRECTORY_SEPARATOR .
105 '..' . DIRECTORY_SEPARATOR .
106 'datasets' . DIRECTORY_SEPARATOR .
107 'config' . DIRECTORY_SEPARATOR .
110 vfsStream::newFile('local.ini.php')
111 ->at($this->root->getChild('config'))
112 ->setContent(file_get_contents($file));
114 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
115 $configCache = new ConfigCache();
117 $configCacheLoader->loadConfigFiles($configCache);
119 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
120 $this->assertEquals('testuser', $configCache->get('database', 'username'));
121 $this->assertEquals('testpw', $configCache->get('database', 'password'));
122 $this->assertEquals('testdb', $configCache->get('database', 'database'));
124 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
128 * Test the loadConfigFile() method with a .htconfig.php file
130 public function testLoadConfigFilesHtconfig()
132 $this->delConfigFile('local.config.php');
134 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
135 '..' . DIRECTORY_SEPARATOR .
136 '..' . DIRECTORY_SEPARATOR .
137 'datasets' . DIRECTORY_SEPARATOR .
138 'config' . DIRECTORY_SEPARATOR .
139 '.htconfig.test.php';
141 vfsStream::newFile('.htconfig.php')
143 ->setContent(file_get_contents($file));
145 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
146 $configCache = new ConfigCache();
148 $configCacheLoader->loadConfigFiles($configCache);
150 $this->assertEquals('testhost', $configCache->get('database', 'hostname'));
151 $this->assertEquals('testuser', $configCache->get('database', 'username'));
152 $this->assertEquals('testpw', $configCache->get('database', 'password'));
153 $this->assertEquals('testdb', $configCache->get('database', 'database'));
154 $this->assertEquals('anotherCharset', $configCache->get('database', 'charset'));
156 $this->assertEquals('/var/run/friendica.pid', $configCache->get('system', 'pidfile'));
157 $this->assertEquals('Europe/Berlin', $configCache->get('system', 'default_timezone'));
158 $this->assertEquals('fr', $configCache->get('system', 'language'));
160 $this->assertEquals('admin@friendica.local', $configCache->get('config', 'admin_email'));
161 $this->assertEquals('Friendly admin', $configCache->get('config', 'admin_nickname'));
163 $this->assertEquals('/another/php', $configCache->get('config', 'php_path'));
164 $this->assertEquals('999', $configCache->get('config', 'max_import_size'));
165 $this->assertEquals('666', $configCache->get('system', 'maximagesize'));
167 $this->assertEquals('quattro,vier,duepuntozero', $configCache->get('system', 'allowed_themes'));
168 $this->assertEquals('1', $configCache->get('system', 'no_regfullname'));
171 public function testLoadAddonConfig()
181 vfsStream::create($structure, $this->root);
183 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
184 '..' . DIRECTORY_SEPARATOR .
185 '..' . DIRECTORY_SEPARATOR .
186 'datasets' . DIRECTORY_SEPARATOR .
187 'config' . DIRECTORY_SEPARATOR .
190 vfsStream::newFile('test.config.php')
191 ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
192 ->setContent(file_get_contents($file));
194 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
196 $conf = $configCacheLoader->loadAddonConfig('test');
198 $this->assertEquals('testhost', $conf['database']['hostname']);
199 $this->assertEquals('testuser', $conf['database']['username']);
200 $this->assertEquals('testpw', $conf['database']['password']);
201 $this->assertEquals('testdb', $conf['database']['database']);
203 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);