Merge pull request #11199 from kinglozzer/11197-oob-exception

FIX: OutOfBoundsException when recipe-core isn't installed (fixes #11197)
This commit is contained in:
Guy Sartorelli 2024-04-16 13:32:21 +12:00 committed by GitHub
commit 908bdcfc31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View File

@ -189,6 +189,9 @@ class VersionProvider
{
$versions = [];
foreach ($modules as $module) {
if (!InstalledVersions::isInstalled($module)) {
continue;
}
$versions[$module] = InstalledVersions::getPrettyVersion($module);
}
return $versions;

View File

@ -102,6 +102,27 @@ class VersionProviderTest extends SapphireTest
$this->assertStringNotContainsString('Framework: 1.2.3', $result);
}
public function testGetModuleVersionWhenPackageMayNotBeInstalled()
{
if (!class_exists(VersionParser::class)) {
$this->markTestSkipped('This test requires composer/semver to be installed');
}
$provider = $this->getProvider();
// VersionProvider::getModuleVersion() will loop over the modules defined in the "modules" config value, which
// may sometimes include packages that are optional (e.g. recipe-core). This tests that the version can still
// be found even if non-existent modules are encountered
Config::modify()->set(VersionProvider::class, 'modules', [
'this/module/cannot/possibly/exist' => 'Oopsies',
'silverstripe/framework' => 'Framework',
'silverstripe/another-module-that-does-not-exist' => 'Sapphire',
]);
$moduleVersion = $provider->getModuleVersion('silverstripe/framework');
$parser = new VersionParser();
$this->assertIsString($parser->normalize($moduleVersion), "Expected a valid semver but got $moduleVersion");
$result = $provider->getVersion();
$this->assertStringNotContainsString('Framework: 1.2.3', $result);
}
private function clearCache()
{
$cache = Injector::inst()->get(CacheInterface::class . '.VersionProvider');