2018-10-06 10:27:20 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 09:45:36 -05: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/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-10-06 10:27:20 -04:00
|
|
|
|
|
|
|
namespace Friendica\Test\src\App;
|
|
|
|
|
2019-08-16 03:46:38 -04:00
|
|
|
use Detection\MobileDetect;
|
2021-11-19 16:47:49 -05:00
|
|
|
use Friendica\App\Arguments;
|
2018-10-06 10:27:20 -04:00
|
|
|
use Friendica\App\Mode;
|
2021-10-26 15:44:29 -04:00
|
|
|
use Friendica\Core\Config\ValueObject\Cache;
|
2019-07-20 19:22:10 -04:00
|
|
|
use Friendica\Database\Database;
|
2018-11-01 08:44:47 -04:00
|
|
|
use Friendica\Test\MockedTest;
|
2018-10-06 10:27:20 -04:00
|
|
|
use Friendica\Test\Util\VFSTrait;
|
2019-07-20 19:22:10 -04:00
|
|
|
use Friendica\Util\BasePath;
|
2020-10-18 14:31:57 -04:00
|
|
|
use Mockery;
|
2019-07-20 19:22:10 -04:00
|
|
|
use Mockery\MockInterface;
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2018-11-01 08:44:47 -04:00
|
|
|
class ModeTest extends MockedTest
|
2018-10-06 10:27:20 -04:00
|
|
|
{
|
|
|
|
use VFSTrait;
|
|
|
|
|
2019-07-20 19:22:10 -04:00
|
|
|
/**
|
|
|
|
* @var BasePath|MockInterface
|
|
|
|
*/
|
|
|
|
private $basePathMock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Database|MockInterface
|
|
|
|
*/
|
|
|
|
private $databaseMock;
|
|
|
|
|
|
|
|
/**
|
2020-10-18 14:31:57 -04:00
|
|
|
* @var Cache|MockInterface
|
2019-07-20 19:22:10 -04:00
|
|
|
*/
|
|
|
|
private $configCacheMock;
|
|
|
|
|
2021-10-18 14:08:40 -04:00
|
|
|
protected function setUp(): void
|
2018-10-06 10:27:20 -04:00
|
|
|
{
|
2019-02-07 14:44:03 -05:00
|
|
|
parent::setUp();
|
2018-10-06 10:27:20 -04:00
|
|
|
|
|
|
|
$this->setUpVfsDir();
|
2019-07-20 19:22:10 -04:00
|
|
|
|
2021-10-18 14:08:40 -04:00
|
|
|
$this->basePathMock = Mockery::mock(BasePath::class);
|
|
|
|
$this->databaseMock = Mockery::mock(Database::class);
|
2020-10-18 14:31:57 -04:00
|
|
|
$this->configCacheMock = Mockery::mock(Cache::class);
|
2018-10-06 10:27:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItEmpty()
|
|
|
|
{
|
2019-08-12 12:13:58 -04:00
|
|
|
$mode = new Mode();
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->isInstall());
|
|
|
|
self::assertFalse($mode->isNormal());
|
2018-10-06 10:27:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithoutConfig()
|
|
|
|
{
|
2019-08-12 12:13:58 -04:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($this->root->hasChild('config/local.config.php'));
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2018-11-25 01:44:09 -05:00
|
|
|
$this->delConfigFile('local.config.php');
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($this->root->hasChild('config/local.config.php'));
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2019-08-12 12:13:58 -04:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->isInstall());
|
|
|
|
self::assertFalse($mode->isNormal());
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
|
2018-10-06 10:27:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithoutDatabase()
|
|
|
|
{
|
2019-08-12 12:13:58 -04:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-20 19:22:10 -04:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2019-08-12 12:13:58 -04:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($mode->isNormal());
|
|
|
|
self::assertTrue($mode->isInstall());
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
|
|
|
|
self::assertFalse($mode->has(Mode::DBAVAILABLE));
|
2018-10-06 10:27:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithoutDatabaseSetup()
|
|
|
|
{
|
2019-08-12 12:13:58 -04:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-20 19:22:10 -04:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
|
|
|
|
$this->databaseMock->shouldReceive('fetchFirst')
|
2021-10-18 14:08:40 -04:00
|
|
|
->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2019-08-12 12:13:58 -04:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($mode->isNormal());
|
|
|
|
self::assertTrue($mode->isInstall());
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
|
2018-10-06 10:27:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithMaintenanceMode()
|
|
|
|
{
|
2019-08-12 12:13:58 -04:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-20 19:22:10 -04:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
|
|
|
|
$this->databaseMock->shouldReceive('fetchFirst')
|
2021-10-18 14:08:40 -04:00
|
|
|
->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
|
2019-07-20 19:22:10 -04:00
|
|
|
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
|
2021-10-18 14:08:40 -04:00
|
|
|
->andReturn(true)->once();
|
2019-07-20 19:22:10 -04:00
|
|
|
|
2019-08-12 12:13:58 -04:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($mode->isNormal());
|
|
|
|
self::assertFalse($mode->isInstall());
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
|
|
|
|
self::assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
|
2018-10-06 10:27:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNormalMode()
|
|
|
|
{
|
2019-08-12 12:13:58 -04:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-20 19:22:10 -04:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
|
|
|
|
$this->databaseMock->shouldReceive('fetchFirst')
|
2021-10-18 14:08:40 -04:00
|
|
|
->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
|
2019-07-20 19:22:10 -04:00
|
|
|
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
|
2021-10-18 14:08:40 -04:00
|
|
|
->andReturn(false)->once();
|
2019-07-20 19:22:10 -04:00
|
|
|
$this->databaseMock->shouldReceive('selectFirst')
|
2021-10-18 14:08:40 -04:00
|
|
|
->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
|
|
|
|
->andReturn(['v' => null])->once();
|
2019-07-21 08:40:50 -04:00
|
|
|
|
2019-08-12 12:13:58 -04:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2019-07-21 08:40:50 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->isNormal());
|
|
|
|
self::assertFalse($mode->isInstall());
|
2019-07-21 08:40:50 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
|
|
|
|
self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
|
2019-07-21 08:40:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test explicit disabled maintenance (in case you manually disable it)
|
|
|
|
*/
|
|
|
|
public function testDisabledMaintenance()
|
|
|
|
{
|
2019-08-12 12:13:58 -04:00
|
|
|
$this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
|
|
|
|
|
2019-07-21 08:40:50 -04:00
|
|
|
$this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
|
|
|
|
$this->databaseMock->shouldReceive('fetchFirst')
|
2021-10-18 14:08:40 -04:00
|
|
|
->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
|
2019-07-21 08:40:50 -04:00
|
|
|
$this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
|
2021-10-18 14:08:40 -04:00
|
|
|
->andReturn(false)->once();
|
2019-07-21 08:40:50 -04:00
|
|
|
$this->databaseMock->shouldReceive('selectFirst')
|
2021-10-18 14:08:40 -04:00
|
|
|
->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
|
|
|
|
->andReturn(['v' => '0'])->once();
|
2019-07-20 19:22:10 -04:00
|
|
|
|
2019-08-12 12:13:58 -04:00
|
|
|
$mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->isNormal());
|
|
|
|
self::assertFalse($mode->isInstall());
|
2018-10-06 10:27:20 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
|
|
|
|
self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
|
2018-10-06 10:27:20 -04:00
|
|
|
}
|
2019-08-12 12:13:58 -04: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);
|
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertNotSame($modeNew, $mode);
|
2019-08-12 12:13:58 -04:00
|
|
|
}
|
2019-08-15 09:51:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if not called by index is backend
|
|
|
|
*/
|
2019-09-17 10:47:00 -04:00
|
|
|
public function testIsBackendNotIsBackend()
|
2019-08-15 09:51:15 -04:00
|
|
|
{
|
2021-10-18 14:08:40 -04:00
|
|
|
$server = [];
|
2021-11-19 16:47:49 -05:00
|
|
|
$args = new Arguments();
|
2019-08-16 03:46:38 -04:00
|
|
|
$mobileDetect = new MobileDetect();
|
2019-08-15 09:51:15 -04:00
|
|
|
|
2021-11-19 16:47:49 -05:00
|
|
|
$mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
|
2019-08-15 09:51:15 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->isBackend());
|
2019-08-15 09:51:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test is called by index but module is backend
|
|
|
|
*/
|
|
|
|
public function testIsBackendButIndex()
|
|
|
|
{
|
2021-10-18 14:08:40 -04:00
|
|
|
$server = [];
|
2021-11-19 16:47:49 -05:00
|
|
|
$args = new Arguments('', '', Mode::BACKEND_MODULES[0]);
|
2019-08-16 03:46:38 -04:00
|
|
|
$mobileDetect = new MobileDetect();
|
2019-08-15 09:51:15 -04:00
|
|
|
|
2021-11-19 16:47:49 -05:00
|
|
|
$mode = (new Mode())->determineRunMode(false, $server, $args, $mobileDetect);
|
2019-08-15 09:51:15 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->isBackend());
|
2019-08-15 09:51:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test is called by index and module is not backend
|
|
|
|
*/
|
|
|
|
public function testIsNotBackend()
|
|
|
|
{
|
2021-10-18 14:08:40 -04:00
|
|
|
$server = [];
|
2021-11-19 16:47:49 -05:00
|
|
|
$args = new Arguments('', '', Arguments::DEFAULT_MODULE);
|
2019-08-16 03:46:38 -04:00
|
|
|
$mobileDetect = new MobileDetect();
|
2019-08-15 09:51:15 -04:00
|
|
|
|
2021-11-19 16:47:49 -05:00
|
|
|
$mode = (new Mode())->determineRunMode(false, $server, $args, $mobileDetect);
|
2019-08-15 09:51:15 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($mode->isBackend());
|
2019-08-15 09:51:15 -04:00
|
|
|
}
|
2019-08-16 03:46:38 -04: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',
|
|
|
|
];
|
|
|
|
|
2021-11-19 16:47:49 -05:00
|
|
|
$args = new Arguments('', '', Arguments::DEFAULT_MODULE);
|
2019-08-16 03:46:38 -04:00
|
|
|
$mobileDetect = new MobileDetect();
|
|
|
|
|
2021-11-19 16:47:49 -05:00
|
|
|
$mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
|
2019-08-16 03:46:38 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->isAjax());
|
2019-08-16 03:46:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the call is not nan ajax call
|
|
|
|
*/
|
|
|
|
public function testIsNotAjax()
|
|
|
|
{
|
2021-10-18 14:08:40 -04:00
|
|
|
$server = [];
|
2021-11-19 16:47:49 -05:00
|
|
|
$args = new Arguments('', '', Arguments::DEFAULT_MODULE);
|
2019-08-16 03:46:38 -04:00
|
|
|
$mobileDetect = new MobileDetect();
|
|
|
|
|
2021-11-19 16:47:49 -05:00
|
|
|
$mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
|
2019-08-16 03:46:38 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($mode->isAjax());
|
2019-08-16 03:46:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the call is a mobile and is a tablet call
|
|
|
|
*/
|
|
|
|
public function testIsMobileIsTablet()
|
|
|
|
{
|
2021-10-18 14:08:40 -04:00
|
|
|
$server = [];
|
2021-11-19 16:47:49 -05:00
|
|
|
$args = new Arguments('', '', Arguments::DEFAULT_MODULE);
|
2020-10-18 14:31:57 -04:00
|
|
|
$mobileDetect = Mockery::mock(MobileDetect::class);
|
2019-08-16 03:46:38 -04:00
|
|
|
$mobileDetect->shouldReceive('isMobile')->andReturn(true);
|
|
|
|
$mobileDetect->shouldReceive('isTablet')->andReturn(true);
|
|
|
|
|
2021-11-19 16:47:49 -05:00
|
|
|
$mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
|
2019-08-16 03:46:38 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($mode->isMobile());
|
|
|
|
self::assertTrue($mode->isTablet());
|
2019-08-16 03:46:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the call is not a mobile and is not a tablet call
|
|
|
|
*/
|
|
|
|
public function testIsNotMobileIsNotTablet()
|
|
|
|
{
|
2021-10-18 14:08:40 -04:00
|
|
|
$server = [];
|
2021-11-19 16:47:49 -05:00
|
|
|
$args = new Arguments('', '', Arguments::DEFAULT_MODULE);
|
2020-10-18 14:31:57 -04:00
|
|
|
$mobileDetect = Mockery::mock(MobileDetect::class);
|
2019-08-16 03:46:38 -04:00
|
|
|
$mobileDetect->shouldReceive('isMobile')->andReturn(false);
|
|
|
|
$mobileDetect->shouldReceive('isTablet')->andReturn(false);
|
|
|
|
|
2021-11-19 16:47:49 -05:00
|
|
|
$mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
|
2019-08-16 03:46:38 -04:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($mode->isMobile());
|
|
|
|
self::assertFalse($mode->isTablet());
|
2019-08-16 03:46:38 -04:00
|
|
|
}
|
2018-10-06 10:27:20 -04:00
|
|
|
}
|