3 * @copyright Copyright (C) 2010-2021, the Friendica project
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() : void
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',
450 'pdo_emulate_prepares' => false,
453 // ****************************************************************
454 // The configuration below will be overruled by the admin panel.
455 // Changes made below will only have an effect if the database does
456 // not contain any configuration for the friendica system.
457 // ****************************************************************
460 'admin_email' => '{$conf('config', 'admin_email')}',
461 'hostname' => '{$conf('config', 'hostname')}',
462 'sitename' => 'Friendica Social Network',
463 'register_policy' => \Friendica\Module\Register::OPEN,
464 'register_text' => '',
467 'basepath' => '{$conf('system', 'basepath')}',
468 'urlpath' => '{$conf('system', 'urlpath')}',
469 'url' => '{$conf('system', 'url')}',
470 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
471 'default_timezone' => '{$conf('system', 'default_timezone')}',
472 'language' => '{$conf('system', 'language')}',
477 vfsStream::newFile('prepared.config.php')
479 ->setContent($config);
481 $console = new AutomaticInstallation($this->consoleArgv);
482 $console->setOption('f', 'prepared.config.php');
484 $txt = $this->dumpExecute($console);
486 self::assertFinished($txt, false, true);
488 self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
489 self::assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
491 self::assertConfig($data, true, false, false);
495 * Test the automatic installation with environment variables
496 * Includes saving the DB credentials to the file
497 * @dataProvider dataInstaller
499 public function testWithEnvironmentAndSave(array $data)
501 $this->mockConnect(true, 1);
502 $this->mockConnected(true, 1);
503 $this->mockExistsTable('user', false, 1);
504 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
506 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
507 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
509 self::assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
510 self::assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
511 self::assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
512 self::assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
513 self::assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
515 self::assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
516 self::assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
517 self::assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
518 self::assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
519 self::assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
520 self::assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
521 self::assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
523 $console = new AutomaticInstallation($this->consoleArgv);
524 $console->setOption('savedb', true);
526 $txt = $this->dumpExecute($console);
528 self::assertFinished($txt, true);
529 self::assertConfig($data, true, true, false, true);
533 * Test the automatic installation with environment variables
534 * Don't save the db credentials to the file
535 * @dataProvider dataInstaller
537 public function testWithEnvironmentWithoutSave(array $data)
539 $this->mockConnect(true, 1);
540 $this->mockConnected(true, 1);
541 $this->mockExistsTable('user', false, 1);
542 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
544 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
545 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
547 self::assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
548 self::assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
549 self::assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
550 self::assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
551 self::assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
553 self::assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
554 self::assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
555 self::assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
556 self::assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
557 self::assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
558 self::assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
559 self::assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
561 $console = new AutomaticInstallation($this->consoleArgv);
563 $txt = $this->dumpExecute($console);
565 self::assertFinished($txt, true);
566 self::assertConfig($data, false, true, false, true);
570 * Test the automatic installation with arguments
571 * @dataProvider dataInstaller
573 public function testWithArguments(array $data)
575 $this->mockConnect(true, 1);
576 $this->mockConnected(true, 1);
577 $this->mockExistsTable('user', false, 1);
578 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
580 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
581 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
583 $console = new AutomaticInstallation($this->consoleArgv);
585 $option = function($var, $cat, $key) use ($data, $console) {
586 if (!empty($data[$cat][$key])) {
587 $console->setOption($var, $data[$cat][$key]);
590 $option('dbhost' , 'database', 'hostname');
591 $option('dbport' , 'database', 'port');
592 $option('dbuser' , 'database', 'username');
593 $option('dbpass' , 'database', 'password');
594 $option('dbdata' , 'database', 'database');
595 $option('url' , 'system' , 'url');
596 $option('phppath' , 'config' , 'php_path');
597 $option('admin' , 'config' , 'admin_email');
598 $option('tz' , 'system' , 'default_timezone');
599 $option('lang' , 'system' , 'language');
600 $option('basepath' , 'system' , 'basepath');
602 $txt = $this->dumpExecute($console);
604 self::assertFinished($txt, true);
605 self::assertConfig($data, true, true, true, true);
609 * Test the automatic installation with a wrong database connection
611 public function testNoDatabaseConnection()
613 $this->mockConnect(false, 1);
615 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
616 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
618 $console = new AutomaticInstallation($this->consoleArgv);
619 $console->setOption('url', 'http://friendica.local');
621 $txt = $this->dumpExecute($console);
623 self::assertStuckDB($txt);
624 self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
626 self::assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
629 public function testGetHelp()
631 // Usable to purposely fail if new commands are added without taking tests into account
633 Installation - Install Friendica automatically
635 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
638 Installs Friendica with data based on the local.config.php file or environment variables
641 Not checking .htaccess/URL-Rewrite during CLI installation.
644 -h|--help|-? Show help information
645 -v Show more debug information.
646 -a All setup checks are required (except .htaccess)
647 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
648 -s|--savedb Save the DB credentials to the file (if environment variables is used)
649 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
650 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
651 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
652 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
653 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
654 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
655 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
656 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
657 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
658 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
660 Environment variables
661 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
662 MYSQL_PORT The port of the mysql/mariadb database
663 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
664 MYSQL_PASSWORD The password of the mysql/mariadb database login
665 MYSQL_DATABASE The name of the mysql/mariadb database
666 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
667 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
668 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
669 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
670 FRIENDICA_TZ The timezone of Friendica
671 FRIENDICA_LANG The langauge of Friendica
674 bin/console autoinstall -f 'input.config.php
675 Installs Friendica with the prepared 'input.config.php' file
677 bin/console autoinstall --savedb
678 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
680 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
681 Installs Friendica with a local mysql database with credentials
685 $console = new AutomaticInstallation($this->consoleArgv);
686 $console->setOption('help', true);
688 $txt = $this->dumpExecute($console);
690 self::assertEquals($theHelp, $txt);