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')));
58 $this->mockApp($this->root, null, true);
60 $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) {
61 if ($key !== 'basepath') {
62 return $this->configCache->set($cat, $key, $value);
68 $this->configMock->shouldReceive('has')->andReturn(true);
69 $this->configMock->shouldReceive('get')->andReturnUsing(function ($cat, $key) {
70 return $this->configCache->get($cat, $key);
72 $this->configMock->shouldReceive('load')->andReturnUsing(function ($config, $overwrite = false) {
73 return $this->configCache->load($config, $overwrite);
76 $this->mode->shouldReceive('isInstall')->andReturn(true);
77 Logger::init(new VoidLogger());
81 * Returns the dataset for each automatic installation test
83 * @return array the dataset
85 public function dataInstaller()
103 'default_timezone' => '',
111 'hostname' => 'testhost',
113 'username' => 'friendica',
114 'password' => 'a password',
115 'database' => 'database',
119 'admin_email' => 'admin@philipp.info',
122 'urlpath' => 'test/it',
123 'default_timezone' => 'en',
124 'language' => 'Europe/Berlin',
131 'hostname' => 'testhost.new.domain',
133 'username' => 'fr"ยง%ica',
134 'password' => '$%\"gse',
139 'admin_email' => 'admin@philipp.info',
142 'urlpath' => 'test/it',
143 'default_timezone' => 'en',
144 'language' => 'Europe/Berlin',
151 private function assertFinished($txt, $withconfig = false, $copyfile = false)
159 Creating config file...
169 Copying config file...
176 Initializing setup...
181 Checking environment...
183 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
194 Inserting data into database...
205 Installation is finished
209 $this->assertEquals($finished, $txt);
212 private function assertStuckDB($txt)
215 Initializing setup...
220 Checking environment...
222 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
227 Creating config file...
235 Could not connect to database.:
240 $this->assertEquals($finished, $txt);
244 * Asserts one config entry
246 * @param string $cat The category to test
247 * @param string $key The key to test
248 * @param null|array $assertion The asserted value (null = empty, or array/string)
249 * @param string $default_value The default value
251 public function assertConfigEntry($cat, $key, $assertion = null, $default_value = null)
253 if (!empty($assertion[$cat][$key])) {
254 $this->assertEquals($assertion[$cat][$key], $this->configCache->get($cat, $key));
255 } elseif (!empty($assertion) && !is_array($assertion)) {
256 $this->assertEquals($assertion, $this->configCache->get($cat, $key));
257 } elseif (!empty($default_value)) {
258 $this->assertEquals($default_value, $this->configCache->get($cat, $key));
260 $this->assertEmpty($this->configCache->get($cat, $key), $this->configCache->get($cat, $key));
265 * Asserts all config entries
267 * @param null|array $assertion The optional assertion array
268 * @param boolean $saveDb True, if the db credentials should get saved to the file
269 * @param boolean $default True, if we use the default values
270 * @param boolean $defaultDb True, if we use the default value for the DB
272 public function assertConfig($assertion = null, $saveDb = false, $default = true, $defaultDb = true)
274 if (!empty($assertion['database']['hostname'])) {
275 $assertion['database']['hostname'] .= (!empty($assertion['database']['port']) ? ':' . $assertion['database']['port'] : '');
278 $this->assertConfigEntry('database', 'hostname', ($saveDb) ? $assertion : null, (!$saveDb || $defaultDb) ? Installer::DEFAULT_HOST : null);
279 $this->assertConfigEntry('database', 'username', ($saveDb) ? $assertion : null);
280 $this->assertConfigEntry('database', 'password', ($saveDb) ? $assertion : null);
281 $this->assertConfigEntry('database', 'database', ($saveDb) ? $assertion : null);
283 $this->assertConfigEntry('config', 'admin_email', $assertion);
284 $this->assertConfigEntry('config', 'php_path', trim(shell_exec('which php')));
286 $this->assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
287 $this->assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
291 * Test the automatic installation without any parameter/setting
293 public function testEmpty()
295 $this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
297 $this->mockConnect(true, 1);
298 $this->mockConnected(true, 1);
299 $this->mockExistsTable('user', false, 1);
300 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
302 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
303 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
305 $console = new AutomaticInstallation($this->consoleArgv);
307 $txt = $this->dumpExecute($console);
309 $this->assertFinished($txt, true, false);
310 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
312 $this->assertConfig();
316 * Test the automatic installation with a prepared config file
317 * @dataProvider dataInstaller
319 public function testWithConfig(array $data)
321 $this->mockConnect(true, 1);
322 $this->mockConnected(true, 1);
323 $this->mockExistsTable('user', false, 1);
324 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
326 $conf = function ($cat, $key) use ($data) {
327 if ($cat == 'database' && $key == 'hostname' && !empty($data['database']['port'])) {
328 return $data[$cat][$key] . ':' . $data['database']['port'];
330 return $data[$cat][$key];
336 // Local configuration
338 // If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
339 // documentation of their data type and behavior.
343 'hostname' => '{$conf('database', 'hostname')}',
344 'username' => '{$conf('database', 'username')}',
345 'password' => '{$conf('database', 'password')}',
346 'database' => '{$conf('database', 'database')}',
347 'charset' => 'utf8mb4',
350 // ****************************************************************
351 // The configuration below will be overruled by the admin panel.
352 // Changes made below will only have an effect if the database does
353 // not contain any configuration for the friendica system.
354 // ****************************************************************
357 'admin_email' => '{$conf('config', 'admin_email')}',
358 'sitename' => 'Friendica Social Network',
359 'register_policy' => \Friendica\Module\Register::OPEN,
360 'register_text' => '',
363 'urlpath' => '{$conf('system', 'urlpath')}',
364 'default_timezone' => '{$conf('system', 'default_timezone')}',
365 'language' => '{$conf('system', 'language')}',
370 vfsStream::newFile('prepared.config.php')
372 ->setContent($config);
374 $console = new AutomaticInstallation($this->consoleArgv);
375 $console->setOption('f', 'prepared.config.php');
377 $txt = $this->dumpExecute($console);
379 $this->assertFinished($txt, false, true);
381 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
382 $this->assertEquals($config, file_get_contents($this->root->getChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')->url()));
384 $this->assertConfig($data, true, false, false);
388 * Test the automatic installation with environment variables
389 * Includes saving the DB credentials to the file
390 * @dataProvider dataInstaller
392 public function testWithEnvironmentAndSave(array $data)
394 $this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
396 $this->mockConnect(true, 1);
397 $this->mockConnected(true, 1);
398 $this->mockExistsTable('user', false, 1);
399 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
401 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
402 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
404 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
405 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
406 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
407 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
408 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
410 $this->assertTrue(putenv('FRIENDICA_URL_PATH=' . $data['system']['urlpath']));
411 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
412 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
413 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
414 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
416 $console = new AutomaticInstallation($this->consoleArgv);
417 $console->setOption('savedb', true);
419 $txt = $this->dumpExecute($console);
421 $this->assertFinished($txt, true);
422 $this->assertConfig($data, true, true, false);
426 * Test the automatic installation with environment variables
427 * Don't save the db credentials to the file
428 * @dataProvider dataInstaller
430 public function testWithEnvironmentWithoutSave(array $data)
432 $this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
434 $this->mockConnect(true, 1);
435 $this->mockConnected(true, 1);
436 $this->mockExistsTable('user', false, 1);
437 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
439 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
440 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
442 $this->assertTrue(putenv('MYSQL_HOST=' . $data['database']['hostname']));
443 $this->assertTrue(putenv('MYSQL_PORT=' . $data['database']['port']));
444 $this->assertTrue(putenv('MYSQL_DATABASE=' . $data['database']['database']));
445 $this->assertTrue(putenv('MYSQL_USERNAME=' . $data['database']['username']));
446 $this->assertTrue(putenv('MYSQL_PASSWORD=' . $data['database']['password']));
448 $this->assertTrue(putenv('FRIENDICA_URL_PATH=' . $data['system']['urlpath']));
449 $this->assertTrue(putenv('FRIENDICA_PHP_PATH=' . $data['config']['php_path']));
450 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=' . $data['config']['admin_email']));
451 $this->assertTrue(putenv('FRIENDICA_TZ=' . $data['system']['default_timezone']));
452 $this->assertTrue(putenv('FRIENDICA_LANG=' . $data['system']['language']));
454 $console = new AutomaticInstallation($this->consoleArgv);
456 $txt = $this->dumpExecute($console);
458 $this->assertFinished($txt, true);
459 $this->assertConfig($data, false, true);
463 * Test the automatic installation with arguments
464 * @dataProvider dataInstaller
466 public function testWithArguments(array $data)
468 $this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
470 $this->mockConnect(true, 1);
471 $this->mockConnected(true, 1);
472 $this->mockExistsTable('user', false, 1);
473 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
475 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
476 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
478 $console = new AutomaticInstallation($this->consoleArgv);
480 $option = function($var, $cat, $key) use ($data, $console) {
481 if (!empty($data[$cat][$key])) {
482 $console->setOption($var, $data[$cat][$key]);
485 $option('dbhost' , 'database', 'hostname');
486 $option('dbport' , 'database', 'port');
487 $option('dbuser' , 'database', 'username');
488 $option('dbpass' , 'database', 'password');
489 $option('dbdata' , 'database', 'database');
490 $option('urlpath' , 'system' , 'urlpath');
491 $option('phppath' , 'config' , 'php_path');
492 $option('admin' , 'config' , 'admin_email');
493 $option('tz' , 'system' , 'default_timezone');
494 $option('lang' , 'system' , 'language');
496 $txt = $this->dumpExecute($console);
498 $this->assertFinished($txt, true);
499 $this->assertConfig($data, true, true, true);
503 * Test the automatic installation with a wrong database connection
505 public function testNoDatabaseConnection()
507 $this->app->shouldReceive('getURLPath')->andReturn('')->atLeast()->once();
508 $this->mockConnect(false, 1);
510 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
511 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
513 $console = new AutomaticInstallation($this->consoleArgv);
515 $txt = $this->dumpExecute($console);
517 $this->assertStuckDB($txt);
518 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
520 $this->assertConfig(null, false, true, false);
523 public function testGetHelp()
525 // Usable to purposely fail if new commands are added without taking tests into account
527 Installation - Install Friendica automatically
529 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
532 Installs Friendica with data based on the local.config.php file or environment variables
535 Not checking .htaccess/URL-Rewrite during CLI installation.
538 -h|--help|-? Show help information
539 -v Show more debug information.
540 -a All setup checks are required (except .htaccess)
541 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
542 -s|--savedb Save the DB credentials to the file (if environment variables is used)
543 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
544 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
545 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
546 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
547 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
548 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
549 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
550 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
551 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
552 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
554 Environment variables
555 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
556 MYSQL_PORT The port of the mysql/mariadb database
557 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
558 MYSQL_PASSWORD The password of the mysql/mariadb database login
559 MYSQL_DATABASE The name of the mysql/mariadb database
560 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
561 FRIENDICA_PHP_PATH The path of the PHP binary
562 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
563 FRIENDICA_TZ The timezone of Friendica
564 FRIENDICA_LANG The langauge of Friendica
567 bin/console autoinstall -f 'input.config.php
568 Installs Friendica with the prepared 'input.config.php' file
570 bin/console autoinstall --savedb
571 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
573 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
574 Installs Friendica with a local mysql database with credentials
578 $console = new AutomaticInstallation($this->consoleArgv);
579 $console->setOption('help', true);
581 $txt = $this->dumpExecute($console);
583 $this->assertEquals($theHelp, $txt);