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\Test\Util\DBAMockTrait;
9 use Friendica\Test\Util\DBStructureMockTrait;
10 use Friendica\Test\Util\L10nMockTrait;
11 use Friendica\Test\Util\RendererMockTrait;
12 use org\bovigo\vfs\vfsStream;
13 use org\bovigo\vfs\vfsStreamFile;
16 * @runTestsInSeparateProcesses
17 * @preserveGlobalState disabled
20 class AutomaticInstallationConsoleTest extends ConsoleTest
24 use DBStructureMockTrait;
25 use RendererMockTrait;
34 * @var vfsStreamFile Assert file without DB credentials
38 * @var vfsStreamFile Assert file with DB credentials
40 private $assertFileDb;
42 public function setUp()
46 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
47 $this->root->getChild('config')
48 ->removeChild('local.config.php');
51 $this->db_host = getenv('MYSQL_HOST');
52 $this->db_port = !empty(getenv('MYSQL_PORT')) ? getenv('MYSQL_PORT') : null;
53 $this->db_data = getenv('MYSQL_DATABASE');
54 $this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
55 $this->db_pass = getenv('MYSQL_PASSWORD');
61 * Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.config.php
63 * @param bool $withDb if true, DB will get saved too
65 * @return array The arguments to pass to the mock for 'replaceMacros()'
67 private function createArgumentsForMacro($withDb)
70 '$phpath' => trim(shell_exec('which php')),
71 '$dbhost' => (($withDb) ? $this->db_host . (isset($this->db_port) ? ':' . $this->db_port : '') : ''),
72 '$dbuser' => (($withDb) ? $this->db_user : ''),
73 '$dbpass' => (($withDb) ? $this->db_pass : ''),
74 '$dbdata' => (($withDb) ? $this->db_data : ''),
75 '$timezone' => Installer::DEFAULT_TZ,
76 '$language' => Installer::DEFAULT_LANG,
77 '$urlpath' => '/friendica',
78 '$basepath' => '/test',
79 '$hostname' => 'friendica.local',
80 '$adminmail' => 'admin@friendica.local'
86 private function assertFinished($txt, $withconfig = false, $copyfile = false)
94 Creating config file...
104 Copying config file...
111 Initializing setup...
116 Checking environment...
118 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
129 Inserting data into database...
140 Installation is finished
144 $this->assertEquals($finished, $txt);
147 private function assertStuckDB($txt)
150 Initializing setup...
155 Checking environment...
157 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
162 Creating config file...
170 Could not connect to database.:
175 $this->assertEquals($finished, $txt);
178 public function dataInstaller()
198 'default_timezone' => '',
207 * Test the automatic installation without any parameter
208 * @dataProvider dataInstaller
210 public function testEmpty(array $data)
212 $configCache = new ConfigCache();
213 $configCache->load($data);
214 $configCache->set('system', 'basepath', $this->root->url());
216 $this->mockApp($this->root, $configCache);
218 $this->mockConnect(true, 1);
219 $this->mockConnected(true, 1);
220 $this->mockExistsTable('user', false, 1);
221 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
223 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
224 $this->mockReplaceMacros('testTemplate', \Mockery::any(), '', 1);
226 $console = new AutomaticInstallation($this->consoleArgv);
228 $txt = $this->dumpExecute($console);
230 $this->assertEquals(Installer::DEFAULT_LANG, $configCache->get('system', 'language'));
231 $this->assertEquals(Installer::DEFAULT_TZ, $configCache->get('system', 'default_timezone'));
232 $this->assertEquals(Installer::DEFAULT_HOST, $configCache->get('database', 'hostname'));
233 $this->assertFinished($txt, true, false);
239 * @dataProvider dataInstaller
241 public function testWithConfig(array $data)
243 $this->mockConnect(true, 1);
244 $this->mockConnected(true, 1);
245 $this->mockExistsTable('user', false, 1);
246 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
251 // Local configuration
253 // If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
254 // documentation of their data type and behavior.
262 'charset' => 'utf8mb4',
265 // ****************************************************************
266 // The configuration below will be overruled by the admin panel.
267 // Changes made below will only have an effect if the database does
268 // not contain any configuration for the friendica system.
269 // ****************************************************************
273 'sitename' => 'Friendica Social Network',
274 'register_policy' => \Friendica\Module\Register::OPEN,
275 'register_text' => '',
278 'default_timezone' => 'UTC',
284 vfsStream::newFile('prepared.config.php')
286 ->setContent($config);
288 $console = new AutomaticInstallation($this->consoleArgv);
289 $console->setOption('f', 'prepared.config.php');
291 $txt = $this->dumpExecute($console);
293 $this->assertFinished($txt, false, true);
295 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
301 public function testWithEnvironmentAndSave()
303 $this->mockConnect(true, 1);
304 $this->mockConnected(true, 1);
305 $this->mockExistsTable('user', false, 1);
306 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
308 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
309 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
311 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
312 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
313 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
314 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
316 $console = new AutomaticInstallation($this->consoleArgv);
317 $console->setOption('savedb', true);
319 $txt = $this->dumpExecute($console);
321 $this->assertFinished($txt, true);
327 public function testWithEnvironmentWithoutSave()
329 $this->mockConnect(true, 1);
330 $this->mockConnected(true, 1);
331 $this->mockExistsTable('user', false, 1);
332 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
334 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
335 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
337 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
338 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
339 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
340 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
342 $console = new AutomaticInstallation($this->consoleArgv);
344 $txt = $this->dumpExecute($console);
346 $this->assertFinished($txt, true);
352 public function testWithArguments()
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', $this->createArgumentsForMacro(true), '', 1);
362 $console = new AutomaticInstallation($this->consoleArgv);
364 $console->setOption('dbhost', $this->db_host);
365 $console->setOption('dbuser', $this->db_user);
366 if (!empty($this->db_pass)) {
367 $console->setOption('dbpass', $this->db_pass);
369 if (!empty($this->db_port)) {
370 $console->setOption('dbport', $this->db_port);
372 $console->setOption('dbdata', $this->db_data);
374 $console->setOption('admin', 'admin@friendica.local');
375 $console->setOption('tz', 'Europe/Berlin');
376 $console->setOption('lang', 'de');
378 $console->setOption('urlpath', '/friendica');
380 $txt = $this->dumpExecute($console);
382 $this->assertFinished($txt, true);
386 * @runTestsInSeparateProcesses
387 * @preserveGlobalState disabled
389 public function testNoDatabaseConnection()
391 $this->mockConnect(false, 1);
393 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
394 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
396 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
397 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
398 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
399 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
401 $console = new AutomaticInstallation($this->consoleArgv);
403 $txt = $this->dumpExecute($console);
405 $this->assertStuckDB($txt);
408 public function testGetHelp()
410 // Usable to purposely fail if new commands are added without taking tests into account
412 Installation - Install Friendica automatically
414 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
417 Installs Friendica with data based on the local.config.php file or environment variables
420 Not checking .htaccess/URL-Rewrite during CLI installation.
423 -h|--help|-? Show help information
424 -v Show more debug information.
425 -a All setup checks are required (except .htaccess)
426 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
427 -s|--savedb Save the DB credentials to the file (if environment variables is used)
428 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
429 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
430 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
431 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
432 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
433 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
434 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
435 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
436 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
437 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
439 Environment variables
440 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
441 MYSQL_PORT The port of the mysql/mariadb database
442 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
443 MYSQL_PASSWORD The password of the mysql/mariadb database login
444 MYSQL_DATABASE The name of the mysql/mariadb database
445 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
446 FRIENDICA_PHP_PATH The path of the PHP binary
447 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
448 FRIENDICA_TZ The timezone of Friendica
449 FRIENDICA_LANG The langauge of Friendica
452 bin/console autoinstall -f 'input.config.php
453 Installs Friendica with the prepared 'input.config.php' file
455 bin/console autoinstall --savedb
456 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
458 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
459 Installs Friendica with a local mysql database with credentials
463 $console = new AutomaticInstallation($this->consoleArgv);
464 $console->setOption('help', true);
466 $txt = $this->dumpExecute($console);
468 $this->assertEquals($txt, $theHelp);