]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Config/ConfigFileSaverTest.php
Merge pull request #6990 from annando/fix-warning
[friendica.git] / tests / src / Util / Config / ConfigFileSaverTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util\Config;
4
5 use Friendica\App;
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;
13
14 class ConfigFileSaverTest extends MockedTest
15 {
16         use VFSTrait;
17
18         /**
19          * @var App\Mode|MockInterface
20          */
21         private $mode;
22
23         protected function setUp()
24         {
25                 parent::setUp();
26                 $this->setUpVfsDir();
27                 $this->mode = \Mockery::mock(App\Mode::class);
28                 $this->mode->shouldReceive('isInstall')->andReturn(true);
29         }
30
31         public function dataConfigFiles()
32         {
33                 return [
34                         'config' => [
35                                 'fileName' => 'local.config.php',
36                                 'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
37                                         '..' . DIRECTORY_SEPARATOR .
38                                         '..' . DIRECTORY_SEPARATOR .
39                                         'datasets' . DIRECTORY_SEPARATOR .
40                                         'config',
41                                 'relativePath' => 'config',
42                         ],
43                         'ini' => [
44                                 'fileName' => 'local.ini.php',
45                                 'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
46                                         '..' . DIRECTORY_SEPARATOR .
47                                         '..' . DIRECTORY_SEPARATOR .
48                                         'datasets' . DIRECTORY_SEPARATOR .
49                                         'config',
50                                 'relativePath' => 'config',
51                         ],
52                         'htconfig' => [
53                                 'fileName' => '.htconfig.php',
54                                 'filePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR .
55                                         '..' . DIRECTORY_SEPARATOR .
56                                         '..' . DIRECTORY_SEPARATOR .
57                                         'datasets' . DIRECTORY_SEPARATOR .
58                                         'config',
59                                 'relativePath' => '',
60                         ],
61                 ];
62         }
63
64         /**
65          * Test the saveToConfigFile() method
66          * @dataProvider dataConfigFiles
67          *
68          * @todo 20190324 [nupplaphil] for ini-configs, it isn't possible to use $ or ! inside values
69          */
70         public function testSaveToConfig($fileName, $filePath, $relativePath)
71         {
72                 $this->delConfigFile('local.config.php');
73
74                 if (empty($relativePath)) {
75                         $root = $this->root;
76                         $relativeFullName = $fileName;
77                 } else {
78                         $root = $this->root->getChild($relativePath);
79                         $relativeFullName = $relativePath . DIRECTORY_SEPARATOR . $fileName;
80                 }
81
82                 vfsStream::newFile($fileName)
83                         ->at($root)
84                         ->setContent(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName));
85
86                 $configFileSaver = new ConfigFileSaver($this->root->url());
87                 $configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
88                 $configCache = new ConfigCache();
89                 $configFileLoader->setupCache($configCache);
90
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'));
95
96                 // update values (system and config value)
97                 $configFileSaver->addConfigValue('config', 'admin_email', 'new@mail.it');
98                 $configFileSaver->addConfigValue('system', 'theme', 'vier');
99
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');
103
104                 // overwrite value
105                 $configFileSaver->addConfigValue('system', 'test_val2', 'TestIt Now');
106
107                 // save it
108                 $this->assertTrue($configFileSaver->saveToConfigFile());
109
110                 $newConfigCache = new ConfigCache();
111                 $configFileLoader->setupCache($newConfigCache);
112
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'));
117
118                 $this->assertTrue($this->root->hasChild($relativeFullName));
119                 $this->assertTrue($this->root->hasChild($relativeFullName . '.old'));
120                 $this->assertFalse($this->root->hasChild($relativeFullName . '.tmp'));
121
122                 $this->assertEquals(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName), file_get_contents($this->root->getChild($relativeFullName . '.old')->url()));
123         }
124
125         /**
126          * Test the saveToConfigFile() method without permissions
127          * @dataProvider dataConfigFiles
128          */
129         public function testNoPermission($fileName, $filePath, $relativePath)
130         {
131                 $this->delConfigFile('local.config.php');
132
133                 if (empty($relativePath)) {
134                         $root = $this->root;
135                         $relativeFullName = $fileName;
136                 } else {
137                         $root = $this->root->getChild($relativePath);
138                         $relativeFullName = $relativePath . DIRECTORY_SEPARATOR . $fileName;
139                 }
140
141                 $root->chmod(000);
142
143                 vfsStream::newFile($fileName)
144                         ->at($root)
145                         ->setContent(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName));
146
147                 $configFileSaver = new ConfigFileSaver($this->root->url());
148
149                 $configFileSaver->addConfigValue('system', 'test_val2', 'TestIt Now');
150
151                 // wrong mod, so return false if nothing to write
152                 $this->assertFalse($configFileSaver->saveToConfigFile());
153         }
154
155         /**
156          * Test the saveToConfigFile() method with nothing to do
157          * @dataProvider dataConfigFiles
158          */
159         public function testNothingToDo($fileName, $filePath, $relativePath)
160         {
161                 $this->delConfigFile('local.config.php');
162
163                 if (empty($relativePath)) {
164                         $root = $this->root;
165                         $relativeFullName = $fileName;
166                 } else {
167                         $root = $this->root->getChild($relativePath);
168                         $relativeFullName = $relativePath . DIRECTORY_SEPARATOR . $fileName;
169                 }
170
171                 vfsStream::newFile($fileName)
172                         ->at($root)
173                         ->setContent(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName));
174
175                 $configFileSaver = new ConfigFileSaver($this->root->url());
176                 $configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode);
177                 $configCache = new ConfigCache();
178                 $configFileLoader->setupCache($configCache);
179
180                 // save nothing
181                 $this->assertTrue($configFileSaver->saveToConfigFile());
182
183                 $this->assertTrue($this->root->hasChild($relativeFullName));
184                 $this->assertFalse($this->root->hasChild($relativeFullName . '.old'));
185                 $this->assertFalse($this->root->hasChild($relativeFullName . '.tmp'));
186
187                 $this->assertEquals(file_get_contents($filePath . DIRECTORY_SEPARATOR . $fileName), file_get_contents($this->root->getChild($relativeFullName)->url()));
188         }
189 }