FIX: OutOfBoundsException when recipe-core isn't installed (fixes #11197)

This commit is contained in:
Loz Calver 2024-04-15 16:59:59 +01:00
parent 6743de4356
commit a53301f673
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');