3 namespace Friendica\Test\src\Console;
7 use Friendica\BaseObject;
8 use Friendica\Console\AutomaticInstallation;
9 use Friendica\Core\Config\Cache\ConfigCache;
10 use Friendica\Core\Installer;
11 use Friendica\Core\L10n\L10n;
12 use Friendica\Core\Logger;
13 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\BaseURL;
19 use Friendica\Util\Logger\VoidLogger;
20 use Mockery\MockInterface;
21 use org\bovigo\vfs\vfsStream;
22 use org\bovigo\vfs\vfsStreamFile;
24 class AutomaticInstallationConsoleTest extends ConsoleTest
28 use DBStructureMockTrait;
29 use RendererMockTrait;
32 * @var vfsStreamFile Assert file without DB credentials
36 * @var vfsStreamFile Assert file with DB credentials
38 private $assertFileDb;
41 * @var ConfigCache The configuration cache to check after each test
56 * @var Dice|MockInterface
60 public function setUp()
62 $this->markTestSkipped('Needs class \'Installer\' as constructing argument for console tests');
66 $this->setUpVfsDir();;
68 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
69 $this->root->getChild('config')
70 ->removeChild('local.config.php');
72 $this->dice = \Mockery::mock(Dice::class)->makePartial();
74 $l10nMock = \Mockery::mock(L10n::class);
75 $l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
77 $this->dice->shouldReceive('create')
79 ->andReturn($l10nMock);
81 BaseObject::setDependencyInjection($this->dice);
83 $this->configCache = new ConfigCache();
84 $this->configCache->set('system', 'basepath', $this->root->url());
85 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
86 $this->configCache->set('system', 'theme', 'smarty3');
88 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
89 if ($key !== 'basepath') {
90 return $this->configCache->set($cat, $key, $value);
96 $this->configMock->shouldReceive('has')->andReturn(true);
97 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
98 return $this->configCache->get($cat, $key);
100 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
101 $this->configCache->load($config, $overwrite);
104 $this->mode->shouldReceive('isInstall')->andReturn(true);
105 Logger::init(new VoidLogger());
109 * Returns the dataset for each automatic installation test
111 * @return array the dataset
113 public function dataInstaller()
127 'hostname' => 'friendica.local',
133 'url' => 'http://friendica.local',
135 'default_timezone' => '',
143 'hostname' => 'testhost',
145 'username' => 'friendica',
146 'password' => 'a password',
147 'database' => 'database',
151 'hostname' => 'friendica.local',
152 'admin_email' => 'admin@philipp.info',
155 'urlpath' => 'test/it',
156 'url' => 'http://friendica.local/test/it',
159 'default_timezone' => 'en',
160 'language' => 'Europe/Berlin',
167 'hostname' => 'testhost.new.domain',
169 'username' => 'fr"ยง%ica',
170 'password' => '$%\"gse',
175 'hostname' => 'friendica.local',
176 'admin_email' => 'admin@philipp.info',
179 'urlpath' => 'test/it',
180 'url' => 'https://friendica.local/test/it',
183 'default_timezone' => 'en',
184 'language' => 'Europe/Berlin',
191 private function assertFinished($txt, $withconfig = false, $copyfile = false)
199 Creating config file...
209 Copying config file...
216 Initializing setup...
221 Checking environment...
223 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
234 Inserting data into database...
245 Installation is finished
249 $this->assertEquals($finished, $txt);
252 private function assertStuckDB($txt)
255 Initializing setup...
260 Checking environment...
262 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
267 Creating config file...
275 Could not connect to database.:
280 $this->assertEquals($finished, $txt);
283 private function assertStuckURL($txt)
286 Initializing setup...
291 Checking environment...
293 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
298 Creating config file...
300 The Friendica URL has to be set during CLI installation.
304 $this->assertEquals($finished, $txt);
308 * Asserts one config entry
310 * @param string $cat The category to test
311 * @param string $key The key to test
312 * @param null|array $assertion The asserted value (null = empty, or array/string)
313 * @param string $default_value The default value
315 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
317 if (!empty($assertion[$cat][$key])) {
318 $this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
319 } elseif (!empty($assertion) && !is_array($assertion)) {
320 $this->assertEquals($assertion, $this->configCache->get($cat, $key));
321 } elseif (!empty($default_value)) {
322 $this->assertEquals($default_value, $this->configCache->get($cat, $key));
324 $this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
329 * Asserts all config entries
331 * @param null|array $assertion The optional assertion array
332 * @param boolean $saveDb True, if the db credentials should get saved to the file
333 * @param boolean $default True, if we use the default values
334 * @param boolean $defaultDb True, if we use the default value for the DB
335 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
337 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
339 if (!empty($assertion['database']['hostname'])) {
340 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
343 $this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
344 $this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
345 $this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
346 $this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
348 $this->assertConfigEntry('config', 'admin_email', $assertion);
349 $this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
350 $this->assertConfigEntry('config', 'hostname', $assertion);
352 $this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
353 $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
354 $this->assertConfigEntry('system', 'url', $assertion);
355 $this->assertConfigEntry('system', 'urlpath', $assertion);
356 $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? BaseURL::DEFAULT_SSL_SCHEME : null);
357 $this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
361 * Test the automatic installation without any parameter/setting
362 * Should stuck because of missing hostname
364 public function testEmpty()
366 $console = new AutomaticInstallation($this->consoleArgv);
368 $txt = $this->dumpExecute($console);
370 $this->assertStuckURL($txt);
374 * Test the automatic installation without any parameter/setting
377 public function testEmptyWithURL()
379 $this->mockConnect(true, 1);
380 $this->mockConnected(true, 1);
381 $this->mockExistsTable('user', false, 1);
382 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
384 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
385 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
387 $console = new AutomaticInstallation($this->consoleArgv);
388 $console->setOption('url', 'http://friendica.local');
390 $txt = $this->dumpExecute($console);
392 $this->assertFinished($txt, true, false);
393 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
395 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
399 * Test the automatic installation with a prepared config file
400 * @dataProvider dataInstaller
402 public function testWithConfig(array $data)
404 $this->mockConnect(true, 1);
405 $this->mockConnected(true, 1);
406 $this->mockExistsTable('user', false, 1);
407 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
409 $conf = function ($cat, $key) use ($data) {
410 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
411 return $data[$cat][$key] . ':' . $data['database']['port'];
413 return $data[$cat][$key];
419 // Local configuration
421 // If you're unsure about what any of the config keys below do, please check the static/defaults.config.php for detailed
422 // documentation of their data type and behavior.
426 'hostname' => '{$conf('database', 'hostname')}',
427 'username' => '{$conf('database', 'username')}',
428 'password' => '{$conf('database', 'password')}',
429 'database' => '{$conf('database', 'database')}',
430 'charset' => 'utf8mb4',
433 // ****************************************************************
434 // The configuration below will be overruled by the admin panel.
435 // Changes made below will only have an effect if the database does
436 // not contain any configuration for the friendica system.
437 // ****************************************************************
440 'admin_email' => '{$conf('config', 'admin_email')}',
441 'hostname' => '{$conf('config', 'hostname')}',
442 'sitename' => 'Friendica Social Network',
443 'register_policy' => \Friendica\Module\Register::OPEN,
444 'register_text' => '',
447 'basepath' => '{$conf('system', 'basepath')}',
448 'urlpath' => '{$conf('system', 'urlpath')}',
449 'url' => '{$conf('system', 'url')}',
450 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
451 'default_timezone' => '{$conf('system', 'default_timezone')}',
452 'language' => '{$conf('system', 'language')}',
457 vfsStream::newFile('prepared.config.php')
459 ->setContent($config);
461 $console = new AutomaticInstallation($this->consoleArgv);
462 $console->setOption('f', 'prepared.config.php');
464 $txt = $this->dumpExecute($console);
466 $this->assertFinished($txt, false, true);
468 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
469 $this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
471 $this->assertConfig($data, true, false, false);
475 * Test the automatic installation with environment variables
476 * Includes saving the DB credentials to the file
477 * @dataProvider dataInstaller
479 public function testWithEnvironmentAndSave(array $data)
481 $this->mockConnect(true, 1);
482 $this->mockConnected(true, 1);
483 $this->mockExistsTable('user', false, 1);
484 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
486 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
487 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
489 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
490 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
491 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
492 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
493 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
495 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
496 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
497 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
498 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
499 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
500 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
501 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
503 $console = new AutomaticInstallation($this->consoleArgv);
504 $console->setOption('savedb', true);
506 $txt = $this->dumpExecute($console);
508 $this->assertFinished($txt, true);
509 $this->assertConfig($data, true, true, false, true);
513 * Test the automatic installation with environment variables
514 * Don't save the db credentials to the file
515 * @dataProvider dataInstaller
517 public function testWithEnvironmentWithoutSave(array $data)
519 $this->mockConnect(true, 1);
520 $this->mockConnected(true, 1);
521 $this->mockExistsTable('user', false, 1);
522 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
524 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
525 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
527 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
528 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
529 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
530 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
531 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
533 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
534 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
535 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
536 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
537 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
538 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
539 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
541 $console = new AutomaticInstallation($this->consoleArgv);
543 $txt = $this->dumpExecute($console);
545 $this->assertFinished($txt, true);
546 $this->assertConfig($data, false, true, false, true);
550 * Test the automatic installation with arguments
551 * @dataProvider dataInstaller
553 public function testWithArguments(array $data)
555 $this->mockConnect(true, 1);
556 $this->mockConnected(true, 1);
557 $this->mockExistsTable('user', false, 1);
558 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
560 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
561 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
563 $console = new AutomaticInstallation($this->consoleArgv);
565 $option = function($var, $cat, $key) use ($data, $console) {
566 if (!empty($data[$cat][$key])) {
567 $console->setOption($var, $data[$cat][$key]);
570 $option('dbhost' , 'database', 'hostname');
571 $option('dbport' , 'database', 'port');
572 $option('dbuser' , 'database', 'username');
573 $option('dbpass' , 'database', 'password');
574 $option('dbdata' , 'database', 'database');
575 $option('url' , 'system' , 'url');
576 $option('phppath' , 'config' , 'php_path');
577 $option('admin' , 'config' , 'admin_email');
578 $option('tz' , 'system' , 'default_timezone');
579 $option('lang' , 'system' , 'language');
580 $option('basepath' , 'system' , 'basepath');
582 $txt = $this->dumpExecute($console);
584 $this->assertFinished($txt, true);
585 $this->assertConfig($data, true, true, true, true);
589 * Test the automatic installation with a wrong database connection
591 public function testNoDatabaseConnection()
593 $this->mockConnect(false, 1);
595 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
596 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
598 $console = new AutomaticInstallation($this->consoleArgv);
599 $console->setOption('url', 'http://friendica.local');
601 $txt = $this->dumpExecute($console);
603 $this->assertStuckDB($txt);
604 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
606 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
609 public function testGetHelp()
611 // Usable to purposely fail if new commands are added without taking tests into account
613 Installation - Install Friendica automatically
615 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
618 Installs Friendica with data based on the local.config.php file or environment variables
621 Not checking .htaccess/URL-Rewrite during CLI installation.
624 -h|--help|-? Show help information
625 -v Show more debug information.
626 -a All setup checks are required (except .htaccess)
627 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
628 -s|--savedb Save the DB credentials to the file (if environment variables is used)
629 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
630 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
631 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
632 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
633 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
634 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
635 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
636 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
637 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
638 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
640 Environment variables
641 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
642 MYSQL_PORT The port of the mysql/mariadb database
643 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
644 MYSQL_PASSWORD The password of the mysql/mariadb database login
645 MYSQL_DATABASE The name of the mysql/mariadb database
646 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
647 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
648 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
649 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
650 FRIENDICA_TZ The timezone of Friendica
651 FRIENDICA_LANG The langauge of Friendica
654 bin/console autoinstall -f 'input.config.php
655 Installs Friendica with the prepared 'input.config.php' file
657 bin/console autoinstall --savedb
658 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
660 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
661 Installs Friendica with a local mysql database with credentials
665 $console = new AutomaticInstallation($this->consoleArgv);
666 $console->setOption('help', true);
668 $txt = $this->dumpExecute($console);
670 $this->assertEquals($theHelp, $txt);