]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModeTest.php
56b9cc913f21b14edcb10570c243780dd761aeae
[friendica.git] / tests / src / App / ModeTest.php
1 <?php
2
3 namespace Friendica\Test\src\App;
4
5 use Friendica\App\Mode;
6 use Friendica\App\Module;
7 use Friendica\Core\Config;
8 use Friendica\Database\Database;
9 use Friendica\Test\MockedTest;
10 use Friendica\Test\Util\DBAMockTrait;
11 use Friendica\Test\Util\VFSTrait;
12 use Friendica\Util\BasePath;
13 use Mockery\MockInterface;
14
15 class ModeTest extends MockedTest
16 {
17         use VFSTrait;
18         use DBAMockTrait;
19
20         /**
21          * @var BasePath|MockInterface
22          */
23         private $basePathMock;
24
25         /**
26          * @var Database|MockInterface
27          */
28         private $databaseMock;
29
30         /**
31          * @var Config\Cache\ConfigCache|MockInterface
32          */
33         private $configCacheMock;
34
35         public function setUp()
36         {
37                 parent::setUp();
38
39                 $this->setUpVfsDir();
40
41                 $this->basePathMock = \Mockery::mock(BasePath::class);
42                 $this->databaseMock = \Mockery::mock(Database::class);
43                 $this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class);
44         }
45
46         public function testItEmpty()
47         {
48                 $mode = new Mode();
49                 $this->assertTrue($mode->isInstall());
50                 $this->assertFalse($mode->isNormal());
51         }
52
53         public function testWithoutConfig()
54         {
55                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
56
57                 $this->assertTrue($this->root->hasChild('config/local.config.php'));
58
59                 $this->delConfigFile('local.config.php');
60
61                 $this->assertFalse($this->root->hasChild('config/local.config.php'));
62
63                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
64
65                 $this->assertTrue($mode->isInstall());
66                 $this->assertFalse($mode->isNormal());
67
68                 $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
69         }
70
71         public function testWithoutDatabase()
72         {
73                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
74
75                 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
76
77                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
78
79                 $this->assertFalse($mode->isNormal());
80                 $this->assertTrue($mode->isInstall());
81
82                 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
83                 $this->assertFalse($mode->has(Mode::DBAVAILABLE));
84         }
85
86         public function testWithoutDatabaseSetup()
87         {
88                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
89
90                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
91                 $this->databaseMock->shouldReceive('fetchFirst')
92                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
93
94                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
95
96                 $this->assertFalse($mode->isNormal());
97                 $this->assertTrue($mode->isInstall());
98
99                 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
100         }
101
102         public function testWithMaintenanceMode()
103         {
104                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
105
106                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
107                 $this->databaseMock->shouldReceive('fetchFirst')
108                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
109                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
110                                       ->andReturn(true)->once();
111
112                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
113
114                 $this->assertFalse($mode->isNormal());
115                 $this->assertFalse($mode->isInstall());
116
117                 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
118                 $this->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->databaseMock->shouldReceive('fetchFirst')
127                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
128                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
129                                       ->andReturn(false)->once();
130                 $this->databaseMock->shouldReceive('selectFirst')
131                                    ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
132                                    ->andReturn(['v' => null])->once();
133
134                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
135
136                 $this->assertTrue($mode->isNormal());
137                 $this->assertFalse($mode->isInstall());
138
139                 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
140                 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
141         }
142
143         /**
144          * Test explicit disabled maintenance (in case you manually disable it)
145          */
146         public function testDisabledMaintenance()
147         {
148                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
149
150                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
151                 $this->databaseMock->shouldReceive('fetchFirst')
152                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
153                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
154                                       ->andReturn(false)->once();
155                 $this->databaseMock->shouldReceive('selectFirst')
156                                    ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
157                                    ->andReturn(['v' => '0'])->once();
158
159                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
160
161                 $this->assertTrue($mode->isNormal());
162                 $this->assertFalse($mode->isInstall());
163
164                 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
165                 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
166         }
167
168         /**
169          * Test that modes are immutable
170          */
171         public function testImmutable()
172         {
173                 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
174
175                 $mode = new Mode();
176
177                 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
178
179                 $this->assertNotSame($modeNew, $mode);
180         }
181
182         /**
183          * Test if not called by index is backend
184          */
185         public function testIsBackendNotIndex()
186         {
187                 $server = ['PHP_SELF' => '/daemon.php'];
188                 $module = new Module();
189
190                 $mode = (new Mode())->determineBackend($module, $server);
191
192                 $this->assertTrue($mode->isBackend());
193         }
194
195         /**
196          * Test is called by index but module is backend
197          */
198         public function testIsBackendButIndex()
199         {
200                 $server = ['PHP_SELF' => '/index.php'];
201                 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true);
202
203                 $mode = (new Mode())->determineBackend($module, $server);
204
205                 $this->assertTrue($mode->isBackend());
206         }
207
208         /**
209          * Test is called by index and module is not backend
210          */
211         public function testIsNotBackend()
212         {
213                 $server = ['PHP_SELF' => '/index.php'];
214                 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
215
216                 $mode = (new Mode())->determineBackend($module, $server);
217
218                 $this->assertFalse($mode->isBackend());
219         }
220 }