]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/InstallerTest.php
1263fe7e67b73f0f7e72c0ed3f3af80287ec87a3
[friendica.git] / tests / src / Core / InstallerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 /// @todo this is in the same namespace as Install for mocking 'function_exists'
23 namespace Friendica\Core;
24
25 use Dice\Dice;
26 use Friendica\Core\Config\Cache;
27 use Friendica\DI;
28 use Friendica\Network\IHTTPResult;
29 use Friendica\Network\IHTTPClient;
30 use Friendica\Test\MockedTest;
31 use Friendica\Test\Util\VFSTrait;
32 use Mockery;
33 use Mockery\MockInterface;
34
35 class InstallerTest extends MockedTest
36 {
37         use VFSTrait;
38
39         /**
40          * @var L10n|MockInterface
41          */
42         private $l10nMock;
43         /**
44          * @var Dice|MockInterface
45          */
46         private $dice;
47
48         protected function setUp(): void
49         {
50                 parent::setUp();
51
52                 $this->setUpVfsDir();
53
54                 $this->l10nMock = Mockery::mock(L10n::class);
55
56                 /** @var Dice|MockInterface $dice */
57                 $this->dice = Mockery::mock(Dice::class)->makePartial();
58                 $this->dice = $this->dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
59
60                 $this->dice->shouldReceive('create')
61                            ->with(L10n::class)
62                            ->andReturn($this->l10nMock);
63
64                 DI::init($this->dice);
65         }
66
67         public static function tearDownAfterClass(): void
68         {
69                 // Reset mocking
70                 global $phpMock;
71                 $phpMock = [];
72
73                 parent::tearDownAfterClass();
74         }
75
76         private function mockL10nT(string $text, $times = null)
77         {
78                 $this->l10nMock->shouldReceive('t')->with($text)->andReturn($text)->times($times);
79         }
80
81         /**
82          * Mocking the DI::l10n()->t() calls for the function checks
83          */
84         private function mockFunctionL10TCalls()
85         {
86                 $this->mockL10nT('Apache mod_rewrite module', 1);
87                 $this->mockL10nT('PDO or MySQLi PHP module', 1);
88                 $this->mockL10nT('libCurl PHP module', 1);
89                 $this->mockL10nT('Error: libCURL PHP module required but not installed.', 1);
90                 $this->mockL10nT('XML PHP module', 1);
91                 $this->mockL10nT('GD graphics PHP module', 1);
92                 $this->mockL10nT('Error: GD graphics PHP module with JPEG support required but not installed.', 1);
93                 $this->mockL10nT('OpenSSL PHP module', 1);
94                 $this->mockL10nT('Error: openssl PHP module required but not installed.', 1);
95                 $this->mockL10nT('mb_string PHP module', 1);
96                 $this->mockL10nT('Error: mb_string PHP module required but not installed.', 1);
97                 $this->mockL10nT('iconv PHP module', 1);
98                 $this->mockL10nT('Error: iconv PHP module required but not installed.', 1);
99                 $this->mockL10nT('POSIX PHP module', 1);
100                 $this->mockL10nT('Error: POSIX PHP module required but not installed.', 1);
101                 $this->mockL10nT('JSON PHP module', 1);
102                 $this->mockL10nT('Error: JSON PHP module required but not installed.', 1);
103                 $this->mockL10nT('File Information PHP module', 1);
104                 $this->mockL10nT('Error: File Information PHP module required but not installed.', 1);
105                 $this->mockL10nT('Program execution functions', 1);
106                 $this->mockL10nT('Error: Program execution functions (proc_open) required but not enabled.', 1);
107         }
108
109         private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray)
110         {
111                 $subSet = [$position => [
112                         'title' => $title,
113                         'status' => $status,
114                         'required' => $required,
115                         'error_msg' => null,
116                         'help' => $help]
117                 ];
118
119                 self::assertArraySubset($subSet, $assertionArray, false, "expected subset: " . PHP_EOL . print_r($subSet, true) . PHP_EOL . "current subset: " . print_r($assertionArray, true));
120         }
121
122         /**
123          * Replaces function_exists results with given mocks
124          *
125          * @param array $functions a list from function names and their result
126          */
127         private function setFunctions(array $functions)
128         {
129                 global $phpMock;
130                 $phpMock['function_exists'] = function($function) use ($functions) {
131                         foreach ($functions as $name => $value) {
132                                 if ($function == $name) {
133                                         return $value;
134                                 }
135                         }
136                         return '__phpunit_continue__';
137                 };
138         }
139
140         /**
141          * Replaces class_exist results with given mocks
142          *
143          * @param array $classes a list from class names and their results
144          */
145         private function setClasses(array $classes)
146         {
147                 global $phpMock;
148                 $phpMock['class_exists'] = function($class) use ($classes) {
149                         foreach ($classes as $name => $value) {
150                                 if ($class == $name) {
151                                         return $value;
152                                 }
153                         }
154                         return '__phpunit_continue__';
155                 };
156         }
157
158         /**
159          * @small
160          */
161         public function testCheckKeys()
162         {
163                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
164
165                 $this->setFunctions(['openssl_pkey_new' => false]);
166                 $install = new Installer();
167                 self::assertFalse($install->checkKeys());
168
169                 $this->setFunctions(['openssl_pkey_new' => true]);
170                 $install = new Installer();
171                 self::assertTrue($install->checkKeys());
172         }
173
174         /**
175          * @small
176          */
177         public function testCheckFunctions()
178         {
179                 $this->mockFunctionL10TCalls();
180                 $this->setFunctions(['curl_init' => false, 'imagecreatefromjpeg' => true]);
181                 $install = new Installer();
182                 self::assertFalse($install->checkFunctions());
183                 self::assertCheckExist(3,
184                         'libCurl PHP module',
185                         'Error: libCURL PHP module required but not installed.',
186                         false,
187                         true,
188                         $install->getChecks());
189
190                 $this->mockFunctionL10TCalls();
191                 $this->setFunctions(['imagecreatefromjpeg' => false]);
192                 $install = new Installer();
193                 self::assertFalse($install->checkFunctions());
194                 self::assertCheckExist(4,
195                         'GD graphics PHP module',
196                         'Error: GD graphics PHP module with JPEG support required but not installed.',
197                         false,
198                         true,
199                         $install->getChecks());
200
201                 $this->mockFunctionL10TCalls();
202                 $this->setFunctions(['openssl_public_encrypt' => false]);
203                 $install = new Installer();
204                 self::assertFalse($install->checkFunctions());
205                 self::assertCheckExist(5,
206                         'OpenSSL PHP module',
207                         'Error: openssl PHP module required but not installed.',
208                         false,
209                         true,
210                         $install->getChecks());
211
212                 $this->mockFunctionL10TCalls();
213                 $this->setFunctions(['mb_strlen' => false]);
214                 $install = new Installer();
215                 self::assertFalse($install->checkFunctions());
216                 self::assertCheckExist(6,
217                         'mb_string PHP module',
218                         'Error: mb_string PHP module required but not installed.',
219                         false,
220                         true,
221                         $install->getChecks());
222
223                 $this->mockFunctionL10TCalls();
224                 $this->setFunctions(['iconv_strlen' => false]);
225                 $install = new Installer();
226                 self::assertFalse($install->checkFunctions());
227                 self::assertCheckExist(7,
228                         'iconv PHP module',
229                         'Error: iconv PHP module required but not installed.',
230                         false,
231                         true,
232                         $install->getChecks());
233
234                 $this->mockFunctionL10TCalls();
235                 $this->setFunctions(['posix_kill' => false]);
236                 $install = new Installer();
237                 self::assertFalse($install->checkFunctions());
238                 self::assertCheckExist(8,
239                         'POSIX PHP module',
240                         'Error: POSIX PHP module required but not installed.',
241                         false,
242                         true,
243                         $install->getChecks());
244
245                 $this->mockFunctionL10TCalls();
246                 $this->setFunctions(['proc_open' => false]);
247                 $install = new Installer();
248                 self::assertFalse($install->checkFunctions());
249                 self::assertCheckExist(9,
250                         'Program execution functions',
251                         'Error: Program execution functions (proc_open) required but not enabled.',
252                         false,
253                         true,
254                         $install->getChecks());
255                 $this->mockFunctionL10TCalls();
256                 $this->setFunctions(['json_encode' => false]);
257                 $install = new Installer();
258                 self::assertFalse($install->checkFunctions());
259                 self::assertCheckExist(10,
260                         'JSON PHP module',
261                         'Error: JSON PHP module required but not installed.',
262                         false,
263                         true,
264                         $install->getChecks());
265
266                 $this->mockFunctionL10TCalls();
267                 $this->setFunctions(['finfo_open' => false]);
268                 $install = new Installer();
269                 self::assertFalse($install->checkFunctions());
270                 self::assertCheckExist(11,
271                         'File Information PHP module',
272                         'Error: File Information PHP module required but not installed.',
273                         false,
274                         true,
275                         $install->getChecks());
276
277                 $this->mockFunctionL10TCalls();
278                 $this->setFunctions([
279                         'curl_init' => true,
280                         'imagecreatefromjpeg' => true,
281                         'openssl_public_encrypt' => true,
282                         'mb_strlen' => true,
283                         'iconv_strlen' => true,
284                         'posix_kill' => true,
285                         'json_encode' => true,
286                         'finfo_open' => true,
287                 ]);
288                 $install = new Installer();
289                 self::assertTrue($install->checkFunctions());
290         }
291
292         /**
293          * @small
294          */
295         public function testCheckLocalIni()
296         {
297                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
298
299                 self::assertTrue($this->root->hasChild('config/local.config.php'));
300
301                 $install = new Installer();
302                 self::assertTrue($install->checkLocalIni());
303
304                 $this->delConfigFile('local.config.php');
305
306                 self::assertFalse($this->root->hasChild('config/local.config.php'));
307
308                 $install = new Installer();
309                 self::assertTrue($install->checkLocalIni());
310         }
311
312         /**
313          * @small
314          * @runInSeparateProcess
315          * @preserveGlobalState disabled
316          */
317         public function testCheckHtAccessFail()
318         {
319                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
320
321                 // Mocking the CURL Response
322                 $IHTTPResult = Mockery::mock(IHTTPResult::class);
323                 $IHTTPResult
324                         ->shouldReceive('getReturnCode')
325                         ->andReturn('404');
326                 $IHTTPResult
327                         ->shouldReceive('getRedirectUrl')
328                         ->andReturn('');
329                 $IHTTPResult
330                         ->shouldReceive('getError')
331                         ->andReturn('test Error');
332
333                 // Mocking the CURL Request
334                 $networkMock = Mockery::mock(IHTTPClient::class);
335                 $networkMock
336                         ->shouldReceive('fetchFull')
337                         ->with('https://test/install/testrewrite')
338                         ->andReturn($IHTTPResult);
339                 $networkMock
340                         ->shouldReceive('fetchFull')
341                         ->with('http://test/install/testrewrite')
342                         ->andReturn($IHTTPResult);
343
344                 $this->dice->shouldReceive('create')
345                      ->with(IHTTPClient::class)
346                      ->andReturn($networkMock);
347
348                 DI::init($this->dice);
349
350                 // Mocking that we can use CURL
351                 $this->setFunctions(['curl_init' => true]);
352
353                 $install = new Installer();
354
355                 self::assertFalse($install->checkHtAccess('https://test'));
356                 self::assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
357         }
358
359         /**
360          * @small
361          * @runInSeparateProcess
362          * @preserveGlobalState disabled
363          */
364         public function testCheckHtAccessWork()
365         {
366                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
367
368                 // Mocking the failed CURL Response
369                 $IHTTPResultF = Mockery::mock(IHTTPResult::class);
370                 $IHTTPResultF
371                         ->shouldReceive('getReturnCode')
372                         ->andReturn('404');
373
374                 // Mocking the working CURL Response
375                 $IHTTPResultW = Mockery::mock(IHTTPResult::class);
376                 $IHTTPResultW
377                         ->shouldReceive('getReturnCode')
378                         ->andReturn('204');
379
380                 // Mocking the CURL Request
381                 $networkMock = Mockery::mock(IHTTPClient::class);
382                 $networkMock
383                         ->shouldReceive('fetchFull')
384                         ->with('https://test/install/testrewrite')
385                         ->andReturn($IHTTPResultF);
386                 $networkMock
387                         ->shouldReceive('fetchFull')
388                         ->with('http://test/install/testrewrite')
389                         ->andReturn($IHTTPResultW);
390
391                 $this->dice->shouldReceive('create')
392                            ->with(IHTTPClient::class)
393                            ->andReturn($networkMock);
394
395                 DI::init($this->dice);
396
397                 // Mocking that we can use CURL
398                 $this->setFunctions(['curl_init' => true]);
399
400                 $install = new Installer();
401
402                 self::assertTrue($install->checkHtAccess('https://test'));
403         }
404
405         /**
406          * @small
407          * @runInSeparateProcess
408          * @preserveGlobalState disabled
409          */
410         public function testImagick()
411         {
412                 static::markTestIncomplete('needs adapted class_exists() mock');
413
414                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
415
416                 $this->setClasses(['Imagick' => true]);
417
418                 $install = new Installer();
419
420                 // even there is no supported type, Imagick should return true (because it is not required)
421                 self::assertTrue($install->checkImagick());
422
423                 self::assertCheckExist(1,
424                         $this->l10nMock->t('ImageMagick supports GIF'),
425                         '',
426                         true,
427                         false,
428                         $install->getChecks());
429         }
430
431         /**
432          * @small
433          * @runInSeparateProcess
434          * @preserveGlobalState disabled
435          */
436         public function testImagickNotFound()
437         {
438                 static::markTestIncomplete('Disabled due not working/difficult mocking global functions - needs more care!');
439
440                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
441
442                 $this->setClasses(['Imagick' => true]);
443
444                 $install = new Installer();
445
446                 // even there is no supported type, Imagick should return true (because it is not required)
447                 self::assertTrue($install->checkImagick());
448                 self::assertCheckExist(1,
449                         $this->l10nMock->t('ImageMagick supports GIF'),
450                         '',
451                         false,
452                         false,
453                         $install->getChecks());
454         }
455
456         public function testImagickNotInstalled()
457         {
458                 $this->setClasses(['Imagick' => false]);
459                 $this->mockL10nT('ImageMagick PHP extension is not installed');
460
461                 $install = new Installer();
462
463                 // even there is no supported type, Imagick should return true (because it is not required)
464                 self::assertTrue($install->checkImagick());
465                 self::assertCheckExist(0,
466                         'ImageMagick PHP extension is not installed',
467                         '',
468                         false,
469                         false,
470                         $install->getChecks());
471         }
472
473         /**
474          * Test the setup of the config cache for installation
475          * @doesNotPerformAssertions
476          */
477         public function testSetUpCache()
478         {
479                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
480
481                 $install = new Installer();
482                 $configCache = Mockery::mock(Cache::class);
483                 $configCache->shouldReceive('set')->with('config', 'php_path', Mockery::any())->once();
484                 $configCache->shouldReceive('set')->with('system', 'basepath', '/test/')->once();
485
486                 $install->setUpCache($configCache, '/test/');
487         }
488 }
489
490 /**
491  * A workaround to replace the PHP native function_exists with a mocked function
492  *
493  * @param string $function_name the Name of the function
494  *
495  * @return bool true or false
496  */
497 function function_exists(string $function_name)
498 {
499         global $phpMock;
500         if (isset($phpMock['function_exists'])) {
501                 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
502                 if ($result !== '__phpunit_continue__') {
503                         return $result;
504                 }
505         }
506         return call_user_func_array('\function_exists', func_get_args());
507 }
508
509 function class_exists($class_name)
510 {
511         global $phpMock;
512         if (isset($phpMock['class_exists'])) {
513                 $result = call_user_func_array($phpMock['class_exists'], func_get_args());
514                 if ($result !== '__phpunit_continue__') {
515                         return $result;
516                 }
517         }
518         return call_user_func_array('\class_exists', func_get_args());
519 }