From db8ebd19187c288abb899bcaab2ac24ccd2c8e71 Mon Sep 17 00:00:00 2001 From: Simon Hughesdon Date: Wed, 5 May 2021 19:02:50 +0100 Subject: [PATCH 1/2] Turn off custom schemas at voting closing time The polls close at 10:00pm, so we should turn the hardcoded schema off at that time. Updates to the content are scheduled to be published at that time too, so the cache will be cleared by our standard processes. --- Gemfile | 1 + Gemfile.lock | 2 + app/presenters/content_item_presenter.rb | 7 ++- test/integration/guide_test.rb | 67 +++++++++++++++++++----- 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index 93b2c2cd5..aa1b76acf 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,7 @@ group :test do gem "minitest-reporters" gem "mocha" gem "simplecov" + gem "timecop" gem "webdrivers" gem "webmock", require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 9a70e323f..1eb4e9ca7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -326,6 +326,7 @@ GEM thor (1.1.0) thread_safe (0.3.6) tilt (2.0.10) + timecop (0.9.4) tzinfo (1.2.9) thread_safe (~> 0.1) uglifier (4.2.0) @@ -395,6 +396,7 @@ DEPENDENCIES sassc-rails simplecov slimmer + timecop uglifier webdrivers webmock diff --git a/app/presenters/content_item_presenter.rb b/app/presenters/content_item_presenter.rb index 405106210..5369aba8b 100644 --- a/app/presenters/content_item_presenter.rb +++ b/app/presenters/content_item_presenter.rb @@ -88,11 +88,16 @@ def show_phase_banner? def render_guide_as_single_page? # /how-to-vote - content_id == "9315bc67-33e7-42e9-8dea-e022f56dabfa" + content_id == "9315bc67-33e7-42e9-8dea-e022f56dabfa" && voting_is_open? end private + def voting_is_open? + polls_closing_time = Time.zone.parse("2021-05-06 22:00:00") + Time.zone.now < polls_closing_time + end + def display_date(timestamp, format = "%-d %B %Y") I18n.l(Time.zone.parse(timestamp), format: format, locale: "en") if timestamp end diff --git a/test/integration/guide_test.rb b/test/integration/guide_test.rb index 919688248..86f11d96b 100644 --- a/test/integration/guide_test.rb +++ b/test/integration/guide_test.rb @@ -113,28 +113,69 @@ class GuideTest < ActionDispatch::IntegrationTest end # The schema config is in /config/machine_readable/how-to-vote.yml - test "voting in the UK guide shows hard coded FAQ schema" do - setup_and_visit_voting_guide + test "voting in the UK guide shows hard coded FAQ schema until voting closes" do + when_voting_is_open do + setup_and_visit_voting_guide - faq_schema = find_structured_data(page, "FAQPage") - q_and_as = faq_schema["mainEntity"] + faq_schema = find_structured_data(page, "FAQPage") + q_and_as = faq_schema["mainEntity"] - assert_equal faq_schema["@type"], "FAQPage" - assert_equal faq_schema["headline"], "How to vote" - assert_equal faq_schema["description"], "

You need to register to vote before you can vote in UK elections or referendums.

If you’re eligible, you can vote in person on the day of the election at a named polling station. You can also apply for a postal or proxy vote instead.

There are elections and referendums in England, Scotland and Wales on 6 May 2021.

\n" + assert_equal faq_schema["@type"], "FAQPage" + assert_equal faq_schema["headline"], "How to vote" + assert_equal faq_schema["description"], "

You need to register to vote before you can vote in UK elections or referendums.

If you’re eligible, you can vote in person on the day of the election at a named polling station. You can also apply for a postal or proxy vote instead.

There are elections and referendums in England, Scotland and Wales on 6 May 2021.

\n" + + assert_equal 10, q_and_as.count + end + end + + test "voting in the UK guide shows all chapters on a single page until voting closes" do + when_voting_is_open do + content_item = setup_and_visit_voting_guide + part_titles = content_item["details"]["parts"].map { |part| part["title"] } + + part_titles.each do |part_title| + assert page.has_css? "h1", text: part_title + end + end + end + + test "voting in the UK guide shows autogenerated schemas after voting closes" do + once_voting_has_closed do + setup_and_visit_voting_guide + + faq_schema = find_structured_data(page, "FAQPage") - assert_equal 10, q_and_as.count + assert_equal faq_schema["@type"], "FAQPage" + assert_equal faq_schema["headline"], "The national curriculum" # the fake guide used in tests + end end - test "voting in the UK guide shows all chapters on a single page" do - content_item = setup_and_visit_voting_guide - part_titles = content_item["details"]["parts"].map { |part| part["title"] } + test "voting in the UK guide shows chapters on separate pages after voting closes" do + once_voting_has_closed do + content_item = setup_and_visit_voting_guide + + first_part = content_item["details"]["parts"].first + assert_selector "h1", text: first_part["title"] - part_titles.each do |part_title| - assert page.has_css? "h1", text: part_title + part_titles = content_item["details"]["parts"].drop(1).map { |part| part["title"] } + part_titles.each do |part_title| + assert_no_selector "h1", text: part_title + end end end + def once_voting_has_closed + Timecop.freeze(Time.zone.local(2021, 5, 6, 22, 0, 0)) + yield + Timecop.return + end + + def when_voting_is_open + Timecop.freeze(Time.zone.local(2021, 5, 6, 21, 59, 0)) + yield + Timecop.return + end + def setup_and_visit_voting_guide @content_item = get_content_example("guide").tap do |item| item["base_path"] = "/how-to-vote" From 3d6a7d216fe971d43ffcd55f94a82419b7b1f396 Mon Sep 17 00:00:00 2001 From: Simon Hughesdon Date: Wed, 5 May 2021 19:05:13 +0100 Subject: [PATCH 2/2] Stop redirecting /how-to-vote parts to the front We'll be reverting to using a multipart guide once polls have closed. This redirect is a bit of courtesy rather than anything crucial, so it's fine to turn it off a little ahead of time. --- config/routes.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 1cd261b87..54f6cafbd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,9 +20,6 @@ get "/government/uploads/*path" => "asset_manager_redirect#show", format: false - # Testing guides as a single page so we redirect parts to the default page - get "/how-to-vote/:chapter", to: redirect("/how-to-vote#%{chapter}") - get "*path/:variant" => "content_items#show", constraints: { variant: /print/,