From 321b1a984005183710ee059111500596eba2f5a6 Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 30 Dec 2024 14:41:33 +0100 Subject: [PATCH] =?UTF-8?q?Improvement:=20Add=20checks=20for=20Hyv=C3=A4?= =?UTF-8?q?=20to=20the=20self=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SelfTests/AreHyvaModulesInstalled.php | 38 +++++++++ .../SelfTests/IsHyvaThemesJsonCorrect.php | 78 +++++++++++++++++++ etc/adminhtml/di.xml | 2 + 3 files changed, 118 insertions(+) create mode 100644 Service/Mollie/SelfTests/AreHyvaModulesInstalled.php create mode 100644 Service/Mollie/SelfTests/IsHyvaThemesJsonCorrect.php diff --git a/Service/Mollie/SelfTests/AreHyvaModulesInstalled.php b/Service/Mollie/SelfTests/AreHyvaModulesInstalled.php new file mode 100644 index 00000000000..980872b6a87 --- /dev/null +++ b/Service/Mollie/SelfTests/AreHyvaModulesInstalled.php @@ -0,0 +1,38 @@ +moduleManager = $moduleManager; + } + + public function execute(): void + { + if (!$this->moduleManager->isEnabled('Hyva_Theme')) { + return; + } + + if (!$this->moduleManager->isEnabled('Mollie_HyvaCompatibility')) { + $this->addMessage('error', __('The Mollie Hyvä Compatibility module is not installed. Please install this module to use Mollie with the Hyvä Theme.')); + } + + if ($this->moduleManager->isEnabled('Hyva_Checkout') && + !$this->moduleManager->isEnabled('Mollie_HyvaCheckout') + ) { + $this->addMessage('error', __('You have installed the Hyvä Checkout module, but not the Mollie Hyvä Checkout module. Please install this module to use Mollie with the Hyvä Checkout.')); + } + } +} diff --git a/Service/Mollie/SelfTests/IsHyvaThemesJsonCorrect.php b/Service/Mollie/SelfTests/IsHyvaThemesJsonCorrect.php new file mode 100644 index 00000000000..a4bd368155a --- /dev/null +++ b/Service/Mollie/SelfTests/IsHyvaThemesJsonCorrect.php @@ -0,0 +1,78 @@ +moduleManager = $moduleManager; + $this->file = $file; + } + + public function execute(): void + { + if (!$this->moduleManager->isEnabled('Hyva_Theme') || + !class_exists(HyvaModulesConfig::class) + ) { + return; + } + + $file = HyvaModulesConfig::FILE; + $path = HyvaModulesConfig::PATH; + + $path = trim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + // @phpstan-ignore-next-line + $fullPath = BP . DIRECTORY_SEPARATOR . $path . strtolower($file); + + $contents = $this->file->read($fullPath); + + if ($contents === false) { + $this->addMessage('error', __('The Hyva Themes configuration file is missing. Please run the command `bin/magento hyva:modules:config:generate` to generate the file.')); + return; + } + + try { + $json = json_decode($contents, true, 512, JSON_THROW_ON_ERROR); + $this->validateThatModuleIsPresent($json, 'Mollie_HyvaCompatibility'); + $this->validateThatModuleIsPresent($json, 'Mollie_HyvaCheckout'); + } catch (\Exception $exception) { + $this->addMessage('error', __('The Hyva Themes configuration file is not a valid JSON file. Please run the command `bin/magento hyva:modules:config:generate` to generate the file.')); + } + } + + private function validateThatModuleIsPresent(array $json, string $module): void + { + if (!$this->moduleManager->isEnabled($module)) { + return; + } + + foreach ($json['extensions'] as $extension) { + if (strpos($extension['src'], $module) !== false) { + return; + } + } + + $this->addMessage('error', __( + 'The %1 module is not present in the Hyva Themes configuration file. Please run the command `bin/magento hyva:modules:config:generate` to generate the file.', + $module + )); + } +} diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml index 6a418746e08..06ea8c0c0c6 100644 --- a/etc/adminhtml/di.xml +++ b/etc/adminhtml/di.xml @@ -39,6 +39,8 @@ Mollie\Payment\Service\Mollie\SelfTests\TestWebhooksDisabled Mollie\Payment\Service\Mollie\SelfTests\TestApplePayDomainValidationFile Mollie\Payment\Service\Mollie\SelfTests\TestIsDobEnabled + Mollie\Payment\Service\Mollie\SelfTests\AreHyvaModulesInstalled + Mollie\Payment\Service\Mollie\SelfTests\IsHyvaThemesJsonCorrect