diff --git a/src/Hook/FieldEncryptRequirementsHook.php b/src/Hook/FieldEncryptRequirementsHook.php new file mode 100644 index 0000000000000000000000000000000000000000..eab1d816f9123089ced7897482b6b08268abd648 --- /dev/null +++ b/src/Hook/FieldEncryptRequirementsHook.php @@ -0,0 +1,52 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\field_encrypt\Hook; + +use Drupal\Core\Hook\Attribute\Hook; +use Drupal\Core\Update\UpdateHookRegistry; + +/** + * Update requirements for field_encrypt. + */ +class FieldEncryptRequirementsHook { + + public function __construct(protected UpdateHookRegistry $updateHookRegistry) { + } + + /** + * Checks whether the site meets the module requirements. + */ + private function requirementsCheck(): array { + $requirements = []; + $schema = $this->updateHookRegistry->getInstalledVersion('field_encrypt'); + + if ((int) $schema < 8300) { + $requirements['field_encrypt_update_to_v4'] = [ + 'title' => t('Field Encrypt'), + 'severity' => REQUIREMENT_ERROR, + 'value' => t('Updating to field_encrypt version 4 is not supported.'), + 'description' => t('In order to upgrade you need to decrypt all your data on the previous version and uninstall the module.'), + ]; + } + return $requirements; + } + + /** + * Implements hook_update_requirements(). + */ + #[Hook('update_requirements')] + public function update(): array { + return $this->requirementsCheck(); + } + + /** + * Implements hook_runtime_requirements(). + */ + #[Hook('runtime_requirements')] + public function runtime(): array { + return $this->requirementsCheck(); + } + +} diff --git a/src/StateManager.php b/src/StateManager.php index 18c79e40f24be8210e50beef611b0b5803051b23..89ec4f0831adfdd8f9624e6a901743540e7a09ce 100644 --- a/src/StateManager.php +++ b/src/StateManager.php @@ -37,7 +37,7 @@ class StateManager { protected EntityLastInstalledSchemaRepositoryInterface $entitySchemaRepository, protected EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager, protected DrupalKernelInterface $kernel, - // Set by \Drupal\field_encrypt\FieldEncryptServiceProvider + // Set by \Drupal\field_encrypt\FieldEncryptServiceProvider. protected array $entityTypes = [], ) { } diff --git a/tests/src/Functional/FieldEncryptUpdatePathTest.php b/tests/src/Functional/FieldEncryptUpdatePathTest.php index 0a50cd05ed3195411e6ce7ecc376811ec589195f..0f33f4cd84f50daac581eb8a03ec31014cf43763 100644 --- a/tests/src/Functional/FieldEncryptUpdatePathTest.php +++ b/tests/src/Functional/FieldEncryptUpdatePathTest.php @@ -23,6 +23,16 @@ class FieldEncryptUpdatePathTest extends FieldEncryptTestBase { */ private string $updateUrl; + /** + * @var string + */ + private string $fieldEncryptCannotUpdateMessage = 'Updating to field_encrypt version 4 is not supported.'; + + /** + * @var string + */ + private string $drupalCoreCannotUpdateMessage = 'The installed version of the Field Encrypt module is too old to update.'; + /** * {@inheritdoc} */ @@ -44,10 +54,16 @@ class FieldEncryptUpdatePathTest extends FieldEncryptTestBase { // Simulate having an old version of field_encrypt. \Drupal::service('update.update_hook_registry')->setInstalledVersion('field_encrypt', 8000); $this->drupalGet($this->updateUrl, ['external' => TRUE]); - $this->assertSession()->pageTextContains('Updating to field_encrypt version 4 is not supported.'); + $this->assertThat($this->getTextContent(), + $this->logicalOr( + // 11.2+. + $this->stringContains($this->drupalCoreCannotUpdateMessage), + // Before 11.2. + $this->stringContains($this->fieldEncryptCannotUpdateMessage), + )); $this->drupalGet('admin/reports/status'); - $this->assertSession()->pageTextContains('Updating to field_encrypt version 4 is not supported.'); + $this->assertSession()->pageTextContains($this->fieldEncryptCannotUpdateMessage); } }