2 namespace Friendica\Test\src\Util;
4 use Friendica\Core\Config\Configuration;
5 use Friendica\Util\BaseURL;
6 use PHPUnit\Framework\TestCase;
8 class BaseURLTest extends TestCase
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 $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();
304 $baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
306 $this->assertEquals($url, $baseUrl->get());
310 * Test the saveByUrl() method
311 * @dataProvider dataSave
317 public function testSaveByUrl($input, $save, $url)
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']);
326 $baseUrl = new BaseURL($configMock, []);
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();
333 $baseUrl->saveByURL($url);
335 $this->assertEquals($url, $baseUrl->get());
338 public function dataGetBaseUrl()
342 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
344 'url' => 'http://friendica.local/new/test',
345 'assert' => 'http://friendica.local/new/test',
347 'DefaultWithSSL' => [
348 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
350 'url' => 'http://friendica.local/new/test',
351 'assert' => 'https://friendica.local/new/test',
353 'SSLFullWithSSL' => [
354 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
356 'url' => 'http://friendica.local/new/test',
357 'assert' => 'http://friendica.local/new/test',
359 'SSLFullWithoutSSL' => [
360 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
362 'url' => 'https://friendica.local/new/test',
363 'assert' => 'https://friendica.local/new/test',
366 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
368 'url' => 'http://friendica.local/new/test',
369 'assert' => 'http://friendica.local/new/test',
371 'NoSSLWithoutSSL' => [
372 'sslPolicy' => BaseURL::SSL_POLICY_NONE,
374 'url' => 'http://friendica.local/new/test',
375 'assert' => 'http://friendica.local/new/test',
381 * Test the get() method
382 * @dataProvider dataGetBaseUrl
384 public function testGetURL($sslPolicy, $ssl, $url, $assert)
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);
392 $baseUrl = new BaseURL($configMock, []);
394 $this->assertEquals($assert, $baseUrl->get($ssl));
397 public function dataCheckRedirectHTTPS()
402 'REQUEST_METHOD' => 'GET',
406 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
407 'url' => 'https://friendica.local',
412 'REQUEST_METHOD' => 'GET',
415 'sslPolicy' => BaseURL::DEFAULT_SSL_SCHEME,
416 'url' => 'https://friendica.local',
419 'forceSSLWithSSLPolicy' => [
422 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
423 'url' => 'https://friendica.local',
426 'forceSSLWithSSLPolicyAndGet' => [
428 'REQUEST_METHOD' => 'GET',
431 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
432 'url' => 'https://friendica.local',
439 * Test the checkRedirectHTTPS() method
440 * @dataProvider dataCheckRedirectHTTPS
442 public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
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);
451 $baseUrl = new BaseURL($configMock, $server);
453 $this->assertEquals($redirect, $baseUrl->checkRedirectHttps());