Skip to content

Commit

Permalink
Fix alert banner cookie path (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
andybroomfield authored Jan 9, 2025
1 parent 124a971 commit f4b7793
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion js/alert_banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function setAlertBannerHideCookie(cookieTokens, token) {
const expiry = Date.now() + 30 * 24 * 60 * 60 * 1000;
document.cookie = `hide-alert-banner-token=${newCookie}; expires=${new Date(
expiry
).toUTCString()}; SameSite=Lax;`;
).toUTCString()}; path=/; SameSite=Lax;`;
}

(function localgovAlertBannerScript(Drupal) {
Expand Down
29 changes: 28 additions & 1 deletion tests/src/FunctionalJavascript/AlertBannerHideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testAlertBannerHide() {
$this->assertSession()->pageTextNotContains($alert_message);

// Test on login page.
$this->drupalGet('/user');
$this->drupalGet('/user/login');
$this->assertSession()->pageTextNotContains($alert_message);

// Update alert message.
Expand Down Expand Up @@ -117,6 +117,33 @@ public function testAlertBannerHide() {
$this->assertSession()->pageTextNotContains($alert_message);
$this->assertSession()->pageTextNotContains($alert_message_2);

// Set up a third alert banner.
$title_3 = $this->randomMachineName(8);
$alert_message_3 = 'Alert message: ' . $this->randomMachineName(16);
$alert_3 = $this->container->get('entity_type.manager')->getStorage('localgov_alert_banner')
->create([
'type' => 'localgov_alert_banner',
'title' => $title_3,
'short_description' => $alert_message_3,
'type_of_alert' => 'minor',
'moderation_state' => 'published',
]);
$alert_3->save();

// Hide the banner on the /user/login page.
// This is to test the cookie is set for the site and not just the path.
// @see https://github.com/localgovdrupal/localgov_alert_banner/issues/401
$this->drupalGet('/user/login');
$page = $this->getSession()->getPage();
$button_3 = $page->find('css', '[data-dismiss-alert-token="' . $alert_3->getToken() . '"] button');
$button_3->click();

// Reload home page.
$this->drupalGet('<front>');

// Test banner is not present.
$this->assertSession()->pageTextNotContains($alert_message_3);

}

}

0 comments on commit f4b7793

Please sign in to comment.