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