diff --git a/app/controllers/forms/welsh_translation_controller.rb b/app/controllers/forms/welsh_translation_controller.rb index 9f05e39c9..f7896c16e 100644 --- a/app/controllers/forms/welsh_translation_controller.rb +++ b/app/controllers/forms/welsh_translation_controller.rb @@ -13,7 +13,13 @@ def create @welsh_translation_input = WelshTranslationInput.new(welsh_translation_params) @table_presenter = Forms::TranslationTablePresenter.new - if @welsh_translation_input.submit + if @welsh_translation_input.all_fields_empty? + # if all fields are empty we delete the welsh translation + @delete_welsh_translation_input = Forms::DeleteWelshTranslationInput.new(form: current_form) + if @delete_welsh_translation_input.submit_without_confirm + redirect_to form_path(@welsh_translation_input.form), success: t("forms.welsh_translation.destroy.success") + end + elsif @welsh_translation_input.submit success_message = if @welsh_translation_input.mark_complete == "true" t("banner.success.form.welsh_translation_saved_and_completed") else diff --git a/app/input_objects/forms/delete_welsh_translation_input.rb b/app/input_objects/forms/delete_welsh_translation_input.rb index c61019196..28bbbc1f2 100644 --- a/app/input_objects/forms/delete_welsh_translation_input.rb +++ b/app/input_objects/forms/delete_welsh_translation_input.rb @@ -8,6 +8,11 @@ def submit true end + def submit_without_confirm + reset_translations + true + end + private def reset_translations diff --git a/app/input_objects/forms/welsh_translation_input.rb b/app/input_objects/forms/welsh_translation_input.rb index c8b7b87ef..393d7855f 100644 --- a/app/input_objects/forms/welsh_translation_input.rb +++ b/app/input_objects/forms/welsh_translation_input.rb @@ -123,6 +123,10 @@ def assign_form_values self end + def all_fields_empty? + attributes.except!("mark_complete").values.all?(&:blank?) + end + def form_has_declaration? form.declaration_markdown.present? end diff --git a/spec/input_objects/forms/delete_welsh_translation_input_spec.rb b/spec/input_objects/forms/delete_welsh_translation_input_spec.rb index 60a6f733d..3eb2d53cc 100644 --- a/spec/input_objects/forms/delete_welsh_translation_input_spec.rb +++ b/spec/input_objects/forms/delete_welsh_translation_input_spec.rb @@ -215,6 +215,156 @@ end end + describe "#submit_without_confirm" do + let(:form) do + create :form, + name_cy: "New Welsh name", + what_happens_next_markdown: "English what happens next", + what_happens_next_markdown_cy: "New Welsh what happens next", + declaration_text: "English declaration", + declaration_text_cy: "New Welsh declaration", + support_email: "english-support@example.gov.uk", + support_email_cy: "new-welsh-support@example.gov.uk", + support_phone: "English support phone", + support_phone_cy: "0800 123 4567", + support_url: "https://www.gov.uk/english-support", + support_url_cy: "https://www.gov.uk/new-welsh-support", + support_url_text: "English support url text", + support_url_text_cy: "New Welsh Support", + privacy_policy_url: "https://www.gov.uk/english-privacy", + privacy_policy_url_cy: "https://www.gov.uk/new-welsh-privacy", + payment_url_cy: "https://www.gov.uk/payments/new-welsh-payment-link", + available_languages: %w[en cy], + welsh_completed: true, + pages: [page] + end + + let(:page) do + create :page, + question_text_cy: "Ydych chi'n adnewyddu trwydded?", + hint_text: "English hint text", + hint_text_cy: "Dewiswch 'Ydw' os oes gennych drwydded ddilys eisoes.", + page_heading: "English page heading", + page_heading_cy: "Trwyddedu", + guidance_markdown: "English guidance", + guidance_markdown_cy: "Mae'r rhan hon o'r ffurflen yn ymwneud รข thrwyddedu.", + answer_settings_cy: { + selection_options: [ + { name: "Welsh option 1", value: "Option 1" }, + { name: "Welsh option 2", value: "Option 2" }, + ], + none_of_the_above_question: { + question_text: "Welsh none of the above question?", + }, + } + end + + let(:condition) do + create :condition, + routing_page: page, + exit_page_markdown_cy: "Nid ydych yn gymwys", + exit_page_heading_cy: "Mae'n ddrwg gennym, nid ydych yn gymwys ar gyfer y gwasanaeth hwn." + end + + context "when called" do + it "returns true" do + expect(delete_welsh_translation_input.submit_without_confirm).to be true + end + + it "deletes all of the welsh form content" do + expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.what_happens_next_markdown_cy }.to(nil) + .and change { form.reload.declaration_text_cy }.to(nil) + .and change { form.reload.support_email_cy }.to(nil) + .and change { form.reload.support_phone_cy }.to(nil) + .and change { form.reload.support_url_cy }.to(nil) + .and change { form.reload.support_url_text_cy }.to(nil) + .and change { form.reload.privacy_policy_url_cy }.to(nil) + .and change { form.reload.payment_url_cy }.to(nil) + end + + it "deletes all of the welsh page content" do + expect { delete_welsh_translation_input.submit_without_confirm }.to change { page.reload.question_text_cy }.to(nil) + .and change { page.reload.hint_text_cy }.to(nil) + .and change { page.reload.page_heading_cy }.to(nil) + .and change { page.reload.guidance_markdown_cy }.to(nil) + .and change { page.reload.answer_settings_cy }.to(nil) + end + + it "deletes all of the welsh condition content" do + expect { delete_welsh_translation_input.submit_without_confirm }.to change { condition.reload.exit_page_markdown_cy }.to(nil) + .and change { condition.reload.exit_page_heading_cy }.to(nil) + end + + it "resets the available_languages field to only include English" do + expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.available_languages }.to(%w[en]) + end + + it "resets the welsh_completed status" do + expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.welsh_completed }.to(false) + end + + context "when the form is live" do + let(:form) do + create :form, + :live, + name_cy: "New Welsh name", + what_happens_next_markdown: "English what happens next", + what_happens_next_markdown_cy: "New Welsh what happens next", + declaration_text: "English declaration", + declaration_text_cy: "New Welsh declaration", + support_email: "english-support@example.gov.uk", + support_email_cy: "new-welsh-support@example.gov.uk", + support_phone: "English support phone", + support_phone_cy: "0800 123 4567", + support_url: "https://www.gov.uk/english-support", + support_url_cy: "https://www.gov.uk/new-welsh-support", + support_url_text: "English support url text", + support_url_text_cy: "New Welsh Support", + privacy_policy_url: "https://www.gov.uk/english-privacy", + privacy_policy_url_cy: "https://www.gov.uk/new-welsh-privacy", + payment_url_cy: "https://www.gov.uk/payments/new-welsh-payment-link", + available_languages: %w[en cy], + welsh_completed: true, + pages: [page] + end + + it "sets the form_status to `live_with_draft`" do + expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.state }.to("live_with_draft") + end + end + + context "when the form is archived" do + let(:form) do + create :form, + :archived, + name_cy: "New Welsh name", + what_happens_next_markdown: "English what happens next", + what_happens_next_markdown_cy: "New Welsh what happens next", + declaration_text: "English declaration", + declaration_text_cy: "New Welsh declaration", + support_email: "english-support@example.gov.uk", + support_email_cy: "new-welsh-support@example.gov.uk", + support_phone: "English support phone", + support_phone_cy: "0800 123 4567", + support_url: "https://www.gov.uk/english-support", + support_url_cy: "https://www.gov.uk/new-welsh-support", + support_url_text: "English support url text", + support_url_text_cy: "New Welsh Support", + privacy_policy_url: "https://www.gov.uk/english-privacy", + privacy_policy_url_cy: "https://www.gov.uk/new-welsh-privacy", + payment_url_cy: "https://www.gov.uk/payments/new-welsh-payment-link", + available_languages: %w[en cy], + welsh_completed: true, + pages: [page] + end + + it "sets the form_status to `archived_with_draft`" do + expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.state }.to("archived_with_draft") + end + end + end + end + describe "#confirmed?" do context "when the input is invalid" do it "returns false" do diff --git a/spec/input_objects/forms/welsh_translation_input_spec.rb b/spec/input_objects/forms/welsh_translation_input_spec.rb index 6e2e13556..bb4c082dc 100644 --- a/spec/input_objects/forms/welsh_translation_input_spec.rb +++ b/spec/input_objects/forms/welsh_translation_input_spec.rb @@ -56,6 +56,30 @@ def build_form(attributes = {}) build(:form, default_attributes.merge(attributes)) end + def build_empty_welsh_form + empty_attributes = { + id: 1, + name: "Apply for a juggling licence", + what_happens_next_markdown: "English what happens next", + welsh_completed: false, + what_happens_next_markdown_cy: "", + declaration_text: "English declaration", + declaration_text_cy: "", + support_email: "english-support@example.gov.uk", + support_email_cy: "", + support_phone: "01234 987654", + support_phone_cy: "", + support_url_cy: "", + support_url: "https://www.gov.uk/english-support", + support_url_text_cy: "", + support_url_text: "English Support", + privacy_policy_url_cy: "", + payment_url: "https://www.gov.uk/english-payment", + payment_url_cy: "", + } + build(:form, empty_attributes) + end + describe "validations" do it "is not valid if mark complete is blank" do form = OpenStruct.new(welsh_completed: false, name: "Apply for a juggling licence") @@ -657,4 +681,23 @@ def build_form(attributes = {}) expect(welsh_translation_input.mark_complete).to eq(form.welsh_completed) end end + + describe "#all_fields_empty?" do + context "when the welsh translation fields are not empty" do + let(:form) { build_form } + + it "returns false" do + expect(welsh_translation_input.all_fields_empty?).to be false + end + end + + context "when the welsh translation fields are all empty" do + let(:form) { build_empty_welsh_form } + + it "returns true" do + welsh_translation_input = described_class.new(form:) + expect(welsh_translation_input.all_fields_empty?).to be true + end + end + end end diff --git a/spec/requests/forms/welsh_translation_controller_spec.rb b/spec/requests/forms/welsh_translation_controller_spec.rb index 93526a57a..0c3a1c0f0 100644 --- a/spec/requests/forms/welsh_translation_controller_spec.rb +++ b/spec/requests/forms/welsh_translation_controller_spec.rb @@ -94,6 +94,23 @@ end end + context "when 'Yes' is selected and all fields are empty" do + let(:page_translations_attributes) { { "0" => { "id" => form.pages.first.id, question_text_cy: "", condition_translations_attributes: } } } + let(:params) { { forms_welsh_translation_input: { form:, mark_complete:, name_cy: "", privacy_policy_url_cy: "", page_translations_attributes: } } } + + it "deletes the form" do + post(welsh_translation_create_path(id), params:) + expect(form.pages.first.reload.question_text_cy).to be_nil + expect(condition.reload.exit_page_heading_cy).to be_nil + end + + it "redirects to the form with a success banner" do + post(welsh_translation_create_path(id), params:) + expect(response).to redirect_to(form_path(id)) + expect(flash[:success]).to eq(I18n.t("forms.welsh_translation.destroy.success")) + end + end + context "when the user is not authorized" do let(:current_user) { build :user }