]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/InstallerTest.php
d860321d43d1201bb263514f9df5176e97a164e1
[friendica.git] / tests / src / Core / InstallerTest.php
1 <?php
2
3 // this is in the same namespace as Install for mocking 'function_exists'
4 namespace Friendica\Core;
5
6 use Friendica\Test\MockedTest;
7 use Friendica\Test\Util\L10nMockTrait;
8 use Friendica\Test\Util\VFSTrait;
9
10 /**
11  * @runTestsInSeparateProcesses
12  * @preserveGlobalState disabled
13  */
14 class InstallerTest extends MockedTest
15 {
16         use VFSTrait;
17         use L10nMockTrait;
18
19         public function setUp()
20         {
21                 parent::setUp();
22
23                 $this->setUpVfsDir();
24         }
25
26         /**
27          * Mocking the L10n::t() calls for the function checks
28          */
29         private function mockFunctionL10TCalls()
30         {
31                 $this->mockL10nT('Apache mod_rewrite module', 1);
32                 $this->mockL10nT('PDO or MySQLi PHP module', 1);
33                 $this->mockL10nT('libCurl PHP module', 1);
34                 $this->mockL10nT('Error: libCURL PHP module required but not installed.', 1);
35                 $this->mockL10nT('XML PHP module', 1);
36                 $this->mockL10nT('GD graphics PHP module', 1);
37                 $this->mockL10nT('Error: GD graphics PHP module with JPEG support required but not installed.', 1);
38                 $this->mockL10nT('OpenSSL PHP module', 1);
39                 $this->mockL10nT('Error: openssl PHP module required but not installed.', 1);
40                 $this->mockL10nT('mb_string PHP module', 1);
41                 $this->mockL10nT('Error: mb_string PHP module required but not installed.', 1);
42                 $this->mockL10nT('iconv PHP module', 1);
43                 $this->mockL10nT('Error: iconv PHP module required but not installed.', 1);
44                 $this->mockL10nT('POSIX PHP module', 1);
45                 $this->mockL10nT('Error: POSIX PHP module required but not installed.', 1);
46         }
47
48         private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray)
49         {
50                 $this->assertArraySubset([$position => [
51                         'title' => $title,
52                         'status' => $status,
53                         'required' => $required,
54                         'error_msg' => null,
55                         'help' => $help]
56                 ], $assertionArray);
57         }
58
59         /**
60          * Replaces function_exists results with given mocks
61          *
62          * @param array $functions a list from function names and their result
63          */
64         private function setFunctions($functions)
65         {
66                 global $phpMock;
67                 $phpMock['function_exists'] = function($function) use ($functions) {
68                         foreach ($functions as $name => $value) {
69                                 if ($function == $name) {
70                                         return $value;
71                                 }
72                         }
73                         return '__phpunit_continue__';
74                 };
75         }
76
77         /**
78          * Replaces class_exist results with given mocks
79          *
80          * @param array $classes a list from class names and their results
81          */
82         private function setClasses($classes)
83         {
84                 global $phpMock;
85                 $phpMock['class_exists'] = function($class) use ($classes) {
86                         foreach ($classes as $name => $value) {
87                                 if ($class == $name) {
88                                         return $value;
89                                 }
90                         }
91                         return '__phpunit_continue__';
92                 };
93         }
94
95         /**
96          * @small
97          */
98         public function testCheckKeys()
99         {
100                 $this->setFunctions(['openssl_pkey_new' => false]);
101                 $install = new Installer();
102                 $this->assertFalse($install->checkKeys());
103
104                 $this->setFunctions(['openssl_pkey_new' => true]);
105                 $install = new Installer();
106                 $this->assertTrue($install->checkKeys());
107         }
108
109         /**
110          * @small
111          */
112         public function testCheckFunctions()
113         {
114                 $this->mockFunctionL10TCalls();
115                 $this->setFunctions(['curl_init' => false, 'imagecreatefromjpeg' => true]);
116                 $install = new Installer();
117                 $this->assertFalse($install->checkFunctions());
118                 $this->assertCheckExist(3,
119                         'libCurl PHP module',
120                         'Error: libCURL PHP module required but not installed.',
121                         false,
122                         true,
123                         $install->getChecks());
124
125                 $this->mockFunctionL10TCalls();
126                 $this->setFunctions(['imagecreatefromjpeg' => false]);
127                 $install = new Installer();
128                 $this->assertFalse($install->checkFunctions());
129                 $this->assertCheckExist(4,
130                         'GD graphics PHP module',
131                         'Error: GD graphics PHP module with JPEG support required but not installed.',
132                         false,
133                         true,
134                         $install->getChecks());
135
136                 $this->mockFunctionL10TCalls();
137                 $this->setFunctions(['openssl_public_encrypt' => false]);
138                 $install = new Installer();
139                 $this->assertFalse($install->checkFunctions());
140                 $this->assertCheckExist(5,
141                         'OpenSSL PHP module',
142                         'Error: openssl PHP module required but not installed.',
143                         false,
144                         true,
145                         $install->getChecks());
146
147                 $this->mockFunctionL10TCalls();
148                 $this->setFunctions(['mb_strlen' => false]);
149                 $install = new Installer();
150                 $this->assertFalse($install->checkFunctions());
151                 $this->assertCheckExist(6,
152                         'mb_string PHP module',
153                         'Error: mb_string PHP module required but not installed.',
154                         false,
155                         true,
156                         $install->getChecks());
157
158                 $this->mockFunctionL10TCalls();
159                 $this->setFunctions(['iconv_strlen' => false]);
160                 $install = new Installer();
161                 $this->assertFalse($install->checkFunctions());
162                 $this->assertCheckExist(7,
163                         'iconv PHP module',
164                         'Error: iconv PHP module required but not installed.',
165                         false,
166                         true,
167                         $install->getChecks());
168
169                 $this->mockFunctionL10TCalls();
170                 $this->setFunctions(['posix_kill' => false]);
171                 $install = new Installer();
172                 $this->assertFalse($install->checkFunctions());
173                 $this->assertCheckExist(8,
174                         'POSIX PHP module',
175                         'Error: POSIX PHP module required but not installed.',
176                         false,
177                         true,
178                         $install->getChecks());
179
180                 $this->mockFunctionL10TCalls();
181                 $this->setFunctions(['json_encode' => false]);
182                 $install = new Installer();
183                 $this->assertFalse($install->checkFunctions());
184                 $this->assertCheckExist(9,
185                         'JSON PHP module',
186                         'Error: JSON PHP module required but not installed.',
187                         false,
188                         true,
189                         $install->getChecks());
190
191                 $this->mockFunctionL10TCalls();
192                 $this->setFunctions([
193                         'curl_init' => true,
194                         'imagecreatefromjpeg' => true,
195                         'openssl_public_encrypt' => true,
196                         'mb_strlen' => true,
197                         'iconv_strlen' => true,
198                         'posix_kill' => true
199                 ]);
200                 $install = new Installer();
201                 $this->assertTrue($install->checkFunctions());
202         }
203
204         /**
205          * @small
206          */
207         public function testCheckLocalIni()
208         {
209                 $this->assertTrue($this->root->hasChild('config/local.config.php'));
210
211                 $install = new Installer();
212                 $this->assertTrue($install->checkLocalIni());
213
214                 $this->delConfigFile('local.config.php');
215
216                 $this->assertFalse($this->root->hasChild('config/local.config.php'));
217
218                 $install = new Installer();
219                 $this->assertTrue($install->checkLocalIni());
220         }
221
222         /**
223          * @small
224          */
225         public function testCheckHtAccessFail()
226         {
227                 // Mocking the CURL Response
228                 $curlResult = \Mockery::mock('Friendica\Network\CurlResult');
229                 $curlResult
230                         ->shouldReceive('getReturnCode')
231                         ->andReturn('404');
232                 $curlResult
233                         ->shouldReceive('getRedirectUrl')
234                         ->andReturn('');
235                 $curlResult
236                         ->shouldReceive('getError')
237                         ->andReturn('test Error');
238
239                 // Mocking the CURL Request
240                 $networkMock = \Mockery::mock('alias:Friendica\Util\Network');
241                 $networkMock
242                         ->shouldReceive('fetchUrlFull')
243                         ->with('https://test/install/testrewrite')
244                         ->andReturn($curlResult);
245                 $networkMock
246                         ->shouldReceive('fetchUrlFull')
247                         ->with('http://test/install/testrewrite')
248                         ->andReturn($curlResult);
249
250                 // Mocking that we can use CURL
251                 $this->setFunctions(['curl_init' => true]);
252
253                 // needed because of "normalise_link"
254                 require_once __DIR__ . '/../../../include/text.php';
255
256                 $install = new Installer();
257
258                 $this->assertFalse($install->checkHtAccess('https://test'));
259                 $this->assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
260         }
261
262         /**
263          * @small
264          */
265         public function testCheckHtAccessWork()
266         {
267                 // Mocking the failed CURL Response
268                 $curlResultF = \Mockery::mock('Friendica\Network\CurlResult');
269                 $curlResultF
270                         ->shouldReceive('getReturnCode')
271                         ->andReturn('404');
272
273                 // Mocking the working CURL Response
274                 $curlResultW = \Mockery::mock('Friendica\Network\CurlResult');
275                 $curlResultW
276                         ->shouldReceive('getReturnCode')
277                         ->andReturn('204');
278
279                 // Mocking the CURL Request
280                 $networkMock = \Mockery::mock('alias:Friendica\Util\Network');
281                 $networkMock
282                         ->shouldReceive('fetchUrlFull')
283                         ->with('https://test/install/testrewrite')
284                         ->andReturn($curlResultF);
285                 $networkMock
286                         ->shouldReceive('fetchUrlFull')
287                         ->with('http://test/install/testrewrite')
288                         ->andReturn($curlResultW);
289
290                 // Mocking that we can use CURL
291                 $this->setFunctions(['curl_init' => true]);
292
293                 // needed because of "normalise_link"
294                 require_once __DIR__ . '/../../../include/text.php';
295
296                 $install = new Installer();
297
298                 $this->assertTrue($install->checkHtAccess('https://test'));
299         }
300
301         /**
302          * @small
303          */
304         public function testImagick()
305         {
306                 $imageMock = \Mockery::mock('alias:Friendica\Object\Image');
307                 $imageMock
308                         ->shouldReceive('supportedTypes')
309                         ->andReturn(['image/gif' => 'gif']);
310
311                 $this->setClasses(['Imagick' => true]);
312
313                 $install = new Installer();
314
315                 // even there is no supported type, Imagick should return true (because it is not required)
316                 $this->assertTrue($install->checkImagick());
317
318                 $this->assertCheckExist(1,
319                         L10n::t('ImageMagick supports GIF'),
320                         '',
321                         true,
322                         false,
323                         $install->getChecks());
324         }
325
326         /**
327          * @small
328          */
329         public function testImagickNotFound()
330         {
331                 $imageMock = \Mockery::mock('alias:Friendica\Object\Image');
332                 $imageMock
333                         ->shouldReceive('supportedTypes')
334                         ->andReturn([]);
335
336                 $this->setClasses(['Imagick' => true]);
337
338                 $install = new Installer();
339
340                 // even there is no supported type, Imagick should return true (because it is not required)
341                 $this->assertTrue($install->checkImagick());
342                 $this->assertCheckExist(1,
343                         L10n::t('ImageMagick supports GIF'),
344                         '',
345                         false,
346                         false,
347                         $install->getChecks());
348         }
349
350         public function testImagickNotInstalled()
351         {
352                 $this->setClasses(['Imagick' => false]);
353                 $this->mockL10nT('ImageMagick PHP extension is not installed');
354
355                 $install = new Installer();
356
357                 // even there is no supported type, Imagick should return true (because it is not required)
358                 $this->assertTrue($install->checkImagick());
359                 $this->assertCheckExist(0,
360                         'ImageMagick PHP extension is not installed',
361                         '',
362                         false,
363                         false,
364                         $install->getChecks());
365         }
366 }
367
368 /**
369  * A workaround to replace the PHP native function_exists with a mocked function
370  *
371  * @param string $function_name the Name of the function
372  *
373  * @return bool true or false
374  */
375 function function_exists($function_name)
376 {
377         global $phpMock;
378         if (isset($phpMock['function_exists'])) {
379                 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
380                 if ($result !== '__phpunit_continue__') {
381                         return $result;
382                 }
383         }
384         return call_user_func_array('\function_exists', func_get_args());
385 }
386
387 function class_exists($class_name)
388 {
389         global $phpMock;
390         if (isset($phpMock['class_exists'])) {
391                 $result = call_user_func_array($phpMock['class_exists'], func_get_args());
392                 if ($result !== '__phpunit_continue__') {
393                         return $result;
394                 }
395         }
396         return call_user_func_array('\class_exists', func_get_args());
397 }