]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/BaseURLTest.php
Update copyright
[friendica.git] / tests / src / Util / BaseURLTest.php
1 <?php
2 namespace Friendica\Test\src\Util;
3
4 use Friendica\App\BaseURL;
5 use Friendica\Core\Config\IConfig;
6 use Friendica\Test\MockedTest;
7
8 class BaseURLTest extends MockedTest
9 {
10         public function dataDefault()
11         {
12                 return [
13                         'null' => [
14                                 'server' => [],
15                                 'input' => [
16                                 'hostname' => null,
17                                 'urlPath' => null,
18                                 'sslPolicy' => null,
19                                 'url' => null,
20                                         ],
21                                 'assert' => [
22                                         'hostname'  => '',
23                                         'urlPath'   => '',
24                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
25                                         'url'       => 'http://',
26                                         'scheme'    => 'http',
27                                 ],
28                         ],
29                         'WithSubDirectory' => [
30                                 'server' => [
31                                         'SERVER_NAME'  => 'friendica.local',
32                                         'REDIRECT_URI' => 'test/module/more',
33                                         'QUERY_STRING' => 'module/more',
34                                 ],
35                                 'input' => [
36                                         'hostname'  => null,
37                                         'urlPath'   => null,
38                                         'sslPolicy' => null,
39                                         'url'       => null,
40                                 ],
41                                 'assert' => [
42                                         'hostname'  => 'friendica.local',
43                                         'urlPath'   => 'test',
44                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
45                                         'url'       => 'http://friendica.local/test',
46                                         'scheme'    => 'http',
47                                 ],
48                         ],
49                         'input' => [
50                                 'server' => [],
51                                 'input' => [
52                                         'hostname'  => 'friendica.local',
53                                         'urlPath'   => 'test',
54                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
55                                         'url'       => 'http://friendica.local/test',
56                                 ],
57                                 'assert' => [
58                                         'hostname'  => 'friendica.local',
59                                         'urlPath'   => 'test',
60                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
61                                         'url'       => 'http://friendica.local/test',
62                                         'scheme'    => 'http',
63                                 ],
64                         ],
65                         'WithHttpsScheme' => [
66                                 'server' => [
67                                         'SERVER_NAME'    => 'friendica.local',
68                                         'REDIRECT_URI'   => 'test/module/more',
69                                         'QUERY_STRING'   => 'module/more',
70                                         'HTTPS'          => true,
71                                 ],
72                                 'input' => [
73                                         'hostname'  => null,
74                                         'urlPath'   => null,
75                                         'sslPolicy' => null,
76                                         'url'       => null,
77                                 ],
78                                 'assert' => [
79                                         'hostname'  => 'friendica.local',
80                                         'urlPath'   => 'test',
81                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
82                                         'url'       => 'https://friendica.local/test',
83                                         'scheme'    => 'https',
84                                 ],
85                         ],
86                         'WithoutQueryString' => [
87                                 'server' => [
88                                         'SERVER_NAME'    => 'friendica.local',
89                                         'REDIRECT_URI'   => 'test/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/more',
101                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
102                                         'url'       => 'https://friendica.local/test/more',
103                                         'scheme'    => 'https',
104                                 ],
105                         ],
106                         'WithPort' => [
107                                 'server' => [
108                                         'SERVER_NAME'    => 'friendica.local',
109                                         'SERVER_PORT'    => '1234',
110                                         'REDIRECT_URI'   => 'test/more',
111                                         'HTTPS'          => true,
112                                 ],
113                                 'input' => [
114                                         'hostname'  => null,
115                                         'urlPath'   => null,
116                                         'sslPolicy' => null,
117                                         'url'       => null,
118                                 ],
119                                 'assert' => [
120                                         'hostname'  => 'friendica.local:1234',
121                                         'urlPath'   => 'test/more',
122                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
123                                         'url'       => 'https://friendica.local:1234/test/more',
124                                         'scheme'    => 'https',
125                                 ],
126                         ],
127                         'With443Port' => [
128                                 'server' => [
129                                         'SERVER_NAME'    => 'friendica.local',
130                                         'SERVER_PORT'    => '443',
131                                         'REDIRECT_URI'   => 'test/more',
132                                 ],
133                                 'input' => [
134                                         'hostname'  => null,
135                                         'urlPath'   => null,
136                                         'sslPolicy' => null,
137                                         'url'       => null,
138                                 ],
139                                 'assert' => [
140                                         'hostname'  => 'friendica.local',
141                                         'urlPath'   => 'test/more',
142                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
143                                         'url'       => 'https://friendica.local/test/more',
144                                         'scheme'    => 'https',
145                                 ],
146                         ],
147                         'With80Port' => [
148                                 'server' => [
149                                         'SERVER_NAME'  => 'friendica.local',
150                                         'SERVER_PORT'  => '80',
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::DEFAULT_SSL_SCHEME,
163                                         'url'       => 'http://friendica.local/test/more',
164                                         'scheme'    => 'http',
165                                 ],
166                         ],
167                 ];
168         }
169
170         /**
171          * Test the default config determination
172          * @dataProvider dataDefault
173          */
174         public function testCheck($server, $input, $assert)
175         {
176                 $configMock = \Mockery::mock(IConfig::class);
177                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
178                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
179                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
180                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
181
182                 // If we don't have an urlPath as an input, we assert it, we will save it to the DB for the next time
183                 if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
184                         $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
185                 }
186
187                 // 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
188                 if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
189                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
190                 }
191
192                 // If we don't have the hostname as an input, we assert it, we will save it to the DB for the next time
193                 if (empty($input['hostname']) && !empty($assert['hostname'])) {
194                         $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
195                 }
196
197                 // If we don't have an URL at first, but we assert it, we will save it to the DB for the next time
198                 if (empty($input['url']) && !empty($assert['url'])) {
199                         $configMock->shouldReceive('set')->with('system', 'url', $assert['url'])->once();
200                 }
201
202                 $baseUrl = new BaseURL($configMock, $server);
203
204                 self::assertEquals($assert['hostname'], $baseUrl->getHostname());
205                 self::assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
206                 self::assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
207                 self::assertEquals($assert['scheme'], $baseUrl->getScheme());
208                 self::assertEquals($assert['url'], $baseUrl->get());
209         }
210
211         public function dataSave()
212         {
213                 return [
214                         'default' => [
215                                 'input' => [
216                                         'hostname'  => 'friendica.old',
217                                         'urlPath'   => 'is/old/path',
218                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
219                                         'url'       => 'http://friendica.old/is/old/path',
220                                         'force_ssl' => true,
221                                 ],
222                                 'save' => [
223                                         'hostname'  => 'friendica.local',
224                                         'urlPath'   => 'new/path',
225                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
226                                 ],
227                                 'url' => 'https://friendica.local/new/path',
228                         ],
229                         'null' => [
230                                 'input' => [
231                                         'hostname'  => 'friendica.old',
232                                         'urlPath'   => 'is/old/path',
233                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
234                                         'url'       => 'http://friendica.old/is/old/path',
235                                         'force_ssl' => true,
236                                 ],
237                                 'save' => [
238                                         'hostname'  => null,
239                                         'urlPath'   => null,
240                                         'sslPolicy' => null,
241                                 ],
242                                 'url' => 'http://friendica.old/is/old/path',
243                         ],
244                         'changeHostname' => [
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'   => null,
255                                         'sslPolicy' => null,
256                                 ],
257                                 'url' => 'http://friendica.local/is/old/path',
258                         ],
259                         'changeUrlPath' => [
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'   => 'new/path',
270                                         'sslPolicy' => null,
271                                 ],
272                                 'url' => 'http://friendica.old/new/path',
273                         ],
274                         'changeSSLPolicy' => [
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'  => null,
284                                         'urlPath'   => null,
285                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
286                                 ],
287                                 'url' => 'https://friendica.old/is/old/path',
288                         ],
289                 ];
290         }
291
292         /**
293          * Test the save() method
294          * @dataProvider dataSave
295          */
296         public function testSave($input, $save, $url)
297         {
298                 $configMock = \Mockery::mock(IConfig::class);
299                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
300                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
301                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
302                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
303                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
304
305                 $baseUrl = new BaseURL($configMock, []);
306
307                 if (isset($save['hostname'])) {
308                         $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
309                 }
310
311                 if (isset($save['urlPath'])) {
312                         $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
313                 }
314
315                 if (isset($save['sslPolicy'])) {
316                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
317                 }
318
319                 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
320
321                 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
322
323                 self::assertEquals($url, $baseUrl->get());
324         }
325
326         /**
327          * Test the saveByUrl() method
328          * @dataProvider dataSave
329          *
330          * @param $input
331          * @param $save
332          * @param $url
333          */
334         public function testSaveByUrl($input, $save, $url)
335         {
336                 $configMock = \Mockery::mock(IConfig::class);
337                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
338                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
339                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
340                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
341                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
342
343                 $baseUrl = new BaseURL($configMock, []);
344
345                 if (isset($save['hostname'])) {
346                         $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
347                 }
348
349                 if (isset($save['urlPath'])) {
350                         $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
351                 }
352
353                 if (isset($save['sslPolicy'])) {
354                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
355                 }
356
357                 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
358
359                 $baseUrl->saveByURL($url);
360
361                 self::assertEquals($url, $baseUrl->get());
362         }
363
364         public function dataGetBaseUrl()
365         {
366                 return [
367                         'default'           => [
368                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
369                                 'ssl'       => false,
370                                 'url'       => 'http://friendica.local/new/test',
371                                 'assert'    => 'http://friendica.local/new/test',
372                         ],
373                         'DefaultWithSSL'    => [
374                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
375                                 'ssl'       => true,
376                                 'url'       => 'http://friendica.local/new/test',
377                                 'assert'    => 'https://friendica.local/new/test',
378                         ],
379                         'SSLFullWithSSL'    => [
380                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
381                                 'ssl'       => true,
382                                 'url'       => 'http://friendica.local/new/test',
383                                 'assert'    => 'http://friendica.local/new/test',
384                         ],
385                         'SSLFullWithoutSSL' => [
386                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
387                                 'ssl'       => false,
388                                 'url'       => 'https://friendica.local/new/test',
389                                 'assert'    => 'https://friendica.local/new/test',
390                         ],
391                         'NoSSLWithSSL'      => [
392                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
393                                 'ssl'       => true,
394                                 'url'       => 'http://friendica.local/new/test',
395                                 'assert'    => 'http://friendica.local/new/test',
396                         ],
397                         'NoSSLWithoutSSL'   => [
398                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
399                                 'ssl'       => false,
400                                 'url'       => 'http://friendica.local/new/test',
401                                 'assert'    => 'http://friendica.local/new/test',
402                         ],
403                 ];
404         }
405
406         /**
407          * Test the get() method
408          * @dataProvider dataGetBaseUrl
409          */
410         public function testGetURL($sslPolicy, $ssl, $url, $assert)
411         {
412                 $configMock = \Mockery::mock(IConfig::class);
413                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
414                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
415                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
416                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
417
418                 $baseUrl = new BaseURL($configMock, []);
419
420                 self::assertEquals($assert, $baseUrl->get($ssl));
421         }
422
423         public function dataCheckRedirectHTTPS()
424         {
425                 return [
426                         'default' => [
427                                 'server' => [
428                                         'REQUEST_METHOD' => 'GET',
429                                         'HTTPS' => true,
430                                 ],
431                                 'forceSSL'  => false,
432                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
433                                 'url'       => 'https://friendica.local',
434                                 'redirect'  => false,
435                         ],
436                         'forceSSL' => [
437                                 'server' => [
438                                         'REQUEST_METHOD' => 'GET',
439                                 ],
440                                 'forceSSL'  => true,
441                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
442                                 'url'       => 'https://friendica.local',
443                                 'redirect'  => false,
444                         ],
445                         'forceSSLWithSSLPolicy' => [
446                                 'server' => [],
447                                 'forceSSL'  => true,
448                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
449                                 'url'       => 'https://friendica.local',
450                                 'redirect'  => false,
451                         ],
452                         'forceSSLWithSSLPolicyAndGet' => [
453                                 'server' => [
454                                         'REQUEST_METHOD' => 'GET',
455                                 ],
456                                 'forceSSL'  => true,
457                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
458                                 'url'       => 'https://friendica.local',
459                                 'redirect'  => true,
460                         ],
461                 ];
462         }
463
464         /**
465          * Test the checkRedirectHTTPS() method
466          * @dataProvider dataCheckRedirectHTTPS
467          */
468         public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
469         {
470                 $configMock = \Mockery::mock(IConfig::class);
471                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
472                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
473                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
474                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
475                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL);
476
477                 $baseUrl = new BaseURL($configMock, $server);
478
479                 self::assertEquals($redirect, $baseUrl->checkRedirectHttps());
480         }
481
482         public function dataWrongSave()
483         {
484                 return [
485                         'wrongHostname' => [
486                                 'fail' => 'hostname',
487                         ],
488                         'wrongSSLPolicy' => [
489                                 'fail' => 'sslPolicy',
490                         ],
491                         'wrongURLPath' => [
492                                 'fail' => 'urlPath',
493                         ],
494                         'wrongURL' => [
495                                 'fail' => 'url',
496                         ],
497                 ];
498         }
499
500         /**
501          * Test the save() method with wrong parameters
502          * @dataProvider dataWrongSave
503          */
504         public function testWrongSave($fail)
505         {
506                 $configMock = \Mockery::mock(IConfig::class);
507                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
508                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
509                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(BaseURL::DEFAULT_SSL_SCHEME);
510                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn('http://friendica.local/new/test');
511
512                 switch ($fail) {
513                         case 'hostname':
514                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(false)->once();
515                                 break;
516                         case 'sslPolicy':
517                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
518                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(false)->once();
519                                 break;
520                         case 'urlPath':
521                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
522                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
523                                 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(false)->once();
524                                 break;
525                         case 'url':
526                                 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
527                                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
528                                 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(true)->twice();
529                                 $configMock->shouldReceive('set')->with('system', 'url', \Mockery::any())->andReturn(false)->once();
530                                 break;
531                 }
532
533                 $baseUrl = new BaseURL($configMock, []);
534                 self::assertFalse($baseUrl->save('test', 10, 'nope'));
535
536                 // nothing should have changed because we never successfully saved anything
537                 self::assertEquals('friendica.local', $baseUrl->getHostname());
538                 self::assertEquals('new/test', $baseUrl->getUrlPath());
539                 self::assertEquals(BaseURL::DEFAULT_SSL_SCHEME, $baseUrl->getSSLPolicy());
540                 self::assertEquals('http://friendica.local/new/test', $baseUrl->get());
541         }
542 }