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