X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=tests%2Fsrc%2FApp%2FModeTest.php;h=c3a5c5e72b4f99f0c10edf3f573a9b1bcfcf890e;hb=ab83d0dd27e46e733ecb49f25789bfb318852ac7;hp=30d0dc531ba3872ace4929f6fad2d0438dab5529;hpb=e9fc2af1c30601c4a919cd3bae49a5fc568a9878;p=friendica.git diff --git a/tests/src/App/ModeTest.php b/tests/src/App/ModeTest.php index 30d0dc531b..c3a5c5e72b 100644 --- a/tests/src/App/ModeTest.php +++ b/tests/src/App/ModeTest.php @@ -1,20 +1,40 @@ . + * + */ namespace Friendica\Test\src\App; +use Detection\MobileDetect; use Friendica\App\Mode; -use Friendica\Core\Config; +use Friendica\App\Module; +use Friendica\Core\Config\Cache\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; /** * @var BasePath|MockInterface @@ -27,44 +47,44 @@ class ModeTest extends MockedTest private $databaseMock; /** - * @var Config\Cache\ConfigCache|MockInterface + * @var Cache|MockInterface */ private $configCacheMock; - public function setUp() + protected function setUp(): void { parent::setUp(); $this->setUpVfsDir(); - $this->basePathMock = \Mockery::mock(BasePath::class); - $this->databaseMock = \Mockery::mock(Database::class); - $this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class); + $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->assertTrue($mode->isInstall()); - $this->assertFalse($mode->isNormal()); + self::assertTrue($mode->isInstall()); + self::assertFalse($mode->isNormal()); } public function testWithoutConfig() { $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); - $this->assertTrue($mode->isInstall()); - $this->assertFalse($mode->isNormal()); + self::assertTrue($mode->isInstall()); + self::assertFalse($mode->isNormal()); - $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT)); + self::assertFalse($mode->has(Mode::LOCALCONFIGPRESENT)); } public function testWithoutDatabase() @@ -75,11 +95,11 @@ class ModeTest extends MockedTest $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertFalse($mode->isNormal()); - $this->assertTrue($mode->isInstall()); + self::assertFalse($mode->isNormal()); + self::assertTrue($mode->isInstall()); - $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT)); - $this->assertFalse($mode->has(Mode::DBAVAILABLE)); + self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT)); + self::assertFalse($mode->has(Mode::DBAVAILABLE)); } public function testWithoutDatabaseSetup() @@ -88,14 +108,14 @@ class ModeTest extends MockedTest $this->databaseMock->shouldReceive('connected')->andReturn(true)->once(); $this->databaseMock->shouldReceive('fetchFirst') - ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once(); + ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once(); $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertFalse($mode->isNormal()); - $this->assertTrue($mode->isInstall()); + self::assertFalse($mode->isNormal()); + self::assertTrue($mode->isInstall()); - $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT)); + self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT)); } public function testWithMaintenanceMode() @@ -104,17 +124,17 @@ class ModeTest extends MockedTest $this->databaseMock->shouldReceive('connected')->andReturn(true)->once(); $this->databaseMock->shouldReceive('fetchFirst') - ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once(); + ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once(); $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance') - ->andReturn(true)->once(); + ->andReturn(true)->once(); $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertFalse($mode->isNormal()); - $this->assertFalse($mode->isInstall()); + self::assertFalse($mode->isNormal()); + self::assertFalse($mode->isInstall()); - $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); - $this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED)); + self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); + self::assertFalse($mode->has(Mode::MAINTENANCEDISABLED)); } public function testNormalMode() @@ -123,20 +143,20 @@ class ModeTest extends MockedTest $this->databaseMock->shouldReceive('connected')->andReturn(true)->once(); $this->databaseMock->shouldReceive('fetchFirst') - ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once(); + ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once(); $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance') - ->andReturn(false)->once(); + ->andReturn(false)->once(); $this->databaseMock->shouldReceive('selectFirst') - ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance']) - ->andReturn(['v' => null])->once(); + ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance']) + ->andReturn(['v' => null])->once(); $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertTrue($mode->isNormal()); - $this->assertFalse($mode->isInstall()); + self::assertTrue($mode->isNormal()); + self::assertFalse($mode->isInstall()); - $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); - $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED)); + self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); + self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED)); } /** @@ -148,20 +168,20 @@ class ModeTest extends MockedTest $this->databaseMock->shouldReceive('connected')->andReturn(true)->once(); $this->databaseMock->shouldReceive('fetchFirst') - ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once(); + ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once(); $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance') - ->andReturn(false)->once(); + ->andReturn(false)->once(); $this->databaseMock->shouldReceive('selectFirst') - ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance']) - ->andReturn(['v' => '0'])->once(); + ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance']) + ->andReturn(['v' => '0'])->once(); $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertTrue($mode->isNormal()); - $this->assertFalse($mode->isInstall()); + self::assertTrue($mode->isNormal()); + self::assertFalse($mode->isInstall()); - $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); - $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED)); + self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE)); + self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED)); } /** @@ -175,6 +195,115 @@ class ModeTest extends MockedTest $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock); - $this->assertNotSame($modeNew, $mode); + self::assertNotSame($modeNew, $mode); + } + + /** + * Test if not called by index is backend + */ + public function testIsBackendNotIsBackend() + { + $server = []; + $module = new Module(); + $mobileDetect = new MobileDetect(); + + $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect); + + self::assertTrue($mode->isBackend()); + } + + /** + * 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(); + + $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect); + + self::assertTrue($mode->isBackend()); + } + + /** + * 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(); + + $mode = (new Mode())->determineRunMode(false, $module, $server, $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', + ]; + + $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false); + $mobileDetect = new MobileDetect(); + + $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(); + + $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); + + $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); + + $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect); + + self::assertFalse($mode->isMobile()); + self::assertFalse($mode->isTablet()); } }