<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
namespace Friendica\Test\src\Util;
-use Friendica\Core\Config\Configuration;
-use Friendica\Util\BaseURL;
-use PHPUnit\Framework\TestCase;
+use Friendica\App\BaseURL;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
+use Friendica\Core\Config\Model\Config;
+use Friendica\Core\Config\Model\ConfigTransaction;
+use Friendica\Core\Config\Util\ConfigFileManager;
+use Friendica\Core\Config\ValueObject\Cache;
+use Friendica\Test\MockedTest;
+use Friendica\Test\Util\VFSTrait;
-class BaseURLTest extends TestCase
+class BaseURLTest extends MockedTest
{
+ use VFSTrait;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->setUpVfsDir();
+ }
+
public function dataDefault()
{
return [
*/
public function testCheck($server, $input, $assert)
{
- $configMock = \Mockery::mock(Configuration::class);
+ $configMock = \Mockery::mock(IManageConfigValues::class);
$configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
$configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
$configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
$configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
+ // If we don't have an urlPath as an input, we assert it, we will save it to the DB for the next time
if (!isset($input['urlPath']) && isset($assert['urlPath'])) {
$configMock->shouldReceive('set')->with('system', 'urlpath', $assert['urlPath'])->once();
}
+ // 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
if (!isset($input['sslPolicy']) && isset($assert['sslPolicy'])) {
$configMock->shouldReceive('set')->with('system', 'ssl_policy', $assert['sslPolicy'])->once();
}
- if (!isset($input['hostname']) && !empty($assert['hostname'])) {
+ // If we don't have the hostname as an input, we assert it, we will save it to the DB for the next time
+ if (empty($input['hostname']) && !empty($assert['hostname'])) {
$configMock->shouldReceive('set')->with('config', 'hostname', $assert['hostname'])->once();
}
+ // If we don't have an URL at first, but we assert it, we will save it to the DB for the next time
+ if (empty($input['url']) && !empty($assert['url'])) {
+ $configMock->shouldReceive('set')->with('system', 'url', $assert['url'])->once();
+ }
+
$baseUrl = new BaseURL($configMock, $server);
- $this->assertEquals($assert['hostname'], $baseUrl->getHostname());
- $this->assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
- $this->assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
- $this->assertEquals($assert['scheme'], $baseUrl->getScheme());
- $this->assertEquals($assert['url'], $baseUrl->get());
+ self::assertEquals($assert['hostname'], $baseUrl->getHostname());
+ self::assertEquals($assert['urlPath'], $baseUrl->getUrlPath());
+ self::assertEquals($assert['sslPolicy'], $baseUrl->getSSLPolicy());
+ self::assertEquals($assert['scheme'], $baseUrl->getScheme());
+ self::assertEquals($assert['url'], $baseUrl->get());
}
public function dataSave()
{
return [
+ 'no_change' => [
+ 'input' => [
+ 'hostname' => 'friendica.local',
+ 'urlPath' => 'path',
+ 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
+ 'url' => 'https://friendica.local/path',
+ 'force_ssl' => true,
+ ],
+ 'save' => [
+ 'hostname' => 'friendica.local',
+ 'urlPath' => 'path',
+ 'sslPolicy' => BaseURL::SSL_POLICY_FULL,
+ ],
+ 'url' => 'https://friendica.local/path',
+ ],
'default' => [
'input' => [
'hostname' => 'friendica.old',
*/
public function testSave($input, $save, $url)
{
- $configMock = \Mockery::mock(Configuration::class);
- $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
- $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
- $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
- $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
- $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
-
- $baseUrl = new BaseURL($configMock, []);
+ $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
+ $config = new Config($configFileManager, new Cache([
+ 'config' => [
+ 'hostname' => $input['hostname'] ?? null,
+ ],
+ 'system' => [
+ 'urlpath' => $input['urlPath'] ?? null,
+ 'ssl_policy' => $input['sslPolicy'] ?? null,
+ 'url' => $input['url'] ?? null,
+ 'force_ssl' => $input['force_ssl'] ?? null,
+ ],
+ ]));
- $configMock->shouldReceive('set')->with('config', 'hostname', $save['hostname'])->andReturn(true)->once();
- $configMock->shouldReceive('set')->with('system', 'urlpath', $save['urlPath'])->andReturn(true)->once();
- $configMock->shouldReceive('set')->with('system', 'ssl_policy', $save['sslPolicy'])->andReturn(true)->once();
- $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
+ $baseUrl = new BaseURL($config, []);
$baseUrl->save($save['hostname'], $save['sslPolicy'], $save['urlPath']);
- $this->assertEquals($url, $baseUrl->get());
+ self::assertEquals($url, $baseUrl->get());
}
/**
*/
public function testSaveByUrl($input, $save, $url)
{
- $configMock = \Mockery::mock(Configuration::class);
- $configMock->shouldReceive('get')->with('config', 'hostname')->andReturn($input['hostname']);
- $configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn($input['urlPath']);
- $configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($input['sslPolicy']);
- $configMock->shouldReceive('get')->with('system', 'url')->andReturn($input['url']);
- $configMock->shouldReceive('get')->with('system', 'force_ssl')->andReturn($input['force_ssl']);
-
- $baseUrl = new BaseURL($configMock, []);
+ $configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
+ $config = new Config($configFileManager, new Cache([
+ 'config' => [
+ 'hostname' => $input['hostname'] ?? null,
+ ],
+ 'system' => [
+ 'urlpath' => $input['urlPath'] ?? null,
+ 'ssl_policy' => $input['sslPolicy'] ?? null,
+ 'url' => $input['url'] ?? null,
+ 'force_ssl' => $input['force_ssl'] ?? null,
+ ],
+ ]));
- $configMock->shouldReceive('set')->with('config', 'hostname', (!empty($save['hostname']) ? $save['hostname'] : $input['hostname']))->andReturn(true)->once();
- $configMock->shouldReceive('set')->with('system', 'urlpath', (!empty($save['urlPath']) ? $save['urlPath'] : $input['urlPath']))->andReturn(true)->once();
- $configMock->shouldReceive('set')->with('system', 'ssl_policy', (!empty($save['sslPolicy']) ? $save['sslPolicy'] : $input['sslPolicy']))->andReturn(true)->once();
- $configMock->shouldReceive('set')->with('system', 'url', $url)->andReturn(true)->once();
+ $baseUrl = new BaseURL($config, []);
$baseUrl->saveByURL($url);
- $this->assertEquals($url, $baseUrl->get());
+ self::assertEquals($url, $baseUrl->get());
}
public function dataGetBaseUrl()
*/
public function testGetURL($sslPolicy, $ssl, $url, $assert)
{
- $configMock = \Mockery::mock(Configuration::class);
+ $configMock = \Mockery::mock(IManageConfigValues::class);
$configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
$configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
$configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
$baseUrl = new BaseURL($configMock, []);
- $this->assertEquals($assert, $baseUrl->get($ssl));
+ self::assertEquals($assert, $baseUrl->get($ssl));
}
public function dataCheckRedirectHTTPS()
*/
public function testCheckRedirectHTTPS($server, $forceSSL, $sslPolicy, $url, $redirect)
{
- $configMock = \Mockery::mock(Configuration::class);
+ $configMock = \Mockery::mock(IManageConfigValues::class);
$configMock->shouldReceive('get')->with('config', 'hostname')->andReturn('friendica.local');
$configMock->shouldReceive('get')->with('system', 'urlpath')->andReturn('new/test');
$configMock->shouldReceive('get')->with('system', 'ssl_policy')->andReturn($sslPolicy);
$baseUrl = new BaseURL($configMock, $server);
- $this->assertEquals($redirect, $baseUrl->checkRedirectHttps());
+ self::assertEquals($redirect, $baseUrl->checkRedirectHttps());
}
}