From 36f78c4840551c9ce5cec79a9ee32d2eb2842f6e Mon Sep 17 00:00:00 2001 From: upchuk Date: Thu, 24 Jun 2021 12:45:31 +0200 Subject: [PATCH] EWPP-490: Cookie consent page URL. --- .../oe_webtools_cookie_consent.settings.yml | 2 ++ .../oe_webtools_cookie_consent.schema.yml | 4 ++++ .../oe_webtools_cookie_consent.module | 13 +++++++--- .../WebtoolsCookieConsentSettingsForm.php | 8 +++++++ tests/Behat/WebtoolsCookieConsentContext.php | 24 ++++++++++++++----- tests/features/cookie-consent-kit.feature | 18 ++++++++++++-- 6 files changed, 58 insertions(+), 11 deletions(-) diff --git a/modules/oe_webtools_cookie_consent/config/install/oe_webtools_cookie_consent.settings.yml b/modules/oe_webtools_cookie_consent/config/install/oe_webtools_cookie_consent.settings.yml index df2b05a6..213801f3 100644 --- a/modules/oe_webtools_cookie_consent/config/install/oe_webtools_cookie_consent.settings.yml +++ b/modules/oe_webtools_cookie_consent/config/install/oe_webtools_cookie_consent.settings.yml @@ -1,2 +1,4 @@ banner_popup: true video_popup: true +cookie_notice_url: '' +langcode: en diff --git a/modules/oe_webtools_cookie_consent/config/schema/oe_webtools_cookie_consent.schema.yml b/modules/oe_webtools_cookie_consent/config/schema/oe_webtools_cookie_consent.schema.yml index 154867e2..224d6e74 100644 --- a/modules/oe_webtools_cookie_consent/config/schema/oe_webtools_cookie_consent.schema.yml +++ b/modules/oe_webtools_cookie_consent/config/schema/oe_webtools_cookie_consent.schema.yml @@ -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.' diff --git a/modules/oe_webtools_cookie_consent/oe_webtools_cookie_consent.module b/modules/oe_webtools_cookie_consent/oe_webtools_cookie_consent.module index cbdcf380..402af2ae 100644 --- a/modules/oe_webtools_cookie_consent/oe_webtools_cookie_consent.module +++ b/modules/oe_webtools_cookie_consent/oe_webtools_cookie_consent.module @@ -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/'); @@ -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'], ]; diff --git a/modules/oe_webtools_cookie_consent/src/Form/WebtoolsCookieConsentSettingsForm.php b/modules/oe_webtools_cookie_consent/src/Form/WebtoolsCookieConsentSettingsForm.php index 35aa0e51..2a6d0977 100644 --- a/modules/oe_webtools_cookie_consent/src/Form/WebtoolsCookieConsentSettingsForm.php +++ b/modules/oe_webtools_cookie_consent/src/Form/WebtoolsCookieConsentSettingsForm.php @@ -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); } @@ -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); } diff --git a/tests/Behat/WebtoolsCookieConsentContext.php b/tests/Behat/WebtoolsCookieConsentContext.php index 7f5a435f..88486873 100644 --- a/tests/Behat/WebtoolsCookieConsentContext.php +++ b/tests/Behat/WebtoolsCookieConsentContext.php @@ -128,9 +128,9 @@ 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"}')) { @@ -138,15 +138,27 @@ public function assertCckJsonExists(): void { } } + /** + * 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"'); } /** @@ -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; diff --git a/tests/features/cookie-consent-kit.feature b/tests/features/cookie-consent-kit.feature index 4f9c1041..df79159f 100644 --- a/tests/features/cookie-consent-kit.feature +++ b/tests/features/cookie-consent-kit.feature @@ -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 @@ -8,7 +8,7 @@ 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 @@ -16,6 +16,20 @@ Feature: Cookie Consent kit. 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."