Skip to content

Commit

Permalink
Check that images have the correct kinds when presented
Browse files Browse the repository at this point in the history
  • Loading branch information
richardTowers committed Nov 18, 2024
1 parent b713dcc commit af572ca
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
11 changes: 10 additions & 1 deletion app/presenters/publishing_api/landing_page_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,63 @@ 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 =>
{
blocks: [
{
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",
],
}
},
],
Expand Down

0 comments on commit af572ca

Please sign in to comment.