]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/BaseURLTest.php
Merge remote-tracking branch 'upstream/develop' into blocked-server
[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         /**
204          * Test the default config determination
205          * @dataProvider dataDefault
206          */
207         public function testCheck($server, $input, $assert)
208         {
209                 $configMock = \Mockery::mock(IManageConfigValues::class);
210                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
211                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
212                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
213                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
214
215                 // If we don't have an urlPath as an input, we assert it, we will save it to the DB for the next time
216                 if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
217                         $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
218                 }
219
220                 // If we don't have the ssl_policy as an input, we assert it, we will save it to the DB for the next time
221                 if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
222                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
223                 }
224
225                 // If we don't have the hostname as an input, we assert it, we will save it to the DB for the next time
226                 if (empty($input['hostname']) && !empty($assert['hostname'])) {
227                         $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
228                 }
229
230                 // If we don't have an URL at first, but we assert it, we will save it to the DB for the next time
231                 if (empty($input['url']) && !empty($assert['url'])) {
232                         $configMock->shouldReceive('set')->with('system', 'url', $assert['url'])->once();
233                 }
234
235                 $baseUrl = new BaseURL($configMock, $server);
236
237                 self::assertEquals($assert['hostname'], $baseUrl->getHostname());
238                 self::assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
239                 self::assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
240                 self::assertEquals($assert['scheme'], $baseUrl->getScheme());
241                 self::assertEquals($assert['url'], $baseUrl->get());
242         }
243
244         public function dataSave()
245         {
246                 return [
247                         'no_change' => [
248                                 'input' => [
249                                         'hostname'  => 'friendica.local',
250                                         'urlPath'   => 'path',
251                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
252                                         'url'       => 'https://friendica.local/path',
253                                         'force_ssl' => true,
254                                 ],
255                                 'save' => [
256                                         'hostname'  => 'friendica.local',
257                                         'urlPath'   => 'path',
258                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
259                                 ],
260                                 'url' => 'https://friendica.local/path',
261                         ],
262                         'default' => [
263                                 'input' => [
264                                         'hostname'  => 'friendica.old',
265                                         'urlPath'   => 'is/old/path',
266                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
267                                         'url'       => 'http://friendica.old/is/old/path',
268                                         'force_ssl' => true,
269                                 ],
270                                 'save' => [
271                                         'hostname'  => 'friendica.local',
272                                         'urlPath'   => 'new/path',
273                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
274                                 ],
275                                 'url' => 'https://friendica.local/new/path',
276                         ],
277                         'null' => [
278                                 'input' => [
279                                         'hostname'  => 'friendica.old',
280                                         'urlPath'   => 'is/old/path',
281                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
282                                         'url'       => 'http://friendica.old/is/old/path',
283                                         'force_ssl' => true,
284                                 ],
285                                 'save' => [
286                                         'hostname'  => null,
287                                         'urlPath'   => null,
288                                         'sslPolicy' => null,
289                                 ],
290                                 'url' => 'http://friendica.old/is/old/path',
291                         ],
292                         'changeHostname' => [
293                                 'input' => [
294                                         'hostname'  => 'friendica.old',
295                                         'urlPath'   => 'is/old/path',
296                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
297                                         'url'       => 'http://friendica.old/is/old/path',
298                                         'force_ssl' => true,
299                                 ],
300                                 'save' => [
301                                         'hostname'  => 'friendica.local',
302                                         'urlPath'   => null,
303                                         'sslPolicy' => null,
304                                 ],
305                                 'url' => 'http://friendica.local/is/old/path',
306                         ],
307                         'changeUrlPath' => [
308                                 'input' => [
309                                         'hostname'  => 'friendica.old',
310                                         'urlPath'   => 'is/old/path',
311                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
312                                         'url'       => 'http://friendica.old/is/old/path',
313                                         'force_ssl' => true,
314                                 ],
315                                 'save' => [
316                                         'hostname'  => null,
317                                         'urlPath'   => 'new/path',
318                                         'sslPolicy' => null,
319                                 ],
320                                 'url' => 'http://friendica.old/new/path',
321                         ],
322                         'changeSSLPolicy' => [
323                                 'input' => [
324                                         'hostname'  => 'friendica.old',
325                                         'urlPath'   => 'is/old/path',
326                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
327                                         'url'       => 'http://friendica.old/is/old/path',
328                                         'force_ssl' => true,
329                                 ],
330                                 'save' => [
331                                         'hostname'  => null,
332                                         'urlPath'   => null,
333                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
334                                 ],
335                                 'url' => 'https://friendica.old/is/old/path',
336                         ],
337                 ];
338         }
339
340         /**
341          * Test the save() method
342          * @dataProvider dataSave
343          */
344         public function testSave($input, $save, $url)
345         {
346                 $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
347                 $config = new Config($configFileManager, new Cache([
348                         'config' => [
349                                 'hostname' => $input['hostname'] ?? null,
350                         ],
351                         'system' => [
352                                 'urlpath' => $input['urlPath'] ?? null,
353                                 'ssl_policy' => $input['sslPolicy'] ?? null,
354                                 'url' => $input['url'] ?? null,
355                                 'force_ssl' => $input['force_ssl'] ?? null,
356                         ],
357                 ]));
358
359                 $baseUrl = new BaseURL($config, []);
360
361                 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
362
363                 self::assertEquals($url, $baseUrl->get());
364         }
365
366         /**
367          * Test the saveByUrl() method
368          * @dataProvider dataSave
369          *
370          * @param $input
371          * @param $save
372          * @param $url
373          */
374         public function testSaveByUrl($input, $save, $url)
375         {
376                 $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
377                 $config = new Config($configFileManager, new Cache([
378                         'config' => [
379                                 'hostname' => $input['hostname'] ?? null,
380                         ],
381                         'system' => [
382                                 'urlpath' => $input['urlPath'] ?? null,
383                                 'ssl_policy' => $input['sslPolicy'] ?? null,
384                                 'url' => $input['url'] ?? null,
385                                 'force_ssl' => $input['force_ssl'] ?? null,
386                         ],
387                 ]));
388
389                 $baseUrl = new BaseURL($config, []);
390
391                 $baseUrl->saveByURL($url);
392
393                 self::assertEquals($url, $baseUrl->get());
394         }
395
396         public function dataGetBaseUrl()
397         {
398                 return [
399                         'default'           => [
400                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
401                                 'ssl'       => false,
402                                 'url'       => 'http://friendica.local/new/test',
403                                 'assert'    => 'http://friendica.local/new/test',
404                         ],
405                         'DefaultWithSSL'    => [
406                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
407                                 'ssl'       => true,
408                                 'url'       => 'http://friendica.local/new/test',
409                                 'assert'    => 'https://friendica.local/new/test',
410                         ],
411                         'SSLFullWithSSL'    => [
412                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
413                                 'ssl'       => true,
414                                 'url'       => 'http://friendica.local/new/test',
415                                 'assert'    => 'http://friendica.local/new/test',
416                         ],
417                         'SSLFullWithoutSSL' => [
418                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
419                                 'ssl'       => false,
420                                 'url'       => 'https://friendica.local/new/test',
421                                 'assert'    => 'https://friendica.local/new/test',
422                         ],
423                         'NoSSLWithSSL'      => [
424                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
425                                 'ssl'       => true,
426                                 'url'       => 'http://friendica.local/new/test',
427                                 'assert'    => 'http://friendica.local/new/test',
428                         ],
429                         'NoSSLWithoutSSL'   => [
430                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
431                                 'ssl'       => false,
432                                 'url'       => 'http://friendica.local/new/test',
433                                 'assert'    => 'http://friendica.local/new/test',
434                         ],
435                 ];
436         }
437
438         /**
439          * Test the get() method
440          * @dataProvider dataGetBaseUrl
441          */
442         public function testGetURL($sslPolicy, $ssl, $url, $assert)
443         {
444                 $configMock = \Mockery::mock(IManageConfigValues::class);
445                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
446                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
447                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
448                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
449
450                 $baseUrl = new BaseURL($configMock, []);
451
452                 self::assertEquals($assert, $baseUrl->get($ssl));
453         }
454
455         public function dataCheckRedirectHTTPS()
456         {
457                 return [
458                         'default' => [
459                                 'server' => [
460                                         'REQUEST_METHOD' => 'GET',
461                                         'HTTPS' => true,
462                                 ],
463                                 'forceSSL'  => false,
464                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
465                                 'url'       => 'https://friendica.local',
466                                 'redirect'  => false,
467                         ],
468                         'forceSSL' => [
469                                 'server' => [
470                                         'REQUEST_METHOD' => 'GET',
471                                 ],
472                                 'forceSSL'  => true,
473                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
474                                 'url'       => 'https://friendica.local',
475                                 'redirect'  => false,
476                         ],
477                         'forceSSLWithSSLPolicy' => [
478                                 'server' => [],
479                                 'forceSSL'  => true,
480                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
481                                 'url'       => 'https://friendica.local',
482                                 'redirect'  => false,
483                         ],
484                         'forceSSLWithSSLPolicyAndGet' => [
485                                 'server' => [
486                                         'REQUEST_METHOD' => 'GET',
487                                 ],
488                                 'forceSSL'  => true,
489                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
490                                 'url'       => 'https://friendica.local',
491                                 'redirect'  => true,
492                         ],
493                 ];
494         }
495
496         /**
497          * Test the checkRedirectHTTPS() method
498          * @dataProvider dataCheckRedirectHTTPS
499          */
500         public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
501         {
502                 $configMock = \Mockery::mock(IManageConfigValues::class);
503                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
504                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
505                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
506                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
507                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL);
508
509                 $baseUrl = new BaseURL($configMock, $server);
510
511                 self::assertEquals($redirect, $baseUrl->checkRedirectHttps());
512         }
513 }