]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModeTest.php
Merge pull request #7500 from nupplaphil/task/arguments_module_class
[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\Core\Config;
7 use Friendica\Database\Database;
8 use Friendica\Test\MockedTest;
9 use Friendica\Test\Util\DBAMockTrait;
10 use Friendica\Test\Util\VFSTrait;
11 use Friendica\Util\BasePath;
12 use Mockery\MockInterface;
13
14 class ModeTest extends MockedTest
15 {
16         use VFSTrait;
17         use DBAMockTrait;
18
19         /**
20          * @var BasePath|MockInterface
21          */
22         private $basePathMock;
23
24         /**
25          * @var Database|MockInterface
26          */
27         private $databaseMock;
28
29         /**
30          * @var Config\Cache\ConfigCache|MockInterface
31          */
32         private $configCacheMock;
33
34         public function setUp()
35         {
36                 parent::setUp();
37
38                 $this->setUpVfsDir();
39
40                 $this->basePathMock = \Mockery::mock(BasePath::class);
41                 $this->databaseMock = \Mockery::mock(Database::class);
42                 $this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class);
43         }
44
45         public function testItEmpty()
46         {
47                 $mode = new Mode();
48                 $this->assertTrue($mode->isInstall());
49                 $this->assertFalse($mode->isNormal());
50         }
51
52         public function testWithoutConfig()
53         {
54                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
55
56                 $this->assertTrue($this->root->hasChild('config/local.config.php'));
57
58                 $this->delConfigFile('local.config.php');
59
60                 $this->assertFalse($this->root->hasChild('config/local.config.php'));
61
62                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
63
64                 $this->assertTrue($mode->isInstall());
65                 $this->assertFalse($mode->isNormal());
66
67                 $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
68         }
69
70         public function testWithoutDatabase()
71         {
72                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
73
74                 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
75
76                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
77
78                 $this->assertFalse($mode->isNormal());
79                 $this->assertTrue($mode->isInstall());
80
81                 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
82                 $this->assertFalse($mode->has(Mode::DBAVAILABLE));
83         }
84
85         public function testWithoutDatabaseSetup()
86         {
87                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
88
89                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
90                 $this->databaseMock->shouldReceive('fetchFirst')
91                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
92
93                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
94
95                 $this->assertFalse($mode->isNormal());
96                 $this->assertTrue($mode->isInstall());
97
98                 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
99         }
100
101         public function testWithMaintenanceMode()
102         {
103                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
104
105                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
106                 $this->databaseMock->shouldReceive('fetchFirst')
107                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
108                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
109                                       ->andReturn(true)->once();
110
111                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
112
113                 $this->assertFalse($mode->isNormal());
114                 $this->assertFalse($mode->isInstall());
115
116                 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
117                 $this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
118         }
119
120         public function testNormalMode()
121         {
122                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
123
124                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
125                 $this->databaseMock->shouldReceive('fetchFirst')
126                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
127                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
128                                       ->andReturn(false)->once();
129                 $this->databaseMock->shouldReceive('selectFirst')
130                                    ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
131                                    ->andReturn(['v' => null])->once();
132
133                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
134
135                 $this->assertTrue($mode->isNormal());
136                 $this->assertFalse($mode->isInstall());
137
138                 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
139                 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
140         }
141
142         /**
143          * Test explicit disabled maintenance (in case you manually disable it)
144          */
145         public function testDisabledMaintenance()
146         {
147                 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
148
149                 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
150                 $this->databaseMock->shouldReceive('fetchFirst')
151                                    ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
152                 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
153                                       ->andReturn(false)->once();
154                 $this->databaseMock->shouldReceive('selectFirst')
155                                    ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
156                                    ->andReturn(['v' => '0'])->once();
157
158                 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
159
160                 $this->assertTrue($mode->isNormal());
161                 $this->assertFalse($mode->isInstall());
162
163                 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
164                 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
165         }
166
167         /**
168          * Test that modes are immutable
169          */
170         public function testImmutable()
171         {
172                 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
173
174                 $mode = new Mode();
175
176                 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
177
178                 $this->assertNotSame($modeNew, $mode);
179         }
180 }