From f4003c1205c920aff30e84976e900e69ba1fca14 Mon Sep 17 00:00:00 2001 From: lucashedding <lucashedding@1463982.no-reply.drupal.org> Date: Fri, 17 May 2019 10:58:21 -0500 Subject: [PATCH] Issue #3053804 by heddn, catch: Checker: Are there pending updates (update.php needs to be run) --- automatic_updates.services.yml | 4 ++ src/ReadinessChecker/PendingDbUpdates.php | 38 +++++++++++++++ .../ReadinessChecker/PendingDbUpdatesTest.php | 47 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/ReadinessChecker/PendingDbUpdates.php create mode 100644 tests/src/Kernel/ReadinessChecker/PendingDbUpdatesTest.php diff --git a/automatic_updates.services.yml b/automatic_updates.services.yml index be10d60e87..76c420edff 100644 --- a/automatic_updates.services.yml +++ b/automatic_updates.services.yml @@ -61,3 +61,7 @@ services: - '@automatic_updates.drupal_finder' tags: - { name: readiness_checker, category: warning} + automatic_updates.pending_db_updates: + class: Drupal\automatic_updates\ReadinessChecker\PendingDbUpdates + tags: + - { name: readiness_checker, category: error} diff --git a/src/ReadinessChecker/PendingDbUpdates.php b/src/ReadinessChecker/PendingDbUpdates.php new file mode 100644 index 0000000000..4cd4c6676f --- /dev/null +++ b/src/ReadinessChecker/PendingDbUpdates.php @@ -0,0 +1,38 @@ +<?php + +namespace Drupal\automatic_updates\ReadinessChecker; + +use Drupal\Core\StringTranslation\StringTranslationTrait; + +/** + * Pending database updates checker. + */ +class PendingDbUpdates implements ReadinessCheckerInterface { + use StringTranslationTrait; + + /** + * {@inheritdoc} + */ + public function run() { + $messages = []; + + if ($this->areDbUpdatesPending()) { + $messages[] = $this->t('There are pending database updates, therefore updates cannot be applied. Please run update.php.'); + } + return $messages; + } + + /** + * Checks if there are pending database updates. + * + * @return bool + * TRUE if there are pending updates, otherwise FALSE. + */ + protected function areDbUpdatesPending() { + require_once DRUPAL_ROOT . '/core/includes/install.inc'; + require_once DRUPAL_ROOT . '/core/includes/update.inc'; + drupal_load_updates(); + return (bool) update_get_update_list(); + } + +} diff --git a/tests/src/Kernel/ReadinessChecker/PendingDbUpdatesTest.php b/tests/src/Kernel/ReadinessChecker/PendingDbUpdatesTest.php new file mode 100644 index 0000000000..e2c2104ed4 --- /dev/null +++ b/tests/src/Kernel/ReadinessChecker/PendingDbUpdatesTest.php @@ -0,0 +1,47 @@ +<?php + +namespace Drupal\Tests\automatic_updates\Kernel\ReadinessChecker; + +use Drupal\automatic_updates\ReadinessChecker\PendingDbUpdates; +use Drupal\KernelTests\KernelTestBase; + +/** + * Tests pending db updates readiness checking. + * + * @group automatic_updates + */ +class PendingDbUpdatesTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = [ + 'automatic_updates', + ]; + + /** + * Tests pending db updates readiness checks. + */ + public function testPendingDbUpdates() { + $messages = $this->container->get('automatic_updates.pending_db_updates')->run(); + $this->assertEmpty($messages); + + $messages = (new TestPendingDbUpdates())->run(); + $this->assertEquals('There are pending database updates, therefore updates cannot be applied. Please run update.php.', $messages[0]); + } + +} + +/** + * Class TestPendingDbUpdates. + */ +class TestPendingDbUpdates extends PendingDbUpdates { + + /** + * {@inheritdoc} + */ + protected function areDbUpdatesPending() { + return TRUE; + } + +} -- GitLab