Skip to content

Commit

Permalink
Prevent crash if meta tag dig encounters wrong data structure/type
Browse files Browse the repository at this point in the history
  • Loading branch information
AshGDS committed Nov 30, 2023
1 parent 7ff9203 commit 8ca006a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
useful summary for people upgrading their application, not a replication
of the commit log.

## Unreleased

* Prevent crash if meta tag dig encounters wrong data structure/type ([PR #3741](https://github.com/alphagov/govuk_publishing_components/pull/3741))

## 36.0.1

* Use component wrapper on option select ([PR #3738](https://github.com/alphagov/govuk_publishing_components/pull/3738))
Expand Down
8 changes: 7 additions & 1 deletion lib/govuk_publishing_components/presenters/meta_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ def add_political_tags(meta_tags)
# Some 'government' objects are nested in the content item on some pages. We want to grab these for GA4 tracking.
# We only add the publishing government if 'political' is true, otherwise some content items get incorrectly linked to a government
def add_ga4_political_tags(meta_tags)
government = content_item.dig(:links, :government, 0)
government = content_item.dig(:links, :government)

if government.is_a?(Array)
government = government[0]
else
return meta_tags
end

# political: true/false is in a different place to current: true/false, which is why we have 'details' and 'government[:details]'
if government && details[:political]
Expand Down
11 changes: 11 additions & 0 deletions spec/components/meta_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,17 @@ def example_document_for(schema_name, example_name)
assert_no_meta_tag("govuk:ga4-political-status")
end

it "doesn't crash generating GA4 political tags if it tries to dig into 'government' when it can't" do
content_item = {
"links": {
"government": "i am not an array nor a hash, therefore it seems that i may crash",
},
}
render_component(content_item: example_document_for("html_publication", "published_with_history_mode").merge(content_item))
assert_no_meta_tag("govuk:ga4-publishing-government")
assert_no_meta_tag("govuk:ga4-political-status")
end

it "doesn't render GA4 political tags if the government object exists in the content item, but political is false" do
content_item = {
"political": false,
Expand Down

0 comments on commit 8ca006a

Please sign in to comment.