Skip to content

Commit e4a62cd

Browse files
authored
Merge pull request #23 from customgento/DEV-219-add-eu-cdn-support
Add eu option, DEV-219
2 parents 1c2e47c + 011d8a6 commit e4a62cd

File tree

7 files changed

+95
-15
lines changed

7 files changed

+95
-15
lines changed

Model/Config.php

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Config
1212
public const XML_PATH_COOKIEBOT_ENABLED = 'web/cookiebot/enabled';
1313
public const XML_PATH_COOKIEBOT_ID = 'web/cookiebot/id';
1414
public const XML_PATH_DATA_CULTURE = 'web/cookiebot/data_culture';
15+
public const XML_PATH_USE_EU_CDN = 'web/cookiebot/use_eu_cdn';
1516

1617
/**
1718
* @var ScopeConfigInterface
@@ -37,4 +38,9 @@ public function getDataCulture(): string
3738
{
3839
return (string)$this->scopeConfig->getValue(self::XML_PATH_DATA_CULTURE, ScopeInterface::SCOPE_STORE);
3940
}
41+
42+
public function useEuCdn(): bool
43+
{
44+
return $this->scopeConfig->isSetFlag(self::XML_PATH_USE_EU_CDN, ScopeInterface::SCOPE_STORE);
45+
}
4046
}

Model/ScriptGenerator.php

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class ScriptGenerator
88
{
99
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>';
10+
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>';
1011

1112
/**
1213
* @var Config
@@ -24,6 +25,10 @@ public function generate(): string
2425
$dataCulture = $this->config->getDataCulture() ?
2526
sprintf('data-culture="%s"', $this->config->getDataCulture()) : '';
2627

28+
if ($this->config->useEuCdn()) {
29+
return sprintf(self::EU_COOKIEBOT_SCRIPT_FORMAT, $cookiebotId, $dataCulture);
30+
}
31+
2732
return sprintf(self::COOKIEBOT_SCRIPT_FORMAT, $cookiebotId, $dataCulture);
2833
}
2934
}

Test/Integration/AddScriptTest.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,52 @@ class AddScriptTest extends AbstractController
1414
*/
1515
private $script;
1616

17-
protected function setUp()
17+
protected function setUp(): void
1818
{
1919
parent::setUp();
2020
$this->script = $this->_objectManager->create(ScriptGenerator::class)->generate();
2121
}
2222

2323
/**
24-
* @magentoConfigFixture default/web/cookiebot/enabled 1
25-
* @magentoConfigFixture default/web/cookiebot/id 123-456-789
24+
* @magentoConfigFixture current_store web/cookiebot/enabled 1
25+
* @magentoConfigFixture current_store web/cookiebot/id 123-456-789
2626
*/
2727
public function testScriptAddedOnHomepage(): void
2828
{
2929
$this->dispatch('/');
30-
self::assertContains($this->script, $this->getResponse()->getBody());
30+
self::assertStringContainsString($this->script, $this->getResponse()->getBody());
3131
}
3232

3333
/**
3434
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
35-
* @magentoConfigFixture default/web/cookiebot/enabled 1
36-
* @magentoConfigFixture default/web/cookiebot/id 123-456-789
35+
* @magentoConfigFixture current_store web/cookiebot/enabled 1
36+
* @magentoConfigFixture current_store web/cookiebot/id 123-456-789
3737
*/
3838
public function testScriptAddedOnProductPage(): void
3939
{
4040
$this->dispatch('/catalog/product/view/id/1');
41-
self::assertContains($this->script, $this->getResponse()->getBody());
41+
self::assertStringContainsString($this->script, $this->getResponse()->getBody());
4242
}
4343

4444
/**
4545
* @magentoDataFixture Magento/Catalog/_files/category.php
46-
* @magentoConfigFixture default/web/cookiebot/enabled 1
47-
* @magentoConfigFixture default/web/cookiebot/id 123-456-789
46+
* @magentoConfigFixture current_store web/cookiebot/enabled 1
47+
* @magentoConfigFixture current_store web/cookiebot/id 123-456-789
4848
*/
4949
public function testScriptAddedOnCategoryPage(): void
5050
{
5151
$this->dispatch('/catalog/category/view/id/333');
52-
self::assertContains($this->script, $this->getResponse()->getBody());
52+
self::assertStringContainsString($this->script, $this->getResponse()->getBody());
5353
}
5454

