3 namespace Friendica\Test\src\Core\Console;
5 use Friendica\Core\Console\AutomaticInstallation;
6 use Friendica\Test\Util\DBAMockTrait;
7 use Friendica\Test\Util\DBStructureMockTrait;
8 use org\bovigo\vfs\vfsStream;
9 use org\bovigo\vfs\vfsStreamFile;
12 * @runTestsInSeparateProcesses
13 * @preserveGlobalState disabled
16 class AutomaticInstallationConsoleTest extends ConsoleTest
19 use DBStructureMockTrait;
28 * @var vfsStreamFile Assert file without DB credentials
32 * @var vfsStreamFile Assert file with DB credentials
34 private $assertFileDb;
36 public function setUp()
40 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
41 $this->root->getChild('config')
42 ->removeChild('local.ini.php');
45 $this->db_host = getenv('MYSQL_HOST');
46 $this->db_port = (!empty(getenv('MYSQL_PORT'))) ? getenv('MYSQL_PORT') : null;
47 $this->db_data = getenv('MYSQL_DATABASE');
48 $this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
49 $this->db_pass = getenv('MYSQL_PASSWORD');
51 $this->mockConfigGet('config', 'php_path', false);
53 $assertFile = dirname(__DIR__) . DIRECTORY_SEPARATOR .
54 '..' . DIRECTORY_SEPARATOR .
55 '..' . DIRECTORY_SEPARATOR .
56 'datasets' . DIRECTORY_SEPARATOR .
57 'ini' . DIRECTORY_SEPARATOR .
59 $this->assertFile = vfsStream::newFile('assert.ini.php')
60 ->at($this->root->getChild('test'))
61 ->setContent($this->replaceEnvironmentSettings($assertFile, false));
62 $this->assertFileDb = vfsStream::newFile('assert_db.ini.php')
63 ->at($this->root->getChild('test'))
64 ->setContent($this->replaceEnvironmentSettings($assertFile, true));
68 * Replacing environment specific variables in the assertion file
70 * @param string $file The file to compare in later tests
71 * @param bool $withDb If true, db settings are replaced too
72 * @return string The file content
74 private function replaceEnvironmentSettings($file, $withDb)
76 $fileContent = file_get_contents($file);
77 $fileContent = str_replace("/usr/bin/php", trim(shell_exec('which php')), $fileContent);
79 $fileContent = str_replace("hostname = \"\"", "hostname = \"" . $this->db_host . (!empty($this->db_port) ? ":" . $this->db_port : "") . "\"", $fileContent);
80 $fileContent = str_replace("username = \"\"", "username = \"" . $this->db_user . "\"", $fileContent);
81 $fileContent = str_replace("password = \"\"", "password = \"" . $this->db_pass . "\"", $fileContent);
82 $fileContent = str_replace("database = \"\"", "database = \"" . $this->db_data . "\"", $fileContent);
87 private function assertFinished($txt, $withconfig = false, $copyfile = false)
95 Creating config file...
105 Copying config file...
112 Initializing setup...
117 Checking environment...
119 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
130 Inserting data into database...
141 Installation is finished
145 $this->assertEquals($finished, $txt);
148 private function assertStuckDB($txt)
151 Initializing setup...
156 Checking environment...
158 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
163 Creating config file...
171 Could not connect to database.:
176 $this->assertEquals($finished, $txt);
182 public function testWithConfig()
184 $this->mockConnect(true, 1);
185 $this->mockConnected(true, 1);
186 $this->mockExistsTable('user', false, 1);
187 $this->mockUpdate([false, true, true], null, 1);
200 ; ****************************************************************
201 ; The configuration below will be overruled by the admin panel.
202 ; Changes made below will only have an effect if the database does
203 ; not contain any configuration for the friendica system.
204 ; ****************************************************************
209 sitename = Friendica Social Network
211 register_policy = REGISTER_OPEN
215 default_timezone = UTC
223 vfsStream::newFile('prepared.ini.php')
225 ->setContent($config);
227 $console = new AutomaticInstallation($this->consoleArgv);
228 $console->setOption('f', 'prepared.ini.php');
230 $txt = $this->dumpExecute($console);
232 $this->assertFinished($txt, false, true);
234 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
240 public function testWithEnvironmentAndSave()
242 $this->mockConnect(true, 1);
243 $this->mockConnected(true, 1);
244 $this->mockExistsTable('user', false, 1);
245 $this->mockUpdate([false, true, true], null, 1);
247 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
248 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
249 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
250 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
252 $console = new AutomaticInstallation($this->consoleArgv);
253 $console->setOption('savedb', true);
255 $txt = $this->dumpExecute($console);
257 $this->assertFinished($txt, true);
259 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
261 $this->assertFileEquals(
262 $this->assertFileDb->url(),
263 $this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')->url());
269 public function testWithEnvironmentWithoutSave()
271 $this->mockConnect(true, 1);
272 $this->mockConnected(true, 1);
273 $this->mockExistsTable('user', false, 1);
274 $this->mockUpdate([false, true, true], null, 1);
276 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
277 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
278 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
279 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
281 $console = new AutomaticInstallation($this->consoleArgv);
283 $txt = $this->dumpExecute($console);
285 $this->assertFinished($txt, true);
287 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
289 $this->assertFileEquals(
290 $this->assertFile->url(),
291 $this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')->url());
297 public function testWithArguments()
299 $this->mockConnect(true, 1);
300 $this->mockConnected(true, 1);
301 $this->mockExistsTable('user', false, 1);
302 $this->mockUpdate([false, true, true], null, 1);
304 $console = new AutomaticInstallation($this->consoleArgv);
306 $console->setOption('dbhost', $this->db_host);
307 $console->setOption('dbuser', $this->db_user);
308 if (!empty($this->db_pass)) {
309 $console->setOption('dbpass', $this->db_pass);
311 if (!empty($this->db_port)) {
312 $console->setOption('dbport', $this->db_port);
314 $console->setOption('dbdata', $this->db_data);
316 $console->setOption('admin', 'admin@friendica.local');
317 $console->setOption('tz', 'Europe/Berlin');
318 $console->setOption('lang', 'de');
320 $console->setOption('urlpath', '/friendica');
322 $txt = $this->dumpExecute($console);
324 $this->assertFinished($txt, true);
326 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
328 $this->assertFileEquals(
329 $this->assertFileDb->url(),
330 $this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')->url());
334 * @runTestsInSeparateProcesses
335 * @preserveGlobalState disabled
337 public function testNoDatabaseConnection()
339 $this->mockConnect(false, 1);
341 $console = new AutomaticInstallation($this->consoleArgv);
343 $txt = $this->dumpExecute($console);
345 $this->assertStuckDB($txt);
348 public function testGetHelp()
350 // Usable to purposely fail if new commands are added without taking tests into account
352 Installation - Install Friendica automatically
354 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
357 Installs Friendica with data based on the local.ini.php file or environment variables
360 Not checking .htaccess/URL-Rewrite during CLI installation.
363 -h|--help|-? Show help information
364 -v Show more debug information.
365 -a All setup checks are required (except .htaccess)
366 -f|--file <config> prepared config file (e.g. "config/local.ini.php" itself) which will override every other config option - except the environment variables)
367 -s|--savedb Save the DB credentials to the file (if environment variables is used)
368 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
369 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
370 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
371 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
372 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
373 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
374 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
375 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
376 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
377 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
379 Environment variables
380 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
381 MYSQL_PORT The port of the mysql/mariadb database
382 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
383 MYSQL_PASSWORD The password of the mysql/mariadb database login
384 MYSQL_DATABASE The name of the mysql/mariadb database
385 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
386 FRIENDICA_PHP_PATH The path of the PHP binary
387 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
388 FRIENDICA_TZ The timezone of Friendica
389 FRIENDICA_LANG The langauge of Friendica
392 bin/console autoinstall -f 'input.ini.php
393 Installs Friendica with the prepared 'input.ini.php' file
395 bin/console autoinstall --savedb
396 Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
398 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
399 Installs Friendica with a local mysql database with credentials
403 $console = new AutomaticInstallation($this->consoleArgv);
404 $console->setOption('help', true);
406 $txt = $this->dumpExecute($console);
408 $this->assertEquals($txt, $theHelp);