Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/controllers/forms/welsh_translation_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/input_objects/forms/delete_welsh_translation_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ def submit
true
end

def submit_without_confirm
reset_translations
true
end

private

def reset_translations
Expand Down
4 changes: 4 additions & 0 deletions app/input_objects/forms/welsh_translation_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
150 changes: 150 additions & 0 deletions spec/input_objects/forms/delete_welsh_translation_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions spec/input_objects/forms/welsh_translation_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
def build_form(attributes = {})
default_attributes = {
id: 1,
name: "Apply for a juggling licence",

Check failure on line 38 in spec/input_objects/forms/welsh_translation_input_spec.rb

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "Apply for a juggling licence" 3 times.

See more on https://sonarcloud.io/project/issues?id=alphagov_forms-admin&issues=AZz28SmoSq2uLhM1R5Vh&open=AZz28SmoSq2uLhM1R5Vh&pullRequest=2632
what_happens_next_markdown: "English what happens next",
welsh_completed: false,
what_happens_next_markdown_cy: "Welsh what happens next",
Expand All @@ -56,6 +56,30 @@
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")
Expand Down Expand Up @@ -657,4 +681,23 @@
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
17 changes: 17 additions & 0 deletions spec/requests/forms/welsh_translation_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
Loading