3 * @copyright Copyright (C) 2020, Friendica
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;
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;
33 use Mockery\MockInterface;
35 class ModeTest extends MockedTest
41 * @var BasePath|MockInterface
43 private $basePathMock;
46 * @var Database|MockInterface
48 private $databaseMock;
51 * @var \Friendica\Core\Config\Cache|MockInterface
53 private $configCacheMock;
55 public function setUp()
61 $this->basePathMock = \Mockery::mock(BasePath::class);
62 $this->databaseMock = \Mockery::mock(Database::class);
63 $this->configCacheMock = \Mockery::mock(Config\Cache::class);
66 public function testItEmpty()
69 $this->assertTrue($mode->isInstall());
70 $this->assertFalse($mode->isNormal());
73 public function testWithoutConfig()
75 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
77 $this->assertTrue($this->root->hasChild('config/local.config.php'));
79 $this->delConfigFile('local.config.php');
81 $this->assertFalse($this->root->hasChild('config/local.config.php'));
83 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
85 $this->assertTrue($mode->isInstall());
86 $this->assertFalse($mode->isNormal());
88 $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
91 public function testWithoutDatabase()
93 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
95 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
97 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
99 $this->assertFalse($mode->isNormal());
100 $this->assertTrue($mode->isInstall());
102 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
103 $this->assertFalse($mode->has(Mode::DBAVAILABLE));
106 public function testWithoutDatabaseSetup()
108 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
110 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
111 $this->databaseMock->shouldReceive('fetchFirst')
112 ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
114 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
116 $this->assertFalse($mode->isNormal());
117 $this->assertTrue($mode->isInstall());
119 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
122 public function testWithMaintenanceMode()
124 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
126 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
127 $this->databaseMock->shouldReceive('fetchFirst')
128 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
129 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
130 ->andReturn(true)->once();
132 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
134 $this->assertFalse($mode->isNormal());
135 $this->assertFalse($mode->isInstall());
137 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
138 $this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
141 public function testNormalMode()
143 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
145 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
146 $this->databaseMock->shouldReceive('fetchFirst')
147 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
148 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
149 ->andReturn(false)->once();
150 $this->databaseMock->shouldReceive('selectFirst')
151 ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
152 ->andReturn(['v' => null])->once();
154 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
156 $this->assertTrue($mode->isNormal());
157 $this->assertFalse($mode->isInstall());
159 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
160 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
164 * Test explicit disabled maintenance (in case you manually disable it)
166 public function testDisabledMaintenance()
168 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
170 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
171 $this->databaseMock->shouldReceive('fetchFirst')
172 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
173 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
174 ->andReturn(false)->once();
175 $this->databaseMock->shouldReceive('selectFirst')
176 ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
177 ->andReturn(['v' => '0'])->once();
179 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
181 $this->assertTrue($mode->isNormal());
182 $this->assertFalse($mode->isInstall());
184 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
185 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
189 * Test that modes are immutable
191 public function testImmutable()
193 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
197 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
199 $this->assertNotSame($modeNew, $mode);
203 * Test if not called by index is backend
205 public function testIsBackendNotIsBackend()
208 $module = new Module();
209 $mobileDetect = new MobileDetect();
211 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
213 $this->assertTrue($mode->isBackend());
217 * Test is called by index but module is backend
219 public function testIsBackendButIndex()
222 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], true);
223 $mobileDetect = new MobileDetect();
225 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
227 $this->assertTrue($mode->isBackend());
231 * Test is called by index and module is not backend
233 public function testIsNotBackend()
236 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
237 $mobileDetect = new MobileDetect();
239 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
241 $this->assertFalse($mode->isBackend());
245 * Test if the call is an ajax call
247 public function testIsAjax()
249 // This is the server environment variable to determine ajax calls
251 'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
254 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
255 $mobileDetect = new MobileDetect();
257 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
259 $this->assertTrue($mode->isAjax());
263 * Test if the call is not nan ajax call
265 public function testIsNotAjax()
268 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
269 $mobileDetect = new MobileDetect();
271 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
273 $this->assertFalse($mode->isAjax());
277 * Test if the call is a mobile and is a tablet call
279 public function testIsMobileIsTablet()
282 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
283 $mobileDetect = \Mockery::mock(MobileDetect::class);
284 $mobileDetect->shouldReceive('isMobile')->andReturn(true);
285 $mobileDetect->shouldReceive('isTablet')->andReturn(true);
287 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
289 $this->assertTrue($mode->isMobile());
290 $this->assertTrue($mode->isTablet());
295 * Test if the call is not a mobile and is not a tablet call
297 public function testIsNotMobileIsNotTablet()
300 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
301 $mobileDetect = \Mockery::mock(MobileDetect::class);
302 $mobileDetect->shouldReceive('isMobile')->andReturn(false);
303 $mobileDetect->shouldReceive('isTablet')->andReturn(false);
305 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
307 $this->assertFalse($mode->isMobile());
308 $this->assertFalse($mode->isTablet());