]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Console/AutomaticInstallationConsoleTest.php
Replace REGISTER_* constants by Module\Register::* ones
[friendica.git] / tests / src / Core / Console / AutomaticInstallationConsoleTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Console;
4
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;
12
13 /**
14  * @runTestsInSeparateProcesses
15  * @preserveGlobalState disabled
16  * @requires PHP 7.0
17  */
18 class AutomaticInstallationConsoleTest extends ConsoleTest
19 {
20         use L10nMockTrait;
21         use DBAMockTrait;
22         use DBStructureMockTrait;
23         use RendererMockTrait;
24
25         private $db_host;
26         private $db_port;
27         private $db_data;
28         private $db_user;
29         private $db_pass;
30
31         /**
32          * @var vfsStreamFile Assert file without DB credentials
33          */
34         private $assertFile;
35         /**
36          * @var vfsStreamFile Assert file with DB credentials
37          */
38         private $assertFileDb;
39
40         public function setUp()
41         {
42                 parent::setUp();
43
44                 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
45                         $this->root->getChild('config')
46                                 ->removeChild('local.config.php');
47                 }
48
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');
54
55                 $this->mockConfigGet('config', 'php_path', false);
56
57                 $this->mockL10nT();
58         }
59
60         /**
61          * Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.config.php
62          *
63          * @param bool $withDb if true, DB will get saved too
64          *
65          * @return array The arguments to pass to the mock for 'replaceMacros()'
66          */
67         private function createArgumentsForMacro($withDb)
68         {
69                 $args = [
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',
76                         '$language' => 'de',
77                         '$urlpath' => '/friendica',
78                         '$adminmail' => 'admin@friendica.local'
79                 ];
80
81                 return $args;
82         }
83
84         private function assertFinished($txt, $withconfig = false, $copyfile = false)
85         {
86                 $cfg = '';
87
88                 if ($withconfig) {
89                         $cfg = <<<CFG
90
91
92 Creating config file...
93
94  Complete!
95 CFG;
96                 }
97
98                 if ($copyfile) {
99                         $cfg = <<<CFG
100
101
102 Copying config file...
103
104  Complete!
105 CFG;
106                 }
107
108                 $finished = <<<FIN
109 Initializing setup...
110
111  Complete!
112
113
114 Checking environment...
115
116  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
117
118  Complete!
119 {$cfg}
120
121
122 Checking database...
123
124  Complete!
125
126
127 Inserting data into database...
128
129  Complete!
130
131
132 Installing theme
133
134  Complete
135
136
137
138 Installation is finished
139
140
141 FIN;
142                 $this->assertEquals($finished, $txt);
143         }
144
145         private function assertStuckDB($txt)
146         {
147                 $finished = <<<FIN
148 Initializing setup...
149
150  Complete!
151
152
153 Checking environment...
154
155  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
156
157  Complete!
158
159
160 Creating config file...
161
162  Complete!
163
164
165 Checking database...
166
167 [Error] --------
168 Could not connect to database.: 
169
170
171 FIN;
172
173                 $this->assertEquals($finished, $txt);
174         }
175
176         /**
177          * @medium
178          */
179         public function testWithConfig()
180         {
181                 $this->mockConnect(true, 1);
182                 $this->mockConnected(true, 1);
183                 $this->mockExistsTable('user', false, 1);
184                 $this->mockUpdate([false, true, true], null, 1);
185
186                 $config = <<<CONF
187 <?php
188
189 // Local configuration
190
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.
193
194 return [
195         'database' => [
196                 'hostname' => '',
197                 'username' => '',
198                 'password' => '',
199                 'database' => '',
200                 'charset' => 'utf8mb4',
201         ],
202
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         // ****************************************************************
208
209         'config' => [
210                 'admin_email' => '',
211                 'sitename' => 'Friendica Social Network',
212                 'register_policy' => \Friendica\Module\Register::OPEN,
213                 'register_text' => '',
214         ],
215         'system' => [
216                 'default_timezone' => 'UTC',
217                 'language' => 'en',
218         ],
219 ];
220 CONF;
221
222                 vfsStream::newFile('prepared.config.php')
223                         ->at($this->root)
224                         ->setContent($config);
225
226                 $console = new AutomaticInstallation($this->consoleArgv);
227                 $console->setOption('f', 'prepared.config.php');
228
229                 $txt = $this->dumpExecute($console);
230
231                 $this->assertFinished($txt, false, true);
232
233                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
234         }
235
236         /**
237          * @medium
238          */
239         public function testWithEnvironmentAndSave()
240         {
241                 $this->mockConnect(true, 1);
242                 $this->mockConnected(true, 1);
243                 $this->mockExistsTable('user', false, 1);
244                 $this->mockUpdate([false, true, true], null, 1);
245
246                 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
247                 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
248
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'));
253
254                 $console = new AutomaticInstallation($this->consoleArgv);
255                 $console->setOption('savedb', true);
256
257                 $txt = $this->dumpExecute($console);
258
259                 $this->assertFinished($txt, true);
260         }
261
262         /**
263          * @medium
264          */
265         public function testWithEnvironmentWithoutSave()
266         {
267                 $this->mockConnect(true, 1);
268                 $this->mockConnected(true, 1);
269                 $this->mockExistsTable('user', false, 1);
270                 $this->mockUpdate([false, true, true], null, 1);
271
272                 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
273                 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
274
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'));
279
280                 $console = new AutomaticInstallation($this->consoleArgv);
281
282                 $txt = $this->dumpExecute($console);
283
284                 $this->assertFinished($txt, true);
285         }
286
287         /**
288          * @medium
289          */
290         public function testWithArguments()
291         {
292                 $this->mockConnect(true, 1);
293                 $this->mockConnected(true, 1);
294                 $this->mockExistsTable('user', false, 1);
295                 $this->mockUpdate([false, true, true], null, 1);
296
297                 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
298                 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
299
300                 $console = new AutomaticInstallation($this->consoleArgv);
301
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);
306                 }
307                 if (!empty($this->db_port)) {
308                         $console->setOption('dbport', $this->db_port);
309                 }
310                 $console->setOption('dbdata', $this->db_data);
311
312                 $console->setOption('admin', 'admin@friendica.local');
313                 $console->setOption('tz', 'Europe/Berlin');
314                 $console->setOption('lang', 'de');
315
316                 $console->setOption('urlpath', '/friendica');
317
318                 $txt = $this->dumpExecute($console);
319
320                 $this->assertFinished($txt, true);
321         }
322
323         /**
324          * @runTestsInSeparateProcesses
325          * @preserveGlobalState disabled
326          */
327         public function testNoDatabaseConnection()
328         {
329                 $this->mockConnect(false, 1);
330
331                 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
332                 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
333
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'));
338
339                 $console = new AutomaticInstallation($this->consoleArgv);
340
341                 $txt = $this->dumpExecute($console);
342
343                 $this->assertStuckDB($txt);
344         }
345
346         public function testGetHelp()
347         {
348                 // Usable to purposely fail if new commands are added without taking tests into account
349                 $theHelp = <<<HELP
350 Installation - Install Friendica automatically
351 Synopsis
352         bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
353
354 Description
355     Installs Friendica with data based on the local.config.php file or environment variables
356
357 Notes
358     Not checking .htaccess/URL-Rewrite during CLI installation.
359
360 Options
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)
376  
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
388    
389 Examples
390         bin/console autoinstall -f 'input.config.php
391                 Installs Friendica with the prepared 'input.config.php' file
392
393         bin/console autoinstall --savedb
394                 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
395
396         bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
397                 Installs Friendica with a local mysql database with credentials
398
399 HELP;
400
401                 $console = new AutomaticInstallation($this->consoleArgv);
402                 $console->setOption('help', true);
403
404                 $txt = $this->dumpExecute($console);
405
406                 $this->assertEquals($txt, $theHelp);
407         }
408 }