View source
<?php
namespace Drupal\Tests\automatic_updates\Traits;
use Drupal\automatic_updates\Validation\ValidationResult;
trait ValidationTestTrait {
protected static $errorsExplanation = 'Your site does not pass some readiness checks for automatic updates. It cannot be automatically updated until further action is performed.';
protected static $warningsExplanation = 'Your site does not pass some readiness checks for automatic updates. Depending on the nature of the failures, it might affect the eligibility for automatic updates.';
protected $testResults;
protected function createTestValidationResults() : void {
foreach ([
1,
2,
] as $listener_number) {
$this->testResults["checker_{$listener_number}"]['1 error'] = [
ValidationResult::createError([
t("{$listener_number}:OMG ๐. Your server is on ๐ฅ!"),
], t("{$listener_number}:Summary: ๐ฅ")),
];
$this->testResults["checker_{$listener_number}"]['1 error 1 warning'] = [
"{$listener_number}:error" => ValidationResult::createError([
t("{$listener_number}:OMG ๐. Some one unplugged the server! How is this site even running?"),
], t("{$listener_number}:Summary: ๐ฅ")),
"{$listener_number}:warning" => ValidationResult::createWarning([
t("{$listener_number}:It looks like it going to rain and your server is outside."),
], t("{$listener_number}:Warnings summary not displayed because only 1 warning message.")),
];
$this->testResults["checker_{$listener_number}"]['2 errors 2 warnings'] = [
"{$listener_number}:errors" => ValidationResult::createError([
t("{$listener_number}:๐ฌYour server is in a cloud, a literal cloud!โ๏ธ."),
t("{$listener_number}:๐PHP only has 32k memory."),
], t("{$listener_number}:Errors summary displayed because more than 1 error message")),
"{$listener_number}:warnings" => ValidationResult::createWarning([
t("{$listener_number}:Your server is a smart fridge. Will this work?"),
t("{$listener_number}:Your server case is duct tape!"),
], t("{$listener_number}:Warnings summary displayed because more than 1 warning message.")),
];
$this->testResults["checker_{$listener_number}"]['2 warnings'] = [
ValidationResult::createWarning([
t("{$listener_number}:The universe could collapse in on itself in the next second, in which case automatic updates will not run."),
t("{$listener_number}:An asteroid could hit your server farm, which would also stop automatic updates from running."),
], t("{$listener_number}:Warnings summary displayed because more than 1 warning message.")),
];
$this->testResults["checker_{$listener_number}"]['1 warning'] = [
ValidationResult::createWarning([
t("{$listener_number}:This is your one and only warning. You have been warned."),
], t("{$listener_number}:No need for this summary with only 1 warning.")),
];
}
}
protected function assertValidationResultsEqual(array $expected_results, array $actual_results) : void {
$this
->assertCount(count($expected_results), $actual_results);
foreach ($expected_results as $expected_result) {
$actual_result = array_shift($actual_results);
$this
->assertSame($expected_result
->getSeverity(), $actual_result
->getSeverity());
$this
->assertSame((string) $expected_result
->getSummary(), (string) $actual_result
->getSummary());
$this
->assertSame(array_map('strval', $expected_result
->getMessages()), array_map('strval', $actual_result
->getMessages()));
}
}
protected function getResultsFromManager(bool $call_run = FALSE, ?int $severity = NULL) : ?array {
$manager = $this->container
->get('automatic_updates.readiness_validation_manager');
if ($call_run) {
$manager
->run();
}
return $manager
->getResults($severity);
}
protected function assertCheckerResultsFromManager(array $expected_results, bool $call_run = FALSE, ?int $severity = NULL) : void {
$actual_results = $this
->getResultsFromManager($call_run, $severity);
$this
->assertValidationResultsEqual($expected_results, $actual_results);
}
}