3 namespace Friendica\Test\src\Console;
7 use Friendica\Console\AutomaticInstallation;
8 use Friendica\Core\Config\Cache\ConfigCache;
9 use Friendica\Core\Installer;
10 use Friendica\Core\L10n\L10n;
11 use Friendica\Core\Logger;
12 use Friendica\Database\Database;
14 use Friendica\Test\Util\DBAMockTrait;
15 use Friendica\Test\Util\DBStructureMockTrait;
16 use Friendica\Test\Util\RendererMockTrait;
17 use Friendica\Test\Util\VFSTrait;
18 use Friendica\Util\Logger\VoidLogger;
19 use Mockery\MockInterface;
20 use org\bovigo\vfs\vfsStream;
21 use org\bovigo\vfs\vfsStreamFile;
23 class AutomaticInstallationConsoleTest extends ConsoleTest
27 use DBStructureMockTrait;
28 use RendererMockTrait;
31 * @var vfsStreamFile Assert file without DB credentials
35 * @var vfsStreamFile Assert file with DB credentials
37 private $assertFileDb;
40 * @var ConfigCache The configuration cache to check after each test
55 * @var Dice|MockInterface
59 public function setUp()
61 $this->markTestSkipped('Needs class \'Installer\' as constructing argument for console tests');
65 $this->setUpVfsDir();;
67 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
68 $this->root->getChild('config')
69 ->removeChild('local.config.php');
71 $this->dice = \Mockery::mock(Dice::class)->makePartial();
73 $l10nMock = \Mockery::mock(L10n::class);
74 $l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
76 $this->dice->shouldReceive('create')
78 ->andReturn($l10nMock);
80 DI::init($this->dice);
82 $this->configCache = new ConfigCache();
83 $this->configCache->set('system', 'basepath', $this->root->url());
84 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
85 $this->configCache->set('system', 'theme', 'smarty3');
87 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
88 if ($key !== 'basepath') {
89 return $this->configCache->set($cat, $key, $value);
95 $this->configMock->shouldReceive('has')->andReturn(true);
96 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
97 return $this->configCache->get($cat, $key);
99 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
100 $this->configCache->load($config, $overwrite);
103 $this->mode->shouldReceive('isInstall')->andReturn(true);
104 Logger::init(new VoidLogger());
108 * Returns the dataset for each automatic installation test
110 * @return array the dataset
112 public function dataInstaller()
126 'hostname' => 'friendica.local',
132 'url' => 'http://friendica.local',
134 'default_timezone' => '',
142 'hostname' => 'testhost',
144 'username' => 'friendica',
145 'password' => 'a password',
146 'database' => 'database',
150 'hostname' => 'friendica.local',
151 'admin_email' => 'admin@philipp.info',
154 'urlpath' => 'test/it',
155 'url' => 'http://friendica.local/test/it',
158 'default_timezone' => 'en',
159 'language' => 'Europe/Berlin',
166 'hostname' => 'testhost.new.domain',
168 'username' => 'fr"§%ica',
169 'password' => '$%\"gse',
174 'hostname' => 'friendica.local',
175 'admin_email' => 'admin@philipp.info',
178 'urlpath' => 'test/it',
179 'url' => 'https://friendica.local/test/it',
182 'default_timezone' => 'en',
183 'language' => 'Europe/Berlin',
190 private function assertFinished($txt, $withconfig = false, $copyfile = false)
198 Creating config file...
208 Copying config file...
215 Initializing setup...
220 Checking environment...
222 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
233 Inserting data into database...
244 Installation is finished
248 $this->assertEquals($finished, $txt);
251 private function assertStuckDB($txt)
254 Initializing setup...
259 Checking environment...
261 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
266 Creating config file...
274 Could not connect to database.:
279 $this->assertEquals($finished, $txt);
282 private function assertStuckURL($txt)
285 Initializing setup...
290 Checking environment...
292 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
297 Creating config file...
299 The Friendica URL has to be set during CLI installation.
303 $this->assertEquals($finished, $txt);
307 * Asserts one config entry
309 * @param string $cat The category to test
310 * @param string $key The key to test
311 * @param null|array $assertion The asserted value (null = empty, or array/string)
312 * @param string $default_value The default value
314 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
316 if (!empty($assertion[$cat][$key])) {
317 $this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
318 } elseif (!empty($assertion) && !is_array($assertion)) {
319 $this->assertEquals($assertion, $this->configCache->get($cat, $key));
320 } elseif (!empty($default_value)) {
321 $this->assertEquals($default_value, $this->configCache->get($cat, $key));
323 $this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
328 * Asserts all config entries
330 * @param null|array $assertion The optional assertion array
331 * @param boolean $saveDb True, if the db credentials should get saved to the file
332 * @param boolean $default True, if we use the default values
333 * @param boolean $defaultDb True, if we use the default value for the DB
334 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
336 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
338 if (!empty($assertion['database']['hostname'])) {
339 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
342 $this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
343 $this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
344 $this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
345 $this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
347 $this->assertConfigEntry('config', 'admin_email', $assertion);
348 $this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
349 $this->assertConfigEntry('config', 'hostname', $assertion);
351 $this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
352 $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
353 $this->assertConfigEntry('system', 'url', $assertion);
354 $this->assertConfigEntry('system', 'urlpath', $assertion);
355 $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? App\BaseURL::DEFAULT_SSL_SCHEME : null);
356 $this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
360 * Test the automatic installation without any parameter/setting
361 * Should stuck because of missing hostname
363 public function testEmpty()
365 $console = new AutomaticInstallation($this->consoleArgv);
367 $txt = $this->dumpExecute($console);
369 $this->assertStuckURL($txt);
373 * Test the automatic installation without any parameter/setting
376 public function testEmptyWithURL()
378 $this->mockConnect(true, 1);
379 $this->mockConnected(true, 1);
380 $this->mockExistsTable('user', false, 1);
381 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
383 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
384 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
386 $console = new AutomaticInstallation($this->consoleArgv);
387 $console->setOption('url', 'http://friendica.local');
389 $txt = $this->dumpExecute($console);
391 $this->assertFinished($txt, true, false);
392 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
394 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
398 * Test the automatic installation with a prepared config file
399 * @dataProvider dataInstaller
401 public function testWithConfig(array $data)
403 $this->mockConnect(true, 1);
404 $this->mockConnected(true, 1);
405 $this->mockExistsTable('user', false, 1);
406 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
408 $conf = function ($cat, $key) use ($data) {
409 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
410 return $data[$cat][$key] . ':' . $data['database']['port'];
412 return $data[$cat][$key];
418 // Local configuration
420 // If you're unsure about what any of the config keys below do, please check the static/defaults.config.php for detailed
421 // documentation of their data type and behavior.
425 'hostname' => '{$conf('database', 'hostname')}',
426 'username' => '{$conf('database', 'username')}',
427 'password' => '{$conf('database', 'password')}',
428 'database' => '{$conf('database', 'database')}',
429 'charset' => 'utf8mb4',
432 // ****************************************************************
433 // The configuration below will be overruled by the admin panel.
434 // Changes made below will only have an effect if the database does
435 // not contain any configuration for the friendica system.
436 // ****************************************************************
439 'admin_email' => '{$conf('config', 'admin_email')}',
440 'hostname' => '{$conf('config', 'hostname')}',
441 'sitename' => 'Friendica Social Network',
442 'register_policy' => \Friendica\Module\Register::OPEN,
443 'register_text' => '',
446 'basepath' => '{$conf('system', 'basepath')}',
447 'urlpath' => '{$conf('system', 'urlpath')}',
448 'url' => '{$conf('system', 'url')}',
449 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
450 'default_timezone' => '{$conf('system', 'default_timezone')}',
451 'language' => '{$conf('system', 'language')}',
456 vfsStream::newFile('prepared.config.php')
458 ->setContent($config);
460 $console = new AutomaticInstallation($this->consoleArgv);
461 $console->setOption('f', 'prepared.config.php');
463 $txt = $this->dumpExecute($console);
465 $this->assertFinished($txt, false, true);
467 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
468 $this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
470 $this->assertConfig($data, true, false, false);
474 * Test the automatic installation with environment variables
475 * Includes saving the DB credentials to the file
476 * @dataProvider dataInstaller
478 public function testWithEnvironmentAndSave(array $data)
480 $this->mockConnect(true, 1);
481 $this->mockConnected(true, 1);
482 $this->mockExistsTable('user', false, 1);
483 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
485 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
486 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
488 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
489 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
490 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
491 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
492 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
494 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
495 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
496 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
497 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
498 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
499 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
500 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
502 $console = new AutomaticInstallation($this->consoleArgv);
503 $console->setOption('savedb', true);
505 $txt = $this->dumpExecute($console);
507 $this->assertFinished($txt, true);
508 $this->assertConfig($data, true, true, false, true);
512 * Test the automatic installation with environment variables
513 * Don't save the db credentials to the file
514 * @dataProvider dataInstaller
516 public function testWithEnvironmentWithoutSave(array $data)
518 $this->mockConnect(true, 1);
519 $this->mockConnected(true, 1);
520 $this->mockExistsTable('user', false, 1);
521 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
523 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
524 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
526 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
527 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
528 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
529 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
530 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
532 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
533 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
534 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
535 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
536 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
537 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
538 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
540 $console = new AutomaticInstallation($this->consoleArgv);
542 $txt = $this->dumpExecute($console);
544 $this->assertFinished($txt, true);
545 $this->assertConfig($data, false, true, false, true);
549 * Test the automatic installation with arguments
550 * @dataProvider dataInstaller
552 public function testWithArguments(array $data)
554 $this->mockConnect(true, 1);
555 $this->mockConnected(true, 1);
556 $this->mockExistsTable('user', false, 1);
557 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
559 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
560 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
562 $console = new AutomaticInstallation($this->consoleArgv);
564 $option = function($var, $cat, $key) use ($data, $console) {
565 if (!empty($data[$cat][$key])) {
566 $console->setOption($var, $data[$cat][$key]);
569 $option('dbhost' , 'database', 'hostname');
570 $option('dbport' , 'database', 'port');
571 $option('dbuser' , 'database', 'username');
572 $option('dbpass' , 'database', 'password');
573 $option('dbdata' , 'database', 'database');
574 $option('url' , 'system' , 'url');
575 $option('phppath' , 'config' , 'php_path');
576 $option('admin' , 'config' , 'admin_email');
577 $option('tz' , 'system' , 'default_timezone');
578 $option('lang' , 'system' , 'language');
579 $option('basepath' , 'system' , 'basepath');
581 $txt = $this->dumpExecute($console);
583 $this->assertFinished($txt, true);
584 $this->assertConfig($data, true, true, true, true);
588 * Test the automatic installation with a wrong database connection
590 public function testNoDatabaseConnection()
592 $this->mockConnect(false, 1);
594 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
595 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
597 $console = new AutomaticInstallation($this->consoleArgv);
598 $console->setOption('url', 'http://friendica.local');
600 $txt = $this->dumpExecute($console);
602 $this->assertStuckDB($txt);
603 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
605 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
608 public function testGetHelp()
610 // Usable to purposely fail if new commands are added without taking tests into account
612 Installation - Install Friendica automatically
614 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
617 Installs Friendica with data based on the local.config.php file or environment variables
620 Not checking .htaccess/URL-Rewrite during CLI installation.
623 -h|--help|-? Show help information
624 -v Show more debug information.
625 -a All setup checks are required (except .htaccess)
626 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
627 -s|--savedb Save the DB credentials to the file (if environment variables is used)
628 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
629 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
630 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
631 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
632 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
633 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
634 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
635 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
636 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
637 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
639 Environment variables
640 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
641 MYSQL_PORT The port of the mysql/mariadb database
642 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
643 MYSQL_PASSWORD The password of the mysql/mariadb database login
644 MYSQL_DATABASE The name of the mysql/mariadb database
645 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
646 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
647 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
648 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
649 FRIENDICA_TZ The timezone of Friendica
650 FRIENDICA_LANG The langauge of Friendica
653 bin/console autoinstall -f 'input.config.php
654 Installs Friendica with the prepared 'input.config.php' file
656 bin/console autoinstall --savedb
657 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
659 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
660 Installs Friendica with a local mysql database with credentials
664 $console = new AutomaticInstallation($this->consoleArgv);
665 $console->setOption('help', true);
667 $txt = $this->dumpExecute($console);
669 $this->assertEquals($theHelp, $txt);