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(12,
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,
302 'gmp_strval' => true,
304 $install = new Installer();
305 self::assertTrue($install->checkFunctions());
311 public function testCheckLocalIni()
313 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
315 self::assertTrue($this->root->hasChild('config/local.config.php'));
317 $install = new Installer();
318 self::assertTrue($install->checkLocalIni());
320 $this->delConfigFile('local.config.php');
322 self::assertFalse($this->root->hasChild('config/local.config.php'));
324 $install = new Installer();
325 self::assertTrue($install->checkLocalIni());
330 * @runInSeparateProcess
331 * @preserveGlobalState disabled
333 public function testCheckHtAccessFail()
335 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
337 // Mocking the CURL Response
338 $IHTTPResult = Mockery::mock(ICanHandleHttpResponses::class);
340 ->shouldReceive('getReturnCode')
343 ->shouldReceive('getRedirectUrl')
346 ->shouldReceive('getError')
347 ->andReturn('test Error');
349 // Mocking the CURL Request
350 $networkMock = Mockery::mock(ICanSendHttpRequests::class);
352 ->shouldReceive('fetchFull')
353 ->with('https://test/install/testrewrite')
354 ->andReturn($IHTTPResult);
356 ->shouldReceive('fetchFull')
357 ->with('http://test/install/testrewrite')
358 ->andReturn($IHTTPResult);
360 $this->dice->shouldReceive('create')
361 ->with(ICanSendHttpRequests::class)
362 ->andReturn($networkMock);
364 DI::init($this->dice);
366 // Mocking that we can use CURL
367 $this->setFunctions(['curl_init' => true]);
369 $install = new Installer();
371 self::assertFalse($install->checkHtAccess('https://test'));
372 self::assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
377 * @runInSeparateProcess
378 * @preserveGlobalState disabled
380 public function testCheckHtAccessWork()
382 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
384 // Mocking the failed CURL Response
385 $IHTTPResultF = Mockery::mock(ICanHandleHttpResponses::class);
387 ->shouldReceive('getReturnCode')
390 // Mocking the working CURL Response
391 $IHTTPResultW = Mockery::mock(ICanHandleHttpResponses::class);
393 ->shouldReceive('getReturnCode')
396 // Mocking the CURL Request
397 $networkMock = Mockery::mock(ICanSendHttpRequests::class);
399 ->shouldReceive('fetchFull')
400 ->with('https://test/install/testrewrite')
401 ->andReturn($IHTTPResultF);
403 ->shouldReceive('fetchFull')
404 ->with('http://test/install/testrewrite')
405 ->andReturn($IHTTPResultW);
407 $this->dice->shouldReceive('create')
408 ->with(ICanSendHttpRequests::class)
409 ->andReturn($networkMock);
411 DI::init($this->dice);
413 // Mocking that we can use CURL
414 $this->setFunctions(['curl_init' => true]);
416 $install = new Installer();
418 self::assertTrue($install->checkHtAccess('https://test'));
423 * @runInSeparateProcess
424 * @preserveGlobalState disabled
426 public function testImagick()
428 static::markTestIncomplete('needs adapted class_exists() mock');
430 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
432 $this->setClasses(['Imagick' => true]);
434 $install = new Installer();
436 // even there is no supported type, Imagick should return true (because it is not required)
437 self::assertTrue($install->checkImagick());
439 self::assertCheckExist(1,
440 $this->l10nMock->t('ImageMagick supports GIF'),
444 $install->getChecks());
449 * @runInSeparateProcess
450 * @preserveGlobalState disabled
452 public function testImagickNotFound()
454 static::markTestIncomplete('Disabled due not working/difficult mocking global functions - needs more care!');
456 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
458 $this->setClasses(['Imagick' => true]);
460 $install = new Installer();
462 // even there is no supported type, Imagick should return true (because it is not required)
463 self::assertTrue($install->checkImagick());
464 self::assertCheckExist(1,
465 $this->l10nMock->t('ImageMagick supports GIF'),
469 $install->getChecks());
472 public function testImagickNotInstalled()
474 $this->setClasses(['Imagick' => false]);
475 $this->mockL10nT('ImageMagick PHP extension is not installed');
477 $install = new Installer();
479 // even there is no supported type, Imagick should return true (because it is not required)
480 self::assertTrue($install->checkImagick());
481 self::assertCheckExist(0,
482 'ImageMagick PHP extension is not installed',
486 $install->getChecks());
490 * Test the setup of the config cache for installation
491 * @doesNotPerformAssertions
493 public function testSetUpCache()
495 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
497 $install = new Installer();
498 $configCache = Mockery::mock(Cache::class);
499 $configCache->shouldReceive('set')->with('config', 'php_path', Mockery::any())->once();
500 $configCache->shouldReceive('set')->with('system', 'basepath', '/test/')->once();
502 $install->setUpCache($configCache, '/test/');
507 * A workaround to replace the PHP native function_exists with a mocked function
509 * @param string $function_name the Name of the function
511 * @return bool true or false
513 function function_exists(string $function_name)
516 if (isset($phpMock['function_exists'])) {
517 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
518 if ($result !== '__phpunit_continue__') {
522 return call_user_func_array('\function_exists', func_get_args());
525 function class_exists($class_name)
528 if (isset($phpMock['class_exists'])) {
529 $result = call_user_func_array($phpMock['class_exists'], func_get_args());
530 if ($result !== '__phpunit_continue__') {
534 return call_user_func_array('\class_exists', func_get_args());