Skip to content

Commit

Permalink
EWPP-490: Cookie consent page URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
upchuk committed Jun 24, 2021
1 parent 91b07ff commit 36f78c4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
banner_popup: true
video_popup: true
cookie_notice_url: ''
langcode: en
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ oe_webtools_cookie_consent.settings:
type: boolean
label: 'CCK banner'
description: 'Enable CCK video banner for the supported video elements.'
cookie_notice_url:
type: text
label: 'Cookie Notice Page URL'
description: 'A custom URL for the cookie notice page.'
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use Drupal\Core\Url;
use Drupal\media\IFrameMarkup;
use Drupal\oe_webtools_cookie_consent\Event\ConfigBannerPopupEvent;
use Drupal\oe_webtools_cookie_consent\Event\ConfigVideoPopupEvent;
use Drupal\oe_webtools_cookie_consent\Form\WebtoolsCookieConsentSettingsForm;

define('OE_WEBTOOLS_COOKIE_CONSENT_EMBED_COOKIE_URL', '//europa.eu/webtools/crs/iframe/');

Expand All @@ -31,12 +32,18 @@ function oe_webtools_cookie_consent_page_attachments(array &$attachments) {
$cache->applyTo($attachments);
if ($event->isBannerPopup()) {
$attachments['#attached']['library'][] = 'oe_webtools/drupal.webtools-smartloader';
$script_values = [
'utility' => 'cck',
];

$url = \Drupal::config(WebtoolsCookieConsentSettingsForm::CONFIG_NAME)->get('cookie_notice_url');
if ($url) {
$script_values['url'] = $url;
}
$cck_attachment = [
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => Json::encode([
'utility' => 'cck',
]),
'#value' => Json::encode($script_values),
'#attributes' => ['type' => 'application/json'],
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
'#description' => $this->t('If checked, CCK will alter the URL to go through the EC Cookie Consent service.'),
];

$form['cookie_notice_url'] = [
'#type' => 'textfield',
'#title' => $this->t('Cookie Notice Page URL'),
'#default_value' => $this->config(static::CONFIG_NAME)->get('cookie_notice_url'),
'#description' => $this->t('The URL to the cookie notice page. The "{lang}" part of the URL will be automatically replaced by Webtools with the current language.'),
];

return parent::buildForm($form, $form_state);
}

Expand All @@ -52,6 +59,7 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config(static::CONFIG_NAME)
->set('banner_popup', $form_state->getValue('banner_popup'))
->set('video_popup', $form_state->getValue('video_popup'))
->set('cookie_notice_url', $form_state->getValue('cookie_notice_url'))
->save();
parent::submitForm($form, $form_state);
}
Expand Down
24 changes: 18 additions & 6 deletions tests/Behat/WebtoolsCookieConsentContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,37 @@ public function assertNoOembedIframeWithCckUsage(): void {
}

/**
* Asserts that the CCK JSON is available on the page.
* Asserts that the CCK JSON is available on the page with no URL.
*
* @Then the CCK JSON is available on the page
* @Then the CCK JSON is available on the page without a page URL
*/
public function assertCckJsonExists(): void {
if (!$this->webtoolsJsonSnippetExists('{"utility":"cck"}')) {
throw new \Exception(sprintf('No cookie consent kit JSON found and it should be available.'));
}
}

/**
* Asserts that the CCK JSON is available on the page with a URL.
*
* @param string $url
* The Cookie notice page URL.
*
* @Then the CCK JSON is available on the page with the URL :url
*/
public function assertCckJsonExistsWithUrl(string $url): void {
if (!$this->webtoolsJsonSnippetExists('{"utility":"cck","url":"' . addcslashes($url, '/') . '"}')) {
throw new \Exception(sprintf('No cookie consent kit JSON found with the specified URL and it should be available.'));
}
}

/**
* Asserts that the CCK JSON is NOT available on the page.
*
* @Then the CCK JSON is not available on the page
*/
public function assertNoCckJsonExists(): void {
if ($this->webtoolsJsonSnippetExists('{"utility":"cck"}')) {
throw new \Exception(sprintf('Cookie consent kit JSON found and it should not be available.'));
}
$this->assertSession()->responseNotContains('{"utility":"cck"');
}

/**
Expand All @@ -159,7 +171,7 @@ public function assertNoCckJsonExists(): void {
* Whether webtools JSON snippet is present or not.
*/
protected function webtoolsJsonSnippetExists(string $snippet): bool {
$xpath_query = "//script[@type='application/json'][.='" . addcslashes($snippet, '\\\'') . "']";
$xpath_query = "//script[@type='application/json'][.='" . addcslashes($snippet, '\'') . "']";
// Assert presence of webtools JSON with enabled javascript.
if (!$this->browserSupportsJavaScript()) {
return count($this->getSession()->getPage()->findAll('xpath', $xpath_query)) === 1;
Expand Down
18 changes: 16 additions & 2 deletions tests/features/cookie-consent-kit.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@api @install:oe_webtools_cookie_consent @BackupCookieConsentConfigs
@api @install:oe_webtools_cookie_consent @BackupCookieConsentConfigs @javascript @run
Feature: Cookie Consent kit.
In order to show usage of Cookie Consent kit on the website
As a anonymous user
Expand All @@ -8,14 +8,28 @@ Feature: Cookie Consent kit.
# Check that CCK javascript is loaded for the anonymous user.
Given I am an anonymous user
When I am on homepage
Then the CCK JSON is available on the page
Then the CCK JSON is available on the page without a page URL

# Change the configuration.
Given I am logged in as a user with the "administer webtools cookie consent" permission
When I am on "the Webtools Cookie Consent configuration page"
Then I should see "Webtools Cookie Consent settings"
And the "Enable the CCK banner." checkbox should be checked

# Set a cookie page URL.
When I fill in "Cookie Notice Page URL" with "http://example.com/cookie"
And I press "Save configuration"
Then I should see the message "The configuration options have been saved."
And the "Cookie Notice Page URL" field should contain "http://example.com/cookie"

# Check the CCK javascript is rendered with the page URL.
Given I am an anonymous user
When I am on homepage
Then the CCK JSON is available on the page with the URL "http://example.com/cookie"

# Disable the banner.
Given I am logged in as a user with the "administer webtools cookie consent" permission
When I am on "the Webtools Cookie Consent configuration page"
When I uncheck "Enable the CCK banner."
And I press "Save configuration"
Then I should see the message "The configuration options have been saved."
Expand Down

0 comments on commit 36f78c4

Please sign in to comment.