Skip to content

Commit

Permalink
Merge pull request #1142 from alphagov/faq-errors
Browse files Browse the repository at this point in the history
FAQPage schema fixes
  • Loading branch information
sihugh authored Sep 26, 2019
2 parents 25c6a0c + dd2a678 commit 820f405
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## Unreleased

* Fix some minor issues with the FAQPage schema ([PR #1142](https://github.com/alphagov/govuk_publishing_components/pull/1142))
* Add error state to select component ([PR #1141](https://github.com/alphagov/govuk_publishing_components/pull/1141))
* Add size option to label ([PR #1140](https://github.com/alphagov/govuk_publishing_components/pull/1140))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def main_entity
end

def questions_and_answers_markup
question_and_answers(page.body).map do |question, value|
question_and_answers(page.body)
.select { |_, value| value[:answer].present? }
.map do |question, value|
q_and_a_url = section_url(value[:anchor])

{
Expand All @@ -42,7 +44,7 @@ def questions_and_answers_markup

# Generates a hash of questions and associated information:
# - question: the text in the h2 tag preceding other markup. Questions are
# used to key the hash. "Summary" is set as the default, as
# used to key the hash. The page title is set as the default, as
# there is often a preamble in guides before any h2 is set.
#
# - :answer: the markup that is not an h2 tag. It is associated with the
Expand All @@ -53,7 +55,7 @@ def questions_and_answers_markup
def question_and_answers(html)
doc = Nokogiri::HTML(html)

question = "Summary"
question = page.title

# rubocop:disable Style/IfInsideElse
doc.xpath("html/body").children.each_with_object({}) do |element, q_and_as|
Expand Down
24 changes: 21 additions & 3 deletions spec/lib/govuk_publishing_components/presenters/schema_org_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
expect(q_and_a.second["acceptedAnswer"]["text"].strip).to eq("<p>Give it a treat</p>")
end

it "handles missing h2s at the start of the body" do
it "handles missing h2s at the start of the body by using the page title" do
part_body = "<p>First catch your dragon</p>
<h2 id='step-two'>Step two</h2>
<p>Give it a treat</p>
Expand All @@ -103,18 +103,35 @@

content_item = dragon_guide


q_and_a = generate_structured_data(
content_item: content_item,
schema: :faq,
body: part_body
).structured_data['mainEntity']

expect(q_and_a.first["name"]).to eq("Summary")
expect(q_and_a.first["name"]).to eq("How to train your dragon")
expect(q_and_a.first["url"]).to eq("http://www.dev.gov.uk/how-to-train-your-dragon")
expect(q_and_a.first["acceptedAnswer"]["text"].strip).to eq("<p>First catch your dragon</p>")
end

it "does not include questions when there is no associated answer" do
part_body = "<p>First catch your dragon</p>
<h2 id='step-two'>Step two</h2>
<h2 id='step-three'>Step three</h2>
<p>Give it a pat (wear gloves)</p>"

content_item = dragon_guide

q_and_a = generate_structured_data(
content_item: content_item,
schema: :faq,
body: part_body
).structured_data['mainEntity']

expect(q_and_a.count).to eq(2)
expect(q_and_a.map { |faq| faq["name"] }).to_not include("Step two")
end

it "handles an empty body to ensure that preview works OK" do
empty_part_body = ""
content_item = dragon_guide
Expand All @@ -134,6 +151,7 @@ def dragon_guide
canonical_url: "http://www.dev.gov.uk/how-to-train-your-dragon/insurance"
) do |random_item|
random_item.merge(
"title" => "How to train your dragon",
"base_path" => "/how-to-train-your-dragon"
)
end
Expand Down

0 comments on commit 820f405

Please sign in to comment.