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\BaseURL;
14 use Friendica\Util\Logger\VoidLogger;
15 use org\bovigo\vfs\vfsStream;
16 use org\bovigo\vfs\vfsStreamFile;
19 * @runTestsInSeparateProcesses
20 * @preserveGlobalState disabled
23 class AutomaticInstallationConsoleTest extends ConsoleTest
27 use DBStructureMockTrait;
28 use RendererMockTrait;
31 * @var vfsStreamFile Assert file without DB credentials
35 * @var vfsStreamFile Assert file with DB credentials
37 private $assertFileDb;
40 * @var ConfigCache The configuration cache to check after each test
44 public function setUp()
48 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
49 $this->root->getChild('config')
50 ->removeChild('local.config.php');
55 $this->configCache = new ConfigCache();
56 $this->configCache->set('system', 'basepath', $this->root->url());
57 $this->configCache->set('config', 'php_path', trim(shell_exec('which php')));
58 $this->configCache->set('system', 'theme', 'smarty3');
60 $this->mockApp($this->root, true);
62 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
63 if ($key !== 'basepath') {
64 return $this->configCache->set($cat, $key, $value);
70 $this->configMock->shouldReceive('has')->andReturn(true);
71 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
72 return $this->configCache->get($cat, $key);
74 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
75 return $this->configCache->load($config, $overwrite);
78 $this->mode->shouldReceive('isInstall')->andReturn(true);
79 Logger::init(new VoidLogger());
83 * Returns the dataset for each automatic installation test
85 * @return array the dataset
87 public function dataInstaller()
101 'hostname' => 'friendica.local',
107 'url' => 'http://friendica.local',
109 'default_timezone' => '',
117 'hostname' => 'testhost',
119 'username' => 'friendica',
120 'password' => 'a password',
121 'database' => 'database',
125 'hostname' => 'friendica.local',
126 'admin_email' => 'admin@philipp.info',
129 'urlpath' => 'test/it',
130 'url' => 'http://friendica.local/test/it',
133 'default_timezone' => 'en',
134 'language' => 'Europe/Berlin',
141 'hostname' => 'testhost.new.domain',
143 'username' => 'fr"ยง%ica',
144 'password' => '$%\"gse',
149 'hostname' => 'friendica.local',
150 'admin_email' => 'admin@philipp.info',
153 'urlpath' => 'test/it',
154 'url' => 'https://friendica.local/test/it',
157 'default_timezone' => 'en',
158 'language' => 'Europe/Berlin',
165 private function assertFinished($txt, $withconfig = false, $copyfile = false)
173 Creating config file...
183 Copying config file...
190 Initializing setup...
195 Checking environment...
197 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
208 Inserting data into database...
219 Installation is finished
223 $this->assertEquals($finished, $txt);
226 private function assertStuckDB($txt)
229 Initializing setup...
234 Checking environment...
236 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
241 Creating config file...
249 Could not connect to database.:
254 $this->assertEquals($finished, $txt);
257 private function assertStuckURL($txt)
260 Initializing setup...
265 Checking environment...
267 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
272 Creating config file...
274 The Friendica URL has to be set during CLI installation.
278 $this->assertEquals($finished, $txt);
282 * Asserts one config entry
284 * @param string $cat The category to test
285 * @param string $key The key to test
286 * @param null|array $assertion The asserted value (null = empty, or array/string)
287 * @param string $default_value The default value
289 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
291 if (!empty($assertion[$cat][$key])) {
292 $this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
293 } elseif (!empty($assertion) && !is_array($assertion)) {
294 $this->assertEquals($assertion, $this->configCache->get($cat, $key));
295 } elseif (!empty($default_value)) {
296 $this->assertEquals($default_value, $this->configCache->get($cat, $key));
298 $this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
303 * Asserts all config entries
305 * @param null|array $assertion The optional assertion array
306 * @param boolean $saveDb True, if the db credentials should get saved to the file
307 * @param boolean $default True, if we use the default values
308 * @param boolean $defaultDb True, if we use the default value for the DB
309 * @param boolean $realBasepath True, if we use the real basepath of the installation, not the mocked one
311 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true, $realBasepath = false)
313 if (!empty($assertion['database']['hostname'])) {
314 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
317 $this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
318 $this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
319 $this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
320 $this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
322 $this->assertConfigEntry('config', 'admin_email', $assertion);
323 $this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
324 $this->assertConfigEntry('config', 'hostname', $assertion);
326 $this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
327 $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
328 $this->assertConfigEntry('system', 'url', $assertion);
329 $this->assertConfigEntry('system', 'urlpath', $assertion);
330 $this->assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? BaseURL::DEFAULT_SSL_SCHEME : null);
331 $this->assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
335 * Test the automatic installation without any parameter/setting
336 * Should stuck because of missing hostname
338 public function testEmpty()
340 $console = new AutomaticInstallation($this->consoleArgv);
342 $txt = $this->dumpExecute($console);
344 $this->assertStuckURL($txt);
348 * Test the automatic installation without any parameter/setting
351 public function testEmptyWithURL()
353 $this->mockConnect(true, 1);
354 $this->mockConnected(true, 1);
355 $this->mockExistsTable('user', false, 1);
356 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
358 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
359 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
361 $console = new AutomaticInstallation($this->consoleArgv);
362 $console->setOption('url', 'http://friendica.local');
364 $txt = $this->dumpExecute($console);
366 $this->assertFinished($txt, true, false);
367 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
369 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlPath' => '']], false, true, true, true);
373 * Test the automatic installation with a prepared config file
374 * @dataProvider dataInstaller
376 public function testWithConfig(array $data)
378 $this->mockConnect(true, 1);
379 $this->mockConnected(true, 1);
380 $this->mockExistsTable('user', false, 1);
381 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
383 $conf = function ($cat, $key) use ($data) {
384 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
385 return $data[$cat][$key] . ':' . $data['database']['port'];
387 return $data[$cat][$key];
393 // Local configuration
395 // If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
396 // documentation of their data type and behavior.
400 'hostname' => '{$conf('database', 'hostname')}',
401 'username' => '{$conf('database', 'username')}',
402 'password' => '{$conf('database', 'password')}',
403 'database' => '{$conf('database', 'database')}',
404 'charset' => 'utf8mb4',
407 // ****************************************************************
408 // The configuration below will be overruled by the admin panel.
409 // Changes made below will only have an effect if the database does
410 // not contain any configuration for the friendica system.
411 // ****************************************************************
414 'admin_email' => '{$conf('config', 'admin_email')}',
415 'hostname' => '{$conf('config', 'hostname')}',
416 'sitename' => 'Friendica Social Network',
417 'register_policy' => \Friendica\Module\Register::OPEN,
418 'register_text' => '',
421 'basepath' => '{$conf('system', 'basepath')}',
422 'urlpath' => '{$conf('system', 'urlpath')}',
423 'url' => '{$conf('system', 'url')}',
424 'ssl_policy' => '{$conf('system', 'ssl_policy')}',
425 'default_timezone' => '{$conf('system', 'default_timezone')}',
426 'language' => '{$conf('system', 'language')}',
431 vfsStream::newFile('prepared.config.php')
433 ->setContent($config);
435 $console = new AutomaticInstallation($this->consoleArgv);
436 $console->setOption('f', 'prepared.config.php');
438 $txt = $this->dumpExecute($console);
440 $this->assertFinished($txt, false, true);
442 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
443 $this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
445 $this->assertConfig($data, true, false, false);
449 * Test the automatic installation with environment variables
450 * Includes saving the DB credentials to the file
451 * @dataProvider dataInstaller
453 public function testWithEnvironmentAndSave(array $data)
455 $this->mockConnect(true, 1);
456 $this->mockConnected(true, 1);
457 $this->mockExistsTable('user', false, 1);
458 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
460 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
461 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
463 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
464 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
465 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
466 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
467 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
469 $this->assertTrue(putenv('FRIENDICA_HOSTNAME=' . $data['config']['hostname']));
470 $this->assertTrue(putenv('FRIENDICA_BASE_PATH=' . $data['system']['basepath']));
471 $this->assertTrue(putenv('FRIENDICA_URL=' . $data['system']['url']));
472 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
473 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
474 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
475 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
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(), false, '', 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=' . $data['system']['url']));
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']));
515 $console = new AutomaticInstallation($this->consoleArgv);
517 $txt = $this->dumpExecute($console);
519 $this->assertFinished($txt, true);
520 $this->assertConfig($data, false, true, false, true);
524 * Test the automatic installation with arguments
525 * @dataProvider dataInstaller
527 public function testWithArguments(array $data)
529 $this->mockConnect(true, 1);
530 $this->mockConnected(true, 1);
531 $this->mockExistsTable('user', false, 1);
532 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
534 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
535 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
537 $console = new AutomaticInstallation($this->consoleArgv);
539 $option = function($var, $cat, $key) use ($data, $console) {
540 if (!empty($data[$cat][$key])) {
541 $console->setOption($var, $data[$cat][$key]);
544 $option('dbhost' , 'database', 'hostname');
545 $option('dbport' , 'database', 'port');
546 $option('dbuser' , 'database', 'username');
547 $option('dbpass' , 'database', 'password');
548 $option('dbdata' , 'database', 'database');
549 $option('url' , 'system' , 'url');
550 $option('phppath' , 'config' , 'php_path');
551 $option('admin' , 'config' , 'admin_email');
552 $option('tz' , 'system' , 'default_timezone');
553 $option('lang' , 'system' , 'language');
554 $option('basepath' , 'system' , 'basepath');
556 $txt = $this->dumpExecute($console);
558 $this->assertFinished($txt, true);
559 $this->assertConfig($data, true, true, true, true);
563 * Test the automatic installation with a wrong database connection
565 public function testNoDatabaseConnection()
567 $this->mockConnect(false, 1);
569 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
570 $this->mockReplaceMacros('testTemplate', \Mockery::any(), false, '', 1);
572 $console = new AutomaticInstallation($this->consoleArgv);
573 $console->setOption('url', 'http://friendica.local');
575 $txt = $this->dumpExecute($console);
577 $this->assertStuckDB($txt);
578 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
580 $this->assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
583 public function testGetHelp()
585 // Usable to purposely fail if new commands are added without taking tests into account
587 Installation - Install Friendica automatically
589 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
592 Installs Friendica with data based on the local.config.php file or environment variables
595 Not checking .htaccess/URL-Rewrite during CLI installation.
598 -h|--help|-? Show help information
599 -v Show more debug information.
600 -a All setup checks are required (except .htaccess)
601 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
602 -s|--savedb Save the DB credentials to the file (if environment variables is used)
603 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
604 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
605 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
606 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
607 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
608 -U|--url <url> The full base URL of Friendica - f.e. 'https://friendica.local/sub' (env FRIENDICA_URL)
609 -B|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
610 -b|--basepath <base_path> The basepath of Friendica(env FRIENDICA_BASE_PATH)
611 -t|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
612 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
614 Environment variables
615 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
616 MYSQL_PORT The port of the mysql/mariadb database
617 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
618 MYSQL_PASSWORD The password of the mysql/mariadb database login
619 MYSQL_DATABASE The name of the mysql/mariadb database
620 FRIENDICA_URL The full base URL of Friendica - f.e. 'https://friendica.local/sub'
621 FRIENDICA_PHP_PATH The path of the PHP binary - leave empty for auto detection
622 FRIENDICA_BASE_PATH The basepath of Friendica - leave empty for auto detection
623 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
624 FRIENDICA_TZ The timezone of Friendica
625 FRIENDICA_LANG The langauge of Friendica
628 bin/console autoinstall -f 'input.config.php
629 Installs Friendica with the prepared 'input.config.php' file
631 bin/console autoinstall --savedb
632 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
634 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
635 Installs Friendica with a local mysql database with credentials
639 $console = new AutomaticInstallation($this->consoleArgv);
640 $console->setOption('help', true);
642 $txt = $this->dumpExecute($console);
644 $this->assertEquals($theHelp, $txt);