Skip to content
Snippets Groups Projects
Commit 3ade8eb5 authored by Yash Rode's avatar Yash Rode Committed by Adam G-H
Browse files

Issue #3343463 by yash.rode: When finding an installed package by Drupal...

Issue #3343463 by yash.rode: When finding an installed package by Drupal project name, throw an exception if we find the same project name across multiple packages
parent a7dd3e6f
No related branches found
No related tags found
No related merge requests found
...@@ -61,6 +61,9 @@ final class InstalledPackage { ...@@ -61,6 +61,9 @@ final class InstalledPackage {
* `drupal-profile`. * `drupal-profile`.
* - The package's vendor is not `drupal`. * - The package's vendor is not `drupal`.
* - The project name could not otherwise be determined. * - The project name could not otherwise be determined.
*
* @throws \UnexpectedValueException
* Thrown if the same project name exists in more than one package.
*/ */
public function getProjectName(): ?string { public function getProjectName(): ?string {
// Only consider packages which are packaged by drupal.org and will be // Only consider packages which are packaged by drupal.org and will be
......
...@@ -118,14 +118,16 @@ final class InstalledPackagesList extends \ArrayObject { ...@@ -118,14 +118,16 @@ final class InstalledPackagesList extends \ArrayObject {
* be determined. * be determined.
*/ */
public function getPackageByDrupalProjectName(string $project_name): ?InstalledPackage { public function getPackageByDrupalProjectName(string $project_name): ?InstalledPackage {
$matching_package = NULL;
foreach ($this as $package) { foreach ($this as $package) {
if ($package->getProjectName() === $project_name) { if ($package->getProjectName() === $project_name) {
// @todo Throw an exception if we find more than one package matching if ($matching_package) {
// $project_name in https://drupal.org/i/3343463. throw new \UnexpectedValueException(sprintf("Project '%s' was found in packages '%s' and '%s'.", $project_name, $matching_package->name, $package->name));
return $package; }
$matching_package = $package;
} }
} }
return NULL; return $matching_package;
} }
/** /**
......
...@@ -120,6 +120,29 @@ class InstalledPackagesListTest extends PackageManagerKernelTestBase { ...@@ -120,6 +120,29 @@ class InstalledPackagesListTest extends PackageManagerKernelTestBase {
]); ]);
$this->assertNull($list->getPackageByDrupalProjectName('custom_module')); $this->assertNull($list->getPackageByDrupalProjectName('custom_module'));
$this->assertNull($list->getPackageByDrupalProjectName('custom_theme')); $this->assertNull($list->getPackageByDrupalProjectName('custom_theme'));
// The project name is repeated.
(new ActiveFixtureManipulator())
->addProjectAtPath('projects/duplicate_project')
->addProjectAtPath('projects/repeat/duplicate_project')
->commitChanges();
$list = new InstalledPackagesList([
'drupal/test_project1' => InstalledPackage::createFromArray([
'name' => 'drupal/test_project1',
'version' => '1.0.0',
'type' => 'drupal-module',
'path' => $projects_path . '/duplicate_project',
]),
'drupal/test_project2' => InstalledPackage::createFromArray([
'name' => 'drupal/test_project2',
'version' => '1.0.0',
'type' => 'drupal-module',
'path' => $projects_path . '/repeat/duplicate_project',
]),
]);
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage("Project 'duplicate_project' was found in packages 'drupal/test_project1' and 'drupal/test_project2'.");
$list->getPackageByDrupalProjectName('duplicate_project');
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment