3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Console;
26 use Friendica\Console\AutomaticInstallation;
27 use Friendica\Core\Config\Cache;
28 use Friendica\Core\Installer;
29 use Friendica\Core\L10n;
30 use Friendica\Core\Logger;
31 use Friendica\Database\Database;
33 use Friendica\Test\Util\DBAMockTrait;
34 use Friendica\Test\Util\DBStructureMockTrait;
35 use Friendica\Test\Util\RendererMockTrait;
36 use Friendica\Test\Util\VFSTrait;
37 use Friendica\Util\Logger\VoidLogger;
39 use Mockery\MockInterface;
40 use org\bovigo\vfs\vfsStream;
41 use org\bovigo\vfs\vfsStreamFile;
43 class AutomaticInstallationConsoleTest extends ConsoleTest
47 use DBStructureMockTrait;
48 use RendererMockTrait;
51 * @var vfsStreamFile Assert file without DB credentials
55 * @var vfsStreamFile Assert file with DB credentials
57 private $assertFileDb;
60 * @var Cache The configuration cache to check after each test
75 * @var Dice|MockInterface
79 public function setUp()
81 static::markTestSkipped('Needs class \'Installer\' as constructing argument for console tests');
85 $this->setUpVfsDir();;
87 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
88 $this->root->getChild('config')
89 ->removeChild('local.config.php');
91 $this->dice = Mockery::mock(Dice::class)->makePartial();
93 $l10nMock = Mockery::mock(L10n::class);
94 $l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
96 $this->dice->shouldReceive('create')
98 ->andReturn($l10nMock);
100 DI::init($this->dice);
102 $this->configCache = new Cache();
103 $this->configCache->set('system', 'basepath', $this->root->url());
104 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
105 $this->configCache->set('system', 'theme', 'smarty3');
107 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
108 if ($key !== 'basepath') {
109 return $this->configCache->set($cat, $key, $value);
115 $this->configMock->shouldReceive('has')->andReturn(true);
116 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
117 return $this->configCache->get($cat, $key);
119 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
120 $this->configCache->load($config, $overwrite);
123 $this->mode->shouldReceive('isInstall')->andReturn(true);
124 Logger::init(new VoidLogger());
128 * Returns the dataset for each automatic installation test
130 * @return array the dataset
132 public function dataInstaller()
146 'hostname' => 'friendica.local',
152 'url' => 'http://friendica.local',
154 'default_timezone' => '',
162 'hostname' => 'testhost',
164 'username' => 'friendica',
165 'password' => 'a password',
166 'database' => 'database',
170 'hostname' => 'friendica.local',
171 'admin_email' => 'admin@philipp.info',
174 'urlpath' => 'test/it',
175 'url' => 'http://friendica.local/test/it',
178 'default_timezone' => 'en',
179 'language' => 'Europe/Berlin',
186 'hostname' => 'testhost.new.domain',
188 'username' => 'fr"§%ica',
189 'password' => '$%\"gse',
194 'hostname' => 'friendica.local',
195 'admin_email' => 'admin@philipp.info',
198 'urlpath' => 'test/it',
199 'url' => 'https://friendica.local/test/it',
202 'default_timezone' => 'en',
203 'language' => 'Europe/Berlin',
210 private function assertFinished($txt, $withconfig = false, $copyfile = false)
218 Creating config file...
228 Copying config file...
235 Initializing setup...
240 Checking environment...
242 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
253 Inserting data into database...
264 Installation is finished
268 self::assertEquals($finished, $txt);
271 private function assertStuckDB($txt)
274 Initializing setup...
279 Checking environment...
281 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
286 Creating config file...
294 Could not connect to database.:
299 self::assertEquals($finished, $txt);
302 private function assertStuckURL($txt)
305 Initializing setup...
310 Checking environment...
312 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
317 Creating config file...
319 The Friendica URL has to be set during CLI installation.
323 self::assertEquals($finished, $txt);
327 * Asserts one config entry
329 * @param string $cat The category to test
330 * @param string $key The key to test
331 * @param null|array $assertion The asserted value (null = empty, or array/string)
332 * @param string $default_value The default value
334 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
336 if (!empty($assertion[$cat][$key])) {
337 self::assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
338 } elseif (!empty($assertion) && !is_array($assertion)) {
339 self::assertEquals($assertion, $this->configCache->get($cat, $key));
340 } elseif (!empty($default_value)) {
341 self::assertEquals($default_value, $this->configCache->get($cat, $key));
343 self::assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
348 * Asserts all config entries
350 * @param null|array $assertion The optional assertion array
351 * @param boolean $saveDb True, if the db credentials should get saved to the file
352 * @param boolean $default True, if we use the default values
353 * @param boolean $defaultDb True, if we use the default value for the DB
354 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
356 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
358 if (!empty($assertion['database']['hostname'])) {
359 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
362 self::assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
363 self::assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
364 self::assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
365 self::assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
367 self::assertConfigEntry('config', 'admin_email', $assertion);
368 self::assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
369 self::assertConfigEntry('config', 'hostname', $assertion);
371 self::assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
372 self::assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
373 self::assertConfigEntry('system', 'url', $assertion);
374 self::assertConfigEntry('system', 'urlpath', $assertion);
375 self::assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? App\BaseURL::DEFAULT_SSL_SCHEME : null);
376 self::assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
380 * Test the automatic installation without any parameter/setting
381 * Should stuck because of missing hostname
383 public function testEmpty()
385 $console = new AutomaticInstallation($this->consoleArgv);
387 $txt = $this->dumpExecute($console);
389 self::assertStuckURL($txt);
393 * Test the automatic installation without any parameter/setting
396 public function testEmptyWithURL()
398 $this->mockConnect(true, 1);
399 $this->mockConnected(true, 1);
400 $this->mockExistsTable('user', false, 1);
401 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
403 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
404 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
406 $console = new AutomaticInstallation($this->consoleArgv);
407 $console->setOption('url', 'http://friendica.local');
409 $txt = $this->dumpExecute($console);
411 self::assertFinished($txt, true, false);
412 self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
414 self::assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
418 * Test the automatic installation with a prepared config file
419 * @dataProvider dataInstaller
421 public function testWithConfig(array $data)
423 $this->mockConnect(true, 1);
424 $this->mockConnected(true, 1);
425 $this->mockExistsTable('user', false, 1);
426 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
428 $conf = function ($cat, $key) use ($data) {
429 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
430 return $data[$cat][$key] . ':' . $data['database']['port'];
432 return $data[$cat][$key];
438 // Local configuration
440 // If you're unsure about what any of the config keys below do, please check the static/defaults.config.php for detailed
441 // documentation of their data type and behavior.
445 'hostname' => '{$conf('database', 'hostname')}',
446 'username' => '{$conf('database', 'username')}',
447 'password' => '{$conf('database', 'password')}',
448 'database' => '{$conf('database', 'database')}',
449 'charset' => 'utf8mb4',
452 // ****************************************************************
453 // The configuration below will be overruled by the admin panel.
454 // Changes made below will only have an effect if the database does
455 // not contain any configuration for the friendica system.
456 // ****************************************************************
459 'admin_email' => '{$conf('config', 'admin_email')}',
460 'hostname' => '{$conf('config', 'hostname')}',
461 'sitename' => 'Friendica Social Network',
462 'register_policy' => \Friendica\Module\Register::OPEN,
463 'register_text' => '',
466 'basepath' => '{$conf('system', 'basepath')}',
467 'urlpath' => '{$conf('system', 'urlpath')}',
468 'url' => '{$conf('system', 'url')}',
469 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
470 'default_timezone' => '{$conf('system', 'default_timezone')}',
471 'language' => '{$conf('system', 'language')}',
476 vfsStream::newFile('prepared.config.php')
478 ->setContent($config);
480 $console = new AutomaticInstallation($this->consoleArgv);
481 $console->setOption('f', 'prepared.config.php');
483 $txt = $this->dumpExecute($console);
485 self::assertFinished($txt, false, true);
487 self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
488 self::assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
490 self::assertConfig($data, true, false, false);
494 * Test the automatic installation with environment variables
495 * Includes saving the DB credentials to the file
496 * @dataProvider dataInstaller
498 public function testWithEnvironmentAndSave(array $data)
500 $this->mockConnect(true, 1);
501 $this->mockConnected(true, 1);
502 $this->mockExistsTable('user', false, 1);
503 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
505 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
506 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
508 self::assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
509 self::assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
510 self::assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
511 self::assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
512 self::assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
514 self::assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
515 self::assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
516 self::assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
517 self::assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
518 self::assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
519 self::assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
520 self::assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
522 $console = new AutomaticInstallation($this->consoleArgv);
523 $console->setOption('savedb', true);
525 $txt = $this->dumpExecute($console);
527 self::assertFinished($txt, true);
528 self::assertConfig($data, true, true, false, true);
532 * Test the automatic installation with environment variables
533 * Don't save the db credentials to the file
534 * @dataProvider dataInstaller
536 public function testWithEnvironmentWithoutSave(array $data)
538 $this->mockConnect(true, 1);
539 $this->mockConnected(true, 1);
540 $this->mockExistsTable('user', false, 1);
541 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
543 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
544 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
546 self::assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
547 self::assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
548 self::assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
549 self::assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
550 self::assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
552 self::assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
553 self::assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
554 self::assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
555 self::assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
556 self::assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
557 self::assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
558 self::assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
560 $console = new AutomaticInstallation($this->consoleArgv);
562 $txt = $this->dumpExecute($console);
564 self::assertFinished($txt, true);
565 self::assertConfig($data, false, true, false, true);
569 * Test the automatic installation with arguments
570 * @dataProvider dataInstaller
572 public function testWithArguments(array $data)
574 $this->mockConnect(true, 1);
575 $this->mockConnected(true, 1);
576 $this->mockExistsTable('user', false, 1);
577 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
579 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
580 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
582 $console = new AutomaticInstallation($this->consoleArgv);
584 $option = function($var, $cat, $key) use ($data, $console) {
585 if (!empty($data[$cat][$key])) {
586 $console->setOption($var, $data[$cat][$key]);
589 $option('dbhost' , 'database', 'hostname');
590 $option('dbport' , 'database', 'port');
591 $option('dbuser' , 'database', 'username');
592 $option('dbpass' , 'database', 'password');
593 $option('dbdata' , 'database', 'database');
594 $option('url' , 'system' , 'url');
595 $option('phppath' , 'config' , 'php_path');
596 $option('admin' , 'config' , 'admin_email');
597 $option('tz' , 'system' , 'default_timezone');
598 $option('lang' , 'system' , 'language');
599 $option('basepath' , 'system' , 'basepath');
601 $txt = $this->dumpExecute($console);
603 self::assertFinished($txt, true);
604 self::assertConfig($data, true, true, true, true);
608 * Test the automatic installation with a wrong database connection
610 public function testNoDatabaseConnection()
612 $this->mockConnect(false, 1);
614 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
615 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
617 $console = new AutomaticInstallation($this->consoleArgv);
618 $console->setOption('url', 'http://friendica.local');
620 $txt = $this->dumpExecute($console);
622 self::assertStuckDB($txt);
623 self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
625 self::assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
628 public function testGetHelp()
630 // Usable to purposely fail if new commands are added without taking tests into account
632 Installation - Install Friendica automatically
634 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
637 Installs Friendica with data based on the local.config.php file or environment variables
640 Not checking .htaccess/URL-Rewrite during CLI installation.
643 -h|--help|-? Show help information
644 -v Show more debug information.
645 -a All setup checks are required (except .htaccess)
646 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
647 -s|--savedb Save the DB credentials to the file (if environment variables is used)
648 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
649 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
650 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
651 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
652 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
653 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
654 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
655 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
656 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
657 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
659 Environment variables
660 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
661 MYSQL_PORT The port of the mysql/mariadb database
662 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
663 MYSQL_PASSWORD The password of the mysql/mariadb database login
664 MYSQL_DATABASE The name of the mysql/mariadb database
665 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
666 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
667 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
668 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
669 FRIENDICA_TZ The timezone of Friendica
670 FRIENDICA_LANG The langauge of Friendica
673 bin/console autoinstall -f 'input.config.php
674 Installs Friendica with the prepared 'input.config.php' file
676 bin/console autoinstall --savedb
677 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
679 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
680 Installs Friendica with a local mysql database with credentials
684 $console = new AutomaticInstallation($this->consoleArgv);
685 $console->setOption('help', true);
687 $txt = $this->dumpExecute($console);
689 self::assertEquals($theHelp, $txt);