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\ValueObject\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\RendererMockTrait;
34 use Friendica\Test\Util\VFSTrait;
35 use Friendica\Util\Logger\VoidLogger;
37 use Mockery\MockInterface;
38 use org\bovigo\vfs\vfsStream;
39 use org\bovigo\vfs\vfsStreamFile;
41 class AutomaticInstallationConsoleTest extends ConsoleTest
44 use RendererMockTrait;
47 * @var vfsStreamFile Assert file without DB credentials
51 * @var vfsStreamFile Assert file with DB credentials
53 private $assertFileDb;
56 * @var \Friendica\Core\Config\ValueObject\Cache The configuration cache to check after each test
71 * @var Dice|MockInterface
75 public function setUp() : void
77 static::markTestSkipped('Needs class \'Installer\' as constructing argument for console tests');
81 $this->setUpVfsDir();;
83 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
84 $this->root->getChild('config')
85 ->removeChild('local.config.php');
87 $this->dice = Mockery::mock(Dice::class)->makePartial();
89 $l10nMock = Mockery::mock(L10n::class);
90 $l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
92 $this->dice->shouldReceive('create')
94 ->andReturn($l10nMock);
96 DI::init($this->dice);
98 $this->configCache = new Cache();
99 $this->configCache->set('system', 'basepath', $this->root->url());
100 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
101 $this->configCache->set('system', 'theme', 'smarty3');
103 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
104 if ($key !== 'basepath') {
105 return $this->configCache->set($cat, $key, $value);
111 $this->configMock->shouldReceive('has')->andReturn(true);
112 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
113 return $this->configCache->get($cat, $key);
115 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
116 $this->configCache->load($config, $overwrite);
119 $this->mode->shouldReceive('isInstall')->andReturn(true);
120 Logger::init(new VoidLogger());
124 * Returns the dataset for each automatic installation test
126 * @return array the dataset
128 public function dataInstaller()
142 'hostname' => 'friendica.local',
148 'url' => 'http://friendica.local',
150 'default_timezone' => '',
158 'hostname' => 'testhost',
160 'username' => 'friendica',
161 'password' => 'a password',
162 'database' => 'database',
166 'hostname' => 'friendica.local',
167 'admin_email' => 'admin@philipp.info',
170 'urlpath' => 'test/it',
171 'url' => 'http://friendica.local/test/it',
174 'default_timezone' => 'en',
175 'language' => 'Europe/Berlin',
182 'hostname' => 'testhost.new.domain',
184 'username' => 'fr"§%ica',
185 'password' => '$%\"gse',
190 'hostname' => 'friendica.local',
191 'admin_email' => 'admin@philipp.info',
194 'urlpath' => 'test/it',
195 'url' => 'https://friendica.local/test/it',
198 'default_timezone' => 'en',
199 'language' => 'Europe/Berlin',
206 private function assertFinished($txt, $withconfig = false, $copyfile = false)
214 Creating config file...
224 Copying config file...
231 Initializing setup...
236 Checking environment...
238 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
249 Inserting data into database...
260 Installation is finished
264 self::assertEquals($finished, $txt);
267 private function assertStuckDB($txt)
270 Initializing setup...
275 Checking environment...
277 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
282 Creating config file...
290 Could not connect to database.:
295 self::assertEquals($finished, $txt);
298 private function assertStuckURL($txt)
301 Initializing setup...
306 Checking environment...
308 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
313 Creating config file...
315 The Friendica URL has to be set during CLI installation.
319 self::assertEquals($finished, $txt);
323 * Asserts one config entry
325 * @param string $cat The category to test
326 * @param string $key The key to test
327 * @param null|array $assertion The asserted value (null = empty, or array/string)
328 * @param string $default_value The default value
330 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
332 if (!empty($assertion[$cat][$key])) {
333 self::assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
334 } elseif (!empty($assertion) && !is_array($assertion)) {
335 self::assertEquals($assertion, $this->configCache->get($cat, $key));
336 } elseif (!empty($default_value)) {
337 self::assertEquals($default_value, $this->configCache->get($cat, $key));
339 self::assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
344 * Asserts all config entries
346 * @param null|array $assertion The optional assertion array
347 * @param boolean $saveDb True, if the db credentials should get saved to the file
348 * @param boolean $default True, if we use the default values
349 * @param boolean $defaultDb True, if we use the default value for the DB
350 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
352 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
354 if (!empty($assertion['database']['hostname'])) {
355 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
358 self::assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
359 self::assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
360 self::assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
361 self::assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
363 self::assertConfigEntry('config', 'admin_email', $assertion);
364 self::assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
365 self::assertConfigEntry('config', 'hostname', $assertion);
367 self::assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
368 self::assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
369 self::assertConfigEntry('system', 'url', $assertion);
370 self::assertConfigEntry('system', 'urlpath', $assertion);
371 self::assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? App\BaseURL::DEFAULT_SSL_SCHEME : null);
372 self::assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
376 * Test the automatic installation without any parameter/setting
377 * Should stuck because of missing hostname
379 public function testEmpty()
381 $console = new AutomaticInstallation($this->consoleArgv);
383 $txt = $this->dumpExecute($console);
385 self::assertStuckURL($txt);
389 * Test the automatic installation without any parameter/setting
392 public function testEmptyWithURL()
394 $this->mockConnect(true, 1);
395 $this->mockConnected(true, 1);
396 $this->mockExistsTable('user', false, 1);
397 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
399 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
400 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
402 $console = new AutomaticInstallation($this->consoleArgv);
403 $console->setOption('url', 'http://friendica.local');
405 $txt = $this->dumpExecute($console);
407 self::assertFinished($txt, true, false);
408 self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
410 self::assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
414 * Test the automatic installation with a prepared config file
415 * @dataProvider dataInstaller
417 public function testWithConfig(array $data)
419 $this->mockConnect(true, 1);
420 $this->mockConnected(true, 1);
421 $this->mockExistsTable('user', false, 1);
422 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
424 $conf = function ($cat, $key) use ($data) {
425 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
426 return $data[$cat][$key] . ':' . $data['database']['port'];
428 return $data[$cat][$key];
434 // Local configuration
436 // If you're unsure about what any of the config keys below do, please check the static/defaults.config.php for detailed
437 // documentation of their data type and behavior.
441 'hostname' => '{$conf('database', 'hostname')}',
442 'username' => '{$conf('database', 'username')}',
443 'password' => '{$conf('database', 'password')}',
444 'database' => '{$conf('database', 'database')}',
445 'charset' => 'utf8mb4',
446 'pdo_emulate_prepares' => false,
449 // ****************************************************************
450 // The configuration below will be overruled by the admin panel.
451 // Changes made below will only have an effect if the database does
452 // not contain any configuration for the friendica system.
453 // ****************************************************************
456 'admin_email' => '{$conf('config', 'admin_email')}',
457 'hostname' => '{$conf('config', 'hostname')}',
458 'sitename' => 'Friendica Social Network',
459 'register_policy' => \Friendica\Module\Register::OPEN,
460 'register_text' => '',
463 'basepath' => '{$conf('system', 'basepath')}',
464 'urlpath' => '{$conf('system', 'urlpath')}',
465 'url' => '{$conf('system', 'url')}',
466 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
467 'default_timezone' => '{$conf('system', 'default_timezone')}',
468 'language' => '{$conf('system', 'language')}',
473 vfsStream::newFile('prepared.config.php')
475 ->setContent($config);
477 $console = new AutomaticInstallation($this->consoleArgv);
478 $console->setOption('f', 'prepared.config.php');
480 $txt = $this->dumpExecute($console);
482 self::assertFinished($txt, false, true);
484 self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
485 self::assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
487 self::assertConfig($data, true, false, false);
491 * Test the automatic installation with environment variables
492 * Includes saving the DB credentials to the file
493 * @dataProvider dataInstaller
495 public function testWithEnvironmentAndSave(array $data)
497 $this->mockConnect(true, 1);
498 $this->mockConnected(true, 1);
499 $this->mockExistsTable('user', false, 1);
500 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
502 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
503 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
505 self::assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
506 self::assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
507 self::assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
508 self::assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
509 self::assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
511 self::assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
512 self::assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
513 self::assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
514 self::assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
515 self::assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
516 self::assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
517 self::assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
519 $console = new AutomaticInstallation($this->consoleArgv);
520 $console->setOption('savedb', true);
522 $txt = $this->dumpExecute($console);
524 self::assertFinished($txt, true);
525 self::assertConfig($data, true, true, false, true);
529 * Test the automatic installation with environment variables
530 * Don't save the db credentials to the file
531 * @dataProvider dataInstaller
533 public function testWithEnvironmentWithoutSave(array $data)
535 $this->mockConnect(true, 1);
536 $this->mockConnected(true, 1);
537 $this->mockExistsTable('user', false, 1);
538 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
540 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
541 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
543 self::assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
544 self::assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
545 self::assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
546 self::assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
547 self::assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
549 self::assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
550 self::assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
551 self::assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
552 self::assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
553 self::assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
554 self::assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
555 self::assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
557 $console = new AutomaticInstallation($this->consoleArgv);
559 $txt = $this->dumpExecute($console);
561 self::assertFinished($txt, true);
562 self::assertConfig($data, false, true, false, true);
566 * Test the automatic installation with arguments
567 * @dataProvider dataInstaller
569 public function testWithArguments(array $data)
571 $this->mockConnect(true, 1);
572 $this->mockConnected(true, 1);
573 $this->mockExistsTable('user', false, 1);
574 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
576 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
577 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
579 $console = new AutomaticInstallation($this->consoleArgv);
581 $option = function($var, $cat, $key) use ($data, $console) {
582 if (!empty($data[$cat][$key])) {
583 $console->setOption($var, $data[$cat][$key]);
586 $option('dbhost' , 'database', 'hostname');
587 $option('dbport' , 'database', 'port');
588 $option('dbuser' , 'database', 'username');
589 $option('dbpass' , 'database', 'password');
590 $option('dbdata' , 'database', 'database');
591 $option('url' , 'system' , 'url');
592 $option('phppath' , 'config' , 'php_path');
593 $option('admin' , 'config' , 'admin_email');
594 $option('tz' , 'system' , 'default_timezone');
595 $option('lang' , 'system' , 'language');
596 $option('basepath' , 'system' , 'basepath');
598 $txt = $this->dumpExecute($console);
600 self::assertFinished($txt, true);
601 self::assertConfig($data, true, true, true, true);
605 * Test the automatic installation with a wrong database connection
607 public function testNoDatabaseConnection()
609 $this->mockConnect(false, 1);
611 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
612 $this->mockReplaceMacros('testTemplate', Mockery::any(), '', 1);
614 $console = new AutomaticInstallation($this->consoleArgv);
615 $console->setOption('url', 'http://friendica.local');
617 $txt = $this->dumpExecute($console);
619 self::assertStuckDB($txt);
620 self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
622 self::assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
625 public function testGetHelp()
627 // Usable to purposely fail if new commands are added without taking tests into account
629 Installation - Install Friendica automatically
631 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
634 Installs Friendica with data based on the local.config.php file or environment variables
637 Not checking .htaccess/URL-Rewrite during CLI installation.
640 -h|--help|-? Show help information
641 -v Show more debug information.
642 -a All setup checks are required (except .htaccess)
643 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
644 -s|--savedb Save the DB credentials to the file (if environment variables is used)
645 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
646 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
647 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
648 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
649 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
650 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
651 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
652 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
653 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
654 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
656 Environment variables
657 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
658 MYSQL_PORT The port of the mysql/mariadb database
659 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
660 MYSQL_PASSWORD The password of the mysql/mariadb database login
661 MYSQL_DATABASE The name of the mysql/mariadb database
662 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
663 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
664 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
665 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
666 FRIENDICA_TZ The timezone of Friendica
667 FRIENDICA_LANG The langauge of Friendica
670 bin/console autoinstall -f 'input.config.php
671 Installs Friendica with the prepared 'input.config.php' file
673 bin/console autoinstall --savedb
674 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
676 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
677 Installs Friendica with a local mysql database with credentials
681 $console = new AutomaticInstallation($this->consoleArgv);
682 $console->setOption('help', true);
684 $txt = $this->dumpExecute($console);
686 self::assertEquals($theHelp, $txt);