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

Issue #3364725 by omkar.podey, phenaproxima, Wim Leers: The $format_result...

Issue #3364725 by omkar.podey, phenaproxima, Wim Leers: The $format_result closure in AdminStatusCheckMessages::displayResults() expects to return a translatable string, which will not be the case for validation results created from throwables
parent 36514d5e
No related branches found
No related tags found
No related merge requests found
...@@ -248,12 +248,15 @@ ...@@ -248,12 +248,15 @@
* cycle from proceeding any further. If a validator encounters an exception, * cycle from proceeding any further. If a validator encounters an exception,
* it can use ::addErrorFromThrowable() instead of ::addError(). During status * it can use ::addErrorFromThrowable() instead of ::addError(). During status
* checks, validators can call ::addWarning() for less severe problems -- * checks, validators can call ::addWarning() for less severe problems --
* warnings will NOT stop the stage life cycle. * warnings will NOT stop the stage life cycle. All three are convenience
* methods for equivalent \Drupal\package_manager\ValidationResult constructors,
* which can then be added to the event using ::addResult().
* *
* @see \Drupal\package_manager\ValidationResult * @see \Drupal\package_manager\ValidationResult
* @see \Drupal\package_manager\Event\PreOperationStageEvent::addError() * @see \Drupal\package_manager\Event\PreOperationStageEvent::addError()
* @see \Drupal\package_manager\Event\PreOperationStageEvent::addErrorFromThrowable() * @see \Drupal\package_manager\Event\PreOperationStageEvent::addErrorFromThrowable()
* @see \Drupal\package_manager\Event\StatusCheckEvent::addWarning() * @see \Drupal\package_manager\Event\StatusCheckEvent::addWarning()
* @see \Drupal\package_manager\Event\PreOperationStageEvent::addResult()
* *
* @section sec_excluded_paths Excluding files from stage operations * @section sec_excluded_paths Excluding files from stage operations
* Certain files are never copied into the stage directory because they are * Certain files are never copied into the stage directory because they are
......
...@@ -40,7 +40,7 @@ abstract class PreOperationStageEvent extends StageEvent { ...@@ -40,7 +40,7 @@ abstract class PreOperationStageEvent extends StageEvent {
} }
/** /**
* Adds error information to the event. * Convenience method, adds error validation result.
* *
* @param \Drupal\Core\StringTranslation\TranslatableMarkup[] $messages * @param \Drupal\Core\StringTranslation\TranslatableMarkup[] $messages
* The error messages. * The error messages.
...@@ -49,11 +49,11 @@ abstract class PreOperationStageEvent extends StageEvent { ...@@ -49,11 +49,11 @@ abstract class PreOperationStageEvent extends StageEvent {
* is more than one message. * is more than one message.
*/ */
public function addError(array $messages, ?TranslatableMarkup $summary = NULL): void { public function addError(array $messages, ?TranslatableMarkup $summary = NULL): void {
$this->results[] = ValidationResult::createError(array_values($messages), $summary); $this->addResult(ValidationResult::createError(array_values($messages), $summary));
} }
/** /**
* Adds an error from a throwable. * Convenience method, adds an error validation result from a throwable.
* *
* @param \Throwable $throwable * @param \Throwable $throwable
* The throwable. * The throwable.
...@@ -61,7 +61,24 @@ abstract class PreOperationStageEvent extends StageEvent { ...@@ -61,7 +61,24 @@ abstract class PreOperationStageEvent extends StageEvent {
* (optional) The summary of error messages. * (optional) The summary of error messages.
*/ */
public function addErrorFromThrowable(\Throwable $throwable, ?TranslatableMarkup $summary = NULL): void { public function addErrorFromThrowable(\Throwable $throwable, ?TranslatableMarkup $summary = NULL): void {
$this->results[] = ValidationResult::createErrorFromThrowable($throwable, $summary); $this->addResult(ValidationResult::createErrorFromThrowable($throwable, $summary));
}
/**
* Adds a validation result to the event.
*
* @param \Drupal\package_manager\ValidationResult $result
* The validation result to add.
*
* @throws \InvalidArgumentException
* Thrown if the validation result is not an error.
*/
public function addResult(ValidationResult $result): void {
// Only errors are allowed for this event.
if ($result->severity !== SystemManager::REQUIREMENT_ERROR) {
throw new \InvalidArgumentException('Only errors are allowed.');
}
$this->results[] = $result;
} }
/** /**
......
...@@ -60,7 +60,15 @@ final class StatusCheckEvent extends PreOperationStageEvent { ...@@ -60,7 +60,15 @@ final class StatusCheckEvent extends PreOperationStageEvent {
* message, optional otherwise. * message, optional otherwise.
*/ */
public function addWarning(array $messages, ?TranslatableMarkup $summary = NULL): void { public function addWarning(array $messages, ?TranslatableMarkup $summary = NULL): void {
$this->results[] = ValidationResult::createWarning($messages, $summary); $this->addResult(ValidationResult::createWarning($messages, $summary));
}
/**
* {@inheritdoc}
*/
public function addResult(ValidationResult $result): void {
// Override the parent to also allow warnings.
$this->results[] = $result;
} }
} }
...@@ -15,7 +15,6 @@ use Drupal\package_manager\Event\PreDestroyEvent; ...@@ -15,7 +15,6 @@ use Drupal\package_manager\Event\PreDestroyEvent;
use Drupal\package_manager\Event\PreRequireEvent; use Drupal\package_manager\Event\PreRequireEvent;
use Drupal\package_manager\Event\StageEvent; use Drupal\package_manager\Event\StageEvent;
use Drupal\package_manager\Event\StatusCheckEvent; use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\system\SystemManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/** /**
...@@ -144,12 +143,7 @@ class TestSubscriber implements EventSubscriberInterface { ...@@ -144,12 +143,7 @@ class TestSubscriber implements EventSubscriberInterface {
} }
/** @var \Drupal\package_manager\ValidationResult $result */ /** @var \Drupal\package_manager\ValidationResult $result */
foreach ($results as $result) { foreach ($results as $result) {
if ($result->severity === SystemManager::REQUIREMENT_ERROR) { $event->addResult($result);
$event->addError($result->messages, $result->summary);
}
else {
$event->addWarning($result->messages, $result->summary);
}
} }
} }
......
...@@ -20,7 +20,6 @@ use Drupal\package_manager\PathLocator; ...@@ -20,7 +20,6 @@ use Drupal\package_manager\PathLocator;
use Drupal\package_manager\StatusCheckTrait; use Drupal\package_manager\StatusCheckTrait;
use Drupal\package_manager\Validator\DiskSpaceValidator; use Drupal\package_manager\Validator\DiskSpaceValidator;
use Drupal\package_manager\StageBase; use Drupal\package_manager\StageBase;
use Drupal\system\SystemManager;
use Drupal\Tests\package_manager\Traits\AssertPreconditionsTrait; use Drupal\Tests\package_manager\Traits\AssertPreconditionsTrait;
use Drupal\Tests\package_manager\Traits\FixtureManipulatorTrait; use Drupal\Tests\package_manager\Traits\FixtureManipulatorTrait;
use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait; use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait;
...@@ -456,12 +455,7 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { ...@@ -456,12 +455,7 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
*/ */
protected function createStageEventExceptionFromResults(array $expected_results, string $event_class = PreCreateEvent::class, StageBase $stage = NULL): StageEventException { protected function createStageEventExceptionFromResults(array $expected_results, string $event_class = PreCreateEvent::class, StageBase $stage = NULL): StageEventException {
$event = new $event_class($stage ?? $this->createStage(), []); $event = new $event_class($stage ?? $this->createStage(), []);
array_walk($expected_results, $event->addResult(...));
foreach ($expected_results as $result) {
if ($result->severity === SystemManager::REQUIREMENT_ERROR) {
$event->addError($result->messages, $result->summary);
}
}
return new StageEventException($event); return new StageEventException($event);
} }
......
...@@ -15,6 +15,7 @@ use Drupal\package_manager\Event\PreDestroyEvent; ...@@ -15,6 +15,7 @@ use Drupal\package_manager\Event\PreDestroyEvent;
use Drupal\package_manager\Event\PreOperationStageEvent; use Drupal\package_manager\Event\PreOperationStageEvent;
use Drupal\package_manager\Event\PreRequireEvent; use Drupal\package_manager\Event\PreRequireEvent;
use Drupal\package_manager\Event\StageEvent; use Drupal\package_manager\Event\StageEvent;
use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\package_manager\Exception\StageEventException; use Drupal\package_manager\Exception\StageEventException;
use Drupal\package_manager\ValidationResult; use Drupal\package_manager\ValidationResult;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
...@@ -155,6 +156,35 @@ class StageEventsTest extends PackageManagerKernelTestBase implements EventSubsc ...@@ -155,6 +156,35 @@ class StageEventsTest extends PackageManagerKernelTestBase implements EventSubsc
$this->assertResults([$result], $event_class); $this->assertResults([$result], $event_class);
} }
/**
* Tests adding validation results to events.
*/
public function testAddResult(): void {
$stage = $this->createStage();
$error = ValidationResult::createError([
t('Burn, baby, burn!'),
]);
$warning = ValidationResult::createWarning([
t('The path ahead is scary...'),
]);
// Status check events can accept both errors and warnings.
$event = new StatusCheckEvent($stage, NULL);
$event->addResult($error);
$event->addResult($warning);
$this->assertSame([$error, $warning], $event->getResults());
// Other stage events will accept errors, but throw an exception if you try
// to add a warning.
$event = new PreCreateEvent($stage, []);
$event->addResult($error);
$this->assertSame([$error], $event->getResults());
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only errors are allowed.');
$event->addResult($warning);
}
/** /**
* Tests that pre- and post-require events have access to the package lists. * Tests that pre- and post-require events have access to the package lists.
*/ */
......
...@@ -201,7 +201,7 @@ final class AdminStatusCheckMessages implements ContainerInjectionInterface { ...@@ -201,7 +201,7 @@ final class AdminStatusCheckMessages implements ContainerInjectionInterface {
// multiple messages. This is because, on regular admin pages, we merely // multiple messages. This is because, on regular admin pages, we merely
// want to alert users that problems exist, but not burden them with the // want to alert users that problems exist, but not burden them with the
// details. They can get those on the status report and updater form. // details. They can get those on the status report and updater form.
$format_result = function (ValidationResult $result): TranslatableMarkup { $format_result = function (ValidationResult $result): TranslatableMarkup|string {
$messages = $result->messages; $messages = $result->messages;
return $result->summary ?: reset($messages); return $result->summary ?: reset($messages);
}; };
......
...@@ -76,6 +76,21 @@ class UpdateErrorTest extends UpdaterFormTestBase { ...@@ -76,6 +76,21 @@ class UpdateErrorTest extends UpdaterFormTestBase {
$assert_session->buttonExists('Cancel update'); $assert_session->buttonExists('Cancel update');
} }
/**
* Tests that throwables will be displayed properly.
*/
public function testDisplayErrorCreatedFromThrowable(): void {
$throwable = new \Exception("I want to be the pirate king because he's the freest man alive.");
$result = ValidationResult::createErrorFromThrowable($throwable);
TestSubscriber1::setTestResult([$result], StatusCheckEvent::class);
$this->drupalGet('/admin/reports/status');
$this->clickLink('Rerun readiness checks');
$this->drupalGet('/admin');
$assert_session = $this->assertSession();
$assert_session->statusCodeEquals(200);
$assert_session->statusMessageContains($throwable->getMessage(), 'error');
}
/** /**
* Tests the display of errors and warnings during status check. * Tests the display of errors and warnings during status check.
*/ */
......
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