Skip to content

Commit

Permalink
Merge pull request #2098 from alphagov/retire-schema
Browse files Browse the repository at this point in the history
Retire hardcoded schema at 10pm on voting day
  • Loading branch information
sihugh authored May 7, 2021
2 parents 1533f0d + 3d6a7d2 commit 918c3c0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ group :test do
gem "minitest-reporters"
gem "mocha"
gem "simplecov"
gem "timecop"
gem "webdrivers"
gem "webmock", require: false
end
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -395,6 +396,7 @@ DEPENDENCIES
sassc-rails
simplecov
slimmer
timecop
uglifier
webdrivers
webmock
Expand Down
7 changes: 6 additions & 1 deletion app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
Expand Down
67 changes: 54 additions & 13 deletions test/integration/guide_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"], "<p>You need to <a href=\"/register-to-vote?src=schema\">register to vote</a> before you can vote in UK elections or referendums.</p> <p>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.</p> <p>There are elections and referendums in England, Scotland and Wales on 6 May 2021.</p>\n"
assert_equal faq_schema["@type"], "FAQPage"
assert_equal faq_schema["headline"], "How to vote"
assert_equal faq_schema["description"], "<p>You need to <a href=\"/register-to-vote?src=schema\">register to vote</a> before you can vote in UK elections or referendums.</p> <p>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.</p> <p>There are elections and referendums in England, Scotland and Wales on 6 May 2021.</p>\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"
Expand Down

0 comments on commit 918c3c0

Please sign in to comment.