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