5555
/**
5656
* @magentoDataFixture Magento/Cms/_files/pages.php
57-
* @magentoConfigFixture default/web/cookiebot/enabled 1
58-
* @magentoConfigFixture default/web/cookiebot/id 123-456-789
57+
* @magentoConfigFixture current_store web/cookiebot/enabled 1
58+
* @magentoConfigFixture current_store web/cookiebot/id 123-456-789
5959
*/
6060
public function testScriptAddedOnCmsPage(): void
6161
{
6262
$this->dispatch('/page100');
63-
self::assertContains($this->script, $this->getResponse()->getBody());
63+
self::assertStringContainsString($this->script, $this->getResponse()->getBody());
6464
}
6565
}

Test/Integration/Model/ConfigTest.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testIsEnabledReturnsFalse(): void
3434
}
3535

3636
/**
37-
* @magentoConfigFixture default/web/cookiebot/enabled 1
37+
* @magentoConfigFixture current_store web/cookiebot/enabled 1
3838
*/
3939
public function testIsEnabledReturnsTrue(): void
4040
{
@@ -47,10 +47,31 @@ public function testGetIdReturnsEmptyStringByDefault(): void
4747
}
4848

4949
/**
50-
* @magentoConfigFixture default/web/cookiebot/id 123-456-789
50+
* @magentoConfigFixture current_store web/cookiebot/id 123-456-789
5151
*/
5252
public function testGetIdReturnsId(): void
5353
{
5454
self::assertEquals('123-456-789', $this->config->getId());
5555
}
56+
57+
public function testEuCdnEnabledReturnsFalseByDefault(): void
58+
{
59+
self::assertFalse($this->config->useEuCdn());
60+
}
61+
62+
/**
63+
* @magentoConfigFixture current_store web/cookiebot/use_eu_cdn 0
64+
*/
65+
public function testIfEuCdnEnabledConfigFunctionReturnsFalse(): void
66+
{
67+
self::assertFalse($this->config->useEuCdn());
68+
}
69+
70+
/**
71+
* @magentoConfigFixture current_store web/cookiebot/use_eu_cdn 1
72+
*/
73+
public function testIfEuCdnEnabledConfigFunctionReturnsTrue(): void
74+
{
75+
self::assertTrue($this->config->useEuCdn());
76+
}
5677
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CustomGento\Cookiebot\Test\Integration\Model;
6+
7+
use CustomGento\Cookiebot\Model\Config;
8+
use CustomGento\Cookiebot\Model\ScriptGenerator;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\TestFramework\ObjectManager as TestFrameworkObjectManager;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class ScriptGeneratorTest extends TestCase
14+
{
15+
/**
16+
* @var Config
17+
*/
18+
private $config;
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
$this->config = TestFrameworkObjectManager::getInstance()->create(Config::class);
24+
}
25+
26+
/**
27+
* @magentoConfigFixture current_store web/cookiebot/use_eu_cdn 0
28+
*/
29+
public function testDomainWhenUseEuCdnIsDisabled(): void
30+
{
31+
$generatedScript = ObjectManager::getInstance()->create(ScriptGenerator::class)->generate();
32+
self::assertStringContainsString('https://consent.cookiebot.com/uc.js', $generatedScript);
33+
}
34+
35+
/**
36+
* @magentoConfigFixture current_store web/cookiebot/use_eu_cdn 1
37+
*/
38+
public function testDomainWhenUseEuCdnIsEnabled(): void
39+
{
40+
$generatedScript = ObjectManager::getInstance()->create(ScriptGenerator::class)->generate();
41+
self::assertStringContainsString('https://consent.cookiebot.eu/uc.js', $generatedScript);
42+
}
43+
}

etc/adminhtml/system.xml

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<field id="enabled">1</field>
2222
</depends>
2323
</field>
24+
<field id="use_eu_cdn" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
25+
<label>Use European CDN</label>
26+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
27+
</field>
2428
</group>
2529
</section>
2630
</system>

etc/config.xml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<enabled>0</enabled>
88
<id/>
99
<data_culture/>
10+
<use_eu_cdn>0</use_eu_cdn>
1011
</cookiebot>
1112
</web>
1213
</default>

0 commit comments

Comments
 (0)