3 * @copyright Copyright (C) 2010-2023, 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\Arguments;
26 use Friendica\App\Mode;
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 testWithMaintenanceMode()
107 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
109 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
110 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
111 ->andReturn(true)->once();
113 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
115 self::assertFalse($mode->isNormal());
116 self::assertFalse($mode->isInstall());
118 self::assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
121 public function testNormalMode()
123 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
125 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
126 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
127 ->andReturn(false)->once();
129 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
131 self::assertTrue($mode->isNormal());
132 self::assertFalse($mode->isInstall());
134 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
138 * Test explicit disabled maintenance (in case you manually disable it)
140 public function testDisabledMaintenance()
142 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
144 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
145 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
146 ->andReturn(false)->once();
148 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
150 self::assertTrue($mode->isNormal());
151 self::assertFalse($mode->isInstall());
153 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
157 * Test that modes are immutable
159 public function testImmutable()
161 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
165 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
167 self::assertNotSame($modeNew, $mode);
171 * Test if not called by index is backend
173 public function testIsBackendNotIsBackend()
176 $args = new Arguments();
177 $mobileDetect = new MobileDetect();
179 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
181 self::assertTrue($mode->isBackend());
185 * Test is called by index but module is backend
187 public function testIsBackendButIndex()
190 $args = new Arguments('', '', Mode::BACKEND_MODULES[0]);
191 $mobileDetect = new MobileDetect();
193 $mode = (new Mode())->determineRunMode(false, $server, $args, $mobileDetect);
195 self::assertTrue($mode->isBackend());
199 * Test is called by index and module is not backend
201 public function testIsNotBackend()
204 $args = new Arguments('', '', Arguments::DEFAULT_MODULE);
205 $mobileDetect = new MobileDetect();
207 $mode = (new Mode())->determineRunMode(false, $server, $args, $mobileDetect);
209 self::assertFalse($mode->isBackend());
213 * Test if the call is an ajax call
215 public function testIsAjax()
217 // This is the server environment variable to determine ajax calls
219 'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
222 $args = new Arguments('', '', Arguments::DEFAULT_MODULE);
223 $mobileDetect = new MobileDetect();
225 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
227 self::assertTrue($mode->isAjax());
231 * Test if the call is not nan ajax call
233 public function testIsNotAjax()
236 $args = new Arguments('', '', Arguments::DEFAULT_MODULE);
237 $mobileDetect = new MobileDetect();
239 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
241 self::assertFalse($mode->isAjax());
245 * Test if the call is a mobile and is a tablet call
247 public function testIsMobileIsTablet()
250 $args = new Arguments('', '', Arguments::DEFAULT_MODULE);
251 $mobileDetect = Mockery::mock(MobileDetect::class);
252 $mobileDetect->shouldReceive('isMobile')->andReturn(true);
253 $mobileDetect->shouldReceive('isTablet')->andReturn(true);
255 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
257 self::assertTrue($mode->isMobile());
258 self::assertTrue($mode->isTablet());
263 * Test if the call is not a mobile and is not a tablet call
265 public function testIsNotMobileIsNotTablet()
268 $args = new Arguments('', '', Arguments::DEFAULT_MODULE);
269 $mobileDetect = Mockery::mock(MobileDetect::class);
270 $mobileDetect->shouldReceive('isMobile')->andReturn(false);
271 $mobileDetect->shouldReceive('isTablet')->andReturn(false);
273 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
275 self::assertFalse($mode->isMobile());
276 self::assertFalse($mode->isTablet());