diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index a502e5fe3..6e16a0476 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -40,10 +40,26 @@ private relationship = record.public_send("relat#{person_num}") return unless age && economic_status && relationship - if age >= 16 && age <= 19 && person_is_fulltime_student?(economic_status) && !person_is_child?(relationship) - record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19") - record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19") - record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19") + age_between_16_19 = age.between?(16, 19) + student = person_is_fulltime_student?(economic_status) + child = person_is_child?(relationship) + + if age_between_16_19 && student && !child + record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.student_not_child") + record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.16_19_not_child") + record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child") + end + + if age_between_16_19 && !student && child + record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student") + record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student") + record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student") + end + + if !age_between_16_19 && student && child + record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.must_be_16_19") + record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19") + record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19") end end diff --git a/config/locales/en.yml b/config/locales/en.yml index d67864d6d..446e1343e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -297,7 +297,11 @@ en: child_over_16: "Answer cannot be over 16 as person’s %{person_num} working situation is ‘child under 16‘" child_over_20: "Answer cannot be 20 or over as the relationship is ‘child’" not_student_16_19: "Answer cannot be between 16 and 19 as person %{person_num} is a child of the lead tenant but is not a full-time student" - student_16_19: "Person cannot be aged 16-19 if they are a student but don't have relationship ‘child’" + student_16_19: + cannot_be_16_19: + student_not_child: "Person cannot be aged 16-19 if they are a student but don't have relationship ‘child’" + child_not_student: "Person cannot be aged 16-19 if they have relationship ‘child’ but are not a student" + must_be_16_19: "Person must be aged 16-19 if they are a student and have relationship ‘child’" partner_under_16: "Cannot be under 16 if the relationship is partner" lead: over_20: "The lead tenant must be under 20 as you told us their housing situation immediately before this letting was a children’s home or foster care" @@ -306,7 +310,11 @@ en: child_under_16: "Person’s %{person_num} working situation must be ’child under 16‘ as you told us they’re under 16" child_over_16: "Answer cannot be ‘child under 16’ as you told us the person %{person_num} is older than 16" not_student_16_19: "Person’s %{person_num} working situation must be full-time student or prefers not to say as you told us they’re between 16 and 19." - student_16_19: "Person cannot be a student if they are aged 16-19 and but don't have relationship ‘child’" + student_16_19: + cannot_be_student: + child_not_16_19: "Person cannot be a student if they are not aged 16-19 but have relationship ‘child’" + 16_19_not_child: "Person cannot be a student if they are aged 16-19 but don‘t have relationship ‘child’" + must_be_student: "Person must be a student if they are aged 16-19 and have relationship ‘child’" retired_male: "Answer cannot be ‘retired’ as the male tenant is under 65" retired_female: "Answer cannot be ‘retired’ as the female tenant is under 60" relat: @@ -315,7 +323,11 @@ en: child_over_20: "Answer cannot be ‘child’ if the person's age is 20 or over" one_partner: "Number of partners cannot be greater than 1" not_student_16_19: "Answer cannot be ‘child’ as you told us the person %{person_num} is between 16 and 19 and is not a full-time student" - student_16_19: "Answer must be ‘child’ if the person is aged 16-19 and a student" + student_16_19: + cannot_be_child: + student_not_16_19: "Answer cannot be ‘child’ if the person is a student but not aged 16-19" + 16_19_not_student: "Answer cannot be ‘child’ if the person is aged 16-19 but not a student" + must_be_child: "Answer must be ‘child’ if the person is aged 16-19 and a student" housingneeds_a: one_or_two_choices: "You can only select one option or ‘other disabled access needs’ plus ‘wheelchair-accessible housing’, ‘wheelchair access to essential rooms’ or ‘level access housing’" prevten: diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 4cc6de48c..bdfb01ea4 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -126,17 +126,43 @@ RSpec.describe Validations::Sales::HouseholdValidations do .to include(match I18n.t("validations.household.age.child_over_20")) end - it "validates that a person aged 16-19 who is a student must be a child of the buyer" do + it "adds errors for a person aged 16-19 who is a student but not a child of the buyer" do record.age2 = 18 record.ecstat2 = "7" record.relat2 = "P" household_validator.validate_household_number_of_other_members(record) expect(record.errors["relat2"]) - .to include(match I18n.t("validations.household.relat.student_16_19")) + .to include(match I18n.t("validations.household.relat.student_16_19.must_be_child")) expect(record.errors["age2"]) - .to include(match I18n.t("validations.household.age.student_16_19")) + .to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.student_not_child")) expect(record.errors["ecstat2"]) - .to include(match I18n.t("validations.household.ecstat.student_16_19")) + .to include(match I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.16_19_not_child")) + end + + it "adds errors for a person aged 16-19 who is a child of the buyer but not a student" do + record.age2 = 17 + record.ecstat2 = "1" + record.relat2 = "C" + household_validator.validate_household_number_of_other_members(record) + expect(record.errors["relat2"]) + .to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.16_19_not_student")) + expect(record.errors["age2"]) + .to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student")) + expect(record.errors["ecstat2"]) + .to include(match I18n.t("validations.household.ecstat.student_16_19.must_be_student")) + end + + it "adds errors for a person who is a child of the buyer and a student but not aged 16-19" do + record.age2 = 14 + record.ecstat2 = "7" + record.relat2 = "C" + household_validator.validate_household_number_of_other_members(record) + expect(record.errors["relat2"]) + .to include(match I18n.t("validations.household.relat.student_16_19.cannot_be_child.student_not_16_19")) + expect(record.errors["age2"]) + .to include(match I18n.t("validations.household.age.student_16_19.must_be_16_19")) + expect(record.errors["ecstat2"]) + .to include(match I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")) end end end