From ca62d8c1a8a9bbd791502c0d47aed2515cab239b Mon Sep 17 00:00:00 2001 From: Richard Towers Date: Wed, 6 Nov 2024 15:31:09 +0000 Subject: [PATCH] Add tests for landing page presenter expanding images --- .../publishing_api/landing_page_presenter.rb | 7 +- .../landing_page_presenter_test.rb | 101 ++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/app/presenters/publishing_api/landing_page_presenter.rb b/app/presenters/publishing_api/landing_page_presenter.rb index 02aa3f9da3c..7ba1527c1d7 100644 --- a/app/presenters/publishing_api/landing_page_presenter.rb +++ b/app/presenters/publishing_api/landing_page_presenter.rb @@ -99,8 +99,11 @@ def present_image(image, pattern) end def present_image_versions(image) - image.image_data.image_kind_config.versions.to_h do |v| - [v.name, image.url(v.name)] + image.image_data.image_kind_config.versions.map do |v| + { + name: v.name, + url: image.url(v.name), + } end end end 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 49c81898f0c..ca34192f751 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 @@ -117,4 +117,105 @@ class PublishingApi::LandingPagePresenterTest < ActiveSupport::TestCase assert_equal expected_details, presented_content[:details].deep_stringify_keys end + + test "it recursively expands images in the body" do + body = <<~YAML + blocks: + - type: hero + image: + sources: + - "[Image: big-cheese.960x640.jpg]" + - "[Image: minister-of-funk.960x640.jpg]" + - type: grid_container + blocks: + - type: hero + image: + sources: + - "[Image: big-cheese.960x640.jpg]" + 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(:image_data, file: upload_fixture("big-cheese.960x640.jpg", "image/jpg"))), + build(:image, image_data: build(:image_data, file: upload_fixture("minister-of-funk.960x640.jpg", "image/jpg"))), + ], + ) + + presented_landing_page = PublishingApi::LandingPagePresenter.new(landing_page) + presented_content = I18n.with_locale("en") { presented_landing_page.content } + + assert_pattern do + presented_content[:details].deep_symbolize_keys => { + blocks: [ + { + type: "hero", + image: { + sources: [ + { id: "big-cheese.960x640.jpg", image_kind: "default", versions: Array }, + { id: "minister-of-funk.960x640.jpg", image_kind: "default", versions: Array }, + ] + } + }, + { + type: "grid_container", + blocks: [{ + type: "hero", + image: { + sources: [ + { id: "big-cheese.960x640.jpg", image_kind: "default", versions: Array }, + ] + } + }], + }, + ]} + end + end + + test "it presents errors if files are not found" do + body = <<~YAML + blocks: + - type: hero + image: + sources: + - "[Image: non-existent-file.jpg]" + 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: [], + ) + + 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: { + sources: [ + { errors: ["Image not found for pattern [Image: non-existent-file.jpg]"] }, + ] + } + }, + ], + } + end + end end