2 namespace Friendica\Test\src\Util;
4 use Friendica\Core\Config\Configuration;
5 use Friendica\Test\MockedTest;
6 use Friendica\Util\BaseURL;
8 class BaseURLTest extends MockedTest
10 public function dataDefault()
24 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
29 'WithSubDirectory' => [
31 'SERVER_NAME' => 'friendica.local',
32 'REDIRECT_URI' => 'test/module/more',
33 'QUERY_STRING' => 'module/more',
42 'hostname' => 'friendica.local',
44 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
45 'url' => 'http://friendica.local/test',
52 'hostname' => 'friendica.local',
54 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
55 'url' => 'http://friendica.local/test',
58 'hostname' => 'friendica.local',
60 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
61 'url' => 'http://friendica.local/test',
65 'WithHttpsScheme' => [
67 'SERVER_NAME' => 'friendica.local',
68 'REDIRECT_URI' => 'test/module/more',
69 'QUERY_STRING' => 'module/more',
79 'hostname' => 'friendica.local',
81 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
82 'url' => 'https://friendica.local/test',
86 'WithoutQueryString' => [
88 'SERVER_NAME' => 'friendica.local',
89 'REDIRECT_URI' => 'test/more',
99 'hostname' => 'friendica.local',
100 'urlPath' => 'test/more',
101 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
102 'url' => 'https://friendica.local/test/more',
108 'SERVER_NAME' => 'friendica.local',
109 'SERVER_PORT' => '1234',
110 'REDIRECT_URI' => 'test/more',
120 'hostname' => 'friendica.local:1234',
121 'urlPath' => 'test/more',
122 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
123 'url' => 'https://friendica.local:1234/test/more',
129 'SERVER_NAME' => 'friendica.local',
130 'SERVER_PORT' => '443',
131 'REDIRECT_URI' => 'test/more',
140 'hostname' => 'friendica.local',
141 'urlPath' => 'test/more',
142 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
143 'url' => 'https://friendica.local/test/more',
149 'SERVER_NAME' => 'friendica.local',
150 'SERVER_PORT' => '80',
151 'REDIRECT_URI' => 'test/more',
160 'hostname' => 'friendica.local',
161 'urlPath' => 'test/more',
162 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
163 'url' => 'http://friendica.local/test/more',
171 * Test the default config determination
172 * @dataProvider dataDefault
174 public function testCheck($server, $input, $assert)
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']);
182 if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
183 $configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
186 if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
187 $configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
190 if (!isset($input['hostname']) && !empty($assert['hostname'])) {
191 $configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
194 $baseUrl = new BaseURL($configMock, $server);
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());
203 public function dataSave()
208 'hostname' => 'friendica.old',
209 'urlPath' => 'is/old/path',
210 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
211 'url' => 'http://friendica.old/is/old/path',
215 'hostname' => 'friendica.local',
216 'urlPath' => 'new/path',
217 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
219 'url' => 'https://friendica.local/new/path',
223 'hostname' => 'friendica.old',
224 'urlPath' => 'is/old/path',
225 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
226 'url' => 'http://friendica.old/is/old/path',
234 'url' => 'http://friendica.old/is/old/path',
236 'changeHostname' => [
238 'hostname' => 'friendica.old',
239 'urlPath' => 'is/old/path',
240 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
241 'url' => 'http://friendica.old/is/old/path',
245 'hostname' => 'friendica.local',
249 'url' => 'http://friendica.local/is/old/path',
253 'hostname' => 'friendica.old',
254 'urlPath' => 'is/old/path',
255 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
256 'url' => 'http://friendica.old/is/old/path',
261 'urlPath' => 'new/path',
264 'url' => 'http://friendica.old/new/path',
266 'changeSSLPolicy' => [
268 'hostname' => 'friendica.old',
269 'urlPath' => 'is/old/path',
270 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
271 'url' => 'http://friendica.old/is/old/path',
277 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
279 'url' => 'https://friendica.old/is/old/path',
285 * Test the save() method
286 * @dataProvider dataSave
288 public function testSave($input, $save, $url)
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']);
297 $baseUrl = new BaseURL($configMock, []);
299 if (isset($save['hostname'])) {
300 $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
303 if (isset($save['urlPath'])) {
304 $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
307 if (isset($save['sslPolicy'])) {
308 $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
311 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
313 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
315 $this->assertEquals($url, $baseUrl->get());
319 * Test the saveByUrl() method
320 * @dataProvider dataSave
326 public function testSaveByUrl($input, $save, $url)
328 $configMock = \Mockery::mock(Configuration::class);
329 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
330 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
331 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
332 $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
333 $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
335 $baseUrl = new BaseURL($configMock, []);
337 if (isset($save['hostname'])) {
338 $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
341 if (isset($save['urlPath'])) {
342 $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
345 if (isset($save['sslPolicy'])) {
346 $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
349 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
351 $baseUrl->saveByURL($url);
353 $this->assertEquals($url, $baseUrl->get());
356 public function dataGetBaseUrl()
360 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
362 'url' => 'http://friendica.local/new/test',
363 'assert' => 'http://friendica.local/new/test',
365 'DefaultWithSSL' => [
366 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
368 'url' => 'http://friendica.local/new/test',
369 'assert' => 'https://friendica.local/new/test',
371 'SSLFullWithSSL' => [
372 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
374 'url' => 'http://friendica.local/new/test',
375 'assert' => 'http://friendica.local/new/test',
377 'SSLFullWithoutSSL' => [
378 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
380 'url' => 'https://friendica.local/new/test',
381 'assert' => 'https://friendica.local/new/test',
384 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
386 'url' => 'http://friendica.local/new/test',
387 'assert' => 'http://friendica.local/new/test',
389 'NoSSLWithoutSSL' => [
390 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
392 'url' => 'http://friendica.local/new/test',
393 'assert' => 'http://friendica.local/new/test',
399 * Test the get() method
400 * @dataProvider dataGetBaseUrl
402 public function testGetURL($sslPolicy, $ssl, $url, $assert)
404 $configMock = \Mockery::mock(Configuration::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);
410 $baseUrl = new BaseURL($configMock, []);
412 $this->assertEquals($assert, $baseUrl->get($ssl));
415 public function dataCheckRedirectHTTPS()
420 'REQUEST_METHOD' => 'GET',
424 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
425 'url' => 'https://friendica.local',
430 'REQUEST_METHOD' => 'GET',
433 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
434 'url' => 'https://friendica.local',
437 'forceSSLWithSSLPolicy' => [
440 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
441 'url' => 'https://friendica.local',
444 'forceSSLWithSSLPolicyAndGet' => [
446 'REQUEST_METHOD' => 'GET',
449 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
450 'url' => 'https://friendica.local',
457 * Test the checkRedirectHTTPS() method
458 * @dataProvider dataCheckRedirectHTTPS
460 public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
462 $configMock = \Mockery::mock(Configuration::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);
469 $baseUrl = new BaseURL($configMock, $server);
471 $this->assertEquals($redirect, $baseUrl->checkRedirectHttps());
474 public function dataWrongSave()
478 'fail' => 'hostname',
480 'wrongSSLPolicy' => [
481 'fail' => 'sslPolicy',
493 * Test the save() method with wrong parameters
494 * @dataProvider dataWrongSave
496 public function testWrongSave($fail)
498 $configMock = \Mockery::mock(Configuration::class);
499 $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
500 $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
501 $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(BaseURL::DEFAULT_SSL_SCHEME);
502 $configMock->shouldReceive('get')->with('system', 'url')->andReturn('http://friendica.local/new/test');
506 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(false)->once();
509 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
510 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(false)->once();
513 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
514 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
515 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(false)->once();
518 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(true)->twice();
519 $configMock->shouldReceive('set')->with('system', 'ssl_policy', \Mockery::any())->andReturn(true)->twice();
520 $configMock->shouldReceive('set')->with('system', 'urlpath', \Mockery::any())->andReturn(true)->twice();
521 $configMock->shouldReceive('set')->with('system', 'url', \Mockery::any())->andReturn(false)->once();
525 $baseUrl = new BaseURL($configMock, []);
526 $this->assertFalse($baseUrl->save('test', 10, 'nope'));
528 // nothing should have changed because we never successfully saved anything
529 $this->assertEquals($baseUrl->getHostname(), 'friendica.local');
530 $this->assertEquals($baseUrl->getUrlPath(), 'new/test');
531 $this->assertEquals($baseUrl->getSSLPolicy(), BaseURL::DEFAULT_SSL_SCHEME);
532 $this->assertEquals($baseUrl->get(), 'http://friendica.local/new/test');