Skip to content

Commit b417f03

Browse files
committed
Make removing the Welsh content for a form reset the Welsh task
When a user removes all the welsh content from a form, we delete the welsh version and reset the welsh task
1 parent 3b68280 commit b417f03

File tree

6 files changed

+226
-1
lines changed

6 files changed

+226
-1
lines changed

app/controllers/forms/welsh_translation_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ def create
1313
@welsh_translation_input = WelshTranslationInput.new(welsh_translation_params)
1414
@table_presenter = Forms::TranslationTablePresenter.new
1515

16-
if @welsh_translation_input.submit
16+
if @welsh_translation_input.all_fields_empty?
17+
# if all fields are empty we delete the welsh translation
18+
@delete_welsh_translation_input = Forms::DeleteWelshTranslationInput.new(form: current_form)
19+
if @delete_welsh_translation_input.submit_without_confirm
20+
redirect_to form_path(@welsh_translation_input.form), success: t("forms.welsh_translation.destroy.success")
21+
end
22+
elsif @welsh_translation_input.submit
1723
success_message = if @welsh_translation_input.mark_complete == "true"
1824
t("banner.success.form.welsh_translation_saved_and_completed")
1925
else

app/input_objects/forms/delete_welsh_translation_input.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ def submit
88
true
99
end
1010

11+
def submit_without_confirm
12+
reset_translations
13+
true
14+
end
15+
1116
private
1217

1318
def reset_translations

app/input_objects/forms/welsh_translation_input.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ def assign_form_values
123123
self
124124
end
125125

126+
def all_fields_empty?
127+
attributes.except!("mark_complete").values.all?(&:blank?)
128+
end
129+
126130
def form_has_declaration?
127131
form.declaration_markdown.present?
128132
end

spec/input_objects/forms/delete_welsh_translation_input_spec.rb

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,156 @@
215215
end
216216
end
217217

