Skip to content

Commit db8ebd1

Browse files
committed
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.
1 parent 7a0b068 commit db8ebd1

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ group :test do
3939
gem "minitest-reporters"
4040
gem "mocha"
4141
gem "simplecov"
42+
gem "timecop"
4243
gem "webdrivers"
4344
gem "webmock", require: false
4445
end

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ GEM
326326
thor (1.1.0)
327327
thread_safe (0.3.6)
328328
tilt (2.0.10)
329+
timecop (0.9.4)
329330
tzinfo (1.2.9)
330331
thread_safe (~> 0.1)
331332
uglifier (4.2.0)
@@ -395,6 +396,7 @@ DEPENDENCIES
395396
sassc-rails
396397
simplecov
397398
slimmer
399+
timecop
398400
uglifier
399401
webdrivers
400402
webmock

app/presenters/content_item_presenter.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ def show_phase_banner?
8888

8989
def render_guide_as_single_page?
9090
# /how-to-vote
91-
content_id == "9315bc67-33e7-42e9-8dea-e022f56dabfa"
91+
content_id == "9315bc67-33e7-42e9-8dea-e022f56dabfa" && voting_is_open?
9292
end
9393

9494
private
9595

96+
def voting_is_open?
97+
polls_closing_time = Time.zone.parse("2021-05-06 22:00:00")
98+
Time.zone.now < polls_closing_time
99+
end
100+
96101
def display_date(timestamp, format = "%-d %B %Y")
97102
I18n.l(Time.zone.parse(timestamp), format: format, locale: "en") if timestamp
98103
end

test/integration/guide_test.rb

+54-13
Original file line numberDiff line numberDiff line change
@@ -113,28 +113,69 @@ class GuideTest < ActionDispatch::IntegrationTest
113113
end
114114

115115
# The schema config is in /config/machine_readable/how-to-vote.yml
116-
test "voting in the UK guide shows hard coded FAQ schema" do
117-
setup_and_visit_voting_guide
116+
test "voting in the UK guide shows hard coded FAQ schema until voting closes" do
117+
when_voting_is_open do
118+
setup_and_visit_voting_guide
118119

119-
faq_schema = find_structured_data(page, "FAQPage")
120-
q_and_as = faq_schema["mainEntity"]
120+
faq_schema = find_structured_data(page, "FAQPage")
121+
q_and_as = faq_schema["mainEntity"]
121122

122-
assert_equal faq_schema["@type"], "FAQPage"
123-
assert_equal faq_schema["headline"], "How to vote"
124-
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"
123+
assert_equal faq_schema["@type"], "FAQPage"
124+
assert_equal faq_schema["headline"], "How to vote"
125+
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"
126+
127+
assert_equal 10, q_and_as.count
128+
end
129+
end
130+
131+
test "voting in the UK guide shows all chapters on a single page until voting closes" do
132+
when_voting_is_open do
133+
content_item = setup_and_visit_voting_guide
134+
part_titles = content_item["details"]["parts"].map { |part| part["title"] }
135+
136+
part_titles.each do |part_title|
137+
assert page.has_css? "h1", text: part_title
138+
end
139+
end
140+
end
141+
142+
test "voting in the UK guide shows autogenerated schemas after voting closes" do
143+
once_voting_has_closed do
144+
setup_and_visit_voting_guide
145+
146+
faq_schema = find_structured_data(page, "FAQPage")
125147

126-
assert_equal 10, q_and_as.count
148+
assert_equal faq_schema["@type"], "FAQPage"
149+
assert_equal faq_schema["headline"], "The national curriculum" # the fake guide used in tests
150+
end
127151
end
128152

129-
test "voting in the UK guide shows all chapters on a single page" do
130-
content_item = setup_and_visit_voting_guide
131-
part_titles = content_item["details"]["parts"].map { |part| part["title"] }
153+
test "voting in the UK guide shows chapters on separate pages after voting closes" do
154+
once_voting_has_closed do
155+
content_item = setup_and_visit_voting_guide
156+
157+
first_part = content_item["details"]["parts"].first
158+
assert_selector "h1", text: first_part["title"]
132159

133-
part_titles.each do |part_title|
134-
assert page.has_css? "h1", text: part_title
160+
part_titles = content_item["details"]["parts"].drop(1).map { |part| part["title"] }
161+
part_titles.each do |part_title|
162+
assert_no_selector "h1", text: part_title
163+
end
135164
end
136165
end
137166

167+
def once_voting_has_closed
168+
Timecop.freeze(Time.zone.local(2021, 5, 6, 22, 0, 0))
169+
yield
170+
Timecop.return
171+
end
172+
173+
def when_voting_is_open
174+
Timecop.freeze(Time.zone.local(2021, 5, 6, 21, 59, 0))
175+
yield
176+
Timecop.return
177+
end
178+
138179
def setup_and_visit_voting_guide
139180
@content_item = get_content_example("guide").tap do |item|
140181
item["base_path"] = "/how-to-vote"

0 commit comments

Comments
 (0)