]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/BaseURLTest.php
Merge remote-tracking branch 'upstream/2021.12-rc' into api-fixes
[friendica.git] / tests / src / Util / BaseURLTest.php
index c819a22e5ff364cdc4ac26834eb0644e63f47dc6..4c15f440dc452b23457ad70090b950b71425c455 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 namespace Friendica\Test\src\Util;
 
-use Friendica\Core\Config\Configuration;
+use Friendica\App\BaseURL;
+use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Test\MockedTest;
-use Friendica\Util\BaseURL;
 
 class BaseURLTest extends MockedTest
 {
@@ -173,31 +173,39 @@ class BaseURLTest extends MockedTest
         */
        public function testCheck($server, $input, $assert)
        {
-               $configMock = \Mockery::mock(Configuration::class);
+               $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']);
 
+               // 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'])->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'])->once();
                }
 
-               if (!isset($input['hostname']) && !empty($assert['hostname'])) {
+               // 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'])->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'])->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());
+               self::assertEquals($assert['hostname'], $baseUrl->getHostname());
+               self::assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
+               self::assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
+               self::assertEquals($assert['scheme'], $baseUrl->getScheme());
+               self::assertEquals($assert['url'], $baseUrl->get());
        }
 
        public function dataSave()
@@ -287,7 +295,7 @@ class BaseURLTest extends MockedTest
         */
        public function testSave($input, $save, $url)
        {
-               $configMock = \Mockery::mock(Configuration::class);
+               $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']);
@@ -312,7 +320,7 @@ class BaseURLTest extends MockedTest
 
                $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
 
-               $this->assertEquals($url, $baseUrl->get());
+               self::assertEquals($url, $baseUrl->get());
        }
 
        /**
@@ -325,7 +333,7 @@ class BaseURLTest extends MockedTest
         */
        public function testSaveByUrl($input, $save, $url)
        {
-               $configMock = \Mockery::mock(Configuration::class);
+               $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']);
@@ -350,7 +358,7 @@ class BaseURLTest extends MockedTest
 
                $baseUrl->saveByURL($url);
 
-               $this->assertEquals($url, $baseUrl->get());
+               self::assertEquals($url, $baseUrl->get());
        }
 
        public function dataGetBaseUrl()
@@ -401,7 +409,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 +417,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 +467,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,6 +476,67 @@ class BaseURLTest extends MockedTest
 
                $baseUrl = new BaseURL($configMock, $server);
 
-               $this->assertEquals($redirect, $baseUrl->checkRedirectHttps());
+               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())->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, []);
+               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());
        }
 }