3 namespace Friendica\Test\src\Core\Console;
5 use Friendica\Core\Console\AutomaticInstallation;
6 use Friendica\Test\Util\DBAMockTrait;
7 use Friendica\Test\Util\DBStructureMockTrait;
8 use Friendica\Test\Util\L10nMockTrait;
9 use Friendica\Test\Util\RendererMockTrait;
10 use org\bovigo\vfs\vfsStream;
11 use org\bovigo\vfs\vfsStreamFile;
14 * @runTestsInSeparateProcesses
15 * @preserveGlobalState disabled
18 class AutomaticInstallationConsoleTest extends ConsoleTest
22 use DBStructureMockTrait;
23 use RendererMockTrait;
32 * @var vfsStreamFile Assert file without DB credentials
36 * @var vfsStreamFile Assert file with DB credentials
38 private $assertFileDb;
40 public function setUp()
44 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
45 $this->root->getChild('config')
46 ->removeChild('local.config.php');
49 $this->db_host = getenv('MYSQL_HOST');
50 $this->db_port = !empty(getenv('MYSQL_PORT')) ? getenv('MYSQL_PORT') : null;
51 $this->db_data = getenv('MYSQL_DATABASE');
52 $this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
53 $this->db_pass = getenv('MYSQL_PASSWORD');
56 ->shouldReceive('get')
57 ->with('config', 'php_path', NULL)
64 * Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.config.php
66 * @param bool $withDb if true, DB will get saved too
68 * @return array The arguments to pass to the mock for 'replaceMacros()'
70 private function createArgumentsForMacro($withDb)
73 '$phpath' => trim(shell_exec('which php')),
74 '$dbhost' => (($withDb) ? $this->db_host . (isset($this->db_port) ? ':' . $this->db_port : '') : ''),
75 '$dbuser' => (($withDb) ? $this->db_user : ''),
76 '$dbpass' => (($withDb) ? $this->db_pass : ''),
77 '$dbdata' => (($withDb) ? $this->db_data : ''),
78 '$timezone' => 'Europe/Berlin',
80 '$urlpath' => '/friendica',
81 '$adminmail' => 'admin@friendica.local'
87 private function assertFinished($txt, $withconfig = false, $copyfile = false)
95 Creating config file...
105 Copying config file...
112 Initializing setup...
117 Checking environment...
119 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
130 Inserting data into database...
141 Installation is finished
145 $this->assertEquals($finished, $txt);
148 private function assertStuckDB($txt)
151 Initializing setup...
156 Checking environment...
158 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
163 Creating config file...
171 Could not connect to database.:
176 $this->assertEquals($finished, $txt);
182 public function testWithConfig()
184 $this->mockConnect(true, 1);
185 $this->mockConnected(true, 1);
186 $this->mockExistsTable('user', false, 1);
187 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
192 // Local configuration
194 // If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
195 // documentation of their data type and behavior.
203 'charset' => 'utf8mb4',
206 // ****************************************************************
207 // The configuration below will be overruled by the admin panel.
208 // Changes made below will only have an effect if the database does
209 // not contain any configuration for the friendica system.
210 // ****************************************************************
214 'sitename' => 'Friendica Social Network',
215 'register_policy' => \Friendica\Module\Register::OPEN,
216 'register_text' => '',
219 'default_timezone' => 'UTC',
225 vfsStream::newFile('prepared.config.php')
227 ->setContent($config);
229 $console = new AutomaticInstallation($this->consoleArgv);
230 $console->setOption('f', 'prepared.config.php');
232 $txt = $this->dumpExecute($console);
234 $this->assertFinished($txt, false, true);
236 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
242 public function testWithEnvironmentAndSave()
244 $this->mockConnect(true, 1);
245 $this->mockConnected(true, 1);
246 $this->mockExistsTable('user', false, 1);
247 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
249 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
250 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
252 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
253 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
254 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
255 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
257 $console = new AutomaticInstallation($this->consoleArgv);
258 $console->setOption('savedb', true);
260 $txt = $this->dumpExecute($console);
262 $this->assertFinished($txt, true);
268 public function testWithEnvironmentWithoutSave()
270 $this->mockConnect(true, 1);
271 $this->mockConnected(true, 1);
272 $this->mockExistsTable('user', false, 1);
273 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
275 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
276 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
278 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
279 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
280 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
281 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
283 $console = new AutomaticInstallation($this->consoleArgv);
285 $txt = $this->dumpExecute($console);
287 $this->assertFinished($txt, true);
293 public function testWithArguments()
295 $this->mockConnect(true, 1);
296 $this->mockConnected(true, 1);
297 $this->mockExistsTable('user', false, 1);
298 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
300 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
301 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
303 $console = new AutomaticInstallation($this->consoleArgv);
305 $console->setOption('dbhost', $this->db_host);
306 $console->setOption('dbuser', $this->db_user);
307 if (!empty($this->db_pass)) {
308 $console->setOption('dbpass', $this->db_pass);
310 if (!empty($this->db_port)) {
311 $console->setOption('dbport', $this->db_port);
313 $console->setOption('dbdata', $this->db_data);
315 $console->setOption('admin', 'admin@friendica.local');
316 $console->setOption('tz', 'Europe/Berlin');
317 $console->setOption('lang', 'de');
319 $console->setOption('urlpath', '/friendica');
321 $txt = $this->dumpExecute($console);
323 $this->assertFinished($txt, true);
327 * @runTestsInSeparateProcesses
328 * @preserveGlobalState disabled
330 public function testNoDatabaseConnection()
332 $this->mockConnect(false, 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->assertStuckDB($txt);
349 public function testGetHelp()
351 // Usable to purposely fail if new commands are added without taking tests into account
353 Installation - Install Friendica automatically
355 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
358 Installs Friendica with data based on the local.config.php file or environment variables
361 Not checking .htaccess/URL-Rewrite during CLI installation.
364 -h|--help|-? Show help information
365 -v Show more debug information.
366 -a All setup checks are required (except .htaccess)
367 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
368 -s|--savedb Save the DB credentials to the file (if environment variables is used)
369 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
370 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
371 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
372 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
373 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
374 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
375 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
376 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
377 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
378 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
380 Environment variables
381 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
382 MYSQL_PORT The port of the mysql/mariadb database
383 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
384 MYSQL_PASSWORD The password of the mysql/mariadb database login
385 MYSQL_DATABASE The name of the mysql/mariadb database
386 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
387 FRIENDICA_PHP_PATH The path of the PHP binary
388 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
389 FRIENDICA_TZ The timezone of Friendica
390 FRIENDICA_LANG The langauge of Friendica
393 bin/console autoinstall -f 'input.config.php
394 Installs Friendica with the prepared 'input.config.php' file
396 bin/console autoinstall --savedb
397 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
399 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
400 Installs Friendica with a local mysql database with credentials
404 $console = new AutomaticInstallation($this->consoleArgv);
405 $console->setOption('help', true);
407 $txt = $this->dumpExecute($console);
409 $this->assertEquals($txt, $theHelp);