]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/BaseURLTest.php
Merge remote-tracking branch 'upstream/develop' into c2s-post
[friendica.git] / tests / src / Util / BaseURLTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util;
23
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\Config\Model\DatabaseConfig;
27 use Friendica\Core\Config\Model\ReadOnlyFileConfig;
28 use Friendica\Core\Config\Util\ConfigFileManager;
29 use Friendica\Core\Config\ValueObject\Cache;
30 use Friendica\Test\DatabaseTest;
31 use Friendica\Test\Util\CreateDatabaseTrait;
32 use Friendica\Test\Util\VFSTrait;
33
34 class BaseURLTest extends DatabaseTest
35 {
36         use VFSTrait;
37         use CreateDatabaseTrait;
38
39         protected function setUp(): void
40         {
41                 parent::setUp();
42
43                 $this->setUpVfsDir();
44         }
45
46         public function dataDefault()
47         {
48                 return [
49                         'null' => [
50                                 'server' => [],
51                                 'input' => [
52                                 'hostname' => null,
53                                 'urlPath' => null,
54                                 'sslPolicy' => null,
55                                 'url' => null,
56                                         ],
57                                 'assert' => [
58                                         'hostname'  => '',
59                                         'urlPath'   => '',
60                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
61                                         'url'       => 'http://',
62                                         'scheme'    => 'http',
63                                 ],
64                         ],
65                         'WithSubDirectory' => [
66                                 'server' => [
67                                         'SERVER_NAME'  => 'friendica.local',
68                                         'REDIRECT_URI' => 'test/module/more',
69                                         'QUERY_STRING' => 'module/more',
70                                 ],
71                                 'input' => [
72                                         'hostname'  => null,
73                                         'urlPath'   => null,
74                                         'sslPolicy' => null,
75                                         'url'       => null,
76                                 ],
77                                 'assert' => [
78                                         'hostname'  => 'friendica.local',
79                                         'urlPath'   => 'test',
80                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
81                                         'url'       => 'http://friendica.local/test',
82                                         'scheme'    => 'http',
83                                 ],
84                         ],
85                         'input' => [
86                                 'server' => [],
87                                 'input' => [
88                                         'hostname'  => 'friendica.local',
89                                         'urlPath'   => 'test',
90                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
91                                         'url'       => 'http://friendica.local/test',
92                                 ],
93                                 'assert' => [
94                                         'hostname'  => 'friendica.local',
95                                         'urlPath'   => 'test',
96                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
97                                         'url'       => 'http://friendica.local/test',
98                                         'scheme'    => 'http',
99                                 ],
100                         ],
101                         'WithHttpsScheme' => [
102                                 'server' => [
103                                         'SERVER_NAME'    => 'friendica.local',
104                                         'REDIRECT_URI'   => 'test/module/more',
105                                         'QUERY_STRING'   => 'module/more',
106                                         'HTTPS'          => true,
107                                 ],
108                                 'input' => [
109                                         'hostname'  => null,
110                                         'urlPath'   => null,
111                                         'sslPolicy' => null,
112                                         'url'       => null,
113                                 ],
114                                 'assert' => [
115                                         'hostname'  => 'friendica.local',
116                                         'urlPath'   => 'test',
117                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
118                                         'url'       => 'https://friendica.local/test',
119                                         'scheme'    => 'https',
120                                 ],
121                         ],
122                         'WithoutQueryString' => [
123                                 'server' => [
124                                         'SERVER_NAME'    => 'friendica.local',
125                                         'REDIRECT_URI'   => 'test/more',
126                                         'HTTPS'          => true,
127                                 ],
128                                 'input' => [
129                                         'hostname'  => null,
130                                         'urlPath'   => null,
131                                         'sslPolicy' => null,
132                                         'url'       => null,
133                                 ],
134                                 'assert' => [
135                                         'hostname'  => 'friendica.local',
136                                         'urlPath'   => 'test/more',
137                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
138                                         'url'       => 'https://friendica.local/test/more',
139                                         'scheme'    => 'https',
140                                 ],
141                         ],
142                         'WithPort' => [
143                                 'server' => [
144                                         'SERVER_NAME'    => 'friendica.local',
145                                         'SERVER_PORT'    => '1234',
146                                         'REDIRECT_URI'   => 'test/more',
147                                         'HTTPS'          => true,
148                                 ],
149                                 'input' => [
150                                         'hostname'  => null,
151                                         'urlPath'   => null,
152                                         'sslPolicy' => null,
153                                         'url'       => null,
154                                 ],
155                                 'assert' => [
156                                         'hostname'  => 'friendica.local:1234',
157                                         'urlPath'   => 'test/more',
158                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
159                                         'url'       => 'https://friendica.local:1234/test/more',
160                                         'scheme'    => 'https',
161                                 ],
162                         ],
163                         'With443Port' => [
164                                 'server' => [
165                                         'SERVER_NAME'    => 'friendica.local',
166                                         'SERVER_PORT'    => '443',
167                                         'REDIRECT_URI'   => 'test/more',
168                                 ],
169                                 'input' => [
170                                         'hostname'  => null,
171                                         'urlPath'   => null,
172                                         'sslPolicy' => null,
173                                         'url'       => null,
174                                 ],
175                                 'assert' => [
176                                         'hostname'  => 'friendica.local',
177                                         'urlPath'   => 'test/more',
178                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
179                                         'url'       => 'https://friendica.local/test/more',
180                                         'scheme'    => 'https',
181                                 ],
182                         ],
183                         'With80Port' => [
184                                 'server' => [
185                                         'SERVER_NAME'  => 'friendica.local',
186                                         'SERVER_PORT'  => '80',
187                                         'REDIRECT_URI' => 'test/more',
188                                 ],
189                                 'input' => [
190                                         'hostname'  => null,
191                                         'urlPath'   => null,
192                                         'sslPolicy' => null,
193                                         'url'       => null,
194                                 ],
195                                 'assert' => [
196                                         'hostname'  => 'friendica.local',
197                                         'urlPath'   => 'test/more',
198                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
199                                         'url'       => 'http://friendica.local/test/more',
200                                         'scheme'    => 'http',
201                                 ],
202                         ],
203                 ];
204         }
205
206         public function dataSave()
207         {
208                 return [
209                         'no_change' => [
210                                 'input' => [
211                                         'hostname'  => 'friendica.local',
212                                         'urlPath'   => 'path',
213                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
214                                         'url'       => 'https://friendica.local/path',
215                                         'force_ssl' => true,
216                                 ],
217                                 'save' => [
218                                         'hostname'  => 'friendica.local',
219                                         'urlPath'   => 'path',
220                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
221                                 ],
222                                 'url' => 'https://friendica.local/path',
223                         ],
224                         'default' => [
225                                 'input' => [
226                                         'hostname'  => 'friendica.old',
227                                         'urlPath'   => 'is/old/path',
228                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
229                                         'url'       => 'http://friendica.old/is/old/path',
230                                         'force_ssl' => true,
231                                 ],
232                                 'save' => [
233                                         'hostname'  => 'friendica.local',
234                                         'urlPath'   => 'new/path',
235                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
236                                 ],
237                                 'url' => 'https://friendica.local/new/path',
238                         ],
239                         'null' => [
240                                 'input' => [
241                                         'hostname'  => 'friendica.old',
242                                         'urlPath'   => 'is/old/path',
243                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
244                                         'url'       => 'http://friendica.old/is/old/path',
245                                         'force_ssl' => true,
246                                 ],
247                                 'save' => [
248                                         'hostname'  => null,
249                                         'urlPath'   => null,
250                                         'sslPolicy' => null,
251                                 ],
252                                 'url' => 'http://friendica.old/is/old/path',
253                         ],
254                         'changeHostname' => [
255                                 'input' => [
256                                         'hostname'  => 'friendica.old',
257                                         'urlPath'   => 'is/old/path',
258                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
259                                         'url'       => 'http://friendica.old/is/old/path',
260                                         'force_ssl' => true,
261                                 ],
262                                 'save' => [
263                                         'hostname'  => 'friendica.local',
264                                         'urlPath'   => null,
265                                         'sslPolicy' => null,
266                                 ],
267                                 'url' => 'http://friendica.local/is/old/path',
268                         ],
269                         'changeUrlPath' => [
270                                 'input' => [
271                                         'hostname'  => 'friendica.old',
272                                         'urlPath'   => 'is/old/path',
273                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
274                                         'url'       => 'http://friendica.old/is/old/path',
275                                         'force_ssl' => true,
276                                 ],
277                                 'save' => [
278                                         'hostname'  => null,
279                                         'urlPath'   => 'new/path',
280                                         'sslPolicy' => null,
281                                 ],
282                                 'url' => 'http://friendica.old/new/path',
283                         ],
284                         'changeSSLPolicy' => [
285                                 'input' => [
286                                         'hostname'  => 'friendica.old',
287                                         'urlPath'   => 'is/old/path',
288                                         'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
289                                         'url'       => 'http://friendica.old/is/old/path',
290                                         'force_ssl' => true,
291                                 ],
292                                 'save' => [
293                                         'hostname'  => null,
294                                         'urlPath'   => null,
295                                         'sslPolicy' => BaseURL::SSL_POLICY_FULL,
296                                 ],
297                                 'url' => 'https://friendica.old/is/old/path',
298                         ],
299                 ];
300         }
301
302         /**
303          * Test the save() method
304          * @dataProvider dataSave
305          */
306         public function testSave($input, $save, $url)
307         {
308                 $config = new DatabaseConfig($this->getDbInstance(), new Cache([
309                         'config' => [
310                                 'hostname' => $input['hostname'] ?? null,
311                         ],
312                         'system' => [
313                                 'urlpath' => $input['urlPath'] ?? null,
314                                 'ssl_policy' => $input['sslPolicy'] ?? null,
315                                 'url' => $input['url'] ?? null,
316                                 'force_ssl' => $input['force_ssl'] ?? null,
317                         ],
318                 ]));
319
320                 $baseUrl = new BaseURL($config, []);
321
322                 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
323
324                 self::assertEquals($url, $baseUrl->get());
325         }
326
327         /**
328          * Test the saveByUrl() method
329          * @dataProvider dataSave
330          *
331          * @param $input
332          * @param $save
333          * @param $url
334          */
335         public function testSaveByUrl($input, $save, $url)
336         {
337                 $config = new DatabaseConfig($this->getDbInstance(), new Cache([
338                         'config' => [
339                                 'hostname' => $input['hostname'] ?? null,
340                         ],
341                         'system' => [
342                                 'urlpath' => $input['urlPath'] ?? null,
343                                 'ssl_policy' => $input['sslPolicy'] ?? null,
344                                 'url' => $input['url'] ?? null,
345                                 'force_ssl' => $input['force_ssl'] ?? null,
346                         ],
347                 ]));
348
349                 $baseUrl = new BaseURL($config, []);
350
351                 $baseUrl->saveByURL($url);
352
353                 self::assertEquals($url, $baseUrl->get());
354         }
355
356         public function dataGetBaseUrl()
357         {
358                 return [
359                         'default'           => [
360                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
361                                 'ssl'       => false,
362                                 'url'       => 'http://friendica.local/new/test',
363                                 'assert'    => 'http://friendica.local/new/test',
364                         ],
365                         'DefaultWithSSL'    => [
366                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
367                                 'ssl'       => true,
368                                 'url'       => 'http://friendica.local/new/test',
369                                 'assert'    => 'https://friendica.local/new/test',
370                         ],
371                         'SSLFullWithSSL'    => [
372                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
373                                 'ssl'       => true,
374                                 'url'       => 'http://friendica.local/new/test',
375                                 'assert'    => 'http://friendica.local/new/test',
376                         ],
377                         'SSLFullWithoutSSL' => [
378                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
379                                 'ssl'       => false,
380                                 'url'       => 'https://friendica.local/new/test',
381                                 'assert'    => 'https://friendica.local/new/test',
382                         ],
383                         'NoSSLWithSSL'      => [
384                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
385                                 'ssl'       => true,
386                                 'url'       => 'http://friendica.local/new/test',
387                                 'assert'    => 'http://friendica.local/new/test',
388                         ],
389                         'NoSSLWithoutSSL'   => [
390                                 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
391                                 'ssl'       => false,
392                                 'url'       => 'http://friendica.local/new/test',
393                                 'assert'    => 'http://friendica.local/new/test',
394                         ],
395                 ];
396         }
397
398         /**
399          * Test the get() method
400          * @dataProvider dataGetBaseUrl
401          */
402         public function testGetURL($sslPolicy, $ssl, $url, $assert)
403         {
404                 $configMock = \Mockery::mock(IManageConfigValues::class);
405                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
406                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
407                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
408                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
409
410                 $baseUrl = new BaseURL($configMock, []);
411
412                 self::assertEquals($assert, $baseUrl->get($ssl));
413         }
414
415         public function dataCheckRedirectHTTPS()
416         {
417                 return [
418                         'default' => [
419                                 'server' => [
420                                         'REQUEST_METHOD' => 'GET',
421                                         'HTTPS' => true,
422                                 ],
423                                 'forceSSL'  => false,
424                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
425                                 'url'       => 'https://friendica.local',
426                                 'redirect'  => false,
427                         ],
428                         'forceSSL' => [
429                                 'server' => [
430                                         'REQUEST_METHOD' => 'GET',
431                                 ],
432                                 'forceSSL'  => true,
433                                 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
434                                 'url'       => 'https://friendica.local',
435                                 'redirect'  => false,
436                         ],
437                         'forceSSLWithSSLPolicy' => [
438                                 'server' => [],
439                                 'forceSSL'  => true,
440                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
441                                 'url'       => 'https://friendica.local',
442                                 'redirect'  => false,
443                         ],
444                         'forceSSLWithSSLPolicyAndGet' => [
445                                 'server' => [
446                                         'REQUEST_METHOD' => 'GET',
447                                 ],
448                                 'forceSSL'  => true,
449                                 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
450                                 'url'       => 'https://friendica.local',
451                                 'redirect'  => true,
452                         ],
453                 ];
454         }
455
456         /**
457          * Test the checkRedirectHTTPS() method
458          * @dataProvider dataCheckRedirectHTTPS
459          */
460         public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
461         {
462                 $configMock = \Mockery::mock(IManageConfigValues::class);
463                 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
464                 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
465                 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
466                 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($url);
467                 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($forceSSL);
468
469                 $baseUrl = new BaseURL($configMock, $server);
470
471                 self::assertEquals($redirect, $baseUrl->checkRedirectHttps());
472         }
473 }