From 6873221cb80c82563d0c03a788b4d11704c076d5 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Wed, 19 Apr 2023 09:15:11 +0100 Subject: [PATCH] add a validation to prevent an inconsistent combination of values and tests for this validation --- .../sales/household_validations.rb | 12 ++++ config/locales/en.yml | 2 + .../sales/household_validations_spec.rb | 56 +++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index 5d4552eb3..dafd947f5 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -20,6 +20,18 @@ module Validations::Sales::HouseholdValidations end end + def validate_buyers_living_in_property(record) + return unless record.form.start_date.year >= 2023 + + if record.buyers_will_live_in? && + record.joint_purchase? && + record.buyer_one_will_not_live_in_property? && + record.buyer_two_will_not_live_in_property? + record.errors.add :buy1livein, I18n.t("validations.household.buyers_will_live_in_property.buyers_live_but_no_buyers_live") + record.errors.add :buy2livein, I18n.t("validations.household.buyers_will_live_in_property.buyers_live_but_no_buyers_live") + end + end + private def validate_person_age_matches_relationship(record, person_num) diff --git a/config/locales/en.yml b/config/locales/en.yml index ff409f2e0..7dd19188c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -442,6 +442,8 @@ en: no_choices: "You cannot answer this question as you told us nobody in the household has a physical or mental health condition (or other illness) expected to last 12 months or more" postcode: discounted_ownership: "Last settled accommodation and discounted ownership property postcodes must match" + buyers_will_live_in_property: + buyers_live_but_no_buyers_live: "You have already told us that the buyers will live in the property. Either buyer 1 or buyer 2 must live in the property" tenancy: length: diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 953f27c1d..3f412efaf 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -201,4 +201,60 @@ RSpec.describe Validations::Sales::HouseholdValidations do end end end + + describe "validating fields about buyers living in the property" do + let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } + + context "when buyers will live in the property and the sale is a joint purchase" do + let(:buylivein) { 1 } + let(:jointpur) { 1 } + let(:jointmore) { 2 } + + context "and buyer one will live in the property" do + let(:buy1livein) { 1 } + + it "does not add validations regardless of whether buyer two will live in the property" do + sales_log.buy2livein = 1 + household_validator.validate_buyers_living_in_property(sales_log) + expect(sales_log.errors).to be_empty + sales_log.buy2livein = 2 + household_validator.validate_buyers_living_in_property(sales_log) + expect(sales_log.errors).to be_empty + end + end + + context "and buyer one will not live in the property" do + let(:buy1livein) { 2 } + + it "does not add validations if buyer two will live in the property or if we do not yet know" do + sales_log.buy2livein = 1 + household_validator.validate_buyers_living_in_property(sales_log) + expect(sales_log.errors).to be_empty + sales_log.buy2livein = nil + household_validator.validate_buyers_living_in_property(sales_log) + expect(sales_log.errors).to be_empty + end + + it "triggers a validation if buyer two will also not live in the property" do + sales_log.buy2livein = 2 + household_validator.validate_buyers_living_in_property(sales_log) + expect(sales_log.errors[:buy2livein]).to include I18n.t("validations.household.buyers_will_live_in_property.buyers_live_but_no_buyers_live") + expect(sales_log.errors[:buy1livein]).to include I18n.t("validations.household.buyers_will_live_in_property.buyers_live_but_no_buyers_live") + end + end + + context "and we don't know whether buyer one will live in the property" do + let(:buy1livein) { nil } + + it "does not add validations regardless of whether buyer two will live in the property" do + sales_log.buy2livein = 1 + household_validator.validate_buyers_living_in_property(sales_log) + expect(sales_log.errors).to be_empty + sales_log.buy2livein = 2 + household_validator.validate_buyers_living_in_property(sales_log) + expect(sales_log.errors).to be_empty + end + end + end + end end