]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/InstallTest.php
b44104d5f800ce19f30fef31190e6ebb127dd8ac
[friendica.git] / tests / src / Core / InstallTest.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\Util\VFSTrait;
7 use PHPUnit\Framework\TestCase;
8
9 /**
10  * @runTestsInSeparateProcesses
11  * @preserveGlobalState disabled
12  */
13 class InstallTest extends TestCase
14 {
15         use VFSTrait;
16
17         public function setUp()
18         {
19                 parent::setUp(); // TODO: Change the autogenerated stub
20
21                 $this->setUpVfsDir();
22         }
23
24         private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray)
25         {
26                 $this->assertArraySubset([$position => [
27                         'title' => $title,
28                         'status' => $status,
29                         'required' => $required,
30                         'error_msg' => null,
31                         'help' => $help]
32                 ], $assertionArray);
33         }
34
35         /**
36          * Replaces function_exists results with given mocks
37          *
38          * @param array $functions a list from function names and their result
39          */
40         private function setFunctions($functions)
41         {
42                 global $phpMock;
43                 $phpMock['function_exists'] = function($function) use ($functions) {
44                         foreach ($functions as $name => $value) {
45                                 if ($function == $name) {
46                                         return $value;
47                                 }
48                         }
49                         return '__phpunit_continue__';
50                 };
51         }
52
53         /**
54          * Replaces class_exist results with given mocks
55          *
56          * @param array $classes a list from class names and their results
57          */
58         private function setClasses($classes)
59         {
60                 global $phpMock;
61                 $phpMock['class_exists'] = function($class) use ($classes) {
62                         foreach ($classes as $name => $value) {
63                                 if ($class == $name) {
64                                         return $value;
65                                 }
66                         }
67                         return '__phpunit_continue__';
68                 };
69         }
70
71         /**
72          * @small
73          */
74         public function testCheckKeys()
75         {
76                 $this->setFunctions(['openssl_pkey_new' => false]);
77                 $install = new Install();
78                 $this->assertFalse($install->checkKeys());
79
80                 $this->setFunctions(['openssl_pkey_new' => true]);
81                 $install = new Install();
82                 $this->assertTrue($install->checkKeys());
83         }
84
85         /**
86          * @small
87          */
88         public function testCheckFunctions()
89         {
90                 $this->setFunctions(['curl_init' => false]);
91                 $install = new Install();
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.'),
96                         false,
97                         true,
98                         $install->getChecks());
99
100                 $this->setFunctions(['imagecreatefromjpeg' => false]);
101                 $install = new Install();
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.'),
106                         false,
107                         true,
108                         $install->getChecks());
109
110                 $this->setFunctions(['openssl_public_encrypt' => false]);
111                 $install = new Install();
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.'),
116                         false,
117                         true,
118                         $install->getChecks());
119
120                 $this->setFunctions(['mb_strlen' => false]);
121                 $install = new Install();
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.'),
126                         false,
127                         true,
128                         $install->getChecks());
129
130                 $this->setFunctions(['iconv_strlen' => false]);
131                 $install = new Install();
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.'),
136                         false,
137                         true,
138                         $install->getChecks());
139
140                 $this->setFunctions(['posix_kill' => false]);
141                 $install = new Install();
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.'),
146                         false,
147                         true,
148                         $install->getChecks());
149
150                 $this->setFunctions([
151                         'curl_init' => true,
152                         'imagecreatefromjpeg' => true,
153                         'openssl_public_encrypt' => true,
154                         'mb_strlen' => true,
155                         'iconv_strlen' => true,
156                         'posix_kill' => true
157                 ]);
158                 $install = new Install();
159                 $this->assertTrue($install->checkFunctions());
160         }
161
162         /**
163          * @small
164          */
165         public function testCheckLocalIni()
166         {
167                 $this->assertTrue($this->root->hasChild('config/local.ini.php'));
168
169                 $install = new Install();
170                 $this->assertTrue($install->checkLocalIni());
171
172                 $this->delConfigFile('local.ini.php');
173
174                 $this->assertFalse($this->root->hasChild('config/local.ini.php'));
175
176                 $install = new Install();
177                 $this->assertTrue($install->checkLocalIni());
178         }
179
180         /**
181          * @small
182          */
183         public function testCheckHtAccessFail()
184         {
185                 // Mocking the CURL Response
186                 $curlResult = \Mockery::mock('Friendica\Network\CurlResult');
187                 $curlResult
188                         ->shouldReceive('getReturnCode')
189                         ->andReturn('404');
190                 $curlResult
191                         ->shouldReceive('getRedirectUrl')
192                         ->andReturn('');
193                 $curlResult
194                         ->shouldReceive('getError')
195                         ->andReturn('test Error');
196
197                 // Mocking the CURL Request
198                 $networkMock = \Mockery::mock('alias:Friendica\Util\Network');
199                 $networkMock
200                         ->shouldReceive('fetchUrlFull')
201                         ->with('https://test/install/testrewrite')
202                         ->andReturn($curlResult);
203                 $networkMock
204                         ->shouldReceive('fetchUrlFull')
205                         ->with('http://test/install/testrewrite')
206                         ->andReturn($curlResult);
207
208                 // Mocking that we can use CURL
209                 $this->setFunctions(['curl_init' => true]);
210
211                 // needed because of "normalise_link"
212                 require_once __DIR__ . '/../../../include/text.php';
213
214                 $install = new Install();
215
216                 $this->assertFalse($install->checkHtAccess('https://test'));
217                 $this->assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
218         }
219
220         /**
221          * @small
222          */
223         public function testCheckHtAccessWork()
224         {
225                 // Mocking the failed CURL Response
226                 $curlResultF = \Mockery::mock('Friendica\Network\CurlResult');
227                 $curlResultF
228                         ->shouldReceive('getReturnCode')
229                         ->andReturn('404');
230
231                 // Mocking the working CURL Response
232                 $curlResultW = \Mockery::mock('Friendica\Network\CurlResult');
233                 $curlResultW
234                         ->shouldReceive('getReturnCode')
235                         ->andReturn('204');
236
237                 // Mocking the CURL Request
238                 $networkMock = \Mockery::mock('alias:Friendica\Util\Network');
239                 $networkMock
240                         ->shouldReceive('fetchUrlFull')
241                         ->with('https://test/install/testrewrite')
242                         ->andReturn($curlResultF);
243                 $networkMock
244                         ->shouldReceive('fetchUrlFull')
245                         ->with('http://test/install/testrewrite')
246                         ->andReturn($curlResultW);
247
248                 // Mocking that we can use CURL
249                 $this->setFunctions(['curl_init' => true]);
250
251                 // needed because of "normalise_link"
252                 require_once __DIR__ . '/../../../include/text.php';
253
254                 $install = new Install();
255
256                 $this->assertTrue($install->checkHtAccess('https://test'));
257         }
258
259         /**
260          * @small
261          */
262         public function testImagick()
263         {
264                 $imageMock = \Mockery::mock('alias:Friendica\Object\Image');
265                 $imageMock
266                         ->shouldReceive('supportedTypes')
267                         ->andReturn(['image/gif' => 'gif']);
268
269                 $this->setClasses(['Imagick' => true]);
270
271                 $install = new Install();
272
273                 // even there is no supported type, Imagick should return true (because it is not required)
274                 $this->assertTrue($install->checkImagick());
275
276                 $this->assertCheckExist(1,
277                         L10n::t('ImageMagick supports GIF'),
278                         '',
279                         true,
280                         false,
281                         $install->getChecks());
282         }
283
284         /**
285          * @small
286          */
287         public function testImagickNotFound()
288         {
289                 $imageMock = \Mockery::mock('alias:Friendica\Object\Image');
290                 $imageMock
291                         ->shouldReceive('supportedTypes')
292                         ->andReturn([]);
293
294                 $this->setClasses(['Imagick' => true]);
295
296                 $install = new Install();
297
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'),
302                         '',
303                         false,
304                         false,
305                         $install->getChecks());
306         }
307
308         public function testImagickNotInstalled()
309         {
310                 $this->setClasses(['Imagick' => false]);
311
312                 $install = new Install();
313
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'),
318                         '',
319                         false,
320                         false,
321                         $install->getChecks());
322         }
323 }
324
325 /**
326  * A workaround to replace the PHP native function_exists with a mocked function
327  *
328  * @param string $function_name the Name of the function
329  *
330  * @return bool true or false
331  */
332 function function_exists($function_name)
333 {
334         global $phpMock;
335         if (isset($phpMock['function_exists'])) {
336                 $result = call_user_func_array($phpMock['function_exists'], func_get_args());
337                 if ($result !== '__phpunit_continue__') {
338                         return $result;
339                 }
340         }
341         return call_user_func_array('\function_exists', func_get_args());
342 }
343
344 function class_exists($class_name)
345 {
346         global $phpMock;
347         if (isset($phpMock['class_exists'])) {
348                 $result = call_user_func_array($phpMock['class_exists'], func_get_args());
349                 if ($result !== '__phpunit_continue__') {
350                         return $result;
351                 }
352         }
353         return call_user_func_array('\class_exists', func_get_args());
354 }