]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Console/AutomaticInstallationConsoleTest.php
Merge pull request #5679 from annando/neverending-notices
[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 CFG;
52                 }
53
54                 if ($copyfile) {
55                         $cfg = <<<CFG
56
57
58 Copying config file...
59 CFG;
60                 }
61
62                 $finished = <<<FIN
63 Initializing setup...{$cfg}
64
65  Complete!
66
67
68 Checking basic setup...
69
70  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
71
72  Complete!
73
74
75 Checking database...
76
77  Complete!
78
79
80 Inserting data into database...
81
82  Complete!
83
84
85 Installing theme
86
87  Complete
88
89
90
91 Installation is finished
92
93
94 FIN;
95                 $this->assertEquals($finished, $txt);
96         }
97
98         private function assertStuckDB($txt)
99         {
100                 $finished = <<<FIN
101 Initializing setup...
102
103 Creating config file...
104
105  Complete!
106
107
108 Checking basic setup...
109
110  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
111
112  Complete!
113
114
115 Checking database...
116
117 [Error] --------
118 MySQL Connection: Failed, please check your MySQL settings and credentials.
119
120
121 FIN;
122
123                 $this->assertEquals($finished, $txt);
124         }
125
126         /**
127          * @medium
128          */
129         public function testWithConfig()
130         {
131                 $config = <<<CONF
132 <?php return <<<INI
133
134 [database]
135 hostname = 
136 username = 
137 password = 
138 database = 
139 charset = utf8mb4
140
141
142 ; ****************************************************************
143 ; The configuration below will be overruled by the admin panel.
144 ; Changes made below will only have an effect if the database does
145 ; not contain any configuration for the friendica system.
146 ; ****************************************************************
147
148 [config]
149 admin_email =
150
151 sitename = Friendica Social Network
152
153 register_policy = REGISTER_OPEN
154 register_text =
155
156 [system]
157 default_timezone = UTC
158
159 language = en
160 INI;
161 // Keep this line
162
163 CONF;
164
165                 vfsStream::newFile('prepared.ini.php')
166                         ->at($this->root)
167                         ->setContent($config);
168
169                 $txt = $this->execute(['autoinstall', '-f', 'prepared.ini.php']);
170
171                 $this->assertFinished($txt, false, true);
172
173                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
174         }
175
176         /**
177          * @medium
178          */
179         public function testWithEnvironmentAndSave()
180         {
181                 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
182                 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
183                 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
184
185                 $txt = $this->execute(['autoinstall', '--savedb']);
186
187                 $this->assertFinished($txt, true);
188
189                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
190
191                 $this->assertConfig('database', 'hostname', $this->db_host . (!empty($this->db_port) ? ':' . $this->db_port : ''));
192                 $this->assertConfig('database', 'username', $this->db_user);
193                 $this->assertConfig('database', 'database', $this->db_data);
194                 $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
195                 $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
196                 $this->assertConfig('system', 'language', 'de');
197         }
198
199
200         /**
201          * @medium
202          */
203         public function testWithEnvironmentWithoutSave()
204         {
205                 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
206                 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
207                 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
208
209                 $txt = $this->execute(['autoinstall']);
210
211                 $this->assertFinished($txt, true);
212
213                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
214
215                 $this->assertConfig('database', 'hostname', '');
216                 $this->assertConfig('database', 'username', '');
217                 $this->assertConfig('database', 'database', '');
218                 $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
219                 $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
220                 $this->assertConfig('system', 'language', 'de');
221         }
222
223         /**
224          * @medium
225          */
226         public function testWithArguments()
227         {
228                 $args = ['autoinstall'];
229                 array_push($args, '--dbhost');
230                 array_push($args, $this->db_host);
231                 array_push($args, '--dbuser');
232                 array_push($args, $this->db_user);
233                 if (!empty($this->db_pass)) {
234                         array_push($args, '--dbpass');
235                         array_push($args, $this->db_pass);
236                 }
237                 if (!empty($this->db_port)) {
238                         array_push($args, '--dbport');
239                         array_push($args, $this->db_port);
240                 }
241                 array_push($args, '--dbdata');
242                 array_push($args, $this->db_data);
243
244                 array_push($args, '--admin');
245                 array_push($args, 'admin@friendica.local');
246                 array_push($args, '--tz');
247                 array_push($args, 'Europe/Berlin');
248                 array_push($args, '--lang');
249                 array_push($args, 'de');
250
251                 $txt = $this->execute($args);
252
253                 $this->assertFinished($txt, true);
254
255                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.ini.php'));
256
257                 $this->assertConfig('database', 'hostname', $this->db_host . (!empty($this->db_port) ? ':' . $this->db_port : ''));
258                 $this->assertConfig('database', 'username', $this->db_user);
259                 $this->assertConfig('database', 'database', $this->db_data);
260                 $this->assertConfig('config', 'admin_email', 'admin@friendica.local');
261                 $this->assertConfig('system', 'default_timezone', 'Europe/Berlin');
262                 $this->assertConfig('system', 'language', 'de');
263         }
264
265         public function testNoDatabaseConnection()
266         {
267                 $this->assertTrue(putenv('MYSQL_USERNAME='));
268                 $this->assertTrue(putenv('MYSQL_PASSWORD='));
269                 $this->assertTrue(putenv('MYSQL_DATABASE='));
270
271                 $txt = $this->execute(['autoinstall']);
272
273                 $this->assertStuckDB($txt);
274         }
275
276         public function testGetHelp()
277         {
278                 // Usable to purposely fail if new commands are added without taking tests into account
279                 $theHelp = <<<HELP
280 Installation - Install Friendica automatically
281 Synopsis
282         bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
283
284 Description
285     Installs Friendica with data based on the local.ini.php file or environment variables
286
287 Notes
288     Not checking .htaccess/URL-Rewrite during CLI installation.
289
290 Options
291     -h|--help|-?           Show help information
292     -v                     Show more debug information.
293     -a                     All setup checks are required (except .htaccess)
294     -f|--file <config>     prepared config file (e.g. "config/local.ini.php" itself) which will override every other config option - except the environment variables)
295     -s|--savedb            Save the DB credentials to the file (if environment variables is used)
296     -H|--dbhost <host>     The host of the mysql/mariadb database (env MYSQL_HOST)
297     -p|--dbport <port>     The port of the mysql/mariadb database (env MYSQL_PORT)
298     -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
299     -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
300     -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
301     -b|--phppath <path>    The path of the PHP binary (env FRIENDICA_PHP_PATH) 
302     -A|--admin <mail>      The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
303     -T|--tz <timezone>     The timezone of Friendica (env FRIENDICA_TZ)
304     -L|--lang <language>   The language of Friendica (env FRIENDICA_LANG)
305  
306 Environment variables
307    MYSQL_HOST                  The host of the mysql/mariadb database (mandatory if mysql and environment is used)
308    MYSQL_PORT                  The port of the mysql/mariadb database
309    MYSQL_USERNAME|MYSQL_USER   The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
310    MYSQL_PASSWORD              The password of the mysql/mariadb database login
311    MYSQL_DATABASE              The name of the mysql/mariadb database
312    FRIENDICA_PHP_PATH          The path of the PHP binary
313    FRIENDICA_ADMIN_MAIL        The admin email address of Friendica (this email will be used for admin access)
314    FRIENDICA_TZ                The timezone of Friendica
315    FRIENDICA_LANG              The langauge of Friendica
316    
317 Examples
318         bin/console autoinstall -f 'input.ini.php
319                 Installs Friendica with the prepared 'input.ini.php' file
320
321         bin/console autoinstall --savedb
322                 Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
323
324         bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
325                 Installs Friendica with a local mysql database with credentials
326
327 HELP;
328
329                 $txt = $this->execute(['autoinstall', '-h']);
330
331                 $this->assertEquals($txt, $theHelp);
332         }
333 }