]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/BaseURLTest.php
Merge pull request #11250 from nupplaphil/bug/redis_pw
[friendica.git] / tests / src / Util / BaseURLTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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                 // If we don't have an urlPath as an input, we assert it, we will save it to the DB for the next time
203                 if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
204                         $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
205                 }
206
207                 // 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
208                 if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
209                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
210                 }
211
212                 // If we don't have the hostname as an input, we assert it, we will save it to the DB for the next time
213                 if (empty($input['hostname']) && !empty($assert['hostname'])) {
214                         $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
215                 }
216
217                 // If we don't have an URL at first, but we assert it, we will save it to the DB for the next time
218                 if (empty($input['url']) && !empty($assert['url'])) {
219                         $configMock->shouldReceive('set')->with('system', 'url', $assert['url'])->once();
220                 }
221
222                 $baseUrl = new BaseURL($configMock, $server);
223
224                 self::assertEquals($assert['hostname'], $baseUrl->getHostname());
225                 self::assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
226                 self::assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
227                 self::assertEquals($assert['scheme'], $baseUrl->getScheme());
228                 self::assertEquals($assert['url'], $baseUrl->get());
229         }
230
231         public function dataSave()
232         {
233                 return [
234                         'default' => [
235                                 'input' => [
236                                         'hostname'  => 'friendica.old',
237                                         'urlPath'   => 'is/old/path',
238                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
239                                         'url'       => 'http://friendica.old/is/old/path',
240                                         'force_ssl' => true,
241                                 ],
242                                 'save' => [
243                                         'hostname'  => 'friendica.local',
244                                         'urlPath'   => 'new/path',
245                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
246                                 ],
247                                 'url' => 'https://friendica.local/new/path',
248                         ],
249                         'null' => [
250                                 'input' => [
251                                         'hostname'  => 'friendica.old',
252                                         'urlPath'   => 'is/old/path',
253                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
254                                         'url'       => 'http://friendica.old/is/old/path',
255                                         'force_ssl' => true,
256                                 ],
257                                 'save' => [
258                                         'hostname'  => null,
259                                         'urlPath'   => null,
260                                         'sslPolicy' => null,
261                                 ],
262                                 'url' => 'http://friendica.old/is/old/path',
263                         ],
264                         'changeHostname' => [
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'   => null,
275                                         'sslPolicy' => null,
276                                 ],
277                                 'url' => 'http://friendica.local/is/old/path',
278                         ],
279                         'changeUrlPath' => [
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'   => 'new/path',
290                                         'sslPolicy' => null,
291                                 ],
292                                 'url' => 'http://friendica.old/new/path',
293                         ],
294                         'changeSSLPolicy' => [
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'  => null,
304                                         'urlPath'   => null,
305                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
306                                 ],
307                                 'url' => 'https://friendica.old/is/old/path',
308                         ],
309                 ];
310         }
311
312         /**
313          * Test the save() method
314          * @dataProvider dataSave
315          */
316         public function testSave($input, $save, $url)
317         {
318                 $configMock = \Mockery::mock(IManageConfigValues::class);
319                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
320                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
321                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
322                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
323                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
324
325                 $baseUrl = new BaseURL($configMock, []);
326
327                 if (isset($save['hostname'])) {
328                         $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
329                 }
330
331                 if (isset($save['urlPath'])) {
332                         $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
333                 }
334
335                 if (isset($save['sslPolicy'])) {
336                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
337                 }
338
339                 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
340
341                 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
342
343                 self::assertEquals($url, $baseUrl->get());
344         }
345
346         /**
347          * Test the saveByUrl() method
348          * @dataProvider dataSave
349          *
350          * @param $input
351          * @param $save
352          * @param $url
353          */
354         public function testSaveByUrl($input, $save, $url)
355         {
356                 $configMock = \Mockery::mock(IManageConfigValues::class);
357                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
358                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
359                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
360                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
361                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
362
363                 $baseUrl = new BaseURL($configMock, []);
364
365                 if (isset($save['hostname'])) {
366                         $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
367                 }
368
369                 if (isset($save['urlPath'])) {
370                         $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
371                 }
372
373                 if (isset($save['sslPolicy'])) {
374                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
375                 }
376
377                 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
378
379                 $baseUrl->saveByURL($url);
380
381                 self::assertEquals($url, $baseUrl->get());
382         }
383
384         public function dataGetBaseUrl()
385         {
386                 return [
387                         'default'           => [
388                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
389                                 'ssl'       => false,
390                                 'url'       => 'http://friendica.local/new/test',
391                                 'assert'    => 'http://friendica.local/new/test',
392                         ],
393                         'DefaultWithSSL'    => [
394                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
395                                 'ssl'       => true,
396                                 'url'       => 'http://friendica.local/new/test',
397                                 'assert'    => 'https://friendica.local/new/test',
398                         ],
399                         'SSLFullWithSSL'    => [
400                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
401                                 'ssl'       => true,
402                                 'url'       => 'http://friendica.local/new/test',
403                                 'assert'    => 'http://friendica.local/new/test',
404                         ],
405                         'SSLFullWithoutSSL' => [
406                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
407                                 'ssl'       => false,
408                                 'url'       => 'https://friendica.local/new/test',
409                                 'assert'    => 'https://friendica.local/new/test',
410                         ],
411                         'NoSSLWithSSL'      => [
412                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
413                                 'ssl'       => true,
414                                 'url'       => 'http://friendica.local/new/test',
415                                 'assert'    => 'http://friendica.local/new/test',
416                         ],
417                         'NoSSLWithoutSSL'   => [
418                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
419                                 'ssl'       => false,
420                                 'url'       => 'http://friendica.local/new/test',
421                                 'assert'    => 'http://friendica.local/new/test',
422                         ],
423                 ];
424         }
425
426         /**
427          * Test the get() method
428          * @dataProvider dataGetBaseUrl
429          */
430         public function testGetURL($sslPolicy, $ssl, $url, $assert)
431         {
432                 $configMock = \Mockery::mock(IManageConfigValues::class);
433                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
434                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
435                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
436                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
437
438                 $baseUrl = new BaseURL($configMock, []);
439
440                 self::assertEquals($assert, $baseUrl->get($ssl));
441         }
442
443         public function dataCheckRedirectHTTPS()
444         {
445                 return [
446                         'default' => [
447                                 'server' => [
448                                         'REQUEST_METHOD' => 'GET',
449                                         'HTTPS' => true,
450                                 ],
451                                 'forceSSL'  => false,
452                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
453                                 'url'       => 'https://friendica.local',
454                                 'redirect'  => false,
455                         ],
456                         'forceSSL' => [
457                                 'server' => [
458                                         'REQUEST_METHOD' => 'GET',
459                                 ],
460                                 'forceSSL'  => true,
461                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
462                                 'url'       => 'https://friendica.local',
463                                 'redirect'  => false,
464                         ],
465                         'forceSSLWithSSLPolicy' => [
466                                 'server' => [],
467                                 'forceSSL'  => true,
468                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
469                                 'url'       => 'https://friendica.local',
470                                 'redirect'  => false,
471                         ],
472                         'forceSSLWithSSLPolicyAndGet' => [
473                                 'server' => [
474                                         'REQUEST_METHOD' => 'GET',
475                                 ],
476                                 'forceSSL'  => true,
477                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
478                                 'url'       => 'https://friendica.local',
479                                 'redirect'  => true,
480                         ],
481                 ];
482         }
483
484         /**
485          * Test the checkRedirectHTTPS() method
486          * @dataProvider dataCheckRedirectHTTPS
487          */
488         public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
489         {
490                 $configMock = \Mockery::mock(IManageConfigValues::class);
491                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
492                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
493                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
494                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
495                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL);
496
497                 $baseUrl = new BaseURL($configMock, $server);
498
499                 self::assertEquals($redirect, $baseUrl->checkRedirectHttps());
500         }
501
502         public function dataWrongSave()
503         {
504                 return [
505                         'wrongHostname' => [
506                                 'fail' => 'hostname',
507                         ],
508                         'wrongSSLPolicy' => [
509                                 'fail' => 'sslPolicy',
510                         ],
511                         'wrongURLPath' => [
512                                 'fail' => 'urlPath',
513                         ],
514                         'wrongURL' => [
515                                 'fail' => 'url',
516                         ],
517                 ];
518         }
519
520         /**
521          * Test the save() method with wrong parameters
522          * @dataProvider dataWrongSave
523          */
524         public function testWrongSave($fail)
525         {
526                 $configMock = \Mockery::mock(IManageConfigValues::class);
527                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
528                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
529                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(BaseURL::DEFAULT_SSL_SCHEME);
530                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn('http://friendica.local/new/test');
531
532                 switch ($fail) {
533                         case 'hostname':
534                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(false)->once();
535                                 break;
536                         case 'sslPolicy':
537                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
538                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(false)->once();
539                                 break;
540                         case 'urlPath':
541                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
542                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
543                                 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(false)->once();
544                                 break;
545                         case 'url':
546                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
547                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
548                                 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(true)->twice();
549                                 $configMock->shouldReceive('set')->with('system', 'url', \Mockery::any())->andReturn(false)->once();
550                                 break;
551                 }
552
553                 $baseUrl = new BaseURL($configMock, []);
554                 self::assertFalse($baseUrl->save('test', 10, 'nope'));
555
556                 // nothing should have changed because we never successfully saved anything
557                 self::assertEquals('friendica.local', $baseUrl->getHostname());
558                 self::assertEquals('new/test', $baseUrl->getUrlPath());
559                 self::assertEquals(BaseURL::DEFAULT_SSL_SCHEME, $baseUrl->getSSLPolicy());
560                 self::assertEquals('http://friendica.local/new/test', $baseUrl->get());
561         }
562 }