]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/BaseURLTest.php
adapt tests
[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\Test\MockedTest;
27
28 class BaseURLTest extends MockedTest
29 {
30         public function dataDefault()
31         {
32                 return [
33                         'null' => [
34                                 'server' => [],
35                                 'input' => [
36                                 'hostname' => null,
37                                 'urlPath' => null,
38                                 'sslPolicy' => null,
39                                 'url' => null,
40                                         ],
41                                 'assert' => [
42                                         'hostname'  => '',
43                                         'urlPath'   => '',
44                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
45                                         'url'       => 'http://',
46                                         'scheme'    => 'http',
47                                 ],
48                         ],
49                         'WithSubDirectory' => [
50                                 'server' => [
51                                         'SERVER_NAME'  => 'friendica.local',
52                                         'REDIRECT_URI' => 'test/module/more',
53                                         'QUERY_STRING' => 'module/more',
54                                 ],
55                                 'input' => [
56                                         'hostname'  => null,
57                                         'urlPath'   => null,
58                                         'sslPolicy' => null,
59                                         'url'       => null,
60                                 ],
61                                 'assert' => [
62                                         'hostname'  => 'friendica.local',
63                                         'urlPath'   => 'test',
64                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
65                                         'url'       => 'http://friendica.local/test',
66                                         'scheme'    => 'http',
67                                 ],
68                         ],
69                         'input' => [
70                                 'server' => [],
71                                 'input' => [
72                                         'hostname'  => 'friendica.local',
73                                         'urlPath'   => 'test',
74                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
75                                         'url'       => 'http://friendica.local/test',
76                                 ],
77                                 'assert' => [
78                                         'hostname'  => 'friendica.local',
79                                         'urlPath'   => 'test',
80                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
81                                         'url'       => 'http://friendica.local/test',
82                                         'scheme'    => 'http',
83                                 ],
84                         ],
85                         'WithHttpsScheme' => [
86                                 'server' => [
87                                         'SERVER_NAME'    => 'friendica.local',
88                                         'REDIRECT_URI'   => 'test/module/more',
89                                         'QUERY_STRING'   => 'module/more',
90                                         'HTTPS'          => true,
91                                 ],
92                                 'input' => [
93                                         'hostname'  => null,
94                                         'urlPath'   => null,
95                                         'sslPolicy' => null,
96                                         'url'       => null,
97                                 ],
98                                 'assert' => [
99                                         'hostname'  => 'friendica.local',
100                                         'urlPath'   => 'test',
101                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
102                                         'url'       => 'https://friendica.local/test',
103                                         'scheme'    => 'https',
104                                 ],
105                         ],
106                         'WithoutQueryString' => [
107                                 'server' => [
108                                         'SERVER_NAME'    => 'friendica.local',
109                                         'REDIRECT_URI'   => 'test/more',
110                                         'HTTPS'          => true,
111                                 ],
112                                 'input' => [
113                                         'hostname'  => null,
114                                         'urlPath'   => null,
115                                         'sslPolicy' => null,
116                                         'url'       => null,
117                                 ],
118                                 'assert' => [
119                                         'hostname'  => 'friendica.local',
120                                         'urlPath'   => 'test/more',
121                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
122                                         'url'       => 'https://friendica.local/test/more',
123                                         'scheme'    => 'https',
124                                 ],
125                         ],
126                         'WithPort' => [
127                                 'server' => [
128                                         'SERVER_NAME'    => 'friendica.local',
129                                         'SERVER_PORT'    => '1234',
130                                         'REDIRECT_URI'   => 'test/more',
131                                         'HTTPS'          => true,
132                                 ],
133                                 'input' => [
134                                         'hostname'  => null,
135                                         'urlPath'   => null,
136                                         'sslPolicy' => null,
137                                         'url'       => null,
138                                 ],
139                                 'assert' => [
140                                         'hostname'  => 'friendica.local:1234',
141                                         'urlPath'   => 'test/more',
142                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
143                                         'url'       => 'https://friendica.local:1234/test/more',
144                                         'scheme'    => 'https',
145                                 ],
146                         ],
147                         'With443Port' => [
148                                 'server' => [
149                                         'SERVER_NAME'    => 'friendica.local',
150                                         'SERVER_PORT'    => '443',
151                                         'REDIRECT_URI'   => 'test/more',
152                                 ],
153                                 'input' => [
154                                         'hostname'  => null,
155                                         'urlPath'   => null,
156                                         'sslPolicy' => null,
157                                         'url'       => null,
158                                 ],
159                                 'assert' => [
160                                         'hostname'  => 'friendica.local',
161                                         'urlPath'   => 'test/more',
162                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
163                                         'url'       => 'https://friendica.local/test/more',
164                                         'scheme'    => 'https',
165                                 ],
166                         ],
167                         'With80Port' => [
168                                 'server' => [
169                                         'SERVER_NAME'  => 'friendica.local',
170                                         'SERVER_PORT'  => '80',
171                                         'REDIRECT_URI' => 'test/more',
172                                 ],
173                                 'input' => [
174                                         'hostname'  => null,
175                                         'urlPath'   => null,
176                                         'sslPolicy' => null,
177                                         'url'       => null,
178                                 ],
179                                 'assert' => [
180                                         'hostname'  => 'friendica.local',
181                                         'urlPath'   => 'test/more',
182                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
183                                         'url'       => 'http://friendica.local/test/more',
184                                         'scheme'    => 'http',
185                                 ],
186                         ],
187                 ];
188         }
189
190         /**
191          * Test the default config determination
192          * @dataProvider dataDefault
193          */
194         public function testCheck($server, $input, $assert)
195         {
196                 $configMock = \Mockery::mock(IManageConfigValues::class);
197                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
198                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
199                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
200                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
201
202                 $savable = false;
203
204                 // If we don't have an urlPath as an input, we assert it, we will save it to the DB for the next time
205                 if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
206                         $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'], false)->once();
207                         $savable = true;
208                 }
209
210                 // 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
211                 if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
212                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'], false)->once();
213                         $savable = true;
214                 }
215
216                 // If we don't have the hostname as an input, we assert it, we will save it to the DB for the next time
217                 if (empty($input['hostname']) && !empty($assert['hostname'])) {
218                         $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'], false)->once();
219                         $savable = true;
220                 }
221
222                 // If we don't have an URL at first, but we assert it, we will save it to the DB for the next time
223                 if (empty($input['url']) && !empty($assert['url'])) {
224                         $configMock->shouldReceive('set')->with('system', 'url', $assert['url'], false)->once();
225                         $savable = true;
226                 }
227
228                 if ($savable) {
229                         $configMock->shouldReceive('save')->once();
230                 }
231
232                 $baseUrl = new BaseURL($configMock, $server);
233
234                 self::assertEquals($assert['hostname'], $baseUrl->getHostname());
235                 self::assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
236                 self::assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
237                 self::assertEquals($assert['scheme'], $baseUrl->getScheme());
238                 self::assertEquals($assert['url'], $baseUrl->get());
239         }
240
241         public function dataSave()
242         {
243                 return [
244                         'default' => [
245                                 'input' => [
246                                         'hostname'  => 'friendica.old',
247                                         'urlPath'   => 'is/old/path',
248                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
249                                         'url'       => 'http://friendica.old/is/old/path',
250                                         'force_ssl' => true,
251                                 ],
252                                 'save' => [
253                                         'hostname'  => 'friendica.local',
254                                         'urlPath'   => 'new/path',
255                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
256                                 ],
257                                 'url' => 'https://friendica.local/new/path',
258                         ],
259                         'null' => [
260                                 'input' => [
261                                         'hostname'  => 'friendica.old',
262                                         'urlPath'   => 'is/old/path',
263                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
264                                         'url'       => 'http://friendica.old/is/old/path',
265                                         'force_ssl' => true,
266                                 ],
267                                 'save' => [
268                                         'hostname'  => null,
269                                         'urlPath'   => null,
270                                         'sslPolicy' => null,
271                                 ],
272                                 'url' => 'http://friendica.old/is/old/path',
273                         ],
274                         'changeHostname' => [
275                                 'input' => [
276                                         'hostname'  => 'friendica.old',
277                                         'urlPath'   => 'is/old/path',
278                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
279                                         'url'       => 'http://friendica.old/is/old/path',
280                                         'force_ssl' => true,
281                                 ],
282                                 'save' => [
283                                         'hostname'  => 'friendica.local',
284                                         'urlPath'   => null,
285                                         'sslPolicy' => null,
286                                 ],
287                                 'url' => 'http://friendica.local/is/old/path',
288                         ],
289                         'changeUrlPath' => [
290                                 'input' => [
291                                         'hostname'  => 'friendica.old',
292                                         'urlPath'   => 'is/old/path',
293                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
294                                         'url'       => 'http://friendica.old/is/old/path',
295                                         'force_ssl' => true,
296                                 ],
297                                 'save' => [
298                                         'hostname'  => null,
299                                         'urlPath'   => 'new/path',
300                                         'sslPolicy' => null,
301                                 ],
302                                 'url' => 'http://friendica.old/new/path',
303                         ],
304                         'changeSSLPolicy' => [
305                                 'input' => [
306                                         'hostname'  => 'friendica.old',
307                                         'urlPath'   => 'is/old/path',
308                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
309                                         'url'       => 'http://friendica.old/is/old/path',
310                                         'force_ssl' => true,
311                                 ],
312                                 'save' => [
313                                         'hostname'  => null,
314                                         'urlPath'   => null,
315                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
316                                 ],
317                                 'url' => 'https://friendica.old/is/old/path',
318                         ],
319                 ];
320         }
321
322         /**
323          * Test the save() method
324          * @dataProvider dataSave
325          */
326         public function testSave($input, $save, $url)
327         {
328                 $configMock = \Mockery::mock(IManageConfigValues::class);
329                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
330                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
331                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
332                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
333                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
334
335                 $baseUrl = new BaseURL($configMock, []);
336
337                 if (isset($save['hostname'])) {
338                         $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'], false)->andReturn(true)->once();
339                 }
340
341                 if (isset($save['urlPath'])) {
342                         $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'], false)->andReturn(true)->once();
343                 }
344
345                 if (isset($save['sslPolicy'])) {
346                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'], false)->andReturn(true)->once();
347                 }
348
349                 $configMock->shouldReceive('set')->with('system', 'url', $url, false)->andReturn(true)->once();
350
351                 $configMock->shouldReceive('save')->once();
352
353                 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
354
355                 self::assertEquals($url, $baseUrl->get());
356         }
357
358         /**
359          * Test the saveByUrl() method
360          * @dataProvider dataSave
361          *
362          * @param $input
363          * @param $save
364          * @param $url
365          */
366         public function testSaveByUrl($input, $save, $url)
367         {
368                 $configMock = \Mockery::mock(IManageConfigValues::class);
369                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
370                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
371                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
372                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
373                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
374
375                 $baseUrl = new BaseURL($configMock, []);
376
377                 if (isset($save['hostname'])) {
378                         $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'], false)->andReturn(true)->once();
379                 }
380
381                 if (isset($save['urlPath'])) {
382                         $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'], false)->andReturn(true)->once();
383                 }
384
385                 if (isset($save['sslPolicy'])) {
386                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'], false)->andReturn(true)->once();
387                 }
388
389                 $configMock->shouldReceive('set')->with('system', 'url', $url, false)->andReturn(true)->once();
390
391                 $configMock->shouldReceive('save')->once();
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
516         public function dataWrongSave()
517         {
518                 return [
519                         'wrongHostname' => [
520                                 'fail' => 'hostname',
521                         ],
522                         'wrongSSLPolicy' => [
523                                 'fail' => 'sslPolicy',
524                         ],
525                         'wrongURLPath' => [
526                                 'fail' => 'urlPath',
527                         ],
528                         'wrongURL' => [
529                                 'fail' => 'url',
530                         ],
531                 ];
532         }
533
534         /**
535          * Test the save() method with wrong parameters
536          * @dataProvider dataWrongSave
537          */
538         public function testWrongSave($fail)
539         {
540                 $configMock = \Mockery::mock(IManageConfigValues::class);
541                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
542                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
543                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(BaseURL::DEFAULT_SSL_SCHEME);
544                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn('http://friendica.local/new/test');
545
546                 switch ($fail) {
547                         case 'hostname':
548                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any(), false)->andReturn(false)->once();
549                                 break;
550                         case 'sslPolicy':
551                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any(), false)->andReturn(true)->twice();
552                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any(), false)->andReturn(false)->once();
553                                 $configMock->shouldReceive('save')->once();
554                                 break;
555                         case 'urlPath':
556                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any(), false)->andReturn(true)->twice();
557                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any(), false)->andReturn(true)->twice();
558                                 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any(), false)->andReturn(false)->once();
559                                 $configMock->shouldReceive('save')->once();
560                                 break;
561                         case 'url':
562                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any(), false)->andReturn(true)->twice();
563                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any(), false)->andReturn(true)->twice();
564                                 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any(), false)->andReturn(true)->twice();
565                                 $configMock->shouldReceive('set')->with('system', 'url', \Mockery::any(), false)->andReturn(false)->once();
566                                 $configMock->shouldReceive('save')->once();
567                                 break;
568                 }
569
570                 $baseUrl = new BaseURL($configMock, []);
571                 self::assertFalse($baseUrl->save('test', 10, 'nope'));
572
573                 // nothing should have changed because we never successfully saved anything
574                 self::assertEquals('friendica.local', $baseUrl->getHostname());
575                 self::assertEquals('new/test', $baseUrl->getUrlPath());
576                 self::assertEquals(BaseURL::DEFAULT_SSL_SCHEME, $baseUrl->getSSLPolicy());
577                 self::assertEquals('http://friendica.local/new/test', $baseUrl->get());
578         }
579 }