Skip to content
Snippets Groups Projects
Commit 7d40ee30 authored by omkar podey's avatar omkar podey Committed by Adam G-H
Browse files

Issue #3303900 by phenaproxima, omkar.podey: Remove PreApply check in...

Issue #3303900 by phenaproxima, omkar.podey: Remove PreApply check in PackagesInstalledWithComposerValidator
parent b7f76e54
No related branches found
No related tags found
1 merge request!502Issue #3303900: Remove PreApply check in PackagesInstalledWithComposerValidator
Showing
with 1 addition and 417 deletions
...@@ -13,12 +13,6 @@ services: ...@@ -13,12 +13,6 @@ services:
- '@datetime.time' - '@datetime.time'
- '@PhpTuf\ComposerStager\Infrastructure\Factory\Path\PathFactoryInterface' - '@PhpTuf\ComposerStager\Infrastructure\Factory\Path\PathFactoryInterface'
- '@package_manager.failure_marker' - '@package_manager.failure_marker'
automatic_updates_extensions.validator.packages_installed_with_composer:
class: Drupal\automatic_updates_extensions\Validator\PackagesInstalledWithComposerValidator
arguments:
- '@string_translation'
tags:
- { name: event_subscriber }
automatic_updates_extensions.validator.target_release: automatic_updates_extensions.validator.target_release:
class: Drupal\automatic_updates_extensions\Validator\UpdateReleaseValidator class: Drupal\automatic_updates_extensions\Validator\UpdateReleaseValidator
tags: tags:
......
<?php
namespace Drupal\automatic_updates_extensions\Validator;
use Composer\Package\PackageInterface;
use Drupal\automatic_updates_extensions\ExtensionUpdater;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\package_manager\Event\PreApplyEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Validates packages are installed via Composer.
*
* @todo Remove this validator completely in https://www.drupal.org/i/3303900.
*/
class PackagesInstalledWithComposerValidator implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* Constructs a InstalledPackagesValidator object.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The translation service.
*/
public function __construct(TranslationInterface $translation) {
$this->setStringTranslation($translation);
}
/**
* Validates that packages are installed with composer or not.
*
* @param \Drupal\package_manager\Event\PreApplyEvent $event
* The event object.
*/
public function checkPackagesInstalledWithComposer(PreApplyEvent $event): void {
$stage = $event->getStage();
if (!$stage instanceof ExtensionUpdater) {
return;
}
$missing_packages = $this->getPackagesNotInstalledWithComposer($event);
if ($missing_packages) {
// Removing drupal/ from package names for better user presentation.
$missing_projects = str_replace('drupal/', '', array_keys($missing_packages));
$event->addError($missing_projects, $this->t('Automatic Updates can only update projects that were installed via Composer. The following packages are not installed through composer:'));
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
PreApplyEvent::class => 'checkPackagesInstalledWithComposer',
];
}
/**
* Gets the packages which aren't installed via composer.
*
* @param \Drupal\package_manager\Event\PreApplyEvent $event
* The event object.
*
* @return \Composer\Package\PackageInterface[]
* Packages not installed via composer.
*/
protected function getPackagesNotInstalledWithComposer(PreApplyEvent $event): array {
$stage = $event->getStage();
$active_composer = $stage->getActiveComposer();
$missing_packages = $stage->getStageComposer()->getPackagesNotIn($active_composer);
// The core update system can only fetch release information for modules,
// themes, or profiles that are in the active code base (whether they're
// installed or not). If a package is not one of those types, ignore it
// even if its vendor namespace is `drupal`.
$types = [
'drupal-module',
'drupal-theme',
'drupal-profile',
];
$filter = function (PackageInterface $package) use ($types): bool {
return in_array($package->getType(), $types, TRUE);
};
$missing_packages = array_filter($missing_packages, $filter);
// The core update system can only fetch release information for drupal
// projects, so saving only the packages whose name starts with drupal/.
$missing_packages = array_filter($missing_packages, function (string $key) {
return str_starts_with($key, 'drupal/');
}, ARRAY_FILTER_USE_KEY);
return $missing_packages;
}
}
{
"packages": [
{
"name": "drupal/core-recommended",
"version": "9.8.0",
"require": {
"drupal/core": "9.8.0"
}
},
{
"name": "drupal/core",
"version": "9.8.0"
},
{
"name": "drupal/my_module",
"version": "9.8.0",
"type": "drupal-module"
},
{
"name": "drupal/my_dev_module",
"version": "9.8.1",
"type": "drupal-module"
},
{
"name": "drupal/existing_module",
"version": "9.8.0",
"type": "drupal-module"
},
{
"name": "drupal/existing_theme",
"version": "9.8.0",
"type": "drupal-theme"
},
{
"name": "drupal/existing_profile",
"version": "9.8.0",
"type": "drupal-profile"
}
],
"dev": true,
"dev-package-names": [
"drupal/my_dev_module"
]
}
{
"packages": [
{
"name": "drupal/core-recommended",
"version": "9.8.0",
"require": {
"drupal/core": "9.8.0"
}
},
{
"name": "drupal/core",
"version": "9.8.0"
},
{
"name": "drupal/my_module",
"version": "9.8.0",
"type": "drupal-module"
},
{
"name": "drupal/my_dev_module",
"version": "9.8.1",
"type": "drupal-module"
},
{
"name": "drupal/new_module",
"version": "9.8.0",
"type": "drupal-module"
}
],
"dev": true,
"dev-package-names": [
"drupal/my_dev_module"
]
}
{
"packages": [
{
"name": "drupal/core-recommended",
"version": "9.8.0",
"require": {
"drupal/core": "9.8.0"
}
},
{
"name": "drupal/core",
"version": "9.8.0"
},
{
"name": "drupal/my_module",
"version": "9.8.0",
"type": "drupal-module"
},
{
"name": "drupal/my_dev_module",
"version": "9.8.1",
"type": "drupal-module"
},
{
"name": "drupal/new_module",
"version": "9.8.0",
"type": "drupal-module"
},
{
"name": "not-drupal/new_module1",
"version": "9.8.0",
"type": "drupal-module"
},
{
"name": "drupal/new_theme",
"version": "9.8.0",
"type": "drupal-theme"
},
{
"name": "drupal/new_profile",
"version": "9.8.0",
"type": "drupal-profile"
},
{
"name": "drupal/new_dependency",
"version": "9.8.0",
"type": "drupal-library"
}
],
"dev": true,
"dev-package-names": [
"drupal/my_dev_module"
]
}
{
"packages": [
{
"name": "drupal/core-recommended",
"version": "9.8.0",
"require": {
"drupal/core": "9.8.0"
}
},
{
"name": "drupal/core",
"version": "9.8.0"
},
{
"name": "drupal/my_module",
"version": "9.8.0",
"type": "drupal-module"
},
{
"name": "drupal/my_dev_module",
"version": "9.8.1",
"type": "drupal-module"
},
{
"name": "drupal/new_profile",
"version": "9.8.0",
"type": "drupal-profile"
}
],
"dev": true,
"dev-package-names": [
"drupal/my_dev_module"
]
}
{
"packages": [
{
"name": "drupal/core-recommended",
"version": "9.8.0",
"require": {
"drupal/core": "9.8.0"
}
},
{
"name": "drupal/core",
"version": "9.8.0"
},
{
"name": "drupal/my_module",
"version": "9.8.0",
"type": "drupal-module"
},
{
"name": "drupal/my_dev_module",
"version": "9.8.1",
"type": "drupal-module"
},
{
"name": "drupal/new_theme",
"version": "9.8.0",
"type": "drupal-theme"
}
],
"dev": true,
"dev-package-names": [
"drupal/my_dev_module"
]
}
...@@ -60,8 +60,6 @@ END; ...@@ -60,8 +60,6 @@ END;
// Use the API endpoint to create a stage and update the 'new_module' module // Use the API endpoint to create a stage and update the 'new_module' module
// to 1.1.0. // to 1.1.0.
// @see \Drupal\automatic_updates_extensions_test_api\ApiController::run() // @see \Drupal\automatic_updates_extensions_test_api\ApiController::run()
// There will be error in updating as this module is not installed
// via composer @see \Drupal\Tests\automatic_updates_extensions\Kernel\Validator\PackagesInstalledWithComposerValidatorTest.
$query = http_build_query([ $query = http_build_query([
'projects' => [ 'projects' => [
'new_module' => '1.1.0', 'new_module' => '1.1.0',
......
...@@ -65,12 +65,8 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase { ...@@ -65,12 +65,8 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function setUp(): void { protected function setUp(): void {
// In this test class, some modules are added and this validator will
// complain because these are not installed via composer. This validator
// already has test coverage.
// @see \Drupal\Tests\automatic_updates_extensions\Build\ModuleUpdateTest
$this->disableValidators[] = 'automatic_updates_extensions.validator.packages_installed_with_composer';
parent::setUp(); parent::setUp();
$user = $this->createUser([ $user = $this->createUser([
'administer site configuration', 'administer site configuration',
'administer software updates', 'administer software updates',
......
<?php
namespace Drupal\Tests\automatic_updates_extensions\Kernel\Validator;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\ValidationResult;
use Drupal\Tests\automatic_updates_extensions\Kernel\AutomaticUpdatesExtensionsKernelTestBase;
/**
* Validates the installed packages via composer after an update.
*
* @coversDefaultClass \Drupal\automatic_updates_extensions\Validator\PackagesInstalledWithComposerValidator
*
* @group automatic_updates_extensions
*/
class PackagesInstalledWithComposerValidatorTest extends AutomaticUpdatesExtensionsKernelTestBase {
/**
* {@inheritdoc}
*/
protected function setUp(): void {
// In this test, we don't care whether the updated projects are secure and
// supported.
$this->disableValidators[] = 'automatic_updates_extensions.validator.target_release';
$this->disableValidators[] = 'package_manager.validator.supported_releases';
// @todo The validator being tested covers the same cases as the following
// validator. PackagesInstalledWithComposerValidatorTest will be removed
// in https://drupal.org/i/3303900.
$this->disableValidators[] = 'package_manager.validator.overwrite_existing_packages';
parent::setUp();
}
/**
* Data provider for testPreApplyException().
*
* @return mixed[][]
* The test cases.
*/
public function providerPreApplyException(): array {
$summary = t('Automatic Updates can only update projects that were installed via Composer. The following packages are not installed through composer:');
$fixtures_folder = __DIR__ . '/../../../fixtures/packages_installed_with_composer_validator';
return [
'module not installed via Composer' => [
"$fixtures_folder/module_not_installed_stage",
[
ValidationResult::createError(['new_module'], $summary),
],
],
'theme not installed via Composer' => [
"$fixtures_folder/theme_not_installed_stage",
[
ValidationResult::createError(['new_theme'], $summary),
],
],
'profile not installed via Composer' => [
"$fixtures_folder/profile_not_installed_stage",
[
ValidationResult::createError(['new_profile'], $summary),
],
],
// The `drupal/new_dependency` package won't show up in the error because
// its type is `drupal-library`, and the validator only considers the
// `drupal-module`, `drupal-theme`, and `drupal-profile` package types.
// The `not-drupal/new_module1` package won't show up either, even though
// its type is `drupal-module`, because it doesn't start with `drupal/`.
// @see \Drupal\automatic_updates_extensions\Validator\PackagesInstalledWithComposerValidator
'module, theme, and profile not installed via Composer' => [
"$fixtures_folder/module_theme_profile_dependency_not_installed_stage",
[
ValidationResult::createError(
['new_module', 'new_theme', 'new_profile'],
$summary
),
],
],
];
}
/**
* Tests the packages installed with composer during pre-apply.
*
* @param string $stage_dir
* Path of fixture stage directory. It will be used as the virtual project's
* stage directory.
* @param array $expected_results
* The expected validation results.
*
* @dataProvider providerPreApplyException
*/
public function testPreApplyException(string $stage_dir, array $expected_results): void {
$active_dir = __DIR__ . '/../../../fixtures/packages_installed_with_composer_validator/active';
$this->copyFixtureFolderToActiveDirectory($active_dir);
$this->copyFixtureFolderToStageDirectoryOnApply($stage_dir);
$this->assertUpdateResults(['my_module' => '9.8.1'], $expected_results, PreApplyEvent::class);
}
}
...@@ -14,14 +14,6 @@ use Drupal\Tests\automatic_updates_extensions\Kernel\AutomaticUpdatesExtensionsK ...@@ -14,14 +14,6 @@ use Drupal\Tests\automatic_updates_extensions\Kernel\AutomaticUpdatesExtensionsK
*/ */
class UpdateReleaseValidatorTest extends AutomaticUpdatesExtensionsKernelTestBase { class UpdateReleaseValidatorTest extends AutomaticUpdatesExtensionsKernelTestBase {
/**
* {@inheritdoc}
*/
protected function setUp(): void {
$this->disableValidators[] = 'automatic_updates_extensions.validator.packages_installed_with_composer';
parent::setUp();
}
/** /**
* Data provider for testPreCreateException(). * Data provider for testPreCreateException().
* *
......
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