]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/BaseURLTest.php
Just commit config transactions if something changed
[friendica.git] / tests / src / Util / BaseURLTest.php
index 2733096e33456fffe28eccb548f077eb5b77600c..b9c23a1d8b12d3b7d24dc6b9be9d556b772372d1 100644 (file)
@@ -23,10 +23,25 @@ namespace Friendica\Test\src\Util;
 
 use Friendica\App\BaseURL;
 use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
+use Friendica\Core\Config\Model\Config;
+use Friendica\Core\Config\Model\ConfigTransaction;
+use Friendica\Core\Config\Util\ConfigFileManager;
+use Friendica\Core\Config\ValueObject\Cache;
 use Friendica\Test\MockedTest;
+use Friendica\Test\Util\VFSTrait;
 
 class BaseURLTest extends MockedTest
 {
+       use VFSTrait;
+
+       protected function setUp(): void
+       {
+               parent::setUp();
+
+               $this->setUpVfsDir();
+       }
+
        public function dataDefault()
        {
                return [
@@ -199,34 +214,24 @@ class BaseURLTest extends MockedTest
                $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
                $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
 
-               $savable = false;
-
                // If we don't have an urlPath as an input, we assert it, we will save it to the DB for the next time
                if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
-                       $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'], false)->once();
-                       $savable = true;
+                       $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
                }
 
                // If we don't have the ssl_policy as an input, we assert it, we will save it to the DB for the next time
                if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
-                       $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'], false)->once();
-                       $savable = true;
+                       $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
                }
 
                // If we don't have the hostname as an input, we assert it, we will save it to the DB for the next time
                if (empty($input['hostname']) && !empty($assert['hostname'])) {
-                       $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'], false)->once();
-                       $savable = true;
+                       $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
                }
 
                // If we don't have an URL at first, but we assert it, we will save it to the DB for the next time
                if (empty($input['url']) && !empty($assert['url'])) {
-                       $configMock->shouldReceive('set')->with('system', 'url', $assert['url'], false)->once();
-                       $savable = true;
-               }
-
-               if ($savable) {
-                       $configMock->shouldReceive('save')->once();
+                       $configMock->shouldReceive('set')->with('system', 'url', $assert['url'])->once();
                }
 
                $baseUrl = new BaseURL($configMock, $server);
@@ -241,6 +246,21 @@ class BaseURLTest extends MockedTest
        public function dataSave()
        {
                return [
+                       'no_change' => [
+                               'input' => [
+                                       'hostname'  => 'friendica.local',
+                                       'urlPath'   => 'path',
+                                       'sslPolicy' => BaseURL::SSL_POLICY_FULL,
+                                       'url'       => 'https://friendica.local/path',
+                                       'force_ssl' => true,
+                               ],
+                               'save' => [
+                                       'hostname'  => 'friendica.local',
+                                       'urlPath'   => 'path',
+                                       'sslPolicy' => BaseURL::SSL_POLICY_FULL,
+                               ],
+                               'url' => 'https://friendica.local/path',
+                       ],
                        'default' => [
                                'input' => [
                                        'hostname'  => 'friendica.old',
@@ -325,30 +345,20 @@ class BaseURLTest extends MockedTest
         */
        public function testSave($input, $save, $url)
        {
-               $configMock = \Mockery::mock(IManageConfigValues::class);
-               $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
-               $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
-               $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
-               $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
-               $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
-
-               $baseUrl = new BaseURL($configMock, []);
-
-               if (isset($save['hostname'])) {
-                       $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'], false)->andReturn(true)->once();
-               }
-
-               if (isset($save['urlPath'])) {
-                       $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'], false)->andReturn(true)->once();
-               }
-
-               if (isset($save['sslPolicy'])) {
-                       $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'], false)->andReturn(true)->once();
-               }
-
-               $configMock->shouldReceive('set')->with('system', 'url', $url, false)->andReturn(true)->once();
+               $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
+               $config = new Config($configFileManager, new Cache([
+                       'config' => [
+                               'hostname' => $input['hostname'] ?? null,
+                       ],
+                       'system' => [
+                               'urlpath' => $input['urlPath'] ?? null,
+                               'ssl_policy' => $input['sslPolicy'] ?? null,
+                               'url' => $input['url'] ?? null,
+                               'force_ssl' => $input['force_ssl'] ?? null,
+                       ],
+               ]));
 
-               $configMock->shouldReceive('save')->once();
+               $baseUrl = new BaseURL($config, []);
 
                $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
 
@@ -365,30 +375,20 @@ class BaseURLTest extends MockedTest
         */
        public function testSaveByUrl($input, $save, $url)
        {
-               $configMock = \Mockery::mock(IManageConfigValues::class);
-               $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
-               $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
-               $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
-               $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
-               $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
-
-               $baseUrl = new BaseURL($configMock, []);
-
-               if (isset($save['hostname'])) {
-                       $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'], false)->andReturn(true)->once();
-               }
-
-               if (isset($save['urlPath'])) {
-                       $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'], false)->andReturn(true)->once();
-               }
-
-               if (isset($save['sslPolicy'])) {
-                       $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'], false)->andReturn(true)->once();
-               }
-
-               $configMock->shouldReceive('set')->with('system', 'url', $url, false)->andReturn(true)->once();
+               $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
+               $config = new Config($configFileManager, new Cache([
+                       'config' => [
+                               'hostname' => $input['hostname'] ?? null,
+                       ],
+                       'system' => [
+                               'urlpath' => $input['urlPath'] ?? null,
+                               'ssl_policy' => $input['sslPolicy'] ?? null,
+                               'url' => $input['url'] ?? null,
+                               'force_ssl' => $input['force_ssl'] ?? null,
+                       ],
+               ]));
 
-               $configMock->shouldReceive('save')->once();
+               $baseUrl = new BaseURL($config, []);
 
                $baseUrl->saveByURL($url);
 
@@ -512,68 +512,4 @@ class BaseURLTest extends MockedTest
 
                self::assertEquals($redirect, $baseUrl->checkRedirectHttps());
        }
-
-       public function dataWrongSave()
-       {
-               return [
-                       'wrongHostname' => [
-                               'fail' => 'hostname',
-                       ],
-                       'wrongSSLPolicy' => [
-                               'fail' => 'sslPolicy',
-                       ],
-                       'wrongURLPath' => [
-                               'fail' => 'urlPath',
-                       ],
-                       'wrongURL' => [
-                               'fail' => 'url',
-                       ],
-               ];
-       }
-
-       /**
-        * Test the save() method with wrong parameters
-        * @dataProvider dataWrongSave
-        */
-       public function testWrongSave($fail)
-       {
-               $configMock = \Mockery::mock(IManageConfigValues::class);
-               $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
-               $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
-               $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(BaseURL::DEFAULT_SSL_SCHEME);
-               $configMock->shouldReceive('get')->with('system', 'url')->andReturn('http://friendica.local/new/test');
-
-               switch ($fail) {
-                       case 'hostname':
-                               $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any(), false)->andReturn(false)->once();
-                               break;
-                       case 'sslPolicy':
-                               $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any(), false)->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any(), false)->andReturn(false)->once();
-                               $configMock->shouldReceive('save')->once();
-                               break;
-                       case 'urlPath':
-                               $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any(), false)->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any(), false)->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any(), false)->andReturn(false)->once();
-                               $configMock->shouldReceive('save')->once();
-                               break;
-                       case 'url':
-                               $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any(), false)->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any(), false)->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any(), false)->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'url', \Mockery::any(), false)->andReturn(false)->once();
-                               $configMock->shouldReceive('save')->once();
-                               break;
-               }
-
-               $baseUrl = new BaseURL($configMock, []);
-               self::assertFalse($baseUrl->save('test', 10, 'nope'));
-
-               // nothing should have changed because we never successfully saved anything
-               self::assertEquals('friendica.local', $baseUrl->getHostname());
-               self::assertEquals('new/test', $baseUrl->getUrlPath());
-               self::assertEquals(BaseURL::DEFAULT_SSL_SCHEME, $baseUrl->getSSLPolicy());
-               self::assertEquals('http://friendica.local/new/test', $baseUrl->get());
-       }
 }