friendica/tests/src/App/ModeTest.php

310 lines
9.2 KiB
PHP
Raw Normal View History

<?php
2020-02-09 15:45:36 +01:00
/**
2021-03-29 08:40:20 +02:00
* @copyright Copyright (C) 2010-2021, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @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\App;
use Detection\MobileDetect;
use Friendica\App\Mode;
2019-08-15 15:51:15 +02:00
use Friendica\App\Module;
use Friendica\Core\Config\Cache;
use Friendica\Database\Database;
2018-11-01 13:44:47 +01:00
use Friendica\Test\MockedTest;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\BasePath;
use Mockery;
use Mockery\MockInterface;
2018-11-01 13:44:47 +01:00
class ModeTest extends MockedTest
{
use VFSTrait;
/**
* @var BasePath|MockInterface
*/
private $basePathMock;
/**
* @var Database|MockInterface
*/
private $databaseMock;
/**
* @var Cache|MockInterface
*/
private $configCacheMock;
protected function setUp(): void
{
parent::setUp();
$this->setUpVfsDir();
$this->basePathMock = Mockery::mock(BasePath::class);
$this->databaseMock = Mockery::mock(Database::class);
$this->configCacheMock = Mockery::mock(Cache::class);
}
public function testItEmpty()
{
2019-08-12 18:13:58 +02:00
$mode = new Mode();
self::assertTrue($mode->isInstall());
self::assertFalse($mode->isNormal());
}
public function testWithoutConfig()
{
2019-08-12 18:13:58 +02:00
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
self::assertTrue($this->root->hasChild('config/local.config.php'));
$this->delConfigFile('local.config.php');
self::assertFalse($this->root->hasChild('config/local.config.php'));
2019-08-12 18:13:58 +02:00
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
self::assertTrue($mode->isInstall());
self::assertFalse($mode->isNormal());
self::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();
$this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
2019-08-12 18:13:58 +02:00
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
self::assertFalse($mode->isNormal());
self::assertTrue($mode->isInstall());
self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
self::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();
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
$this->databaseMock->shouldReceive('fetchFirst')
->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
2019-08-12 18:13:58 +02:00
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
self::assertFalse($mode->isNormal());
self::assertTrue($mode->isInstall());
self::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();
$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);
self::assertFalse($mode->isNormal());
self::assertFalse($mode->isInstall());
self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
self::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();
$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' => null])->once();
2019-07-21 14:40:50 +02:00
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
self::assertTrue($mode->isNormal());
self::assertFalse($mode->isInstall());
2019-07-21 14:40:50 +02:00
self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
2019-07-21 14:40:50 +02:00
}
/**
* 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();
2019-07-21 14:40:50 +02:00
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
->andReturn(false)->once();
2019-07-21 14:40:50 +02:00
$this->databaseMock->shouldReceive('selectFirst')
->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
->andReturn(['v' => '0'])->once();
2019-08-12 18:13:58 +02:00
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
self::assertTrue($mode->isNormal());
self::assertFalse($mode->isInstall());
self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
self::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);
self::assertNotSame($modeNew, $mode);
2019-08-12 18:13:58 +02:00
}
2019-08-15 15:51:15 +02:00
/**
* Test if not called by index is backend
*/
2019-09-17 16:47:00 +02:00
public function testIsBackendNotIsBackend()
2019-08-15 15:51:15 +02:00
{
$server = [];
$module = new Module();
$mobileDetect = new MobileDetect();
2019-08-15 15:51:15 +02:00
2019-09-17 16:47:00 +02:00
$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
2019-08-15 15:51:15 +02:00
self::assertTrue($mode->isBackend());
2019-08-15 15:51:15 +02:00
}
/**
* Test is called by index but module is backend
*/
public function testIsBackendButIndex()
{
$server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], true);
$mobileDetect = new MobileDetect();
2019-08-15 15:51:15 +02:00
2019-09-17 16:47:00 +02:00
$mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
2019-08-15 15:51:15 +02:00
self::assertTrue($mode->isBackend());
2019-08-15 15:51:15 +02:00
}
/**
* Test is called by index and module is not backend
*/
public function testIsNotBackend()
{
$server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = new MobileDetect();
2019-08-15 15:51:15 +02:00
2019-09-17 16:47:00 +02:00
$mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
2019-08-15 15:51:15 +02:00
self::assertFalse($mode->isBackend());
2019-08-15 15:51:15 +02:00
}
/**
* Test if the call is an ajax call
*/
public function testIsAjax()
{
// This is the server environment variable to determine ajax calls
$server = [
'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = new MobileDetect();
2019-09-17 16:47:00 +02:00
$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
self::assertTrue($mode->isAjax());
}
/**
* Test if the call is not nan ajax call
*/
public function testIsNotAjax()
{
$server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = new MobileDetect();
2019-09-17 16:47:00 +02:00
$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
self::assertFalse($mode->isAjax());
}
/**
* Test if the call is a mobile and is a tablet call
*/
public function testIsMobileIsTablet()
{
$server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = Mockery::mock(MobileDetect::class);
$mobileDetect->shouldReceive('isMobile')->andReturn(true);
$mobileDetect->shouldReceive('isTablet')->andReturn(true);
2019-09-17 16:47:00 +02:00
$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
self::assertTrue($mode->isMobile());
self::assertTrue($mode->isTablet());
}
/**
* Test if the call is not a mobile and is not a tablet call
*/
public function testIsNotMobileIsNotTablet()
{
$server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = Mockery::mock(MobileDetect::class);
$mobileDetect->shouldReceive('isMobile')->andReturn(false);
$mobileDetect->shouldReceive('isTablet')->andReturn(false);
2019-09-17 16:47:00 +02:00
$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
self::assertFalse($mode->isMobile());
self::assertFalse($mode->isTablet());
}
}