From ee8d1e9cc26ac59b9d8a0f6af425a2ea730d8b95 Mon Sep 17 00:00:00 2001 From: David Jardin Date: Sat, 11 Jun 2022 18:52:37 +0200 Subject: [PATCH] added tests and tweaks --- ...trainChecker.php => ConstraintChecker.php} | 10 +- .../Cms/Updater/ConstraintCheckerTest.php | 213 ++++++++++++++++++ 2 files changed, 218 insertions(+), 5 deletions(-) rename libraries/src/Updater/{ConstrainChecker.php => ConstraintChecker.php} (94%) create mode 100644 tests/Unit/Libraries/Cms/Updater/ConstraintCheckerTest.php diff --git a/libraries/src/Updater/ConstrainChecker.php b/libraries/src/Updater/ConstraintChecker.php similarity index 94% rename from libraries/src/Updater/ConstrainChecker.php rename to libraries/src/Updater/ConstraintChecker.php index f94574a5e8319..70db5ffaf253e 100644 --- a/libraries/src/Updater/ConstrainChecker.php +++ b/libraries/src/Updater/ConstraintChecker.php @@ -21,7 +21,7 @@ * * @since __DEPLOY_VERSION__ */ -class ConstrainChecker +class ConstraintChecker { /** * Checks whether the passed constraints are matched @@ -76,7 +76,7 @@ public function check(array $constraints) * * @since __DEPLOY_VERSION__ */ - protected function checkTargetplatform(stdClass $targetPlatform) + protected function checkTargetplatform(\stdClass $targetPlatform) { // Lower case and remove the exclamation mark $product = strtolower(InputFilter::getInstance()->clean(Version::PRODUCT, 'cmd')); @@ -115,10 +115,10 @@ protected function checkPhpMinimum(string $phpMinimum) * * @since __DEPLOY_VERSION__ */ - protected function checkSupportedDatabases(stdClass $supportedDatabases) + protected function checkSupportedDatabases(\stdClass $supportedDatabases) { $db = Factory::getDbo(); - $dbType = strtoupper($db->getServerType()); + $dbType = strtolower($db->getServerType()); $dbVersion = $db->getVersion(); // MySQL and MariaDB use the same database driver but not the same version numbers @@ -136,7 +136,7 @@ protected function checkSupportedDatabases(stdClass $supportedDatabases) // Do we have an entry for the database? if (\property_exists($supportedDatabases, $dbType)) { - $minimumVersion = $supportedDatabases[$dbType]; + $minimumVersion = $supportedDatabases->$dbType; return version_compare($dbVersion, $minimumVersion, '>='); } diff --git a/tests/Unit/Libraries/Cms/Updater/ConstraintCheckerTest.php b/tests/Unit/Libraries/Cms/Updater/ConstraintCheckerTest.php new file mode 100644 index 0000000000000..0a2439f3d6156 --- /dev/null +++ b/tests/Unit/Libraries/Cms/Updater/ConstraintCheckerTest.php @@ -0,0 +1,213 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Tests\Unit\Libraries\Cms; + +use Joomla\CMS\Factory; +use Joomla\CMS\Updater\ConstraintChecker; +use Joomla\Database\DatabaseDriver; +use Joomla\Tests\Unit\UnitTestCase; + +/** + * Test class for Version. + * + * @package Joomla.UnitTest + * @subpackage Version + * @since __DEPLOY_VERSION__ + */ +class ConstraintCheckerTest extends UnitTestCase +{ + /** + * @var ConstraintChecker + * @since 3.0 + */ + protected $checker; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + protected function setUp():void + { + $this->checker = new ConstraintChecker(); + } + + /** + * Overrides the parent tearDown method. + * + * @return void + * + * @see \PHPUnit\Framework\TestCase::tearDown() + * @since __DEPLOY_VERSION__ + */ + protected function tearDown():void + { + unset($this->checker); + parent::tearDown(); + } + + public function testCheckMethodReturnsFalseIfPlatformIsMissing() + { + $constraint = []; + $this->assertFalse($this->checker->check($constraint)); + } + + public function testCheckMethodReturnsTrueIfPlatformIsOnlyConstraint() + { + $constraint = ['targetplatform' => (object) ["name" => "joomla", "version" => "4.*"]]; + $this->assertTrue($this->checker->check($constraint)); + } + + /** + * Tests the checkSupportedDatabases method + * + * @return void + * + * @since __DEPLOY_VERSION__ + * + * @dataProvider supportedDatabasesDataProvider + */ + public function testCheckSupportedDatabases($currentDatabase, $supportedDatabases, $expectedResult) + { + $dbMock = $this->createMock(DatabaseDriver::class); + $dbMock->method('getServerType')->willReturn($currentDatabase['type']); + $dbMock->method('getVersion')->willReturn($currentDatabase['version']); + Factory::$database = $dbMock; + + $method = $this->getPublicMethod('checkSupportedDatabases'); + $result = $method->invoke($this->checker, $supportedDatabases); + + $this->assertSame($expectedResult, $result); + } + + /** + * Tests the checkPhpMinimum method + * + * @return void + * + * @since __DEPLOY_VERSION__ + * + * @dataProvider targetplatformDataProvider + */ + public function testCheckPhpMinimumReturnFalseForFuturePhp() + { + $method = $this->getPublicMethod('checkPhpMinimum'); + + $this->assertFalse($method->invoke($this->checker, '99.9.9')); + } + + /** + * Tests the checkTargetplatform method + * + * @return void + * + * @since __DEPLOY_VERSION__ + * + * @dataProvider targetplatformDataProvider + */ + public function testCheckTargetplatform($targetPlatform, $expectedResult) + { + $method = $this->getPublicMethod('checkTargetplatform'); + $result = $method->invoke($this->checker, $targetPlatform); + + $this->assertSame($expectedResult, $result); + } + + /** + * Data provider for testCheckSupportedDatabases method + * + * @since __DEPLOY_VERSION__ + * + * @return array[] + */ + protected function supportedDatabasesDataProvider() + { + return [ + [ + ['type' => 'mysql', 'version' => '5.7.37-log-cll-lve'], + (object) ['mysql' => '5.6', 'mariadb' => '10.3'], + true + ], + [ + ['type' => 'mysql', 'version' => '5.6.0-log-cll-lve'], + (object) ['mysql' => '5.6', 'mariadb' => '10.3'], + true + ], + [ + ['type' => 'mysql', 'version' => '10.3.34-MariaDB-0+deb10u1'], + (object) ['mysql' => '5.6', 'mariadb' => '10.3'], + true + ], + [ + ['type' => 'mysql', 'version' => '5.7.37-log-cll-lve'], + (object) ['mysql' => '5.8', 'mariadb' => '10.3'], + false + ], + [ + ['type' => 'pgsql', 'version' => '14.3'], + (object) ['mysql' => '5.8', 'mariadb' => '10.3'], + false + ], + [ + ['type' => 'mysql', 'version' => '10.3.34-MariaDB-0+deb10u1'], + (object) ['mysql' => '5.6', 'mariadb' => '10.4'], + false + ], + [ + ['type' => 'mysql', 'version' => '5.5.5-10.3.34-MariaDB-0+deb10u1'], + (object) ['mysql' => '5.6', 'mariadb' => '10.3'], + true + ], + ]; + } + + /** + * Data provider for testCheckTargetplatform method + * + * @since __DEPLOY_VERSION__ + * + * @return array[] + */ + protected function targetplatformDataProvider() + { + return [ + [(object) ["name" => "foobar", "version" => "1.*"], false], + [(object) ["name" => "foobar", "version" => "4.*"], false], + [(object) ["name" => "joomla", "version" => "1.*"], false], + [(object) ["name" => "joomla", "version" => "3.1.2"], false], + [(object) ["name" => "joomla", "version" => ""], true], + [(object) ["name" => "joomla", "version" => ".*"], true], + [(object) ["name" => "joomla", "version" => JVERSION], true], + [(object) ["name" => "joomla", "version" => "4.*"], true], + ]; + } + + /** + * Internal helper method to get access to protected methods + * + * @since __DEPLOY_VERSION__ + * + * @param $method + * + * @return \ReflectionMethod + * @throws \ReflectionException + */ + protected function getPublicMethod($method) + { + $reflectionClass = new \ReflectionClass($this->checker); + $method = $reflectionClass->getMethod($method); + $method->setAccessible(true); + + return $method; + } +}