]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/BaseURLTest.php
Revert node.config.php into Config table
[friendica.git] / tests / src / Util / BaseURLTest.php
index ee88bd980461d95f90caf7d6a8b9fd0f16a27276..a72ffc607b72d3acb6f29f9d7a194ab555689627 100644 (file)
@@ -1,12 +1,48 @@
 <?php
-namespace Friendica\Test\src\Util;
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
-use Friendica\Core\Config\Configuration;
-use Friendica\Test\MockedTest;
-use Friendica\Util\BaseURL;
+namespace Friendica\Test\src\Util;
 
-class BaseURLTest extends MockedTest
+use Friendica\App\BaseURL;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Config\Model\DatabaseConfig;
+use Friendica\Core\Config\Model\ReadOnlyFileConfig;
+use Friendica\Core\Config\Util\ConfigFileManager;
+use Friendica\Core\Config\ValueObject\Cache;
+use Friendica\Test\DatabaseTest;
+use Friendica\Test\Util\CreateDatabaseTrait;
+use Friendica\Test\Util\VFSTrait;
+
+class BaseURLTest extends DatabaseTest
 {
+       use VFSTrait;
+       use CreateDatabaseTrait;
+
+       protected function setUp(): void
+       {
+               parent::setUp();
+
+               $this->setUpVfsDir();
+       }
+
        public function dataDefault()
        {
                return [
@@ -167,42 +203,24 @@ class BaseURLTest extends MockedTest
                ];
        }
 
-       /**
-        * Test the default config determination
-        * @dataProvider dataDefault
-        */
-       public function testCheck($server, $input, $assert)
-       {
-               $configMock = \Mockery::mock(Configuration::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']);
-
-               if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
-                       $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
-               }
-
-               if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
-                       $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
-               }
-
-               if (!isset($input['hostname']) && !empty($assert['hostname'])) {
-                       $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
-               }
-
-               $baseUrl = new BaseURL($configMock, $server);
-
-               $this->assertEquals($assert['hostname'], $baseUrl->getHostname());
-               $this->assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
-               $this->assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
-               $this->assertEquals($assert['scheme'], $baseUrl->getScheme());
-               $this->assertEquals($assert['url'], $baseUrl->get());
-       }
-
        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',
@@ -287,32 +305,23 @@ class BaseURLTest extends MockedTest
         */
        public function testSave($input, $save, $url)
        {
-               $configMock = \Mockery::mock(Configuration::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'])->andReturn(true)->once();
-               }
-
-               if (isset($save['urlPath'])) {
-                       $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
-               }
-
-               if (isset($save['sslPolicy'])) {
-                       $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
-               }
+               $config = new DatabaseConfig($this->getDbInstance(), 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('set')->with('system', 'url', $url)->andReturn(true)->once();
+               $baseUrl = new BaseURL($config, []);
 
                $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
 
-               $this->assertEquals($url, $baseUrl->get());
+               self::assertEquals($url, $baseUrl->get());
        }
 
        /**
@@ -325,32 +334,23 @@ class BaseURLTest extends MockedTest
         */
        public function testSaveByUrl($input, $save, $url)
        {
-               $configMock = \Mockery::mock(Configuration::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'])->andReturn(true)->once();
-               }
-
-               if (isset($save['urlPath'])) {
-                       $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
-               }
-
-               if (isset($save['sslPolicy'])) {
-                       $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
-               }
+               $config = new DatabaseConfig($this->getDbInstance(), 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('set')->with('system', 'url', $url)->andReturn(true)->once();
+               $baseUrl = new BaseURL($config, []);
 
                $baseUrl->saveByURL($url);
 
-               $this->assertEquals($url, $baseUrl->get());
+               self::assertEquals($url, $baseUrl->get());
        }
 
        public function dataGetBaseUrl()
@@ -401,7 +401,7 @@ class BaseURLTest extends MockedTest
         */
        public function testGetURL($sslPolicy, $ssl, $url, $assert)
        {
-               $configMock = \Mockery::mock(Configuration::class);
+               $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($sslPolicy);
@@ -409,7 +409,7 @@ class BaseURLTest extends MockedTest
 
                $baseUrl = new BaseURL($configMock, []);
 
-               $this->assertEquals($assert, $baseUrl->get($ssl));
+               self::assertEquals($assert, $baseUrl->get($ssl));
        }
 
        public function dataCheckRedirectHTTPS()
@@ -459,7 +459,7 @@ class BaseURLTest extends MockedTest
         */
        public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
        {
-               $configMock = \Mockery::mock(Configuration::class);
+               $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($sslPolicy);
@@ -468,67 +468,6 @@ class BaseURLTest extends MockedTest
 
                $baseUrl = new BaseURL($configMock, $server);
 
-               $this->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(Configuration::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())->andReturn(false)->once();
-                               break;
-                       case 'sslPolicy':
-                               $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(false)->once();
-                               break;
-                       case 'urlPath':
-                               $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(false)->once();
-                               break;
-                       case 'url':
-                               $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(true)->twice();
-                               $configMock->shouldReceive('set')->with('system', 'url', \Mockery::any())->andReturn(false)->once();
-                               break;
-               }
-
-               $baseUrl = new BaseURL($configMock, []);
-               $this->assertFalse($baseUrl->save('test', 10, 'nope'));
-
-               // nothing should have changed because we never successfully saved anything
-               $this->assertEquals($baseUrl->getHostname(), 'friendica.local');
-               $this->assertEquals($baseUrl->getUrlPath(), 'new/test');
-               $this->assertEquals($baseUrl->getSSLPolicy(), BaseURL::DEFAULT_SSL_SCHEME);
-               $this->assertEquals($baseUrl->get(), 'http://friendica.local/new/test');
+               self::assertEquals($redirect, $baseUrl->checkRedirectHttps());
        }
 }