3 namespace Friendica\Test\src\Console;
5 use Friendica\Console\AutomaticInstallation;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Core\Installer;
8 use Friendica\Core\L10n\L10n;
9 use Friendica\Core\Logger;
10 use Friendica\Test\Util\DBAMockTrait;
11 use Friendica\Test\Util\DBStructureMockTrait;
12 use Friendica\Test\Util\RendererMockTrait;
13 use Friendica\Util\BaseURL;
14 use Friendica\Util\Logger\VoidLogger;
15 use org\bovigo\vfs\vfsStream;
16 use org\bovigo\vfs\vfsStreamFile;
21 class AutomaticInstallationConsoleTest extends ConsoleTest
24 use DBStructureMockTrait;
25 use RendererMockTrait;
28 * @var vfsStreamFile Assert file without DB credentials
32 * @var vfsStreamFile Assert file with DB credentials
34 private $assertFileDb;
37 * @var ConfigCache The configuration cache to check after each test
41 public function setUp()
43 $this->markTestSkipped('Needs class \'Installer\' as constructing argument for console tests');
47 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
48 $this->root->getChild('config')
49 ->removeChild('local.config.php');
52 $l10nMock = \Mockery::mock(L10n::class);
53 $l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
54 \Friendica\Core\L10n::init($l10nMock);
56 $this->configCache = new ConfigCache();
57 $this->configCache->set('system', 'basepath', $this->root->url());
58 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
59 $this->configCache->set('system', 'theme', 'smarty3');
61 $this->mockApp($this->root, true);
63 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
64 if ($key !== 'basepath') {
65 return $this->configCache->set($cat, $key, $value);
71 $this->configMock->shouldReceive('has')->andReturn(true);
72 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
73 return $this->configCache->get($cat, $key);
75 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
76 $this->configCache->load($config, $overwrite);
79 $this->mode->shouldReceive('isInstall')->andReturn(true);
80 Logger::init(new VoidLogger());
84 * Returns the dataset for each automatic installation test
86 * @return array the dataset
88 public function dataInstaller()
102 'hostname' => 'friendica.local',
108 'url' => 'http://friendica.local',
110 'default_timezone' => '',
118 'hostname' => 'testhost',
120 'username' => 'friendica',
121 'password' => 'a password',
122 'database' => 'database',
126 'hostname' => 'friendica.local',
127 'admin_email' => 'admin@philipp.info',
130 'urlpath' => 'test/it',
131 'url' => 'http://friendica.local/test/it',
134 'default_timezone' => 'en',
135 'language' => 'Europe/Berlin',
142 'hostname' => 'testhost.new.domain',
144 'username' => 'fr"ยง%ica',
145 'password' => '$%\"gse',
150 'hostname' => 'friendica.local',
151 'admin_email' => 'admin@philipp.info',
154 'urlpath' => 'test/it',
155 'url' => 'https://friendica.local/test/it',
158 'default_timezone' => 'en',
159 'language' => 'Europe/Berlin',
166 private function assertFinished($txt, $withconfig = false, $copyfile = false)
174 Creating config file...
184 Copying config file...
191 Initializing setup...
196 Checking environment...
198 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
209 Inserting data into database...
220 Installation is finished
224 $this->assertEquals($finished, $txt);
227 private function assertStuckDB($txt)
230 Initializing setup...
235 Checking environment...
237 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
242 Creating config file...
250 Could not connect to database.:
255 $this->assertEquals($finished, $txt);
258 private function assertStuckURL($txt)
261 Initializing setup...
266 Checking environment...
268 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
273 Creating config file...
275 The Friendica URL has to be set during CLI installation.
279 $this->assertEquals($finished, $txt);
283 * Asserts one config entry
285 * @param string $cat The category to test
286 * @param string $key The key to test
287 * @param null|array $assertion The asserted value (null = empty, or array/string)
288 * @param string $default_value The default value
290 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
292 if (!empty($assertion[$cat][$key])) {
293 $this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
294 } elseif (!empty($assertion) && !is_array($assertion)) {
295 $this->assertEquals($assertion, $this->configCache->get($cat, $key));
296 } elseif (!empty($default_value)) {
297 $this->assertEquals($default_value, $this->configCache->get($cat, $key));
299 $this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
304 * Asserts all config entries
306 * @param null|array $assertion The optional assertion array
307 * @param boolean $saveDb True, if the db credentials should get saved to the file
308 * @param boolean $default True, if we use the default values
309 * @param boolean $defaultDb True, if we use the default value for the DB
310 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
312 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
314 if (!empty($assertion['database']['hostname'])) {
315 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
318 $this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
319 $this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
320 $this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
321 $this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
323 $this->assertConfigEntry('config', 'admin_email', $assertion);
324 $this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
325 $this->assertConfigEntry('config', 'hostname', $assertion);
327 $this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
328 $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
329 $this->assertConfigEntry('system', 'url', $assertion);
330 $this->assertConfigEntry('system', 'urlpath', $assertion);
331 $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? BaseURL::DEFAULT_SSL_SCHEME : null);
332 $this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
336 * Test the automatic installation without any parameter/setting
337 * Should stuck because of missing hostname
339 public function testEmpty()
341 $console = new AutomaticInstallation($this->consoleArgv);
343 $txt = $this->dumpExecute($console);
345 $this->assertStuckURL($txt);
349 * Test the automatic installation without any parameter/setting
352 public function testEmptyWithURL()
354 $this->mockConnect(true, 1);
355 $this->mockConnected(true, 1);
356 $this->mockExistsTable('user', false, 1);
357 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
359 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
360 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
362 $console = new AutomaticInstallation($this->consoleArgv);
363 $console->setOption('url', 'http://friendica.local');
365 $txt = $this->dumpExecute($console);
367 $this->assertFinished($txt, true, false);
368 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
370 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
374 * Test the automatic installation with a prepared config file
375 * @dataProvider dataInstaller
377 public function testWithConfig(array $data)
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 $conf = function ($cat, $key) use ($data) {
385 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
386 return $data[$cat][$key] . ':' . $data['database']['port'];
388 return $data[$cat][$key];
394 // Local configuration
396 // If you're unsure about what any of the config keys below do, please check the static/defaults.config.php for detailed
397 // documentation of their data type and behavior.
401 'hostname' => '{$conf('database', 'hostname')}',
402 'username' => '{$conf('database', 'username')}',
403 'password' => '{$conf('database', 'password')}',
404 'database' => '{$conf('database', 'database')}',
405 'charset' => 'utf8mb4',
408 // ****************************************************************
409 // The configuration below will be overruled by the admin panel.
410 // Changes made below will only have an effect if the database does
411 // not contain any configuration for the friendica system.
412 // ****************************************************************
415 'admin_email' => '{$conf('config', 'admin_email')}',
416 'hostname' => '{$conf('config', 'hostname')}',
417 'sitename' => 'Friendica Social Network',
418 'register_policy' => \Friendica\Module\Register::OPEN,
419 'register_text' => '',
422 'basepath' => '{$conf('system', 'basepath')}',
423 'urlpath' => '{$conf('system', 'urlpath')}',
424 'url' => '{$conf('system', 'url')}',
425 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
426 'default_timezone' => '{$conf('system', 'default_timezone')}',
427 'language' => '{$conf('system', 'language')}',
432 vfsStream::newFile('prepared.config.php')
434 ->setContent($config);
436 $console = new AutomaticInstallation($this->consoleArgv);
437 $console->setOption('f', 'prepared.config.php');
439 $txt = $this->dumpExecute($console);
441 $this->assertFinished($txt, false, true);
443 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
444 $this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
446 $this->assertConfig($data, true, false, false);
450 * Test the automatic installation with environment variables
451 * Includes saving the DB credentials to the file
452 * @dataProvider dataInstaller
454 public function testWithEnvironmentAndSave(array $data)
456 $this->mockConnect(true, 1);
457 $this->mockConnected(true, 1);
458 $this->mockExistsTable('user', false, 1);
459 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
461 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
462 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
464 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
465 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
466 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
467 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
468 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
470 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
471 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
472 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
473 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
474 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
475 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
476 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
478 $console = new AutomaticInstallation($this->consoleArgv);
479 $console->setOption('savedb', true);
481 $txt = $this->dumpExecute($console);
483 $this->assertFinished($txt, true);
484 $this->assertConfig($data, true, true, false, true);
488 * Test the automatic installation with environment variables
489 * Don't save the db credentials to the file
490 * @dataProvider dataInstaller
492 public function testWithEnvironmentWithoutSave(array $data)
494 $this->mockConnect(true, 1);
495 $this->mockConnected(true, 1);
496 $this->mockExistsTable('user', false, 1);
497 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
499 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
500 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
502 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
503 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
504 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
505 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
506 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
508 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
509 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
510 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
511 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
512 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
513 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
514 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
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('url' , 'system' , 'url');
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('basepath' , 'system' , 'basepath');
557 $txt = $this->dumpExecute($console);
559 $this->assertFinished($txt, true);
560 $this->assertConfig($data, true, true, true, true);
564 * Test the automatic installation with a wrong database connection
566 public function testNoDatabaseConnection()
568 $this->mockConnect(false, 1);
570 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
571 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
573 $console = new AutomaticInstallation($this->consoleArgv);
574 $console->setOption('url', 'http://friendica.local');
576 $txt = $this->dumpExecute($console);
578 $this->assertStuckDB($txt);
579 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
581 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
584 public function testGetHelp()
586 // Usable to purposely fail if new commands are added without taking tests into account
588 Installation - Install Friendica automatically
590 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
593 Installs Friendica with data based on the local.config.php file or environment variables
596 Not checking .htaccess/URL-Rewrite during CLI installation.
599 -h|--help|-? Show help information
600 -v Show more debug information.
601 -a All setup checks are required (except .htaccess)
602 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
603 -s|--savedb Save the DB credentials to the file (if environment variables is used)
604 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
605 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
606 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
607 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
608 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
609 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
610 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
611 -b|--basepath <base_path> The basepath of Friendica (env FRIENDICA_BASE_PATH)
612 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
613 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
615 Environment variables
616 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
617 MYSQL_PORT The port of the mysql/mariadb database
618 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
619 MYSQL_PASSWORD The password of the mysql/mariadb database login
620 MYSQL_DATABASE The name of the mysql/mariadb database
621 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
622 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
623 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
624 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
625 FRIENDICA_TZ The timezone of Friendica
626 FRIENDICA_LANG The langauge of Friendica
629 bin/console autoinstall -f 'input.config.php
630 Installs Friendica with the prepared 'input.config.php' file
632 bin/console autoinstall --savedb
633 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
635 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
636 Installs Friendica with a local mysql database with credentials
640 $console = new AutomaticInstallation($this->consoleArgv);
641 $console->setOption('help', true);
643 $txt = $this->dumpExecute($console);
645 $this->assertEquals($theHelp, $txt);