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

Issue #3314803 by yash.rode: Move ReadinessTrait::getOverallSeverity() into ValidationResult

parent f2980dd7
No related branches found
No related tags found
No related merge requests found
......@@ -179,7 +179,7 @@ final class UpdateReady extends FormBase {
$this->displayResults($results, $this->messenger(), $this->renderer);
// If any errors occurred, return the form early so the user cannot
// continue.
if ($this->getOverallSeverity($results) === SystemManager::REQUIREMENT_ERROR) {
if (ValidationResult::getOverallSeverity($results) === SystemManager::REQUIREMENT_ERROR) {
return $form;
}
}
......
......@@ -16,6 +16,7 @@ use Drupal\Core\Url;
use Drupal\package_manager\Exception\ApplyFailedException;
use Drupal\package_manager\FailureMarker;
use Drupal\package_manager\ProjectInfo;
use Drupal\package_manager\ValidationResult;
use Drupal\system\SystemManager;
use Drupal\update\UpdateManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -170,7 +171,7 @@ final class UpdaterForm extends FormBase {
$results = $event->getResults();
}
$this->displayResults($results, $this->messenger(), $this->renderer);
$security_level = $this->getOverallSeverity($results);
$security_level = ValidationResult::getOverallSeverity($results);
if ($update_projects && $security_level !== SystemManager::REQUIREMENT_ERROR) {
$form['actions'] = $this->actions($form_state);
......
......@@ -113,4 +113,24 @@ final class ValidationResult {
return $this->severity;
}
/**
* Returns the overall severity for a set of validation results.
*
* @param \Drupal\package_manager\ValidationResult[] $results
* The validation results.
*
* @return int
* The overall severity of the results. Will be be one of the
* SystemManager::REQUIREMENT_* constants.
*/
public static function getOverallSeverity(array $results): int {
foreach ($results as $result) {
if ($result->getSeverity() === SystemManager::REQUIREMENT_ERROR) {
return SystemManager::REQUIREMENT_ERROR;
}
}
// If there were no errors, then any remaining results must be warnings.
return $results ? SystemManager::REQUIREMENT_WARNING : SystemManager::REQUIREMENT_OK;
}
}
......@@ -25,6 +25,27 @@ class ValidationResultTest extends UnitTestCase {
$this->assertResultValid($result, $messages, $summary, SystemManager::REQUIREMENT_WARNING);
}
/**
* @covers ::getOverallSeverity
*/
public function testOverallSeverity(): void {
// An error and a warning should be counted as an error.
$results = [
ValidationResult::createError(['Boo!']),
ValidationResult::createWarning(['Moo!']),
];
$this->assertSame(SystemManager::REQUIREMENT_ERROR, ValidationResult::getOverallSeverity($results));
// If there are no results, but no errors, the results should be counted as
// a warning.
array_shift($results);
$this->assertSame(SystemManager::REQUIREMENT_WARNING, ValidationResult::getOverallSeverity($results));
// If there are just plain no results, we should get REQUIREMENT_OK.
array_shift($results);
$this->assertSame(SystemManager::REQUIREMENT_OK, ValidationResult::getOverallSeverity($results));
}
/**
* @covers ::createError
*
......
......@@ -226,7 +226,7 @@ final class UpdateReady extends FormBase {
$this->displayResults($results, $this->messenger(), $this->renderer);
// If any errors occurred, return the form early so the user cannot
// continue.
if ($this->getOverallSeverity($results) === SystemManager::REQUIREMENT_ERROR) {
if (ValidationResult::getOverallSeverity($results) === SystemManager::REQUIREMENT_ERROR) {
return $form;
}
}
......
......@@ -245,7 +245,7 @@ final class UpdaterForm extends FormBase {
$release_status = $this->t('Available update');
$type = 'update-recommended';
}
$create_update_buttons = !$stage_exists && $this->getOverallSeverity($results) !== SystemManager::REQUIREMENT_ERROR;
$create_update_buttons = !$stage_exists && ValidationResult::getOverallSeverity($results) !== SystemManager::REQUIREMENT_ERROR;
if ($installed_minor_release) {
$installed_version = ExtensionVersion::createFromVersionString($project_info->getInstalledVersion());
$form['installed_minor'] = $this->createReleaseTable(
......
......@@ -39,26 +39,6 @@ trait ReadinessTrait {
$this->t('Your site does not pass some readiness checks for automatic updates. It cannot be automatically updated until further action is performed.');
}
/**
* Returns the overall severity for a set of validation results.
*
* @param \Drupal\package_manager\ValidationResult[] $results
* The validation results.
*
* @return int
* The overall severity of the results. Will be be one of the
* SystemManager::REQUIREMENT_* constants.
*/
protected function getOverallSeverity(array $results): int {
foreach ($results as $result) {
if ($result->getSeverity() === SystemManager::REQUIREMENT_ERROR) {
return SystemManager::REQUIREMENT_ERROR;
}
}
// If there were no errors, then any remaining results must be warnings.
return $results ? SystemManager::REQUIREMENT_WARNING : SystemManager::REQUIREMENT_OK;
}
/**
* Adds a set of validation results to the messages.
*
......@@ -70,7 +50,7 @@ trait ReadinessTrait {
* The renderer service.
*/
protected function displayResults(array $results, MessengerInterface $messenger, RendererInterface $renderer): void {
$severity = $this->getOverallSeverity($results);
$severity = ValidationResult::getOverallSeverity($results);
if ($severity === SystemManager::REQUIREMENT_OK) {
return;
......
......@@ -18,27 +18,6 @@ class ReadinessTraitTest extends AutomaticUpdatesKernelTestBase {
use ReadinessTrait;
use StringTranslationTrait;
/**
* @covers ::getOverallSeverity
*/
public function testOverallSeverity(): void {
// An error and a warning should be counted as an error.
$results = [
ValidationResult::createError(['Boo!']),
ValidationResult::createWarning(['Moo!']),
];
$this->assertSame(SystemManager::REQUIREMENT_ERROR, $this->getOverallSeverity($results));
// If there are no results, but no errors, the results should be counted as
// a warning.
array_shift($results);
$this->assertSame(SystemManager::REQUIREMENT_WARNING, $this->getOverallSeverity($results));
// If there are just plain no results, we should get REQUIREMENT_OK.
array_shift($results);
$this->assertSame(SystemManager::REQUIREMENT_OK, $this->getOverallSeverity($results));
}
/**
* @covers ::displayResults
*/
......
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