3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 /// @todo this is in the same namespace as Install for mocking 'function_exists'
23 namespace Friendica\Core;
26 use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
27 use Friendica\Core\Config\ValueObject\Cache;
29 use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
30 use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
31 use Friendica\Test\MockedTest;
32 use Friendica\Test\Util\VFSTrait;
34 use Mockery\MockInterface;
36 class InstallerTest extends MockedTest
39 use ArraySubsetAsserts;
42 * @var L10n|MockInterface
46 * @var Dice|MockInterface
50 protected function setUp(): void
56 $this->l10nMock = Mockery::mock(L10n::class);
58 /** @var Dice|MockInterface $dice */
59 $this->dice = Mockery::mock(Dice::class)->makePartial();
60 $this->dice = $this->dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
62 $this->dice->shouldReceive('create')
64 ->andReturn($this->l10nMock);
66 DI::init($this->dice);
69 public static function tearDownAfterClass(): void
75 parent::tearDownAfterClass();
78 private function mockL10nT(string $text, $times = null)
80 $this->l10nMock->shouldReceive('t')->with($text)->andReturn($text)->times($times);
84 * Mocking the DI::l10n()->t() calls for the function checks
86 private function mockFunctionL10TCalls()
88 $this->mockL10nT('Apache mod_rewrite module', 1);
89 $this->mockL10nT('PDO or MySQLi PHP module', 1);
90 $this->mockL10nT('libCurl PHP module', 1);
91 $this->mockL10nT('Error: libCURL PHP module required but not installed.', 1);
92 $this->mockL10nT('XML PHP module', 1);
93 $this->mockL10nT('GD graphics PHP module', 1);
94 $this->mockL10nT('Error: GD graphics PHP module with JPEG support required but not installed.', 1);
95 $this->mockL10nT('OpenSSL PHP module', 1);
96 $this->mockL10nT('Error: openssl PHP module required but not installed.', 1);
97 $this->mockL10nT('mb_string PHP module', 1);
98 $this->mockL10nT('Error: mb_string PHP module required but not installed.', 1);
99 $this->mockL10nT('iconv PHP module', 1);
100 $this->mockL10nT('Error: iconv PHP module required but not installed.', 1);
101 $this->mockL10nT('POSIX PHP module', 1);
102 $this->mockL10nT('Error: POSIX PHP module required but not installed.', 1);
103 $this->mockL10nT('JSON PHP module', 1);
104 $this->mockL10nT('Error: JSON PHP module required but not installed.', 1);
105 $this->mockL10nT('File Information PHP module', 1);
106 $this->mockL10nT('Error: File Information PHP module required but not installed.', 1);
107 $this->mockL10nT('GNU Multiple Precision PHP module', 1);
108 $this->mockL10nT('Error: GNU Multiple Precision PHP module required but not installed.', 1);
109 $this->mockL10nT('Program execution functions', 1);
110 $this->mockL10nT('Error: Program execution functions (proc_open) required but not enabled.', 1);
113 private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray)
115 $subSet = [$position => [
118 'required' => $required,
123 self::assertArraySubset($subSet, $assertionArray, false, "expected subset: " . PHP_EOL . print_r($subSet, true) . PHP_EOL . "current subset: " . print_r($assertionArray, true));
127 * Replaces function_exists results with given mocks
129 * @param array $functions a list from function names and their result
131 private function setFunctions(array $functions)
134 $phpMock['function_exists'] = function($function) use ($functions) {
135 foreach ($functions as $name => $value) {
136 if ($function == $name) {
140 return '__phpunit_continue__';
145 * Replaces class_exist results with given mocks
147 * @param array $classes a list from class names and their results
149 private function setClasses(array $classes)
152 $phpMock['class_exists'] = function($class) use ($classes) {
153 foreach ($classes as $name => $value) {
154 if ($class == $name) {
158 return '__phpunit_continue__';
165 public function testCheckKeys()
167 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
169 $this->setFunctions(['openssl_pkey_new' => false]);
170 $install = new Installer();
171 self::assertFalse($install->checkKeys());
173 $this->setFunctions(['openssl_pkey_new' => true]);
174 $install = new Installer();
175 self::assertTrue($install->checkKeys());
181 public function testCheckFunctions()
183 $this->mockFunctionL10TCalls();
184 $this->setFunctions(['curl_init' => false, 'imagecreatefromjpeg' => true]);
185 $install = new Installer();
186 self::assertFalse($install->checkFunctions());
187 self::assertCheckExist(3,
188 'libCurl PHP module',
189 'Error: libCURL PHP module required but not installed.',
192 $install->getChecks());
194 $this->mockFunctionL10TCalls();
195 $this->setFunctions(['imagecreatefromjpeg' => false]);
196 $install = new Installer();
197 self::assertFalse($install->checkFunctions());
198 self::assertCheckExist(4,
199 'GD graphics PHP module',
200 'Error: GD graphics PHP module with JPEG support required but not installed.',
203 $install->getChecks());
205 $this->mockFunctionL10TCalls();
206 $this->setFunctions(['openssl_public_encrypt' => false]);
207 $install = new Installer();
208 self::assertFalse($install->checkFunctions());
209 self::assertCheckExist(5,
210 'OpenSSL PHP module',
211 'Error: openssl PHP module required but not installed.',
214 $install->getChecks());
216 $this->mockFunctionL10TCalls();
217 $this->setFunctions(['mb_strlen' => false]);
218 $install = new Installer();
219 self::assertFalse($install->checkFunctions());
220 self::assertCheckExist(6,
221 'mb_string PHP module',
222 'Error: mb_string PHP module required but not installed.',
225 $install->getChecks());
227 $this->mockFunctionL10TCalls();
228 $this->setFunctions(['iconv_strlen' => false]);
229 $install = new Installer();
230 self::assertFalse($install->checkFunctions());
231 self::assertCheckExist(7,
233 'Error: iconv PHP module required but not installed.',
236 $install->getChecks());
238 $this->mockFunctionL10TCalls();
239 $this->setFunctions(['posix_kill' => false]);
240 $install = new Installer();
241 self::assertFalse($install->checkFunctions());
242 self::assertCheckExist(8,
244 'Error: POSIX PHP module required but not installed.',
247 $install->getChecks());
249 $this->mockFunctionL10TCalls();
250 $this->setFunctions(['proc_open' => false]);
251 $install = new Installer();
252 self::assertFalse($install->checkFunctions());
253 self::assertCheckExist(9,
254 'Program execution functions',
255 'Error: Program execution functions (proc_open) required but not enabled.',
258 $install->getChecks());
259 $this->mockFunctionL10TCalls();
260 $this->setFunctions(['json_encode' => false]);
261 $install = new Installer();
262 self::assertFalse($install->checkFunctions());
263 self::assertCheckExist(10,
265 'Error: JSON PHP module required but not installed.',
268 $install->getChecks());
270 $this->mockFunctionL10TCalls();
271 $this->setFunctions(['finfo_open' => false]);
272 $install = new Installer();
273 self::assertFalse($install->checkFunctions());
274 self::assertCheckExist(11,
275 'File Information PHP module',
276 'Error: File Information PHP module required but not installed.',
279 $install->getChecks());
281 $this->mockFunctionL10TCalls();
282 $this->setFunctions(['gmp_strval' => false]);
283 $install = new Installer();
284 self::assertFalse($install->checkFunctions());
285 self::assertCheckExist(11,
286 'GNU Multiple Precision PHP module',
287 'Error: GNU Multiple Precision PHP module required but not installed.',
290 $install->getChecks());
292 $this->mockFunctionL10TCalls();
293 $this->setFunctions([
295 'imagecreatefromjpeg' => true,
296 'openssl_public_encrypt' => true,
298 'iconv_strlen' => true,
299 'posix_kill' => true,
300 'json_encode' => true,
301 'finfo_open' => true,
303 $install = new Installer();
304 self::assertTrue($install->checkFunctions());
310 public function testCheckLocalIni()
312 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
314 self::assertTrue($this->root->hasChild('config/local.config.php'));
316 $install = new Installer();
317 self::assertTrue($install->checkLocalIni());
319 $this->delConfigFile('local.config.php');
321 self::assertFalse($this->root->hasChild('config/local.config.php'));
323 $install = new Installer();
324 self::assertTrue($install->checkLocalIni());
329 * @runInSeparateProcess
330 * @preserveGlobalState disabled
332 public function testCheckHtAccessFail()
334 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
336 // Mocking the CURL Response
337 $IHTTPResult = Mockery::mock(ICanHandleHttpResponses::class);
339 ->shouldReceive('getReturnCode')
342 ->shouldReceive('getRedirectUrl')
345 ->shouldReceive('getError')
346 ->andReturn('test Error');
348 // Mocking the CURL Request
349 $networkMock = Mockery::mock(ICanSendHttpRequests::class);
351 ->shouldReceive('fetchFull')
352 ->with('https://test/install/testrewrite')
353 ->andReturn($IHTTPResult);
355 ->shouldReceive('fetchFull')
356 ->with('http://test/install/testrewrite')
357 ->andReturn($IHTTPResult);
359 $this->dice->shouldReceive('create')
360 ->with(ICanSendHttpRequests::class)
361 ->andReturn($networkMock);
363 DI::init($this->dice);
365 // Mocking that we can use CURL
366 $this->setFunctions(['curl_init' => true]);
368 $install = new Installer();
370 self::assertFalse($install->checkHtAccess('https://test'));
371 self::assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
376 * @runInSeparateProcess
377 * @preserveGlobalState disabled
379 public function testCheckHtAccessWork()
381 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
383 // Mocking the failed CURL Response
384 $IHTTPResultF = Mockery::mock(ICanHandleHttpResponses::class);
386 ->shouldReceive('getReturnCode')
389 // Mocking the working CURL Response
390 $IHTTPResultW = Mockery::mock(ICanHandleHttpResponses::class);
392 ->shouldReceive('getReturnCode')
395 // Mocking the CURL Request
396 $networkMock = Mockery::mock(ICanSendHttpRequests::class);
398 ->shouldReceive('fetchFull')
399 ->with('https://test/install/testrewrite')
400 ->andReturn($IHTTPResultF);
402 ->shouldReceive('fetchFull')
403 ->with('http://test/install/testrewrite')
404 ->andReturn($IHTTPResultW);
406 $this->dice->shouldReceive('create')
407 ->with(ICanSendHttpRequests::class)
408 ->andReturn($networkMock);
410 DI::init($this->dice);
412 // Mocking that we can use CURL
413 $this->setFunctions(['curl_init' => true]);
415 $install = new Installer();
417 self::assertTrue($install->checkHtAccess('https://test'));
422 * @runInSeparateProcess
423 * @preserveGlobalState disabled
425 public function testImagick()
427 static::markTestIncomplete('needs adapted class_exists() mock');
429 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
431 $this->setClasses(['Imagick' => true]);
433 $install = new Installer();
435 // even there is no supported type, Imagick should return true (because it is not required)
436 self::assertTrue($install->checkImagick());
438 self::assertCheckExist(1,
439 $this->l10nMock->t('ImageMagick supports GIF'),
443 $install->getChecks());
448 * @runInSeparateProcess
449 * @preserveGlobalState disabled
451 public function testImagickNotFound()
453 static::markTestIncomplete('Disabled due not working/difficult mocking global functions - needs more care!');
455 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
457 $this->setClasses(['Imagick' => true]);
459 $install = new Installer();
461 // even there is no supported type, Imagick should return true (because it is not required)
462 self::assertTrue($install->checkImagick());
463 self::assertCheckExist(1,
464 $this->l10nMock->t('ImageMagick supports GIF'),
468 $install->getChecks());
471 public function testImagickNotInstalled()
473 $this->setClasses(['Imagick' => false]);
474 $this->mockL10nT('ImageMagick PHP extension is not installed');
476 $install = new Installer();
478 // even there is no supported type, Imagick should return true (because it is not required)
479 self::assertTrue($install->checkImagick());
480 self::assertCheckExist(0,
481 'ImageMagick PHP extension is not installed',
485 $install->getChecks());
489 * Test the setup of the config cache for installation
490 * @doesNotPerformAssertions
492 public function testSetUpCache()
494 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
496 $install = new Installer();
497 $configCache = Mockery::mock(Cache::class);
498 $configCache->shouldReceive('set')->with('config', 'php_path', Mockery::any())->once();
499 $configCache->shouldReceive('set')->with('system', 'basepath', '/test/')->once();
501 $install->setUpCache($configCache, '/test/');
506 * A workaround to replace the PHP native function_exists with a mocked function
508 * @param string $function_name the Name of the function
510 * @return bool true or false
512 function function_exists(string $function_name)
515 if (isset($phpMock['function_exists'])) {
516 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
517 if ($result !== '__phpunit_continue__') {
521 return call_user_func_array('\function_exists', func_get_args());
524 function class_exists($class_name)
527 if (isset($phpMock['class_exists'])) {
528 $result = call_user_func_array($phpMock['class_exists'], func_get_args());
529 if ($result !== '__phpunit_continue__') {
533 return call_user_func_array('\class_exists', func_get_args());