X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=tests%2Fsrc%2FApp%2FModeTest.php;h=8955a50e7c4dd2d622080f2ed6a94c9590c1cf45;hb=273f4c352e9c521444cda44cf95b10b72cabab11;hp=19dad07cd6eae94af0e590cc0f410889eaa6e38b;hpb=3aa77685fcccff9739384136b4894de43200fa8c;p=friendica.git diff --git a/tests/src/App/ModeTest.php b/tests/src/App/ModeTest.php index 19dad07cd6..8955a50e7c 100644 --- a/tests/src/App/ModeTest.php +++ b/tests/src/App/ModeTest.php @@ -1,149 +1,309 @@ . + * + */ namespace Friendica\Test\src\App; +use Detection\MobileDetect; +use Friendica\App\Arguments; use Friendica\App\Mode; -use Friendica\Core\Config; +use Friendica\Core\Config\ValueObject\Cache; +use Friendica\Database\Database; use Friendica\Test\MockedTest; -use Friendica\Test\Util\DBAMockTrait; use Friendica\Test\Util\VFSTrait; +use Friendica\Util\BasePath; +use Mockery; +use Mockery\MockInterface; class ModeTest extends MockedTest { use VFSTrait; - use DBAMockTrait; - public function setUp() + /** + * @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() { - $mode = new Mode($this->root->url()); - $this->assertTrue($mode->isInstall()); - $this->assertFalse($mode->isNormal()); + $mode = new Mode(); + self::assertTrue($mode->isInstall()); + self::assertFalse($mode->isNormal()); } public function testWithoutConfig() { - $mode = new Mode($this->root->url()); + $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once(); - $this->assertTrue($this->root->hasChild('config/local.config.php')); + self::assertTrue($this->root->hasChild('config/local.config.php')); $this->delConfigFile('local.config.php'); - $this->assertFalse($this->root->hasChild('config/local.config.php')); + self::assertFalse($this->root->hasChild('config/local.config.php')); + + $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() + { + $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once(); + + $this->databaseMock->shouldReceive('connected')->andReturn(false)->once(); + + $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() + { + $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(); + + $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() + { + $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(); + + $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() + { + $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(); - $mode->determine(); + $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertTrue($mode->isInstall()); - $this->assertFalse($mode->isNormal()); + self::assertTrue($mode->isNormal()); + self::assertFalse($mode->isInstall()); - $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT)); + self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); + self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED)); } /** - * @runInSeparateProcess - * @preserveGlobalState disabled + * Test explicit disabled maintenance (in case you manually disable it) */ - public function testWithoutDatabase() + public function testDisabledMaintenance() { - $this->mockConnected(false, 1); + $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' => '0'])->once(); - $mode = new Mode($this->root->url()); - $mode->determine(); + $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertFalse($mode->isNormal()); - $this->assertTrue($mode->isInstall()); + self::assertTrue($mode->isNormal()); + self::assertFalse($mode->isInstall()); - $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT)); - $this->assertFalse($mode->has(Mode::DBAVAILABLE)); + self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); + self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED)); } /** - * @runInSeparateProcess - * @preserveGlobalState disabled + * Test that modes are immutable */ - public function testWithoutDatabaseSetup() + public function testImmutable() { - $this->mockConnected(true, 1); - $this->mockFetchFirst('SHOW TABLES LIKE \'config\'', false, 1); + $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once(); - $mode = new Mode($this->root->url()); - $mode->determine(); + $mode = new Mode(); - $this->assertFalse($mode->isNormal()); - $this->assertTrue($mode->isInstall()); + $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT)); + self::assertNotSame($modeNew, $mode); } /** - * @runInSeparateProcess - * @preserveGlobalState disabled + * Test if not called by index is backend */ - public function testWithMaintenanceMode() + public function testIsBackendNotIsBackend() { - $this->mockConnected(true, 1); - $this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1); + $server = []; + $args = new Arguments(); + $mobileDetect = new MobileDetect(); - $config = \Mockery::mock('Friendica\Core\Config\ConfigCache'); - $config - ->shouldReceive('get') - ->with('system', 'maintenance', null) - ->andReturn(true) - ->once(); - // Initialize empty Config - Config::init($config); - $configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter'); - $configAdapter - ->shouldReceive('isConnected') - ->andReturn(false); - Config::setAdapter($configAdapter); + $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect); - $mode = new Mode($this->root->url()); - $mode->determine(); + self::assertTrue($mode->isBackend()); + } - $this->assertFalse($mode->isNormal()); - $this->assertFalse($mode->isInstall()); + /** + * Test is called by index but module is backend + */ + public function testIsBackendButIndex() + { + $server = []; + $args = new Arguments('', '', Mode::BACKEND_MODULES[0]); + $mobileDetect = new MobileDetect(); + + $mode = (new Mode())->determineRunMode(false, $server, $args, $mobileDetect); - $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); - $this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED)); + self::assertTrue($mode->isBackend()); } /** - * @runInSeparateProcess - * @preserveGlobalState disabled + * Test is called by index and module is not backend */ - public function testNormalMode() + public function testIsNotBackend() + { + $server = []; + $args = new Arguments('', '', Arguments::DEFAULT_MODULE); + $mobileDetect = new MobileDetect(); + + $mode = (new Mode())->determineRunMode(false, $server, $args, $mobileDetect); + + self::assertFalse($mode->isBackend()); + } + + /** + * 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', + ]; + + $args = new Arguments('', '', Arguments::DEFAULT_MODULE); + $mobileDetect = new MobileDetect(); + + $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect); + + self::assertTrue($mode->isAjax()); + } + + /** + * Test if the call is not nan ajax call + */ + public function testIsNotAjax() + { + $server = []; + $args = new Arguments('', '', Arguments::DEFAULT_MODULE); + $mobileDetect = new MobileDetect(); + + $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect); + + self::assertFalse($mode->isAjax()); + } + + /** + * Test if the call is a mobile and is a tablet call + */ + public function testIsMobileIsTablet() { - $this->mockConnected(true, 1); - $this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1); - - $config = \Mockery::mock('Friendica\Core\Config\ConfigCache'); - $config - ->shouldReceive('get') - ->with('system', 'maintenance', null) - ->andReturn(false) - ->once(); - // Initialize empty Config - Config::init($config); - $configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter'); - $configAdapter - ->shouldReceive('isConnected') - ->andReturn(false); - Config::setAdapter($configAdapter); - - $mode = new Mode($this->root->url()); - $mode->determine(); - - $this->assertTrue($mode->isNormal()); - $this->assertFalse($mode->isInstall()); - - $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); - $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED)); + $server = []; + $args = new Arguments('', '', Arguments::DEFAULT_MODULE); + $mobileDetect = Mockery::mock(MobileDetect::class); + $mobileDetect->shouldReceive('isMobile')->andReturn(true); + $mobileDetect->shouldReceive('isTablet')->andReturn(true); + + $mode = (new Mode())->determineRunMode(true, $server, $args, $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 = []; + $args = new Arguments('', '', Arguments::DEFAULT_MODULE); + $mobileDetect = Mockery::mock(MobileDetect::class); + $mobileDetect->shouldReceive('isMobile')->andReturn(false); + $mobileDetect->shouldReceive('isTablet')->andReturn(false); + + $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect); + + self::assertFalse($mode->isMobile()); + self::assertFalse($mode->isTablet()); } }