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