2018-10-06 16:27:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Test\src\App;
|
|
|
|
|
|
|
|
use Friendica\App\Mode;
|
2019-08-15 15:51:15 +02:00
|
|
|
use Friendica\App\Module;
|
2019-02-07 20:44:03 +01:00
|
|
|
use Friendica\Core\Config;
|
2019-07-21 01:22:10 +02:00
|
|
|
use Friendica\Database\Database;
|
2018-11-01 13:44:47 +01:00
|
|
|
use Friendica\Test\MockedTest;
|
|
|
|
use Friendica\Test\Util\DBAMockTrait;
|
2018-10-06 16:27:20 +02:00
|
|
|
use Friendica\Test\Util\VFSTrait;
|
2019-07-21 01:22:10 +02:00
|
|
|
use Friendica\Util\BasePath;
|
|
|
|
use Mockery\MockInterface;
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2018-11-01 13:44:47 +01:00
|
|
|
class ModeTest extends MockedTest
|
2018-10-06 16:27:20 +02:00
|
|
|
{
|
|
|
|
use VFSTrait;
|
2018-11-01 13:44:47 +01:00
|
|
|
use DBAMockTrait;
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
/**
|
|
|
|
* @var BasePath|MockInterface
|
|
|
|
*/
|
|
|
|
private $basePathMock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Database|MockInterface
|
|
|
|
*/
|
|
|
|
private $databaseMock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Config\Cache\ConfigCache|MockInterface
|
|
|
|
*/
|
|
|
|
private $configCacheMock;
|
|
|
|
|
2018-10-06 16:27:20 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
2019-02-07 20:44:03 +01:00
|
|
|
parent::setUp();
|
2018-10-06 16:27:20 +02:00
|
|
|
|
|
|
|
$this->setUpVfsDir();
|
2019-07-21 01:22:10 +02:00
|
|
|
|
|
|
|
$this->basePathMock = \Mockery::mock(BasePath::class);
|
|
|
|
$this->databaseMock = \Mockery::mock(Database::class);
|
|
|
|
$this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class);
|
2018-10-06 16:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItEmpty()
|
|
|
|
{
|
2019-08-12 18:13:58 +02:00
|
|
|
$mode = new Mode();
|
2018-10-06 16:27:20 +02:00
|
|
|
$this->assertTrue($mode->isInstall());
|
|
|
|
$this->assertFalse($mode->isNormal());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithoutConfig()
|
|
|
|
{
|
2019-08-12 18:13:58 +02:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2018-11-25 07:44:09 +01:00
|
|
|
$this->assertTrue($this->root->hasChild('config/local.config.php'));
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2018-11-25 07:44:09 +01:00
|
|
|
$this->delConfigFile('local.config.php');
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2018-11-25 07:44:09 +01:00
|
|
|
$this->assertFalse($this->root->hasChild('config/local.config.php'));
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 16:27:20 +02:00
|
|
|
|
|
|
|
$this->assertTrue($mode->isInstall());
|
|
|
|
$this->assertFalse($mode->isNormal());
|
|
|
|
|
|
|
|
$this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithoutDatabase()
|
|
|
|
{
|
2019-08-12 18:13:58 +02:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 16:27:20 +02:00
|
|
|
|
|
|
|
$this->assertFalse($mode->isNormal());
|
|
|
|
$this->assertTrue($mode->isInstall());
|
|
|
|
|
|
|
|
$this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
|
|
|
|
$this->assertFalse($mode->has(Mode::DBAVAILABLE));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithoutDatabaseSetup()
|
|
|
|
{
|
2019-08-12 18:13:58 +02:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
|
|
|
|
$this->databaseMock->shouldReceive('fetchFirst')
|
|
|
|
->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
|
2018-10-06 16:27:20 +02:00
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 16:27:20 +02:00
|
|
|
|
|
|
|
$this->assertFalse($mode->isNormal());
|
|
|
|
$this->assertTrue($mode->isInstall());
|
|
|
|
|
|
|
|
$this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithMaintenanceMode()
|
|
|
|
{
|
2019-08-12 18:13:58 +02:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
|
|
|
|
$this->databaseMock->shouldReceive('fetchFirst')
|
|
|
|
->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
|
|
|
|
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
|
|
|
|
->andReturn(true)->once();
|
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 16:27:20 +02:00
|
|
|
|
|
|
|
$this->assertFalse($mode->isNormal());
|
|
|
|
$this->assertFalse($mode->isInstall());
|
|
|
|
|
|
|
|
$this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
|
|
|
|
$this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNormalMode()
|
|
|
|
{
|
2019-08-12 18:13:58 +02:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-21 01:22:10 +02:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
|
|
|
|
$this->databaseMock->shouldReceive('fetchFirst')
|
|
|
|
->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
|
|
|
|
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
|
|
|
|
->andReturn(false)->once();
|
|
|
|
$this->databaseMock->shouldReceive('selectFirst')
|
|
|
|
->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
|
2019-07-21 14:40:50 +02:00
|
|
|
->andReturn(['v' => null])->once();
|
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2019-07-21 14:40:50 +02:00
|
|
|
|
|
|
|
$this->assertTrue($mode->isNormal());
|
|
|
|
$this->assertFalse($mode->isInstall());
|
|
|
|
|
|
|
|
$this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
|
|
|
|
$this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test explicit disabled maintenance (in case you manually disable it)
|
|
|
|
*/
|
|
|
|
public function testDisabledMaintenance()
|
|
|
|
{
|
2019-08-12 18:13:58 +02:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-21 14:40:50 +02:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
|
|
|
|
$this->databaseMock->shouldReceive('fetchFirst')
|
|
|
|
->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
|
|
|
|
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
|
|
|
|
->andReturn(false)->once();
|
|
|
|
$this->databaseMock->shouldReceive('selectFirst')
|
|
|
|
->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
|
|
|
|
->andReturn(['v' => '0'])->once();
|
2019-07-21 01:22:10 +02:00
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 16:27:20 +02:00
|
|
|
|
|
|
|
$this->assertTrue($mode->isNormal());
|
|
|
|
$this->assertFalse($mode->isInstall());
|
|
|
|
|
|
|
|
$this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
|
|
|
|
$this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
|
|
|
|
}
|
2019-08-12 18:13:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that modes are immutable
|
|
|
|
*/
|
|
|
|
public function testImmutable()
|
|
|
|
{
|
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
|
|
|
|
|
|
|
|
$mode = new Mode();
|
|
|
|
|
|
|
|
$modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
|
|
|
|
|
|
|
$this->assertNotSame($modeNew, $mode);
|
|
|
|
}
|
2019-08-15 15:51:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if not called by index is backend
|
|
|
|
*/
|
|
|
|
public function testIsBackendNotIndex()
|
|
|
|
{
|
|
|
|
$server = ['PHP_SELF' => '/daemon.php'];
|
|
|
|
$module = new Module();
|
|
|
|
|
|
|
|
$mode = (new Mode())->determineBackend($module, $server);
|
|
|
|
|
|
|
|
$this->assertTrue($mode->isBackend());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test is called by index but module is backend
|
|
|
|
*/
|
|
|
|
public function testIsBackendButIndex()
|
|
|
|
{
|
|
|
|
$server = ['PHP_SELF' => '/index.php'];
|
|
|
|
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true);
|
|
|
|
|
|
|
|
$mode = (new Mode())->determineBackend($module, $server);
|
|
|
|
|
|
|
|
$this->assertTrue($mode->isBackend());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test is called by index and module is not backend
|
|
|
|
*/
|
|
|
|
public function testIsNotBackend()
|
|
|
|
{
|
|
|
|
$server = ['PHP_SELF' => '/index.php'];
|
|
|
|
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
|
|
|
|
|
|
|
|
$mode = (new Mode())->determineBackend($module, $server);
|
|
|
|
|
|
|
|
$this->assertFalse($mode->isBackend());
|
|
|
|
}
|
2018-10-06 16:27:20 +02:00
|
|
|
}
|