Skip to content
Snippets Groups Projects
Commit b683ad5f authored by Yash Rode's avatar Yash Rode
Browse files

Added test coverage

parent 66e783f2
No related branches found
No related tags found
No related merge requests found
......@@ -165,7 +165,7 @@ class CronUpdater extends Updater {
private function performUpdate(string $target_version, ?int $timeout): void {
$project_info = new ProjectInfo('drupal');
// Delete the existing stage if not available and the site is currently on an insecure version.
// Delete the existing staging area if not available and the site is currently on an insecure version.
if (!$project_info->isInstalledVersionSafe() && !$this->isAvailable() && !$this->isApplying()) {
$this->destroy(TRUE);
}
......
......@@ -18,6 +18,7 @@ use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PreDestroyEvent;
use Drupal\package_manager\Event\PreRequireEvent;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\StageEvent;
use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\ValidationResult;
use Drupal\package_manager_bypass\Committer;
......@@ -28,6 +29,7 @@ use Drupal\update\UpdateSettingsForm;
use ColinODell\PsrTestLogger\TestLogger;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @covers \Drupal\automatic_updates\CronUpdater
......@@ -35,7 +37,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
* @group automatic_updates
* @internal
*/
class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
class CronUpdaterTest extends AutomaticUpdatesKernelTestBase implements EventSubscriberInterface {
use EmailNotificationsTestTrait;
use PackageManagerBypassTestTrait;
......@@ -57,6 +59,27 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
*/
private $logger;
/**
* The new stage ID.
*
* @var string
*/
private $new_stage_id;
/**
* The events that were fired, in the order they were fired.
*
* @var string[]
*/
private $events = [];
/**
* The stage under test.
*
* @var \Drupal\package_manager\Stage
*/
private $stage;
/**
* {@inheritdoc}
*/
......@@ -347,6 +370,32 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
}
}
/**
* Tests stage is destroyed if not available and site is on insecure version.
*/
public function testStageDestroyedIfNotAvailable(): void {
$this->container->get('event_dispatcher')->addSubscriber($this);
$stage = $this->createStage();
$stage_id = $stage->create();
$temp_store = $this->container->get('tempstore.shared');
$listener = function (PreApplyEvent $event) use ($temp_store): void {
// @see \Drupal\package_manager\Stage::TEMPSTORE_LOCK_KEY
$this->new_stage_id = $temp_store->get('package_manager_stage')->get('lock')[0];
};
$this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX);
$this->container->get('cron')->run();
// To demonstrate these events are called in order.
$this->assertSame($this->events, [
PreDestroyEvent::class,
PostDestroyEvent::class,
PreApplyEvent::class,
]);
$this->assertNotEquals($stage_id, $this->new_stage_id);
}
/**
* Tests that CronUpdater::begin() unconditionally throws an exception.
*/
......@@ -498,4 +547,22 @@ END;
$this->assertSame('setLogger', $updater_method_calls[0][0]);
}
/**
* Handles a staging area life cycle event.
*
* @param \Drupal\package_manager\Event\StageEvent $event
* The event object.
*/
public function handleEvent(StageEvent $event): void {
array_push($this->events, get_class($event));
}
public static function getSubscribedEvents(): array {
return [
PreDestroyEvent::class => 'handleEvent',
PostDestroyEvent::class => 'handleEvent',
PreApplyEvent::class => 'handleEvent',
];
}
}
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