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 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();
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();
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();
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();
202 $baseUrl = new BaseURL($configMock, $server);
204 $this->assertEquals($assert['hostname'], $baseUrl->getHostname());
205 $this->assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
206 $this->assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
207 $this->assertEquals($assert['scheme'], $baseUrl->getScheme());
208 $this->assertEquals($assert['url'], $baseUrl->get());
211 public function dataSave()
216 'hostname' => 'friendica.old',
217 'urlPath' => 'is/old/path',
218 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
219 'url' => 'http://friendica.old/is/old/path',
223 'hostname' => 'friendica.local',
224 'urlPath' => 'new/path',
225 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
227 'url' => 'https://friendica.local/new/path',
231 'hostname' => 'friendica.old',
232 'urlPath' => 'is/old/path',
233 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
234 'url' => 'http://friendica.old/is/old/path',
242 'url' => 'http://friendica.old/is/old/path',
244 'changeHostname' => [
246 'hostname' => 'friendica.old',
247 'urlPath' => 'is/old/path',
248 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
249 'url' => 'http://friendica.old/is/old/path',
253 'hostname' => 'friendica.local',
257 'url' => 'http://friendica.local/is/old/path',
261 'hostname' => 'friendica.old',
262 'urlPath' => 'is/old/path',
263 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
264 'url' => 'http://friendica.old/is/old/path',
269 'urlPath' => 'new/path',
272 'url' => 'http://friendica.old/new/path',
274 'changeSSLPolicy' => [
276 'hostname' => 'friendica.old',
277 'urlPath' => 'is/old/path',
278 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
279 'url' => 'http://friendica.old/is/old/path',
285 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
287 'url' => 'https://friendica.old/is/old/path',
293 * Test the save() method
294 * @dataProvider dataSave
296 public function testSave($input, $save, $url)
298 $configMock = \Mockery::mock(Configuration::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']);
305 $baseUrl = new BaseURL($configMock, []);
307 if (isset($save['hostname'])) {
308 $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
311 if (isset($save['urlPath'])) {
312 $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
315 if (isset($save['sslPolicy'])) {
316 $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
319 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
321 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
323 $this->assertEquals($url, $baseUrl->get());
327 * Test the saveByUrl() method
328 * @dataProvider dataSave
334 public function testSaveByUrl($input, $save, $url)
336 $configMock = \Mockery::mock(Configuration::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']);
343 $baseUrl = new BaseURL($configMock, []);
345 if (isset($save['hostname'])) {
346 $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
349 if (isset($save['urlPath'])) {
350 $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
353 if (isset($save['sslPolicy'])) {
354 $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
357 $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
359 $baseUrl->saveByURL($url);
361 $this->assertEquals($url, $baseUrl->get());
364 public function dataGetBaseUrl()
368 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
370 'url' => 'http://friendica.local/new/test',
371 'assert' => 'http://friendica.local/new/test',
373 'DefaultWithSSL' => [
374 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
376 'url' => 'http://friendica.local/new/test',
377 'assert' => 'https://friendica.local/new/test',
379 'SSLFullWithSSL' => [
380 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
382 'url' => 'http://friendica.local/new/test',
383 'assert' => 'http://friendica.local/new/test',
385 'SSLFullWithoutSSL' => [
386 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
388 'url' => 'https://friendica.local/new/test',
389 'assert' => 'https://friendica.local/new/test',
392 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
394 'url' => 'http://friendica.local/new/test',
395 'assert' => 'http://friendica.local/new/test',
397 'NoSSLWithoutSSL' => [
398 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
400 'url' => 'http://friendica.local/new/test',
401 'assert' => 'http://friendica.local/new/test',
407 * Test the get() method
408 * @dataProvider dataGetBaseUrl
410 public function testGetURL($sslPolicy, $ssl, $url, $assert)
412 $configMock = \Mockery::mock(Configuration::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);
418 $baseUrl = new BaseURL($configMock, []);
420 $this->assertEquals($assert, $baseUrl->get($ssl));
423 public function dataCheckRedirectHTTPS()
428 'REQUEST_METHOD' => 'GET',
432 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
433 'url' => 'https://friendica.local',
438 'REQUEST_METHOD' => 'GET',
441 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
442 'url' => 'https://friendica.local',
445 'forceSSLWithSSLPolicy' => [
448 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
449 'url' => 'https://friendica.local',
452 'forceSSLWithSSLPolicyAndGet' => [
454 'REQUEST_METHOD' => 'GET',
457 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
458 'url' => 'https://friendica.local',
465 * Test the checkRedirectHTTPS() method
466 * @dataProvider dataCheckRedirectHTTPS
468 public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
470 $configMock = \Mockery::mock(Configuration::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);
477 $baseUrl = new BaseURL($configMock, $server);
479 $this->assertEquals($redirect, $baseUrl->checkRedirectHttps());
482 public function dataWrongSave()
486 'fail' => 'hostname',
488 'wrongSSLPolicy' => [
489 'fail' => 'sslPolicy',
501 * Test the save() method with wrong parameters
502 * @dataProvider dataWrongSave
504 public function testWrongSave($fail)
506 $configMock = \Mockery::mock(Configuration::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');
514 $configMock->shouldReceive('set')->with('config', 'hostname', \Mockery::any())->andReturn(false)->once();
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();
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();
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();
533 $baseUrl = new BaseURL($configMock, []);
534 $this->assertFalse($baseUrl->save('test', 10, 'nope'));
536 // nothing should have changed because we never successfully saved anything
537 $this->assertEquals($baseUrl->getHostname(), 'friendica.local');
538 $this->assertEquals($baseUrl->getUrlPath(), 'new/test');
539 $this->assertEquals($baseUrl->getSSLPolicy(), BaseURL::DEFAULT_SSL_SCHEME);
540 $this->assertEquals($baseUrl->get(), 'http://friendica.local/new/test');