]> git.mxchange.org Git - friendica.git/blob - tests/src/App/BaseURLTest.php
spelling: cached
[friendica.git] / tests / src / App / BaseURLTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\App;
23
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;
30
31 class BaseURLTest extends MockedTest
32 {
33         public function dataSystemUrl(): array
34         {
35                 return [
36                         'default' => [
37                                 'input'     => ['system' => ['url' => 'https://friendica.local',],],
38                                 'server'    => [],
39                                 'assertion' => 'https://friendica.local',
40                         ],
41                         'subPath' => [
42                                 'input'     => ['system' => ['url' => 'https://friendica.local/subpath',],],
43                                 'server'    => [],
44                                 'assertion' => 'https://friendica.local/subpath',
45                         ],
46                         'empty' => [
47                                 'input'     => [],
48                                 'server'    => [],
49                                 'assertion' => 'http://localhost',
50                         ],
51                         'serverArrayStandard' => [
52                                 'input'  => [],
53                                 'server' => [
54                                         'HTTPS'        => 'on',
55                                         'HTTP_HOST'    => 'friendica.server',
56                                         'REQUEST_URI'  => '/test/it?with=query',
57                                         'QUERY_STRING' => 'pagename=test/it',
58                                 ],
59                                 'assertion' => 'https://friendica.server',
60                         ],
61                         'serverArraySubPath' => [
62                                 'input'  => [],
63                                 'server' => [
64                                         'HTTPS'        => 'on',
65                                         'HTTP_HOST'    => 'friendica.server',
66                                         'REQUEST_URI'  => '/test/it/now?with=query',
67                                         'QUERY_STRING' => 'pagename=it/now',
68                                 ],
69                                 'assertion' => 'https://friendica.server/test',
70                         ],
71                         'serverArraySubPath2' => [
72                                 'input'  => [],
73                                 'server' => [
74                                         'HTTPS'        => 'on',
75                                         'HTTP_HOST'    => 'friendica.server',
76                                         'REQUEST_URI'  => '/test/it/now?with=query',
77                                         'QUERY_STRING' => 'pagename=now',
78                                 ],
79                                 'assertion' => 'https://friendica.server/test/it',
80                         ],
81                         'serverArraySubPath3' => [
82                                 'input'  => [],
83                                 'server' => [
84                                         'HTTPS'        => 'on',
85                                         'HTTP_HOST'    => 'friendica.server',
86                                         'REQUEST_URI'  => '/test/it/now?with=query',
87                                         'QUERY_STRING' => 'pagename=test/it/now',
88                                 ],
89                                 'assertion' => 'https://friendica.server',
90                         ],
91                         'serverArrayWithoutQueryString1' => [
92                                 'input'  => [],
93                                 'server' => [
94                                         'HTTPS'       => 'on',
95                                         'HTTP_HOST'   => 'friendica.server',
96                                         'REQUEST_URI' => '/test/it/now?with=query',
97                                 ],
98                                 'assertion' => 'https://friendica.server/test/it/now',
99                         ],
100                         'serverArrayWithoutQueryString2' => [
101                                 'input'  => [],
102                                 'server' => [
103                                         'HTTPS'       => 'on',
104                                         'HTTP_HOST'   => 'friendica.server',
105                                         'REQUEST_URI' => '',
106                                 ],
107                                 'assertion' => 'https://friendica.server',
108                         ],
109                         'serverArrayWithoutQueryString3' => [
110                                 'input'  => [],
111                                 'server' => [
112                                         'HTTPS'       => 'on',
113                                         'HTTP_HOST'   => 'friendica.server',
114                                         'REQUEST_URI' => '/',
115                                 ],
116                                 'assertion' => 'https://friendica.server',
117                         ],
118                 ];
119         }
120
121         /**
122          * @dataProvider dataSystemUrl
123          */
124         public function testDetermine(array $input, array $server, string $assertion)
125         {
126                 $origServerGlobal = $_SERVER;
127
128                 $_SERVER = array_merge_recursive($_SERVER, $server);
129                 $config  = new ReadOnlyFileConfig(new Cache($input));
130
131                 $baseUrl = new BaseURL($config, new NullLogger(), $server);
132
133                 self::assertEquals($assertion, (string)$baseUrl);
134
135                 $_SERVER = $origServerGlobal;
136         }
137
138         public function dataRemove(): array
139         {
140                 return [
141                         'same' => [
142                                 'base'      => ['system' => ['url' => 'https://friendica.local',],],
143                                 'origUrl'   => 'https://friendica.local/test/picture.png',
144                                 'assertion' => 'test/picture.png',
145                         ],
146                         'other' => [
147                                 'base'      => ['system' => ['url' => 'https://friendica.local',],],
148                                 'origUrl'   => 'https://friendica.other/test/picture.png',
149                                 'assertion' => 'https://friendica.other/test/picture.png',
150                         ],
151                         'samSubPath' => [
152                                 'base'      => ['system' => ['url' => 'https://friendica.local/test',],],
153                                 'origUrl'   => 'https://friendica.local/test/picture.png',
154                                 'assertion' => 'picture.png',
155                         ],
156                         'otherSubPath' => [
157                                 'base'      => ['system' => ['url' => 'https://friendica.local/test',],],
158                                 'origUrl'   => 'https://friendica.other/test/picture.png',
159                                 'assertion' => 'https://friendica.other/test/picture.png',
160                         ],
161                 ];
162         }
163
164         /**
165          * @dataProvider dataRemove
166          */
167         public function testRemove(array $base, string $origUrl, string $assertion)
168         {
169                 $config  = new ReadOnlyFileConfig(new Cache($base));
170                 $baseUrl = new BaseURL($config, new NullLogger());
171
172                 self::assertEquals($assertion, $baseUrl->remove($origUrl));
173         }
174
175         /**
176          * Test that redirect to external domains fails
177          */
178         public function testRedirectException()
179         {
180                 self::expectException(InternalServerErrorException::class);
181                 self::expectErrorMessage('https://friendica.other is not a relative path, please use System::externalRedirect');
182
183                 $config = new ReadOnlyFileConfig(new Cache([
184                         'system' => [
185                                 'url' => 'https://friendica.local',
186                         ]
187                 ]));
188                 $baseUrl = new BaseURL($config, new NullLogger());
189                 $baseUrl->redirect('https://friendica.other');
190         }
191 }