3 namespace Friendica\Test\src\App;
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;
14 class ModeTest extends MockedTest
20 * @var BasePath|MockInterface
22 private $basePathMock;
25 * @var Database|MockInterface
27 private $databaseMock;
30 * @var Config\Cache\ConfigCache|MockInterface
32 private $configCacheMock;
34 public function setUp()
40 $this->basePathMock = \Mockery::mock(BasePath::class);
41 $this->databaseMock = \Mockery::mock(Database::class);
42 $this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class);
45 public function testItEmpty()
48 $this->assertTrue($mode->isInstall());
49 $this->assertFalse($mode->isNormal());
52 public function testWithoutConfig()
54 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
56 $this->assertTrue($this->root->hasChild('config/local.config.php'));
58 $this->delConfigFile('local.config.php');
60 $this->assertFalse($this->root->hasChild('config/local.config.php'));
62 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
64 $this->assertTrue($mode->isInstall());
65 $this->assertFalse($mode->isNormal());
67 $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
70 public function testWithoutDatabase()
72 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
74 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
76 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
78 $this->assertFalse($mode->isNormal());
79 $this->assertTrue($mode->isInstall());
81 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
82 $this->assertFalse($mode->has(Mode::DBAVAILABLE));
85 public function testWithoutDatabaseSetup()
87 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
89 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
90 $this->databaseMock->shouldReceive('fetchFirst')
91 ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
93 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
95 $this->assertFalse($mode->isNormal());
96 $this->assertTrue($mode->isInstall());
98 $this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
101 public function testWithMaintenanceMode()
103 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
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();
111 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
113 $this->assertFalse($mode->isNormal());
114 $this->assertFalse($mode->isInstall());
116 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
117 $this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
120 public function testNormalMode()
122 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
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();
133 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
135 $this->assertTrue($mode->isNormal());
136 $this->assertFalse($mode->isInstall());
138 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
139 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
143 * Test explicit disabled maintenance (in case you manually disable it)
145 public function testDisabledMaintenance()
147 $this->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
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();
158 $mode = (new Mode())->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
160 $this->assertTrue($mode->isNormal());
161 $this->assertFalse($mode->isInstall());
163 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
164 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
168 * Test that modes are immutable
170 public function testImmutable()
172 $this->basePathMock->shouldReceive('getPath')->andReturn(null)->once();
176 $modeNew = $mode->determine($this->basePathMock, $this->databaseMock, $this->configCacheMock);
178 $this->assertNotSame($modeNew, $mode);