diff --git a/CHANGELOG.md b/CHANGELOG.md index a35a254f43..0e75820abe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ useful summary for people upgrading their application, not a replication of the commit log. +# Unreleased + +* Add the 'about' property for the schema.org schema for an Article with live taxons (PR #482) + ## 9.12.2 * Remove fixed 'name=button' attribute for buttons, to avoid them becoming a form param (PR #479) diff --git a/Gemfile.lock b/Gemfile.lock index 044cec3658..a6741523ca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - govuk_publishing_components (9.12.1) + govuk_publishing_components (9.12.2) govspeak (>= 5.0.3) govuk_app_config govuk_frontend_toolkit @@ -159,7 +159,7 @@ GEM mime-types-data (~> 3.2015) mime-types-data (3.2016.0521) mimemagic (0.3.2) - mini_mime (1.0.0) + mini_mime (1.0.1) mini_portile2 (2.3.0) minitest (5.11.3) money (6.12.0) @@ -339,4 +339,4 @@ DEPENDENCIES yard BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/lib/govuk_publishing_components/presenters/machine_readable/article_schema.rb b/lib/govuk_publishing_components/presenters/machine_readable/article_schema.rb index 46aa14ef4b..86355447f5 100644 --- a/lib/govuk_publishing_components/presenters/machine_readable/article_schema.rb +++ b/lib/govuk_publishing_components/presenters/machine_readable/article_schema.rb @@ -29,7 +29,7 @@ def structured_data "url" => page.logo_url, } } - }.merge(image_schema).merge(author_schema).merge(body).merge(is_part_of) + }.merge(image_schema).merge(author_schema).merge(body).merge(is_part_of).merge(about) end private @@ -98,6 +98,29 @@ def linked_page(step_by_step) image_placeholders: page.image_placeholders ) end + + def about + return {} unless live_taxons.any? + { + "about" => linked_taxons + } + end + + def live_taxons + taxons = page.content_item.dig("links", "taxons") + return [] unless taxons + taxons.select{ |taxon| taxon["phase"] == "live" } + end + + def linked_taxons + live_taxons.map do |taxon| + { + "@context" => "http://schema.org", + "@type" => "CreativeWork", + "sameAs" => taxon["web_url"] + } + end + end end end end diff --git a/spec/lib/presenters/schema_org_spec.rb b/spec/lib/presenters/schema_org_spec.rb index 917e3dc5da..24638841dc 100644 --- a/spec/lib/presenters/schema_org_spec.rb +++ b/spec/lib/presenters/schema_org_spec.rb @@ -174,6 +174,102 @@ expect(structured_data['image']).to eql([1, 2]) end + it "adds about schema if there are live taxons" do + content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: "answer") do |random_item| + random_item.merge(live_taxons_links) + end + + structured_data = generate_structured_data( + content_item: content_item, + schema: :article + ).structured_data + + expect(structured_data['about']).to eql([ + { + "@context" => "http://schema.org", + "@type" => "CreativeWork", + "sameAs" => "https://www.gov.uk/education/becoming-an-apprentice" + }, + { + "@context" => "http://schema.org", + "@type" => "CreativeWork", + "sameAs" => "https://www.gov.uk/employment/finding-job" + } + ]) + end + + it "adds about schema but not not include non live taxon" do + one_live_one_alpha_taxon = live_taxons_links + one_live_one_alpha_taxon["links"]["taxons"][1]["phase"] = "alpha" + content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: "answer") do |random_item| + random_item.merge(one_live_one_alpha_taxon) + end + + structured_data = generate_structured_data( + content_item: content_item, + schema: :article + ).structured_data + + expect(structured_data['about']).to eql([ + { + "@context" => "http://schema.org", + "@type" => "CreativeWork", + "sameAs" => "https://www.gov.uk/education/becoming-an-apprentice" + } + ]) + end + + it "does not include about if no live taxons" do + no_live_taxons = live_taxons_links + no_live_taxons["links"]["taxons"][0]["phase"] = "alpha" + no_live_taxons["links"]["taxons"][1]["phase"] = "draft" + content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: "answer") do |random_item| + random_item.merge(no_live_taxons) + end + + structured_data = generate_structured_data( + content_item: content_item, + schema: :article + ).structured_data + + expect(structured_data['about']).to eql(nil) + end + + def live_taxons_links + { + "links" => { + "taxons" => [ + { + "api_path" => "/api/content/education/becoming-an-apprentice", + "base_path" => "/education/becoming-an-apprentice", + "content_id" => "ff0e8e1f-4dea-42ff-b1d5-f1ae37807af2", + "document_type" => "taxon", + "locale" => "en", + "schema_name" => "taxon", + "title" => "Becoming an apprentice", + "withdrawn" => false, + "phase" => "live", + "api_url" => "https://www.gov.uk/api/content/education/becoming-an-apprentice", + "web_url" => "https://www.gov.uk/education/becoming-an-apprentice" + }, + { + "api_path" => "/api/content/employment/finding-job", + "base_path" => "/employment/finding-job", + "content_id" => "21bfd8f6-3360-43f9-be42-b00002982d70", + "document_type" => "taxon", + "locale" => "en", + "schema_name" => "taxon", + "title" => "Finding a job", + "withdrawn" => false, + "phase" => "live", + "api_url" => "https://www.gov.uk/api/content/employment/finding-job", + "web_url" => "https://www.gov.uk/employment/finding-job" + } + ] + } + } + end + def generate_structured_data(args) GovukPublishingComponents::Presenters::SchemaOrg.new(GovukPublishingComponents::Presenters::Page.new(args)) end