]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModeTest.php
Restructure (P)Config to follow new paradigm
[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\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 testWithoutDatabaseSetup()
106         {
107                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
108
109                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
110                 $this->databaseMock->shouldReceive('fetchFirst')
111                                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
112
113                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
114
115                 self::assertFalse($mode->isNormal());
116                 self::assertTrue($mode->isInstall());
117
118                 self::assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
119         }
120
121         public function testWithMaintenanceMode()
122         {
123                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
124
125                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
126                 $this->databaseMock->shouldReceive('fetchFirst')
127                                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
128                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
129                                                           ->andReturn(true)->once();
130
131                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
132
133                 self::assertFalse($mode->isNormal());
134                 self::assertFalse($mode->isInstall());
135
136                 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
137                 self::assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
138         }
139
140         public function testNormalMode()
141         {
142                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
143
144                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
145                 $this->databaseMock->shouldReceive('fetchFirst')
146                                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
147                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
148                                                           ->andReturn(false)->once();
149                 $this->databaseMock->shouldReceive('selectFirst')
150                                                    ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
151                                                    ->andReturn(['v' => null])->once();
152
153                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
154
155                 self::assertTrue($mode->isNormal());
156                 self::assertFalse($mode->isInstall());
157
158                 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
159                 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
160         }
161
162         /**
163          * Test explicit disabled maintenance (in case you manually disable it)
164          */
165         public function testDisabledMaintenance()
166         {
167                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
168
169                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
170                 $this->databaseMock->shouldReceive('fetchFirst')
171                                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
172                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
173                                                           ->andReturn(false)->once();
174                 $this->databaseMock->shouldReceive('selectFirst')
175                                                    ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
176                                                    ->andReturn(['v' => '0'])->once();
177
178                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
179
180                 self::assertTrue($mode->isNormal());
181                 self::assertFalse($mode->isInstall());
182
183                 self::assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
184                 self::assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
185         }
186
187         /**
188          * Test that modes are immutable
189          */
190         public function testImmutable()
191         {
192                 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
193
194                 $mode = new Mode();
195
196                 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
197
198                 self::assertNotSame($modeNew, $mode);
199         }
200
201         /**
202          * Test if not called by index is backend
203          */
204         public function testIsBackendNotIsBackend()
205         {
206                 $server       = [];
207                 $module       = new Module();
208                 $mobileDetect = new MobileDetect();
209
210                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
211
212                 self::assertTrue($mode->isBackend());
213         }
214
215         /**
216          * Test is called by index but module is backend
217          */
218         public function testIsBackendButIndex()
219         {
220                 $server       = [];
221                 $module       = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], true);
222                 $mobileDetect = new MobileDetect();
223
224                 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
225
226                 self::assertTrue($mode->isBackend());
227         }
228
229         /**
230          * Test is called by index and module is not backend
231          */
232         public function testIsNotBackend()
233         {
234                 $server       = [];
235                 $module       = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
236                 $mobileDetect = new MobileDetect();
237
238                 $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
239
240                 self::assertFalse($mode->isBackend());
241         }
242
243         /**
244          * Test if the call is an ajax call
245          */
246         public function testIsAjax()
247         {
248                 // This is the server environment variable to determine ajax calls
249                 $server = [
250                         'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
251                 ];
252
253                 $module       = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
254                 $mobileDetect = new MobileDetect();
255
256                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
257
258                 self::assertTrue($mode->isAjax());
259         }
260
261         /**
262          * Test if the call is not nan ajax call
263          */
264         public function testIsNotAjax()
265         {
266                 $server       = [];
267                 $module       = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
268                 $mobileDetect = new MobileDetect();
269
270                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
271
272                 self::assertFalse($mode->isAjax());
273         }
274
275         /**
276          * Test if the call is a mobile and is a tablet call
277          */
278         public function testIsMobileIsTablet()
279         {
280                 $server       = [];
281                 $module       = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
282                 $mobileDetect = Mockery::mock(MobileDetect::class);
283                 $mobileDetect->shouldReceive('isMobile')->andReturn(true);
284                 $mobileDetect->shouldReceive('isTablet')->andReturn(true);
285
286                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
287
288                 self::assertTrue($mode->isMobile());
289                 self::assertTrue($mode->isTablet());
290         }
291
292
293         /**
294          * Test if the call is not a mobile and is not a tablet call
295          */
296         public function testIsNotMobileIsNotTablet()
297         {
298                 $server       = [];
299                 $module       = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
300                 $mobileDetect = Mockery::mock(MobileDetect::class);
301                 $mobileDetect->shouldReceive('isMobile')->andReturn(false);
302                 $mobileDetect->shouldReceive('isTablet')->andReturn(false);
303
304                 $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
305
306                 self::assertFalse($mode->isMobile());
307                 self::assertFalse($mode->isTablet());
308         }
309 }