diff --git a/app/assets/stylesheets/admin/views/_edition-resource.scss b/app/assets/stylesheets/admin/views/_edition-resource.scss index b6013de0c4b..1f877699b65 100644 --- a/app/assets/stylesheets/admin/views/_edition-resource.scss +++ b/app/assets/stylesheets/admin/views/_edition-resource.scss @@ -15,4 +15,8 @@ .app-view-edition-resource__actions { margin-top: govuk-spacing(2); margin-bottom: 0; + + .app-view-edition-resource__actions__link { + align-self: center; + } } diff --git a/app/components/admin/edition_images/image_component.html.erb b/app/components/admin/edition_images/image_component.html.erb index 6c8561cf516..4156eb1a932 100644 --- a/app/components/admin/edition_images/image_component.html.erb +++ b/app/components/admin/edition_images/image_component.html.erb @@ -16,9 +16,18 @@ } %> -
- <% links.each do |link| %> - <%= link %> +
+ <%= link_to("Edit details", edit_admin_edition_image_path(edition, image), class: "govuk-link govuk-!-margin-top-2 app-view-edition-resource__actions__link") %> + <%= link_to("Delete image", confirm_destroy_admin_edition_image_path(edition, image), class: "govuk-link gem-link--destructive govuk-!-margin-top-2 app-view-edition-resource__actions__link") %> + + <% if edition.can_have_custom_lead_image? %> + <%= form_with url: admin_edition_lead_image_path(edition, image), method: :patch do |form| %> + <%= render "govuk_publishing_components/components/button", { + text: "Select as lead image", + secondary_solid: true, + margin_bottom: 4, + } %> + <% end %> <% end %>
diff --git a/app/components/admin/edition_images/image_component.rb b/app/components/admin/edition_images/image_component.rb index 676c3af5f21..a73e52bd409 100644 --- a/app/components/admin/edition_images/image_component.rb +++ b/app/components/admin/edition_images/image_component.rb @@ -38,11 +38,4 @@ def find_index_from_non_lead_images def find_image_index edition.images.find_index(image) end - - def links - links = [] - links << link_to("Edit details", edit_admin_edition_image_path(@edition, image), class: "govuk-link") - links << link_to("Delete image", confirm_destroy_admin_edition_image_path(@edition, image), class: "govuk-link gem-link--destructive") - links - end end diff --git a/app/components/admin/edition_images/lead_image_component.html.erb b/app/components/admin/edition_images/lead_image_component.html.erb index 236b1cc191d..1d396c8d604 100644 --- a/app/components/admin/edition_images/lead_image_component.html.erb +++ b/app/components/admin/edition_images/lead_image_component.html.erb @@ -10,24 +10,35 @@ <% lead_image_guidance %> <% end %> -<% if lead_image.present? || case_study? %> -
- <% if lead_image.present? %> -
-
- Lead image - <% unless lead_image.image_data&.all_asset_variants_uploaded? %> - Processing - <% end %> -
+<% if lead_image.present? %> +
+
+
+ Lead image + <% unless lead_image.image_data&.all_asset_variants_uploaded? %> + Processing + <% end %> +
-
-

Caption: <%= caption %>

-

Alt text: <%= alt_text %>

-
+
+

Caption: <%= caption %>

+

Alt text: <%= alt_text %>

- <% end %> +
+
+<% elsif show_default_lead_image? %> +
+
+
+ Default organisation image +
+
+

Default image for your organisation

