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');
55 $this->mockConfigGet('config', 'php_path', false);
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' => 'Europe/Berlin',
77 '$urlpath' => '/friendica',
78 '$adminmail' => 'admin@friendica.local'
84 private function assertFinished($txt, $withconfig = false, $copyfile = false)
92 Creating config file...
102 Copying config file...
109 Initializing setup...
114 Checking environment...
116 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
127 Inserting data into database...
138 Installation is finished
142 $this->assertEquals($finished, $txt);
145 private function assertStuckDB($txt)
148 Initializing setup...
153 Checking environment...
155 NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
160 Creating config file...
168 Could not connect to database.:
173 $this->assertEquals($finished, $txt);
179 public function testWithConfig()
181 $this->mockConnect(true, 1);
182 $this->mockConnected(true, 1);
183 $this->mockExistsTable('user', false, 1);
184 $this->mockUpdate([false, true, true], null, 1);
189 // Local configuration
191 // If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
192 // documentation of their data type and behavior.
200 'charset' => 'utf8mb4',
203 // ****************************************************************
204 // The configuration below will be overruled by the admin panel.
205 // Changes made below will only have an effect if the database does
206 // not contain any configuration for the friendica system.
207 // ****************************************************************
211 'sitename' => 'Friendica Social Network',
212 'register_policy' => REGISTER_OPEN,
213 'register_text' => '',
216 'default_timezone' => 'UTC',
222 vfsStream::newFile('prepared.config.php')
224 ->setContent($config);
226 $console = new AutomaticInstallation($this->consoleArgv);
227 $console->setOption('f', 'prepared.config.php');
229 $txt = $this->dumpExecute($console);
231 $this->assertFinished($txt, false, true);
233 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
239 public function testWithEnvironmentAndSave()
241 $this->mockConnect(true, 1);
242 $this->mockConnected(true, 1);
243 $this->mockExistsTable('user', false, 1);
244 $this->mockUpdate([false, true, true], null, 1);
246 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
247 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
249 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
250 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
251 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
252 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
254 $console = new AutomaticInstallation($this->consoleArgv);
255 $console->setOption('savedb', true);
257 $txt = $this->dumpExecute($console);
259 $this->assertFinished($txt, true);
265 public function testWithEnvironmentWithoutSave()
267 $this->mockConnect(true, 1);
268 $this->mockConnected(true, 1);
269 $this->mockExistsTable('user', false, 1);
270 $this->mockUpdate([false, true, true], null, 1);
272 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
273 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
275 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
276 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
277 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
278 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
280 $console = new AutomaticInstallation($this->consoleArgv);
282 $txt = $this->dumpExecute($console);
284 $this->assertFinished($txt, true);
290 public function testWithArguments()
292 $this->mockConnect(true, 1);
293 $this->mockConnected(true, 1);
294 $this->mockExistsTable('user', false, 1);
295 $this->mockUpdate([false, true, true], null, 1);
297 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
298 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
300 $console = new AutomaticInstallation($this->consoleArgv);
302 $console->setOption('dbhost', $this->db_host);
303 $console->setOption('dbuser', $this->db_user);
304 if (!empty($this->db_pass)) {
305 $console->setOption('dbpass', $this->db_pass);
307 if (!empty($this->db_port)) {
308 $console->setOption('dbport', $this->db_port);
310 $console->setOption('dbdata', $this->db_data);
312 $console->setOption('admin', 'admin@friendica.local');
313 $console->setOption('tz', 'Europe/Berlin');
314 $console->setOption('lang', 'de');
316 $console->setOption('urlpath', '/friendica');
318 $txt = $this->dumpExecute($console);
320 $this->assertFinished($txt, true);
324 * @runTestsInSeparateProcesses
325 * @preserveGlobalState disabled
327 public function testNoDatabaseConnection()
329 $this->mockConnect(false, 1);
331 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
332 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
334 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
335 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
336 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
337 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
339 $console = new AutomaticInstallation($this->consoleArgv);
341 $txt = $this->dumpExecute($console);
343 $this->assertStuckDB($txt);
346 public function testGetHelp()
348 // Usable to purposely fail if new commands are added without taking tests into account
350 Installation - Install Friendica automatically
352 bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
355 Installs Friendica with data based on the local.config.php file or environment variables
358 Not checking .htaccess/URL-Rewrite during CLI installation.
361 -h|--help|-? Show help information
362 -v Show more debug information.
363 -a All setup checks are required (except .htaccess)
364 -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
365 -s|--savedb Save the DB credentials to the file (if environment variables is used)
366 -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
367 -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
368 -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
369 -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
370 -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
371 -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
372 -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
373 -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
374 -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
375 -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
377 Environment variables
378 MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
379 MYSQL_PORT The port of the mysql/mariadb database
380 MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
381 MYSQL_PASSWORD The password of the mysql/mariadb database login
382 MYSQL_DATABASE The name of the mysql/mariadb database
383 FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
384 FRIENDICA_PHP_PATH The path of the PHP binary
385 FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
386 FRIENDICA_TZ The timezone of Friendica
387 FRIENDICA_LANG The langauge of Friendica
390 bin/console autoinstall -f 'input.config.php
391 Installs Friendica with the prepared 'input.config.php' file
393 bin/console autoinstall --savedb
394 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
396 bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
397 Installs Friendica with a local mysql database with credentials
401 $console = new AutomaticInstallation($this->consoleArgv);
402 $console->setOption('help', true);
404 $txt = $this->dumpExecute($console);
406 $this->assertEquals($txt, $theHelp);