Skip to content

Commit 649e396

Browse files
committed
Make tests pass.
1 parent 91b2647 commit 649e396

File tree

2 files changed

+82
-29
lines changed

2 files changed

+82
-29
lines changed

src/Commands/pm/SecurityUpdateCommands.php

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ public function suggestComposerCommand($result, CommandData $commandData)
8080
}
8181

8282
/**
83+
* Fetches the generated composer.json from drupal-security-advisories.
84+
*
8385
* @return mixed
86+
*
8487
* @throws \Exception
8588
*/
86-
protected function fetchAdvisoryComposerJson(): mixed {
89+
protected function fetchAdvisoryComposerJson() {
8790
try {
8891
$response_body = file_get_contents('https://raw.githubusercontent.com/drupal-composer/drupal-security-advisories/8.x/composer.json');
8992
} catch (Exception $e) {
@@ -94,10 +97,13 @@ protected function fetchAdvisoryComposerJson(): mixed {
9497
}
9598

9699
/**
97-
* @return mixed
100+
* Loads the contents of the local Drupal application's composer.lock file.
101+
*
102+
* @return array
103+
*
98104
* @throws \Exception
99105
*/
100-
protected function loadSiteComposerLock(): mixed {
106+
protected function loadSiteComposerLock() {
101107
$composer_root = Drush::bootstrapManager()->getComposerRoot();
102108
$composer_lock_file_name = getenv('COMPOSER') ? str_replace('.json', '',
103109
getenv('COMPOSER')) : 'composer';
@@ -116,8 +122,11 @@ protected function loadSiteComposerLock(): mixed {
116122
}
117123

118124
/**
119-
* @param $composer_lock_data
120-
* @param $security_advisories_composer_json
125+
* Register all available security updates in $this->securityUpdates.
126+
* @param array $composer_lock_data
127+
* The contents of the local Drupal application's composer.lock file.
128+
* @param array $security_advisories_composer_json
129+
* The composer.json array from drupal-security-advisories.
121130
*/
122131
protected function registerAllSecurityUpdates(
123132
$composer_lock_data,
@@ -130,9 +139,18 @@ protected function registerAllSecurityUpdates(
130139
}
131140

132141
/**
133-
* @param $conflict_constraint
142+
* Determines if update is avaiable based on a conflict constraint.
143+
*
144+
* @param string $conflict_constraint
145+
* The constraint for the conflicting, insecure package version.
146+
* E.g., <1.0.0.
134147
* @param array $package
135-
* @param $name
148+
* The package to be evaluated.
149+
* @param string $name
150+
* The human readable display name for the package.
151+
*
152+
* @return array
153+
* An associative array containing name, version, and min-version keys.
136154
*/
137155
public static function determineUpdatesFromConstraint(
138156
$conflict_constraint,
@@ -147,7 +165,7 @@ public static function determineUpdatesFromConstraint(
147165
return [
148166
'name' => $name,
149167
'version' => $package['version'],
150-
// Assume that conflict constraint of ^1.0.0 indicates that
168+
// Assume that conflict constraint of <1.0.0 indicates that
151169
// 1.0.0 is the available, secure version.
152170
'min-version' => $min_version,
153171
];
@@ -159,27 +177,32 @@ public static function determineUpdatesFromConstraint(
159177
$exact_version = $conflict_constraint;
160178
if (Comparator::equalTo($package['version'],
161179
$exact_version)) {
162-
// $version_parts = explode('.', $package['version']);
163-
$version_parser = new VersionParser();
164-
$constraints = $version_parser->parseConstraints($package['version']);
165-
return [
166-
'name' => $name,
167-
'version' => $package['version'],
168-
// Assume that conflict constraint of 1.0.0 indicates that
169-
// 1.0.1 is the available, secure version.
170-
'min-version' => $exact_version,
171-
];
180+
$version_parts = explode('.', $package['version']);
181+
if (count($version_parts) == 3) {
182+
$version_parts[2]++;
183+
$min_version = implode('.', $version_parts);
184+
return [
185+
'name' => $name,
186+
'version' => $package['version'],
187+
// Assume that conflict constraint of 1.0.0 indicates that
188+
// 1.0.1 is the available, secure version.
189+
'min-version' => $min_version,
190+
];
191+
}
172192
}
173193
}
174-
else {
175-
return [];
176-
}
194+
return [];
177195
}
178196

179197
/**
180-
* @param $security_advisories_composer_json
181-
* @param $name
182-
* @param $package
198+
* Registers available security updates for a given package.
199+
*
200+
* @param array $security_advisories_composer_json
201+
* The composer.json array from drupal-security-advisories.
202+
* @param string $name
203+
* The human readable display name for the package.
204+
* @param array $package
205+
* The package to be evaluated.
183206
*/
184207
protected function registerPackageSecurityUpdates(
185208
$security_advisories_composer_json,

tests/SecurityUpdatesTest.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,51 @@ public function testInsecurePackage()
3434
*
3535
* @dataProvider testConflictConstraintParsingProvider
3636
*/
37-
public function testConflictConstraintParsing($package, $min_version)
37+
public function testConflictConstraintParsing($package, $conflict_constraint, $min_version, $updates_are_available)
3838
{
39-
$available_updates = SecurityUpdateCommands::determineUpdatesFromConstraint($min_version, $package, $package['name']);
40-
$this->assertEquals($package['version'], $available_updates['version']);
41-
$this->assertEquals($min_version, $available_updates['min-version']);
39+
$available_updates = SecurityUpdateCommands::determineUpdatesFromConstraint($conflict_constraint, $package, $package['name']);
40+
$this->assertEquals($updates_are_available, (bool) $available_updates);
41+
42+
if ($available_updates) {
43+
$this->assertEquals($package['version'], $available_updates['version']);
44+
$this->assertEquals($min_version, $available_updates['min-version']);
45+
}
4246
}
4347

48+
/**
49+
* Data provider for testConflictConstraintParsing().
50+
*/
4451
public function testConflictConstraintParsingProvider() {
4552
return [
53+
// Test "minimum version" conflict.
54+
[
55+
[
56+
'name' => 'Alinks',
57+
'version' => '1.0.0'
58+
],
59+
'<1.0.1',
60+
'1.0.1',
61+
TRUE,
62+
],
63+
// Test "exact version" conflict.
64+
[
65+
[
66+
'name' => 'Alinks',
67+
'version' => '1.0.0'
68+
],
69+
'1.0.0',
70+
'1.0.1',
71+
TRUE,
72+
],
73+
// Test "exact version" conflict with 2 digits. Should not work.
4674
[
4775
[
4876
'name' => 'Alinks',
4977
'version' => '1.0.0'
5078
],
51-
'^1.0.1',
79+
'1.0',
80+
'1.0.1',
81+
FALSE,
5282
],
5383
];
5484
}

0 commit comments

Comments
 (0)