]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModeTest.php
Merge remote-tracking branch 'upstream/develop' into mod-item
[friendica.git] / tests / src / App / ModeTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\App;
23
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;
32 use Mockery;
33 use Mockery\MockInterface;
34
35 class ModeTest extends MockedTest
36 {
37         use VFSTrait;
38
39         /**
40          * @var BasePath|MockInterface
41          */
42         private $basePathMock;
43
44         /**
45          * @var Database|MockInterface
46          */
47         private $databaseMock;
48
49         /**
50          * @var Cache|MockInterface
51          */
52         private $configCacheMock;
53
54         protected function setUp(): void
55         {
56                 parent::setUp();
57
58                 $this->setUpVfsDir();
59
60                 $this->basePathMock    = Mockery::mock(BasePath::class);
61                 $this->databaseMock    = Mockery::mock(Database::class);
62                 $this->configCacheMock = Mockery::mock(Cache::class);
63         }
64
65         public function testItEmpty()
66         {
67                 $mode = new Mode();
68                 self::assertTrue($mode->isInstall());
69                 self::assertFalse($mode->isNormal());
70         }
71
72         public function testWithoutConfig()
73         {
74                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
75
76                 self::assertTrue($this->root->hasChild('config/local.config.php'));
77
78                 $this->delConfigFile('local.config.php');
79
80                 self::assertFalse($this->root->hasChild('config/local.config.php'));
81
82                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
83
84                 self::assertTrue($mode->isInstall());
85                 self::assertFalse($mode->isNormal());
86
87                 self::assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
88         }
89
90         public function testWithoutDatabase()
91         {
92                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
93
94                 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
95
96                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
97
98                 self::assertFalse($mode->isNormal());
99                 self::assertTrue($mode->isInstall());
100
101                 self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
102                 self::assertFalse($mode->has(Mode::DBAVAILABLE));
103         }
104
105         public function testWithMaintenanceMode()
106         {
107                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
108
109                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
110                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
111                                                           ->andReturn(true)->once();
112
113                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
114
115                 self::assertFalse($mode->isNormal());
116                 self::assertFalse($mode->isInstall());
117
118                 self::assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
119         }
120
121         public function testNormalMode()
122         {
123                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
124
125                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
126                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
127                                                           ->andReturn(false)->once();
128
129                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
130
131                 self::assertTrue($mode->isNormal());
132                 self::assertFalse($mode->isInstall());
133
134                 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
135         }
136
137         /**
138          * Test explicit disabled maintenance (in case you manually disable it)
139          */
140         public function testDisabledMaintenance()
141         {
142                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
143
144                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
145                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
146                                                           ->andReturn(false)->once();
147
148                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
149
150                 self::assertTrue($mode->isNormal());
151                 self::assertFalse($mode->isInstall());
152
153                 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
154         }
155
156         /**
157          * Test that modes are immutable
158          */
159         public function testImmutable()
160         {
161                 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
162
163                 $mode = new Mode();
164
165                 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
166
167                 self::assertNotSame($modeNew, $mode);
168         }
169
170         /**
171          * Test if not called by index is backend
172          */
173         public function testIsBackendNotIsBackend()
174         {
175                 $server       = [];
176                 $args         = new Arguments();
177                 $mobileDetect = new MobileDetect();
178
179                 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
180
181                 self::assertTrue($mode->isBackend());
182         }
183
184         /**
185          * Test is called by index but module is backend
186          */
187         public function testIsBackendButIndex()
188         {
189                 $server       = [];
190                 $args         = new Arguments('', '', Mode::BACKEND_MODULES[0]);
191                 $mobileDetect = new MobileDetect();
192
193                 $mode = (new Mode())->determineRunMode(false, $server, $args, $mobileDetect);
194
195                 self::assertTrue($mode->isBackend());
196         }
197
198         /**
199          * Test is called by index and module is not backend
200          */
201         public function testIsNotBackend()
202         {
203                 $server       = [];
204                 $args         = new Arguments('', '', Arguments::DEFAULT_MODULE);
205                 $mobileDetect = new MobileDetect();
206
207                 $mode = (new Mode())->determineRunMode(false, $server, $args, $mobileDetect);
208
209                 self::assertFalse($mode->isBackend());
210         }
211
212         /**
213          * Test if the call is an ajax call
214          */
215         public function testIsAjax()
216         {
217                 // This is the server environment variable to determine ajax calls
218                 $server = [
219                         'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
220                 ];
221
222                 $args         = new Arguments('', '', Arguments::DEFAULT_MODULE);
223                 $mobileDetect = new MobileDetect();
224
225                 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
226
227                 self::assertTrue($mode->isAjax());
228         }
229
230         /**
231          * Test if the call is not nan ajax call
232          */
233         public function testIsNotAjax()
234         {
235                 $server       = [];
236                 $args         = new Arguments('', '', Arguments::DEFAULT_MODULE);
237                 $mobileDetect = new MobileDetect();
238
239                 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
240
241                 self::assertFalse($mode->isAjax());
242         }
243
244         /**
245          * Test if the call is a mobile and is a tablet call
246          */
247         public function testIsMobileIsTablet()
248         {
249                 $server       = [];
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);
254
255                 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
256
257                 self::assertTrue($mode->isMobile());
258                 self::assertTrue($mode->isTablet());
259         }
260
261
262         /**
263          * Test if the call is not a mobile and is not a tablet call
264          */
265         public function testIsNotMobileIsNotTablet()
266         {
267                 $server       = [];
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);
272
273                 $mode = (new Mode())->determineRunMode(true, $server, $args, $mobileDetect);
274
275                 self::assertFalse($mode->isMobile());
276                 self::assertFalse($mode->isTablet());
277         }
278 }