From: Philipp Date: Sat, 18 Feb 2023 19:24:48 +0000 (+0100) Subject: Refactor BaseURL.php to UriInterface compatible class X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=45749c14be69aeff3000c33766e5248146f87fef;p=friendica.git Refactor BaseURL.php to UriInterface compatible class - remove parts - added tests --- diff --git a/src/App/BaseURL.php b/src/App/BaseURL.php index 564527a652..8c046fda3e 100644 --- a/src/App/BaseURL.php +++ b/src/App/BaseURL.php @@ -23,284 +23,66 @@ namespace Friendica\App; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\System; -use Friendica\Util\Network; use Friendica\Util\Strings; use Friendica\Network\HTTPException; +use GuzzleHttp\Psr7\ServerRequest; +use GuzzleHttp\Psr7\Uri; +use Psr\Http\Message\UriInterface; +use Psr\Log\LoggerInterface; /** * A class which checks and contains the basic * environment for the BaseURL (url, urlpath, ssl_policy, hostname, scheme) */ -class BaseURL +class BaseURL extends Uri implements UriInterface { - /** - * No SSL necessary - */ - const SSL_POLICY_NONE = 0; - - /** - * SSL is necessary - */ - const SSL_POLICY_FULL = 1; - - /** - * SSL is optional, but preferred - */ - const SSL_POLICY_SELFSIGN = 2; - - /** - * Define the Default SSL scheme - */ - const DEFAULT_SSL_SCHEME = self::SSL_POLICY_SELFSIGN; - - /** - * The Friendica Config - * - * @var IManageConfigValues - */ - private $config; - - /** - * The server side variables - * - * @var array - */ - private $server; - - /** - * The hostname of the Base URL - * - * @var string - */ - private $hostname; - - /** - * The SSL_POLICY of the Base URL - * - * @var int - */ - private $sslPolicy; - - /** - * The URL sub-path of the Base URL - * - * @var string - */ - private $urlPath; - - /** - * The full URL - * - * @var string - */ - private $url; - - /** - * The current scheme of this call - * - * @var string - */ - private $scheme; - - /** - * Returns the hostname of this node - * - * @return string - */ - public function getHostname(): string - { - return $this->hostname; - } - - /** - * Returns the current scheme of this call - * - * @return string - */ - public function getScheme(): string - { - return $this->scheme; - } - - /** - * Returns the SSL policy of this node - * - * @return int - */ - public function getSSLPolicy(): int - { - return $this->sslPolicy; - } - - /** - * Returns the sub-path of this URL - * - * @return string - */ - public function getUrlPath(): string - { - return $this->urlPath; - } - - /** - * Returns the full URL of this call - * - * Note: $ssl parameter value doesn't directly correlate with the resulting protocol - * - * @param bool $ssl True, if ssl should get used - * - * @return string - */ - public function get(bool $ssl = false): string + public function __construct(IManageConfigValues $config, LoggerInterface $logger, array $server = []) { - if ($this->sslPolicy === self::SSL_POLICY_SELFSIGN && $ssl) { - return Network::switchScheme($this->url); - } - - return $this->url; - } - - /** - * Save current parts of the base Url - * - * @param string? $hostname - * @param int? $sslPolicy - * @param string? $urlPath - * - * @return bool true, if successful - * @TODO Find proper types - */ - public function save($hostname = null, $sslPolicy = null, $urlPath = null): bool - { - $currUrl = $this->url; - - $configTransaction = $this->config->beginTransaction(); - - if (!empty($hostname) && $hostname !== $this->hostname) { - $configTransaction->set('config', 'hostname', $hostname); - $this->hostname = $hostname; - } - - if (isset($sslPolicy) && $sslPolicy !== $this->sslPolicy) { - $configTransaction->set('system', 'ssl_policy', $sslPolicy); - $this->sslPolicy = $sslPolicy; - } - - if (isset($urlPath) && $urlPath !== $this->urlPath) { - $configTransaction->set('system', 'urlpath', $urlPath); - $this->urlPath = $urlPath; - } - - $this->determineBaseUrl(); - if ($this->url !== $currUrl) { - $configTransaction->set('system', 'url', $this->url); + $url = $config->get('system', 'url'); + if (empty($url)) { + $logger->critical('Invalid config - Missing system.url'); + $url = ServerRequest::getUriFromGlobals() + ->withQuery('') + ->withPath($this->determineURLPath($server)); + + parent::__construct((string)$url); + } else { + parent::__construct($url); } - - $configTransaction->commit(); - - return true; } /** - * Save the current url as base URL - * - * @param string $url - * - * @return bool true, if the save was successful + * Figure out if we are running at the top of a domain or in a subdirectory */ - public function saveByURL(string $url): bool + private function determineURLPath(array $server): string { - $parsed = @parse_url($url); - - if (empty($parsed) || empty($parsed['host'])) { - return false; - } - - $hostname = $parsed['host']; - if (!empty($hostname) && !empty($parsed['port'])) { - $hostname .= ':' . $parsed['port']; - } - - $urlPath = null; - if (!empty($parsed['path'])) { - $urlPath = trim($parsed['path'], '\\/'); - } - - $sslPolicy = null; - if (!empty($parsed['scheme'])) { - if ($parsed['scheme'] == 'https') { - $sslPolicy = BaseURL::SSL_POLICY_FULL; + /* Relative script path to the web server root + * Not all of those $_SERVER properties can be present, so we do by inverse priority order + */ + $relativeScriptPath = + ($server['REDIRECT_URL'] ?? '') ?: + ($server['REDIRECT_URI'] ?? '') ?: + ($server['REDIRECT_SCRIPT_URL'] ?? '') ?: + ($server['SCRIPT_URL'] ?? '') ?: + $server['REQUEST_URI'] ?? ''; + + /* $relativeScriptPath gives /relative/path/to/friendica/module/parameter + * QUERY_STRING gives pagename=module/parameter + * + * To get /relative/path/to/friendica we perform dirname() for as many levels as there are slashes in the QUERY_STRING + */ + if (!empty($relativeScriptPath)) { + // Module + if (!empty($server['QUERY_STRING'])) { + return trim(dirname($relativeScriptPath, substr_count(trim($server['QUERY_STRING'], '/'), '/') + 1), '/'); + } else { + // Root page + $scriptPathParts = explode('?', $relativeScriptPath, 2); + return trim($scriptPathParts[0], '/'); } } - return $this->save($hostname, $sslPolicy, $urlPath); - } - - /** - * Checks, if a redirect to the HTTPS site would be necessary - * - * @return bool - */ - public function checkRedirectHttps() - { - return $this->config->get('system', 'force_ssl') && - ($this->getScheme() == "http") && - intval($this->getSSLPolicy()) == BaseURL::SSL_POLICY_FULL && - strpos($this->get(), 'https://') === 0 && - !empty($this->server['REQUEST_METHOD']) && - $this->server['REQUEST_METHOD'] === 'GET'; - } - - /** - * @param IManageConfigValues $config The Friendica IConfiguration - * @param array $server The $_SERVER array - */ - public function __construct(IManageConfigValues $config, array $server) - { - $this->config = $config; - $this->server = $server; - $this->hostname = $this->config->get('config', 'hostname'); - $this->urlPath = $this->config->get('system', 'urlpath') ?? ''; - $this->sslPolicy = $this->config->get('system', 'ssl_policy') ?? static::DEFAULT_SSL_SCHEME; - $this->url = $this->config->get('system', 'url'); - - if (empty($this->hostname) || empty($this->url)) { - throw new \Exception('Invalid config - Missing system.url or config.hostname'); - } - - $this->determineSchema(); - } - - /** - * Determine the full URL based on all parts - */ - private function determineBaseUrl() - { - $scheme = 'http'; - - if ($this->sslPolicy == self::SSL_POLICY_FULL) { - $scheme = 'https'; - } - - $this->url = $scheme . '://' . $this->hostname . (!empty($this->urlPath) ? '/' . $this->urlPath : ''); - } - - /** - * Determine the scheme of the current used link - */ - private function determineSchema() - { - $this->scheme = 'http'; - - if (!empty($this->server['HTTPS']) || - !empty($this->server['HTTP_FORWARDED']) && preg_match('/proto=https/', $this->server['HTTP_FORWARDED']) || - !empty($this->server['HTTP_X_FORWARDED_PROTO']) && $this->server['HTTP_X_FORWARDED_PROTO'] == 'https' || - !empty($this->server['HTTP_X_FORWARDED_SSL']) && $this->server['HTTP_X_FORWARDED_SSL'] == 'on' || - !empty($this->server['FRONT_END_HTTPS']) && $this->server['FRONT_END_HTTPS'] == 'on' || - !empty($this->server['SERVER_PORT']) && (intval($this->server['SERVER_PORT']) == 443) // XXX: reasonable assumption, but isn't this hardcoding too much? - ) { - $this->scheme = 'https'; - } + return ''; } /** @@ -314,11 +96,11 @@ class BaseURL { // Remove the hostname from the url if it is an internal link $nurl = Strings::normaliseLink($origURL); - $base = Strings::normaliseLink($this->get()); + $base = Strings::normaliseLink($this->__toString()); $url = str_replace($base . '/', '', $nurl); - // if it is an external link return the orignal value - if ($url == Strings::normaliseLink($origURL)) { + // if it is an external link return the original value + if ($url === $nurl) { return $origURL; } else { return $url; @@ -344,15 +126,7 @@ class BaseURL throw new HTTPException\InternalServerErrorException("$toUrl is not a relative path, please use System::externalRedirectTo"); } - $redirectTo = $this->get($ssl) . '/' . ltrim($toUrl, '/'); + $redirectTo = $this->__toString() . '/' . ltrim($toUrl, '/'); System::externalRedirect($redirectTo); } - - /** - * Returns the base url as string - */ - public function __toString(): string - { - return (string) $this->get(); - } } diff --git a/tests/src/App/BaseURLTest.php b/tests/src/App/BaseURLTest.php new file mode 100644 index 0000000000..e45b30b20a --- /dev/null +++ b/tests/src/App/BaseURLTest.php @@ -0,0 +1,191 @@ +. + * + */ + +namespace Friendica\Test\src\App; + +use Friendica\App\BaseURL; +use Friendica\Core\Config\Model\ReadOnlyFileConfig; +use Friendica\Core\Config\ValueObject\Cache; +use Friendica\Network\HTTPException\InternalServerErrorException; +use Friendica\Test\MockedTest; +use Psr\Log\NullLogger; + +class BaseURLTest extends MockedTest +{ + public function dataSystemUrl(): array + { + return [ + 'default' => [ + 'input' => ['system' => ['url' => 'https://friendica.local',],], + 'server' => [], + 'assertion' => 'https://friendica.local', + ], + 'subPath' => [ + 'input' => ['system' => ['url' => 'https://friendica.local/subpath',],], + 'server' => [], + 'assertion' => 'https://friendica.local/subpath', + ], + 'empty' => [ + 'input' => [], + 'server' => [], + 'assertion' => 'http://localhost', + ], + 'serverArrayStandard' => [ + 'input' => [], + 'server' => [ + 'HTTPS' => 'on', + 'HTTP_HOST' => 'friendica.server', + 'REQUEST_URI' => '/test/it?with=query', + 'QUERY_STRING' => 'pagename=test/it', + ], + 'assertion' => 'https://friendica.server', + ], + 'serverArraySubPath' => [ + 'input' => [], + 'server' => [ + 'HTTPS' => 'on', + 'HTTP_HOST' => 'friendica.server', + 'REQUEST_URI' => '/test/it/now?with=query', + 'QUERY_STRING' => 'pagename=it/now', + ], + 'assertion' => 'https://friendica.server/test', + ], + 'serverArraySubPath2' => [ + 'input' => [], + 'server' => [ + 'HTTPS' => 'on', + 'HTTP_HOST' => 'friendica.server', + 'REQUEST_URI' => '/test/it/now?with=query', + 'QUERY_STRING' => 'pagename=now', + ], + 'assertion' => 'https://friendica.server/test/it', + ], + 'serverArraySubPath3' => [ + 'input' => [], + 'server' => [ + 'HTTPS' => 'on', + 'HTTP_HOST' => 'friendica.server', + 'REQUEST_URI' => '/test/it/now?with=query', + 'QUERY_STRING' => 'pagename=test/it/now', + ], + 'assertion' => 'https://friendica.server', + ], + 'serverArrayWithoutQueryString1' => [ + 'input' => [], + 'server' => [ + 'HTTPS' => 'on', + 'HTTP_HOST' => 'friendica.server', + 'REQUEST_URI' => '/test/it/now?with=query', + ], + 'assertion' => 'https://friendica.server/test/it/now', + ], + 'serverArrayWithoutQueryString2' => [ + 'input' => [], + 'server' => [ + 'HTTPS' => 'on', + 'HTTP_HOST' => 'friendica.server', + 'REQUEST_URI' => '', + ], + 'assertion' => 'https://friendica.server', + ], + 'serverArrayWithoutQueryString3' => [ + 'input' => [], + 'server' => [ + 'HTTPS' => 'on', + 'HTTP_HOST' => 'friendica.server', + 'REQUEST_URI' => '/', + ], + 'assertion' => 'https://friendica.server', + ], + ]; + } + + /** + * @dataProvider dataSystemUrl + */ + public function testDetermine(array $input, array $server, string $assertion) + { + $origServerGlobal = $_SERVER; + + $_SERVER = array_merge_recursive($_SERVER, $server); + $config = new ReadOnlyFileConfig(new Cache($input)); + + $baseUrl = new BaseURL($config, new NullLogger(), $server); + + self::assertEquals($assertion, (string)$baseUrl); + + $_SERVER = $origServerGlobal; + } + + public function dataRemove(): array + { + return [ + 'same' => [ + 'base' => ['system' => ['url' => 'https://friendica.local',],], + 'origUrl' => 'https://friendica.local/test/picture.png', + 'assertion' => 'test/picture.png', + ], + 'other' => [ + 'base' => ['system' => ['url' => 'https://friendica.local',],], + 'origUrl' => 'https://friendica.other/test/picture.png', + 'assertion' => 'https://friendica.other/test/picture.png', + ], + 'samSubPath' => [ + 'base' => ['system' => ['url' => 'https://friendica.local/test',],], + 'origUrl' => 'https://friendica.local/test/picture.png', + 'assertion' => 'picture.png', + ], + 'otherSubPath' => [ + 'base' => ['system' => ['url' => 'https://friendica.local/test',],], + 'origUrl' => 'https://friendica.other/test/picture.png', + 'assertion' => 'https://friendica.other/test/picture.png', + ], + ]; + } + + /** + * @dataProvider dataRemove + */ + public function testRemove(array $base, string $origUrl, string $assertion) + { + $config = new ReadOnlyFileConfig(new Cache($base)); + $baseUrl = new BaseURL($config, new NullLogger()); + + self::assertEquals($assertion, $baseUrl->remove($origUrl)); + } + + /** + * Test that redirect to external domains fails + */ + public function testRedirectException() + { + self::expectException(InternalServerErrorException::class); + self::expectErrorMessage('https://friendica.other is not a relative path, please use System::externalRedirect'); + + $config = new ReadOnlyFileConfig(new Cache([ + 'system' => [ + 'url' => 'https://friendica.local', + ] + ])); + $baseUrl = new BaseURL($config, new NullLogger()); + $baseUrl->redirect('https://friendica.other'); + } +} diff --git a/tests/src/Util/BaseURLTest.php b/tests/src/Util/BaseURLTest.php deleted file mode 100644 index a72ffc607b..0000000000 --- a/tests/src/Util/BaseURLTest.php +++ /dev/null @@ -1,473 +0,0 @@ -. - * - */ - -namespace Friendica\Test\src\Util; - -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 [ - 'null' => [ - 'server' => [], - 'input' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => null, - 'url' => null, - ], - 'assert' => [ - 'hostname' => '', - 'urlPath' => '', - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'http://', - 'scheme' => 'http', - ], - ], - 'WithSubDirectory' => [ - 'server' => [ - 'SERVER_NAME' => 'friendica.local', - 'REDIRECT_URI' => 'test/module/more', - 'QUERY_STRING' => 'module/more', - ], - 'input' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => null, - 'url' => null, - ], - 'assert' => [ - 'hostname' => 'friendica.local', - 'urlPath' => 'test', - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'http://friendica.local/test', - 'scheme' => 'http', - ], - ], - 'input' => [ - 'server' => [], - 'input' => [ - 'hostname' => 'friendica.local', - 'urlPath' => 'test', - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'url' => 'http://friendica.local/test', - ], - 'assert' => [ - 'hostname' => 'friendica.local', - 'urlPath' => 'test', - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'url' => 'http://friendica.local/test', - 'scheme' => 'http', - ], - ], - 'WithHttpsScheme' => [ - 'server' => [ - 'SERVER_NAME' => 'friendica.local', - 'REDIRECT_URI' => 'test/module/more', - 'QUERY_STRING' => 'module/more', - 'HTTPS' => true, - ], - 'input' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => null, - 'url' => null, - ], - 'assert' => [ - 'hostname' => 'friendica.local', - 'urlPath' => 'test', - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'url' => 'https://friendica.local/test', - 'scheme' => 'https', - ], - ], - 'WithoutQueryString' => [ - 'server' => [ - 'SERVER_NAME' => 'friendica.local', - 'REDIRECT_URI' => 'test/more', - 'HTTPS' => true, - ], - 'input' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => null, - 'url' => null, - ], - 'assert' => [ - 'hostname' => 'friendica.local', - 'urlPath' => 'test/more', - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'url' => 'https://friendica.local/test/more', - 'scheme' => 'https', - ], - ], - 'WithPort' => [ - 'server' => [ - 'SERVER_NAME' => 'friendica.local', - 'SERVER_PORT' => '1234', - 'REDIRECT_URI' => 'test/more', - 'HTTPS' => true, - ], - 'input' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => null, - 'url' => null, - ], - 'assert' => [ - 'hostname' => 'friendica.local:1234', - 'urlPath' => 'test/more', - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'url' => 'https://friendica.local:1234/test/more', - 'scheme' => 'https', - ], - ], - 'With443Port' => [ - 'server' => [ - 'SERVER_NAME' => 'friendica.local', - 'SERVER_PORT' => '443', - 'REDIRECT_URI' => 'test/more', - ], - 'input' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => null, - 'url' => null, - ], - 'assert' => [ - 'hostname' => 'friendica.local', - 'urlPath' => 'test/more', - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'url' => 'https://friendica.local/test/more', - 'scheme' => 'https', - ], - ], - 'With80Port' => [ - 'server' => [ - 'SERVER_NAME' => 'friendica.local', - 'SERVER_PORT' => '80', - 'REDIRECT_URI' => 'test/more', - ], - 'input' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => null, - 'url' => null, - ], - 'assert' => [ - 'hostname' => 'friendica.local', - 'urlPath' => 'test/more', - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'http://friendica.local/test/more', - 'scheme' => 'http', - ], - ], - ]; - } - - 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', - 'urlPath' => 'is/old/path', - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'http://friendica.old/is/old/path', - 'force_ssl' => true, - ], - 'save' => [ - 'hostname' => 'friendica.local', - 'urlPath' => 'new/path', - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - ], - 'url' => 'https://friendica.local/new/path', - ], - 'null' => [ - 'input' => [ - 'hostname' => 'friendica.old', - 'urlPath' => 'is/old/path', - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'http://friendica.old/is/old/path', - 'force_ssl' => true, - ], - 'save' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => null, - ], - 'url' => 'http://friendica.old/is/old/path', - ], - 'changeHostname' => [ - 'input' => [ - 'hostname' => 'friendica.old', - 'urlPath' => 'is/old/path', - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'http://friendica.old/is/old/path', - 'force_ssl' => true, - ], - 'save' => [ - 'hostname' => 'friendica.local', - 'urlPath' => null, - 'sslPolicy' => null, - ], - 'url' => 'http://friendica.local/is/old/path', - ], - 'changeUrlPath' => [ - 'input' => [ - 'hostname' => 'friendica.old', - 'urlPath' => 'is/old/path', - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'http://friendica.old/is/old/path', - 'force_ssl' => true, - ], - 'save' => [ - 'hostname' => null, - 'urlPath' => 'new/path', - 'sslPolicy' => null, - ], - 'url' => 'http://friendica.old/new/path', - ], - 'changeSSLPolicy' => [ - 'input' => [ - 'hostname' => 'friendica.old', - 'urlPath' => 'is/old/path', - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'http://friendica.old/is/old/path', - 'force_ssl' => true, - ], - 'save' => [ - 'hostname' => null, - 'urlPath' => null, - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - ], - 'url' => 'https://friendica.old/is/old/path', - ], - ]; - } - - /** - * Test the save() method - * @dataProvider dataSave - */ - public function testSave($input, $save, $url) - { - $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, - ], - ])); - - $baseUrl = new BaseURL($config, []); - - $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']); - - self::assertEquals($url, $baseUrl->get()); - } - - /** - * Test the saveByUrl() method - * @dataProvider dataSave - * - * @param $input - * @param $save - * @param $url - */ - public function testSaveByUrl($input, $save, $url) - { - $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, - ], - ])); - - $baseUrl = new BaseURL($config, []); - - $baseUrl->saveByURL($url); - - self::assertEquals($url, $baseUrl->get()); - } - - public function dataGetBaseUrl() - { - return [ - 'default' => [ - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'ssl' => false, - 'url' => 'http://friendica.local/new/test', - 'assert' => 'http://friendica.local/new/test', - ], - 'DefaultWithSSL' => [ - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'ssl' => true, - 'url' => 'http://friendica.local/new/test', - 'assert' => 'https://friendica.local/new/test', - ], - 'SSLFullWithSSL' => [ - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'ssl' => true, - 'url' => 'http://friendica.local/new/test', - 'assert' => 'http://friendica.local/new/test', - ], - 'SSLFullWithoutSSL' => [ - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'ssl' => false, - 'url' => 'https://friendica.local/new/test', - 'assert' => 'https://friendica.local/new/test', - ], - 'NoSSLWithSSL' => [ - 'sslPolicy' => BaseURL::SSL_POLICY_NONE, - 'ssl' => true, - 'url' => 'http://friendica.local/new/test', - 'assert' => 'http://friendica.local/new/test', - ], - 'NoSSLWithoutSSL' => [ - 'sslPolicy' => BaseURL::SSL_POLICY_NONE, - 'ssl' => false, - 'url' => 'http://friendica.local/new/test', - 'assert' => 'http://friendica.local/new/test', - ], - ]; - } - - /** - * Test the get() method - * @dataProvider dataGetBaseUrl - */ - public function testGetURL($sslPolicy, $ssl, $url, $assert) - { - $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); - $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url); - - $baseUrl = new BaseURL($configMock, []); - - self::assertEquals($assert, $baseUrl->get($ssl)); - } - - public function dataCheckRedirectHTTPS() - { - return [ - 'default' => [ - 'server' => [ - 'REQUEST_METHOD' => 'GET', - 'HTTPS' => true, - ], - 'forceSSL' => false, - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'https://friendica.local', - 'redirect' => false, - ], - 'forceSSL' => [ - 'server' => [ - 'REQUEST_METHOD' => 'GET', - ], - 'forceSSL' => true, - 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME, - 'url' => 'https://friendica.local', - 'redirect' => false, - ], - 'forceSSLWithSSLPolicy' => [ - 'server' => [], - 'forceSSL' => true, - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'url' => 'https://friendica.local', - 'redirect' => false, - ], - 'forceSSLWithSSLPolicyAndGet' => [ - 'server' => [ - 'REQUEST_METHOD' => 'GET', - ], - 'forceSSL' => true, - 'sslPolicy' => BaseURL::SSL_POLICY_FULL, - 'url' => 'https://friendica.local', - 'redirect' => true, - ], - ]; - } - - /** - * Test the checkRedirectHTTPS() method - * @dataProvider dataCheckRedirectHTTPS - */ - public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect) - { - $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); - $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url); - $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL); - - $baseUrl = new BaseURL($configMock, $server); - - self::assertEquals($redirect, $baseUrl->checkRedirectHttps()); - } -}