3 // this is in the same namespace as Install for mocking 'function_exists'
4 namespace Friendica\Core;
6 use Friendica\Core\Config\Cache\ConfigCache;
7 use Friendica\Network\CurlResult;
8 use Friendica\Object\Image;
9 use Friendica\Test\MockedTest;
10 use Friendica\Test\Util\VFSTrait;
11 use Friendica\Util\Network;
12 use Mockery\MockInterface;
14 class InstallerTest extends MockedTest
19 * @var \Friendica\Core\L10n\L10n|MockInterface
23 public function setUp()
29 $this->l10nMock = \Mockery::mock(\Friendica\Core\L10n\L10n::class);
30 L10n::init($this->l10nMock);
33 private function mockL10nT(string $text, $times = null)
35 $this->l10nMock->shouldReceive('t')->with($text)->andReturn($text)->times($times);
39 * Mocking the L10n::t() calls for the function checks
41 private function mockFunctionL10TCalls()
43 $this->mockL10nT('Apache mod_rewrite module', 1);
44 $this->mockL10nT('PDO or MySQLi PHP module', 1);
45 $this->mockL10nT('libCurl PHP module', 1);
46 $this->mockL10nT('Error: libCURL PHP module required but not installed.', 1);
47 $this->mockL10nT('XML PHP module', 1);
48 $this->mockL10nT('GD graphics PHP module', 1);
49 $this->mockL10nT('Error: GD graphics PHP module with JPEG support required but not installed.', 1);
50 $this->mockL10nT('OpenSSL PHP module', 1);
51 $this->mockL10nT('Error: openssl PHP module required but not installed.', 1);
52 $this->mockL10nT('mb_string PHP module', 1);
53 $this->mockL10nT('Error: mb_string PHP module required but not installed.', 1);
54 $this->mockL10nT('iconv PHP module', 1);
55 $this->mockL10nT('Error: iconv PHP module required but not installed.', 1);
56 $this->mockL10nT('POSIX PHP module', 1);
57 $this->mockL10nT('Error: POSIX PHP module required but not installed.', 1);
58 $this->mockL10nT('JSON PHP module', 1);
59 $this->mockL10nT('Error: JSON PHP module required but not installed.', 1);
60 $this->mockL10nT('File Information PHP module', 1);
61 $this->mockL10nT('Error: File Information PHP module required but not installed.', 1);
64 private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray)
66 $this->assertArraySubset([$position => [
69 'required' => $required,
76 * Replaces function_exists results with given mocks
78 * @param array $functions a list from function names and their result
80 private function setFunctions($functions)
83 $phpMock['function_exists'] = function($function) use ($functions) {
84 foreach ($functions as $name => $value) {
85 if ($function == $name) {
89 return '__phpunit_continue__';
94 * Replaces class_exist results with given mocks
96 * @param array $classes a list from class names and their results
98 private function setClasses($classes)
101 $phpMock['class_exists'] = function($class) use ($classes) {
102 foreach ($classes as $name => $value) {
103 if ($class == $name) {
107 return '__phpunit_continue__';
114 public function testCheckKeys()
116 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
118 $this->setFunctions(['openssl_pkey_new' => false]);
119 $install = new Installer();
120 $this->assertFalse($install->checkKeys());
122 $this->setFunctions(['openssl_pkey_new' => true]);
123 $install = new Installer();
124 $this->assertTrue($install->checkKeys());
130 public function testCheckFunctions()
132 $this->mockFunctionL10TCalls();
133 $this->setFunctions(['curl_init' => false, 'imagecreatefromjpeg' => true]);
134 $install = new Installer();
135 $this->assertFalse($install->checkFunctions());
136 $this->assertCheckExist(3,
137 'libCurl PHP module',
138 'Error: libCURL PHP module required but not installed.',
141 $install->getChecks());
143 $this->mockFunctionL10TCalls();
144 $this->setFunctions(['imagecreatefromjpeg' => false]);
145 $install = new Installer();
146 $this->assertFalse($install->checkFunctions());
147 $this->assertCheckExist(4,
148 'GD graphics PHP module',
149 'Error: GD graphics PHP module with JPEG support required but not installed.',
152 $install->getChecks());
154 $this->mockFunctionL10TCalls();
155 $this->setFunctions(['openssl_public_encrypt' => false]);
156 $install = new Installer();
157 $this->assertFalse($install->checkFunctions());
158 $this->assertCheckExist(5,
159 'OpenSSL PHP module',
160 'Error: openssl PHP module required but not installed.',
163 $install->getChecks());
165 $this->mockFunctionL10TCalls();
166 $this->setFunctions(['mb_strlen' => false]);
167 $install = new Installer();
168 $this->assertFalse($install->checkFunctions());
169 $this->assertCheckExist(6,
170 'mb_string PHP module',
171 'Error: mb_string PHP module required but not installed.',
174 $install->getChecks());
176 $this->mockFunctionL10TCalls();
177 $this->setFunctions(['iconv_strlen' => false]);
178 $install = new Installer();
179 $this->assertFalse($install->checkFunctions());
180 $this->assertCheckExist(7,
182 'Error: iconv PHP module required but not installed.',
185 $install->getChecks());
187 $this->mockFunctionL10TCalls();
188 $this->setFunctions(['posix_kill' => false]);
189 $install = new Installer();
190 $this->assertFalse($install->checkFunctions());
191 $this->assertCheckExist(8,
193 'Error: POSIX PHP module required but not installed.',
196 $install->getChecks());
198 $this->mockFunctionL10TCalls();
199 $this->setFunctions(['json_encode' => false]);
200 $install = new Installer();
201 $this->assertFalse($install->checkFunctions());
202 $this->assertCheckExist(9,
204 'Error: JSON PHP module required but not installed.',
207 $install->getChecks());
209 $this->mockFunctionL10TCalls();
210 $this->setFunctions(['finfo_open' => false]);
211 $install = new Installer();
212 $this->assertFalse($install->checkFunctions());
213 $this->assertCheckExist(10,
214 'File Information PHP module',
215 'Error: File Information PHP module required but not installed.',
218 $install->getChecks());
220 $this->mockFunctionL10TCalls();
221 $this->setFunctions([
223 'imagecreatefromjpeg' => true,
224 'openssl_public_encrypt' => true,
226 'iconv_strlen' => true,
227 'posix_kill' => true,
228 'json_encode' => true,
229 'finfo_open' => true,
231 $install = new Installer();
232 $this->assertTrue($install->checkFunctions());
238 public function testCheckLocalIni()
240 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
242 $this->assertTrue($this->root->hasChild('config/local.config.php'));
244 $install = new Installer();
245 $this->assertTrue($install->checkLocalIni());
247 $this->delConfigFile('local.config.php');
249 $this->assertFalse($this->root->hasChild('config/local.config.php'));
251 $install = new Installer();
252 $this->assertTrue($install->checkLocalIni());
257 * @runInSeparateProcess
258 * @preserveGlobalState disabled
260 public function testCheckHtAccessFail()
262 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
264 // Mocking the CURL Response
265 $curlResult = \Mockery::mock(CurlResult::class);
267 ->shouldReceive('getReturnCode')
270 ->shouldReceive('getRedirectUrl')
273 ->shouldReceive('getError')
274 ->andReturn('test Error');
276 // Mocking the CURL Request
277 $networkMock = \Mockery::mock('alias:' . Network::class);
279 ->shouldReceive('fetchUrlFull')
280 ->with('https://test/install/testrewrite')
281 ->andReturn($curlResult);
283 ->shouldReceive('fetchUrlFull')
284 ->with('http://test/install/testrewrite')
285 ->andReturn($curlResult);
287 // Mocking that we can use CURL
288 $this->setFunctions(['curl_init' => true]);
290 $install = new Installer();
292 $this->assertFalse($install->checkHtAccess('https://test'));
293 $this->assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
298 * @runInSeparateProcess
299 * @preserveGlobalState disabled
301 public function testCheckHtAccessWork()
303 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
305 // Mocking the failed CURL Response
306 $curlResultF = \Mockery::mock(CurlResult::class);
308 ->shouldReceive('getReturnCode')
311 // Mocking the working CURL Response
312 $curlResultW = \Mockery::mock(CurlResult::class);
314 ->shouldReceive('getReturnCode')
317 // Mocking the CURL Request
318 $networkMock = \Mockery::mock('alias:' . Network::class);
320 ->shouldReceive('fetchUrlFull')
321 ->with('https://test/install/testrewrite')
322 ->andReturn($curlResultF);
324 ->shouldReceive('fetchUrlFull')
325 ->with('http://test/install/testrewrite')
326 ->andReturn($curlResultW);
328 // Mocking that we can use CURL
329 $this->setFunctions(['curl_init' => true]);
331 // needed because of "normalise_link"
332 require_once __DIR__ . '/../../../include/text.php';
334 $install = new Installer();
336 $this->assertTrue($install->checkHtAccess('https://test'));
341 * @runInSeparateProcess
342 * @preserveGlobalState disabled
344 public function testImagick()
346 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
348 $imageMock = \Mockery::mock('alias:'. Image::class);
350 ->shouldReceive('supportedTypes')
351 ->andReturn(['image/gif' => 'gif']);
353 $this->setClasses(['Imagick' => true]);
355 $install = new Installer();
357 // even there is no supported type, Imagick should return true (because it is not required)
358 $this->assertTrue($install->checkImagick());
360 $this->assertCheckExist(1,
361 L10n::t('ImageMagick supports GIF'),
365 $install->getChecks());
370 * @runInSeparateProcess
371 * @preserveGlobalState disabled
373 public function testImagickNotFound()
375 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
377 $imageMock = \Mockery::mock('alias:' . Image::class);
379 ->shouldReceive('supportedTypes')
382 $this->setClasses(['Imagick' => true]);
384 $install = new Installer();
386 // even there is no supported type, Imagick should return true (because it is not required)
387 $this->assertTrue($install->checkImagick());
388 $this->assertCheckExist(1,
389 L10n::t('ImageMagick supports GIF'),
393 $install->getChecks());
396 public function testImagickNotInstalled()
398 $this->setClasses(['Imagick' => false]);
399 $this->mockL10nT('ImageMagick PHP extension is not installed');
401 $install = new Installer();
403 // even there is no supported type, Imagick should return true (because it is not required)
404 $this->assertTrue($install->checkImagick());
405 $this->assertCheckExist(0,
406 'ImageMagick PHP extension is not installed',
410 $install->getChecks());
414 * Test the setup of the config cache for installation
416 public function testSetUpCache()
418 $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
420 $install = new Installer();
421 $configCache = \Mockery::mock(ConfigCache::class);
422 $configCache->shouldReceive('set')->with('config', 'php_path', \Mockery::any())->once();
423 $configCache->shouldReceive('set')->with('system', 'basepath', '/test/')->once();
425 $install->setUpCache($configCache, '/test/');
430 * A workaround to replace the PHP native function_exists with a mocked function
432 * @param string $function_name the Name of the function
434 * @return bool true or false
436 function function_exists($function_name)
439 if (isset($phpMock['function_exists'])) {
440 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
441 if ($result !== '__phpunit_continue__') {
445 return call_user_func_array('\function_exists', func_get_args());
448 function class_exists($class_name)
451 if (isset($phpMock['class_exists'])) {
452 $result = call_user_func_array($phpMock['class_exists'], func_get_args());
453 if ($result !== '__phpunit_continue__') {
457 return call_user_func_array('\class_exists', func_get_args());