3 namespace Friendica\Test\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'));
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'));
163 public function testLoadAddonConfig()
173 vfsStream::create($structure, $this->root);
175 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
176 '..' . DIRECTORY_SEPARATOR .
177 '..' . DIRECTORY_SEPARATOR .
178 '..' . DIRECTORY_SEPARATOR .
179 'datasets' . DIRECTORY_SEPARATOR .
180 'config' . DIRECTORY_SEPARATOR .
183 vfsStream::newFile('test.config.php')
184 ->at($this->root->getChild('addon')->getChild('test')->getChild('config'))
185 ->setContent(file_get_contents($file));
187 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
189 $conf = $configCacheLoader->loadAddonConfig('test');
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']);
196 $this->assertEquals('admin@test.it', $conf['config']['admin_email']);