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\RendererMockTrait;
9 use org\bovigo\vfs\vfsStream;
10 use org\bovigo\vfs\vfsStreamFile;
13 * @runTestsInSeparateProcesses
14 * @preserveGlobalState disabled
17 class AutomaticInstallationConsoleTest extends ConsoleTest
20 use DBStructureMockTrait;
21 use RendererMockTrait;
30 * @var vfsStreamFile Assert file without DB credentials
34 * @var vfsStreamFile Assert file with DB credentials
36 private $assertFileDb;
38 public function setUp()
42 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
43 $this->root->getChild('config')
44 ->removeChild('local.ini.php');
47 $this->db_host = getenv('MYSQL_HOST');
48 $this->db_port = (!empty(getenv('MYSQL_PORT'))) ? getenv('MYSQL_PORT') : null;
49 $this->db_data = getenv('MYSQL_DATABASE');
50 $this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
51 $this->db_pass = getenv('MYSQL_PASSWORD');
53 $this->mockConfigGet('config', 'php_path', false);
57 * Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.ini.php
59 * @param bool $withDb if true, DB will get saved too
61 * @return array The arguments to pass to the mock for 'replaceMacros()'
63 private function createArgumentsForMacro($withDb)
66 '$phpath' => trim(shell_exec('which php')),
67 '$dbhost' => (($withDb) ? $this->db_host . (isset($this->db_port) ? ':' . $this->db_port : '') : ''),
68 '$dbuser' => (($withDb) ? $this->db_user : ''),
69 '$dbpass' => (($withDb) ? $this->db_pass : ''),
70 '$dbdata' => (($withDb) ? $this->db_data : ''),
71 '$timezone' => 'Europe/Berlin',
73 '$urlpath' => '/friendica',
74 '$adminmail' => 'admin@friendica.local'
80 private function assertFinished($txt, $withconfig = false, $copyfile = false)
88 Creating config file...
98 Copying config file...
105 Initializing setup...
110 Checking environment...
112 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
123 Inserting data into database...
134 Installation is finished
138 $this->assertEquals($finished, $txt);
141 private function assertStuckDB($txt)
144 Initializing setup...
149 Checking environment...
151 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
156 Creating config file...
164 Could not connect to database.:
169 $this->assertEquals($finished, $txt);
175 public function testWithConfig()
177 $this->mockConnect(true, 1);
178 $this->mockConnected(true, 1);
179 $this->mockExistsTable('user', false, 1);
180 $this->mockUpdate([false, true, true], null, 1);
193 ; ****************************************************************
194 ; The configuration below will be overruled by the admin panel.
195 ; Changes made below will only have an effect if the database does
196 ; not contain any configuration for the friendica system.
197 ; ****************************************************************
202 sitename = Friendica Social Network
204 register_policy = REGISTER_OPEN
208 default_timezone = UTC
216 vfsStream::newFile('prepared.ini.php')
218 ->setContent($config);
220 $console = new AutomaticInstallation($this->consoleArgv);
221 $console->setOption('f', 'prepared.ini.php');
223 $txt = $this->dumpExecute($console);
225 $this->assertFinished($txt, false, true);
227 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
233 public function testWithEnvironmentAndSave()
235 $this->mockConnect(true, 1);
236 $this->mockConnected(true, 1);
237 $this->mockExistsTable('user', false, 1);
238 $this->mockUpdate([false, true, true], null, 1);
240 $this->mockGetMarkupTemplate('local.ini.tpl', 'testTemplate', 1);
241 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
243 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
244 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
245 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
246 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
248 $console = new AutomaticInstallation($this->consoleArgv);
249 $console->setOption('savedb', true);
251 $txt = $this->dumpExecute($console);
253 $this->assertFinished($txt, true);
259 public function testWithEnvironmentWithoutSave()
261 $this->mockConnect(true, 1);
262 $this->mockConnected(true, 1);
263 $this->mockExistsTable('user', false, 1);
264 $this->mockUpdate([false, true, true], null, 1);
266 $this->mockGetMarkupTemplate('local.ini.tpl', 'testTemplate', 1);
267 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
269 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
270 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
271 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
272 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
274 $console = new AutomaticInstallation($this->consoleArgv);
276 $txt = $this->dumpExecute($console);
278 $this->assertFinished($txt, true);
284 public function testWithArguments()
286 $this->mockConnect(true, 1);
287 $this->mockConnected(true, 1);
288 $this->mockExistsTable('user', false, 1);
289 $this->mockUpdate([false, true, true], null, 1);
291 $this->mockGetMarkupTemplate('local.ini.tpl', 'testTemplate', 1);
292 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
294 $console = new AutomaticInstallation($this->consoleArgv);
296 $console->setOption('dbhost', $this->db_host);
297 $console->setOption('dbuser', $this->db_user);
298 if (!empty($this->db_pass)) {
299 $console->setOption('dbpass', $this->db_pass);
301 if (!empty($this->db_port)) {
302 $console->setOption('dbport', $this->db_port);
304 $console->setOption('dbdata', $this->db_data);
306 $console->setOption('admin', 'admin@friendica.local');
307 $console->setOption('tz', 'Europe/Berlin');
308 $console->setOption('lang', 'de');
310 $console->setOption('urlpath', '/friendica');
312 $txt = $this->dumpExecute($console);
314 $this->assertFinished($txt, true);
318 * @runTestsInSeparateProcesses
319 * @preserveGlobalState disabled
321 public function testNoDatabaseConnection()
323 $this->mockConnect(false, 1);
325 $this->mockGetMarkupTemplate('local.ini.tpl', 'testTemplate', 1);
326 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
328 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
329 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
330 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
331 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
333 $console = new AutomaticInstallation($this->consoleArgv);
335 $txt = $this->dumpExecute($console);
337 $this->assertStuckDB($txt);
340 public function testGetHelp()
342 // Usable to purposely fail if new commands are added without taking tests into account
344 Installation - Install Friendica automatically
346 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
349 Installs Friendica with data based on the local.ini.php file or environment variables
352 Not checking .htaccess/URL-Rewrite during CLI installation.
355 -h|--help|-? Show help information
356 -v Show more debug information.
357 -a All setup checks are required (except .htaccess)
358 -f|--file <config> prepared config file (e.g. "config/local.ini.php" itself) which will override every other config option - except the environment variables)
359 -s|--savedb Save the DB credentials to the file (if environment variables is used)
360 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
361 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
362 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
363 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
364 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
365 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
366 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
367 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
368 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
369 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
371 Environment variables
372 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
373 MYSQL_PORT The port of the mysql/mariadb database
374 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
375 MYSQL_PASSWORD The password of the mysql/mariadb database login
376 MYSQL_DATABASE The name of the mysql/mariadb database
377 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
378 FRIENDICA_PHP_PATH The path of the PHP binary
379 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
380 FRIENDICA_TZ The timezone of Friendica
381 FRIENDICA_LANG The langauge of Friendica
384 bin/console autoinstall -f 'input.ini.php
385 Installs Friendica with the prepared 'input.ini.php' file
387 bin/console autoinstall --savedb
388 Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
390 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
391 Installs Friendica with a local mysql database with credentials
395 $console = new AutomaticInstallation($this->consoleArgv);
396 $console->setOption('help', true);
398 $txt = $this->dumpExecute($console);
400 $this->assertEquals($txt, $theHelp);