]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/BaseURLTest.php
Adding tests
[friendica.git] / tests / src / Util / BaseURLTest.php
1 <?php
2 namespace Friendica\Test\src\Util;
3
4 use Friendica\Core\Config\Configuration;
5 use Friendica\Util\BaseURL;
6 use PHPUnit\Framework\TestCase;
7
8 class BaseURLTest extends TestCase
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(Configuration::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 (!isset($input['urlPath']) && isset($assert['urlPath'])) {
183                         $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
184                 }
185
186                 if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
187                         $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
188                 }
189
190                 if (!isset($input['hostname']) && !empty($assert['hostname'])) {
191                         $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
192                 }
193
194                 $baseUrl = new BaseURL($configMock, $server);
195
196                 $this->assertEquals($assert['hostname'], $baseUrl->getHostname());
197                 $this->assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
198                 $this->assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
199                 $this->assertEquals($assert['scheme'], $baseUrl->getScheme());
200                 $this->assertEquals($assert['url'], $baseUrl->get());
201         }
202
203         public function dataSave()
204         {
205                 return [
206                         'default' => [
207                                 'input' => [
208                                         'hostname'  => 'friendica.old',
209                                         'urlPath'   => 'is/old/path',
210                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
211                                         'url'       => 'http://friendica.old/is/old/path',
212                                         'force_ssl' => true,
213                                 ],
214                                 'save' => [
215                                         'hostname'  => 'friendica.local',
216                                         'urlPath'   => 'new/path',
217                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
218                                 ],
219                                 'url' => 'https://friendica.local/new/path',
220                         ],
221                         'null' => [
222                                 'input' => [
223                                         'hostname'  => 'friendica.old',
224                                         'urlPath'   => 'is/old/path',
225                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
226                                         'url'       => 'http://friendica.old/is/old/path',
227                                         'force_ssl' => true,
228                                 ],
229                                 'save' => [
230                                         'hostname'  => null,
231                                         'urlPath'   => null,
232                                         'sslPolicy' => null,
233                                 ],
234                                 'url' => 'http://friendica.old/is/old/path',
235                         ],
236                         'changeHostname' => [
237                                 'input' => [
238                                         'hostname'  => 'friendica.old',
239                                         'urlPath'   => 'is/old/path',
240                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
241                                         'url'       => 'http://friendica.old/is/old/path',
242                                         'force_ssl' => true,
243                                 ],
244                                 'save' => [
245                                         'hostname'  => 'friendica.local',
246                                         'urlPath'   => null,
247                                         'sslPolicy' => null,
248                                 ],
249                                 'url' => 'http://friendica.local/is/old/path',
250                         ],
251                         'changeUrlPath' => [
252                                 'input' => [
253                                         'hostname'  => 'friendica.old',
254                                         'urlPath'   => 'is/old/path',
255                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
256                                         'url'       => 'http://friendica.old/is/old/path',
257                                         'force_ssl' => true,
258                                 ],
259                                 'save' => [
260                                         'hostname'  => null,
261                                         'urlPath'   => 'new/path',
262                                         'sslPolicy' => null,
263                                 ],
264                                 'url' => 'http://friendica.old/new/path',
265                         ],
266                         'changeSSLPolicy' => [
267                                 'input' => [
268                                         'hostname'  => 'friendica.old',
269                                         'urlPath'   => 'is/old/path',
270                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
271                                         'url'       => 'http://friendica.old/is/old/path',
272                                         'force_ssl' => true,
273                                 ],
274                                 'save' => [
275                                         'hostname'  => null,
276                                         'urlPath'   => null,
277                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
278                                 ],
279                                 'url' => 'https://friendica.old/is/old/path',
280                         ],
281                 ];
282         }
283
284         /**
285          * Test the save() method
286          * @dataProvider dataSave
287          */
288         public function testSave($input, $save, $url)
289         {
290                 $configMock = \Mockery::mock(Configuration::class);
291                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
292                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
293                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
294                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
295                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
296
297                 $baseUrl = new BaseURL($configMock, []);
298
299                 $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
300                 $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
301                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
302                 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
303
304                 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
305
306                 $this->assertEquals($url, $baseUrl->get());
307         }
308
309         /**
310          * Test the saveByUrl() method
311          * @dataProvider dataSave
312          *
313          * @param $input
314          * @param $save
315          * @param $url
316          */
317         public function testSaveByUrl($input, $save, $url)
318         {
319                 $configMock = \Mockery::mock(Configuration::class);
320                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
321                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
322                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
323                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
324                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
325
326                 $baseUrl = new BaseURL($configMock, []);
327
328                 $configMock->shouldReceive('set')->with('config', 'hostname', (!empty($save['hostname']) ? $save['hostname'] : $input['hostname']))->andReturn(true)->once();
329                 $configMock->shouldReceive('set')->with('system', 'urlpath', (!empty($save['urlPath']) ? $save['urlPath'] : $input['urlPath']))->andReturn(true)->once();
330                 $configMock->shouldReceive('set')->with('system', 'ssl_policy', (!empty($save['sslPolicy']) ? $save['sslPolicy'] : $input['sslPolicy']))->andReturn(true)->once();
331                 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
332
333                 $baseUrl->saveByURL($url);
334
335                 $this->assertEquals($url, $baseUrl->get());
336         }
337
338         public function dataGetBaseUrl()
339         {
340                 return [
341                         'default'           => [
342                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
343                                 'ssl'       => false,
344                                 'url'       => 'http://friendica.local/new/test',
345                                 'assert'    => 'http://friendica.local/new/test',
346                         ],
347                         'DefaultWithSSL'    => [
348                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
349                                 'ssl'       => true,
350                                 'url'       => 'http://friendica.local/new/test',
351                                 'assert'    => 'https://friendica.local/new/test',
352                         ],
353                         'SSLFullWithSSL'    => [
354                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
355                                 'ssl'       => true,
356                                 'url'       => 'http://friendica.local/new/test',
357                                 'assert'    => 'http://friendica.local/new/test',
358                         ],
359                         'SSLFullWithoutSSL' => [
360                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
361                                 'ssl'       => false,
362                                 'url'       => 'https://friendica.local/new/test',
363                                 'assert'    => 'https://friendica.local/new/test',
364                         ],
365                         'NoSSLWithSSL'      => [
366                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
367                                 'ssl'       => true,
368                                 'url'       => 'http://friendica.local/new/test',
369                                 'assert'    => 'http://friendica.local/new/test',
370                         ],
371                         'NoSSLWithoutSSL'   => [
372                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
373                                 'ssl'       => false,
374                                 'url'       => 'http://friendica.local/new/test',
375                                 'assert'    => 'http://friendica.local/new/test',
376                         ],
377                 ];
378         }
379
380         /**
381          * Test the get() method
382          * @dataProvider dataGetBaseUrl
383          */
384         public function testGetURL($sslPolicy, $ssl, $url, $assert)
385         {
386                 $configMock = \Mockery::mock(Configuration::class);
387                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
388                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
389                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
390                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
391
392                 $baseUrl = new BaseURL($configMock, []);
393
394                 $this->assertEquals($assert, $baseUrl->get($ssl));
395         }
396
397         public function dataCheckRedirectHTTPS()
398         {
399                 return [
400                         'default' => [
401                                 'server' => [
402                                         'REQUEST_METHOD' => 'GET',
403                                         'HTTPS' => true,
404                                 ],
405                                 'forceSSL'  => false,
406                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
407                                 'url'       => 'https://friendica.local',
408                                 'redirect'  => false,
409                         ],
410                         'forceSSL' => [
411                                 'server' => [
412                                         'REQUEST_METHOD' => 'GET',
413                                 ],
414                                 'forceSSL'  => true,
415                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
416                                 'url'       => 'https://friendica.local',
417                                 'redirect'  => false,
418                         ],
419                         'forceSSLWithSSLPolicy' => [
420                                 'server' => [],
421                                 'forceSSL'  => true,
422                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
423                                 'url'       => 'https://friendica.local',
424                                 'redirect'  => false,
425                         ],
426                         'forceSSLWithSSLPolicyAndGet' => [
427                                 'server' => [
428                                         'REQUEST_METHOD' => 'GET',
429                                 ],
430                                 'forceSSL'  => true,
431                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
432                                 'url'       => 'https://friendica.local',
433                                 'redirect'  => true,
434                         ],
435                 ];
436         }
437
438         /**
439          * Test the checkRedirectHTTPS() method
440          * @dataProvider dataCheckRedirectHTTPS
441          */
442         public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
443         {
444                 $configMock = \Mockery::mock(Configuration::class);
445                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
446                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
447                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
448                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
449                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL);
450
451                 $baseUrl = new BaseURL($configMock, $server);
452
453                 $this->assertEquals($redirect, $baseUrl->checkRedirectHttps());
454         }
455 }