+
+<% end %> +<% if render_resource_actions? %> +
<% if case_study? %> <%= form_with(url: update_image_display_option_admin_edition_path(edition), method: :patch) do |form| %> @@ -38,18 +49,13 @@ secondary_solid: true, margin_bottom: 4, } %> - - <% if lead_image.present? %> - <% links.each do |link| %> - <%= link %> - <% end %> - <% end %> - <% end %> - <% elsif lead_image.present? %> - <% links.each do |link| %> - <%= link %> <% end %> <% end %> + + <% if lead_image.present? %> + <%= link_to("Edit details", edit_admin_edition_image_path(edition, lead_image), class: "govuk-link app-view-edition-resource__actions__link") %> + <%= link_to("Delete image", confirm_destroy_admin_edition_image_path(edition, lead_image), class: "govuk-link gem-link--destructive app-view-edition-resource__actions__link") %> + <% end %>
<% end %> diff --git a/app/components/admin/edition_images/lead_image_component.rb b/app/components/admin/edition_images/lead_image_component.rb index c27661b1054..511a1a63fbf 100644 --- a/app/components/admin/edition_images/lead_image_component.rb +++ b/app/components/admin/edition_images/lead_image_component.rb @@ -15,9 +15,11 @@ def render? def lead_image_guidance if case_study? - tag.p("Using a lead image is optional and can be shown or hidden. The first image you upload is used as the lead image.", class: "govuk-body") + tag.p("The lead image appears at the top of the document. The same image cannot be used in the body text.", class: "govuk-body") + tag.p("Using a lead image is optional. To use a lead image either select the default image for your organisation or upload an image and select it as the lead image.", class: "govuk-body") + + tag.p("The lead image appears at the top of the document. The same image cannot be used in the body text.", class: "govuk-body") else - tag.p("The first image you upload is used as the lead image.", class: "govuk-body") + tag.p("The lead image appears at the top of the document. The same image cannot be used in the body text.", class: "govuk-body") + tag.p("Any image you upload can be selected as the lead image. If you do not select a new lead image, the default image for your organisation will be used.", class: "govuk-body") + + tag.p("The lead image appears at the top of the document. The same image cannot be used in the body text.", class: "govuk-body") end end @@ -25,6 +27,10 @@ def case_study? edition.type == "CaseStudy" end + def news_article? + edition.type == "NewsArticle" + end + def lead_image @lead_image ||= edition.lead_image end @@ -37,14 +43,16 @@ def alt_text lead_image.alt_text.presence || "None" end + def show_default_lead_image? + if case_study? + edition.emphasised_organisation_default_image_available? && [nil, "organisation_image"].include?(edition.image_display_option) + elsif news_article? + edition.has_lead_image? + end + end + def new_image_display_option - @new_image_display_option ||= if image_display_option_is_no_image? && edition_has_images? - "custom_image" - elsif image_display_option_is_no_image? - "organisation_image" - else - "no_image" - end + @new_image_display_option ||= image_display_option_is_no_image? ? "organisation_image" : "no_image" end def image_display_option_is_no_image? @@ -52,35 +60,18 @@ def image_display_option_is_no_image? end def update_image_display_option_button_text - return image_display_option_button_text_when_image_has_been_uploaded if edition_has_images? - - image_display_option_button_text_when_no_images_uploaded - end - - def image_display_option_button_text_when_image_has_been_uploaded - return "Hide lead image" if new_image_display_option_is_no_image? - - "Show lead image" - end - - def image_display_option_button_text_when_no_images_uploaded - return "Remove lead image" if new_image_display_option_is_no_image? - - "Use default image" + new_image_display_option_is_no_image? ? "Remove lead image" : "Use default image" end def new_image_display_option_is_no_image? new_image_display_option == "no_image" end - def edition_has_images? - edition.images.present? + def render_resource_actions? + case_study? || lead_image.present? end - def links - links = [] - links << link_to("Edit details", edit_admin_edition_image_path(edition, lead_image), class: "govuk-link") - links << link_to("Delete image", confirm_destroy_admin_edition_image_path(edition, lead_image), class: "govuk-link gem-link--destructive") - links + def edition_has_images? + edition.images.present? end end diff --git a/app/controllers/admin/edition_lead_images_controller.rb b/app/controllers/admin/edition_lead_images_controller.rb new file mode 100644 index 00000000000..1f7d1e1b31a --- /dev/null +++ b/app/controllers/admin/edition_lead_images_controller.rb @@ -0,0 +1,45 @@ +class Admin::EditionLeadImagesController < Admin::BaseController + before_action :find_edition, :find_image, :enforce_permissions! + layout "design_system" + + def update + edition_lead_image = @edition.edition_lead_image || @edition.build_edition_lead_image + edition_lead_image.assign_attributes(edition_lead_image_params) + + if updater.can_perform? && edition_lead_image.save! + updater.perform! + redirect_to admin_edition_images_path(@edition), notice: "Lead image updated to #{@image.image_data.carrierwave_image}" + else + redirect_to admin_edition_images_path(@edition), alert: updater.failure_reason + end + end + +private + + def find_edition + edition = Edition.find(params[:edition_id]) + @edition = LocalisedModel.new(edition, edition.primary_locale) + end + + def find_image + @image = @edition.images.find(params[:id]) + end + + def enforce_permissions! + enforce_permission!(:update, @edition) + end + + def edition_lead_image_params + { + image_id: @image.id, + edition_attributes: { + id: @edition.id, + image_display_option: "custom_image", + }, + } + end + + def updater + @updater ||= Whitehall.edition_services.draft_updater(@edition) + end +end diff --git a/app/models/case_study.rb b/app/models/case_study.rb index c716943dc87..8530d043dba 100644 --- a/app/models/case_study.rb +++ b/app/models/case_study.rb @@ -32,4 +32,8 @@ def base_path def publishing_api_presenter PublishingApi::CaseStudyPresenter end + + def emphasised_organisation_default_image_available? + lead_organisations.first&.default_news_image.present? + end end diff --git a/app/models/edition/custom_lead_image.rb b/app/models/edition/custom_lead_image.rb index dda08ad52cd..38c61256751 100644 --- a/app/models/edition/custom_lead_image.rb +++ b/app/models/edition/custom_lead_image.rb @@ -34,12 +34,12 @@ def remove_lead_image end def body_does_not_contain_lead_image - return if lead_image.blank? || images.none? + return if edition_lead_image.blank? || images.none? html = Whitehall::GovspeakRenderer.new.govspeak_edition_to_html(self) doc = Nokogiri::HTML::DocumentFragment.parse(html) - if doc.css("img").any? { |img| img[:src] == lead_image.url } + if doc.css("img").any? { |img| img[:src] == edition_lead_image.image.url } errors.add(:body, "cannot have a reference to the lead image in the text") end end diff --git a/app/models/edition_lead_image.rb b/app/models/edition_lead_image.rb index 03bafdf078a..85085d1c448 100644 --- a/app/models/edition_lead_image.rb +++ b/app/models/edition_lead_image.rb @@ -1,4 +1,6 @@ class EditionLeadImage < ApplicationRecord belongs_to :edition belongs_to :image + + accepts_nested_attributes_for :edition end diff --git a/app/presenters/publishing_api/case_study_presenter.rb b/app/presenters/publishing_api/case_study_presenter.rb index f9c0be64b77..7c685064a97 100644 --- a/app/presenters/publishing_api/case_study_presenter.rb +++ b/app/presenters/publishing_api/case_study_presenter.rb @@ -81,15 +81,11 @@ def image_details end def image_available? - item.lead_image.present? || emphasised_organisation_default_image_available? + item.lead_image.present? || item.emphasised_organisation_default_image_available? end def image_required? item.image_display_option != "no_image" end - - def emphasised_organisation_default_image_available? - item.lead_organisations.first.default_news_image.present? - end end end diff --git a/config/routes.rb b/config/routes.rb index 8cfc324b133..9b1f08e2f83 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -234,6 +234,7 @@ def redirect(path, options = { prefix: Whitehall.router_prefix }) resources :images, controller: "edition_images", only: %i[create destroy edit update index] do get :confirm_destroy, on: :member end + resources :lead_images, controller: "edition_lead_images", only: %i[update] end get "/editions/:id" => "editions#show" diff --git a/features/edition-images.feature b/features/edition-images.feature index cd9286e8df9..faf92fe1dd1 100644 --- a/features/edition-images.feature +++ b/features/edition-images.feature @@ -26,10 +26,21 @@ Feature: Images tab on edit edition Then I should see the updated image details Scenario: Lead image setting can be updated from the images tab - And a draft case study with images exists + And an organisation with a default news image exists + And the organisation has a draft case study with images When I visit the images tab of the document with images - And I click to hide the lead image - Then I should see a button to show the lead image + Then I should see the organisations default news image + When I click to hide the lead image + Then I should see a button to select a custom lead image + And I should see a button to choose to use the default image + + Scenario: User selects a new lead image + And a draft case study with images with the alt text "First image uploaded" and "Second image uploaded" exists + When I visit the images tab of the document with images + And I make the image with alt text "First image uploaded" the lead image + Then I can see that the image with alt text "First image uploaded" is the lead image + And I make the image with alt text "Second image uploaded" the lead image + Then I can see that the image with alt text "Second image uploaded" is the lead image Scenario: Image uploaded with no cropping required And I start drafting a new publication "Standard Beard Lengths" diff --git a/features/step_definitions/image_steps.rb b/features/step_definitions/image_steps.rb index 1fdde8d14a4..4812a3261f8 100644 --- a/features/step_definitions/image_steps.rb +++ b/features/step_definitions/image_steps.rb @@ -5,7 +5,17 @@ Given("a draft case study with images exists") do images = [build(:image), build(:image)] - @edition = create(:draft_case_study, body: "!!2", images:) + @edition = create(:draft_case_study, body: "!!2", images:, lead_image: images.first) +end + +Given("an organisation with a default news image exists") do + default_news_image = build(:featured_image_data) + @organisation = create(:organisation, default_news_image:) +end + +And("the organisation has a draft case study with images") do + images = [build(:image), build(:image)] + @edition = create(:draft_case_study, images:, lead_organisations: [@organisation]) end When("I visit the images tab of the document with images") do @@ -39,7 +49,7 @@ end When("I click to hide the lead image") do - find("button", text: "Hide lead image").click + find("button", text: "Remove lead image").click end When("I confirm the deletion") do @@ -63,8 +73,12 @@ expect(page).to have_content("Test caption") end -Then "I should see a button to show the lead image" do - expect(page).to have_content("Show lead image") +Then "I should see a button to select a custom lead image" do + assert_selector ".govuk-button", text: "Select as lead image", count: 2 +end + +And "I should see a button to choose to use the default image" do + assert_selector ".govuk-button", text: "Use default image", count: 1 end And(/^I upload a (\d+)x(\d+) image$/) do |width, height| @@ -99,3 +113,30 @@ Then(/^I should get (\d+) error message$/) do |count| expect(page).to have_selector(".gem-c-error-summary__list-item", count:) end + +Given(/^a draft case study with images with the alt text "([^"]*)" and "([^"]*)" exists$/) do |first_alt_text, second_alt_text| + # Unfortunately, we have to use alt text here to distinguish between the images. The assets + # are overwritten and stubbed out in asset_manager_helper.rb so the filename and url are always the same. + images = [build(:image, alt_text: first_alt_text), build(:image, alt_text: second_alt_text)] + @edition = create(:draft_case_study, images:) +end + +And(/^I make the image with alt text "([^"]*)" the lead image$/) do |alt_text| + image_container = find(".govuk-body", text: alt_text).ancestor("li") + + within image_container do + click_button "Select as lead image" + end +end + +Then(/^I can see that the image with alt text "([^"]*)" is the lead image$/) do |alt_text| + within ".app-c-edition-images-lead-image-component__lead_image" do + expect(page).to have_content alt_text + end +end + +Then(/^I should see the organisations default news image$/) do + within ".app-c-edition-images-lead-image-component__default_lead_image" do + assert_selector "img", count: 1 + end +end diff --git a/test/components/admin/edition_images/image_component_test.rb b/test/components/admin/edition_images/image_component_test.rb index 6811d3e7236..52b88d00f42 100644 --- a/test/components/admin/edition_images/image_component_test.rb +++ b/test/components/admin/edition_images/image_component_test.rb @@ -17,6 +17,8 @@ class Admin::EditionImages::ImageComponentTest < ViewComponent::TestCase assert_selector ".app-view-edition-resource__actions a[href='#{edit_admin_edition_image_path(edition, image)}']", text: "Edit details" assert_selector ".app-view-edition-resource__actions a[href='#{confirm_destroy_admin_edition_image_path(edition, image)}']", text: "Delete image" assert_selector ".app-view-edition-resource__section-break" + assert_selector "form[action='#{admin_edition_lead_image_path(edition, image)}']", count: 0 + assert_selector ".govuk-button", text: "Select as lead image", count: 0 end test "renders placeholder text for caption and alt text when none has been provided" do @@ -28,6 +30,16 @@ class Admin::EditionImages::ImageComponentTest < ViewComponent::TestCase assert_selector ".govuk-grid-row .govuk-grid-column-two-thirds .govuk-body:nth-child(2)", text: "Alt text: None" end + test "renders a form to the update lead image endpoint for case studies" do + image = build_stubbed(:image, caption: "caption", alt_text: "alt text") + edition = build_stubbed(:draft_case_study, images: [image]) + render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: false)) + + assert_selector "form[action='#{admin_edition_lead_image_path(edition, image)}']" do + assert_selector ".govuk-button", text: "Select as lead image" + end + end + test "image filename markdown displayed" do jpeg = upload_fixture("images/960x640_jpeg.jpg") gif = upload_fixture("images/960x640_gif.gif") diff --git a/test/components/admin/edition_images/lead_image_component_test.rb b/test/components/admin/edition_images/lead_image_component_test.rb index 8c8152fdad1..ad5c146750e 100644 --- a/test/components/admin/edition_images/lead_image_component_test.rb +++ b/test/components/admin/edition_images/lead_image_component_test.rb @@ -16,7 +16,7 @@ class Admin::EditionImages::LeadImageComponentTest < ViewComponent::TestCase edition = build_stubbed(:draft_case_study) render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) - first_para = "Using a lead image is optional and can be shown or hidden. The first image you upload is used as the lead image." + first_para = "Using a lead image is optional. To use a lead image either select the default image for your organisation or upload an image and select it as the lead image." second_para = "The lead image appears at the top of the document. The same image cannot be used in the body text." assert_selector ".govuk-details__text .govuk-body:nth-child(1)", text: first_para, visible: :hidden @@ -27,7 +27,7 @@ class Admin::EditionImages::LeadImageComponentTest < ViewComponent::TestCase edition = build_stubbed(:draft_news_article) render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) - first_para = "The first image you upload is used as the lead image." + first_para = "Any image you upload can be selected as the lead image. If you do not select a new lead image, the default image for your organisation will be used." second_para = "The lead image appears at the top of the document. The same image cannot be used in the body text." assert_selector ".govuk-details__text .govuk-body:nth-child(1)", text: first_para, visible: :hidden @@ -82,35 +82,33 @@ class Admin::EditionImages::LeadImageComponentTest < ViewComponent::TestCase end end - test "case studies has the correct fields when image_display_option is 'organisation_image' and no images have been uploaded" do - edition = build_stubbed(:draft_case_study, image_display_option: "organisation_image") + test "case studies has the correct fields when image_display_option is 'no_image' and images have been uploaded" do + image = build_stubbed(:image) + edition = build_stubbed(:draft_case_study, image_display_option: "no_image", images: [image]) render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) - assert_selector "form[action='#{update_image_display_option_admin_edition_path(edition)}']" do - assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='no_image']", visible: :hidden - assert_selector ".govuk-button", text: "Remove lead image" - end + assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='organisation_image']", visible: :hidden + assert_selector ".govuk-button", text: "Use default image" end - test "case studies has the correct fields when image_display_option is 'no_image' and images have been uploaded" do - image = build_stubbed(:image) - edition = build_stubbed(:draft_case_study, image_display_option: "no_image", images: [image]) + test "case studies has the correct fields when image_display_option is 'organisation_image' and no images have been uploaded" do + edition = build_stubbed(:draft_case_study, image_display_option: "organisation_image") render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) assert_selector "form[action='#{update_image_display_option_admin_edition_path(edition)}']" do - assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='custom_image']", visible: :hidden - assert_selector ".govuk-button", text: "Show lead image" + assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='no_image']", visible: :hidden + assert_selector ".govuk-button", text: "Remove lead image" end end test "case studies has the correct fields when image_display_option is 'custom_image' and images have been uploaded" do image = build_stubbed(:image) - edition = build_stubbed(:draft_case_study, image_display_option: "custom_image", images: [image]) + edition = build_stubbed(:draft_case_study, image_display_option: "custom_image", images: [image], lead_image: image) render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) assert_selector "form[action='#{update_image_display_option_admin_edition_path(edition)}']" do assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='no_image']", visible: :hidden - assert_selector ".govuk-button", text: "Hide lead image" + assert_selector ".govuk-button", text: "Remove lead image" end end @@ -122,4 +120,51 @@ class Admin::EditionImages::LeadImageComponentTest < ViewComponent::TestCase assert_selector "form[action='#{update_image_display_option_admin_edition_path(edition)}']", count: 0 assert_selector "input[type='hidden'][name='edition[image_display_option]']", visible: :hidden, count: 0 end + + test "case studies renders the organisations default_lead_image when image_display_option is 'organisation_image'" do + image = build(:featured_image_data) + organisation = build(:organisation, default_news_image: image) + edition = create(:draft_case_study, image_display_option: "organisation_image", lead_organisations: [organisation]) + render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) + + assert_selector ".app-c-edition-images-lead-image-component__default_lead_image img[alt='Default organisation image']" + assert_selector ".app-c-edition-images-lead-image-component__default_lead_image .govuk-hint", text: "Default image for your organisation" + end + + test "case studies renders the organisations default_lead_image when image_display_option is nil and no lead image is present" do + image = build(:featured_image_data) + organisation = build(:organisation, default_news_image: image) + edition = create(:draft_case_study, image_display_option: nil, lead_organisations: [organisation]) + render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) + + assert_selector ".app-c-edition-images-lead-image-component__default_lead_image img[alt='Default organisation image']" + assert_selector ".app-c-edition-images-lead-image-component__default_lead_image .govuk-hint", text: "Default image for your organisation" + end + + test "case studies doesn't render the organisations default_lead_image when image_display_option is 'no_image'" do + image = build(:featured_image_data) + organisation = build(:organisation, default_news_image: image) + edition = create(:draft_case_study, image_display_option: "no_image", lead_organisations: [organisation]) + render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) + + assert_selector ".app-c-edition-images-lead-image-component__default_lead_image", count: 0 + end + + test "news articles renders the organisations default_lead_image no lead image has been selected" do + image = build(:featured_image_data) + organisation = build(:organisation, default_news_image: image) + edition = create(:draft_news_article, lead_organisations: [organisation]) + render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) + + assert_selector ".app-c-edition-images-lead-image-component__default_lead_image img[alt='Default organisation image']" + assert_selector ".app-c-edition-images-lead-image-component__default_lead_image .govuk-hint", text: "Default image for your organisation" + end + + test "news articles doesn't render the organisations default_lead_image when one is not present" do + organisation = build(:organisation, default_news_image: nil) + edition = create(:draft_news_article, lead_organisations: [organisation]) + render_inline(Admin::EditionImages::LeadImageComponent.new(edition:)) + + assert_selector ".app-c-edition-images-lead-image-component__default_lead_image", count: 0 + end end diff --git a/test/functional/admin/edition_lead_images_controller_test.rb b/test/functional/admin/edition_lead_images_controller_test.rb new file mode 100644 index 00000000000..4aa0c35c08d --- /dev/null +++ b/test/functional/admin/edition_lead_images_controller_test.rb @@ -0,0 +1,71 @@ +require "test_helper" + +class Admin::EditionLeadImagesControllerTest < ActionController::TestCase + test "PATCH :update successfully updates the lead image and republishes the draft edition" do + login_as :writer + + image = build(:image) + edition = create(:draft_case_study, images: [image]) + + Whitehall::PublishingApi + .expects(:save_draft) + .with(edition) + .returns(true) + .once + + get :update, params: { edition_id: edition.id, id: image.id } + + assert_equal image, edition.reload.lead_image + assert_redirected_to admin_edition_images_path(edition) + assert_equal "Lead image updated to minister-of-funk.960x640.jpg", flash[:notice] + end + + test "PATCH :update does not update the lead image when the edition is invalid" do + login_as :writer + + published_edition = create(:published_case_study) + image = build(:image) + edition = create(:draft_case_study, images: [image], document: published_edition.document) + + edition.change_note = nil + edition.save!(validate: false) + + Whitehall::PublishingApi + .expects(:save_draft) + .never + + get :update, params: { edition_id: edition.id, id: image.id } + + assert_nil edition.reload.lead_image + assert_redirected_to admin_edition_images_path(edition) + assert_equal "This edition is invalid: Change note can't be blank", flash[:alert] + end + + test "PATCH :update does not update the lead image when edition's body contains the images markdown" do + login_as :writer + + published_edition = create(:published_case_study) + image = build(:image) + edition = create(:draft_case_study, images: [image], document: published_edition.document, body: "!!1") + + Whitehall::PublishingApi + .expects(:save_draft) + .never + + get :update, params: { edition_id: edition.id, id: image.id } + + assert_nil edition.reload.lead_image + assert_redirected_to admin_edition_images_path(edition) + assert_equal "This edition is invalid: Body cannot have a reference to the lead image in the text", flash[:alert] + end + + test "PATCH :update forbids unauthorised users" do + login_as :world_editor + image = build(:image) + edition = create(:draft_case_study, images: [image]) + + get :update, params: { edition_id: edition.id, id: image.id } + + assert_response :forbidden + end +end