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->basePathMock->shouldReceive('getPath')->andReturn($this->root->url())->once();
43 $this->databaseMock = \Mockery::mock(Database::class);
44 $this->configCacheMock = \Mockery::mock(Config\Cache\ConfigCache::class);
47 public function testItEmpty()
49 $mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
50 $this->assertTrue($mode->isInstall());
51 $this->assertFalse($mode->isNormal());
54 public function testWithoutConfig()
56 $mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
58 $this->assertTrue($this->root->hasChild('config/local.config.php'));
60 $this->delConfigFile('local.config.php');
62 $this->assertFalse($this->root->hasChild('config/local.config.php'));
66 $this->assertTrue($mode->isInstall());
67 $this->assertFalse($mode->isNormal());
69 $this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
72 public function testWithoutDatabase()
74 $this->databaseMock->shouldReceive('connected')->andReturn(false)->once();
76 $mode = new Mode($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->databaseMock->shouldReceive('connected')->andReturn(true)->once();
89 $this->databaseMock->shouldReceive('fetchFirst')
90 ->with('SHOW TABLES LIKE \'config\'')->andReturn(false)->once();
92 $mode = new Mode($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->databaseMock->shouldReceive('connected')->andReturn(true)->once();
104 $this->databaseMock->shouldReceive('fetchFirst')
105 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
106 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
107 ->andReturn(true)->once();
109 $mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
112 $this->assertFalse($mode->isNormal());
113 $this->assertFalse($mode->isInstall());
115 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
116 $this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
119 public function testNormalMode()
121 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
122 $this->databaseMock->shouldReceive('fetchFirst')
123 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
124 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
125 ->andReturn(false)->once();
126 $this->databaseMock->shouldReceive('selectFirst')
127 ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
128 ->andReturn(['v' => null])->once();
130 $mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
133 $this->assertTrue($mode->isNormal());
134 $this->assertFalse($mode->isInstall());
136 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
137 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
141 * Test explicit disabled maintenance (in case you manually disable it)
143 public function testDisabledMaintenance()
145 $this->databaseMock->shouldReceive('connected')->andReturn(true)->once();
146 $this->databaseMock->shouldReceive('fetchFirst')
147 ->with('SHOW TABLES LIKE \'config\'')->andReturn(true)->once();
148 $this->configCacheMock->shouldReceive('get')->with('system', 'maintenance')
149 ->andReturn(false)->once();
150 $this->databaseMock->shouldReceive('selectFirst')
151 ->with('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])
152 ->andReturn(['v' => '0'])->once();
154 $mode = new Mode($this->basePathMock, $this->databaseMock, $this->configCacheMock);
157 $this->assertTrue($mode->isNormal());
158 $this->assertFalse($mode->isInstall());
160 $this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
161 $this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));