Skip to content

Commit

Permalink
Add 'about' to schema.org article schema
Browse files Browse the repository at this point in the history
Add changelog entry

Test schema shorter
  • Loading branch information
oscarwyatt committed Aug 14, 2018
1 parent dc44dd6 commit c6d695a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions 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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -339,4 +339,4 @@ DEPENDENCIES
yard

BUNDLED WITH
1.16.1
1.16.2
Expand Up @@ -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
Expand Down Expand Up @@ -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
96 changes: 96 additions & 0 deletions spec/lib/presenters/schema_org_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit c6d695a

Please sign in to comment.