From 222a870bcd24c87e08afa5efacc183a9eff32d71 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Wed, 29 Jun 2022 11:02:48 +0100 Subject: [PATCH 1/3] Replace the controller tests for GetInvolved This changes the previous tests, which tested a number of controller methods, into a set of tests that take the more idiomatic Rails approach of testing the controllers via requests. The motivation for this work was to enable the retirement of a large set of tests for this set of pages done through Smokey [1]. These Smokey tests are unconventional as they seem to be used as a the primary means to test these pages - this is not a reliable or scalable approach given they're in a separate repository. Nor is it necessary as the logic is within this application. [1]: https://github.com/alphagov/smokey/blob/7553a432259d785fb6d4fb81cfd4fe3572185ad3/features/apps/government_frontend.feature#L23-L140 --- app/views/content_items/get_involved.html.erb | 24 ++-- .../get_involved_controller_test.rb | 115 ++++++++++++------ 2 files changed, 91 insertions(+), 48 deletions(-) diff --git a/app/views/content_items/get_involved.html.erb b/app/views/content_items/get_involved.html.erb index 0f1114557..ed2784924 100644 --- a/app/views/content_items/get_involved.html.erb +++ b/app/views/content_items/get_involved.html.erb @@ -70,17 +70,19 @@ https://github.com/alphagov/smokey/blob/main/features/step_definitions/get_invol - <%# Attention to closing consultation %> - <%= render "govuk_publishing_components/components/inset_text", { - } do %> - <%= render "govuk_publishing_components/components/heading", { - text: time_until_closure(@next_closing_consultation), - heading_level: 3 - } %> -

- <%= @next_closing_consultation['title'] %> -

- <%= link_to t('get_involved.read_respond'), @next_closing_consultation['link'], class: "govuk-link" %> + <% if @next_closing_consultation %> + <%# Attention to closing consultation %> + <%= render "govuk_publishing_components/components/inset_text", { + } do %> + <%= render "govuk_publishing_components/components/heading", { + text: time_until_closure(@next_closing_consultation), + heading_level: 3 + } %> +

+ <%= @next_closing_consultation['title'] %> +

