Skip to content

Commit

Permalink
Merge pull request #3409 from alphagov/unsubscribe_button
Browse files Browse the repository at this point in the history
Unsubscribe button on document collection pages
  • Loading branch information
hannako authored Nov 19, 2024
2 parents b3b4b15 + c53a907 commit c229e1c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 15 deletions.
3 changes: 3 additions & 0 deletions app/views/content_items/document_collection.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
schema: :article
) %>
<% end %>

<%= render 'shared/email_subscribe_unsubscribe_flash', { title: @content_item.title_and_context[:title] } %>

<%= render 'shared/intervention_banner' %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
Expand Down
5 changes: 4 additions & 1 deletion app/views/shared/_document_collections_email_signup.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
} %>
</div>
<% else %>
<%= render 'shared/single_page_notification_button', content_item: @content_item, skip_account: "true" %>
<%= render 'shared/single_page_notification_button', {
content_item: @content_item,
skip_account: @has_govuk_account ? "false" : "true"
} %>
<% end %>
2 changes: 1 addition & 1 deletion test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class ContentItemsControllerTest < ActionController::TestCase
assert response.headers["Vary"].include?("GOVUK-Account-Session-Flash")
end

%w[publication consultation detailed_guide call_for_evidence].each do |schema_name|
%w[publication consultation detailed_guide call_for_evidence document_collection].each do |schema_name|
test "#{schema_name} displays the subscription success banner when the 'email-subscription-success' flash is present" do
example_name = %w[consultation call_for_evidence].include?(schema_name) ? "open_#{schema_name}" : schema_name
content_item = content_store_has_schema_example(schema_name, example_name)
Expand Down
82 changes: 82 additions & 0 deletions test/integration/document_collection/email_notifications_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require "test_helper"

module DocumentCollection
class EmailNotificationsTest < ActionDispatch::IntegrationTest
include GovukPersonalisation::TestHelpers::Features

def schema_type
"document_collection"
end

def taxonomy_topic_base_path
"/taxonomy_topic_base_path"
end

def email_alert_frontend_signup_endpoint_no_account
"/email-signup"
end

def email_alert_frontend_signup_endpoint_enforce_account
"/email/subscriptions/single-page/new"
end

test "renders a signup link if the document collection has a taxonomy topic email override" do
content_item = get_content_example("document_collection")
content_item["links"]["taxonomy_topic_email_override"] = [{ "base_path" => taxonomy_topic_base_path.to_s }]
stub_content_store_has_item(content_item["base_path"], content_item)
visit_with_cachebust(content_item["base_path"])
assert page.has_css?(".gem-c-signup-link")
assert page.has_link?(href: "/email-signup/confirm?topic=#{taxonomy_topic_base_path}")
assert_not page.has_css?(".gem-c-single-page-notification-button")
end

test "renders the single page notification button with a form action of email-alert-frontend's non account signup endpoint" do
setup_and_visit_content_item("document_collection")

form = page.find("form.gem-c-single-page-notification-button")
assert_match(email_alert_frontend_signup_endpoint_no_account, form["action"])

button = page.find(:button, class: "gem-c-single-page-notification-button__submit")

expected_tracking = {
"event_name" => "navigation",
"type" => "subscribe",
"index_link" => 1,
"index_total" => 2,
"section" => "Top",
"url" => email_alert_frontend_signup_endpoint_no_account,
}
actual_tracking = JSON.parse(button["data-ga4-link"])

assert_equal expected_tracking, actual_tracking
end

test "renders the single page notification button with a form action of EmailAlertAPI's account-only endpoint for users logged into their gov.uk account" do
# Need to use Rack as Selenium, the default driver, doesn't provide header access, and we need to set a govuk_account_session header
Capybara.current_driver = :rack_test
mock_logged_in_session
setup_and_visit_content_item("document_collection")

form = page.find("form.gem-c-single-page-notification-button")
assert_match(email_alert_frontend_signup_endpoint_enforce_account, form["action"])

button = page.find(:button, class: "gem-c-single-page-notification-button__submit")

expected_tracking = {
"event_name" => "navigation",
"type" => "subscribe",
"index_link" => 1,
"index_total" => 2,
"section" => "Top",
"url" => "/email/subscriptions/single-page/new",
}

actual_tracking = JSON.parse(button["data-ga4-link"])

assert_equal expected_tracking, actual_tracking

# reset back to default driver
Capybara.use_default_driver
end
end
end
13 changes: 0 additions & 13 deletions test/integration/document_collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,4 @@ class DocumentCollectionTest < ActionDispatch::IntegrationTest
assert page.has_text?("This was published under the 2010 to 2015 Conservative and Liberal Democrat coalition government")
end
end

test "renders with the single page notification button" do
setup_and_visit_content_item("document_collection")
assert page.has_css?(".gem-c-single-page-notification-button")
assert_not page.has_css?(".gem-c-signup-link")
end

test "renders with the taxonomy subscription button" do
setup_and_visit_content_item_with_taxonomy_topic_email_override("document_collection")
assert page.has_css?(".gem-c-signup-link")
assert page.has_link?(href: "/email-signup/confirm?topic=/testpath")
assert_not page.has_css?(".gem-c-single-page-notification-button")
end
end

0 comments on commit c229e1c

Please sign in to comment.