Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
8.6KB

  1. <?php
  2. namespace Friendica\Test\src\App;
  3. use Detection\MobileDetect;
  4. use Friendica\App\Mode;
  5. use Friendica\App\Module;
  6. use Friendica\Core\Config;
  7. use Friendica\Database\Database;
  8. use Friendica\Test\MockedTest;
  9. use Friendica\Test\Util\DBAMockTrait;
  10. use Friendica\Test\Util\VFSTrait;
  11. use Friendica\Util\BasePath;
  12. use Mockery\MockInterface;
  13. class ModeTest extends MockedTest
  14. {
  15. use VFSTrait;
  16. use DBAMockTrait;
  17. /**
  18. * @var BasePath|MockInterface
  19. */
  20. private $basePathMock;
  21. /**
  22. * @var Database|MockInterface
  23. */
  24. private $databaseMock;
  25. /**
  26. * @var Config\Cache\ConfigCache|MockInterface
  27. */
  28. private $configCacheMock;
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->setUpVfsDir();
  33. $this->basePathMock = \Mockery::mock(BasePath::class);
  34. $this->databaseMock = \Mockery::mock(Database::class);
  35. $this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class);
  36. }
  37. public function testItEmpty()
  38. {
  39. $mode = new Mode();
  40. $this->assertTrue($mode->isInstall());
  41. $this->assertFalse($mode->isNormal());
  42. }
  43. public function testWithoutConfig()
  44. {
  45. $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
  46. $this->assertTrue($this->root->hasChild('config/local.config.php'));
  47. $this->delConfigFile('local.config.php');
  48. $this->assertFalse($this->root->hasChild('config/local.config.php'));
  49. $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
  50. $this->assertTrue($mode->isInstall());
  51. $this->assertFalse($mode->isNormal());
  52. $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
  53. }
  54. public function testWithoutDatabase()
  55. {
  56. $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
  57. $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
  58. $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
  59. $this->assertFalse($mode->isNormal());
  60. $this->assertTrue($mode->isInstall());
  61. $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
  62. $this->assertFalse($mode->has(Mode::DBAVAILABLE));
  63. }
  64. public function testWithoutDatabaseSetup()
  65. {
  66. $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
  67. $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
  68. $this->databaseMock->shouldReceive('fetchFirst')
  69. ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
  70. $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
  71. $this->assertFalse($mode->isNormal());
  72. $this->assertTrue($mode->isInstall());
  73. $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
  74. }
  75. public function testWithMaintenanceMode()
  76. {
  77. $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
  78. $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
  79. $this->databaseMock->shouldReceive('fetchFirst')
  80. ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
  81. $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
  82. ->andReturn(true)->once();
  83. $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
  84. $this->assertFalse($mode->isNormal());
  85. $this->assertFalse($mode->isInstall());
  86. $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
  87. $this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
  88. }
  89. public function testNormalMode()
  90. {
  91. $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
  92. $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
  93. $this->databaseMock->shouldReceive('fetchFirst')
  94. ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
  95. $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
  96. ->andReturn(false)->once();
  97. $this->databaseMock->shouldReceive('selectFirst')
  98. ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
  99. ->andReturn(['v' => null])->once();
  100. $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
  101. $this->assertTrue($mode->isNormal());
  102. $this->assertFalse($mode->isInstall());
  103. $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
  104. $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
  105. }
  106. /**
  107. * Test explicit disabled maintenance (in case you manually disable it)
  108. */
  109. public function testDisabledMaintenance()
  110. {
  111. $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
  112. $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
  113. $this->databaseMock->shouldReceive('fetchFirst')
  114. ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
  115. $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
  116. ->andReturn(false)->once();
  117. $this->databaseMock->shouldReceive('selectFirst')
  118. ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
  119. ->andReturn(['v' => '0'])->once();
  120. $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
  121. $this->assertTrue($mode->isNormal());
  122. $this->assertFalse($mode->isInstall());
  123. $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
  124. $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
  125. }
  126. /**
  127. * Test that modes are immutable
  128. */
  129. public function testImmutable()
  130. {
  131. $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
  132. $mode = new Mode();
  133. $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
  134. $this->assertNotSame($modeNew, $mode);
  135. }
  136. /**
  137. * Test if not called by index is backend
  138. */
  139. public function testIsBackendNotIsBackend()
  140. {
  141. $server = [];
  142. $module = new Module();
  143. $mobileDetect = new MobileDetect();
  144. $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
  145. $this->assertTrue($mode->isBackend());
  146. }
  147. /**
  148. * Test is called by index but module is backend
  149. */
  150. public function testIsBackendButIndex()
  151. {
  152. $server = [];
  153. $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true);
  154. $mobileDetect = new MobileDetect();
  155. $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
  156. $this->assertTrue($mode->isBackend());
  157. }
  158. /**
  159. * Test is called by index and module is not backend
  160. */
  161. public function testIsNotBackend()
  162. {
  163. $server = [];
  164. $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
  165. $mobileDetect = new MobileDetect();
  166. $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
  167. $this->assertFalse($mode->isBackend());
  168. }
  169. /**
  170. * Test if the call is an ajax call
  171. */
  172. public function testIsAjax()
  173. {
  174. // This is the server environment variable to determine ajax calls
  175. $server = [
  176. 'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
  177. ];
  178. $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
  179. $mobileDetect = new MobileDetect();
  180. $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
  181. $this->assertTrue($mode->isAjax());
  182. }
  183. /**
  184. * Test if the call is not nan ajax call
  185. */
  186. public function testIsNotAjax()
  187. {
  188. $server = [];
  189. $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
  190. $mobileDetect = new MobileDetect();
  191. $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
  192. $this->assertFalse($mode->isAjax());
  193. }
  194. /**
  195. * Test if the call is a mobile and is a tablet call
  196. */
  197. public function testIsMobileIsTablet()
  198. {
  199. $server = [];
  200. $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
  201. $mobileDetect = \Mockery::mock(MobileDetect::class);
  202. $mobileDetect->shouldReceive('isMobile')->andReturn(true);
  203. $mobileDetect->shouldReceive('isTablet')->andReturn(true);
  204. $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
  205. $this->assertTrue($mode->isMobile());
  206. $this->assertTrue($mode->isTablet());
  207. }
  208. /**
  209. * Test if the call is not a mobile and is not a tablet call
  210. */
  211. public function testIsNotMobileIsNotTablet()
  212. {
  213. $server = [];
  214. $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
  215. $mobileDetect = \Mockery::mock(MobileDetect::class);
  216. $mobileDetect->shouldReceive('isMobile')->andReturn(false);
  217. $mobileDetect->shouldReceive('isTablet')->andReturn(false);
  218. $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
  219. $this->assertFalse($mode->isMobile());
  220. $this->assertFalse($mode->isTablet());
  221. }
  222. }