3 namespace Friendica\Test\src\Core\Console;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Console\AutomaticInstallation;
7 use Friendica\Core\Installer;
8 use Friendica\Core\Logger;
9 use Friendica\Test\Util\DBAMockTrait;
10 use Friendica\Test\Util\DBStructureMockTrait;
11 use Friendica\Test\Util\L10nMockTrait;
12 use Friendica\Test\Util\RendererMockTrait;
13 use Friendica\Util\BaseURL;
14 use Friendica\Util\Logger\VoidLogger;
15 use org\bovigo\vfs\vfsStream;
16 use org\bovigo\vfs\vfsStreamFile;
21 class AutomaticInstallationConsoleTest extends ConsoleTest
25 use DBStructureMockTrait;
26 use RendererMockTrait;
29 * @var vfsStreamFile Assert file without DB credentials
33 * @var vfsStreamFile Assert file with DB credentials
35 private $assertFileDb;
38 * @var ConfigCache The configuration cache to check after each test
42 public function setUp()
46 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
47 $this->root->getChild('config')
48 ->removeChild('local.config.php');
53 $this->configCache = new ConfigCache();
54 $this->configCache->set('system', 'basepath', $this->root->url());
55 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
56 $this->configCache->set('system', 'theme', 'smarty3');
58 $this->mockApp($this->root, true);
60 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
61 if ($key !== 'basepath') {
62 return $this->configCache->set($cat, $key, $value);
68 $this->configMock->shouldReceive('has')->andReturn(true);
69 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
70 return $this->configCache->get($cat, $key);
72 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
73 return $this->configCache->load($config, $overwrite);
76 $this->mode->shouldReceive('isInstall')->andReturn(true);
77 Logger::init(new VoidLogger());
81 * Returns the dataset for each automatic installation test
83 * @return array the dataset
85 public function dataInstaller()
99 'hostname' => 'friendica.local',
105 'url' => 'http://friendica.local',
107 'default_timezone' => '',
115 'hostname' => 'testhost',
117 'username' => 'friendica',
118 'password' => 'a password',
119 'database' => 'database',
123 'hostname' => 'friendica.local',
124 'admin_email' => 'admin@philipp.info',
127 'urlpath' => 'test/it',
128 'url' => 'http://friendica.local/test/it',
131 'default_timezone' => 'en',
132 'language' => 'Europe/Berlin',
139 'hostname' => 'testhost.new.domain',
141 'username' => 'fr"ยง%ica',
142 'password' => '$%\"gse',
147 'hostname' => 'friendica.local',
148 'admin_email' => 'admin@philipp.info',
151 'urlpath' => 'test/it',
152 'url' => 'https://friendica.local/test/it',
155 'default_timezone' => 'en',
156 'language' => 'Europe/Berlin',
163 private function assertFinished($txt, $withconfig = false, $copyfile = false)
171 Creating config file...
181 Copying config file...
188 Initializing setup...
193 Checking environment...
195 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
206 Inserting data into database...
217 Installation is finished
221 $this->assertEquals($finished, $txt);
224 private function assertStuckDB($txt)
227 Initializing setup...
232 Checking environment...
234 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
239 Creating config file...
247 Could not connect to database.:
252 $this->assertEquals($finished, $txt);
255 private function assertStuckURL($txt)
258 Initializing setup...
263 Checking environment...
265 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
270 Creating config file...
272 The Friendica URL has to be set during CLI installation.
276 $this->assertEquals($finished, $txt);
280 * Asserts one config entry
282 * @param string $cat The category to test
283 * @param string $key The key to test
284 * @param null|array $assertion The asserted value (null = empty, or array/string)
285 * @param string $default_value The default value
287 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
289 if (!empty($assertion[$cat][$key])) {
290 $this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
291 } elseif (!empty($assertion) && !is_array($assertion)) {
292 $this->assertEquals($assertion, $this->configCache->get($cat, $key));
293 } elseif (!empty($default_value)) {
294 $this->assertEquals($default_value, $this->configCache->get($cat, $key));
296 $this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
301 * Asserts all config entries
303 * @param null|array $assertion The optional assertion array
304 * @param boolean $saveDb True, if the db credentials should get saved to the file
305 * @param boolean $default True, if we use the default values
306 * @param boolean $defaultDb True, if we use the default value for the DB
307 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
309 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
311 if (!empty($assertion['database']['hostname'])) {
312 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
315 $this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
316 $this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
317 $this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
318 $this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
320 $this->assertConfigEntry('config', 'admin_email', $assertion);
321 $this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
322 $this->assertConfigEntry('config', 'hostname', $assertion);
324 $this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
325 $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
326 $this->assertConfigEntry('system', 'url', $assertion);
327 $this->assertConfigEntry('system', 'urlpath', $assertion);
328 $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? BaseURL::DEFAULT_SSL_SCHEME : null);
329 $this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
333 * Test the automatic installation without any parameter/setting
334 * Should stuck because of missing hostname
336 public function testEmpty()
338 $console = new AutomaticInstallation($this->consoleArgv);
340 $txt = $this->dumpExecute($console);
342 $this->assertStuckURL($txt);
346 * Test the automatic installation without any parameter/setting
349 public function testEmptyWithURL()
351 $this->mockConnect(true, 1);
352 $this->mockConnected(true, 1);
353 $this->mockExistsTable('user', false, 1);
354 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
356 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
357 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
359 $console = new AutomaticInstallation($this->consoleArgv);
360 $console->setOption('url', 'http://friendica.local');
362 $txt = $this->dumpExecute($console);
364 $this->assertFinished($txt, true, false);
365 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
367 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
371 * Test the automatic installation with a prepared config file
372 * @dataProvider dataInstaller
374 public function testWithConfig(array $data)
376 $this->mockConnect(true, 1);
377 $this->mockConnected(true, 1);
378 $this->mockExistsTable('user', false, 1);
379 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
381 $conf = function ($cat, $key) use ($data) {
382 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
383 return $data[$cat][$key] . ':' . $data['database']['port'];
385 return $data[$cat][$key];
391 // Local configuration
393 // If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
394 // documentation of their data type and behavior.
398 'hostname' => '{$conf('database', 'hostname')}',
399 'username' => '{$conf('database', 'username')}',
400 'password' => '{$conf('database', 'password')}',
401 'database' => '{$conf('database', 'database')}',
402 'charset' => 'utf8mb4',
405 // ****************************************************************
406 // The configuration below will be overruled by the admin panel.
407 // Changes made below will only have an effect if the database does
408 // not contain any configuration for the friendica system.
409 // ****************************************************************
412 'admin_email' => '{$conf('config', 'admin_email')}',
413 'hostname' => '{$conf('config', 'hostname')}',
414 'sitename' => 'Friendica Social Network',
415 'register_policy' => \Friendica\Module\Register::OPEN,
416 'register_text' => '',
419 'basepath' => '{$conf('system', 'basepath')}',
420 'urlpath' => '{$conf('system', 'urlpath')}',
421 'url' => '{$conf('system', 'url')}',
422 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
423 'default_timezone' => '{$conf('system', 'default_timezone')}',
424 'language' => '{$conf('system', 'language')}',
429 vfsStream::newFile('prepared.config.php')
431 ->setContent($config);
433 $console = new AutomaticInstallation($this->consoleArgv);
434 $console->setOption('f', 'prepared.config.php');
436 $txt = $this->dumpExecute($console);
438 $this->assertFinished($txt, false, true);
440 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
441 $this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
443 $this->assertConfig($data, true, false, false);
447 * Test the automatic installation with environment variables
448 * Includes saving the DB credentials to the file
449 * @dataProvider dataInstaller
451 public function testWithEnvironmentAndSave(array $data)
453 $this->mockConnect(true, 1);
454 $this->mockConnected(true, 1);
455 $this->mockExistsTable('user', false, 1);
456 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
458 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
459 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
461 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
462 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
463 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
464 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
465 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
467 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
468 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
469 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
470 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
471 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
472 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
473 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
475 $console = new AutomaticInstallation($this->consoleArgv);
476 $console->setOption('savedb', true);
478 $txt = $this->dumpExecute($console);
480 $this->assertFinished($txt, true);
481 $this->assertConfig($data, true, true, false, true);
485 * Test the automatic installation with environment variables
486 * Don't save the db credentials to the file
487 * @dataProvider dataInstaller
489 public function testWithEnvironmentWithoutSave(array $data)
491 $this->mockConnect(true, 1);
492 $this->mockConnected(true, 1);
493 $this->mockExistsTable('user', false, 1);
494 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
496 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
497 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
499 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
500 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
501 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
502 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
503 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
505 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
506 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
507 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
508 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
509 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
510 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
511 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
513 $console = new AutomaticInstallation($this->consoleArgv);
515 $txt = $this->dumpExecute($console);
517 $this->assertFinished($txt, true);
518 $this->assertConfig($data, false, true, false, true);
522 * Test the automatic installation with arguments
523 * @dataProvider dataInstaller
525 public function testWithArguments(array $data)
527 $this->mockConnect(true, 1);
528 $this->mockConnected(true, 1);
529 $this->mockExistsTable('user', false, 1);
530 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
532 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
533 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
535 $console = new AutomaticInstallation($this->consoleArgv);
537 $option = function($var, $cat, $key) use ($data, $console) {
538 if (!empty($data[$cat][$key])) {
539 $console->setOption($var, $data[$cat][$key]);
542 $option('dbhost' , 'database', 'hostname');
543 $option('dbport' , 'database', 'port');
544 $option('dbuser' , 'database', 'username');
545 $option('dbpass' , 'database', 'password');
546 $option('dbdata' , 'database', 'database');
547 $option('url' , 'system' , 'url');
548 $option('phppath' , 'config' , 'php_path');
549 $option('admin' , 'config' , 'admin_email');
550 $option('tz' , 'system' , 'default_timezone');
551 $option('lang' , 'system' , 'language');
552 $option('basepath' , 'system' , 'basepath');
554 $txt = $this->dumpExecute($console);
556 $this->assertFinished($txt, true);
557 $this->assertConfig($data, true, true, true, true);
561 * Test the automatic installation with a wrong database connection
563 public function testNoDatabaseConnection()
565 $this->mockConnect(false, 1);
567 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
568 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
570 $console = new AutomaticInstallation($this->consoleArgv);
571 $console->setOption('url', 'http://friendica.local');
573 $txt = $this->dumpExecute($console);
575 $this->assertStuckDB($txt);
576 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
578 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
581 public function testGetHelp()
583 // Usable to purposely fail if new commands are added without taking tests into account
585 Installation - Install Friendica automatically
587 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
590 Installs Friendica with data based on the local.config.php file or environment variables
593 Not checking .htaccess/URL-Rewrite during CLI installation.
596 -h|--help|-? Show help information
597 -v Show more debug information.
598 -a All setup checks are required (except .htaccess)
599 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
600 -s|--savedb Save the DB credentials to the file (if environment variables is used)
601 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
602 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
603 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
604 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
605 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
606 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
607 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
608 -b|--basepath <base_path> The basepath of Friendica(env FRIENDICA_BASE_PATH)
609 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
610 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
612 Environment variables
613 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
614 MYSQL_PORT The port of the mysql/mariadb database
615 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
616 MYSQL_PASSWORD The password of the mysql/mariadb database login
617 MYSQL_DATABASE The name of the mysql/mariadb database
618 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
619 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
620 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
621 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
622 FRIENDICA_TZ The timezone of Friendica
623 FRIENDICA_LANG The langauge of Friendica
626 bin/console autoinstall -f 'input.config.php
627 Installs Friendica with the prepared 'input.config.php' file
629 bin/console autoinstall --savedb
630 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
632 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
633 Installs Friendica with a local mysql database with credentials
637 $console = new AutomaticInstallation($this->consoleArgv);
638 $console->setOption('help', true);
640 $txt = $this->dumpExecute($console);
642 $this->assertEquals($theHelp, $txt);