Skip to content

Commit 62693b3

Browse files
authored
Merge pull request #2667 from govuk-forms/fix-welsh-deletion
Fix welsh deletion
2 parents b56c652 + 4c48883 commit 62693b3

10 files changed

+161
-1
lines changed

app/controllers/forms/welsh_translation_controller.rb

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

16-
if @welsh_translation_input.all_fields_empty?
16+
if @welsh_translation_input.blanked?
1717
# if all fields are empty we delete the welsh translation
1818
@delete_welsh_translation_input = Forms::DeleteWelshTranslationInput.new(form: current_form)
1919
if @delete_welsh_translation_input.submit_without_confirm

app/input_objects/forms/welsh_condition_translation_input.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,8 @@ def exit_page_markdown_cy_length_and_tags
8383
def page
8484
@page ||= condition.routing_page
8585
end
86+
87+
def all_fields_empty?
88+
attributes.except!("id").values.all?(&:blank?)
89+
end
8690
end

app/input_objects/forms/welsh_page_translation_input.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,12 @@ def welsh_answer_settings
261261

262262
answer_settings_cloned
263263
end
264+
265+
def all_fields_empty?
266+
attributes.except!("id").values.all?(&:blank?)
267+
end
268+
269+
def blanked?
270+
all_fields_empty? && condition_translations.all?(&:all_fields_empty?) && selection_options_cy.all?(&:all_fields_empty?)
271+
end
264272
end

app/input_objects/forms/welsh_selection_option_translation_input.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def question_number
4242
page.position
4343
end
4444

45+
def all_fields_empty?
46+
name_cy.blank?
47+
end
48+
4549
private
4650

4751
def name_present?

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 blanked?
127+
all_fields_empty? && page_translations.all?(&:blanked?)
128+
end
129+
126130
def all_fields_empty?
127131
attributes.except!("mark_complete").values.all?(&:blank?)
128132
end

spec/input_objects/forms/welsh_condition_translation_input_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,20 @@ def create_condition(attributes = {})
222222
expect(welsh_condition_translation_input.form_field_id(:exit_page_heading_cy)).to eq "forms_welsh_condition_translation_input_#{condition.id}_condition_translations_exit_page_heading_cy"
223223
end
224224
end
225+
226+
describe "#all_fields_empty?" do
227+
context "when the welsh condition fields are not empty" do
228+
it "returns false" do
229+
expect(welsh_condition_translation_input.all_fields_empty?).to be false
230+
end
231+
end
232+
233+
context "when the welsh condition fields are all empty" do
234+
let(:new_input_data) { { condition:, exit_page_markdown_cy: "", exit_page_heading_cy: "" } }
235+
236+
it "returns true" do
237+
expect(welsh_condition_translation_input.all_fields_empty?).to be true
238+
end
239+
end
240+
end
225241
end

