Skip to content

Commit

Permalink
Improvement: Add checks for Hyvä to the self tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Dec 30, 2024
1 parent 96fdc1f commit 321b1a9
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Service/Mollie/SelfTests/AreHyvaModulesInstalled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Mollie\Payment\Service\Mollie\SelfTests;

use Magento\Framework\Module\Manager;

class AreHyvaModulesInstalled extends AbstractSelfTest
{
/**
* @var Manager
*/
private $moduleManager;

public function __construct(
Manager $moduleManager
) {
$this->moduleManager = $moduleManager;
}

public function execute(): void
{
if (!$this->moduleManager->isEnabled('Hyva_Theme')) {
return;
}

if (!$this->moduleManager->isEnabled('Mollie_HyvaCompatibility')) {
$this->addMessage('error', __('The <a href="https://github.com/mollie/magento2-hyva-compatibility" target="_blank">Mollie Hyvä Compatibility</a> 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.'));
}
}
}
78 changes: 78 additions & 0 deletions Service/Mollie/SelfTests/IsHyvaThemesJsonCorrect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Mollie\Payment\Service\Mollie\SelfTests;

use Hyva\Theme\Model\HyvaModulesConfig;
use Magento\Framework\Filesystem\Io\File;
use Magento\Framework\Module\Manager;

class IsHyvaThemesJsonCorrect extends AbstractSelfTest
{
/**
* @var Manager
*/
private $moduleManager;
/**
* @var File
*/
private $file;

public function __construct(
Manager $moduleManager,
File $file
) {
$this->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
));
}
}
2 changes: 2 additions & 0 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
<item name="webhooksDisabled" xsi:type="object">Mollie\Payment\Service\Mollie\SelfTests\TestWebhooksDisabled</item>
<item name="applePayDomainValidationFile" xsi:type="object">Mollie\Payment\Service\Mollie\SelfTests\TestApplePayDomainValidationFile</item>
<item name="isDateofbirthEnabled" xsi:type="object">Mollie\Payment\Service\Mollie\SelfTests\TestIsDobEnabled</item>
<item name="areHyvaModulesInstalled" xsi:type="object">Mollie\Payment\Service\Mollie\SelfTests\AreHyvaModulesInstalled</item>
<item name="isHyvaThemesJsonCorrect" xsi:type="object">Mollie\Payment\Service\Mollie\SelfTests\IsHyvaThemesJsonCorrect</item>
</argument>
</arguments>
</type>
Expand Down

0 comments on commit 321b1a9

Please sign in to comment.