3 namespace Friendica\Test\src\App;
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;
15 class ModeTest extends MockedTest
21 * @var BasePath|MockInterface
23 private $basePathMock;
26 * @var Database|MockInterface
28 private $databaseMock;
31 * @var Config\Cache\ConfigCache|MockInterface
33 private $configCacheMock;
35 public function setUp()
41 $this->basePathMock = \Mockery::mock(BasePath::class);
42 $this->databaseMock = \Mockery::mock(Database::class);
43 $this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class);
46 public function testItEmpty()
49 $this->assertTrue($mode->isInstall());
50 $this->assertFalse($mode->isNormal());
53 public function testWithoutConfig()
55 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
57 $this->assertTrue($this->root->hasChild('config/local.config.php'));
59 $this->delConfigFile('local.config.php');
61 $this->assertFalse($this->root->hasChild('config/local.config.php'));
63 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
65 $this->assertTrue($mode->isInstall());
66 $this->assertFalse($mode->isNormal());
68 $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
71 public function testWithoutDatabase()
73 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
75 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
77 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
79 $this->assertFalse($mode->isNormal());
80 $this->assertTrue($mode->isInstall());
82 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
83 $this->assertFalse($mode->has(Mode::DBAVAILABLE));
86 public function testWithoutDatabaseSetup()
88 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
90 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
91 $this->databaseMock->shouldReceive('fetchFirst')
92 ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
94 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
96 $this->assertFalse($mode->isNormal());
97 $this->assertTrue($mode->isInstall());
99 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
102 public function testWithMaintenanceMode()
104 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
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();
112 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
114 $this->assertFalse($mode->isNormal());
115 $this->assertFalse($mode->isInstall());
117 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
118 $this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
121 public function testNormalMode()
123 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
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();
134 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
136 $this->assertTrue($mode->isNormal());
137 $this->assertFalse($mode->isInstall());
139 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
140 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
144 * Test explicit disabled maintenance (in case you manually disable it)
146 public function testDisabledMaintenance()
148 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
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();
159 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
161 $this->assertTrue($mode->isNormal());
162 $this->assertFalse($mode->isInstall());
164 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
165 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
169 * Test that modes are immutable
171 public function testImmutable()
173 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
177 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
179 $this->assertNotSame($modeNew, $mode);
183 * Test if not called by index is backend
185 public function testIsBackendNotIndex()
187 $server = ['PHP_SELF' => '/daemon.php'];
188 $module = new Module();
190 $mode = (new Mode())->determineBackend($module, $server);
192 $this->assertTrue($mode->isBackend());
196 * Test is called by index but module is backend
198 public function testIsBackendButIndex()
200 $server = ['PHP_SELF' => '/index.php'];
201 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true);
203 $mode = (new Mode())->determineBackend($module, $server);
205 $this->assertTrue($mode->isBackend());
209 * Test is called by index and module is not backend
211 public function testIsNotBackend()
213 $server = ['PHP_SELF' => '/index.php'];
214 $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
216 $mode = (new Mode())->determineBackend($module, $server);
218 $this->assertFalse($mode->isBackend());