]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Console/AutomaticInstallationConsoleTest.php
Bugfixings
[friendica.git] / tests / src / Core / Console / AutomaticInstallationConsoleTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Console;
4
5 use org\bovigo\vfs\vfsStream;
6
7 /**
8  * @runTestsInSeparateProcesses
9  * @preserveGlobalState disabled
10  * @requires PHP 7.0
11  */
12 class AutomaticInstallationConsoleTest extends ConsoleTest
13 {
14         private $db_host;
15         private $db_port;
16         private $db_data;
17         private $db_user;
18         private $db_pass;
19
20         public function setUp()
21         {
22                 parent::setUp();
23
24                 if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
25                         $this->root->getChild('config')
26                                 ->removeChild('local.ini.php');
27                 }
28
29                 $this->db_host = getenv('MYSQL_HOST');
30                 $this->db_port = (!empty(getenv('MYSQL_PORT'))) ? getenv('MYSQL_PORT') : null;
31                 $this->db_data = getenv('MYSQL_DATABASE');
32                 $this->db_user = getenv('MYSQL_USERNAME') . getenv('MYSQL_USER');
33                 $this->db_pass = getenv('MYSQL_PASSWORD');
34         }
35
36         private function assertConfig($family, $key, $value)
37         {
38                 $config = $this->execute(['config', $family, $key]);
39                 $this->assertEquals($family . "." . $key . " => " . $value . "\n", $config);
40         }
41
42         private function assertFinished($txt, $withconfig = false, $copyfile = false)
43         {
44                 $cfg = '';
45
46                 if ($withconfig) {
47                         $cfg = <<<CFG
48
49
50 Creating config file...
51
52  Complete!
53 CFG;
54                 }
55
56                 if ($copyfile) {
57                         $cfg = <<<CFG
58
59
60 Copying config file...
61
62  Complete!
63 CFG;
64                 }
65
66                 $finished = <<<FIN
67 Initializing setup...
68
69  Complete!
70
71
72 Checking environment...
73
74  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
75
76  Complete!
77 {$cfg}
78
79
80 Checking database...
81
82  Complete!
83
84
85 Inserting data into database...
86
87  Complete!
88
89
90 Installing theme
91
92  Complete
93
94
95
96 Installation is finished
97
98
99 FIN;
100                 $this->assertEquals($finished, $txt);
101         }
102
103         private function assertStuckDB($txt)
104         {
105                 $finished = <<<FIN
106 Initializing setup...
107
108 Creating config file...
109
110  Complete!
111
112
113 Checking basic setup...
114
115  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
116
117  Complete!
118
119
120 Checking database...
121
122 [Error] --------
123 MySQL Connection: Failed, please check your MySQL settings and credentials.
124
125
126 FIN;
127
128                 $this->assertEquals($finished, $txt);
129         }
130
131         /**
132          * @medium
133          */
134         public function testWithConfig()
135         {
136                 $config = <<<CONF
137 <?php return <<<INI
138
139 [database]
140 hostname = 
141 username = 
142 password = 
143 database = 
144 charset = utf8mb4
145
146
147 ; ****************************************************************
148 ; The configuration below will be overruled by the admin panel.
149 ; Changes made below will only have an effect if the database does
150 ; not contain any configuration for the friendica system.
151 ; ****************************************************************
152
153 [config]
154 admin_email =
155
156 sitename = Friendica Social Network
157
158 register_policy = REGISTER_OPEN
159 register_text =
160
161 [system]
162 default_timezone = UTC
163
164 language = en
165 INI;
166 // Keep this line
167
168 CONF;
169
170                 vfsStream::newFile('prepared.ini.php')
171                         ->at($this->root)
172                         ->setContent($config);
173
174                 $txt = $this->execute(['autoinstall', '-f', 'prepared.ini.php']);
175
176                 $this->assertFinished($txt, false, true);
177
178                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
179         }
180
181         /**
182          * @medium
183          */
184         public function testWithEnvironmentAndSave()
185         {
186                 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
187                 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
188                 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
189
190                 $txt = $this->execute(['autoinstall', '--savedb']);
191
192                 $this->assertFinished($txt, true);
193
194                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
195
196                 $this->assertConfig('database', 'hostname', $this->db_host . (!empty($this->db_port) ? ':' . $this->db_port : ''));
197                 $this->assertConfig('database', 'username', $this->db_user);
198                 $this->assertConfig('database', 'database', $this->db_data);
199                 $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
200                 $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
201                 // TODO language changes back to en
202                 //$this->assertConfig('system', 'language', 'de');
203         }
204
205         /**
206          * @medium
207          */
208         public function testWithEnvironmentWithoutSave()
209         {
210                 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
211                 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
212                 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
213                 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
214
215                 $txt = $this->execute(['autoinstall']);
216
217                 $this->assertFinished($txt, true);
218
219                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
220
221                 $this->assertConfig('database', 'hostname', '');
222                 $this->assertConfig('database', 'username', '');
223                 $this->assertConfig('database', 'database', '');
224                 $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
225                 $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
226                 $this->assertConfig('system', 'urlpath', '/friendica');
227                 // TODO language changes back to en
228                 //$this->assertConfig('system', 'language', 'de');
229         }
230
231         /**
232          * @medium
233          */
234         public function testWithArguments()
235         {
236                 $args = ['autoinstall'];
237                 array_push($args, '--dbhost');
238                 array_push($args, $this->db_host);
239                 array_push($args, '--dbuser');
240                 array_push($args, $this->db_user);
241                 if (!empty($this->db_pass)) {
242                         array_push($args, '--dbpass');
243                         array_push($args, $this->db_pass);
244                 }
245                 if (!empty($this->db_port)) {
246                         array_push($args, '--dbport');
247                         array_push($args, $this->db_port);
248                 }
249                 array_push($args, '--dbdata');
250                 array_push($args, $this->db_data);
251
252                 array_push($args, '--admin');
253                 array_push($args, 'admin@friendica.local');
254                 array_push($args, '--tz');
255                 array_push($args, 'Europe/Berlin');
256                 array_push($args, '--lang');
257                 array_push($args, 'de');
258
259                 array_push($args, '--urlpath');
260                 array_push($args, '/friendica');
261
262                 $txt = $this->execute($args);
263
264                 $this->assertFinished($txt, true);
265
266                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
267
268                 $this->assertConfig('database', 'hostname', $this->db_host . (!empty($this->db_port) ? ':' . $this->db_port : ''));
269                 $this->assertConfig('database', 'username', $this->db_user);
270                 $this->assertConfig('database', 'database', $this->db_data);
271                 $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
272                 $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
273                 $this->assertConfig('system', 'urlpath', '/friendica');
274                 // TODO language changes back to en
275                 //$this->assertConfig('system', 'language', 'de');
276         }
277
278         /**
279          * @runTestsInSeparateProcesses
280          * @preserveGlobalState disabled
281          */
282         public function testNoDatabaseConnection()
283         {
284                 // TODO DBA mocking for whole console tests make this test work again
285                 $this->markTestSkipped('DBA is already loaded, we have to mock the whole App to make it work');
286
287                 $dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
288                 $dbaMock
289                         ->shouldReceive('connected')
290                         ->andReturn(false);
291
292                 $txt = $this->execute(['autoinstall']);
293
294                 $this->assertStuckDB($txt);
295         }
296
297         public function testGetHelp()
298         {
299                 // Usable to purposely fail if new commands are added without taking tests into account
300                 $theHelp = <<<HELP
301 Installation - Install Friendica automatically
302 Synopsis
303         bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
304
305 Description
306     Installs Friendica with data based on the local.ini.php file or environment variables
307
308 Notes
309     Not checking .htaccess/URL-Rewrite during CLI installation.
310
311 Options
312     -h|--help|-?            Show help information
313     -v                      Show more debug information.
314     -a                      All setup checks are required (except .htaccess)
315     -f|--file <config>      prepared config file (e.g. "config/local.ini.php" itself) which will override every other config option - except the environment variables)
316     -s|--savedb             Save the DB credentials to the file (if environment variables is used)
317     -H|--dbhost <host>      The host of the mysql/mariadb database (env MYSQL_HOST)
318     -p|--dbport <port>      The port of the mysql/mariadb database (env MYSQL_PORT)
319     -d|--dbdata <database>  The name of the mysql/mariadb database (env MYSQL_DATABASE)
320     -U|--dbuser <username>  The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
321     -P|--dbpass <password>  The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
322     -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH) 
323     -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH) 
324     -A|--admin <mail>       The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
325     -T|--tz <timezone>      The timezone of Friendica (env FRIENDICA_TZ)
326     -L|--lang <language>    The language of Friendica (env FRIENDICA_LANG)
327  
328 Environment variables
329    MYSQL_HOST                  The host of the mysql/mariadb database (mandatory if mysql and environment is used)
330    MYSQL_PORT                  The port of the mysql/mariadb database
331    MYSQL_USERNAME|MYSQL_USER   The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
332    MYSQL_PASSWORD              The password of the mysql/mariadb database login
333    MYSQL_DATABASE              The name of the mysql/mariadb database
334    FRIENDICA_URL_PATH          The URL path of Friendica (f.e. '/friendica')
335    FRIENDICA_PHP_PATH          The path of the PHP binary
336    FRIENDICA_ADMIN_MAIL        The admin email address of Friendica (this email will be used for admin access)
337    FRIENDICA_TZ                The timezone of Friendica
338    FRIENDICA_LANG              The langauge of Friendica
339    
340 Examples
341         bin/console autoinstall -f 'input.ini.php
342                 Installs Friendica with the prepared 'input.ini.php' file
343
344         bin/console autoinstall --savedb
345                 Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
346
347         bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
348                 Installs Friendica with a local mysql database with credentials
349
350 HELP;
351
352                 $txt = $this->execute(['autoinstall', '-h']);
353
354                 $this->assertEquals($txt, $theHelp);
355         }
356 }