Skip to content
Snippets Groups Projects
Commit 40028a69 authored by Adam G-H's avatar Adam G-H
Browse files

Issue #3246660 by phenaproxima: Move DiskSpaceValidator into Package Manager

parent c959ebb2
No related branches found
No related tags found
No related merge requests found
...@@ -53,10 +53,9 @@ services: ...@@ -53,10 +53,9 @@ services:
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
automatic_updates.disk_space_validator: automatic_updates.disk_space_validator:
class: Drupal\automatic_updates\Validator\DiskSpaceValidator class: Drupal\automatic_updates\Validator\PackageManagerReadinessCheck
arguments: arguments:
- '@package_manager.path_locator' - '@package_manager.validator.disk_space'
- '@string_translation'
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
automatic_updates.pending_updates_validator: automatic_updates.pending_updates_validator:
......
...@@ -93,3 +93,10 @@ services: ...@@ -93,3 +93,10 @@ services:
- '@string_translation' - '@string_translation'
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
package_manager.validator.disk_space:
class: Drupal\package_manager\EventSubscriber\DiskSpaceValidator
arguments:
- '@package_manager.path_locator'
- '@string_translation'
tags:
- { name: event_subscriber }
<?php <?php
namespace Drupal\automatic_updates\Validator; namespace Drupal\package_manager\EventSubscriber;
use Drupal\automatic_updates\Event\ReadinessCheckEvent; use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\StageEvent;
use Drupal\package_manager\ValidationResult; use Drupal\package_manager\ValidationResult;
use Drupal\Component\FileSystem\FileSystem; use Drupal\Component\FileSystem\FileSystem;
use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Bytes;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\package_manager\PathLocator; use Drupal\package_manager\PathLocator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/** /**
* Validates that there is enough free disk space to do automatic updates. * Validates that there is enough free disk space to do automatic updates.
*/ */
class DiskSpaceValidator implements EventSubscriberInterface { class DiskSpaceValidator implements StageValidatorInterface {
use StringTranslationTrait; use StringTranslationTrait;
...@@ -97,12 +97,9 @@ class DiskSpaceValidator implements EventSubscriberInterface { ...@@ -97,12 +97,9 @@ class DiskSpaceValidator implements EventSubscriberInterface {
} }
/** /**
* Checks that there is enough free space to perform automatic updates. * {@inheritdoc}
*
* @param \Drupal\automatic_updates\Event\ReadinessCheckEvent $event
* The event object.
*/ */
public function checkDiskSpace(ReadinessCheckEvent $event): void { public function validateStage(StageEvent $event): void {
$root_path = $this->pathLocator->getProjectRoot(); $root_path = $this->pathLocator->getProjectRoot();
$vendor_path = $this->pathLocator->getVendorDirectory(); $vendor_path = $this->pathLocator->getVendorDirectory();
$messages = []; $messages = [];
...@@ -141,7 +138,7 @@ class DiskSpaceValidator implements EventSubscriberInterface { ...@@ -141,7 +138,7 @@ class DiskSpaceValidator implements EventSubscriberInterface {
if ($messages) { if ($messages) {
$summary = count($messages) > 1 $summary = count($messages) > 1
? $this->t("There is not enough disk space to perform an automatic update.") ? $this->t("There is not enough disk space to create a staging area.")
: NULL; : NULL;
$error = ValidationResult::createError($messages, $summary); $error = ValidationResult::createError($messages, $summary);
...@@ -164,7 +161,7 @@ class DiskSpaceValidator implements EventSubscriberInterface { ...@@ -164,7 +161,7 @@ class DiskSpaceValidator implements EventSubscriberInterface {
*/ */
public static function getSubscribedEvents() { public static function getSubscribedEvents() {
return [ return [
ReadinessCheckEvent::class => 'checkDiskSpace', PreCreateEvent::class => 'validateStage',
]; ];
} }
......
<?php <?php
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation; namespace Drupal\Tests\package_manager\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\EventSubscriber\DiskSpaceValidator;
use Drupal\package_manager\ValidationResult; use Drupal\package_manager\ValidationResult;
use Drupal\automatic_updates\Validator\DiskSpaceValidator;
use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Bytes;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase; use Drupal\Tests\package_manager\Traits\ValidationTestTrait;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
/** /**
* @covers \Drupal\automatic_updates\Validator\DiskSpaceValidator * @covers \Drupal\package_manager\EventSubscriber\DiskSpaceValidator
* *
* @group automatic_updates * @group package_manager
*/ */
class DiskSpaceValidatorTest extends AutomaticUpdatesKernelTestBase { class DiskSpaceValidatorTest extends KernelTestBase {
use ValidationTestTrait;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected static $modules = [ protected static $modules = ['package_manager'];
'automatic_updates',
'package_manager',
];
/** /**
* Data provider for ::testDiskSpaceValidation(). * Data provider for ::testDiskSpaceValidation().
...@@ -35,7 +33,7 @@ class DiskSpaceValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -35,7 +33,7 @@ class DiskSpaceValidatorTest extends AutomaticUpdatesKernelTestBase {
$root_insufficient = t('Drupal root filesystem "root" has insufficient space. There must be at least 1024 megabytes free.'); $root_insufficient = t('Drupal root filesystem "root" has insufficient space. There must be at least 1024 megabytes free.');
$vendor_insufficient = t('Vendor filesystem "vendor" has insufficient space. There must be at least 1024 megabytes free.'); $vendor_insufficient = t('Vendor filesystem "vendor" has insufficient space. There must be at least 1024 megabytes free.');
$temp_insufficient = t('Directory "temp" has insufficient space. There must be at least 1024 megabytes free.'); $temp_insufficient = t('Directory "temp" has insufficient space. There must be at least 1024 megabytes free.');
$summary = t("There is not enough disk space to perform an automatic update."); $summary = t("There is not enough disk space to create a staging area.");
return [ return [
'shared, vendor and temp sufficient, root insufficient' => [ 'shared, vendor and temp sufficient, root insufficient' => [
...@@ -202,38 +200,12 @@ class DiskSpaceValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -202,38 +200,12 @@ class DiskSpaceValidatorTest extends AutomaticUpdatesKernelTestBase {
}; };
$validator->sharedDisk = $shared_disk; $validator->sharedDisk = $shared_disk;
$validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space); $validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space);
$this->container->set('automatic_updates.disk_space_validator', $validator);
$this->assertCheckerResultsFromManager($expected_results, TRUE);
}
} $stage = $this->prophesize('\Drupal\package_manager\Stage');
/** $event = new PreCreateEvent($stage->reveal());
* Implements a translation manager in tests. $validator->validateStage($event);
*
* @todo Copied from core/modules/user/tests/src/Unit/PermissionHandlerTest.php
* when moving to core open an issue consolidate this test class.
*/
class TestTranslationManager implements TranslationInterface {
/** $this->assertValidationResultsEqual($expected_results, $event->getResults());
* {@inheritdoc}
*/
public function translate($string, array $args = [], array $options = []) {
return new TranslatableMarkup($string, $args, $options, $this);
}
/**
* {@inheritdoc}
*/
public function translateString(TranslatableMarkup $translated_string) {
return $translated_string->getUntranslatedString();
}
/**
* {@inheritdoc}
*/
public function formatPlural($count, $singular, $plural, array $args = [], array $options = []) {
return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this);
} }
} }
...@@ -8,13 +8,16 @@ use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase; ...@@ -8,13 +8,16 @@ use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Prophecy\Argument; use Prophecy\Argument;
/** /**
* Tests that the Composer executable is validated during readiness checking. * Tests that Package Manager validators are invoked during readiness checking.
* *
* @group automatic_updates * @group automatic_updates
* *
* @covers \Drupal\automatic_updates\Validator\PackageManagerReadinessCheck
*
* @see \Drupal\Tests\package_manager\Kernel\ComposerExecutableValidatorTest * @see \Drupal\Tests\package_manager\Kernel\ComposerExecutableValidatorTest
* @see \Drupal\Tests\package_manager\Kernel\DiskSpaceValidatorTest
*/ */
class ComposerExecutableValidatorTest extends AutomaticUpdatesKernelTestBase { class PackageManagerReadinessChecksTest extends AutomaticUpdatesKernelTestBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -25,16 +28,34 @@ class ComposerExecutableValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -25,16 +28,34 @@ class ComposerExecutableValidatorTest extends AutomaticUpdatesKernelTestBase {
]; ];
/** /**
* Tests that the Composer executable is validated during readiness checking. * Data provider for ::testValidatorInvoked().
*
* @return string[][]
* Sets of arguments to pass to the test method.
*/
public function providerValidatorInvoked(): array {
return [
['package_manager.validator.composer_executable'],
['package_manager.validator.disk_space'],
];
}
/**
* Tests that a Package Manager validator is invoked during readiness checks.
*
* @param string $service_id
* The service ID of the validator that should be invoked.
*
* @dataProvider providerValidatorInvoked
*/ */
public function testComposerExecutableIsValidated(): void { public function testValidatorInvoked(string $service_id): void {
// Set up a mocked version of the Composer executable validator, to prove // Set up a mocked version of the Composer executable validator, to prove
// that it gets called with a readiness check event, when we run readiness // that it gets called with a readiness check event, when we run readiness
// checks. // checks.
$event = Argument::type(ReadinessCheckEvent::class); $event = Argument::type(ReadinessCheckEvent::class);
$validator = $this->prophesize(StageValidatorInterface::class); $validator = $this->prophesize(StageValidatorInterface::class);
$validator->validateStage($event)->shouldBeCalled(); $validator->validateStage($event)->shouldBeCalled();
$this->container->set('package_manager.validator.composer_executable', $validator->reveal()); $this->container->set($service_id, $validator->reveal());
$this->container->get('automatic_updates.readiness_validation_manager') $this->container->get('automatic_updates.readiness_validation_manager')
->run(); ->run();
......
...@@ -32,8 +32,11 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase { ...@@ -32,8 +32,11 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
// Point to a fake site which requires Drupal core via a distribution. The // Point to a fake site which requires Drupal core via a distribution. The
// lock file should be scanned to determine the core packages, which should // lock file should be scanned to determine the core packages, which should
// result in drupal/core-recommended being updated. // result in drupal/core-recommended being updated.
$fixture_dir = __DIR__ . '/../../fixtures/fake-site';
$locator = $this->prophesize(PathLocator::class); $locator = $this->prophesize(PathLocator::class);
$locator->getActiveDirectory()->willReturn(__DIR__ . '/../../fixtures/fake-site'); $locator->getActiveDirectory()->willReturn($fixture_dir);
$locator->getProjectRoot()->willReturn($fixture_dir);
$locator->getVendorDirectory()->willReturn($fixture_dir);
$locator->getStageDirectory()->willReturn('/tmp'); $locator->getStageDirectory()->willReturn('/tmp');
$this->container->set('package_manager.path_locator', $locator->reveal()); $this->container->set('package_manager.path_locator', $locator->reveal());
......
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