3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\App;
24 use Detection\MobileDetect;
25 use Friendica\App\Mode;
26 use Friendica\App\ModuleController;
27 use Friendica\Core\Config\ValueObject\Cache;
28 use Friendica\Database\Database;
29 use Friendica\Test\MockedTest;
30 use Friendica\Test\Util\VFSTrait;
31 use Friendica\Util\BasePath;
33 use Mockery\MockInterface;
35 class ModeTest extends MockedTest
40 * @var BasePath|MockInterface
42 private $basePathMock;
45 * @var Database|MockInterface
47 private $databaseMock;
50 * @var Cache|MockInterface
52 private $configCacheMock;
54 protected function setUp(): void
60 $this->basePathMock = Mockery::mock(BasePath::class);
61 $this->databaseMock = Mockery::mock(Database::class);
62 $this->configCacheMock = Mockery::mock(Cache::class);
65 public function testItEmpty()
68 self::assertTrue($mode->isInstall());
69 self::assertFalse($mode->isNormal());
72 public function testWithoutConfig()
74 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
76 self::assertTrue($this->root->hasChild('config/local.config.php'));
78 $this->delConfigFile('local.config.php');
80 self::assertFalse($this->root->hasChild('config/local.config.php'));
82 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
84 self::assertTrue($mode->isInstall());
85 self::assertFalse($mode->isNormal());
87 self::assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
90 public function testWithoutDatabase()
92 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
94 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
96 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
98 self::assertFalse($mode->isNormal());
99 self::assertTrue($mode->isInstall());
101 self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
102 self::assertFalse($mode->has(Mode::DBAVAILABLE));
105 public function testWithoutDatabaseSetup()
107 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
109 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
110 $this->databaseMock->shouldReceive('fetchFirst')
111 ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
113 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
115 self::assertFalse($mode->isNormal());
116 self::assertTrue($mode->isInstall());
118 self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
121 public function testWithMaintenanceMode()
123 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
125 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
126 $this->databaseMock->shouldReceive('fetchFirst')
127 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
128 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
129 ->andReturn(true)->once();
131 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
133 self::assertFalse($mode->isNormal());
134 self::assertFalse($mode->isInstall());
136 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
137 self::assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
140 public function testNormalMode()
142 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
144 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
145 $this->databaseMock->shouldReceive('fetchFirst')
146 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
147 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
148 ->andReturn(false)->once();
149 $this->databaseMock->shouldReceive('selectFirst')
150 ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
151 ->andReturn(['v' => null])->once();
153 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
155 self::assertTrue($mode->isNormal());
156 self::assertFalse($mode->isInstall());
158 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
159 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
163 * Test explicit disabled maintenance (in case you manually disable it)
165 public function testDisabledMaintenance()
167 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
169 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
170 $this->databaseMock->shouldReceive('fetchFirst')
171 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
172 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
173 ->andReturn(false)->once();
174 $this->databaseMock->shouldReceive('selectFirst')
175 ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
176 ->andReturn(['v' => '0'])->once();
178 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
180 self::assertTrue($mode->isNormal());
181 self::assertFalse($mode->isInstall());
183 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
184 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
188 * Test that modes are immutable
190 public function testImmutable()
192 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
196 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
198 self::assertNotSame($modeNew, $mode);
202 * Test if not called by index is backend
204 public function testIsBackendNotIsBackend()
207 $module = new ModuleController();
208 $mobileDetect = new MobileDetect();
210 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
212 self::assertTrue($mode->isBackend());
216 * Test is called by index but module is backend
218 public function testIsBackendButIndex()
221 $module = new ModuleController(ModuleController::DEFAULT, null, true);
222 $mobileDetect = new MobileDetect();
224 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
226 self::assertTrue($mode->isBackend());
230 * Test is called by index and module is not backend
232 public function testIsNotBackend()
235 $module = new ModuleController(ModuleController::DEFAULT, null, false);
236 $mobileDetect = new MobileDetect();
238 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
240 self::assertFalse($mode->isBackend());
244 * Test if the call is an ajax call
246 public function testIsAjax()
248 // This is the server environment variable to determine ajax calls
250 'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
253 $module = new ModuleController(ModuleController::DEFAULT, null, false);
254 $mobileDetect = new MobileDetect();
256 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
258 self::assertTrue($mode->isAjax());
262 * Test if the call is not nan ajax call
264 public function testIsNotAjax()
267 $module = new ModuleController(ModuleController::DEFAULT, null, false);
268 $mobileDetect = new MobileDetect();
270 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
272 self::assertFalse($mode->isAjax());
276 * Test if the call is a mobile and is a tablet call
278 public function testIsMobileIsTablet()
281 $module = new ModuleController(ModuleController::DEFAULT, null, false);
282 $mobileDetect = Mockery::mock(MobileDetect::class);
283 $mobileDetect->shouldReceive('isMobile')->andReturn(true);
284 $mobileDetect->shouldReceive('isTablet')->andReturn(true);
286 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
288 self::assertTrue($mode->isMobile());
289 self::assertTrue($mode->isTablet());
294 * Test if the call is not a mobile and is not a tablet call
296 public function testIsNotMobileIsNotTablet()
299 $module = new ModuleController(ModuleController::DEFAULT, null, false);
300 $mobileDetect = Mockery::mock(MobileDetect::class);
301 $mobileDetect->shouldReceive('isMobile')->andReturn(false);
302 $mobileDetect->shouldReceive('isTablet')->andReturn(false);
304 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
306 self::assertFalse($mode->isMobile());
307 self::assertFalse($mode->isTablet());