Skip to content

Commit

Permalink
Merge pull request #23 from customgento/DEV-219-add-eu-cdn-support
Browse files Browse the repository at this point in the history
Add eu option, DEV-219
  • Loading branch information
sprankhub authored Nov 30, 2023
2 parents 1c2e47c + 011d8a6 commit e4a62cd
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 15 deletions.
6 changes: 6 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
5 changes: 5 additions & 0 deletions Model/ScriptGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class ScriptGenerator
{
private const COOKIEBOT_SCRIPT_FORMAT = '<script id="Cookiebot" data-cfasync="false" src="https://consent.cookiebot.com/uc.js" data-cbid="%s" %s type="text/javascript" async></script>';
private const EU_COOKIEBOT_SCRIPT_FORMAT = '<script id="Cookiebot" data-cfasync="false" src="https://consent.cookiebot.eu/uc.js" data-cbid="%s" %s type="text/javascript" async></script>';

/**
* @var Config
Expand All @@ -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);
}
}
26 changes: 13 additions & 13 deletions Test/Integration/AddScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
25 changes: 23 additions & 2 deletions Test/Integration/Model/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}
}
43 changes: 43 additions & 0 deletions Test/Integration/Model/ScriptGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace CustomGento\Cookiebot\Test\Integration\Model;

use CustomGento\Cookiebot\Model\Config;
use CustomGento\Cookiebot\Model\ScriptGenerator;
use Magento\Framework\App\ObjectManager;
use Magento\TestFramework\ObjectManager as TestFrameworkObjectManager;
use PHPUnit\Framework\TestCase;

class ScriptGeneratorTest extends TestCase
{
/**
* @var Config
*/
private $config;

protected function setUp(): void
{
parent::setUp();
$this->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);
}
}
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<field id="enabled">1</field>
</depends>
</field>
<field id="use_eu_cdn" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Use European CDN</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<enabled>0</enabled>
<id/>
<data_culture/>
<use_eu_cdn>0</use_eu_cdn>
</cookiebot>
</web>
</default>
Expand Down

0 comments on commit e4a62cd

Please sign in to comment.