3 // this is in the same namespace as Install for mocking 'function_exists'
4 namespace Friendica\Core;
6 use Friendica\Test\Util\VFSTrait;
7 use PHPUnit\Framework\TestCase;
10 * @runTestsInSeparateProcesses
11 * @preserveGlobalState disabled
13 class InstallerTest extends TestCase
17 public function setUp()
19 parent::setUp(); // TODO: Change the autogenerated stub
24 private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray)
26 $this->assertArraySubset([$position => [
29 'required' => $required,
36 * Replaces function_exists results with given mocks
38 * @param array $functions a list from function names and their result
40 private function setFunctions($functions)
43 $phpMock['function_exists'] = function($function) use ($functions) {
44 foreach ($functions as $name => $value) {
45 if ($function == $name) {
49 return '__phpunit_continue__';
54 * Replaces class_exist results with given mocks
56 * @param array $classes a list from class names and their results
58 private function setClasses($classes)
61 $phpMock['class_exists'] = function($class) use ($classes) {
62 foreach ($classes as $name => $value) {
63 if ($class == $name) {
67 return '__phpunit_continue__';
74 public function testCheckKeys()
76 $this->setFunctions(['openssl_pkey_new' => false]);
77 $install = new Installer();
78 $this->assertFalse($install->checkKeys());
80 $this->setFunctions(['openssl_pkey_new' => true]);
81 $install = new Installer();
82 $this->assertTrue($install->checkKeys());
88 public function testCheckFunctions()
90 $this->setFunctions(['curl_init' => false]);
91 $install = new Installer();
92 $this->assertFalse($install->checkFunctions());
93 $this->assertCheckExist(3,
94 L10n::t('libCurl PHP module'),
95 L10n::t('Error: libCURL PHP module required but not installed.'),
98 $install->getChecks());
100 $this->setFunctions(['imagecreatefromjpeg' => false]);
101 $install = new Installer();
102 $this->assertFalse($install->checkFunctions());
103 $this->assertCheckExist(4,
104 L10n::t('GD graphics PHP module'),
105 L10n::t('Error: GD graphics PHP module with JPEG support required but not installed.'),
108 $install->getChecks());
110 $this->setFunctions(['openssl_public_encrypt' => false]);
111 $install = new Installer();
112 $this->assertFalse($install->checkFunctions());
113 $this->assertCheckExist(5,
114 L10n::t('OpenSSL PHP module'),
115 L10n::t('Error: openssl PHP module required but not installed.'),
118 $install->getChecks());
120 $this->setFunctions(['mb_strlen' => false]);
121 $install = new Installer();
122 $this->assertFalse($install->checkFunctions());
123 $this->assertCheckExist(6,
124 L10n::t('mb_string PHP module'),
125 L10n::t('Error: mb_string PHP module required but not installed.'),
128 $install->getChecks());
130 $this->setFunctions(['iconv_strlen' => false]);
131 $install = new Installer();
132 $this->assertFalse($install->checkFunctions());
133 $this->assertCheckExist(7,
134 L10n::t('iconv PHP module'),
135 L10n::t('Error: iconv PHP module required but not installed.'),
138 $install->getChecks());
140 $this->setFunctions(['posix_kill' => false]);
141 $install = new Installer();
142 $this->assertFalse($install->checkFunctions());
143 $this->assertCheckExist(8,
144 L10n::t('POSIX PHP module'),
145 L10n::t('Error: POSIX PHP module required but not installed.'),
148 $install->getChecks());
150 $this->setFunctions([
152 'imagecreatefromjpeg' => true,
153 'openssl_public_encrypt' => true,
155 'iconv_strlen' => true,
158 $install = new Installer();
159 $this->assertTrue($install->checkFunctions());
165 public function testCheckLocalIni()
167 $this->assertTrue($this->root->hasChild('config/local.ini.php'));
169 $install = new Installer();
170 $this->assertTrue($install->checkLocalIni());
172 $this->delConfigFile('local.ini.php');
174 $this->assertFalse($this->root->hasChild('config/local.ini.php'));
176 $install = new Installer();
177 $this->assertTrue($install->checkLocalIni());
183 public function testCheckHtAccessFail()
185 // Mocking the CURL Response
186 $curlResult = \Mockery::mock('Friendica\Network\CurlResult');
188 ->shouldReceive('getReturnCode')
191 ->shouldReceive('getRedirectUrl')
194 ->shouldReceive('getError')
195 ->andReturn('test Error');
197 // Mocking the CURL Request
198 $networkMock = \Mockery::mock('alias:Friendica\Util\Network');
200 ->shouldReceive('fetchUrlFull')
201 ->with('https://test/install/testrewrite')
202 ->andReturn($curlResult);
204 ->shouldReceive('fetchUrlFull')
205 ->with('http://test/install/testrewrite')
206 ->andReturn($curlResult);
208 // Mocking that we can use CURL
209 $this->setFunctions(['curl_init' => true]);
211 // needed because of "normalise_link"
212 require_once __DIR__ . '/../../../include/text.php';
214 $install = new Installer();
216 $this->assertFalse($install->checkHtAccess('https://test'));
217 $this->assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
223 public function testCheckHtAccessWork()
225 // Mocking the failed CURL Response
226 $curlResultF = \Mockery::mock('Friendica\Network\CurlResult');
228 ->shouldReceive('getReturnCode')
231 // Mocking the working CURL Response
232 $curlResultW = \Mockery::mock('Friendica\Network\CurlResult');
234 ->shouldReceive('getReturnCode')
237 // Mocking the CURL Request
238 $networkMock = \Mockery::mock('alias:Friendica\Util\Network');
240 ->shouldReceive('fetchUrlFull')
241 ->with('https://test/install/testrewrite')
242 ->andReturn($curlResultF);
244 ->shouldReceive('fetchUrlFull')
245 ->with('http://test/install/testrewrite')
246 ->andReturn($curlResultW);
248 // Mocking that we can use CURL
249 $this->setFunctions(['curl_init' => true]);
251 // needed because of "normalise_link"
252 require_once __DIR__ . '/../../../include/text.php';
254 $install = new Installer();
256 $this->assertTrue($install->checkHtAccess('https://test'));
262 public function testImagick()
264 $imageMock = \Mockery::mock('alias:Friendica\Object\Image');
266 ->shouldReceive('supportedTypes')
267 ->andReturn(['image/gif' => 'gif']);
269 $this->setClasses(['Imagick' => true]);
271 $install = new Installer();
273 // even there is no supported type, Imagick should return true (because it is not required)
274 $this->assertTrue($install->checkImagick());
276 $this->assertCheckExist(1,
277 L10n::t('ImageMagick supports GIF'),
281 $install->getChecks());
287 public function testImagickNotFound()
289 $imageMock = \Mockery::mock('alias:Friendica\Object\Image');
291 ->shouldReceive('supportedTypes')
294 $this->setClasses(['Imagick' => true]);
296 $install = new Installer();
298 // even there is no supported type, Imagick should return true (because it is not required)
299 $this->assertTrue($install->checkImagick());
300 $this->assertCheckExist(1,
301 L10n::t('ImageMagick supports GIF'),
305 $install->getChecks());
308 public function testImagickNotInstalled()
310 $this->setClasses(['Imagick' => false]);
312 $install = new Installer();
314 // even there is no supported type, Imagick should return true (because it is not required)
315 $this->assertTrue($install->checkImagick());
316 $this->assertCheckExist(0,
317 L10n::t('ImageMagick PHP extension is not installed'),
321 $install->getChecks());
326 * A workaround to replace the PHP native function_exists with a mocked function
328 * @param string $function_name the Name of the function
330 * @return bool true or false
332 function function_exists($function_name)
335 if (isset($phpMock['function_exists'])) {
336 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
337 if ($result !== '__phpunit_continue__') {
341 return call_user_func_array('\function_exists', func_get_args());
344 function class_exists($class_name)
347 if (isset($phpMock['class_exists'])) {
348 $result = call_user_func_array($phpMock['class_exists'], func_get_args());
349 if ($result !== '__phpunit_continue__') {
353 return call_user_func_array('\class_exists', func_get_args());