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;
38 use Mockery\MockInterface;
39 use org\bovigo\vfs\vfsStream;
40 use org\bovigo\vfs\vfsStreamFile;
42 class AutomaticInstallationConsoleTest extends ConsoleTest
46 use DBStructureMockTrait;
47 use RendererMockTrait;
50 * @var vfsStreamFile Assert file without DB credentials
54 * @var vfsStreamFile Assert file with DB credentials
56 private $assertFileDb;
59 * @var Cache The configuration cache to check after each test
74 * @var Dice|MockInterface
78 public function setUp()
80 $this->markTestSkipped('Needs class \'Installer\' as constructing argument for console tests');
84 $this->setUpVfsDir();;
86 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
87 $this->root->getChild('config')
88 ->removeChild('local.config.php');
90 $this->dice = \Mockery::mock(Dice::class)->makePartial();
92 $l10nMock = \Mockery::mock(L10n::class);
93 $l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
95 $this->dice->shouldReceive('create')
97 ->andReturn($l10nMock);
99 DI::init($this->dice);
101 $this->configCache = new Cache();
102 $this->configCache->set('system', 'basepath', $this->root->url());
103 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
104 $this->configCache->set('system', 'theme', 'smarty3');
106 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
107 if ($key !== 'basepath') {
108 return $this->configCache->set($cat, $key, $value);
114 $this->configMock->shouldReceive('has')->andReturn(true);
115 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
116 return $this->configCache->get($cat, $key);
118 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
119 $this->configCache->load($config, $overwrite);
122 $this->mode->shouldReceive('isInstall')->andReturn(true);
123 Logger::init(new VoidLogger());
127 * Returns the dataset for each automatic installation test
129 * @return array the dataset
131 public function dataInstaller()
145 'hostname' => 'friendica.local',
151 'url' => 'http://friendica.local',
153 'default_timezone' => '',
161 'hostname' => 'testhost',
163 'username' => 'friendica',
164 'password' => 'a password',
165 'database' => 'database',
169 'hostname' => 'friendica.local',
170 'admin_email' => 'admin@philipp.info',
173 'urlpath' => 'test/it',
174 'url' => 'http://friendica.local/test/it',
177 'default_timezone' => 'en',
178 'language' => 'Europe/Berlin',
185 'hostname' => 'testhost.new.domain',
187 'username' => 'fr"§%ica',
188 'password' => '$%\"gse',
193 'hostname' => 'friendica.local',
194 'admin_email' => 'admin@philipp.info',
197 'urlpath' => 'test/it',
198 'url' => 'https://friendica.local/test/it',
201 'default_timezone' => 'en',
202 'language' => 'Europe/Berlin',
209 private function assertFinished($txt, $withconfig = false, $copyfile = false)
217 Creating config file...
227 Copying config file...
234 Initializing setup...
239 Checking environment...
241 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
252 Inserting data into database...
263 Installation is finished
267 $this->assertEquals($finished, $txt);
270 private function assertStuckDB($txt)
273 Initializing setup...
278 Checking environment...
280 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
285 Creating config file...
293 Could not connect to database.:
298 $this->assertEquals($finished, $txt);
301 private function assertStuckURL($txt)
304 Initializing setup...
309 Checking environment...
311 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
316 Creating config file...
318 The Friendica URL has to be set during CLI installation.
322 $this->assertEquals($finished, $txt);
326 * Asserts one config entry
328 * @param string $cat The category to test
329 * @param string $key The key to test
330 * @param null|array $assertion The asserted value (null = empty, or array/string)
331 * @param string $default_value The default value
333 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
335 if (!empty($assertion[$cat][$key])) {
336 $this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
337 } elseif (!empty($assertion) && !is_array($assertion)) {
338 $this->assertEquals($assertion, $this->configCache->get($cat, $key));
339 } elseif (!empty($default_value)) {
340 $this->assertEquals($default_value, $this->configCache->get($cat, $key));
342 $this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
347 * Asserts all config entries
349 * @param null|array $assertion The optional assertion array
350 * @param boolean $saveDb True, if the db credentials should get saved to the file
351 * @param boolean $default True, if we use the default values
352 * @param boolean $defaultDb True, if we use the default value for the DB
353 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
355 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
357 if (!empty($assertion['database']['hostname'])) {
358 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
361 $this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
362 $this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
363 $this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
364 $this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
366 $this->assertConfigEntry('config', 'admin_email', $assertion);
367 $this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
368 $this->assertConfigEntry('config', 'hostname', $assertion);
370 $this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
371 $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
372 $this->assertConfigEntry('system', 'url', $assertion);
373 $this->assertConfigEntry('system', 'urlpath', $assertion);
374 $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? App\BaseURL::DEFAULT_SSL_SCHEME : null);
375 $this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
379 * Test the automatic installation without any parameter/setting
380 * Should stuck because of missing hostname
382 public function testEmpty()
384 $console = new AutomaticInstallation($this->consoleArgv);
386 $txt = $this->dumpExecute($console);
388 $this->assertStuckURL($txt);
392 * Test the automatic installation without any parameter/setting
395 public function testEmptyWithURL()
397 $this->mockConnect(true, 1);
398 $this->mockConnected(true, 1);
399 $this->mockExistsTable('user', false, 1);
400 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
402 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
403 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
405 $console = new AutomaticInstallation($this->consoleArgv);
406 $console->setOption('url', 'http://friendica.local');
408 $txt = $this->dumpExecute($console);
410 $this->assertFinished($txt, true, false);
411 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
413 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
417 * Test the automatic installation with a prepared config file
418 * @dataProvider dataInstaller
420 public function testWithConfig(array $data)
422 $this->mockConnect(true, 1);
423 $this->mockConnected(true, 1);
424 $this->mockExistsTable('user', false, 1);
425 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
427 $conf = function ($cat, $key) use ($data) {
428 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
429 return $data[$cat][$key] . ':' . $data['database']['port'];
431 return $data[$cat][$key];
437 // Local configuration
439 // If you're unsure about what any of the config keys below do, please check the static/defaults.config.php for detailed
440 // documentation of their data type and behavior.
444 'hostname' => '{$conf('database', 'hostname')}',
445 'username' => '{$conf('database', 'username')}',
446 'password' => '{$conf('database', 'password')}',
447 'database' => '{$conf('database', 'database')}',
448 'charset' => 'utf8mb4',
451 // ****************************************************************
452 // The configuration below will be overruled by the admin panel.
453 // Changes made below will only have an effect if the database does
454 // not contain any configuration for the friendica system.
455 // ****************************************************************
458 'admin_email' => '{$conf('config', 'admin_email')}',
459 'hostname' => '{$conf('config', 'hostname')}',
460 'sitename' => 'Friendica Social Network',
461 'register_policy' => \Friendica\Module\Register::OPEN,
462 'register_text' => '',
465 'basepath' => '{$conf('system', 'basepath')}',
466 'urlpath' => '{$conf('system', 'urlpath')}',
467 'url' => '{$conf('system', 'url')}',
468 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
469 'default_timezone' => '{$conf('system', 'default_timezone')}',
470 'language' => '{$conf('system', 'language')}',
475 vfsStream::newFile('prepared.config.php')
477 ->setContent($config);
479 $console = new AutomaticInstallation($this->consoleArgv);
480 $console->setOption('f', 'prepared.config.php');
482 $txt = $this->dumpExecute($console);
484 $this->assertFinished($txt, false, true);
486 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
487 $this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
489 $this->assertConfig($data, true, false, false);
493 * Test the automatic installation with environment variables
494 * Includes saving the DB credentials to the file
495 * @dataProvider dataInstaller
497 public function testWithEnvironmentAndSave(array $data)
499 $this->mockConnect(true, 1);
500 $this->mockConnected(true, 1);
501 $this->mockExistsTable('user', false, 1);
502 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
504 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
505 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
507 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
508 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
509 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
510 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
511 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
513 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
514 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
515 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
516 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
517 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
518 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
519 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
521 $console = new AutomaticInstallation($this->consoleArgv);
522 $console->setOption('savedb', true);
524 $txt = $this->dumpExecute($console);
526 $this->assertFinished($txt, true);
527 $this->assertConfig($data, true, true, false, true);
531 * Test the automatic installation with environment variables
532 * Don't save the db credentials to the file
533 * @dataProvider dataInstaller
535 public function testWithEnvironmentWithoutSave(array $data)
537 $this->mockConnect(true, 1);
538 $this->mockConnected(true, 1);
539 $this->mockExistsTable('user', false, 1);
540 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
542 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
543 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
545 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
546 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
547 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
548 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
549 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
551 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
552 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
553 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
554 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
555 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
556 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
557 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
559 $console = new AutomaticInstallation($this->consoleArgv);
561 $txt = $this->dumpExecute($console);
563 $this->assertFinished($txt, true);
564 $this->assertConfig($data, false, true, false, true);
568 * Test the automatic installation with arguments
569 * @dataProvider dataInstaller
571 public function testWithArguments(array $data)
573 $this->mockConnect(true, 1);
574 $this->mockConnected(true, 1);
575 $this->mockExistsTable('user', false, 1);
576 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
578 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
579 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
581 $console = new AutomaticInstallation($this->consoleArgv);
583 $option = function($var, $cat, $key) use ($data, $console) {
584 if (!empty($data[$cat][$key])) {
585 $console->setOption($var, $data[$cat][$key]);
588 $option('dbhost' , 'database', 'hostname');
589 $option('dbport' , 'database', 'port');
590 $option('dbuser' , 'database', 'username');
591 $option('dbpass' , 'database', 'password');
592 $option('dbdata' , 'database', 'database');
593 $option('url' , 'system' , 'url');
594 $option('phppath' , 'config' , 'php_path');
595 $option('admin' , 'config' , 'admin_email');
596 $option('tz' , 'system' , 'default_timezone');
597 $option('lang' , 'system' , 'language');
598 $option('basepath' , 'system' , 'basepath');
600 $txt = $this->dumpExecute($console);
602 $this->assertFinished($txt, true);
603 $this->assertConfig($data, true, true, true, true);
607 * Test the automatic installation with a wrong database connection
609 public function testNoDatabaseConnection()
611 $this->mockConnect(false, 1);
613 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
614 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
616 $console = new AutomaticInstallation($this->consoleArgv);
617 $console->setOption('url', 'http://friendica.local');
619 $txt = $this->dumpExecute($console);
621 $this->assertStuckDB($txt);
622 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
624 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
627 public function testGetHelp()
629 // Usable to purposely fail if new commands are added without taking tests into account
631 Installation - Install Friendica automatically
633 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
636 Installs Friendica with data based on the local.config.php file or environment variables
639 Not checking .htaccess/URL-Rewrite during CLI installation.
642 -h|--help|-? Show help information
643 -v Show more debug information.
644 -a All setup checks are required (except .htaccess)
645 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
646 -s|--savedb Save the DB credentials to the file (if environment variables is used)
647 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
648 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
649 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
650 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
651 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
652 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
653 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
654 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
655 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
656 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
658 Environment variables
659 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
660 MYSQL_PORT The port of the mysql/mariadb database
661 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
662 MYSQL_PASSWORD The password of the mysql/mariadb database login
663 MYSQL_DATABASE The name of the mysql/mariadb database
664 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
665 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
666 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
667 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
668 FRIENDICA_TZ The timezone of Friendica
669 FRIENDICA_LANG The langauge of Friendica
672 bin/console autoinstall -f 'input.config.php
673 Installs Friendica with the prepared 'input.config.php' file
675 bin/console autoinstall --savedb
676 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
678 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
679 Installs Friendica with a local mysql database with credentials
683 $console = new AutomaticInstallation($this->consoleArgv);
684 $console->setOption('help', true);
686 $txt = $this->dumpExecute($console);
688 $this->assertEquals($theHelp, $txt);