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 Friendica\Util\Config\ConfigCacheSaver;
11 use Mockery\MockInterface;
12 use org\bovigo\vfs\vfsStream;
14 class ConfigCacheSaverTest extends MockedTest
18 * @var App\Mode|MockInterface
21 protected function setUp()
25 $this->mode = \Mockery::mock(App\Mode::class);
26 $this->mode->shouldReceive('isInstall')->andReturn(true);
29 * Test the saveToConfigFile() method with a local.config.php file
31 public function testSaveToConfigFileLocal()
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 .
41 vfsStream::newFile('local.config.php')
42 ->at($this->root->getChild('config'))
43 ->setContent(file_get_contents($file));
45 $configCacheSaver = new ConfigCacheSaver($this->root->url());
46 $configCacheLoader = new ConfigCacheLoader($this->root->url(), $this->mode);
47 $configCache = new ConfigCache();
48 $configCacheLoader->loadConfigFiles($configCache);
50 $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email'));
51 $this->assertNull($configCache->get('config', 'test_val'));
53 $configCacheSaver->saveToConfigFile('config', 'admin_email', 'new@mail.it');
54 $configCacheSaver->saveToConfigFile('config', 'test_val', 'Testing$!"$with@all.we can!');
56 $newConfigCache = new ConfigCache();
57 $configCacheLoader->loadConfigFiles($newConfigCache);
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'));
65 $this->assertEquals(file_get_contents($file), file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php.old')->url()));
68 * Test the saveToConfigFile() method with a local.ini.php file
70 public function testSaveToConfigFileINI()
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 .
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()));
100 * Test the saveToConfigFile() method with a .htconfig.php file
101 * @todo fix it after 2019.03 merge to develop
103 public function testSaveToConfigFileHtconfig()
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')
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()));