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