Skip to content

Commit 5083abc

Browse files
committed
cs fix
1 parent 9529651 commit 5083abc

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

app/Jobs/CheckSiteHealth.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public function handle(): void
4747
$tufFetcher = App::make(TufFetcher::class);
4848
$latestVersion = $tufFetcher->getLatestVersionForBranch((int) $this->site->cms_version[0]);
4949

50+
// No latest version for branch available, unsupported branch - return
51+
if (!$latestVersion) {
52+
return;
53+
}
54+
5055
// Available version is not newer, exit
5156
if (!version_compare($latestVersion, $this->site->cms_version, ">")) {
5257
return;

app/TUF/TufFetcher.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public function __construct()
1818
}
1919

2020
/**
21-
* @return Collection
21+
* @return Collection<string, ReleaseData>
2222
*/
2323
public function getReleases(): Collection
2424
{
2525
// Cache response to avoid to make constant calls on the fly
26-
return Cache::remember(
26+
$releases = Cache::remember(
2727
'cms_targets',
2828
(int) config('autoupdates.tuf_repo_cachetime') * 60, // @phpstan-ignore-line
2929
function () {
@@ -47,14 +47,26 @@ function () {
4747
});
4848
}
4949
);
50+
51+
if (!$releases instanceof Collection) {
52+
throw new MetadataException("Invalid release list");
53+
}
54+
55+
return $releases;
5056
}
5157

52-
public function getLatestVersionForBranch(int $branch): string
58+
public function getLatestVersionForBranch(int $branch): ?string
5359
{
54-
return $this->getReleases()->filter(function (ReleaseData $release) {
55-
return $release->stability === "Stable";
56-
})->sort(function (ReleaseData $releaseA, ReleaseData $releaseB) {
57-
return version_compare($releaseA->version, $releaseB->version, '<');
58-
})->pluck('version')->first();
60+
$versionMatch = $this->getReleases()->filter(function (ReleaseData $release) use ($branch): bool {
61+
return $release->stability === "stable" && $release->version[0] === (string) $branch;
62+
})->sort(function (ReleaseData $releaseA, ReleaseData $releaseB): int {
63+
return version_compare($releaseA->version, $releaseB->version);
64+
})->last();
65+
66+
if (!$versionMatch instanceof ReleaseData) {
67+
return null;
68+
}
69+
70+
return $versionMatch->version;
5971
}
6072
}

tests/Unit/TUF/TufFetcherTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,81 @@ public function testGetReleasesThrowsExceptionOnMissingCustom()
7979
$object->getReleases();
8080
}
8181

82+
public function testGetLatestVersionForBranchReturnsNullForMissingBranch()
83+
{
84+
App::bind(StorageInterface::class, fn() => $this->getStorageMock([
85+
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
86+
"custom" => [
87+
"description" => "Joomla! 5.2.1 Release",
88+
"version" => "5.2.1",
89+
"stability" => "stable",
90+
]
91+
]
92+
]));
93+
94+
$object = new TufFetcher();
95+
$result = $object->getLatestVersionForBranch(6);
96+
97+
$this->assertNull($result);
98+
}
99+
100+
public function testGetLatestVersionForBranchChecksBranch()
101+
{
102+
App::bind(StorageInterface::class, fn() => $this->getStorageMock([
103+
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
104+
"custom" => [
105+
"description" => "Joomla! 5.2.1 Release",
106+
"version" => "5.2.1",
107+
"stability" => "stable",
108+
]
109+
],
110+
"Joomla_4.2.1-Stable-Upgrade_Package.zip" => [
111+
"custom" => [
112+
"description" => "Joomla! 4.2.1 Release",
113+
"version" => "4.1.2",
114+
"stability" => "stable",
115+
]
116+
]
117+
]));
118+
119+
$object = new TufFetcher();
120+
$result = $object->getLatestVersionForBranch(4);
121+
122+
$this->assertEquals("4.1.2", $result);
123+
}
124+
125+
public function testGetLatestVersionForBranchChecksOrdering()
126+
{
127+
App::bind(StorageInterface::class, fn() => $this->getStorageMock([
128+
"Joomla_5.2.3-Stable-Upgrade_Package.zip" => [
129+
"custom" => [
130+
"description" => "Joomla! 5.2.3 Release",
131+
"version" => "5.2.3",
132+
"stability" => "stable",
133+
]
134+
],
135+
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
136+
"custom" => [
137+
"description" => "Joomla! 5.2.1 Release",
138+
"version" => "5.2.1",
139+
"stability" => "stable",
140+
]
141+
],
142+
"Joomla_5.2.2-Stable-Upgrade_Package.zip" => [
143+
"custom" => [
144+
"description" => "Joomla! 5.2.2 Release",
145+
"version" => "5.2.2",
146+
"stability" => "stable",
147+
]
148+
]
149+
]));
150+
151+
$object = new TufFetcher();
152+
$result = $object->getLatestVersionForBranch(5);
153+
154+
$this->assertEquals("5.2.3", $result);
155+
}
156+
82157
protected function getStorageMock(array $targets)
83158
{
84159
$targetsMock = $this->getMockBuilder(TargetsMetadata::class)

0 commit comments

Comments
 (0)