diff --git a/app/controllers/admin/document_collection_email_subscriptions_controller.rb b/app/controllers/admin/document_collection_email_subscriptions_controller.rb index dc172d4bbc89..e889cd6d1420 100644 --- a/app/controllers/admin/document_collection_email_subscriptions_controller.rb +++ b/app/controllers/admin/document_collection_email_subscriptions_controller.rb @@ -3,7 +3,9 @@ class Admin::DocumentCollectionEmailSubscriptionsController < Admin::BaseControl before_action :authorise_user layout "design_system" - def edit; end + def edit + @topic_list_presenter = TopicListPresenter.new(@collection) + end def update if params[:override_email_subscriptions] == "true" diff --git a/app/presenters/topic_list_presenter.rb b/app/presenters/topic_list_presenter.rb new file mode 100644 index 000000000000..dfaea9925f23 --- /dev/null +++ b/app/presenters/topic_list_presenter.rb @@ -0,0 +1,31 @@ +class TopicListPresenter + def initialize(collection) + @collection = collection + end + + attr_reader :collection + + def options(selected_taxon_content_id = nil) + topic_taxonomy.all_taxons.sort.map do |taxon| + { + text: taxon.name, + value: taxon.content_id, + selected: selected?(taxon.content_id, selected_taxon_content_id), + } + end + end + +private + + def selected?(content_id, selected_taxon_content_id) + content_id == (existing_taxonomy_topic_override || selected_taxon_content_id) + end + + def existing_taxonomy_topic_override + collection.taxonomy_topic_email_override + end + + def topic_taxonomy + @topic_taxonomy ||= Taxonomy::TopicTaxonomy.new + end +end \ No newline at end of file diff --git a/app/views/admin/document_collection_email_subscriptions/_taxonomy_choice.html.erb b/app/views/admin/document_collection_email_subscriptions/_taxonomy_choice.html.erb index 387b90fa9b7d..6a2e635f56e2 100644 --- a/app/views/admin/document_collection_email_subscriptions/_taxonomy_choice.html.erb +++ b/app/views/admin/document_collection_email_subscriptions/_taxonomy_choice.html.erb @@ -1,14 +1,9 @@ -<% selected = @collection.taxonomy_topic_email_override || params[:selected_taxon_content_id] %>

Choose the topic

<%= render "govuk_publishing_components/components/select", { id: "selected_taxon_content_id", label: "Choose a topic or subtopic from the topic taxonomy. You can only choose one.", - options: [ - { text: "", value: "" }, - { text: "Topic One", value: "9b889c60-2191-11ee-be56-0242ac120002", selected: selected_field?(selected,"9b889c60-2191-11ee-be56-0242ac120002")}, - { text: "Topic Two", value: "c60-2191-11ee-be56-0242ac120002", selected: selected_field?(selected,"c60-2191-11ee-be56-0242ac120002")} - ] + options: @topic_list_presenter.options(params[:selected_taxon_content_id]) } %>
<%= render "govuk_publishing_components/components/checkboxes", { diff --git a/features/document-collection-email-override.feature b/features/document-collection-email-override.feature index ad7c847640c4..df5fbc235764 100644 --- a/features/document-collection-email-override.feature +++ b/features/document-collection-email-override.feature @@ -6,7 +6,7 @@ Feature: Setting the taxonomy topic email override for a document collection When I visit the edit document collection page Then I click on the tab "Email notifications" And I choose "Emails about this topic" - And I select "Topic One" + And I select "Education" And I click the checkbox to confirm my selection. And I click "Save" Then I see the success message "You have set the email type" @@ -17,7 +17,7 @@ Feature: Setting the taxonomy topic email override for a document collection When I visit the edit document collection page Then I click on the tab "Email notifications" And I choose "Emails about this topic" - And I select "Topic One" + And I select "Education" And I click "Save" Then I see the error "You need to confirm" prompting me to confirm my selection. diff --git a/features/step_definitions/document_collection_email_override_steps.rb b/features/step_definitions/document_collection_email_override_steps.rb index 8d8fa1d26add..cd84d4d76b92 100644 --- a/features/step_definitions/document_collection_email_override_steps.rb +++ b/features/step_definitions/document_collection_email_override_steps.rb @@ -12,6 +12,7 @@ end Then(/^I click on the tab "Email notifications/) do + stub_taxonomy_data expect(page).to have_content("Email notifications") click_on("Email notifications") expect(page).to have_content("Choose the type of email updates users will get if they sign up for notifications.") diff --git a/test/functional/admin/document_collection_email_subscriptions_controller_test.rb b/test/functional/admin/document_collection_email_subscriptions_controller_test.rb index 99809847bf8f..a4ba64189380 100644 --- a/test/functional/admin/document_collection_email_subscriptions_controller_test.rb +++ b/test/functional/admin/document_collection_email_subscriptions_controller_test.rb @@ -1,11 +1,12 @@ require "test_helper" class Admin::DocumentCollectionEmailSubscriptionsControllerTest < ActionController::TestCase + include TaxonomyHelper setup do @collection = create(:draft_document_collection, :with_group) @user_with_permission = create(:writer, permissions: [User::Permissions::EMAIL_OVERRIDE_EDITOR]) @user_without_permission = create(:writer) - @selected_taxon_content_id = "9b889c60-2191-11ee-be56-0242ac120002" + @selected_taxon_content_id = root_taxon_content_id @put_params = { document_collection_id: @collection.id, override_email_subscriptions: "true", @@ -13,6 +14,7 @@ class Admin::DocumentCollectionEmailSubscriptionsControllerTest < ActionControll email_override_confirmation: "true", } login_as @user_without_permission + stub_taxonomy_with_all_taxons end should_be_an_admin_controller diff --git a/test/integration/document_collection_email_override_test.rb b/test/integration/document_collection_email_override_test.rb index ced82408108a..1943541dc356 100644 --- a/test/integration/document_collection_email_override_test.rb +++ b/test/integration/document_collection_email_override_test.rb @@ -15,6 +15,7 @@ class DocumentCollectionEmailOverrideTest < ActionDispatch::IntegrationTest before do login_as(user_with_permission_to_override) + stub_taxonomy_with_all_taxons end it "updates the taxonomy topic email override" do @@ -22,11 +23,11 @@ class DocumentCollectionEmailOverrideTest < ActionDispatch::IntegrationTest click_link "Email notifications" page.choose("Emails about the topic") - select "Topic One", from: "selected_taxon_content_id" + select root_taxon["title"], from: "selected_taxon_content_id" page.check("Select this box to confirm you're happy with what you've selected.") click_button("Save") document_collection.reload - assert_equal document_collection.taxonomy_topic_email_override, "9b889c60-2191-11ee-be56-0242ac120002" + assert_equal document_collection.taxonomy_topic_email_override, root_taxon_content_id end it "does not update taxonomy topic email if confirmation button is unchecked" do @@ -34,7 +35,7 @@ class DocumentCollectionEmailOverrideTest < ActionDispatch::IntegrationTest click_link "Email notifications" page.choose("Emails about the topic") - select "Topic One", from: "selected_taxon_content_id" + select root_taxon["title"], from: "selected_taxon_content_id" click_button("Save") document_collection.reload assert_nil document_collection.taxonomy_topic_email_override