remove basepath and hostname from admin panel and add update path
[friendica.git/.git] / tests / src / Util / Config / ConfigCacheSaverTest.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\ConfigCacheLoader;
10 use Friendica\Util\Config\ConfigCacheSaver;
11 use Mockery\MockInterface;
12 use org\bovigo\vfs\vfsStream;
13
14 class ConfigCacheSaverTest extends MockedTest
15 {
16         use VFSTrait;
17         /**
18          * @var App\Mode|MockInterface
19          */
20         private $mode;
21         protected function setUp()
22         {
23                 parent::setUp();
24                 $this->setUpVfsDir();
25                 $this->mode = \Mockery::mock(App\Mode::class);
26                 $this->mode->shouldReceive('isInstall')->andReturn(true);
27         }
28         /**
29          * Test the saveToConfigFile() method with a local.config.php file
30          */
31         public function testSaveToConfigFileLocal()
32         {
33                 $this->delConfigFile('local.config.php');
34                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
35                         '..' . DIRECTORY_SEPARATOR .
36                         '..' . DIRECTORY_SEPARATOR .
37                         'datasets' . DIRECTORY_SEPARATOR .
38                         'config' . DIRECTORY_SEPARATOR .
39                         'local.config.php';
40
41                 vfsStream::newFile('local.config.php')
42                         ->at($this->root->getChild('config'))
43                         ->setContent(file_get_contents($file));
44
45                 $configCacheSaver = new ConfigCacheSaver($this->root->url());
46                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
47                 $configCache = new ConfigCache();
48                 $configCacheLoader->loadConfigFiles($configCache);
49
50                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
51                 $this->assertNull($configCache->get('config', 'test_val'));
52
53                 $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it');
54                 $configCacheSaver->saveToConfigFile('config', 'test_val', 'Testing$!"$with@all.we can!');
55
56                 $newConfigCache = new ConfigCache();
57                 $configCacheLoader->loadConfigFiles($newConfigCache);
58
59                 $this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email'));
60                 $this->assertEquals('Testing$!"$with@all.we can!', $newConfigCache->get('config', 'test_val'));
61                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
62                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.old'));
63                 $this->assertFalse($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.tmp'));
64
65                 $this->assertEquals(file_get_contents($file), file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.old')->url()));
66         }
67         /**
68          * Test the saveToConfigFile() method with a local.ini.php file
69          */
70         public function testSaveToConfigFileINI()
71         {
72                 $this->delConfigFile('local.config.php');
73                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
74                         '..' . DIRECTORY_SEPARATOR .
75                         '..' . DIRECTORY_SEPARATOR .
76                         'datasets' . DIRECTORY_SEPARATOR .
77                         'config' . DIRECTORY_SEPARATOR .
78                         'local.ini.php';
79                 vfsStream::newFile('local.ini.php')
80                         ->at($this->root->getChild('config'))
81                         ->setContent(file_get_contents($file));
82                 $configCacheSaver = new ConfigCacheSaver($this->root->url());
83                 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
84                 $configCache = new ConfigCache();
85                 $configCacheLoader->loadConfigFiles($configCache);
86                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
87                 $this->assertNull($configCache->get('config', 'test_val'));
88                 $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it');
89                 $configCacheSaver->saveToConfigFile('config', 'test_val', "Testing@with.all we can");
90                 $newConfigCache = new ConfigCache();
91                 $configCacheLoader->loadConfigFiles($newConfigCache);
92                 $this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email'));
93                 $this->assertEquals("Testing@with.all we can", $newConfigCache->get('config', 'test_val'));
94                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
95                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php.old'));
96                 $this->assertFalse($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php.tmp'));
97                 $this->assertEquals(file_get_contents($file), file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.ini.old')->url()));
98         }
99         /**
100          * Test the saveToConfigFile() method with a .htconfig.php file
101          * @todo fix it after 2019.03 merge to develop
102          */
103         public function testSaveToConfigFileHtconfig()
104         {
105                 $this->markTestSkipped('Needs 2019.03 merge to develop first');
106                 $this->delConfigFile('local.config.php');
107                 $file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
108                         '..' . DIRECTORY_SEPARATOR .
109                         '..' . DIRECTORY_SEPARATOR .
110                         '..' . DIRECTORY_SEPARATOR .
111                         'datasets' . DIRECTORY_SEPARATOR .
112                         'config' . DIRECTORY_SEPARATOR .
113                         '.htconfig.test.php';
114                 vfsStream::newFile('.htconfig.php')
115                         ->at($this->root)
116                         ->setContent(file_get_contents($file));
117                 $configCacheSaver = new ConfigCacheSaver($this->root->url(), $this->mode);
118                 $configCache = new ConfigCache();
119                 $configCacheSaver->loadConfigFiles($configCache);
120                 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
121                 $this->assertEquals('!<unset>!', $configCache->get('config', 'test_val'));
122                 $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it');
123                 $configCacheSaver->saveToConfigFile('config', 'test_val', 'Testing$!"$with@all.we can!');
124                 $newConfigCache = new ConfigCache();
125                 $configCacheSaver->loadConfigFiles($newConfigCache);
126                 $this->assertEquals('new@mail.it', $newConfigCache->get('config', 'admin_email'));
127                 $this->assertEquals('Testing$!"$with@all.we can!', $newConfigCache->get('config', 'test_val'));
128                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . '.htconfig.php'));
129                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . '.htconfig.php.old'));
130                 $this->assertFalse($this->root->hasChild('config' . DIRECTORY_SEPARATOR . '.htconfig.php.tmp'));
131                 $this->assertEquals(file_get_contents($file), file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . '.htconfig.php.old')->url()));
132         }
133 }