]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Console/AutomaticInstallationConsoleTest.php
41ccce0b28827410ebe67a6695d9788032af77b1
[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->configCache
56                         ->shouldReceive('get')
57                         ->with('config', 'php_path', NULL)
58                         ->andReturn(false);
59
60                 $this->mockL10nT();
61         }
62
63         /**
64          * Creates the arguments which is asserted to be passed to 'replaceMacros()' for creating the local.config.php
65          *
66          * @param bool $withDb if true, DB will get saved too
67          *
68          * @return array The arguments to pass to the mock for 'replaceMacros()'
69          */
70         private function createArgumentsForMacro($withDb)
71         {
72                 $args = [
73                         '$phpath' => trim(shell_exec('which php')),
74                         '$dbhost' => (($withDb) ? $this->db_host . (isset($this->db_port) ? ':' . $this->db_port : '') : ''),
75                         '$dbuser' => (($withDb) ? $this->db_user : ''),
76                         '$dbpass' => (($withDb) ? $this->db_pass : ''),
77                         '$dbdata' => (($withDb) ? $this->db_data : ''),
78                         '$timezone' => 'Europe/Berlin',
79                         '$language' => 'de',
80                         '$urlpath' => '/friendica',
81                         '$adminmail' => 'admin@friendica.local'
82                 ];
83
84                 return $args;
85         }
86
87         private function assertFinished($txt, $withconfig = false, $copyfile = false)
88         {
89                 $cfg = '';
90
91                 if ($withconfig) {
92                         $cfg = <<<CFG
93
94
95 Creating config file...
96
97  Complete!
98 CFG;
99                 }
100
101                 if ($copyfile) {
102                         $cfg = <<<CFG
103
104
105 Copying config file...
106
107  Complete!
108 CFG;
109                 }
110
111                 $finished = <<<FIN
112 Initializing setup...
113
114  Complete!
115
116
117 Checking environment...
118
119  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
120
121  Complete!
122 {$cfg}
123
124
125 Checking database...
126
127  Complete!
128
129
130 Inserting data into database...
131
132  Complete!
133
134
135 Installing theme
136
137  Complete
138
139
140
141 Installation is finished
142
143
144 FIN;
145                 $this->assertEquals($finished, $txt);
146         }
147
148         private function assertStuckDB($txt)
149         {
150                 $finished = <<<FIN
151 Initializing setup...
152
153  Complete!
154
155
156 Checking environment...
157
158  NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.
159
160  Complete!
161
162
163 Creating config file...
164
165  Complete!
166
167
168 Checking database...
169
170 [Error] --------
171 Could not connect to database.: 
172
173
174 FIN;
175
176                 $this->assertEquals($finished, $txt);
177         }
178
179         /**
180          * @medium
181          */
182         public function testWithConfig()
183         {
184                 $this->mockConnect(true, 1);
185                 $this->mockConnected(true, 1);
186                 $this->mockExistsTable('user', false, 1);
187                 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
188
189                 $config = <<<CONF
190 <?php
191
192 // Local configuration
193
194 // If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed
195 // documentation of their data type and behavior.
196
197 return [
198         'database' => [
199                 'hostname' => '',
200                 'username' => '',
201                 'password' => '',
202                 'database' => '',
203                 'charset' => 'utf8mb4',
204         ],
205
206         // ****************************************************************
207         // The configuration below will be overruled by the admin panel.
208         // Changes made below will only have an effect if the database does
209         // not contain any configuration for the friendica system.
210         // ****************************************************************
211
212         'config' => [
213                 'admin_email' => '',
214                 'sitename' => 'Friendica Social Network',
215                 'register_policy' => \Friendica\Module\Register::OPEN,
216                 'register_text' => '',
217         ],
218         'system' => [
219                 'default_timezone' => 'UTC',
220                 'language' => 'en',
221         ],
222 ];
223 CONF;
224
225                 vfsStream::newFile('prepared.config.php')
226                         ->at($this->root)
227                         ->setContent($config);
228
229                 $console = new AutomaticInstallation($this->consoleArgv);
230                 $console->setOption('f', 'prepared.config.php');
231
232                 $txt = $this->dumpExecute($console);
233
234                 $this->assertFinished($txt, false, true);
235
236                 $this->assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
237         }
238
239         /**
240          * @medium
241          */
242         public function testWithEnvironmentAndSave()
243         {
244                 $this->mockConnect(true, 1);
245                 $this->mockConnected(true, 1);
246                 $this->mockExistsTable('user', false, 1);
247                 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
248
249                 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
250                 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
251
252                 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
253                 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
254                 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
255                 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
256
257                 $console = new AutomaticInstallation($this->consoleArgv);
258                 $console->setOption('savedb', true);
259
260                 $txt = $this->dumpExecute($console);
261
262                 $this->assertFinished($txt, true);
263         }
264
265         /**
266          * @medium
267          */
268         public function testWithEnvironmentWithoutSave()
269         {
270                 $this->mockConnect(true, 1);
271                 $this->mockConnected(true, 1);
272                 $this->mockExistsTable('user', false, 1);
273                 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
274
275                 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
276                 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
277
278                 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
279                 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
280                 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
281                 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
282
283                 $console = new AutomaticInstallation($this->consoleArgv);
284
285                 $txt = $this->dumpExecute($console);
286
287                 $this->assertFinished($txt, true);
288         }
289
290         /**
291          * @medium
292          */
293         public function testWithArguments()
294         {
295                 $this->mockConnect(true, 1);
296                 $this->mockConnected(true, 1);
297                 $this->mockExistsTable('user', false, 1);
298                 $this->mockUpdate([$this->root->url(), false, true, true], null, 1);
299
300                 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
301                 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(true), '', 1);
302
303                 $console = new AutomaticInstallation($this->consoleArgv);
304
305                 $console->setOption('dbhost', $this->db_host);
306                 $console->setOption('dbuser', $this->db_user);
307                 if (!empty($this->db_pass)) {
308                         $console->setOption('dbpass', $this->db_pass);
309                 }
310                 if (!empty($this->db_port)) {
311                         $console->setOption('dbport', $this->db_port);
312                 }
313                 $console->setOption('dbdata', $this->db_data);
314
315                 $console->setOption('admin', 'admin@friendica.local');
316                 $console->setOption('tz', 'Europe/Berlin');
317                 $console->setOption('lang', 'de');
318
319                 $console->setOption('urlpath', '/friendica');
320
321                 $txt = $this->dumpExecute($console);
322
323                 $this->assertFinished($txt, true);
324         }
325
326         /**
327          * @runTestsInSeparateProcesses
328          * @preserveGlobalState disabled
329          */
330         public function testNoDatabaseConnection()
331         {
332                 $this->mockConnect(false, 1);
333
334                 $this->mockGetMarkupTemplate('local.config.tpl', 'testTemplate', 1);
335                 $this->mockReplaceMacros('testTemplate', $this->createArgumentsForMacro(false), '', 1);
336
337                 $this->assertTrue(putenv('FRIENDICA_ADMIN_MAIL=admin@friendica.local'));
338                 $this->assertTrue(putenv('FRIENDICA_TZ=Europe/Berlin'));
339                 $this->assertTrue(putenv('FRIENDICA_LANG=de'));
340                 $this->assertTrue(putenv('FRIENDICA_URL_PATH=/friendica'));
341
342                 $console = new AutomaticInstallation($this->consoleArgv);
343
344                 $txt = $this->dumpExecute($console);
345
346                 $this->assertStuckDB($txt);
347         }
348
349         public function testGetHelp()
350         {
351                 // Usable to purposely fail if new commands are added without taking tests into account
352                 $theHelp = <<<HELP
353 Installation - Install Friendica automatically
354 Synopsis
355         bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
356
357 Description
358     Installs Friendica with data based on the local.config.php file or environment variables
359
360 Notes
361     Not checking .htaccess/URL-Rewrite during CLI installation.
362
363 Options
364     -h|--help|-?            Show help information
365     -v                      Show more debug information.
366     -a                      All setup checks are required (except .htaccess)
367     -f|--file <config>      prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
368     -s|--savedb             Save the DB credentials to the file (if environment variables is used)
369     -H|--dbhost <host>      The host of the mysql/mariadb database (env MYSQL_HOST)
370     -p|--dbport <port>      The port of the mysql/mariadb database (env MYSQL_PORT)
371     -d|--dbdata <database>  The name of the mysql/mariadb database (env MYSQL_DATABASE)
372     -U|--dbuser <username>  The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
373     -P|--dbpass <password>  The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
374     -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH) 
375     -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH) 
376     -A|--admin <mail>       The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
377     -T|--tz <timezone>      The timezone of Friendica (env FRIENDICA_TZ)
378     -L|--lang <language>    The language of Friendica (env FRIENDICA_LANG)
379  
380 Environment variables
381    MYSQL_HOST                  The host of the mysql/mariadb database (mandatory if mysql and environment is used)
382    MYSQL_PORT                  The port of the mysql/mariadb database
383    MYSQL_USERNAME|MYSQL_USER   The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
384    MYSQL_PASSWORD              The password of the mysql/mariadb database login
385    MYSQL_DATABASE              The name of the mysql/mariadb database
386    FRIENDICA_URL_PATH          The URL path of Friendica (f.e. '/friendica')
387    FRIENDICA_PHP_PATH          The path of the PHP binary
388    FRIENDICA_ADMIN_MAIL        The admin email address of Friendica (this email will be used for admin access)
389    FRIENDICA_TZ                The timezone of Friendica
390    FRIENDICA_LANG              The langauge of Friendica
391    
392 Examples
393         bin/console autoinstall -f 'input.config.php
394                 Installs Friendica with the prepared 'input.config.php' file
395
396         bin/console autoinstall --savedb
397                 Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
398
399         bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
400                 Installs Friendica with a local mysql database with credentials
401
402 HELP;
403
404                 $console = new AutomaticInstallation($this->consoleArgv);
405                 $console->setOption('help', true);
406
407                 $txt = $this->dumpExecute($console);
408
409                 $this->assertEquals($txt, $theHelp);
410         }
411 }