218+
describe "#submit_without_confirm" do
219+
let(:form) do
220+
create :form,
221+
name_cy: "New Welsh name",
222+
what_happens_next_markdown: "English what happens next",
223+
what_happens_next_markdown_cy: "New Welsh what happens next",
224+
declaration_text: "English declaration",
225+
declaration_text_cy: "New Welsh declaration",
226+
support_email: "english-support@example.gov.uk",
227+
support_email_cy: "new-welsh-support@example.gov.uk",
228+
support_phone: "English support phone",
229+
support_phone_cy: "0800 123 4567",
230+
support_url: "https://www.gov.uk/english-support",
231+
support_url_cy: "https://www.gov.uk/new-welsh-support",
232+
support_url_text: "English support url text",
233+
support_url_text_cy: "New Welsh Support",
234+
privacy_policy_url: "https://www.gov.uk/english-privacy",
235+
privacy_policy_url_cy: "https://www.gov.uk/new-welsh-privacy",
236+
payment_url_cy: "https://www.gov.uk/payments/new-welsh-payment-link",
237+
available_languages: %w[en cy],
238+
welsh_completed: true,
239+
pages: [page]
240+
end
241+
242+
let(:page) do
243+
create :page,
244+
question_text_cy: "Ydych chi'n adnewyddu trwydded?",
245+
hint_text: "English hint text",
246+
hint_text_cy: "Dewiswch 'Ydw' os oes gennych drwydded ddilys eisoes.",
247+
page_heading: "English page heading",
248+
page_heading_cy: "Trwyddedu",
249+
guidance_markdown: "English guidance",
250+
guidance_markdown_cy: "Mae'r rhan hon o'r ffurflen yn ymwneud â thrwyddedu.",
251+
answer_settings_cy: {
252+
selection_options: [
253+
{ name: "Welsh option 1", value: "Option 1" },
254+
{ name: "Welsh option 2", value: "Option 2" },
255+
],
256+
none_of_the_above_question: {
257+
question_text: "Welsh none of the above question?",
258+
},
259+
}
260+
end
261+
262+
let(:condition) do
263+
create :condition,
264+
routing_page: page,
265+
exit_page_markdown_cy: "Nid ydych yn gymwys",
266+
exit_page_heading_cy: "Mae'n ddrwg gennym, nid ydych yn gymwys ar gyfer y gwasanaeth hwn."
267+
end
268+
269+
context "when called" do
270+
it "returns true" do
271+
expect(delete_welsh_translation_input.submit_without_confirm).to be true
272+
end
273+
274+
it "deletes all of the welsh form content" do
275+
expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.what_happens_next_markdown_cy }.to(nil)
276+
.and change { form.reload.declaration_text_cy }.to(nil)
277+
.and change { form.reload.support_email_cy }.to(nil)
278+
.and change { form.reload.support_phone_cy }.to(nil)
279+
.and change { form.reload.support_url_cy }.to(nil)
280+
.and change { form.reload.support_url_text_cy }.to(nil)
281+
.and change { form.reload.privacy_policy_url_cy }.to(nil)
282+
.and change { form.reload.payment_url_cy }.to(nil)
283+
end
284+
285+
it "deletes all of the welsh page content" do
286+
expect { delete_welsh_translation_input.submit_without_confirm }.to change { page.reload.question_text_cy }.to(nil)
287+
.and change { page.reload.hint_text_cy }.to(nil)
288+
.and change { page.reload.page_heading_cy }.to(nil)
289+
.and change { page.reload.guidance_markdown_cy }.to(nil)
290+
.and change { page.reload.answer_settings_cy }.to(nil)
291+
end
292+
293+
it "deletes all of the welsh condition content" do
294+
expect { delete_welsh_translation_input.submit_without_confirm }.to change { condition.reload.exit_page_markdown_cy }.to(nil)
295+
.and change { condition.reload.exit_page_heading_cy }.to(nil)
296+
end
297+
298+
it "resets the available_languages field to only include English" do
299+
expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.available_languages }.to(%w[en])
300+
end
301+
302+
it "resets the welsh_completed status" do
303+
expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.welsh_completed }.to(false)
304+
end
305+
306+
context "when the form is live" do
307+
let(:form) do
308+
create :form,
309+
:live,
310+
name_cy: "New Welsh name",
311+
what_happens_next_markdown: "English what happens next",
312+
what_happens_next_markdown_cy: "New Welsh what happens next",
313+
declaration_text: "English declaration",
314+
declaration_text_cy: "New Welsh declaration",
315+
support_email: "english-support@example.gov.uk",
316+
support_email_cy: "new-welsh-support@example.gov.uk",
317+
support_phone: "English support phone",
318+
support_phone_cy: "0800 123 4567",
319+
support_url: "https://www.gov.uk/english-support",
320+
support_url_cy: "https://www.gov.uk/new-welsh-support",
321+
support_url_text: "English support url text",
322+
support_url_text_cy: "New Welsh Support",
323+
privacy_policy_url: "https://www.gov.uk/english-privacy",
324+
privacy_policy_url_cy: "https://www.gov.uk/new-welsh-privacy",
325+
payment_url_cy: "https://www.gov.uk/payments/new-welsh-payment-link",
326+
available_languages: %w[en cy],
327+
welsh_completed: true,
328+
pages: [page]
329+
end
330+
331+
it "sets the form_status to `live_with_draft`" do
332+
expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.state }.to("live_with_draft")
333+
end
334+
end
335+
336+
context "when the form is archived" do
337+
let(:form) do
338+
create :form,
339+
:archived,
340+
name_cy: "New Welsh name",
341+
what_happens_next_markdown: "English what happens next",
342+
what_happens_next_markdown_cy: "New Welsh what happens next",
343+
declaration_text: "English declaration",
344+
declaration_text_cy: "New Welsh declaration",
345+
support_email: "english-support@example.gov.uk",
346+
support_email_cy: "new-welsh-support@example.gov.uk",
347+
support_phone: "English support phone",
348+
support_phone_cy: "0800 123 4567",
349+
support_url: "https://www.gov.uk/english-support",
350+
support_url_cy: "https://www.gov.uk/new-welsh-support",
351+
support_url_text: "English support url text",
352+
support_url_text_cy: "New Welsh Support",
353+
privacy_policy_url: "https://www.gov.uk/english-privacy",
354+
privacy_policy_url_cy: "https://www.gov.uk/new-welsh-privacy",
355+
payment_url_cy: "https://www.gov.uk/payments/new-welsh-payment-link",
356+
available_languages: %w[en cy],
357+
welsh_completed: true,
358+
pages: [page]
359+
end
360+
361+
it "sets the form_status to `archived_with_draft`" do
362+
expect { delete_welsh_translation_input.submit_without_confirm }.to change { form.reload.state }.to("archived_with_draft")
363+
end
364+
end
365+
end
366+
end
367+
218368
describe "#confirmed?" do
219369
context "when the input is invalid" do
220370
it "returns false" do

