]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/BaseURLTest.php
Add checks & realpath() usage
[friendica.git] / tests / src / Util / BaseURLTest.php
index 330a4eebe740e6c43051d506886c34e3e1ce8dbd..7f63027fcb7c10f8552f25157ab724a3305b7d8e 100644 (file)
@@ -1,11 +1,11 @@
 <?php
 namespace Friendica\Test\src\Util;
 
+use Friendica\App\BaseURL;
 use Friendica\Core\Config\Configuration;
-use Friendica\Util\BaseURL;
-use PHPUnit\Framework\TestCase;
+use Friendica\Test\MockedTest;
 
-class BaseURLTest extends TestCase
+class BaseURLTest extends MockedTest
 {
        public function dataDefault()
        {
@@ -179,18 +179,26 @@ class BaseURLTest extends TestCase
                $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());
@@ -296,9 +304,18 @@ class BaseURLTest extends TestCase
 
                $baseUrl = new BaseURL($configMock, []);
 
-               $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
-               $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
-               $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
+               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();
+               }
+
                $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
 
                $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
@@ -325,9 +342,18 @@ class BaseURLTest extends TestCase
 
                $baseUrl = new BaseURL($configMock, []);
 
-               $configMock->shouldReceive('set')->with('config', 'hostname', (!empty($save['hostname']) ? $save['hostname'] : $input['hostname']))->andReturn(true)->once();
-               $configMock->shouldReceive('set')->with('system', 'urlpath', (!empty($save['urlPath']) ? $save['urlPath'] : $input['urlPath']))->andReturn(true)->once();
-               $configMock->shouldReceive('set')->with('system', 'ssl_policy', (!empty($save['sslPolicy']) ? $save['sslPolicy'] : $input['sslPolicy']))->andReturn(true)->once();
+               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();
+               }
+
                $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
 
                $baseUrl->saveByURL($url);
@@ -452,4 +478,65 @@ class BaseURLTest extends TestCase
 
                $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');
+       }
 }