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
1 merge request!100Issue #3246660: Move DiskSpaceValidator into Package Manager
......@@ -53,10 +53,9 @@ services:
tags:
- { name: event_subscriber }
automatic_updates.disk_space_validator:
class: Drupal\automatic_updates\Validator\DiskSpaceValidator
class: Drupal\automatic_updates\Validator\PackageManagerReadinessCheck
arguments:
- '@package_manager.path_locator'
- '@string_translation'
- '@package_manager.validator.disk_space'
tags:
- { name: event_subscriber }
automatic_updates.pending_updates_validator:
......
......@@ -93,3 +93,10 @@ services:
- '@string_translation'
tags:
- { 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
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\Component\FileSystem\FileSystem;
use Drupal\Component\Utility\Bytes;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\package_manager\PathLocator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Validates that there is enough free disk space to do automatic updates.
*/
class DiskSpaceValidator implements EventSubscriberInterface {
class DiskSpaceValidator implements StageValidatorInterface {
use StringTranslationTrait;
......@@ -97,12 +97,9 @@ class DiskSpaceValidator implements EventSubscriberInterface {
}
/**
* Checks that there is enough free space to perform automatic updates.
*
* @param \Drupal\automatic_updates\Event\ReadinessCheckEvent $event
* The event object.
* {@inheritdoc}
*/
public function checkDiskSpace(ReadinessCheckEvent $event): void {
public function validateStage(StageEvent $event): void {
$root_path = $this->pathLocator->getProjectRoot();
$vendor_path = $this->pathLocator->getVendorDirectory();
$messages = [];
......@@ -141,7 +138,7 @@ class DiskSpaceValidator implements EventSubscriberInterface {
if ($messages) {
$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;
$error = ValidationResult::createError($messages, $summary);
......@@ -164,7 +161,7 @@ class DiskSpaceValidator implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
ReadinessCheckEvent::class => 'checkDiskSpace',
PreCreateEvent::class => 'validateStage',
];
}
......
<?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\automatic_updates\Validator\DiskSpaceValidator;
use Drupal\Component\Utility\Bytes;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Tests\package_manager\Traits\ValidationTestTrait;
/**
* @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}
*/
protected static $modules = [
'automatic_updates',
'package_manager',
];
protected static $modules = ['package_manager'];
/**
* Data provider for ::testDiskSpaceValidation().
......@@ -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.');
$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.');
$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 [
'shared, vendor and temp sufficient, root insufficient' => [
......@@ -202,38 +200,12 @@ class DiskSpaceValidatorTest extends AutomaticUpdatesKernelTestBase {
};
$validator->sharedDisk = $shared_disk;
$validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space);
$this->container->set('automatic_updates.disk_space_validator', $validator);
$this->assertCheckerResultsFromManager($expected_results, TRUE);
}
}
/**
* Implements a translation manager in tests.
*
* @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 {
$stage = $this->prophesize('\Drupal\package_manager\Stage');
$event = new PreCreateEvent($stage->reveal());
$validator->validateStage($event);
/**
* {@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);
$this->assertValidationResultsEqual($expected_results, $event->getResults());
}
}
......@@ -8,13 +8,16 @@ use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
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
*
* @covers \Drupal\automatic_updates\Validator\PackageManagerReadinessCheck
*
* @see \Drupal\Tests\package_manager\Kernel\ComposerExecutableValidatorTest
* @see \Drupal\Tests\package_manager\Kernel\DiskSpaceValidatorTest
*/
class ComposerExecutableValidatorTest extends AutomaticUpdatesKernelTestBase {
class PackageManagerReadinessChecksTest extends AutomaticUpdatesKernelTestBase {
/**
* {@inheritdoc}
......@@ -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
// that it gets called with a readiness check event, when we run readiness
// checks.
$event = Argument::type(ReadinessCheckEvent::class);
$validator = $this->prophesize(StageValidatorInterface::class);
$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')
->run();
......
......@@ -32,8 +32,11 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
// 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
// result in drupal/core-recommended being updated.
$fixture_dir = __DIR__ . '/../../fixtures/fake-site';
$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');
$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