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\Config;
27 use Friendica\Core\Config\Util\ConfigFileManager;
28 use Friendica\Core\Config\ValueObject\Cache;
29 use Friendica\Test\MockedTest;
30 use Friendica\Test\Util\VFSTrait;
32 class BaseURLTest extends MockedTest
36 protected function setUp(): void
43 public function dataDefault()
57 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
62 'WithSubDirectory' => [
64 'SERVER_NAME' => 'friendica.local',
65 'REDIRECT_URI' => 'test/module/more',
66 'QUERY_STRING' => 'module/more',
75 'hostname' => 'friendica.local',
77 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
78 'url' => 'http://friendica.local/test',
85 'hostname' => 'friendica.local',
87 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
88 'url' => 'http://friendica.local/test',
91 'hostname' => 'friendica.local',
93 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
94 'url' => 'http://friendica.local/test',
98 'WithHttpsScheme' => [
100 'SERVER_NAME' => 'friendica.local',
101 'REDIRECT_URI' => 'test/module/more',
102 'QUERY_STRING' => 'module/more',
112 'hostname' => 'friendica.local',
114 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
115 'url' => 'https://friendica.local/test',
119 'WithoutQueryString' => [
121 'SERVER_NAME' => 'friendica.local',
122 'REDIRECT_URI' => 'test/more',
132 'hostname' => 'friendica.local',
133 'urlPath' => 'test/more',
134 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
135 'url' => 'https://friendica.local/test/more',
141 'SERVER_NAME' => 'friendica.local',
142 'SERVER_PORT' => '1234',
143 'REDIRECT_URI' => 'test/more',
153 'hostname' => 'friendica.local:1234',
154 'urlPath' => 'test/more',
155 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
156 'url' => 'https://friendica.local:1234/test/more',
162 'SERVER_NAME' => 'friendica.local',
163 'SERVER_PORT' => '443',
164 'REDIRECT_URI' => 'test/more',
173 'hostname' => 'friendica.local',
174 'urlPath' => 'test/more',
175 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
176 'url' => 'https://friendica.local/test/more',
182 'SERVER_NAME' => 'friendica.local',
183 'SERVER_PORT' => '80',
184 'REDIRECT_URI' => 'test/more',
193 'hostname' => 'friendica.local',
194 'urlPath' => 'test/more',
195 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
196 'url' => 'http://friendica.local/test/more',
203 public function dataSave()
208 'hostname' => 'friendica.local',
210 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
211 'url' => 'https://friendica.local/path',
215 'hostname' => 'friendica.local',
217 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
219 'url' => 'https://friendica.local/path',
223 'hostname' => 'friendica.old',
224 'urlPath' => 'is/old/path',
225 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
226 'url' => 'http://friendica.old/is/old/path',
230 'hostname' => 'friendica.local',
231 'urlPath' => 'new/path',
232 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
234 'url' => 'https://friendica.local/new/path',
238 'hostname' => 'friendica.old',
239 'urlPath' => 'is/old/path',
240 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
241 'url' => 'http://friendica.old/is/old/path',
249 'url' => 'http://friendica.old/is/old/path',
251 'changeHostname' => [
253 'hostname' => 'friendica.old',
254 'urlPath' => 'is/old/path',
255 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
256 'url' => 'http://friendica.old/is/old/path',
260 'hostname' => 'friendica.local',
264 'url' => 'http://friendica.local/is/old/path',
268 'hostname' => 'friendica.old',
269 'urlPath' => 'is/old/path',
270 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
271 'url' => 'http://friendica.old/is/old/path',
276 'urlPath' => 'new/path',
279 'url' => 'http://friendica.old/new/path',
281 'changeSSLPolicy' => [
283 'hostname' => 'friendica.old',
284 'urlPath' => 'is/old/path',
285 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
286 'url' => 'http://friendica.old/is/old/path',
292 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
294 'url' => 'https://friendica.old/is/old/path',
300 * Test the save() method
301 * @dataProvider dataSave
303 public function testSave($input, $save, $url)
305 $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
306 $config = new Config($configFileManager, new Cache([
308 'hostname' => $input['hostname'] ?? null,
311 'urlpath' => $input['urlPath'] ?? null,
312 'ssl_policy' => $input['sslPolicy'] ?? null,
313 'url' => $input['url'] ?? null,
314 'force_ssl' => $input['force_ssl'] ?? null,
318 $baseUrl = new BaseURL($config, []);
320 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
322 self::assertEquals($url, $baseUrl->get());
326 * Test the saveByUrl() method
327 * @dataProvider dataSave
333 public function testSaveByUrl($input, $save, $url)
335 $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
336 $config = new Config($configFileManager, new Cache([
338 'hostname' => $input['hostname'] ?? null,
341 'urlpath' => $input['urlPath'] ?? null,
342 'ssl_policy' => $input['sslPolicy'] ?? null,
343 'url' => $input['url'] ?? null,
344 'force_ssl' => $input['force_ssl'] ?? null,
348 $baseUrl = new BaseURL($config, []);
350 $baseUrl->saveByURL($url);
352 self::assertEquals($url, $baseUrl->get());
355 public function dataGetBaseUrl()
359 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
361 'url' => 'http://friendica.local/new/test',
362 'assert' => 'http://friendica.local/new/test',
364 'DefaultWithSSL' => [
365 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
367 'url' => 'http://friendica.local/new/test',
368 'assert' => 'https://friendica.local/new/test',
370 'SSLFullWithSSL' => [
371 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
373 'url' => 'http://friendica.local/new/test',
374 'assert' => 'http://friendica.local/new/test',
376 'SSLFullWithoutSSL' => [
377 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
379 'url' => 'https://friendica.local/new/test',
380 'assert' => 'https://friendica.local/new/test',
383 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
385 'url' => 'http://friendica.local/new/test',
386 'assert' => 'http://friendica.local/new/test',
388 'NoSSLWithoutSSL' => [
389 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
391 'url' => 'http://friendica.local/new/test',
392 'assert' => 'http://friendica.local/new/test',
398 * Test the get() method
399 * @dataProvider dataGetBaseUrl
401 public function testGetURL($sslPolicy, $ssl, $url, $assert)
403 $configMock = \Mockery::mock(IManageConfigValues::class);
404 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
405 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
406 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
407 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
409 $baseUrl = new BaseURL($configMock, []);
411 self::assertEquals($assert, $baseUrl->get($ssl));
414 public function dataCheckRedirectHTTPS()
419 'REQUEST_METHOD' => 'GET',
423 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
424 'url' => 'https://friendica.local',
429 'REQUEST_METHOD' => 'GET',
432 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
433 'url' => 'https://friendica.local',
436 'forceSSLWithSSLPolicy' => [
439 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
440 'url' => 'https://friendica.local',
443 'forceSSLWithSSLPolicyAndGet' => [
445 'REQUEST_METHOD' => 'GET',
448 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
449 'url' => 'https://friendica.local',
456 * Test the checkRedirectHTTPS() method
457 * @dataProvider dataCheckRedirectHTTPS
459 public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
461 $configMock = \Mockery::mock(IManageConfigValues::class);
462 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
463 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
464 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
465 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
466 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL);
468 $baseUrl = new BaseURL($configMock, $server);
470 self::assertEquals($redirect, $baseUrl->checkRedirectHttps());