]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Core/InstallerTest.php
Merge pull request #11195 from annando/issue-10966
[friendica.git] / tests / src / Core / InstallerTest.php
index b2ef92b450c6f2d96428fb0bd195f5609a22fd20..9256581b16ce8e8031a5a356a5b7aa68740c4a60 100644 (file)
@@ -1,43 +1,78 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
-// this is in the same namespace as Install for mocking 'function_exists'
+/// @todo this is in the same namespace as Install for mocking 'function_exists'
 namespace Friendica\Core;
 
 use Dice\Dice;
-use Friendica\Core\Config\Cache\ConfigCache;
+use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
+use Friendica\Core\Config\ValueObject\Cache;
 use Friendica\DI;
-use Friendica\Network\CurlResult;
+use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
+use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
 use Friendica\Test\MockedTest;
 use Friendica\Test\Util\VFSTrait;
-use Friendica\Util\Network;
+use Mockery;
 use Mockery\MockInterface;
 
 class InstallerTest extends MockedTest
 {
        use VFSTrait;
+       use ArraySubsetAsserts;
 
        /**
-        * @var \Friendica\Core\L10n\L10n|MockInterface
+        * @var L10n|MockInterface
         */
        private $l10nMock;
+       /**
+        * @var Dice|MockInterface
+        */
+       private $dice;
 
-       public function setUp()
+       protected function setUp(): void
        {
                parent::setUp();
 
                $this->setUpVfsDir();
 
-               $this->l10nMock = \Mockery::mock(\Friendica\Core\L10n\L10n::class);
+               $this->l10nMock = Mockery::mock(L10n::class);
 
                /** @var Dice|MockInterface $dice */
-               $dice = \Mockery::mock(Dice::class)->makePartial();
-               $dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
+               $this->dice = Mockery::mock(Dice::class)->makePartial();
+               $this->dice = $this->dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
 
-               $dice->shouldReceive('create')
-                          ->with(\Friendica\Core\L10n\L10n::class)
+               $this->dice->shouldReceive('create')
+                          ->with(L10n::class)
                           ->andReturn($this->l10nMock);
 
-               DI::init($dice);
+               DI::init($this->dice);
+       }
+
+       public static function tearDownAfterClass(): void
+       {
+               // Reset mocking
+               global $phpMock;
+               $phpMock = [];
+
+               parent::tearDownAfterClass();
        }
 
        private function mockL10nT(string $text, $times = null)
@@ -46,7 +81,7 @@ class InstallerTest extends MockedTest
        }
 
        /**
-        * Mocking the L10n::t() calls for the function checks
+        * Mocking the DI::l10n()->t() calls for the function checks
         */
        private function mockFunctionL10TCalls()
        {
@@ -69,17 +104,21 @@ class InstallerTest extends MockedTest
                $this->mockL10nT('Error: JSON PHP module required but not installed.', 1);
                $this->mockL10nT('File Information PHP module', 1);
                $this->mockL10nT('Error: File Information PHP module required but not installed.', 1);
+               $this->mockL10nT('Program execution functions', 1);
+               $this->mockL10nT('Error: Program execution functions (proc_open) required but not enabled.', 1);
        }
 
        private function assertCheckExist($position, $title, $help, $status, $required, $assertionArray)
        {
-               $this->assertArraySubset([$position => [
+               $subSet = [$position => [
                        'title' => $title,
                        'status' => $status,
                        'required' => $required,
                        'error_msg' => null,
                        'help' => $help]
-               ], $assertionArray);
+               ];
+
+               self::assertArraySubset($subSet, $assertionArray, false, "expected subset: " . PHP_EOL . print_r($subSet, true) . PHP_EOL . "current subset: " . print_r($assertionArray, true));
        }
 
        /**
@@ -87,7 +126,7 @@ class InstallerTest extends MockedTest
         *
         * @param array $functions a list from function names and their result
         */
-       private function setFunctions($functions)
+       private function setFunctions(array $functions)
        {
                global $phpMock;
                $phpMock['function_exists'] = function($function) use ($functions) {
@@ -105,7 +144,7 @@ class InstallerTest extends MockedTest
         *
         * @param array $classes a list from class names and their results
         */
-       private function setClasses($classes)
+       private function setClasses(array $classes)
        {
                global $phpMock;
                $phpMock['class_exists'] = function($class) use ($classes) {
@@ -127,11 +166,11 @@ class InstallerTest extends MockedTest
 
                $this->setFunctions(['openssl_pkey_new' => false]);
                $install = new Installer();
-               $this->assertFalse($install->checkKeys());
+               self::assertFalse($install->checkKeys());
 
                $this->setFunctions(['openssl_pkey_new' => true]);
                $install = new Installer();
-               $this->assertTrue($install->checkKeys());
+               self::assertTrue($install->checkKeys());
        }
 
        /**
@@ -142,8 +181,8 @@ class InstallerTest extends MockedTest
                $this->mockFunctionL10TCalls();
                $this->setFunctions(['curl_init' => false, 'imagecreatefromjpeg' => true]);
                $install = new Installer();
-               $this->assertFalse($install->checkFunctions());
-               $this->assertCheckExist(3,
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(3,
                        'libCurl PHP module',
                        'Error: libCURL PHP module required but not installed.',
                        false,
@@ -153,8 +192,8 @@ class InstallerTest extends MockedTest
                $this->mockFunctionL10TCalls();
                $this->setFunctions(['imagecreatefromjpeg' => false]);
                $install = new Installer();
-               $this->assertFalse($install->checkFunctions());
-               $this->assertCheckExist(4,
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(4,
                        'GD graphics PHP module',
                        'Error: GD graphics PHP module with JPEG support required but not installed.',
                        false,
@@ -164,8 +203,8 @@ class InstallerTest extends MockedTest
                $this->mockFunctionL10TCalls();
                $this->setFunctions(['openssl_public_encrypt' => false]);
                $install = new Installer();
-               $this->assertFalse($install->checkFunctions());
-               $this->assertCheckExist(5,
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(5,
                        'OpenSSL PHP module',
                        'Error: openssl PHP module required but not installed.',
                        false,
@@ -175,8 +214,8 @@ class InstallerTest extends MockedTest
                $this->mockFunctionL10TCalls();
                $this->setFunctions(['mb_strlen' => false]);
                $install = new Installer();
-               $this->assertFalse($install->checkFunctions());
-               $this->assertCheckExist(6,
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(6,
                        'mb_string PHP module',
                        'Error: mb_string PHP module required but not installed.',
                        false,
@@ -186,8 +225,8 @@ class InstallerTest extends MockedTest
                $this->mockFunctionL10TCalls();
                $this->setFunctions(['iconv_strlen' => false]);
                $install = new Installer();
-               $this->assertFalse($install->checkFunctions());
-               $this->assertCheckExist(7,
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(7,
                        'iconv PHP module',
                        'Error: iconv PHP module required but not installed.',
                        false,
@@ -197,8 +236,8 @@ class InstallerTest extends MockedTest
                $this->mockFunctionL10TCalls();
                $this->setFunctions(['posix_kill' => false]);
                $install = new Installer();
-               $this->assertFalse($install->checkFunctions());
-               $this->assertCheckExist(8,
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(8,
                        'POSIX PHP module',
                        'Error: POSIX PHP module required but not installed.',
                        false,
@@ -206,10 +245,20 @@ class InstallerTest extends MockedTest
                        $install->getChecks());
 
                $this->mockFunctionL10TCalls();
+               $this->setFunctions(['proc_open' => false]);
+               $install = new Installer();
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(9,
+                       'Program execution functions',
+                       'Error: Program execution functions (proc_open) required but not enabled.',
+                       false,
+                       true,
+                       $install->getChecks());
+               $this->mockFunctionL10TCalls();
                $this->setFunctions(['json_encode' => false]);
                $install = new Installer();
-               $this->assertFalse($install->checkFunctions());
-               $this->assertCheckExist(9,
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(10,
                        'JSON PHP module',
                        'Error: JSON PHP module required but not installed.',
                        false,
@@ -219,8 +268,8 @@ class InstallerTest extends MockedTest
                $this->mockFunctionL10TCalls();
                $this->setFunctions(['finfo_open' => false]);
                $install = new Installer();
-               $this->assertFalse($install->checkFunctions());
-               $this->assertCheckExist(10,
+               self::assertFalse($install->checkFunctions());
+               self::assertCheckExist(11,
                        'File Information PHP module',
                        'Error: File Information PHP module required but not installed.',
                        false,
@@ -239,7 +288,7 @@ class InstallerTest extends MockedTest
                        'finfo_open' => true,
                ]);
                $install = new Installer();
-               $this->assertTrue($install->checkFunctions());
+               self::assertTrue($install->checkFunctions());
        }
 
        /**
@@ -249,17 +298,17 @@ class InstallerTest extends MockedTest
        {
                $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
 
-               $this->assertTrue($this->root->hasChild('config/local.config.php'));
+               self::assertTrue($this->root->hasChild('config/local.config.php'));
 
                $install = new Installer();
-               $this->assertTrue($install->checkLocalIni());
+               self::assertTrue($install->checkLocalIni());
 
                $this->delConfigFile('local.config.php');
 
-               $this->assertFalse($this->root->hasChild('config/local.config.php'));
+               self::assertFalse($this->root->hasChild('config/local.config.php'));
 
                $install = new Installer();
-               $this->assertTrue($install->checkLocalIni());
+               self::assertTrue($install->checkLocalIni());
        }
 
        /**
@@ -272,35 +321,41 @@ class InstallerTest extends MockedTest
                $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
 
                // Mocking the CURL Response
-               $curlResult = \Mockery::mock(CurlResult::class);
-               $curlResult
+               $IHTTPResult = Mockery::mock(ICanHandleHttpResponses::class);
+               $IHTTPResult
                        ->shouldReceive('getReturnCode')
                        ->andReturn('404');
-               $curlResult
+               $IHTTPResult
                        ->shouldReceive('getRedirectUrl')
                        ->andReturn('');
-               $curlResult
+               $IHTTPResult
                        ->shouldReceive('getError')
                        ->andReturn('test Error');
 
                // Mocking the CURL Request
-               $networkMock = \Mockery::mock('alias:' . Network::class);
+               $networkMock = Mockery::mock(ICanSendHttpRequests::class);
                $networkMock
-                       ->shouldReceive('fetchUrlFull')
+                       ->shouldReceive('fetchFull')
                        ->with('https://test/install/testrewrite')
-                       ->andReturn($curlResult);
+                       ->andReturn($IHTTPResult);
                $networkMock
-                       ->shouldReceive('fetchUrlFull')
+                       ->shouldReceive('fetchFull')
                        ->with('http://test/install/testrewrite')
-                       ->andReturn($curlResult);
+                       ->andReturn($IHTTPResult);
+
+               $this->dice->shouldReceive('create')
+                    ->with(ICanSendHttpRequests::class)
+                    ->andReturn($networkMock);
+
+               DI::init($this->dice);
 
                // Mocking that we can use CURL
                $this->setFunctions(['curl_init' => true]);
 
                $install = new Installer();
 
-               $this->assertFalse($install->checkHtAccess('https://test'));
-               $this->assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
+               self::assertFalse($install->checkHtAccess('https://test'));
+               self::assertSame('test Error', $install->getChecks()[0]['error_msg']['msg']);
        }
 
        /**
@@ -313,34 +368,40 @@ class InstallerTest extends MockedTest
                $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
 
                // Mocking the failed CURL Response
-               $curlResultF = \Mockery::mock(CurlResult::class);
-               $curlResultF
+               $IHTTPResultF = Mockery::mock(ICanHandleHttpResponses::class);
+               $IHTTPResultF
                        ->shouldReceive('getReturnCode')
                        ->andReturn('404');
 
                // Mocking the working CURL Response
-               $curlResultW = \Mockery::mock(CurlResult::class);
-               $curlResultW
+               $IHTTPResultW = Mockery::mock(ICanHandleHttpResponses::class);
+               $IHTTPResultW
                        ->shouldReceive('getReturnCode')
                        ->andReturn('204');
 
                // Mocking the CURL Request
-               $networkMock = \Mockery::mock('alias:' . Network::class);
+               $networkMock = Mockery::mock(ICanSendHttpRequests::class);
                $networkMock
-                       ->shouldReceive('fetchUrlFull')
+                       ->shouldReceive('fetchFull')
                        ->with('https://test/install/testrewrite')
-                       ->andReturn($curlResultF);
+                       ->andReturn($IHTTPResultF);
                $networkMock
-                       ->shouldReceive('fetchUrlFull')
+                       ->shouldReceive('fetchFull')
                        ->with('http://test/install/testrewrite')
-                       ->andReturn($curlResultW);
+                       ->andReturn($IHTTPResultW);
+
+               $this->dice->shouldReceive('create')
+                          ->with(ICanSendHttpRequests::class)
+                          ->andReturn($networkMock);
+
+               DI::init($this->dice);
 
                // Mocking that we can use CURL
                $this->setFunctions(['curl_init' => true]);
 
                $install = new Installer();
 
-               $this->assertTrue($install->checkHtAccess('https://test'));
+               self::assertTrue($install->checkHtAccess('https://test'));
        }
 
        /**
@@ -350,7 +411,7 @@ class InstallerTest extends MockedTest
         */
        public function testImagick()
        {
-               $this->markTestIncomplete('needs adapted class_exists() mock');
+               static::markTestIncomplete('needs adapted class_exists() mock');
 
                $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
 
@@ -359,10 +420,10 @@ class InstallerTest extends MockedTest
                $install = new Installer();
 
                // even there is no supported type, Imagick should return true (because it is not required)
-               $this->assertTrue($install->checkImagick());
+               self::assertTrue($install->checkImagick());
 
-               $this->assertCheckExist(1,
-                       L10n::t('ImageMagick supports GIF'),
+               self::assertCheckExist(1,
+                       $this->l10nMock->t('ImageMagick supports GIF'),
                        '',
                        true,
                        false,
@@ -376,6 +437,8 @@ class InstallerTest extends MockedTest
         */
        public function testImagickNotFound()
        {
+               static::markTestIncomplete('Disabled due not working/difficult mocking global functions - needs more care!');
+
                $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
 
                $this->setClasses(['Imagick' => true]);
@@ -383,9 +446,9 @@ class InstallerTest extends MockedTest
                $install = new Installer();
 
                // even there is no supported type, Imagick should return true (because it is not required)
-               $this->assertTrue($install->checkImagick());
-               $this->assertCheckExist(1,
-                       L10n::t('ImageMagick supports GIF'),
+               self::assertTrue($install->checkImagick());
+               self::assertCheckExist(1,
+                       $this->l10nMock->t('ImageMagick supports GIF'),
                        '',
                        false,
                        false,
@@ -400,8 +463,8 @@ class InstallerTest extends MockedTest
                $install = new Installer();
 
                // even there is no supported type, Imagick should return true (because it is not required)
-               $this->assertTrue($install->checkImagick());
-               $this->assertCheckExist(0,
+               self::assertTrue($install->checkImagick());
+               self::assertCheckExist(0,
                        'ImageMagick PHP extension is not installed',
                        '',
                        false,
@@ -411,14 +474,15 @@ class InstallerTest extends MockedTest
 
        /**
         * Test the setup of the config cache for installation
+        * @doesNotPerformAssertions
         */
        public function testSetUpCache()
        {
                $this->l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
 
                $install = new Installer();
-               $configCache = \Mockery::mock(ConfigCache::class);
-               $configCache->shouldReceive('set')->with('config', 'php_path', \Mockery::any())->once();
+               $configCache = Mockery::mock(Cache::class);
+               $configCache->shouldReceive('set')->with('config', 'php_path', Mockery::any())->once();
                $configCache->shouldReceive('set')->with('system', 'basepath', '/test/')->once();
 
                $install->setUpCache($configCache, '/test/');
@@ -432,7 +496,7 @@ class InstallerTest extends MockedTest
  *
  * @return bool true or false
  */
-function function_exists($function_name)
+function function_exists(string $function_name)
 {
        global $phpMock;
        if (isset($phpMock['function_exists'])) {