diff --git a/app/presenters/content_item_presenter.rb b/app/presenters/content_item_presenter.rb index d92045d10..dac8a18cf 100644 --- a/app/presenters/content_item_presenter.rb +++ b/app/presenters/content_item_presenter.rb @@ -49,6 +49,10 @@ def content_id content_item["content_id"] end + def web_url + Plek.current.website_root + content_item["base_path"] + end + private def display_date(timestamp, format = "%-d %B %Y") diff --git a/app/presenters/travel_advice_presenter.rb b/app/presenters/travel_advice_presenter.rb index c8486d094..c91860537 100644 --- a/app/presenters/travel_advice_presenter.rb +++ b/app/presenters/travel_advice_presenter.rb @@ -90,10 +90,6 @@ def atom_public_updated_at DateTime.parse(content_item["public_updated_at"]) end - def web_url - Plek.current.website_root + content_item["base_path"] - end - private # Treat summary as the first part diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c33ae2af7..c556c0514 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -20,9 +20,28 @@ <%= javascript_include_tag "application", integrity: true, crossorigin: 'anonymous' %> <%= csrf_meta_tags %> <%= render partial: 'govuk_component/analytics_meta_tags', locals: { content_item: @content_item.content_item } %> + <% if @content_item.description %> <% end %> + + + + + + + + <% if @content_item.respond_to?(:image) && @content_item.image %> + " /> + " /> + + + " /> + " /> + <% else %> + + <% end %> + <%= contextual_comms_test_variant.analytics_meta_tag.html_safe if whitelisted_campaign_page? %> <%= yield :extra_head_content %> diff --git a/test/integration/meta_tags_test.rb b/test/integration/meta_tags_test.rb new file mode 100644 index 000000000..e9fda47d9 --- /dev/null +++ b/test/integration/meta_tags_test.rb @@ -0,0 +1,52 @@ +require 'test_helper' + +class MetaTagsTest < ActionDispatch::IntegrationTest + test "correct meta tags are displayed for pages" do + case_study = GovukSchemas::RandomExample.for_schema(frontend_schema: 'news_article') do |random| + random.merge( + "title" => "Zhe title", + "withdrawn_notice" => {}, + ) + end + + content_store_has_item("/some-page", case_study.to_json) + + visit "/some-page" + + assert page.has_css?("meta[property='og:title'][content='Zhe title']", visible: false) + end + + test "correct meta tags are displayed for pages without images" do + case_study = GovukSchemas::RandomExample.for_schema(frontend_schema: 'news_article') do |random| + random["details"].delete("image") + random + end + + content_store_has_item("/some-page", case_study.to_json) + + visit "/some-page" + + assert page.has_css?("meta[name='twitter:card'][content='summary']", visible: false) + end + + test "correct meta tags are displayed for pages with images" do + case_study = GovukSchemas::RandomExample.for_schema(frontend_schema: 'news_article') do |random| + random["details"] = random["details"].merge( + "image" => { + "url" => "https://example.org/image.jpg", + "alt_text" => "An accessible alt text", + } + ) + + random + end + + content_store_has_item("/some-page", case_study.to_json) + + visit "/some-page" + + assert page.has_css?("meta[name='twitter:card'][content='summary_large_image']", visible: false) + assert page.has_css?("meta[name='twitter:image'][content='https://example.org/image.jpg']", visible: false) + assert page.has_css?("meta[property='og:image'][content='https://example.org/image.jpg']", visible: false) + end +end