3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Util;
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\Config\Model\DatabaseConfig;
27 use Friendica\Core\Config\Model\ReadOnlyFileConfig;
28 use Friendica\Core\Config\Util\ConfigFileManager;
29 use Friendica\Core\Config\ValueObject\Cache;
30 use Friendica\Test\DatabaseTest;
31 use Friendica\Test\Util\CreateDatabaseTrait;
32 use Friendica\Test\Util\VFSTrait;
34 class BaseURLTest extends DatabaseTest
37 use CreateDatabaseTrait;
39 protected function setUp(): void
46 public function dataDefault()
60 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
65 'WithSubDirectory' => [
67 'SERVER_NAME' => 'friendica.local',
68 'REDIRECT_URI' => 'test/module/more',
69 'QUERY_STRING' => 'module/more',
78 'hostname' => 'friendica.local',
80 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
81 'url' => 'http://friendica.local/test',
88 'hostname' => 'friendica.local',
90 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
91 'url' => 'http://friendica.local/test',
94 'hostname' => 'friendica.local',
96 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
97 'url' => 'http://friendica.local/test',
101 'WithHttpsScheme' => [
103 'SERVER_NAME' => 'friendica.local',
104 'REDIRECT_URI' => 'test/module/more',
105 'QUERY_STRING' => 'module/more',
115 'hostname' => 'friendica.local',
117 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
118 'url' => 'https://friendica.local/test',
122 'WithoutQueryString' => [
124 'SERVER_NAME' => 'friendica.local',
125 'REDIRECT_URI' => 'test/more',
135 'hostname' => 'friendica.local',
136 'urlPath' => 'test/more',
137 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
138 'url' => 'https://friendica.local/test/more',
144 'SERVER_NAME' => 'friendica.local',
145 'SERVER_PORT' => '1234',
146 'REDIRECT_URI' => 'test/more',
156 'hostname' => 'friendica.local:1234',
157 'urlPath' => 'test/more',
158 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
159 'url' => 'https://friendica.local:1234/test/more',
165 'SERVER_NAME' => 'friendica.local',
166 'SERVER_PORT' => '443',
167 'REDIRECT_URI' => 'test/more',
176 'hostname' => 'friendica.local',
177 'urlPath' => 'test/more',
178 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
179 'url' => 'https://friendica.local/test/more',
185 'SERVER_NAME' => 'friendica.local',
186 'SERVER_PORT' => '80',
187 'REDIRECT_URI' => 'test/more',
196 'hostname' => 'friendica.local',
197 'urlPath' => 'test/more',
198 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
199 'url' => 'http://friendica.local/test/more',
206 public function dataSave()
211 'hostname' => 'friendica.local',
213 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
214 'url' => 'https://friendica.local/path',
218 'hostname' => 'friendica.local',
220 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
222 'url' => 'https://friendica.local/path',
226 'hostname' => 'friendica.old',
227 'urlPath' => 'is/old/path',
228 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
229 'url' => 'http://friendica.old/is/old/path',
233 'hostname' => 'friendica.local',
234 'urlPath' => 'new/path',
235 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
237 'url' => 'https://friendica.local/new/path',
241 'hostname' => 'friendica.old',
242 'urlPath' => 'is/old/path',
243 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
244 'url' => 'http://friendica.old/is/old/path',
252 'url' => 'http://friendica.old/is/old/path',
254 'changeHostname' => [
256 'hostname' => 'friendica.old',
257 'urlPath' => 'is/old/path',
258 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
259 'url' => 'http://friendica.old/is/old/path',
263 'hostname' => 'friendica.local',
267 'url' => 'http://friendica.local/is/old/path',
271 'hostname' => 'friendica.old',
272 'urlPath' => 'is/old/path',
273 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
274 'url' => 'http://friendica.old/is/old/path',
279 'urlPath' => 'new/path',
282 'url' => 'http://friendica.old/new/path',
284 'changeSSLPolicy' => [
286 'hostname' => 'friendica.old',
287 'urlPath' => 'is/old/path',
288 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
289 'url' => 'http://friendica.old/is/old/path',
295 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
297 'url' => 'https://friendica.old/is/old/path',
303 * Test the save() method
304 * @dataProvider dataSave
306 public function testSave($input, $save, $url)
308 $config = new DatabaseConfig($this->getDbInstance(), new Cache([
310 'hostname' => $input['hostname'] ?? null,
313 'urlpath' => $input['urlPath'] ?? null,
314 'ssl_policy' => $input['sslPolicy'] ?? null,
315 'url' => $input['url'] ?? null,
316 'force_ssl' => $input['force_ssl'] ?? null,
320 $baseUrl = new BaseURL($config, []);
322 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
324 self::assertEquals($url, $baseUrl->get());
328 * Test the saveByUrl() method
329 * @dataProvider dataSave
335 public function testSaveByUrl($input, $save, $url)
337 $config = new DatabaseConfig($this->getDbInstance(), new Cache([
339 'hostname' => $input['hostname'] ?? null,
342 'urlpath' => $input['urlPath'] ?? null,
343 'ssl_policy' => $input['sslPolicy'] ?? null,
344 'url' => $input['url'] ?? null,
345 'force_ssl' => $input['force_ssl'] ?? null,
349 $baseUrl = new BaseURL($config, []);
351 $baseUrl->saveByURL($url);
353 self::assertEquals($url, $baseUrl->get());
356 public function dataGetBaseUrl()
360 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
362 'url' => 'http://friendica.local/new/test',
363 'assert' => 'http://friendica.local/new/test',
365 'DefaultWithSSL' => [
366 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
368 'url' => 'http://friendica.local/new/test',
369 'assert' => 'https://friendica.local/new/test',
371 'SSLFullWithSSL' => [
372 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
374 'url' => 'http://friendica.local/new/test',
375 'assert' => 'http://friendica.local/new/test',
377 'SSLFullWithoutSSL' => [
378 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
380 'url' => 'https://friendica.local/new/test',
381 'assert' => 'https://friendica.local/new/test',
384 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
386 'url' => 'http://friendica.local/new/test',
387 'assert' => 'http://friendica.local/new/test',
389 'NoSSLWithoutSSL' => [
390 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
392 'url' => 'http://friendica.local/new/test',
393 'assert' => 'http://friendica.local/new/test',
399 * Test the get() method
400 * @dataProvider dataGetBaseUrl
402 public function testGetURL($sslPolicy, $ssl, $url, $assert)
404 $configMock = \Mockery::mock(IManageConfigValues::class);
405 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
406 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
407 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
408 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
410 $baseUrl = new BaseURL($configMock, []);
412 self::assertEquals($assert, $baseUrl->get($ssl));
415 public function dataCheckRedirectHTTPS()
420 'REQUEST_METHOD' => 'GET',
424 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
425 'url' => 'https://friendica.local',
430 'REQUEST_METHOD' => 'GET',
433 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
434 'url' => 'https://friendica.local',
437 'forceSSLWithSSLPolicy' => [
440 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
441 'url' => 'https://friendica.local',
444 'forceSSLWithSSLPolicyAndGet' => [
446 'REQUEST_METHOD' => 'GET',
449 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
450 'url' => 'https://friendica.local',
457 * Test the checkRedirectHTTPS() method
458 * @dataProvider dataCheckRedirectHTTPS
460 public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
462 $configMock = \Mockery::mock(IManageConfigValues::class);
463 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
464 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
465 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
466 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
467 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL);
469 $baseUrl = new BaseURL($configMock, $server);
471 self::assertEquals($redirect, $baseUrl->checkRedirectHttps());