]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/BaseURLTest.php
Merge remote-tracking branch 'upstream/develop' into api
[friendica.git] / tests / src / Util / 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\Util;
23
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;
31
32 class BaseURLTest extends MockedTest
33 {
34         use VFSTrait;
35
36         protected function setUp(): void
37         {
38                 parent::setUp();
39
40                 $this->setUpVfsDir();
41         }
42
43         public function dataDefault()
44         {
45                 return [
46                         'null' => [
47                                 'server' => [],
48                                 'input' => [
49                                 'hostname' => null,
50                                 'urlPath' => null,
51                                 'sslPolicy' => null,
52                                 'url' => null,
53                                         ],
54                                 'assert' => [
55                                         'hostname'  => '',
56                                         'urlPath'   => '',
57                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
58                                         'url'       => 'http://',
59                                         'scheme'    => 'http',
60                                 ],
61                         ],
62                         'WithSubDirectory' => [
63                                 'server' => [
64                                         'SERVER_NAME'  => 'friendica.local',
65                                         'REDIRECT_URI' => 'test/module/more',
66                                         'QUERY_STRING' => 'module/more',
67                                 ],
68                                 'input' => [
69                                         'hostname'  => null,
70                                         'urlPath'   => null,
71                                         'sslPolicy' => null,
72                                         'url'       => null,
73                                 ],
74                                 'assert' => [
75                                         'hostname'  => 'friendica.local',
76                                         'urlPath'   => 'test',
77                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
78                                         'url'       => 'http://friendica.local/test',
79                                         'scheme'    => 'http',
80                                 ],
81                         ],
82                         'input' => [
83                                 'server' => [],
84                                 'input' => [
85                                         'hostname'  => 'friendica.local',
86                                         'urlPath'   => 'test',
87                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
88                                         'url'       => 'http://friendica.local/test',
89                                 ],
90                                 'assert' => [
91                                         'hostname'  => 'friendica.local',
92                                         'urlPath'   => 'test',
93                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
94                                         'url'       => 'http://friendica.local/test',
95                                         'scheme'    => 'http',
96                                 ],
97                         ],
98                         'WithHttpsScheme' => [
99                                 'server' => [
100                                         'SERVER_NAME'    => 'friendica.local',
101                                         'REDIRECT_URI'   => 'test/module/more',
102                                         'QUERY_STRING'   => 'module/more',
103                                         'HTTPS'          => true,
104                                 ],
105                                 'input' => [
106                                         'hostname'  => null,
107                                         'urlPath'   => null,
108                                         'sslPolicy' => null,
109                                         'url'       => null,
110                                 ],
111                                 'assert' => [
112                                         'hostname'  => 'friendica.local',
113                                         'urlPath'   => 'test',
114                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
115                                         'url'       => 'https://friendica.local/test',
116                                         'scheme'    => 'https',
117                                 ],
118                         ],
119                         'WithoutQueryString' => [
120                                 'server' => [
121                                         'SERVER_NAME'    => 'friendica.local',
122                                         'REDIRECT_URI'   => 'test/more',
123                                         'HTTPS'          => true,
124                                 ],
125                                 'input' => [
126                                         'hostname'  => null,
127                                         'urlPath'   => null,
128                                         'sslPolicy' => null,
129                                         'url'       => null,
130                                 ],
131                                 'assert' => [
132                                         'hostname'  => 'friendica.local',
133                                         'urlPath'   => 'test/more',
134                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
135                                         'url'       => 'https://friendica.local/test/more',
136                                         'scheme'    => 'https',
137                                 ],
138                         ],
139                         'WithPort' => [
140                                 'server' => [
141                                         'SERVER_NAME'    => 'friendica.local',
142                                         'SERVER_PORT'    => '1234',
143                                         'REDIRECT_URI'   => 'test/more',
144                                         'HTTPS'          => true,
145                                 ],
146                                 'input' => [
147                                         'hostname'  => null,
148                                         'urlPath'   => null,
149                                         'sslPolicy' => null,
150                                         'url'       => null,
151                                 ],
152                                 'assert' => [
153                                         'hostname'  => 'friendica.local:1234',
154                                         'urlPath'   => 'test/more',
155                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
156                                         'url'       => 'https://friendica.local:1234/test/more',
157                                         'scheme'    => 'https',
158                                 ],
159                         ],
160                         'With443Port' => [
161                                 'server' => [
162                                         'SERVER_NAME'    => 'friendica.local',
163                                         'SERVER_PORT'    => '443',
164                                         'REDIRECT_URI'   => 'test/more',
165                                 ],
166                                 'input' => [
167                                         'hostname'  => null,
168                                         'urlPath'   => null,
169                                         'sslPolicy' => null,
170                                         'url'       => null,
171                                 ],
172                                 'assert' => [
173                                         'hostname'  => 'friendica.local',
174                                         'urlPath'   => 'test/more',
175                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
176                                         'url'       => 'https://friendica.local/test/more',
177                                         'scheme'    => 'https',
178                                 ],
179                         ],
180                         'With80Port' => [
181                                 'server' => [
182                                         'SERVER_NAME'  => 'friendica.local',
183                                         'SERVER_PORT'  => '80',
184                                         'REDIRECT_URI' => 'test/more',
185                                 ],
186                                 'input' => [
187                                         'hostname'  => null,
188                                         'urlPath'   => null,
189                                         'sslPolicy' => null,
190                                         'url'       => null,
191                                 ],
192                                 'assert' => [
193                                         'hostname'  => 'friendica.local',
194                                         'urlPath'   => 'test/more',
195                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
196                                         'url'       => 'http://friendica.local/test/more',
197                                         'scheme'    => 'http',
198                                 ],
199                         ],
200                 ];
201         }
202
203         public function dataSave()
204         {
205                 return [
206                         'no_change' => [
207                                 'input' => [
208                                         'hostname'  => 'friendica.local',
209                                         'urlPath'   => 'path',
210                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
211                                         'url'       => 'https://friendica.local/path',
212                                         'force_ssl' => true,
213                                 ],
214                                 'save' => [
215                                         'hostname'  => 'friendica.local',
216                                         'urlPath'   => 'path',
217                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
218                                 ],
219                                 'url' => 'https://friendica.local/path',
220                         ],
221                         'default' => [
222                                 'input' => [
223                                         'hostname'  => 'friendica.old',
224                                         'urlPath'   => 'is/old/path',
225                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
226                                         'url'       => 'http://friendica.old/is/old/path',
227                                         'force_ssl' => true,
228                                 ],
229                                 'save' => [
230                                         'hostname'  => 'friendica.local',
231                                         'urlPath'   => 'new/path',
232                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
233                                 ],
234                                 'url' => 'https://friendica.local/new/path',
235                         ],
236                         'null' => [
237                                 'input' => [
238                                         'hostname'  => 'friendica.old',
239                                         'urlPath'   => 'is/old/path',
240                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
241                                         'url'       => 'http://friendica.old/is/old/path',
242                                         'force_ssl' => true,
243                                 ],
244                                 'save' => [
245                                         'hostname'  => null,
246                                         'urlPath'   => null,
247                                         'sslPolicy' => null,
248                                 ],
249                                 'url' => 'http://friendica.old/is/old/path',
250                         ],
251                         'changeHostname' => [
252                                 'input' => [
253                                         'hostname'  => 'friendica.old',
254                                         'urlPath'   => 'is/old/path',
255                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
256                                         'url'       => 'http://friendica.old/is/old/path',
257                                         'force_ssl' => true,
258                                 ],
259                                 'save' => [
260                                         'hostname'  => 'friendica.local',
261                                         'urlPath'   => null,
262                                         'sslPolicy' => null,
263                                 ],
264                                 'url' => 'http://friendica.local/is/old/path',
265                         ],
266                         'changeUrlPath' => [
267                                 'input' => [
268                                         'hostname'  => 'friendica.old',
269                                         'urlPath'   => 'is/old/path',
270                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
271                                         'url'       => 'http://friendica.old/is/old/path',
272                                         'force_ssl' => true,
273                                 ],
274                                 'save' => [
275                                         'hostname'  => null,
276                                         'urlPath'   => 'new/path',
277                                         'sslPolicy' => null,
278                                 ],
279                                 'url' => 'http://friendica.old/new/path',
280                         ],
281                         'changeSSLPolicy' => [
282                                 'input' => [
283                                         'hostname'  => 'friendica.old',
284                                         'urlPath'   => 'is/old/path',
285                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
286                                         'url'       => 'http://friendica.old/is/old/path',
287                                         'force_ssl' => true,
288                                 ],
289                                 'save' => [
290                                         'hostname'  => null,
291                                         'urlPath'   => null,
292                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
293                                 ],
294                                 'url' => 'https://friendica.old/is/old/path',
295                         ],
296                 ];
297         }
298
299         /**
300          * Test the save() method
301          * @dataProvider dataSave
302          */
303         public function testSave($input, $save, $url)
304         {
305                 $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
306                 $config = new Config($configFileManager, new Cache([
307                         'config' => [
308                                 'hostname' => $input['hostname'] ?? null,
309                         ],
310                         'system' => [
311                                 'urlpath' => $input['urlPath'] ?? null,
312                                 'ssl_policy' => $input['sslPolicy'] ?? null,
313                                 'url' => $input['url'] ?? null,
314                                 'force_ssl' => $input['force_ssl'] ?? null,
315                         ],
316                 ]));
317
318                 $baseUrl = new BaseURL($config, []);
319
320                 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
321
322                 self::assertEquals($url, $baseUrl->get());
323         }
324
325         /**
326          * Test the saveByUrl() method
327          * @dataProvider dataSave
328          *
329          * @param $input
330          * @param $save
331          * @param $url
332          */
333         public function testSaveByUrl($input, $save, $url)
334         {
335                 $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
336                 $config = new Config($configFileManager, new Cache([
337                         'config' => [
338                                 'hostname' => $input['hostname'] ?? null,
339                         ],
340                         'system' => [
341                                 'urlpath' => $input['urlPath'] ?? null,
342                                 'ssl_policy' => $input['sslPolicy'] ?? null,
343                                 'url' => $input['url'] ?? null,
344                                 'force_ssl' => $input['force_ssl'] ?? null,
345                         ],
346                 ]));
347
348                 $baseUrl = new BaseURL($config, []);
349
350                 $baseUrl->saveByURL($url);
351
352                 self::assertEquals($url, $baseUrl->get());
353         }
354
355         public function dataGetBaseUrl()
356         {
357                 return [
358                         'default'           => [
359                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
360                                 'ssl'       => false,
361                                 'url'       => 'http://friendica.local/new/test',
362                                 'assert'    => 'http://friendica.local/new/test',
363                         ],
364                         'DefaultWithSSL'    => [
365                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
366                                 'ssl'       => true,
367                                 'url'       => 'http://friendica.local/new/test',
368                                 'assert'    => 'https://friendica.local/new/test',
369                         ],
370                         'SSLFullWithSSL'    => [
371                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
372                                 'ssl'       => true,
373                                 'url'       => 'http://friendica.local/new/test',
374                                 'assert'    => 'http://friendica.local/new/test',
375                         ],
376                         'SSLFullWithoutSSL' => [
377                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
378                                 'ssl'       => false,
379                                 'url'       => 'https://friendica.local/new/test',
380                                 'assert'    => 'https://friendica.local/new/test',
381                         ],
382                         'NoSSLWithSSL'      => [
383                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
384                                 'ssl'       => true,
385                                 'url'       => 'http://friendica.local/new/test',
386                                 'assert'    => 'http://friendica.local/new/test',
387                         ],
388                         'NoSSLWithoutSSL'   => [
389                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
390                                 'ssl'       => false,
391                                 'url'       => 'http://friendica.local/new/test',
392                                 'assert'    => 'http://friendica.local/new/test',
393                         ],
394                 ];
395         }
396
397         /**
398          * Test the get() method
399          * @dataProvider dataGetBaseUrl
400          */
401         public function testGetURL($sslPolicy, $ssl, $url, $assert)
402         {
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);
408
409                 $baseUrl = new BaseURL($configMock, []);
410
411                 self::assertEquals($assert, $baseUrl->get($ssl));
412         }
413
414         public function dataCheckRedirectHTTPS()
415         {
416                 return [
417                         'default' => [
418                                 'server' => [
419                                         'REQUEST_METHOD' => 'GET',
420                                         'HTTPS' => true,
421                                 ],
422                                 'forceSSL'  => false,
423                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
424                                 'url'       => 'https://friendica.local',
425                                 'redirect'  => false,
426                         ],
427                         'forceSSL' => [
428                                 'server' => [
429                                         'REQUEST_METHOD' => 'GET',
430                                 ],
431                                 'forceSSL'  => true,
432                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
433                                 'url'       => 'https://friendica.local',
434                                 'redirect'  => false,
435                         ],
436                         'forceSSLWithSSLPolicy' => [
437                                 'server' => [],
438                                 'forceSSL'  => true,
439                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
440                                 'url'       => 'https://friendica.local',
441                                 'redirect'  => false,
442                         ],
443                         'forceSSLWithSSLPolicyAndGet' => [
444                                 'server' => [
445                                         'REQUEST_METHOD' => 'GET',
446                                 ],
447                                 'forceSSL'  => true,
448                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
449                                 'url'       => 'https://friendica.local',
450                                 'redirect'  => true,
451                         ],
452                 ];
453         }
454
455         /**
456          * Test the checkRedirectHTTPS() method
457          * @dataProvider dataCheckRedirectHTTPS
458          */
459         public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
460         {
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);
467
468                 $baseUrl = new BaseURL($configMock, $server);
469
470                 self::assertEquals($redirect, $baseUrl->checkRedirectHttps());
471         }
472 }