From 3588910e85b36d69e059433726ca18fbe24325f6 Mon Sep 17 00:00:00 2001 From: Simon Hughesdon Date: Thu, 24 Oct 2019 13:57:32 +0100 Subject: [PATCH] Temp revert to previous FAQPages on Guides This is a temporary reversion to the original implementation of FAQPage schemas on Guides. We're running user research next week, and want to use this version's Google results. If the research goes well, we'll make a further decision then whether to roll this into the standard implementation. It was [originally implemented in govuk_publishing_components](https://github.com/alphagov/govuk_publishing_components/pull/1087) but I don't want to revert it in publishing components yet because: - Answers also use that implementation, and I don't want to mess with them right now - This version is temporary until 31/10/2019 when we'll decide what to do This version of the schema includes the entirety of a guide's content in the FAQPage schema on the first chapter. The subsequent chapters do not include an FAQPage schema to avoid duplicating rich results (but do include the Article schema). --- .../guide_faq_page_schema_presenter.rb | 74 +++++++++++++++++++ app/views/content_items/guide.html.erb | 8 +- test/integration/guide_test.rb | 29 ++++++-- 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 app/presenters/machine_readable/guide_faq_page_schema_presenter.rb diff --git a/app/presenters/machine_readable/guide_faq_page_schema_presenter.rb b/app/presenters/machine_readable/guide_faq_page_schema_presenter.rb new file mode 100644 index 000000000..64c7bec6f --- /dev/null +++ b/app/presenters/machine_readable/guide_faq_page_schema_presenter.rb @@ -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 diff --git a/app/views/content_items/guide.html.erb b/app/views/content_items/guide.html.erb index 1924dab20..db132ad49 100644 --- a/app/views/content_items/guide.html.erb +++ b/app/views/content_items/guide.html.erb @@ -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? %> + +<% end %> + <% content_for :simple_header, true %>
diff --git a/test/integration/guide_test.rb b/test/integration/guide_test.rb index dc421b28a..d59ac5f32 100644 --- a/test/integration/guide_test.rb +++ b/test/integration/guide_test.rb @@ -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