+ <%= link_to t('get_involved.read_respond'), @next_closing_consultation['link'], class: "govuk-link" %> + <% end %> <% end %> diff --git a/test/controllers/get_involved_controller_test.rb b/test/controllers/get_involved_controller_test.rb index c84c8085e..6e363fc9d 100644 --- a/test/controllers/get_involved_controller_test.rb +++ b/test/controllers/get_involved_controller_test.rb @@ -7,22 +7,23 @@ class GetInvolvedControllerTest < ActionController::TestCase def setup content_item_body = { + "document_type" => "get_involved", + "schema_name" => "get_involved", + "details" => {}, "links" => { "take_part_pages" => [ { - "title": "Page 1", + "title" => "Take part page 1", + "base_path" => "/take-part-1", "details" => { - "body" => "", "image" => {}, - "ordering" => 1, }, }, { - "title": "Page 2", + "title" => "Take part page 2", + "base_path" => "/take-part-2", "details" => { - "body" => "", "image" => {}, - "ordering" => 2, }, }, ], @@ -30,49 +31,89 @@ def setup } stub_content_store_has_item("/government/get-involved", content_item_body) + + no_results = { "results" => {}, "total" => 0, "start" => 0 } + stub_any_search.to_return("body" => no_results.to_json) end - test "retrieves correct number of open consultations from search_api" do - body = { - "results" => {}, - "total" => 83, - "start" => 0, - } - stub_search(body) + test "returns a 200 response" do + get :show + assert_response :ok + end - @controller.load_get_involved_data - assert_equal @controller.instance_variable_get(:@open_consultation_count), 83 + test "showing total number of open consultations" do + stub_search_query(query: hash_including(filter_content_store_document_type: "open_consultation"), + response: { "results" => [], "total" => 83 }) + + get :show + assert_select ".gem-c-big-number", /83.+Open consultations/m end - test "retrieves correct number of closed consultations from search_api" do - body = { - "results" => {}, - "total" => 42, - "start" => 0, - } - stub_search(body) + test "showing total number of closed consultations" do + stub_search_query(query: hash_including(filter_content_store_document_type: "closed_consultation"), + response: { "results" => [], "total" => 110 }) - assert_equal @controller.retrieve_date_filtered_closed_consultations(0), 42 + get :show + assert_select ".gem-c-big-number", /110.+Closed consultations/m end - test "retrieves next closing consultation from search_api" do - body = { - "results" => { - "first result" => {}, - "second result" => {}, - "third result" => {}, - }, - "total" => 42, - "start" => 0, - } - stub_search(body) + test "showing the next closing consultation" do + Timecop.freeze do + title = "Next closing consultation on time zones" + stub_search_query(query: hash_including(filter_content_store_document_type: "open_consultation", + filter_end_date: "from: #{Time.zone.now.to_date}"), + response: { "results" => [consultation_result(title: title)] }) + + get :show + assert_select ".gem-c-inset-text", /#{title}/ + end + end - assert_equal @controller.retrieve_next_closing, ["first result", {}] + test "showing recently opened consultations" do + title = "Open consultation on time zones" + stub_search_query(query: hash_including(filter_content_store_document_type: "open_consultation"), + response: { "results" => [consultation_result(title: title)] }) + + get :show + assert response.body.include?(title) + end + + test "showing recent consultation outcomes" do + title = "Consultation outcome on time zones" + stub_search_query(query: hash_including(filter_content_store_document_type: "consultation_outcome"), + response: { "results" => [consultation_result(title: title)] }) + + get :show + assert response.body.include?(title) + end + + test "shows the take part pages" do + get :show + assert response.body.include?("Take part page 1") + assert response.body.include?("Take part page 2") end private - def stub_search(body) - stub_any_search.to_return("body" => body.to_json) + def stub_search_query(query:, response:) + stub_request(:get, /\A#{Plek.current.find('search')}\/search.json/) + .with(query: query) + .to_return(body: response.to_json) + end + + def consultation_result(title: "Consulting on time zones") + { + "title" => title, + "public_timestamp" => "2022-02-14T00:00:00.000+01:00", + "end_date" => "2022-02-14T00:00:00.000+01:00", + "link" => "/consultation/link", + "organisations" => [{ + "slug" => "ministry-of-justice", + "link" => "/government/organisations/ministry-of-justice", + "title" => "Ministry of Justice", + "acronym" => "MoJ", + "organisation_state" => "live", + }], + } end end From 56a86f85e90a054dc0b56d857e5acb4345c54f4d Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Wed, 29 Jun 2022 11:09:26 +0100 Subject: [PATCH 2/3] Optimistically remove Smokey warning I intend to remove the majority of Smokey tests for this page so this warning will no longer be applicable. --- app/views/content_items/get_involved.html.erb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/views/content_items/get_involved.html.erb b/app/views/content_items/get_involved.html.erb index ed2784924..2018c5578 100644 --- a/app/views/content_items/get_involved.html.erb +++ b/app/views/content_items/get_involved.html.erb @@ -1,8 +1,3 @@ -<%# -Changes to this page impacts smokey tests that may need to be updated in parallel. -https://github.com/alphagov/smokey/blob/main/features/step_definitions/get_involved_page_steps.rb -%> - <% page_title %> <% page_class "govuk-main-wrapper" %> <%= render "govuk_publishing_components/components/breadcrumbs", { From fb039c66b4ffa5f2de54300b52ef8481148097ed Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Wed, 29 Jun 2022 11:11:05 +0100 Subject: [PATCH 3/3] Fix incorrect links on /government/get-involved These links were incorrect, presumably they are an artefact from what was used to copy+paste from originally. Interestingly these links actually have explicit tests in Smokey [1]. But it turns out they don't actually test correctly as they don't have an expect step [2]. As it is quite unusual to test at the fidelity of individual links in HTML (they're just testing you typed the same thing afterall) I've not added replacement tests. [1]: https://github.com/alphagov/smokey/blob/7553a432259d785fb6d4fb81cfd4fe3572185ad3/features/apps/government_frontend.feature#L23-L47 [2]: https://github.com/alphagov/smokey/blob/7553a432259d785fb6d4fb81cfd4fe3572185ad3/features/step_definitions/get_involved_page_steps.rb#L39-L41 --- app/views/content_items/get_involved.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/content_items/get_involved.html.erb b/app/views/content_items/get_involved.html.erb index 2018c5578..f94bb0c0a 100644 --- a/app/views/content_items/get_involved.html.erb +++ b/app/views/content_items/get_involved.html.erb @@ -53,14 +53,14 @@ <%= render "govuk_publishing_components/components/big_number", { number: @open_consultation_count, label: t('get_involved.open_consultations'), - href: "/government/organisations#ministerial_departments" + href: "/search/policy-papers-and-consultations?content_store_document_type=open_consultations" } %>
<%= render "govuk_publishing_components/components/big_number", { number: @closed_consultation_count, label: t('get_involved.closed_consultations'), - href: "/government/organisations#ministerial_departments" + href: "/search/policy-papers-and-consultations?content_store_document_type=closed_consultations" } %>