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