Skip to content

Commit

Permalink
Merge pull request #1525 from alphagov/temp-guide-faq
Browse files Browse the repository at this point in the history
Temp revert to previous FAQPages on Guides
  • Loading branch information
sihugh authored Oct 24, 2019
2 parents a0b7a8e + 3588910 commit 0ba405d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 6 deletions.
74 changes: 74 additions & 0 deletions app/presenters/machine_readable/guide_faq_page_schema_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module MachineReadable
class GuideFaqPageSchemaPresenter
attr_reader :guide

def initialize(guide)
@guide = guide
end

def structured_data
# http://schema.org/FAQPage
{
"@context" => "http://schema.org",
"@type" => "FAQPage",
"headline" => guide.title,
"description" => guide.description,
"publisher" => {
"@type" => "Organization",
"name" => "GOV.UK",
"url" => "https://www.gov.uk",
"logo" => {
"@type" => "ImageObject",
"url" => logo_url,
},
},
}
.merge(main_entity)
end

private

def main_entity
{
"mainEntity" => questions_and_answers,
}
end

def questions_and_answers
guide.parts.each_with_index.map do |part, index|
part_url = part_url(part, index)

{
"@type" => "Question",
"name" => part["title"],
"url" => part_url,
"acceptedAnswer" => {
"@type" => "Answer",
"url" => part_url,
"text" => part["body"],
},
}
end
end

def part_url(part, index)
if index.zero?
guide_url
else
guide_url + "/" + part["slug"]
end
end

def guide_url
Plek.new.website_root + guide.base_path
end

def logo_url
image_url("govuk_publishing_components/govuk-logo.png")
end

def image_url(image_file)
ActionController::Base.helpers.asset_url(image_file, type: :image)
end
end
end
8 changes: 7 additions & 1 deletion app/views/content_items/guide.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<% content_for :extra_head_content do %>
<%= machine_readable_metadata(
schema: :faq,
schema: :article,
canonical_url: @content_item.canonical_url,
body: @content_item.has_parts? ? @content_item.current_part_body : nil
) %>
<% end %>

<% unless @content_item.requesting_a_part? %>
<script type="application/ld+json">
<%= raw MachineReadable::GuideFaqPageSchemaPresenter.new(@content_item).structured_data.to_json %>
</script>
<% end %>

<% content_for :simple_header, true %>

<div class="govuk-grid-row">
Expand Down
29 changes: 24 additions & 5 deletions test/integration/guide_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,35 @@ class GuideTest < ActionDispatch::IntegrationTest
setup_and_visit_content_item("guide")
faq_schema = find_structured_data(page, "FAQPage")

assert_equal faq_schema["headline"], @content_item["title"]
assert_not_equal faq_schema["mainEntity"], []
assert_equal faq_schema["@type"], "FAQPage"
assert_equal faq_schema["headline"], "The national curriculum"

q_and_as = faq_schema["mainEntity"]
answers = q_and_as.map { |q_and_a| q_and_a["acceptedAnswer"] }

chapter_titles = [
"Overview",
"Key stage 1 and 2",
"Key stage 3 and 4",
"Other compulsory subjects",
]
assert_equal chapter_titles, (q_and_as.map { |q_and_a| q_and_a["name"] })

guide_part_urls = [
"https://www.test.gov.uk/national-curriculum",
"https://www.test.gov.uk/national-curriculum/key-stage-1-and-2",
"https://www.test.gov.uk/national-curriculum/key-stage-3-and-4",
"https://www.test.gov.uk/national-curriculum/other-compulsory-subjects",
]
assert_equal guide_part_urls, (q_and_as.map { |q_and_a| q_and_a["url"] })
assert_equal guide_part_urls, (answers.map { |answer| answer["url"] })
end

test "guide chapters show the faq schema" do
test "guide chapters do not show the faq schema" do
setup_and_visit_part_in_guide
faq_schema = find_structured_data(page, "FAQPage")

assert_equal faq_schema["headline"], @content_item["title"]
assert_not_equal faq_schema["mainEntity"], []
assert_nil faq_schema
end

def setup_and_visit_part_in_guide
Expand Down

0 comments on commit 0ba405d

Please sign in to comment.