From d8654de0b71ca43e656efd7deb0a8eb4b1eb9d08 Mon Sep 17 00:00:00 2001 From: Ted Bowman <41201-tedbow@users.noreply.drupalcode.org> Date: Wed, 10 May 2023 14:07:26 +0000 Subject: [PATCH] Issue #3358570 by phenaproxima: InstalledPackage::scanForProjectName() can cause PHP notices if the `project` key is not in the info file --- package_manager/src/InstalledPackage.php | 2 +- .../src/Kernel/InstalledPackagesListTest.php | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/package_manager/src/InstalledPackage.php b/package_manager/src/InstalledPackage.php index 17e357fa36..f3cff37081 100644 --- a/package_manager/src/InstalledPackage.php +++ b/package_manager/src/InstalledPackage.php @@ -98,7 +98,7 @@ final class InstalledPackage { $info = file_get_contents($match[0]); $info = Yaml::decode($info); - if (is_string($info['project']) && !empty($info['project'])) { + if (!empty($info['project'])) { return $info['project']; } } diff --git a/package_manager/tests/src/Kernel/InstalledPackagesListTest.php b/package_manager/tests/src/Kernel/InstalledPackagesListTest.php index a9cdbddab5..6a9fffd534 100644 --- a/package_manager/tests/src/Kernel/InstalledPackagesListTest.php +++ b/package_manager/tests/src/Kernel/InstalledPackagesListTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\package_manager\Kernel; +use Drupal\Component\Serialization\Yaml; use Drupal\fixture_manipulator\ActiveFixtureManipulator; use Drupal\package_manager\InstalledPackage; use Drupal\package_manager\InstalledPackagesList; @@ -121,6 +122,25 @@ class InstalledPackagesListTest extends PackageManagerKernelTestBase { $this->assertNull($list->getPackageByDrupalProjectName('custom_module')); $this->assertNull($list->getPackageByDrupalProjectName('custom_theme')); + // The `project` key has been removed from the info file. + (new ActiveFixtureManipulator()) + ->addProjectAtPath('projects/no_project_key') + ->commitChanges(); + $list = new InstalledPackagesList([ + 'drupal/no_project_key' => InstalledPackage::createFromArray([ + 'name' => 'drupal/no_project_key', + 'version' => '1.0.0', + 'type' => 'drupal-module', + 'path' => $projects_path . '/no_project_key', + ]), + ]); + $info_file = $list['drupal/no_project_key']->path . '/no_project_key.info.yml'; + $this->assertFileIsWritable($info_file); + $info = Yaml::decode(file_get_contents($info_file)); + unset($info['project']); + file_put_contents($info_file, Yaml::encode($info)); + $this->assertNull($list->getPackageByDrupalProjectName('no_project_key')); + // The project name is repeated. (new ActiveFixtureManipulator()) ->addProjectAtPath('projects/duplicate_project') -- GitLab