diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 1f6936520..1672daf44 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -107,9 +107,13 @@ private day = params["case_log"]["#{question_key}(3i)"] month = params["case_log"]["#{question_key}(2i)"] year = params["case_log"]["#{question_key}(1i)"] - next unless day.present? && month.present? && year.present? + next unless [day, month, year].any?(&:present?) - result[question_key] = Date.new(year.to_i, month.to_i, day.to_i) + result[question_key] = if day.to_i.between?(1, 31) && month.to_i.between?(1, 12) && year.to_i.between?(2000, 2200) + Date.new(year.to_i, month.to_i, day.to_i) + else + Date.new(0, 1, 1) + end end next unless question_params diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 5b5dc6f40..2134a766a 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -5,6 +5,7 @@ class CaseLogValidator < ActiveModel::Validator include PropertyValidations include FinancialValidations include TenancyValidations + include DateValidations def validate(record) # If we've come from the form UI we only want to validate the specific fields diff --git a/app/validations/date_validations.rb b/app/validations/date_validations.rb new file mode 100644 index 000000000..b44cb8d53 --- /dev/null +++ b/app/validations/date_validations.rb @@ -0,0 +1,21 @@ +module DateValidations + def validate_property_major_repairs(record) + date_valid?("mrcdate", record) + end + + def validate_tenancy_start_date(record) + date_valid?("startdate", record) + end + + def validate_sale_completion_date(record) + date_valid?("sale_completion_date", record) + end + +private + + def date_valid?(question, record) + if record[question].is_a?(ActiveSupport::TimeWithZone) && record[question].year.zero? + record.errors.add question, "Please enter a valid date" + end + end +end diff --git a/spec/controllers/case_logs_controller_spec.rb b/spec/controllers/case_logs_controller_spec.rb index 1b4c1fcf6..8a99caae0 100644 --- a/spec/controllers/case_logs_controller_spec.rb +++ b/spec/controllers/case_logs_controller_spec.rb @@ -184,7 +184,7 @@ RSpec.describe CaseLogsController, type: :controller do "mrcdate(1i)": "2021", "mrcdate(2i)": "05", "mrcdate(3i)": "04", - page: "major_repairs_date", + page: "property_major_repairs", } end it "saves full and partial dates" do diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index 6db2cbe6a..079c3bc55 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -489,4 +489,56 @@ RSpec.describe "Test Features" do expect(page).to have_current_path("/case_logs/#{id}/rent") end end + + describe "date validation", js: true do + def fill_in_date(case_log_id, question, day, month, year, path) + visit("/case_logs/#{case_log_id}/#{path}") + fill_in("#{question}_1i", with: year) + fill_in("#{question}_2i", with: month) + fill_in("#{question}_3i", with: day) + end + it "does not allow out of range dates to be submitted" do + fill_in_date(id, "case_log_mrcdate", 3100, 12, 2000, "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs") + + fill_in_date(id, "case_log_mrcdate", 12, 1, 20_000, "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs") + + fill_in_date(id, "case_log_mrcdate", 13, 100, 2020, "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs") + + fill_in_date(id, "case_log_mrcdate", 21, 11, 2020, "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/local_authority/check_answers") + end + + it "does not allow non numeric inputs to be submitted" do + fill_in_date(id, "case_log_mrcdate", "abc", "de", "ff", "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs") + end + + it "does not allow partial inputs to be submitted" do + fill_in_date(id, "case_log_mrcdate", 21, 12, nil, "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs") + + fill_in_date(id, "case_log_mrcdate", 12, nil, 2000, "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs") + + fill_in_date(id, "case_log_mrcdate", nil, 10, 2020, "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs") + end + + it "allows valid inputs to be submitted" do + fill_in_date(id, "case_log_mrcdate", 21, 11, 2020, "property_major_repairs") + click_button("Save and continue") + expect(page).to have_current_path("/case_logs/#{id}/local_authority/check_answers") + end + end end diff --git a/spec/fixtures/forms/test_form.json b/spec/fixtures/forms/test_form.json index 28feb44b9..4ac56645f 100644 --- a/spec/fixtures/forms/test_form.json +++ b/spec/fixtures/forms/test_form.json @@ -511,7 +511,7 @@ } } }, - "major_repairs_date": { + "property_major_repairs": { "questions": { "mrcdate": { "check_answer_label": "What was the major repairs completion date?", @@ -545,4 +545,4 @@ } } } - } \ No newline at end of file + }