diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index b43650bfc..b9aa66724 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -833,8 +833,10 @@ private next if question.completed?(log) question.page.interruption_screen_question_ids.each do |interruption_screen_question_id| + next if log.form.questions.none? { |q| q.id == interruption_screen_question_id && q.page.routed_to?(log, nil) } + field_mapping_for_errors[interruption_screen_question_id.to_sym]&.each do |field| - unless errors.any? { |e| e.options[:category] == :soft_validation && field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } + if errors.none? { |e| e.options[:category] == :soft_validation && field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } error_message = [display_title_text(question.page.title_text, log), display_informative_text(question.page.informative_text, log)].reject(&:empty?).join(". ") errors.add(field, message: error_message, category: :soft_validation) end diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 0fe2d7238..99cd5aaed 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -540,8 +540,10 @@ private next if question.completed?(log) question.page.interruption_screen_question_ids.each do |interruption_screen_question_id| + next if log.form.questions.none? { |q| q.id == interruption_screen_question_id && q.page.routed_to?(log, nil) } + field_mapping_for_errors[interruption_screen_question_id.to_sym]&.each do |field| - unless errors.any? { |e| field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } + if errors.none? { |e| field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } error_message = [display_title_text(question.page.title_text, log), display_informative_text(question.page.informative_text, log)].reject(&:empty?).join(". ") errors.add(field, message: error_message, category: :soft_validation) end diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb index 1acb9ddf2..77dadbc0b 100644 --- a/app/services/bulk_upload/sales/year2022/row_parser.rb +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -1118,8 +1118,10 @@ private next if question.completed?(log) question.page.interruption_screen_question_ids.each do |interruption_screen_question_id| + next if log.form.questions.none? { |q| q.id == interruption_screen_question_id && q.page.routed_to?(log, nil) } + field_mapping_for_errors[interruption_screen_question_id.to_sym]&.each do |field| - unless errors.any? { |e| e.options[:category] == :soft_validation && field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } + if errors.none? { |e| e.options[:category] == :soft_validation && field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } error_message = [display_title_text(question.page.title_text, log), display_informative_text(question.page.informative_text, log)].reject(&:empty?).join(". ") errors.add(field, message: error_message, category: :soft_validation) end diff --git a/app/services/bulk_upload/sales/year2023/row_parser.rb b/app/services/bulk_upload/sales/year2023/row_parser.rb index 3a9f7ed41..da03cf32e 100644 --- a/app/services/bulk_upload/sales/year2023/row_parser.rb +++ b/app/services/bulk_upload/sales/year2023/row_parser.rb @@ -1293,8 +1293,10 @@ private next if question.completed?(log) question.page.interruption_screen_question_ids.each do |interruption_screen_question_id| + next if log.form.questions.none? { |q| q.id == interruption_screen_question_id && q.page.routed_to?(log, nil) } + field_mapping_for_errors[interruption_screen_question_id.to_sym]&.each do |field| - unless errors.any? { |e| e.options[:category] == :soft_validation && field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } + if errors.none? { |e| e.options[:category] == :soft_validation && field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } error_message = [display_title_text(question.page.title_text, log), display_informative_text(question.page.informative_text, log)].reject(&:empty?).join(". ") errors.add(field, message: error_message, category: :soft_validation) end diff --git a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index 041a9265a..9efa4c5a0 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -1048,11 +1048,23 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do end end + context "when a soft validation is triggered that relates both to fields that are and are not routed to" do + let(:attributes) { setup_section_params.merge({ field_47: "1", field_20: "M", field_21: "M" }) } + + it "adds errors to fields that are routed to" do + expect(parser.errors.where(:field_20, category: :soft_validation)).to be_present + expect(parser.errors.where(:field_21, category: :soft_validation)).to be_present + end + + it "does not add errors to fields that are not routed to" do + expect(parser.errors.where(:field_22, category: :soft_validation)).not_to be_present + expect(parser.errors.where(:field_23, category: :soft_validation)).not_to be_present + end + end + context "when soft validation is triggered and the mappings for errors are not defined" do let(:attributes) { setup_section_params.merge({ field_12: 22, field_35: 5 }) } - # rubocop:disable RSpec/AnyInstance - let(:mock_interruption_ids) { allow_any_instance_of(Form::Page).to receive(:interruption_screen_question_ids).and_return(%w[fake_question_id]) } - # rubocop:enable RSpec/AnyInstance + let(:mock_interruption_ids) { allow_any_instance_of(Form::Page).to receive(:interruption_screen_question_ids).and_return(%w[fake_question_id]) } # rubocop:disable RSpec/AnyInstance it "does not crash" do expect(parser.errors).to be_present diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index d8df99a2e..4df2757cd 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -49,8 +49,6 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do FormHandler.instance.use_real_forms! example.run - - FormHandler.instance.use_fake_forms! end describe "#blank_row?" do @@ -1077,6 +1075,20 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end end + context "when a soft validation is triggered that relates both to fields that are and are not routed to" do + let(:attributes) { setup_section_params.merge({ field_82: "1", field_47: "M", field_53: "M", field_57: "M" }) } + + it "adds errors to fields that are routed to" do + expect(parser.errors.where(:field_53, category: :soft_validation)).to be_present + expect(parser.errors.where(:field_57, category: :soft_validation)).to be_present + end + + it "does not add errors to fields that are not routed to" do + expect(parser.errors.where(:field_61, category: :soft_validation)).not_to be_present + expect(parser.errors.where(:field_65, category: :soft_validation)).not_to be_present + end + end + context "when soft validation is triggered and not required" do let(:attributes) { setup_section_params.merge({ field_128: 120, field_126: 1, field_32: 1, field_4: 1, field_5: "3", field_25: "E09000008" }) } diff --git a/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb index f14ec9d6c..37e68b26c 100644 --- a/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb @@ -112,8 +112,6 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do Singleton.__init__(FormHandler) example.run end - Timecop.return - Singleton.__init__(FormHandler) end describe "#blank_row?" do @@ -736,6 +734,19 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do expect(parser.errors.where(:field_24, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") end end + + context "when a soft validation is triggered that relates both to fields that are and are not routed to" do + let(:attributes) { valid_attributes.merge({ field_123: "2" }) } + + it "adds errors to fields that are routed to" do + expect(parser.errors.where(:field_123, category: :soft_validation)).to be_present + end + + it "does not add errors to fields that are not routed to" do + expect(parser.errors.where(:field_73, category: :soft_validation)).not_to be_present + expect(parser.errors.where(:field_70, category: :soft_validation)).not_to be_present + end + end end context "when the log already exists in the db" do diff --git a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb index a04b9822d..cddc042c6 100644 --- a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb @@ -115,8 +115,6 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do Singleton.__init__(FormHandler) example.run end - Timecop.return - Singleton.__init__(FormHandler) end describe "#blank_row?" do @@ -884,6 +882,18 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do expect(parser.errors.where(:field_30, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") end end + + context "when a soft validation is triggered that relates both to fields that are and are not routed to" do + let(:attributes) { valid_attributes.merge({ field_103: "300000" }) } + + it "adds errors to fields that are routed to" do + expect(parser.errors.where(:field_103, category: :soft_validation)).to be_present + end + + it "does not add errors to fields that are not routed to" do + expect(parser.errors.where(:field_112, category: :soft_validation)).not_to be_present + end + end end end