diff --git a/Gemfile b/Gemfile index eec0e26c8..11f1437f5 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem "dalli" gem "gds-api-adapters" gem "govuk_ab_testing" gem "govuk_app_config" +gem "govuk_personalisation" gem "govuk_publishing_components" gem "htmlentities" gem "plek" diff --git a/Gemfile.lock b/Gemfile.lock index 6919f088b..6dce19365 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -419,6 +419,7 @@ DEPENDENCIES gds-api-adapters govuk_ab_testing govuk_app_config + govuk_personalisation govuk_publishing_components govuk_schemas govuk_test diff --git a/app/controllers/content_items_controller.rb b/app/controllers/content_items_controller.rb index 1e3bbc6d1..8f93584eb 100644 --- a/app/controllers/content_items_controller.rb +++ b/app/controllers/content_items_controller.rb @@ -1,4 +1,6 @@ class ContentItemsController < ApplicationController + include GovukPersonalisation::ControllerConcern + rescue_from GdsApi::HTTPForbidden, with: :error_403 rescue_from GdsApi::HTTPNotFound, with: :error_notfound rescue_from GdsApi::HTTPGone, with: :error_410 @@ -199,4 +201,10 @@ def error_redirect(exception) ) redirect_to destination, status: status_code end + + def set_account_vary_header + # Override the default from GovukPersonalisation::ControllerConcern so pages are cached on each flash message + # variation, rather than caching pages per user + response.headers["Vary"] = [response.headers["Vary"], "GOVUK-Account-Session-Flash"].compact.join(", ") + end end diff --git a/app/views/content_items/detailed_guide.html.erb b/app/views/content_items/detailed_guide.html.erb index 60e46356a..26ff23ada 100644 --- a/app/views/content_items/detailed_guide.html.erb +++ b/app/views/content_items/detailed_guide.html.erb @@ -4,6 +4,8 @@ ) %> <% end %> +<%= render 'shared/email_subscribe_unsubscribe_flash' %> +
Go to your GOV.UK account to see and manage all your GOV.UK email subscriptions.
fatality_notice: alt_text: Ministry of Defence crest field_of_operation: Field of operation diff --git a/test/controllers/content_items_controller_test.rb b/test/controllers/content_items_controller_test.rb index 1261b4448..a68a230cf 100644 --- a/test/controllers/content_items_controller_test.rb +++ b/test/controllers/content_items_controller_test.rb @@ -366,6 +366,29 @@ class ContentItemsControllerTest < ActionController::TestCase assert_equal response.headers["Access-Control-Allow-Origin"], "*" end + test "sets GOVUK-Account-Session-Flash in the Vary header" do + content_item = content_store_has_schema_example("case_study", "case_study") + get :show, params: { path: path_for(content_item) } + + assert response.headers["Vary"].include?("GOVUK-Account-Session-Flash") + end + + test "displays the subscription success banner when the 'email-subscription-success' flash is present" do + content_item = content_store_has_schema_example("publication", "publication") + + request.headers["GOVUK-Account-Session"] = GovukPersonalisation::Flash.encode_session("session-id", %w[email-subscription-success]) + get :show, params: { path: path_for(content_item) } + assert response.body.include?("subscribed to emails about this page") + end + + test "displays the unsubscribe success banner when the 'email-unsubscribe-success' flash is present" do + content_item = content_store_has_schema_example("publication", "publication") + + request.headers["GOVUK-Account-Session"] = GovukPersonalisation::Flash.encode_session("session-id", %w[email-unsubscribe-success]) + get :show, params: { path: path_for(content_item) } + assert response.body.include?("unsubscribed from emails about this page") + end + def path_for(content_item, locale = nil) base_path = content_item["base_path"].sub(/^\//, "") base_path.gsub!(/\.#{locale}$/, "") if locale