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\ConfigFileLoader;
10 use Friendica\Util\Config\ConfigFileSaver;
11 use Mockery\MockInterface;
12 use org\bovigo\vfs\vfsStream;
14 class ConfigFileSaverTest extends MockedTest
19 * @var App\Mode|MockInterface
23 protected function setUp()
27 $this->mode = \Mockery::mock(App\Mode::class);
28 $this->mode->shouldReceive('isInstall')->andReturn(true);
31 public function dataConfigFiles()
35 'fileName' => 'local.config.php',
36 'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
37 '..' . DIRECTORY_SEPARATOR .
38 '..' . DIRECTORY_SEPARATOR .
39 'datasets' . DIRECTORY_SEPARATOR .
41 'relativePath' => 'config',
44 'fileName' => 'local.ini.php',
45 'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
46 '..' . DIRECTORY_SEPARATOR .
47 '..' . DIRECTORY_SEPARATOR .
48 'datasets' . DIRECTORY_SEPARATOR .
50 'relativePath' => 'config',
53 'fileName' => '.htconfig.php',
54 'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
55 '..' . DIRECTORY_SEPARATOR .
56 '..' . DIRECTORY_SEPARATOR .
57 'datasets' . DIRECTORY_SEPARATOR .
65 * Test the saveToConfigFile() method
66 * @dataProvider dataConfigFiles
68 * @todo 20190324 [nupplaphil] for ini-configs, it isn't possible to use $ or ! inside values
70 public function testSaveToConfig($fileName, $filePath, $relativePath)
72 $this->delConfigFile('local.config.php');
74 if (empty($relativePath)) {
76 $relativeFullName = $fileName;
78 $root = $this->root->getChild($relativePath);
79 $relativeFullName = $relativePath . DIRECTORY_SEPARATOR . $fileName;
82 vfsStream::newFile($fileName)
84 ->setContent(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName));
86 $configFileSaver = new ConfigFileSaver($this->root->url());
87 $configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
88 $configCache = new ConfigCache();
89 $configFileLoader->setupCache($configCache);
91 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
92 $this->assertEquals('frio', $configCache->get('system', 'theme'));
93 $this->assertNull($configCache->get('config', 'test_val'));
94 $this->assertNull($configCache->get('system', 'test_val2'));
96 // update values (system and config value)
97 $configFileSaver->addConfigValue('config', 'admin_email', 'new@mail.it');
98 $configFileSaver->addConfigValue('system', 'theme', 'vier');
100 // insert values (system and config value)
101 $configFileSaver->addConfigValue('config', 'test_val', 'Testingwith@all.we can');
102 $configFileSaver->addConfigValue('system', 'test_val2', 'TestIt First');
105 $configFileSaver->addConfigValue('system', 'test_val2', 'TestIt Now');
108 $this->assertTrue($configFileSaver->saveToConfigFile());
110 $newConfigCache = new ConfigCache();
111 $configFileLoader->setupCache($newConfigCache);
113 $this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email'));
114 $this->assertEquals('Testingwith@all.we can', $newConfigCache->get('config', 'test_val'));
115 $this->assertEquals('vier', $newConfigCache->get('system', 'theme'));
116 $this->assertEquals('TestIt Now', $newConfigCache->get('system', 'test_val2'));
118 $this->assertTrue($this->root->hasChild($relativeFullName));
119 $this->assertTrue($this->root->hasChild($relativeFullName . '.old'));
120 $this->assertFalse($this->root->hasChild($relativeFullName . '.tmp'));
122 $this->assertEquals(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName), file_get_contents($this->root->getChild($relativeFullName . '.old')->url()));
126 * Test the saveToConfigFile() method without permissions
127 * @dataProvider dataConfigFiles
129 public function testNoPermission($fileName, $filePath, $relativePath)
131 $this->delConfigFile('local.config.php');
133 if (empty($relativePath)) {
135 $relativeFullName = $fileName;
137 $root = $this->root->getChild($relativePath);
138 $relativeFullName = $relativePath . DIRECTORY_SEPARATOR . $fileName;
143 vfsStream::newFile($fileName)
145 ->setContent(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName));
147 $configFileSaver = new ConfigFileSaver($this->root->url());
149 $configFileSaver->addConfigValue('system', 'test_val2', 'TestIt Now');
151 // wrong mod, so return false if nothing to write
152 $this->assertFalse($configFileSaver->saveToConfigFile());
156 * Test the saveToConfigFile() method with nothing to do
157 * @dataProvider dataConfigFiles
159 public function testNothingToDo($fileName, $filePath, $relativePath)
161 $this->delConfigFile('local.config.php');
163 if (empty($relativePath)) {
165 $relativeFullName = $fileName;
167 $root = $this->root->getChild($relativePath);
168 $relativeFullName = $relativePath . DIRECTORY_SEPARATOR . $fileName;
171 vfsStream::newFile($fileName)
173 ->setContent(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName));
175 $configFileSaver = new ConfigFileSaver($this->root->url());
176 $configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
177 $configCache = new ConfigCache();
178 $configFileLoader->setupCache($configCache);
181 $this->assertTrue($configFileSaver->saveToConfigFile());
183 $this->assertTrue($this->root->hasChild($relativeFullName));
184 $this->assertFalse($this->root->hasChild($relativeFullName . '.old'));
185 $this->assertFalse($this->root->hasChild($relativeFullName . '.tmp'));
187 $this->assertEquals(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName), file_get_contents($this->root->getChild($relativeFullName)->url()));