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\App;
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Config\Model\ReadOnlyFileConfig;
26 use Friendica\Core\Config\ValueObject\Cache;
27 use Friendica\Network\HTTPException\InternalServerErrorException;
28 use Friendica\Test\MockedTest;
29 use Psr\Log\NullLogger;
31 class BaseURLTest extends MockedTest
33 public function dataSystemUrl(): array
37 'input' => ['system' => ['url' => 'https://friendica.local',],],
39 'assertion' => 'https://friendica.local',
42 'input' => ['system' => ['url' => 'https://friendica.local/subpath',],],
44 'assertion' => 'https://friendica.local/subpath',
49 'assertion' => 'http://localhost',
51 'serverArrayStandard' => [
55 'HTTP_HOST' => 'friendica.server',
56 'REQUEST_URI' => '/test/it?with=query',
57 'QUERY_STRING' => 'pagename=test/it',
59 'assertion' => 'https://friendica.server',
61 'serverArraySubPath' => [
65 'HTTP_HOST' => 'friendica.server',
66 'REQUEST_URI' => '/test/it/now?with=query',
67 'QUERY_STRING' => 'pagename=it/now',
69 'assertion' => 'https://friendica.server/test',
71 'serverArraySubPath2' => [
75 'HTTP_HOST' => 'friendica.server',
76 'REQUEST_URI' => '/test/it/now?with=query',
77 'QUERY_STRING' => 'pagename=now',
79 'assertion' => 'https://friendica.server/test/it',
81 'serverArraySubPath3' => [
85 'HTTP_HOST' => 'friendica.server',
86 'REQUEST_URI' => '/test/it/now?with=query',
87 'QUERY_STRING' => 'pagename=test/it/now',
89 'assertion' => 'https://friendica.server',
91 'serverArrayWithoutQueryString1' => [
95 'HTTP_HOST' => 'friendica.server',
96 'REQUEST_URI' => '/test/it/now?with=query',
98 'assertion' => 'https://friendica.server/test/it/now',
100 'serverArrayWithoutQueryString2' => [
104 'HTTP_HOST' => 'friendica.server',
107 'assertion' => 'https://friendica.server',
109 'serverArrayWithoutQueryString3' => [
113 'HTTP_HOST' => 'friendica.server',
114 'REQUEST_URI' => '/',
116 'assertion' => 'https://friendica.server',
122 * @dataProvider dataSystemUrl
124 public function testDetermine(array $input, array $server, string $assertion)
126 $origServerGlobal = $_SERVER;
128 $_SERVER = array_merge_recursive($_SERVER, $server);
129 $config = new ReadOnlyFileConfig(new Cache($input));
131 $baseUrl = new BaseURL($config, new NullLogger(), $server);
133 self::assertEquals($assertion, (string)$baseUrl);
135 $_SERVER = $origServerGlobal;
138 public function dataRemove(): array
142 'base' => ['system' => ['url' => 'https://friendica.local',],],
143 'origUrl' => 'https://friendica.local/test/picture.png',
144 'assertion' => 'test/picture.png',
147 'base' => ['system' => ['url' => 'https://friendica.local',],],
148 'origUrl' => 'https://friendica.other/test/picture.png',
149 'assertion' => 'https://friendica.other/test/picture.png',
152 'base' => ['system' => ['url' => 'https://friendica.local/test',],],
153 'origUrl' => 'https://friendica.local/test/picture.png',
154 'assertion' => 'picture.png',
157 'base' => ['system' => ['url' => 'https://friendica.local/test',],],
158 'origUrl' => 'https://friendica.other/test/picture.png',
159 'assertion' => 'https://friendica.other/test/picture.png',
165 * @dataProvider dataRemove
167 public function testRemove(array $base, string $origUrl, string $assertion)
169 $config = new ReadOnlyFileConfig(new Cache($base));
170 $baseUrl = new BaseURL($config, new NullLogger());
172 self::assertEquals($assertion, $baseUrl->remove($origUrl));
176 * Test that redirect to external domains fails
178 public function testRedirectException()
180 self::expectException(InternalServerErrorException::class);
181 self::expectErrorMessage('https://friendica.other is not a relative path, please use System::externalRedirect');
183 $config = new ReadOnlyFileConfig(new Cache([
185 'url' => 'https://friendica.local',
188 $baseUrl = new BaseURL($config, new NullLogger());
189 $baseUrl->redirect('https://friendica.other');