]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModeTest.php
b2a19726ded638c60847fe3147f7c6140140f98d
[friendica.git] / tests / src / App / ModeTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\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;
33 use Mockery;
34 use Mockery\MockInterface;
35
36 class ModeTest extends MockedTest
37 {
38         use VFSTrait;
39         use DBAMockTrait;
40
41         /**
42          * @var BasePath|MockInterface
43          */
44         private $basePathMock;
45
46         /**
47          * @var Database|MockInterface
48          */
49         private $databaseMock;
50
51         /**
52          * @var Cache|MockInterface
53          */
54         private $configCacheMock;
55
56         protected function setUp()
57         {
58                 parent::setUp();
59
60                 $this->setUpVfsDir();
61
62                 $this->basePathMock = Mockery::mock(BasePath::class);
63                 $this->databaseMock = Mockery::mock(Database::class);
64                 $this->configCacheMock = Mockery::mock(Cache::class);
65         }
66
67         public function testItEmpty()
68         {
69                 $mode = new Mode();
70                 self::assertTrue($mode->isInstall());
71                 self::assertFalse($mode->isNormal());
72         }
73
74         public function testWithoutConfig()
75         {
76                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
77
78                 self::assertTrue($this->root->hasChild('config/local.config.php'));
79
80                 $this->delConfigFile('local.config.php');
81
82                 self::assertFalse($this->root->hasChild('config/local.config.php'));
83
84                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
85
86                 self::assertTrue($mode->isInstall());
87                 self::assertFalse($mode->isNormal());
88
89                 self::assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
90         }
91
92         public function testWithoutDatabase()
93         {
94                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
95
96                 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
97
98                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
99
100                 self::assertFalse($mode->isNormal());
101                 self::assertTrue($mode->isInstall());
102
103                 self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
104                 self::assertFalse($mode->has(Mode::DBAVAILABLE));
105         }
106
107         public function testWithoutDatabaseSetup()
108         {
109                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
110
111                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
112                 $this->databaseMock->shouldReceive('fetchFirst')
113                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
114
115                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
116
117                 self::assertFalse($mode->isNormal());
118                 self::assertTrue($mode->isInstall());
119
120                 self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
121         }
122
123         public function testWithMaintenanceMode()
124         {
125                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
126
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();
132
133                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
134
135                 self::assertFalse($mode->isNormal());
136                 self::assertFalse($mode->isInstall());
137
138                 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
139                 self::assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
140         }
141
142         public function testNormalMode()
143         {
144                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
145
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();
154
155                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
156
157                 self::assertTrue($mode->isNormal());
158                 self::assertFalse($mode->isInstall());
159
160                 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
161                 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
162         }
163
164         /**
165          * Test explicit disabled maintenance (in case you manually disable it)
166          */
167         public function testDisabledMaintenance()
168         {
169                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
170
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();
179
180                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
181
182                 self::assertTrue($mode->isNormal());
183                 self::assertFalse($mode->isInstall());
184
185                 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
186                 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
187         }
188
189         /**
190          * Test that modes are immutable
191          */
192         public function testImmutable()
193         {
194                 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
195
196                 $mode = new Mode();
197
198                 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
199
200                 self::assertNotSame($modeNew, $mode);
201         }
202
203         /**
204          * Test if not called by index is backend
205          */
206         public function testIsBackendNotIsBackend()
207         {
208                 $server = [];
209                 $module = new Module();
210                 $mobileDetect = new MobileDetect();
211
212                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
213
214                 self::assertTrue($mode->isBackend());
215         }
216
217         /**
218          * Test is called by index but module is backend
219          */
220         public function testIsBackendButIndex()
221         {
222                 $server = [];
223                 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], true);
224                 $mobileDetect = new MobileDetect();
225
226                 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
227
228                 self::assertTrue($mode->isBackend());
229         }
230
231         /**
232          * Test is called by index and module is not backend
233          */
234         public function testIsNotBackend()
235         {
236                 $server = [];
237                 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
238                 $mobileDetect = new MobileDetect();
239
240                 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
241
242                 self::assertFalse($mode->isBackend());
243         }
244
245         /**
246          * Test if the call is an ajax call
247          */
248         public function testIsAjax()
249         {
250                 // This is the server environment variable to determine ajax calls
251                 $server = [
252                         'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
253                 ];
254
255                 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
256                 $mobileDetect = new MobileDetect();
257
258                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
259
260                 self::assertTrue($mode->isAjax());
261         }
262
263         /**
264          * Test if the call is not nan ajax call
265          */
266         public function testIsNotAjax()
267         {
268                 $server = [];
269                 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
270                 $mobileDetect = new MobileDetect();
271
272                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
273
274                 self::assertFalse($mode->isAjax());
275         }
276
277         /**
278          * Test if the call is a mobile and is a tablet call
279          */
280         public function testIsMobileIsTablet()
281         {
282                 $server = [];
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);
287
288                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
289
290                 self::assertTrue($mode->isMobile());
291                 self::assertTrue($mode->isTablet());
292         }
293
294
295         /**
296          * Test if the call is not a mobile and is not a tablet call
297          */
298         public function testIsNotMobileIsNotTablet()
299         {
300                 $server = [];
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);
305
306                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
307
308                 self::assertFalse($mode->isMobile());
309                 self::assertFalse($mode->isTablet());
310         }
311 }