]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/InstallerTest.php
Fix a lot of notices/warnings/deprecation notes in the test directory
[friendica.git] / tests / src / Core / InstallerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\CurlResult;
29 use Friendica\Network\IHTTPRequest;
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()
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         private function mockL10nT(string $text, $times = null)
68         {
69                 $this->l10nMock->shouldReceive('t')->with($text)->andReturn($text)->times($times);
70         }
71
72         /**
73          * Mocking the DI::l10n()->t() calls for the function checks
74          */
75         private function mockFunctionL10TCalls()
76         {
77                 $this->mockL10nT('Apache mod_rewrite module', 1);
78                 $this->mockL10nT('PDO or MySQLi PHP module', 1);
79                 $this->mockL10nT('libCurl PHP module', 1);
80                 $this->mockL10nT('Error: libCURL PHP module required but not installed.', 1);
81                 $this->mockL10nT('XML PHP module', 1);
82                 $this->mockL10nT('GD graphics PHP module', 1);
83                 $this->mockL10nT('Error: GD graphics PHP module with JPEG support required but not installed.', 1);
84                 $this->mockL10nT('OpenSSL PHP module', 1);
85                 $this->mockL10nT('Error: openssl PHP module required but not installed.', 1);
86                 $this->mockL10nT('mb_string PHP module', 1);
87                 $this->mockL10nT('Error: mb_string PHP module required but not installed.', 1);
88                 $this->mockL10nT('iconv PHP module', 1);
89                 $this->mockL10nT('Error: iconv PHP module required but not installed.', 1);
90                 $this->mockL10nT('POSIX PHP module', 1);
91                 $this->mockL10nT('Error: POSIX PHP module required but not installed.', 1);
92                 $this->mockL10nT('JSON PHP module', 1);
93                 $this->mockL10nT('Error: JSON PHP module required but not installed.', 1);
94                 $this->mockL10nT('File Information PHP module', 1);
95                 $this->mockL10nT('Error: File Information PHP module required but not installed.', 1);
96         }
97
98         private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray)
99         {
100                 $subSet = [$position => [
101                         'title' => $title,
102                         'status' => $status,
103                         'required' => $required,
104                         'error_msg' => null,
105                         'help' => $help]
106                 ];
107
108                 self::assertArraySubset($subSet, $assertionArray, false, "expected subset: " . PHP_EOL . print_r($subSet, true) . PHP_EOL . "current subset: " . print_r($assertionArray, true));
109         }
110
111         /**
112          * Replaces function_exists results with given mocks
113          *
114          * @param array $functions a list from function names and their result
115          */
116         private function setFunctions(array $functions)
117         {
118                 global $phpMock;
119                 $phpMock['function_exists'] = function($function) use ($functions) {
120                         foreach ($functions as $name => $value) {
121                                 if ($function == $name) {
122                                         return $value;
123                                 }
124                         }
125                         return '__phpunit_continue__';
126                 };
127         }
128
129         /**
130          * Replaces class_exist results with given mocks
131          *
132          * @param array $classes a list from class names and their results
133          */
134         private function setClasses(array $classes)
135         {
136                 global $phpMock;
137                 $phpMock['class_exists'] = function($class) use ($classes) {
138                         foreach ($classes as $name => $value) {
139                                 if ($class == $name) {
140                                         return $value;
141                                 }
142                         }
143                         return '__phpunit_continue__';
144                 };
145         }
146
147         /**
148          * @small
149          */
150         public function testCheckKeys()
151         {
152                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
153
154                 $this->setFunctions(['openssl_pkey_new' => false]);
155                 $install = new Installer();
156                 self::assertFalse($install->checkKeys());
157
158                 $this->setFunctions(['openssl_pkey_new' => true]);
159                 $install = new Installer();
160                 self::assertTrue($install->checkKeys());
161         }
162
163         /**
164          * @small
165          */
166         public function testCheckFunctions()
167         {
168                 $this->mockFunctionL10TCalls();
169                 $this->setFunctions(['curl_init' => false, 'imagecreatefromjpeg' => true]);
170                 $install = new Installer();
171                 self::assertFalse($install->checkFunctions());
172                 self::assertCheckExist(3,
173                         'libCurl PHP module',
174                         'Error: libCURL PHP module required but not installed.',
175                         false,
176                         true,
177                         $install->getChecks());
178
179                 $this->mockFunctionL10TCalls();
180                 $this->setFunctions(['imagecreatefromjpeg' => false]);
181                 $install = new Installer();
182                 self::assertFalse($install->checkFunctions());
183                 self::assertCheckExist(4,
184                         'GD graphics PHP module',
185                         'Error: GD graphics PHP module with JPEG support required but not installed.',
186                         false,
187                         true,
188                         $install->getChecks());
189
190                 $this->mockFunctionL10TCalls();
191                 $this->setFunctions(['openssl_public_encrypt' => false]);
192                 $install = new Installer();
193                 self::assertFalse($install->checkFunctions());
194                 self::assertCheckExist(5,
195                         'OpenSSL PHP module',
196                         'Error: openssl PHP module required but not installed.',
197                         false,
198                         true,
199                         $install->getChecks());
200
201                 $this->mockFunctionL10TCalls();
202                 $this->setFunctions(['mb_strlen' => false]);
203                 $install = new Installer();
204                 self::assertFalse($install->checkFunctions());
205                 self::assertCheckExist(6,
206                         'mb_string PHP module',
207                         'Error: mb_string PHP module required but not installed.',
208                         false,
209                         true,
210                         $install->getChecks());
211
212                 $this->mockFunctionL10TCalls();
213                 $this->setFunctions(['iconv_strlen' => false]);
214                 $install = new Installer();
215                 self::assertFalse($install->checkFunctions());
216                 self::assertCheckExist(7,
217                         'iconv PHP module',
218                         'Error: iconv PHP module required but not installed.',
219                         false,
220                         true,
221                         $install->getChecks());
222
223                 $this->mockFunctionL10TCalls();
224                 $this->setFunctions(['posix_kill' => false]);
225                 $install = new Installer();
226                 self::assertFalse($install->checkFunctions());
227                 self::assertCheckExist(8,
228                         'POSIX PHP module',
229                         'Error: POSIX PHP module required but not installed.',
230                         false,
231                         true,
232                         $install->getChecks());
233
234                 $this->mockFunctionL10TCalls();
235                 $this->setFunctions(['json_encode' => false]);
236                 $install = new Installer();
237                 self::assertFalse($install->checkFunctions());
238                 self::assertCheckExist(9,
239                         'JSON PHP module',
240                         'Error: JSON PHP module required but not installed.',
241                         false,
242                         true,
243                         $install->getChecks());
244
245                 $this->mockFunctionL10TCalls();
246                 $this->setFunctions(['finfo_open' => false]);
247                 $install = new Installer();
248                 self::assertFalse($install->checkFunctions());
249                 self::assertCheckExist(10,
250                         'File Information PHP module',
251                         'Error: File Information PHP module required but not installed.',
252                         false,
253                         true,
254                         $install->getChecks());
255
256                 $this->mockFunctionL10TCalls();
257                 $this->setFunctions([
258                         'curl_init' => true,
259                         'imagecreatefromjpeg' => true,
260                         'openssl_public_encrypt' => true,
261                         'mb_strlen' => true,
262                         'iconv_strlen' => true,
263                         'posix_kill' => true,
264                         'json_encode' => true,
265                         'finfo_open' => true,
266                 ]);
267                 $install = new Installer();
268                 self::assertTrue($install->checkFunctions());
269         }
270
271         /**
272          * @small
273          */
274         public function testCheckLocalIni()
275         {
276                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
277
278                 self::assertTrue($this->root->hasChild('config/local.config.php'));
279
280                 $install = new Installer();
281                 self::assertTrue($install->checkLocalIni());
282
283                 $this->delConfigFile('local.config.php');
284
285                 self::assertFalse($this->root->hasChild('config/local.config.php'));
286
287                 $install = new Installer();
288                 self::assertTrue($install->checkLocalIni());
289         }
290
291         /**
292          * @small
293          * @runInSeparateProcess
294          * @preserveGlobalState disabled
295          */
296         public function testCheckHtAccessFail()
297         {
298                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
299
300                 // Mocking the CURL Response
301                 $curlResult = Mockery::mock(CurlResult::class);
302                 $curlResult
303                         ->shouldReceive('getReturnCode')
304                         ->andReturn('404');
305                 $curlResult
306                         ->shouldReceive('getRedirectUrl')
307                         ->andReturn('');
308                 $curlResult
309                         ->shouldReceive('getError')
310                         ->andReturn('test Error');
311
312                 // Mocking the CURL Request
313                 $networkMock = Mockery::mock(IHTTPRequest::class);
314                 $networkMock
315                         ->shouldReceive('fetchFull')
316                         ->with('https://test/install/testrewrite')
317                         ->andReturn($curlResult);
318                 $networkMock
319                         ->shouldReceive('fetchFull')
320                         ->with('http://test/install/testrewrite')
321                         ->andReturn($curlResult);
322
323                 $this->dice->shouldReceive('create')
324                      ->with(IHTTPRequest::class)
325                      ->andReturn($networkMock);
326
327                 DI::init($this->dice);
328
329                 // Mocking that we can use CURL
330                 $this->setFunctions(['curl_init' => true]);
331
332                 $install = new Installer();
333
334                 self::assertFalse($install->checkHtAccess('https://test'));
335                 self::assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
336         }
337
338         /**
339          * @small
340          * @runInSeparateProcess
341          * @preserveGlobalState disabled
342          */
343         public function testCheckHtAccessWork()
344         {
345                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
346
347                 // Mocking the failed CURL Response
348                 $curlResultF = Mockery::mock(CurlResult::class);
349                 $curlResultF
350                         ->shouldReceive('getReturnCode')
351                         ->andReturn('404');
352
353                 // Mocking the working CURL Response
354                 $curlResultW = Mockery::mock(CurlResult::class);
355                 $curlResultW
356                         ->shouldReceive('getReturnCode')
357                         ->andReturn('204');
358
359                 // Mocking the CURL Request
360                 $networkMock = Mockery::mock(IHTTPRequest::class);
361                 $networkMock
362                         ->shouldReceive('fetchFull')
363                         ->with('https://test/install/testrewrite')
364                         ->andReturn($curlResultF);
365                 $networkMock
366                         ->shouldReceive('fetchFull')
367                         ->with('http://test/install/testrewrite')
368                         ->andReturn($curlResultW);
369
370                 $this->dice->shouldReceive('create')
371                            ->with(IHTTPRequest::class)
372                            ->andReturn($networkMock);
373
374                 DI::init($this->dice);
375
376                 // Mocking that we can use CURL
377                 $this->setFunctions(['curl_init' => true]);
378
379                 $install = new Installer();
380
381                 self::assertTrue($install->checkHtAccess('https://test'));
382         }
383
384         /**
385          * @small
386          * @runInSeparateProcess
387          * @preserveGlobalState disabled
388          */
389         public function testImagick()
390         {
391                 static::markTestIncomplete('needs adapted class_exists() mock');
392
393                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
394
395                 $this->setClasses(['Imagick' => true]);
396
397                 $install = new Installer();
398
399                 // even there is no supported type, Imagick should return true (because it is not required)
400                 self::assertTrue($install->checkImagick());
401
402                 self::assertCheckExist(1,
403                         $this->l10nMock->t('ImageMagick supports GIF'),
404                         '',
405                         true,
406                         false,
407                         $install->getChecks());
408         }
409
410         /**
411          * @small
412          * @runInSeparateProcess
413          * @preserveGlobalState disabled
414          */
415         public function testImagickNotFound()
416         {
417                 static::markTestIncomplete('Disabled due not working/difficult mocking global functions - needs more care!');
418
419                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
420
421                 $this->setClasses(['Imagick' => true]);
422
423                 $install = new Installer();
424
425                 // even there is no supported type, Imagick should return true (because it is not required)
426                 self::assertTrue($install->checkImagick());
427                 self::assertCheckExist(1,
428                         $this->l10nMock->t('ImageMagick supports GIF'),
429                         '',
430                         false,
431                         false,
432                         $install->getChecks());
433         }
434
435         public function testImagickNotInstalled()
436         {
437                 $this->setClasses(['Imagick' => false]);
438                 $this->mockL10nT('ImageMagick PHP extension is not installed');
439
440                 $install = new Installer();
441
442                 // even there is no supported type, Imagick should return true (because it is not required)
443                 self::assertTrue($install->checkImagick());
444                 self::assertCheckExist(0,
445                         'ImageMagick PHP extension is not installed',
446                         '',
447                         false,
448                         false,
449                         $install->getChecks());
450         }
451
452         /**
453          * Test the setup of the config cache for installation
454          */
455         public function testSetUpCache()
456         {
457                 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
458
459                 $install = new Installer();
460                 $configCache = Mockery::mock(Cache::class);
461                 $configCache->shouldReceive('set')->with('config', 'php_path', Mockery::any())->once();
462                 $configCache->shouldReceive('set')->with('system', 'basepath', '/test/')->once();
463
464                 $install->setUpCache($configCache, '/test/');
465         }
466 }
467
468 /**
469  * A workaround to replace the PHP native function_exists with a mocked function
470  *
471  * @param string $function_name the Name of the function
472  *
473  * @return bool true or false
474  */
475 function function_exists(string $function_name)
476 {
477         global $phpMock;
478         if (isset($phpMock['function_exists'])) {
479                 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
480                 if ($result !== '__phpunit_continue__') {
481                         return $result;
482                 }
483         }
484         return call_user_func_array('\function_exists', func_get_args());
485 }
486
487 function class_exists($class_name)
488 {
489         global $phpMock;
490         if (isset($phpMock['class_exists'])) {
491                 $result = call_user_func_array($phpMock['class_exists'], func_get_args());
492                 if ($result !== '__phpunit_continue__') {
493                         return $result;
494                 }
495         }
496         return call_user_func_array('\class_exists', func_get_args());
497 }