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