diff --git a/Model/Config.php b/Model/Config.php index 5945a48..71e2db6 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -12,6 +12,7 @@ class Config public const XML_PATH_COOKIEBOT_ENABLED = 'web/cookiebot/enabled'; public const XML_PATH_COOKIEBOT_ID = 'web/cookiebot/id'; public const XML_PATH_DATA_CULTURE = 'web/cookiebot/data_culture'; + public const XML_PATH_USE_EU_CDN = 'web/cookiebot/use_eu_cdn'; /** * @var ScopeConfigInterface @@ -37,4 +38,9 @@ public function getDataCulture(): string { return (string)$this->scopeConfig->getValue(self::XML_PATH_DATA_CULTURE, ScopeInterface::SCOPE_STORE); } + + public function useEuCdn(): bool + { + return $this->scopeConfig->isSetFlag(self::XML_PATH_USE_EU_CDN, ScopeInterface::SCOPE_STORE); + } } diff --git a/Model/ScriptGenerator.php b/Model/ScriptGenerator.php index c9088ad..eb8d20d 100644 --- a/Model/ScriptGenerator.php +++ b/Model/ScriptGenerator.php @@ -7,6 +7,7 @@ class ScriptGenerator { private const COOKIEBOT_SCRIPT_FORMAT = ''; + private const EU_COOKIEBOT_SCRIPT_FORMAT = ''; /** * @var Config @@ -24,6 +25,10 @@ public function generate(): string $dataCulture = $this->config->getDataCulture() ? sprintf('data-culture="%s"', $this->config->getDataCulture()) : ''; + if ($this->config->useEuCdn()) { + return sprintf(self::EU_COOKIEBOT_SCRIPT_FORMAT, $cookiebotId, $dataCulture); + } + return sprintf(self::COOKIEBOT_SCRIPT_FORMAT, $cookiebotId, $dataCulture); } } diff --git a/Test/Integration/AddScriptTest.php b/Test/Integration/AddScriptTest.php index f1cd06f..5b90bff 100644 --- a/Test/Integration/AddScriptTest.php +++ b/Test/Integration/AddScriptTest.php @@ -14,52 +14,52 @@ class AddScriptTest extends AbstractController */ private $script; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->script = $this->_objectManager->create(ScriptGenerator::class)->generate(); } /** - * @magentoConfigFixture default/web/cookiebot/enabled 1 - * @magentoConfigFixture default/web/cookiebot/id 123-456-789 + * @magentoConfigFixture current_store web/cookiebot/enabled 1 + * @magentoConfigFixture current_store web/cookiebot/id 123-456-789 */ public function testScriptAddedOnHomepage(): void { $this->dispatch('/'); - self::assertContains($this->script, $this->getResponse()->getBody()); + self::assertStringContainsString($this->script, $this->getResponse()->getBody()); } /** * @magentoDataFixture Magento/Catalog/_files/product_simple.php - * @magentoConfigFixture default/web/cookiebot/enabled 1 - * @magentoConfigFixture default/web/cookiebot/id 123-456-789 + * @magentoConfigFixture current_store web/cookiebot/enabled 1 + * @magentoConfigFixture current_store web/cookiebot/id 123-456-789 */ public function testScriptAddedOnProductPage(): void { $this->dispatch('/catalog/product/view/id/1'); - self::assertContains($this->script, $this->getResponse()->getBody()); + self::assertStringContainsString($this->script, $this->getResponse()->getBody()); } /** * @magentoDataFixture Magento/Catalog/_files/category.php - * @magentoConfigFixture default/web/cookiebot/enabled 1 - * @magentoConfigFixture default/web/cookiebot/id 123-456-789 + * @magentoConfigFixture current_store web/cookiebot/enabled 1 + * @magentoConfigFixture current_store web/cookiebot/id 123-456-789 */ public function testScriptAddedOnCategoryPage(): void { $this->dispatch('/catalog/category/view/id/333'); - self::assertContains($this->script, $this->getResponse()->getBody()); + self::assertStringContainsString($this->script, $this->getResponse()->getBody()); } /** * @magentoDataFixture Magento/Cms/_files/pages.php - * @magentoConfigFixture default/web/cookiebot/enabled 1 - * @magentoConfigFixture default/web/cookiebot/id 123-456-789 + * @magentoConfigFixture current_store web/cookiebot/enabled 1 + * @magentoConfigFixture current_store web/cookiebot/id 123-456-789 */ public function testScriptAddedOnCmsPage(): void { $this->dispatch('/page100'); - self::assertContains($this->script, $this->getResponse()->getBody()); + self::assertStringContainsString($this->script, $this->getResponse()->getBody()); } } diff --git a/Test/Integration/Model/ConfigTest.php b/Test/Integration/Model/ConfigTest.php index 8d1dfd9..de75c87 100644 --- a/Test/Integration/Model/ConfigTest.php +++ b/Test/Integration/Model/ConfigTest.php @@ -34,7 +34,7 @@ public function testIsEnabledReturnsFalse(): void } /** - * @magentoConfigFixture default/web/cookiebot/enabled 1 + * @magentoConfigFixture current_store web/cookiebot/enabled 1 */ public function testIsEnabledReturnsTrue(): void { @@ -47,10 +47,31 @@ public function testGetIdReturnsEmptyStringByDefault(): void } /** - * @magentoConfigFixture default/web/cookiebot/id 123-456-789 + * @magentoConfigFixture current_store web/cookiebot/id 123-456-789 */ public function testGetIdReturnsId(): void { self::assertEquals('123-456-789', $this->config->getId()); } + + public function testEuCdnEnabledReturnsFalseByDefault(): void + { + self::assertFalse($this->config->useEuCdn()); + } + + /** + * @magentoConfigFixture current_store web/cookiebot/use_eu_cdn 0 + */ + public function testIfEuCdnEnabledConfigFunctionReturnsFalse(): void + { + self::assertFalse($this->config->useEuCdn()); + } + + /** + * @magentoConfigFixture current_store web/cookiebot/use_eu_cdn 1 + */ + public function testIfEuCdnEnabledConfigFunctionReturnsTrue(): void + { + self::assertTrue($this->config->useEuCdn()); + } } diff --git a/Test/Integration/Model/ScriptGeneratorTest.php b/Test/Integration/Model/ScriptGeneratorTest.php new file mode 100644 index 0000000..81bd4fa --- /dev/null +++ b/Test/Integration/Model/ScriptGeneratorTest.php @@ -0,0 +1,43 @@ +config = TestFrameworkObjectManager::getInstance()->create(Config::class); + } + + /** + * @magentoConfigFixture current_store web/cookiebot/use_eu_cdn 0 + */ + public function testDomainWhenUseEuCdnIsDisabled(): void + { + $generatedScript = ObjectManager::getInstance()->create(ScriptGenerator::class)->generate(); + self::assertStringContainsString('https://consent.cookiebot.com/uc.js', $generatedScript); + } + + /** + * @magentoConfigFixture current_store web/cookiebot/use_eu_cdn 1 + */ + public function testDomainWhenUseEuCdnIsEnabled(): void + { + $generatedScript = ObjectManager::getInstance()->create(ScriptGenerator::class)->generate(); + self::assertStringContainsString('https://consent.cookiebot.eu/uc.js', $generatedScript); + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 06a4543..fd4af28 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -21,6 +21,10 @@ 1 + + + Magento\Config\Model\Config\Source\Yesno + diff --git a/etc/config.xml b/etc/config.xml index 4a138b0..2bd5237 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -7,6 +7,7 @@ 0 + 0