spec/input_objects/forms/welsh_page_translation_input_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,79 @@ def create_page(attributes = {})
632632
end
633633
end
634634
end
635+
636+
describe "#all_fields_empty?" do
637+
context "when the welsh page fields are not empty" do
638+
it "returns false" do
639+
expect(welsh_page_translation_input.all_fields_empty?).to be false
640+
end
641+
end
642+
643+
context "when the welsh page fields are all empty" do
644+
let(:new_input_data) { { page:, question_text_cy: "", hint_text_cy: "", page_heading_cy: "", guidance_markdown_cy: "" } }
645+
646+
it "returns true" do
647+
expect(welsh_page_translation_input.all_fields_empty?).to be true
648+
end
649+
end
650+
end
651+
652+
describe "#blanked?" do
653+
let(:page) do
654+
create_page(answer_type: "selection",
655+
answer_settings: { only_one_option: "true", selection_options: [{ name: "Option 1", value: "Option 1" }, { name: "Option 2", value: "Option 2" }] })
656+
end
657+
let(:new_input_data) do
658+
super().merge(
659+
question_text_cy: "",
660+
hint_text_cy: "",
661+
page_heading_cy: "",
662+
guidance_markdown_cy: "",
663+
condition_translations: [condition_translation],
664+
selection_options_cy_attributes: {
665+
"0" => { "id" => "0", "name_cy" => "" },
666+
"1" => { "id" => "1", "name_cy" => "" },
667+
},
668+
)
669+
end
670+
let(:condition_translation) { Forms::WelshConditionTranslationInput.new(condition: condition, exit_page_heading_cy: "", exit_page_markdown_cy: "") }
671+
let(:selection_options_cy) { welsh_page_translation_input.selection_options_cy.map(&:as_selection_option) }
672+
673+
it "returns true when all fields are blank" do
674+
expect(welsh_page_translation_input.blanked?).to be true
675+
end
676+
677+
context "when the welsh page fields are not all blank" do
678+
let(:new_input_data) { super().merge(question_text_cy: "New question text") }
679+
680+
it "returns false" do
681+
expect(welsh_page_translation_input.blanked?).to be false
682+
end
683+
end
684+
685+
context "when the condition is not blank" do
686+
let(:condition_translation) { Forms::WelshConditionTranslationInput.new(condition: condition, exit_page_heading_cy: "Heading", exit_page_markdown_cy: "markdown") }
687+
688+
it "returns false" do
689+
expect(welsh_page_translation_input.blanked?).to be false
690+
end
691+
end
692+
693+
context "when the selection optiosn are not all blank" do
694+
let(:new_input_data) do
695+
super().merge(
696+
question_text_cy: "New question text",
697+
condition_translations: [],
698+
selection_options_cy_attributes: {
699+
"0" => { "id" => "0", "name_cy" => "welsh option 1" },
700+
"1" => { "id" => "1", "name_cy" => "" },
701+
},
702+
)
703+
end
704+
705+
it "returns false" do
706+
expect(welsh_page_translation_input.blanked?).to be false
707+
end
708+
end
709+
end
635710
end

spec/input_objects/forms/welsh_selection_option_translation_input_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,18 @@
9393
end
9494
end
9595
end
96+
97+
describe "#all_fields_empty?" do
98+
it "returns false when fields are not empty" do
99+
expect(welsh_selection_option_translation_input.all_fields_empty?).to be false
100+
end
101+
102+
context "when the welsh selection option fields are all empty" do
103+
let(:new_input_data) { { selection_option: selection_option_cy, page:, id: "1", name_cy: "" } }
104+
105+
it "returns true" do
106+
expect(welsh_selection_option_translation_input.all_fields_empty?).to be true
107+
end
108+
end
109+
end
96110
end

spec/input_objects/forms/welsh_translation_input_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,4 +700,38 @@ def build_empty_welsh_form
700700
end
701701
end
702702
end
703+
704+
describe "#blanked?" do
705+
let(:page_translation) { Forms::WelshPageTranslationInput.new(page:) }
706+
let(:new_input_data) do
707+
{
708+
form:,
709+
mark_complete:,
710+
name_cy: "",
711+
what_happens_next_markdown_cy: "",
712+
declaration_markdown_cy: "",
713+
support_email_cy: "",
714+
support_phone_cy: "",
715+
support_url_cy: "",
716+
support_url_text_cy: "",
717+
privacy_policy_url_cy: "",
718+
payment_url_cy: "",
719+
page_translations: [page_translation],
720+
}
721+
end
722+
723+
context "when the welsh translation fields are all blank" do
724+
it "returns true" do
725+
expect(welsh_translation_input.blanked?).to be true
726+
end
727+
end
728+
729+
context "when the pages are not all blank" do
730+
let(:page_translation) { Forms::WelshPageTranslationInput.new(page:, question_text_cy: "Some question text") }
731+
732+
it "returns false" do
733+
expect(welsh_translation_input.blanked?).to be false
734+
end
735+
end
736+
end
703737
end

spec/requests/forms/welsh_translation_controller_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
end
9696

9797
context "when 'Yes' is selected and all fields are empty" do
98+
let(:condition_translations_attributes) { { "0" => { "id" => condition.id, exit_page_heading_cy: "", exit_page_markdown_cy: "" } } }
9899
let(:page_translations_attributes) { { "0" => { "id" => form.pages.first.id, question_text_cy: "", condition_translations_attributes: } } }
99100
let(:params) { { forms_welsh_translation_input: { form:, mark_complete:, name_cy: "", privacy_policy_url_cy: "", page_translations_attributes: } } }
100101

0 commit comments

Comments
 (0)