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 8, 2024
1 parent e5be42e commit a35c116
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 @@ -225,6 +225,51 @@ class PublishingApi::LandingPagePresenterTest < ActiveSupport::TestCase
first_published_at: @first_published_at = Time.zone.now,
updated_at: 1.year.ago,
images: [],
)

Check failure on line 228 in test/unit/app/presenters/publishing_api/landing_page_presenter_test.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/ClosingParenthesisIndentation: Indent `)` to column 4 (not 6)

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"],
}
},
],
}
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)
Expand All @@ -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",
],
}
},
],
Expand Down

0 comments on commit a35c116

Please sign in to comment.