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\Module;
27 use Friendica\Core\Config\Cache;
28 use Friendica\Database\Database;
29 use Friendica\Test\MockedTest;
30 use Friendica\Test\Util\DBAMockTrait;
31 use Friendica\Test\Util\VFSTrait;
32 use Friendica\Util\BasePath;
34 use Mockery\MockInterface;
36 class ModeTest extends MockedTest
42 * @var BasePath|MockInterface
44 private $basePathMock;
47 * @var Database|MockInterface
49 private $databaseMock;
52 * @var Cache|MockInterface
54 private $configCacheMock;
56 protected function setUp() : void
62 $this->basePathMock = Mockery::mock(BasePath::class);
63 $this->databaseMock = Mockery::mock(Database::class);
64 $this->configCacheMock = Mockery::mock(Cache::class);
67 public function testItEmpty()
70 self::assertTrue($mode->isInstall());
71 self::assertFalse($mode->isNormal());
74 public function testWithoutConfig()
76 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
78 self::assertTrue($this->root->hasChild('config/local.config.php'));
80 $this->delConfigFile('local.config.php');
82 self::assertFalse($this->root->hasChild('config/local.config.php'));
84 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
86 self::assertTrue($mode->isInstall());
87 self::assertFalse($mode->isNormal());
89 self::assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
92 public function testWithoutDatabase()
94 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
96 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
98 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
100 self::assertFalse($mode->isNormal());
101 self::assertTrue($mode->isInstall());
103 self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
104 self::assertFalse($mode->has(Mode::DBAVAILABLE));
107 public function testWithoutDatabaseSetup()
109 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
111 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
112 $this->databaseMock->shouldReceive('fetchFirst')
113 ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
115 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
117 self::assertFalse($mode->isNormal());
118 self::assertTrue($mode->isInstall());
120 self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
123 public function testWithMaintenanceMode()
125 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
127 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
128 $this->databaseMock->shouldReceive('fetchFirst')
129 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
130 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
131 ->andReturn(true)->once();
133 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
135 self::assertFalse($mode->isNormal());
136 self::assertFalse($mode->isInstall());
138 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
139 self::assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
142 public function testNormalMode()
144 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
146 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
147 $this->databaseMock->shouldReceive('fetchFirst')
148 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
149 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
150 ->andReturn(false)->once();
151 $this->databaseMock->shouldReceive('selectFirst')
152 ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
153 ->andReturn(['v' => null])->once();
155 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
157 self::assertTrue($mode->isNormal());
158 self::assertFalse($mode->isInstall());
160 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
161 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
165 * Test explicit disabled maintenance (in case you manually disable it)
167 public function testDisabledMaintenance()
169 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
171 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
172 $this->databaseMock->shouldReceive('fetchFirst')
173 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
174 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
175 ->andReturn(false)->once();
176 $this->databaseMock->shouldReceive('selectFirst')
177 ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
178 ->andReturn(['v' => '0'])->once();
180 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
182 self::assertTrue($mode->isNormal());
183 self::assertFalse($mode->isInstall());
185 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
186 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
190 * Test that modes are immutable
192 public function testImmutable()
194 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
198 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
200 self::assertNotSame($modeNew, $mode);
204 * Test if not called by index is backend
206 public function testIsBackendNotIsBackend()
209 $module = new Module();
210 $mobileDetect = new MobileDetect();
212 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
214 self::assertTrue($mode->isBackend());
218 * Test is called by index but module is backend
220 public function testIsBackendButIndex()
223 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], true);
224 $mobileDetect = new MobileDetect();
226 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
228 self::assertTrue($mode->isBackend());
232 * Test is called by index and module is not backend
234 public function testIsNotBackend()
237 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
238 $mobileDetect = new MobileDetect();
240 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
242 self::assertFalse($mode->isBackend());
246 * Test if the call is an ajax call
248 public function testIsAjax()
250 // This is the server environment variable to determine ajax calls
252 'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
255 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
256 $mobileDetect = new MobileDetect();
258 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
260 self::assertTrue($mode->isAjax());
264 * Test if the call is not nan ajax call
266 public function testIsNotAjax()
269 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
270 $mobileDetect = new MobileDetect();
272 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
274 self::assertFalse($mode->isAjax());
278 * Test if the call is a mobile and is a tablet call
280 public function testIsMobileIsTablet()
283 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
284 $mobileDetect = Mockery::mock(MobileDetect::class);
285 $mobileDetect->shouldReceive('isMobile')->andReturn(true);
286 $mobileDetect->shouldReceive('isTablet')->andReturn(true);
288 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
290 self::assertTrue($mode->isMobile());
291 self::assertTrue($mode->isTablet());
296 * Test if the call is not a mobile and is not a tablet call
298 public function testIsNotMobileIsNotTablet()
301 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
302 $mobileDetect = Mockery::mock(MobileDetect::class);
303 $mobileDetect->shouldReceive('isMobile')->andReturn(false);
304 $mobileDetect->shouldReceive('isTablet')->andReturn(false);
306 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
308 self::assertFalse($mode->isMobile());
309 self::assertFalse($mode->isTablet());