diff --git a/app/controllers/admin/editions_controller.rb b/app/controllers/admin/editions_controller.rb index df07efe1c66..4bdda6bfdfc 100644 --- a/app/controllers/admin/editions_controller.rb +++ b/app/controllers/admin/editions_controller.rb @@ -401,14 +401,32 @@ def set_default_edition_locations end end + # TODO: not sure this is the way forward. We can't reset orgs to [] because some update calls will + # naturally omit orgs, and we get a whole bunch of failing tests. + # I'm also not convinced we should set supporting orgs to [] if lead_orgs association is present. + # Feels a bit unclean - if we wanted to configure 'supporting orgs only' then that should perhaps + # be possible. + # Instead it feels like we should have some mechanism for saying: + # - Here's the current edition + # - Here's what the edition will be if the save completes + # - Has the current user lost access in this transition? If so, raise validation error def delete_absent_edition_organisations return if edition_params.empty? - if edition_params[:lead_organisation_ids] - edition_params[:lead_organisation_ids] = edition_params[:lead_organisation_ids].reject(&:blank?) + if @edition.respond_to?(:lead_organisation_ids) + edition_params[:lead_organisation_ids] = if edition_params[:lead_organisation_ids].blank? + [] + else + edition_params[:lead_organisation_ids].reject(&:blank?) + end end - if edition_params[:supporting_organisation_ids] - edition_params[:supporting_organisation_ids] = edition_params[:supporting_organisation_ids].reject(&:blank?) + + if @edition.respond_to?(:supporting_organisation_ids) + edition_params[:supporting_organisation_ids] = if edition_params[:supporting_organisation_ids].blank? + [] + else + edition_params[:supporting_organisation_ids].reject(&:blank?) + end end end diff --git a/test/functional/admin/standard_editions_controller_test.rb b/test/functional/admin/standard_editions_controller_test.rb index 02547e0a472..f4f6648c80c 100644 --- a/test/functional/admin/standard_editions_controller_test.rb +++ b/test/functional/admin/standard_editions_controller_test.rb @@ -1181,6 +1181,45 @@ class Admin::StandardEditionsControllerTest < ActionController::TestCase assert_select "textarea[name='edition[block_content][sidebar]']", text: "My sidebar content" end + test "treats absence of supporting_organisation_ids as an empty array" do + login_as create(:user, organisation: nil) + + configurable_document_type = build_configurable_document_type( + "test_type", + { + "forms" => { + "documents" => { + "fields" => { + "supporting_organisations" => { + "title" => "Supporting organisations", + "block" => "select_with_search_tagging", + "container" => "organisations", + "attribute_path" => %w[supporting_organisation_ids], + "translatable" => false, + }, + }, + }, + }, + }, + ) + ConfigurableDocumentType.setup_test_types(configurable_document_type) + + edition = create(:standard_edition, configurable_document_type: "test_type", supporting_organisation_ids: [create(:organisation).id]) + assert_not edition.supporting_organisation_ids.empty? + + patch :update, params: { + id: edition, + edition: { + # minimal permitted attrs to avoid strong params rejection + title: edition.title, + summary: edition.summary, + }, + save: "save", + } + assert_response :redirect + assert edition.reload.supporting_organisation_ids.empty? + end + view_test "GET edit renders hidden current_tab field for the default tab when document type defines tabs" do configurable_document_type = build_configurable_document_type("test_type", { "forms" => {