3 namespace Friendica\Test\src\Core\Console;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Console\AutomaticInstallation;
7 use Friendica\Core\Installer;
8 use Friendica\Core\Logger;
9 use Friendica\Test\Util\DBAMockTrait;
10 use Friendica\Test\Util\DBStructureMockTrait;
11 use Friendica\Test\Util\L10nMockTrait;
12 use Friendica\Test\Util\RendererMockTrait;
13 use Friendica\Util\Logger\VoidLogger;
14 use org\bovigo\vfs\vfsStream;
15 use org\bovigo\vfs\vfsStreamFile;
18 * @runTestsInSeparateProcesses
19 * @preserveGlobalState disabled
22 class AutomaticInstallationConsoleTest extends ConsoleTest
26 use DBStructureMockTrait;
27 use RendererMockTrait;
30 * @var vfsStreamFile Assert file without DB credentials
34 * @var vfsStreamFile Assert file with DB credentials
36 private $assertFileDb;
39 * @var ConfigCache The configuration cache to check after each test
43 public function setUp()
47 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
48 $this->root->getChild('config')
49 ->removeChild('local.config.php');
54 $this->configCache = new ConfigCache();
55 $this->configCache->set('system', 'basepath', $this->root->url());
56 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
57 $this->configCache->set('system', 'theme', 'smarty3');
59 $this->mockApp($this->root, true);
61 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
62 if ($key !== 'basepath') {
63 return $this->configCache->set($cat, $key, $value);
69 $this->configMock->shouldReceive('has')->andReturn(true);
70 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
71 return $this->configCache->get($cat, $key);
73 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
74 return $this->configCache->load($config, $overwrite);
77 $this->mode->shouldReceive('isInstall')->andReturn(true);
78 Logger::init(new VoidLogger());
82 * Returns the dataset for each automatic installation test
84 * @return array the dataset
86 public function dataInstaller()
100 'hostname' => 'friendica.local',
106 'url' => 'http://friendica.local',
108 'default_timezone' => '',
116 'hostname' => 'testhost',
118 'username' => 'friendica',
119 'password' => 'a password',
120 'database' => 'database',
124 'hostname' => 'friendica.local',
125 'admin_email' => 'admin@philipp.info',
128 'urlpath' => 'test/it',
129 'url' => 'http://friendica.local/test/it',
132 'default_timezone' => 'en',
133 'language' => 'Europe/Berlin',
140 'hostname' => 'testhost.new.domain',
142 'username' => 'fr"ยง%ica',
143 'password' => '$%\"gse',
148 'hostname' => 'friendica.local',
149 'admin_email' => 'admin@philipp.info',
152 'urlpath' => 'test/it',
153 'url' => 'https://friendica.local/test/it',
156 'default_timezone' => 'en',
157 'language' => 'Europe/Berlin',
164 private function assertFinished($txt, $withconfig = false, $copyfile = false)
172 Creating config file...
182 Copying config file...
189 Initializing setup...
194 Checking environment...
196 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
207 Inserting data into database...
218 Installation is finished
222 $this->assertEquals($finished, $txt);
225 private function assertStuckDB($txt)
228 Initializing setup...
233 Checking environment...
235 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
240 Creating config file...
248 Could not connect to database.:
253 $this->assertEquals($finished, $txt);
256 private function assertStuckHostnam($txt)
259 Initializing setup...
264 Checking environment...
266 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
271 Creating config file...
273 The Friendica hostname has to be set during CLI installation.
277 $this->assertEquals($finished, $txt);
281 * Asserts one config entry
283 * @param string $cat The category to test
284 * @param string $key The key to test
285 * @param null|array $assertion The asserted value (null = empty, or array/string)
286 * @param string $default_value The default value
288 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
290 if (!empty($assertion[$cat][$key])) {
291 $this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
292 } elseif (!empty($assertion) && !is_array($assertion)) {
293 $this->assertEquals($assertion, $this->configCache->get($cat, $key));
294 } elseif (!empty($default_value)) {
295 $this->assertEquals($default_value, $this->configCache->get($cat, $key));
297 $this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
302 * Asserts all config entries
304 * @param null|array $assertion The optional assertion array
305 * @param boolean $saveDb True, if the db credentials should get saved to the file
306 * @param boolean $default True, if we use the default values
307 * @param boolean $defaultDb True, if we use the default value for the DB
308 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
310 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
312 if (!empty($assertion['database']['hostname'])) {
313 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
316 $this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
317 $this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
318 $this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
319 $this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
321 $this->assertConfigEntry('config', 'admin_email', $assertion);
322 $this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
323 $this->assertConfigEntry('config', 'hostname', $assertion);
325 $this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
326 $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
327 $this->assertConfigEntry('system', 'url', $assertion);
328 $this->assertConfigEntry('system', 'urlpath', $assertion);
329 $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? SSL_POLICY_NONE : null);
330 $this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
334 * Test the automatic installation without any parameter/setting
335 * Should stuck because of missing hostname
337 public function testEmpty()
339 $console = new AutomaticInstallation($this->consoleArgv);
341 $txt = $this->dumpExecute($console);
343 $this->assertStuckHostnam($txt);
347 * Test the automatic installation without any parameter/setting
350 public function testEmptyWithHostname()
352 $this->mockConnect(true, 1);
353 $this->mockConnected(true, 1);
354 $this->mockExistsTable('user', false, 1);
355 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
357 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
358 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
360 $console = new AutomaticInstallation($this->consoleArgv);
361 $console->setOption('n', 'friendica.local');
363 $txt = $this->dumpExecute($console);
365 $this->assertFinished($txt, true, false);
366 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
368 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local']], false, true, true, true);
372 * Test the automatic installation with a prepared config file
373 * @dataProvider dataInstaller
375 public function testWithConfig(array $data)
377 $this->mockConnect(true, 1);
378 $this->mockConnected(true, 1);
379 $this->mockExistsTable('user', false, 1);
380 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
382 $conf = function ($cat, $key) use ($data) {
383 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
384 return $data[$cat][$key] . ':' . $data['database']['port'];
386 return $data[$cat][$key];
392 // Local configuration
394 // If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
395 // documentation of their data type and behavior.
399 'hostname' => '{$conf('database', 'hostname')}',
400 'username' => '{$conf('database', 'username')}',
401 'password' => '{$conf('database', 'password')}',
402 'database' => '{$conf('database', 'database')}',
403 'charset' => 'utf8mb4',
406 // ****************************************************************
407 // The configuration below will be overruled by the admin panel.
408 // Changes made below will only have an effect if the database does
409 // not contain any configuration for the friendica system.
410 // ****************************************************************
413 'admin_email' => '{$conf('config', 'admin_email')}',
414 'hostname' => '{$conf('config', 'hostname')}',
415 'sitename' => 'Friendica Social Network',
416 'register_policy' => \Friendica\Module\Register::OPEN,
417 'register_text' => '',
420 'basepath' => '{$conf('system', 'basepath')}',
421 'urlpath' => '{$conf('system', 'urlpath')}',
422 'url' => '{$conf('system', 'url')}',
423 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
424 'default_timezone' => '{$conf('system', 'default_timezone')}',
425 'language' => '{$conf('system', 'language')}',
430 vfsStream::newFile('prepared.config.php')
432 ->setContent($config);
434 $console = new AutomaticInstallation($this->consoleArgv);
435 $console->setOption('f', 'prepared.config.php');
437 $txt = $this->dumpExecute($console);
439 $this->assertFinished($txt, false, true);
441 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
442 $this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
444 $this->assertConfig($data, true, false, false);
448 * Test the automatic installation with environment variables
449 * Includes saving the DB credentials to the file
450 * @dataProvider dataInstaller
452 public function testWithEnvironmentAndSave(array $data)
454 $this->mockConnect(true, 1);
455 $this->mockConnected(true, 1);
456 $this->mockExistsTable('user', false, 1);
457 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
459 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
460 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
462 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
463 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
464 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
465 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
466 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
468 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
469 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
470 $this->assertTrue(putenv('FRIENDICA_URL_PATH=' . $data['system']['urlpath']));
471 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
472 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
473 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
474 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
475 $this->assertTrue(putenv('FRIENDICA_SSL_POLICY=' . $data['system']['ssl_policy']));
477 $console = new AutomaticInstallation($this->consoleArgv);
478 $console->setOption('savedb', true);
480 $txt = $this->dumpExecute($console);
482 $this->assertFinished($txt, true);
483 $this->assertConfig($data, true, true, false, true);
487 * Test the automatic installation with environment variables
488 * Don't save the db credentials to the file
489 * @dataProvider dataInstaller
491 public function testWithEnvironmentWithoutSave(array $data)
493 $this->mockConnect(true, 1);
494 $this->mockConnected(true, 1);
495 $this->mockExistsTable('user', false, 1);
496 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
498 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
499 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
501 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
502 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
503 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
504 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
505 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
507 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
508 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
509 $this->assertTrue(putenv('FRIENDICA_URL_PATH=' . $data['system']['urlpath']));
510 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
511 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
512 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
513 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
514 $this->assertTrue(putenv('FRIENDICA_SSL_POLICY=' . $data['system']['ssl_policy']));
516 $console = new AutomaticInstallation($this->consoleArgv);
518 $txt = $this->dumpExecute($console);
520 $this->assertFinished($txt, true);
521 $this->assertConfig($data, false, true, false, true);
525 * Test the automatic installation with arguments
526 * @dataProvider dataInstaller
528 public function testWithArguments(array $data)
530 $this->mockConnect(true, 1);
531 $this->mockConnected(true, 1);
532 $this->mockExistsTable('user', false, 1);
533 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
535 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
536 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
538 $console = new AutomaticInstallation($this->consoleArgv);
540 $option = function($var, $cat, $key) use ($data, $console) {
541 if (!empty($data[$cat][$key])) {
542 $console->setOption($var, $data[$cat][$key]);
545 $option('dbhost' , 'database', 'hostname');
546 $option('dbport' , 'database', 'port');
547 $option('dbuser' , 'database', 'username');
548 $option('dbpass' , 'database', 'password');
549 $option('dbdata' , 'database', 'database');
550 $option('urlpath' , 'system' , 'urlpath');
551 $option('phppath' , 'config' , 'php_path');
552 $option('admin' , 'config' , 'admin_email');
553 $option('tz' , 'system' , 'default_timezone');
554 $option('lang' , 'system' , 'language');
555 $option('hostname' , 'config' , 'hostname');
556 $option('basepath' , 'system' , 'basepath');
557 $option('sslpolicy' , 'system' , 'ssl_policy');
559 $txt = $this->dumpExecute($console);
561 $this->assertFinished($txt, true);
562 $this->assertConfig($data, true, true, true, true);
566 * Test the automatic installation with a wrong database connection
568 public function testNoDatabaseConnection()
570 $this->mockConnect(false, 1);
572 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
573 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
575 $console = new AutomaticInstallation($this->consoleArgv);
576 $console->setOption('n', 'friendica.local');
578 $txt = $this->dumpExecute($console);
580 $this->assertStuckDB($txt);
581 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
583 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local']], false, true, false, true);
586 public function testGetHelp()
588 // Usable to purposely fail if new commands are added without taking tests into account
590 Installation - Install Friendica automatically
592 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
595 Installs Friendica with data based on the local.config.php file or environment variables
598 Not checking .htaccess/URL-Rewrite during CLI installation.
601 -h|--help|-? Show help information
602 -v Show more debug information.
603 -a All setup checks are required (except .htaccess)
604 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
605 -s|--savedb Save the DB credentials to the file (if environment variables is used)
606 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
607 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
608 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
609 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
610 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
611 -U|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
612 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
613 -b|--basepath <base_path> The basepath of Friendica(env FRIENDICA_BASE_PATH)
614 -S|--sslpolicy <ssl_policy> The SSL policy of Friendica (env FRIENDICA_SSL_POLICY)
615 -n|--hostname <hostname> The hostname of Friendica (env FRIENDICA_PHP_HOSTNAME)
616 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
617 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
619 Environment variables
620 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
621 MYSQL_PORT The port of the mysql/mariadb database
622 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
623 MYSQL_PASSWORD The password of the mysql/mariadb database login
624 MYSQL_DATABASE The name of the mysql/mariadb database
625 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica') - leave empty for auto detection
626 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
627 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
628 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
629 FRIENDICA_SSL_POLICY The SSL policy of Friendica (default is NO SSL)
630 FRIENDICA_HOSTNAME The hostname of Friendica - leave empty for auto detection
631 FRIENDICA_TZ The timezone of Friendica
632 FRIENDICA_LANG The langauge of Friendica
635 bin/console autoinstall -f 'input.config.php
636 Installs Friendica with the prepared 'input.config.php' file
638 bin/console autoinstall --savedb
639 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
641 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
642 Installs Friendica with a local mysql database with credentials
646 $console = new AutomaticInstallation($this->consoleArgv);
647 $console->setOption('help', true);
649 $txt = $this->dumpExecute($console);
651 $this->assertEquals($theHelp, $txt);