spec/input_objects/forms/welsh_translation_input_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ def build_form(attributes = {})
5656
build(:form, default_attributes.merge(attributes))
5757
end
5858

59+
def build_empty_welsh_form
60+
empty_attributes = {
61+
id: 1,
62+
name: "Apply for a juggling licence",
63+
what_happens_next_markdown: "English what happens next",
64+
welsh_completed: false,
65+
what_happens_next_markdown_cy: "",
66+
declaration_text: "English declaration",
67+
declaration_text_cy: "",
68+
support_email: "english-support@example.gov.uk",
69+
support_email_cy: "",
70+
support_phone: "01234 987654",
71+
support_phone_cy: "",
72+
support_url_cy: "",
73+
support_url: "https://www.gov.uk/english-support",
74+
support_url_text_cy: "",
75+
support_url_text: "English Support",
76+
privacy_policy_url_cy: "",
77+
payment_url: "https://www.gov.uk/english-payment",
78+
payment_url_cy: "",
79+
}
80+
build(:form, empty_attributes)
81+
end
82+
5983
describe "validations" do
6084
it "is not valid if mark complete is blank" do
6185
form = OpenStruct.new(welsh_completed: false, name: "Apply for a juggling licence")
@@ -657,4 +681,23 @@ def build_form(attributes = {})
657681
expect(welsh_translation_input.mark_complete).to eq(form.welsh_completed)
658682
end
659683
end
684+
685+
describe "#all_fields_empty?" do
686+
context "when the welsh translation fields are not empty" do
687+
let(:form) { build_form }
688+
689+
it "returns false" do
690+
expect(welsh_translation_input.all_fields_empty?).to be false
691+
end
692+
end
693+
694+
context "when the welsh translation fields are all empty" do
695+
let(:form) { build_empty_welsh_form }
696+
697+
it "returns true" do
698+
welsh_translation_input = described_class.new(form:)
699+
expect(welsh_translation_input.all_fields_empty?).to be true
700+
end
701+
end
702+
end
660703
end

spec/requests/forms/welsh_translation_controller_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@
9494
end
9595
end
9696

97+
context "when 'Yes' is selected and all fields are empty" do
98+
let(:page_translations_attributes) { { "0" => { "id" => form.pages.first.id, question_text_cy: "", condition_translations_attributes: } } }
99+
let(:params) { { forms_welsh_translation_input: { form:, mark_complete:, name_cy: "", privacy_policy_url_cy: "", page_translations_attributes: } } }
100+
101+
it "deletes the form" do
102+
post(welsh_translation_create_path(id), params:)
103+
expect(form.pages.first.reload.question_text_cy).to be_nil
104+
expect(condition.reload.exit_page_heading_cy).to be_nil
105+
end
106+
107+
it "redirects to the form with a success banner" do
108+
post(welsh_translation_create_path(id), params:)
109+
expect(response).to redirect_to(form_path(id))
110+
expect(flash[:success]).to eq(I18n.t("forms.welsh_translation.destroy.success"))
111+
end
112+
end
113+
97114
context "when the user is not authorized" do
98115
let(:current_user) { build :user }
99116

0 commit comments

Comments
 (0)