From af572ca7beaf49cdd6bf600718732fa4de1320ef Mon Sep 17 00:00:00 2001 From: Richard Towers Date: Fri, 8 Nov 2024 11:50:53 +0000 Subject: [PATCH] Check that images have the correct kinds when presented --- .../publishing_api/landing_page_presenter.rb | 11 +++- .../landing_page_presenter_test.rb | 51 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/app/presenters/publishing_api/landing_page_presenter.rb b/app/presenters/publishing_api/landing_page_presenter.rb index a6899324d2d..9e043ddd2f6 100644 --- a/app/presenters/publishing_api/landing_page_presenter.rb +++ b/app/presenters/publishing_api/landing_page_presenter.rb @@ -81,7 +81,16 @@ def recursively_expand_images(input) def present_hero_image(desktop, tablet, mobile) images = find_images(desktop, tablet, mobile) return { errors: ["Some image expressions weren't correctly formatted, or images could not be found"] } if images.any?(&:nil?) - return { errors: ["Some image variants hadn't finished uploading"] } unless images.map(&:image_data).all?(&:all_asset_variants_uploaded?) + + image_data = images.map(&:image_data) + desktop_image_kind, tablet_image_kind, mobile_image_kind = image_data.map(&:image_kind) + errors = [ + ("Some image variants hadn't finished uploading" unless image_data.all?(&:all_asset_variants_uploaded?)), + ("Desktop image is of the wrong image kind: #{desktop_image_kind}" unless desktop_image_kind == "hero_desktop"), + ("Tablet image is of the wrong image kind: #{tablet_image_kind}" unless tablet_image_kind == "hero_tablet"), + ("Mobile image is of the wrong image kind: #{mobile_image_kind}" unless mobile_image_kind == "hero_mobile"), + ].compact + return { errors: } unless errors.empty? desktop_image, tablet_image, mobile_image = images diff --git a/test/unit/app/presenters/publishing_api/landing_page_presenter_test.rb b/test/unit/app/presenters/publishing_api/landing_page_presenter_test.rb index e27b36e7327..b860654a354 100644 --- a/test/unit/app/presenters/publishing_api/landing_page_presenter_test.rb +++ b/test/unit/app/presenters/publishing_api/landing_page_presenter_test.rb @@ -231,6 +231,51 @@ class PublishingApi::LandingPagePresenterTest < ActiveSupport::TestCase presented_content = I18n.with_locale("en") { presented_landing_page.content } details = presented_content[:details].deep_symbolize_keys + assert_pattern do + details => + { + blocks: [ + { + type: "hero", + image: { + errors: ["Some image expressions weren't correctly formatted, or images could not be found"], + } + }, + ], + } + end + end + + test "it presents errors if image kinds don't match up" do + body = <<~YAML + blocks: + - type: hero + image: + sources: + desktop: "[Image: hero_image_mobile_2x.png]" # NOTE - using mobile image for desktop field + tablet: "[Image: hero_image_desktop_2x.png]" # NOTE - using desktop image for tablet field + mobile: "[Image: hero_image_tablet_2x.png]" # NOTE - using tablet image for desktop field + YAML + + landing_page = create( + :landing_page, + document: create(:document, id: 12_346, slug: "/landing-page/with-images"), + body:, + title: "Landing Page title", + summary: "Landing Page summary", + first_published_at: @first_published_at = Time.zone.now, + updated_at: 1.year.ago, + images: [ + build(:image, image_data: build(:hero_image_data, image_kind: "hero_desktop", file: upload_fixture("hero_image_desktop_2x.png", "image/png"))), + build(:image, image_data: build(:hero_image_data, image_kind: "hero_tablet", file: upload_fixture("hero_image_tablet_2x.png", "image/png"))), + build(:image, image_data: build(:hero_image_data, image_kind: "hero_mobile", file: upload_fixture("hero_image_mobile_2x.png", "image/png"))), + ], + ) + + presented_landing_page = PublishingApi::LandingPagePresenter.new(landing_page) + presented_content = I18n.with_locale("en") { presented_landing_page.content } + details = presented_content[:details].deep_symbolize_keys + assert_pattern do details => { @@ -238,7 +283,11 @@ class PublishingApi::LandingPagePresenterTest < ActiveSupport::TestCase { type: "hero", image: { - errors: ["Some image expressions weren't correctly formatted, or images could not be found"], + errors: [ + "Desktop image is of the wrong image kind: hero_mobile", + "Tablet image is of the wrong image kind: hero_desktop", + "Mobile image is of the wrong image kind: hero_tablet", + ], } }, ],