diff --git a/automatic_updates.services.yml b/automatic_updates.services.yml index be10d60e87d659fc041f2e52764eb9fb9ff6da71..76c420edffe25b1275e44ffdfcbfa061c905a4f6 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 0000000000000000000000000000000000000000..4cd4c6676f051ececc23bf53e1e0285eb360157e --- /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 0000000000000000000000000000000000000000..e2c2104ed4fd526250f91bd5bb839728b8e93dab --- /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; + } + +}