Skip to content
Snippets Groups Projects
Commit 2e0feff8 authored by Adam G-H's avatar Adam G-H Committed by Ted Bowman
Browse files

Issue #3243348 by phenaproxima: Improve the kernel tests

parent a996c03f
No related branches found
No related tags found
1 merge request!73Issue #3243348: Improve the kernel tests
......@@ -4,6 +4,7 @@ namespace Drupal\Tests\automatic_updates\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
......@@ -15,10 +16,12 @@ use GuzzleHttp\Psr7\Utils;
*/
abstract class AutomaticUpdatesKernelTestBase extends KernelTestBase {
use ValidationTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['update', 'update_test'];
protected static $modules = ['system', 'update', 'update_test'];
/**
* The mocked HTTP client that returns metadata about available updates.
......@@ -32,6 +35,33 @@ abstract class AutomaticUpdatesKernelTestBase extends KernelTestBase {
*/
private $client;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// The Update module's default configuration must be installed for our
// fake release metadata to be fetched.
$this->installConfig('update');
// Make the update system think that all of System's post-update functions
// have run. Since kernel tests don't normally install modules and register
// their updates, we need to do this so that all validators are tested from
// a clean, fully up-to-date state.
$updates = $this->container->get('update.post_update_registry')
->getPendingUpdateFunctions();
$this->container->get('keyvalue')
->get('post_update')
->set('existing_updates', $updates);
// By default, pretend we're running Drupal core 9.8.0 and a non-security
// update to 9.8.1 is available.
$this->setCoreVersion('9.8.0');
$this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.9.8.1.xml');
}
/**
* Sets the current (running) version of core, as known to the Update module.
*
......
......@@ -5,7 +5,6 @@ namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\automatic_updates\Validator\ComposerExecutableValidator;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
use PhpTuf\ComposerStager\Exception\IOException;
use PhpTuf\ComposerStager\Infrastructure\Process\ExecutableFinderInterface;
use Prophecy\Argument;
......@@ -17,8 +16,6 @@ use Prophecy\Argument;
*/
class ComposerExecutableValidatorTest extends AutomaticUpdatesKernelTestBase {
use ValidationTestTrait;
/**
* {@inheritdoc}
*/
......@@ -27,16 +24,6 @@ class ComposerExecutableValidatorTest extends AutomaticUpdatesKernelTestBase {
'package_manager',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig('update');
$this->setCoreVersion('9.8.0');
$this->setReleaseMetadata(__DIR__ . '/../../../fixtures/release-history/drupal.9.8.1.xml');
}
/**
* Tests that an error is raised if the Composer executable isn't found.
*/
......@@ -55,10 +42,7 @@ class ComposerExecutableValidatorTest extends AutomaticUpdatesKernelTestBase {
$error = ValidationResult::createError([
$exception->getMessage(),
]);
$results = $this->container->get('automatic_updates.readiness_validation_manager')
->run()
->getResults();
$this->assertValidationResultsEqual([$error], $results);
$this->assertCheckerResultsFromManager([$error], TRUE);
}
/**
......@@ -155,10 +139,7 @@ class ComposerExecutableValidatorTest extends AutomaticUpdatesKernelTestBase {
// If the validator can't find a recognized, supported version of Composer,
// it should produce errors.
$actual_results = $this->container->get('automatic_updates.readiness_validation_manager')
->run()
->getResults();
$this->assertValidationResultsEqual($expected_results, $actual_results);
$this->assertCheckerResultsFromManager($expected_results, TRUE);
}
}
......@@ -6,7 +6,6 @@ use Drupal\automatic_updates\PathLocator;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
/**
* @covers \Drupal\automatic_updates\Validator\CoreComposerValidator
......@@ -15,8 +14,6 @@ use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
*/
class CoreComposerValidatorTest extends AutomaticUpdatesKernelTestBase {
use ValidationTestTrait;
/**
* {@inheritdoc}
*/
......@@ -38,23 +35,15 @@ class CoreComposerValidatorTest extends AutomaticUpdatesKernelTestBase {
* Tests that an error is raised if core is not required in composer.json.
*/
public function testCoreNotRequired(): void {
$this->installConfig('update');
$this->setCoreVersion('9.8.0');
$this->setReleaseMetadata(__DIR__ . '/../../../fixtures/release-history/drupal.9.8.1.xml');
// Point to a valid composer.json with no requirements.
$locator = $this->prophesize(PathLocator::class);
$locator->getActiveDirectory()->willReturn(__DIR__ . '/../../../fixtures/project_staged_validation/no_core_requirements');
$this->container->set('automatic_updates.path_locator', $locator->reveal());
$results = $this->container->get('automatic_updates.readiness_validation_manager')
->run()
->getResults();
$error = ValidationResult::createError([
'Drupal core does not appear to be required in the project-level composer.json.',
]);
$this->assertValidationResultsEqual([$error], $results);
$this->assertCheckerResultsFromManager([$error], TRUE);
}
}
......@@ -2,85 +2,28 @@
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\Validation\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\KernelTests\KernelTestBase;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
/**
* @covers \Drupal\automatic_updates\Validator\DiskSpaceValidator
*
* @group automatic_updates
*/
class DiskSpaceValidatorTest extends KernelTestBase {
use ValidationTestTrait;
/**
* The validator under test.
*
* @var \Drupal\automatic_updates\Validator\DiskSpaceValidator
*/
private $validator;
class DiskSpaceValidatorTest extends AutomaticUpdatesKernelTestBase {
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$path_locator = $this->prophesize('\Drupal\automatic_updates\PathLocator');
$path_locator->getProjectRoot()->willReturn('root');
$path_locator->getVendorDirectory()->willReturn('vendor');
// Create a mocked version of the validator which can be rigged up to return
// specific values for various filesystem checks.
$this->validator = new class ($path_locator->reveal(), new TestTranslationManager()) extends DiskSpaceValidator {
/**
* Whether the root and vendor directories are on the same logical disk.
*
* @var bool
*/
public $sharedDisk;
/**
* The amount of free space, keyed by location.
*
* @var float[]
*/
public $freeSpace = [];
/**
* {@inheritdoc}
*/
protected function stat(string $path): array {
return [
'dev' => $this->sharedDisk ? 'disk' : uniqid(),
];
}
/**
* {@inheritdoc}
*/
protected function freeSpace(string $path): float {
return $this->freeSpace[$path];
}
/**
* {@inheritdoc}
*/
protected function temporaryDirectory(): string {
return 'temp';
}
};
}
protected static $modules = [
'automatic_updates',
'package_manager',
];
/**
* Data provider for ::testDiskSpaceValidation().
......@@ -208,13 +151,59 @@ class DiskSpaceValidatorTest extends KernelTestBase {
* @dataProvider providerDiskSpaceValidation
*/
public function testDiskSpaceValidation(bool $shared_disk, array $free_space, array $expected_results): void {
$this->validator->sharedDisk = $shared_disk;
$this->validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space);
$path_locator = $this->prophesize('\Drupal\automatic_updates\PathLocator');
$path_locator->getProjectRoot()->willReturn('root');
$path_locator->getVendorDirectory()->willReturn('vendor');
// Create a mocked version of the validator which can be rigged up to return
// specific values for various filesystem checks.
$validator = new class (
$path_locator->reveal(),
$this->container->get('string_translation')
) extends DiskSpaceValidator {
/**
* Whether the root and vendor directories are on the same logical disk.
*
* @var bool
*/
public $sharedDisk;
/**
* The amount of free space, keyed by location.
*
* @var float[]
*/
public $freeSpace = [];
$composer = $this->createMock('\Drupal\package_manager\ComposerUtility');
$event = new UpdateEvent($composer);
$this->validator->checkDiskSpace($event);
$this->assertValidationResultsEqual($expected_results, $event->getResults());
/**
* {@inheritdoc}
*/
protected function stat(string $path): array {
return [
'dev' => $this->sharedDisk ? 'disk' : uniqid(),
];
}
/**
* {@inheritdoc}
*/
protected function freeSpace(string $path): float {
return $this->freeSpace[$path];
}
/**
* {@inheritdoc}
*/
protected function temporaryDirectory(): string {
return 'temp';
}
};
$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);
}
}
......
......@@ -2,19 +2,15 @@
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
/**
* @covers \Drupal\automatic_updates\Validator\PendingUpdatesValidator
*
* @group automatic_updates
*/
class PendingUpdatesValidatorTest extends KernelTestBase {
use ValidationTestTrait;
class PendingUpdatesValidatorTest extends AutomaticUpdatesKernelTestBase {
/**
* {@inheritdoc}
......@@ -22,45 +18,13 @@ class PendingUpdatesValidatorTest extends KernelTestBase {
protected static $modules = [
'automatic_updates',
'package_manager',
'system',
'update',
];
/**
* The update event object that will be dispatched.
*
* @var \Drupal\automatic_updates\Event\UpdateEvent
*/
private $event;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Make the update system think that all of System's post-update functions
// have run. Since kernel tests don't normally install modules and register
// their updates, we need to do this so that the validator is being tested
// from a clean, fully up-to-date state.
$updates = $this->container->get('update.post_update_registry')
->getPendingUpdateFunctions();
$this->container->get('keyvalue')
->get('post_update')
->set('existing_updates', $updates);
$composer = $this->createMock('\Drupal\package_manager\ComposerUtility');
$this->event = new UpdateEvent($composer);
}
/**
* Tests that no error is raised if there are no pending updates.
*/
public function testNoPendingUpdates(): void {
$this->container->get('automatic_updates.pending_updates_validator')
->checkPendingUpdates($this->event);
$this->assertEmpty($this->event->getResults());
$this->assertCheckerResultsFromManager([], TRUE);
}
/**
......@@ -74,10 +38,7 @@ class PendingUpdatesValidatorTest extends KernelTestBase {
->set('automatic_updates', \Drupal::CORE_MINIMUM_SCHEMA_VERSION);
$result = ValidationResult::createError(['Some modules have database schema updates to install. You should run the <a href="/update.php">database update script</a> immediately.']);
$this->container->get('automatic_updates.pending_updates_validator')
->checkPendingUpdates($this->event);
$this->assertValidationResultsEqual([$result], $this->event->getResults());
$this->assertCheckerResultsFromManager([$result], TRUE);
}
/**
......@@ -85,12 +46,8 @@ class PendingUpdatesValidatorTest extends KernelTestBase {
*/
public function testPendingPostUpdate(): void {
require __DIR__ . '/../../../fixtures/post_update.php';
$result = ValidationResult::createError(['Some modules have database schema updates to install. You should run the <a href="/update.php">database update script</a> immediately.']);
$this->container->get('automatic_updates.pending_updates_validator')
->checkPendingUpdates($this->event);
$this->assertValidationResultsEqual([$result], $this->event->getResults());
$this->assertCheckerResultsFromManager([$result], TRUE);
}
}
......@@ -6,7 +6,6 @@ use Drupal\automatic_updates_test\ReadinessChecker\TestChecker1;
use Drupal\automatic_updates_test2\ReadinessChecker\TestChecker2;
use Drupal\system\SystemManager;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
/**
* @coversDefaultClass \Drupal\automatic_updates\Validation\ReadinessValidationManager
......@@ -15,8 +14,6 @@ use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
*/
class ReadinessValidationManagerTest extends AutomaticUpdatesKernelTestBase {
use ValidationTestTrait;
/**
* {@inheritdoc}
*/
......@@ -34,10 +31,6 @@ class ReadinessValidationManagerTest extends AutomaticUpdatesKernelTestBase {
$this->installEntitySchema('user');
$this->installSchema('user', ['users_data']);
$this->createTestValidationResults();
$this->installConfig('update');
$this->setCoreVersion('9.8.0');
$this->setReleaseMetadata(__DIR__ . '/../../../fixtures/release-history/drupal.9.8.1.xml');
}
/**
......@@ -218,40 +211,4 @@ class ReadinessValidationManagerTest extends AutomaticUpdatesKernelTestBase {
$this->assertCheckerResultsFromManager($expected_results);
}
/**
* Asserts expected validation results from the manager.
*
* @param \Drupal\automatic_updates\Validation\ValidationResult[] $expected_results
* The expected results.
* @param bool $call_run
* (Optional) Whether to call ::run() on the manager. Defaults to FALSE.
* @param int|null $severity
* (optional) The severity for the results to return. Should be one of the
* SystemManager::REQUIREMENT_* constants.
*/
private 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);
}
/**
* Gets the messages of a particular type from the manager.
*
* @param bool $call_run
* Whether to run the checkers.
* @param int|null $severity
* (optional) The severity for the results to return. Should be one of the
* SystemManager::REQUIREMENT_* constants.
*
* @return \Drupal\automatic_updates\Validation\ValidationResult[]|null
* The messages of the type.
*/
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);
}
}
......@@ -19,21 +19,10 @@ class UpdateRecommenderTest extends AutomaticUpdatesKernelTestBase {
'package_manager',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig('update');
$this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.9.8.1.xml');
}
/**
* Tests fetching the recommended release when an update is available.
*/
public function testUpdateAvailable(): void {
$this->setCoreVersion('9.8.0');
$recommender = new UpdateRecommender();
$recommended_release = $recommender->getRecommendedRelease(TRUE);
$this->assertNotEmpty($recommended_release);
......
......@@ -27,9 +27,6 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
public function testCorrectVersionsStaged() {
$this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.9.8.1-security.xml');
// Set the running core version to 9.8.0.
$this->setCoreVersion('9.8.0');
$this->container->get('automatic_updates.updater')->begin([
'drupal' => '9.8.1',
]);
......
......@@ -110,4 +110,40 @@ trait ValidationTestTrait {
}
}
/**
* Gets the messages of a particular type from the manager.
*
* @param bool $call_run
* Whether to run the checkers.
* @param int|null $severity
* (optional) The severity for the results to return. Should be one of the
* SystemManager::REQUIREMENT_* constants.
*
* @return \Drupal\automatic_updates\Validation\ValidationResult[]|null
* The messages of the type.
*/
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);
}
/**
* Asserts expected validation results from the manager.
*
* @param \Drupal\automatic_updates\Validation\ValidationResult[] $expected_results
* The expected results.
* @param bool $call_run
* (Optional) Whether to call ::run() on the manager. Defaults to FALSE.
* @param int|null $severity
* (optional) The severity for the results to return. Should be one of the
* SystemManager::REQUIREMENT_* constants.
*/
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);
}
}
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