diff --git a/core/modules/update/src/ModuleVersion.php b/core/modules/update/src/ModuleVersion.php index 96657948ef..dc2227b53d 100644 --- a/core/modules/update/src/ModuleVersion.php +++ b/core/modules/update/src/ModuleVersion.php @@ -11,43 +11,17 @@ class ModuleVersion { /** * The core compatibility prefix used in version strings. - */ - const CORE_COMPATIBILITY_PREFIX = \Drupal::CORE_COMPATIBILITY . '-'; - - /** - * The major version. * * @var string */ - protected $majorVersion; - - /** - * The minor version. - * - * @var string|null - */ - protected $minorVersion; - - /** - * The patch version. - * - * @var string|null - */ - protected $patchVersion; - - /** - * The version extra string. - * - * @var string|null - */ - protected $versionExtra; + const CORE_COMPATIBILITY_PREFIX = \Drupal::CORE_COMPATIBILITY . '-'; /** - * Whether the core compatibility prefix should be used. + * The module version. * - * @var bool + * @var string */ - protected $useCorePrefix; + protected $version; /** * Constructs a module version object from a version string. @@ -59,48 +33,17 @@ class ModuleVersion { * The module version instance. */ public static function createFromVersionString($version_string) { - $use_compatibility_prefix = strpos($version_string, static::CORE_COMPATIBILITY_PREFIX) === 0; - if ($use_compatibility_prefix) { - $version_string = str_replace(static::CORE_COMPATIBILITY_PREFIX, '', $version_string); - } - $version_parts = explode('.', $version_string); - $major_version = $version_parts[0]; - if (count($version_parts) === 2) { - $last_version_part = $version_parts[1]; - $minor_version = NULL; - } - else { - $last_version_part = $version_parts[2]; - $minor_version = $version_parts[1]; - } - $last_version_split = explode('-', $last_version_part); - // If patch equals 'x' this instance was created from a branch and the patch - // version cannot be determined. - $patch_version = $last_version_split[0] === 'x' ? NULL : $last_version_split[0]; - $version_extra = count($last_version_split) === 1 ? NULL : $last_version_split[1]; - return new static($major_version, $minor_version, $patch_version, $version_extra, $use_compatibility_prefix); + return new static($version_string); } /** * Constructs a ModuleVersion object. * - * @param string $major_version - * The major version. - * @param string|null $minor_version - * The minor version. - * @param string|null $patch_version - * The patch version. - * @param string|null $version_extra - * The extra version string. - * @param bool $use_core_compatibility_prefix - * Whether to use the core compatibility prefix. + * @param string $version + * The version number. */ - protected function __construct($major_version, $minor_version, $patch_version, $version_extra, $use_core_compatibility_prefix) { - $this->majorVersion = $major_version; - $this->minorVersion = $minor_version; - $this->patchVersion = $patch_version; - $this->versionExtra = $version_extra; - $this->useCorePrefix = $use_core_compatibility_prefix; + protected function __construct($version) { + $this->version = $version; } /** @@ -126,27 +69,8 @@ public static function createFromSupportBranch($branch) { * The major version. */ public function getMajorVersion() { - return $this->majorVersion; - } - - /** - * Gets the minor version. - * - * @return string|null - * The minor version if available otherwise NULL. - */ - public function getMinorVersion() { - return $this->minorVersion; - } - - /** - * Gets the patch version. - * - * @return string - * The patch version. - */ - public function getPatchVersion() { - return $this->patchVersion; + $version_string = strpos($this->version, static::CORE_COMPATIBILITY_PREFIX) === 0 ? str_replace(static::CORE_COMPATIBILITY_PREFIX, '', $this->version) : $this->version; + return explode('.', $version_string)[0]; } /** @@ -156,24 +80,9 @@ public function getPatchVersion() { * The version extra string if available otherwise NULL. */ public function getVersionExtra() { - return $this->versionExtra; - } - - /** - * Determines if the version is in a specific support branch. - * - * @param string $support_branch - * The support branch. - * - * @return bool - * TRUE if the version is in the support branch, otherwise FALSE. - */ - public function isInSupportBranch($support_branch) { - $branch_version = static::createFromSupportBranch($support_branch); - if ($branch_version->minorVersion === NULL) { - return $this->getMajorVersion() === $branch_version->getMajorVersion(); - } - return $this->getMajorVersion() === $branch_version->getMajorVersion() && $this->getMinorVersion() === $branch_version->getMinorVersion(); + $version_parts = explode('.', $this->version); + $last_part_split = explode('-', array_pop($version_parts)); + return count($last_part_split) == 1 ? NULL : $last_part_split[1]; } } diff --git a/core/modules/update/tests/src/Unit/ModuleVersionTest.php b/core/modules/update/tests/src/Unit/ModuleVersionTest.php index 6cc1d89549..10e27f23d0 100644 --- a/core/modules/update/tests/src/Unit/ModuleVersionTest.php +++ b/core/modules/update/tests/src/Unit/ModuleVersionTest.php @@ -22,26 +22,6 @@ public function testGetMajorVersion($version, $expected_version_info) { $this->assertSame($expected_version_info['major'], $version->getMajorVersion()); } - /** - * @covers ::getMinorVersion - * - * @dataProvider providerVersionInfos - */ - public function testGetMinorVersion($version, $expected_version_info) { - $version = ModuleVersion::createFromVersionString($version); - $this->assertSame($expected_version_info['minor'], $version->getMinorVersion()); - } - - /** - * @covers ::getPatchVersion - * - * @dataProvider providerVersionInfos - */ - public function testGetPatchVersion($version, $expected_version_info) { - $version = ModuleVersion::createFromVersionString($version); - $this->assertSame($expected_version_info['patch'], $version->getPatchVersion()); - } - /** * @covers ::getVersionExtra * @@ -57,14 +37,12 @@ public function testGetVersionExtra($version, $expected_version_info) { * * @dataProvider providerCreateFromSupportBranch */ - public function testCreateFromSupportBranch($branch, $expected_major, $expected_minor) { + public function testCreateFromSupportBranch($branch, $expected_major) { $version = ModuleVersion::createFromSupportBranch($branch); $this->assertInstanceOf(ModuleVersion::class, $version); $this->assertSame($expected_major, $version->getMajorVersion()); - $this->assertSame($expected_minor, $version->getMinorVersion()); - // Version extra and Patch version can't be determined from a branch. + // Version extra can't be determined from a branch. $this->assertSame(NULL, $version->getVersionExtra()); - $this->assertSame(NULL, $version->getPatchVersion()); } /** @@ -75,22 +53,22 @@ public function providerCreateFromSupportBranch() { '0.' => [ '0.', '0', - NULL, ], '1.' => [ '1.', '1', - NULL, ], '0.1.' => [ '0.1.', '0', - '1', ], '1.2.' => [ '1.2.', '1', - '2', + ], + '8.x-1.' => [ + '8.x-1.', + '1', ], ]; } @@ -107,8 +85,6 @@ public function providerVersionInfos() { '8.x-1.3', [ 'major' => '1', - 'minor' => NULL, - 'patch' => '3', 'extra' => NULL, ], ], @@ -116,8 +92,6 @@ public function providerVersionInfos() { '8.x-1.0', [ 'major' => '1', - 'minor' => NULL, - 'patch' => '0', 'extra' => NULL, ], ], @@ -125,8 +99,6 @@ public function providerVersionInfos() { '8.x-1.0-dev', [ 'major' => '1', - 'minor' => NULL, - 'patch' => '0', 'extra' => 'dev', ], ], @@ -134,8 +106,6 @@ public function providerVersionInfos() { '8.x-1.3-dev', [ 'major' => '1', - 'minor' => NULL, - 'patch' => '3', 'extra' => 'dev', ], ], @@ -143,8 +113,6 @@ public function providerVersionInfos() { '0.1', [ 'major' => '0', - 'minor' => NULL, - 'patch' => '1', 'extra' => NULL, ], ], @@ -152,8 +120,6 @@ public function providerVersionInfos() { '1.0', [ 'major' => '1', - 'minor' => NULL, - 'patch' => '0', 'extra' => NULL, ], ], @@ -161,8 +127,6 @@ public function providerVersionInfos() { '1.3', [ 'major' => '1', - 'minor' => NULL, - 'patch' => '3', 'extra' => NULL, ], ], @@ -170,8 +134,6 @@ public function providerVersionInfos() { '1.0-dev', [ 'major' => '1', - 'minor' => NULL, - 'patch' => '0', 'extra' => 'dev', ], ], @@ -179,8 +141,6 @@ public function providerVersionInfos() { '1.3-dev', [ 'major' => '1', - 'minor' => NULL, - 'patch' => '3', 'extra' => 'dev', ], ], @@ -188,8 +148,6 @@ public function providerVersionInfos() { '0.2.0', [ 'major' => '0', - 'minor' => '2', - 'patch' => '0', 'extra' => NULL, ], ], @@ -197,8 +155,6 @@ public function providerVersionInfos() { '1.2.0', [ 'major' => '1', - 'minor' => '2', - 'patch' => '0', 'extra' => NULL, ], ], @@ -206,8 +162,6 @@ public function providerVersionInfos() { '1.0.3', [ 'major' => '1', - 'minor' => '0', - 'patch' => '3', 'extra' => NULL, ], ], @@ -215,8 +169,6 @@ public function providerVersionInfos() { '1.2.3', [ 'major' => '1', - 'minor' => '2', - 'patch' => '3', 'extra' => NULL, ], ], @@ -224,8 +176,6 @@ public function providerVersionInfos() { '1.2.0-dev', [ 'major' => '1', - 'minor' => '2', - 'patch' => '0', 'extra' => 'dev', ], ], @@ -233,8 +183,6 @@ public function providerVersionInfos() { '1.2.3-dev', [ 'major' => '1', - 'minor' => '2', - 'patch' => '3', 'extra' => 'dev', ], ], @@ -242,8 +190,6 @@ public function providerVersionInfos() { '1.0.x', [ 'major' => '1', - 'minor' => '0', - 'patch' => NULL, 'extra' => NULL, ], ], @@ -251,59 +197,10 @@ public function providerVersionInfos() { '1.2.x', [ 'major' => '1', - 'minor' => '2', - 'patch' => NULL, 'extra' => NULL, ], ], ]; } - /** - * @covers ::isInSupportBranch - * - * @dataProvider providerIsInSupportBranch - */ - public function testIsInSupportBranch($version, $branch, $expected_result) { - $this->assertEquals($expected_result, ModuleVersion::createFromVersionString($version)->isInSupportBranch($branch)); - } - - /** - * Data provider for testIsInSupportBranch(). - */ - public function providerIsInSupportBranch() { - return [ - [ - '1.2.3', - '1.', - TRUE, - ], - [ - '1.2.3', - '1.2.', - TRUE, - ], - [ - '1.2.3', - '1.3.', - FALSE, - ], - [ - '1.2.3', - '2.', - FALSE, - ], - [ - '1.2.3', - '2.2.', - FALSE, - ], - [ - '1.2.3', - '2.3.', - FALSE, - ], - ]; - } - } diff --git a/core/modules/update/update.compare.inc b/core/modules/update/update.compare.inc index 71ac563939..b56f61e368 100644 --- a/core/modules/update/update.compare.inc +++ b/core/modules/update/update.compare.inc @@ -254,22 +254,21 @@ function update_calculate_project_update_status(&$project_data, $available) { } // Figure out the target major version. - $existing_module_version = ModuleVersion::createFromVersionString($project_data['existing_version']); - $existing_major = $existing_module_version->getMajorVersion(); + $existing_major = ModuleVersion::createFromVersionString($project_data['existing_version'])->getMajorVersion(); $supported_branches = []; if (isset($available['supported_branches'])) { $supported_branches = explode(',', $available['supported_branches']); } - $is_in_supported_branch = function (ModuleVersion $version) use ($supported_branches) { + $is_in_supported_branch = function ($version) use ($supported_branches) { foreach ($supported_branches as $supported_branch) { - if ($version->isInSupportBranch($supported_branch)) { + if (strpos($version, $supported_branch) === 0) { return TRUE; } } return FALSE; }; - if ($is_in_supported_branch($existing_module_version)) { + if ($is_in_supported_branch($project_data['existing_version'])) { // Still supported, stay at the current major version. $target_major = $existing_major; } @@ -294,9 +293,6 @@ function update_calculate_project_update_status(&$project_data, $available) { // what they have is unsupported and let them figure it out. $target_major = max($existing_major, $target_major); - $release_patch_changed = ''; - $patch = ''; - // If the project is marked as UpdateFetcherInterface::FETCH_PENDING, it // means that the data we currently have (if any) is stale, and we've got a // task queued up to (re)fetch the data. In that case, we mark it as such, @@ -356,7 +352,6 @@ function update_calculate_project_update_status(&$project_data, $available) { } $release_major_version = $release_module_version->getMajorVersion(); - $release_patch_version = $release_module_version->getPatchVersion(); // See if this is a higher major version than our target and yet still // supported. If so, record it as an "Also available" release. // Note: Some projects have a HEAD release from CVS days, which could @@ -401,15 +396,10 @@ function update_calculate_project_update_status(&$project_data, $available) { // Look for the 'recommended' version if we haven't found it yet (see // phpdoc at the top of this function for the definition). if (!isset($project_data['recommended']) - && $release_major_version == $target_major - && $release_patch_version !== NULL) { - if ($patch != $release_patch_version) { - $patch = $release_patch_version; - $release_patch_changed = $release; - } - if ($release_module_version->getVersionExtra() === NULL && $patch == $release_patch_version) { - $project_data['recommended'] = $release_patch_changed['version']; - $project_data['releases'][$release_patch_changed['version']] = $release_patch_changed; + && $release_major_version == $target_major) { + if ($release_module_version->getVersionExtra() === NULL) { + $project_data['recommended'] = $release['version']; + $project_data['releases'][$release['version']] = $release; } }