Skip to content

Commit

Permalink
Merge pull request #868 from M0rgan01/fix_php_requirements
Browse files Browse the repository at this point in the history
Removing patch version when comparing php version for destination version
  • Loading branch information
M0rgan01 authored Sep 5, 2024
2 parents b661205 + 4fe859d commit 3c8706e
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 10 deletions.
2 changes: 1 addition & 1 deletion classes/Twig/Block/UpgradeChecklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getTemplateVars(): array
'token' => $this->token,
'cachingIsDisabled' => $this->selfCheck->isCacheDisabled(),
'maxExecutionTime' => $this->selfCheck->getMaxExecutionTime(),
'phpRequirementsState' => $this->selfCheck->getPhpRequirementsState(),
'phpRequirementsState' => $this->selfCheck->getPhpRequirementsState(PHP_VERSION_ID),
'phpCompatibilityRange' => $this->selfCheck->getPhpCompatibilityRange(),
'checkApacheModRewrite' => $this->selfCheck->isApacheModRewriteEnabled(),
'notLoadedPhpExtensions' => $this->selfCheck->getNotLoadedPhpExtensions(),
Expand Down
14 changes: 10 additions & 4 deletions classes/UpgradeSelfCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public function isOkForUpgrade(): bool
&& ($this->isShopDeactivated() || $this->isLocalEnvironment())
&& $this->isCacheDisabled()
&& $this->isModuleVersionLatest()
&& $this->getPhpRequirementsState() !== $this::PHP_REQUIREMENTS_INVALID
&& $this->getPhpRequirementsState(PHP_VERSION_ID) !== $this::PHP_REQUIREMENTS_INVALID
&& $this->isShopVersionMatchingVersionInDatabase()
&& $this->isApacheModRewriteEnabled()
&& $this->checkKeyGeneration()
Expand Down Expand Up @@ -457,9 +457,11 @@ public function getPhpCompatibilityRange(): ?array
}

/**
* @param int $currentVersionId
*
* @return self::PHP_REQUIREMENTS_*
*/
public function getPhpRequirementsState(): int
public function getPhpRequirementsState($currentVersionId): int
{
$phpCompatibilityRange = $this->getPhpCompatibilityRange();

Expand All @@ -469,9 +471,13 @@ public function getPhpRequirementsState(): int

$versionMin = VersionUtils::getPhpVersionId($phpCompatibilityRange['php_min_version']);
$versionMax = VersionUtils::getPhpVersionId($phpCompatibilityRange['php_max_version']);
$currentVersion = VersionUtils::getPhpMajorMinorVersionId();

if ($currentVersion >= $versionMin && $currentVersion <= $versionMax) {
$versionMinWithoutPatch = VersionUtils::getPhpMajorMinorVersionId($versionMin);
$versionMaxWithoutPatch = VersionUtils::getPhpMajorMinorVersionId($versionMax);

$currentVersion = VersionUtils::getPhpMajorMinorVersionId($currentVersionId);

if ($currentVersion >= $versionMinWithoutPatch && $currentVersion <= $versionMaxWithoutPatch) {
return self::PHP_REQUIREMENTS_VALID;
}

Expand Down
6 changes: 3 additions & 3 deletions classes/VersionUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ public static function getPhpVersionId($version)
}

/**
* @param int $phpVersionId
*
* @return int
*/
public static function getPhpMajorMinorVersionId()
public static function getPhpMajorMinorVersionId($phpVersionId)
{
$phpVersionId = PHP_VERSION_ID;

$major = (int) ($phpVersionId / 10000);
$minor = (int) (($phpVersionId % 10000) / 100);

Expand Down
77 changes: 77 additions & 0 deletions tests/unit/UpgradeSelfCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace unit;

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\UpgradeSelfCheck;

class UpgradeSelfCheckTest extends TestCase
{
/** @var UpgradeSelfCheck */
private $upgradeSelfCheck;

public function setUp()
{
if (PHP_VERSION_ID >= 80000) {
$this->markTestSkipped('An issue with this version of PHPUnit and PHP 8+ prevents this test to run.');
}

$this->upgradeSelfCheck = $this->getMockBuilder(UpgradeSelfCheck::class)
->disableOriginalConstructor()
->setMethods(['getPhpCompatibilityRange'])
->getMock();
}

public function testInvalidCompatibilityRange()
{
$this->upgradeSelfCheck->method('getPhpCompatibilityRange')
->willReturn(['php_min_version' => '7.1.0', 'php_max_version' => '7.4.0']);

$this->assertEquals(UpgradeSelfCheck::PHP_REQUIREMENTS_INVALID, $this->upgradeSelfCheck->getPhpRequirementsState(80000));
}

public function testValidCompatibilityRange()
{
$this->upgradeSelfCheck->method('getPhpCompatibilityRange')
->willReturn(['php_min_version' => '7.1.0', 'php_max_version' => '7.4.0']);

$this->assertEquals(UpgradeSelfCheck::PHP_REQUIREMENTS_VALID, $this->upgradeSelfCheck->getPhpRequirementsState(70300));

$this->upgradeSelfCheck->method('getPhpCompatibilityRange')
->willReturn(['php_min_version' => '7.2.5', 'php_max_version' => '8.1']);

$this->assertEquals(UpgradeSelfCheck::PHP_REQUIREMENTS_VALID, $this->upgradeSelfCheck->getPhpRequirementsState(70213));
}

public function testUnknownCompatibilityRange()
{
$this->upgradeSelfCheck->method('getPhpCompatibilityRange')
->willReturn(null);

$this->assertEquals(UpgradeSelfCheck::PHP_REQUIREMENTS_UNKNOWN, $this->upgradeSelfCheck->getPhpRequirementsState(70300));
}
}
12 changes: 10 additions & 2 deletions tests/unit/VersionUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public function testGetPhpVersionId()
$version = VersionUtils::getPhpVersionId('7.1');

$this->assertSame(70100, $version);

$version = VersionUtils::getPhpVersionId('7.2.18');

$this->assertSame(70218, $version);

$version = VersionUtils::getPhpVersionId('7.2.5');

$this->assertSame(70205, $version);
}

public function testGetPhpVersionIdFailForBadType()
Expand All @@ -98,9 +106,9 @@ public function testGetPhpVersionIdFailForNonNumericValue()

public function testGetPhpMajorMinorVersionId()
{
$version = VersionUtils::getPhpMajorMinorVersionId();
$version = VersionUtils::getPhpMajorMinorVersionId(70218);

$this->assertSame(PHP_MAJOR_VERSION * 10000 + PHP_MINOR_VERSION * 100, $version);
$this->assertSame(70200, $version);
}

/**
Expand Down

0 comments on commit 3c8706e

Please sign in to comment.