From ff788afcb5f91328b44d6b1d7e781e586c984b5c Mon Sep 17 00:00:00 2001 From: David May-Miller Date: Tue, 8 Nov 2022 16:33:48 +0000 Subject: [PATCH] CLDC-1698 Update tenancy length validations (#975) --- app/models/validations/tenancy_validations.rb | 16 +++++-- config/locales/en.yml | 4 +- .../validations/tenancy_validations_spec.rb | 48 +++++++++++++------ 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/app/models/validations/tenancy_validations.rb b/app/models/validations/tenancy_validations.rb index 25e09f205..e2ea108cf 100644 --- a/app/models/validations/tenancy_validations.rb +++ b/app/models/validations/tenancy_validations.rb @@ -5,7 +5,7 @@ module Validations::TenancyValidations def validate_fixed_term_tenancy(record) is_present = record.tenancylength.present? - is_in_range = record.tenancylength.to_i.between?(2, 99) + is_in_range = record.tenancylength.to_i.between?(min_tenancy_length(record), 99) conditions = [ { condition: !(record.is_secure_tenancy? || record.is_assured_shorthold_tenancy?) && is_present, @@ -13,11 +13,17 @@ module Validations::TenancyValidations }, { condition: (record.is_assured_shorthold_tenancy? && !is_in_range) && is_present, - error: I18n.t("validations.tenancy.length.shorthold"), + error: I18n.t( + "validations.tenancy.length.shorthold", + min_tenancy_length: min_tenancy_length(record), + ), }, { condition: record.is_secure_tenancy? && (!is_in_range && is_present), - error: I18n.t("validations.tenancy.length.secure"), + error: I18n.t( + "validations.tenancy.length.secure", + min_tenancy_length: min_tenancy_length(record), + ), }, ] @@ -41,4 +47,8 @@ module Validations::TenancyValidations record.errors.add :hhmemb, I18n.t("validations.tenancy.joint_more_than_one_member") end end + + def min_tenancy_length(record) + record.is_supported_housing? || record.renttype == 3 ? 1 : 2 + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 003f573dc..55ea3a0f7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -301,8 +301,8 @@ en: tenancy: length: fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term" - shorthold: "Enter a tenancy length between 2 and 99 years for a Fixed Term – Assured Shorthold Tenancy (AST)" - secure: "Enter a tenancy length between 2 and 99 years (or don't specify the length) for a Secure (including flexible) tenancy" + shorthold: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type" + secure: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type" internal_transfer: "Answer must be secure tenancy as this tenancy is an internal transfer" cannot_be_internal_transfer: "Answer cannot be internal transfer as this is not a secure tenancy" not_joint: "This cannot be a joint tenancy as you've told us there's only one person in the household" diff --git a/spec/models/validations/tenancy_validations_spec.rb b/spec/models/validations/tenancy_validations_spec.rb index ccb5503d0..2d008e6e3 100644 --- a/spec/models/validations/tenancy_validations_spec.rb +++ b/spec/models/validations/tenancy_validations_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Validations::TenancyValidations do subject(:tenancy_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::TenancyValidations } } - let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2021, 5, 1)) } + let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2021, 5, 1), needstype: 1, rent_type: 1) } describe "fixed term tenancy validations" do context "when fixed term tenancy" do @@ -21,11 +21,16 @@ RSpec.describe Validations::TenancyValidations do end context "when type of tenancy is assured shorthold" do - let(:expected_error) { I18n.t("validations.tenancy.length.shorthold") } + let(:expected_error) do + I18n.t( + "validations.tenancy.length.shorthold", + min_tenancy_length: 2, + ) + end before { record.tenancy = 4 } - context "when tenancy length is greater than 1" do + context "when tenancy length is less than 2" do it "adds an error" do record.tenancylength = 1 tenancy_validator.validate_fixed_term_tenancy(record) @@ -34,7 +39,7 @@ RSpec.describe Validations::TenancyValidations do end end - context "when tenancy length is less than 100" do + context "when tenancy length is greater than 99" do it "adds an error" do record.tenancylength = 100 tenancy_validator.validate_fixed_term_tenancy(record) @@ -64,11 +69,16 @@ RSpec.describe Validations::TenancyValidations do context "when the collection start year is before 2022" do context "when type of tenancy is secure" do - let(:expected_error) { I18n.t("validations.tenancy.length.secure") } + let(:expected_error) do + I18n.t( + "validations.tenancy.length.secure", + min_tenancy_length: 2, + ) + end before { record.tenancy = 1 } - context "when tenancy length is greater than 1" do + context "when tenancy length is less than 2" do it "adds an error" do record.tenancylength = 1 tenancy_validator.validate_fixed_term_tenancy(record) @@ -77,7 +87,7 @@ RSpec.describe Validations::TenancyValidations do end end - context "when tenancy length is less than 100" do + context "when tenancy length is greater than 99" do it "adds an error" do record.tenancylength = 100 tenancy_validator.validate_fixed_term_tenancy(record) @@ -107,14 +117,19 @@ RSpec.describe Validations::TenancyValidations do end context "when the collection start year is 2022 or later" do - let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 5, 1)) } + let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 5, 1), needstype: 1, rent_type: 1) } context "when type of tenancy is Secure - fixed term" do - let(:expected_error) { I18n.t("validations.tenancy.length.secure") } + let(:expected_error) do + I18n.t( + "validations.tenancy.length.secure", + min_tenancy_length: 2, + ) + end before { record.tenancy = 6 } - context "when tenancy length is greater than 1" do + context "when tenancy length is less than 2" do it "adds an error" do record.tenancylength = 1 tenancy_validator.validate_fixed_term_tenancy(record) @@ -123,7 +138,7 @@ RSpec.describe Validations::TenancyValidations do end end - context "when tenancy length is less than 100" do + context "when tenancy length is greater than 99" do it "adds an error" do record.tenancylength = 100 tenancy_validator.validate_fixed_term_tenancy(record) @@ -152,11 +167,16 @@ RSpec.describe Validations::TenancyValidations do end context "when type of tenancy is Secure - lifetime" do - let(:expected_error) { I18n.t("validations.tenancy.length.secure") } + let(:expected_error) do + I18n.t( + "validations.tenancy.length.secure", + min_tenancy_length: 2, + ) + end before { record.tenancy = 7 } - context "when tenancy length is greater than 1" do + context "when tenancy length is less than 2" do it "adds an error" do record.tenancylength = 1 tenancy_validator.validate_fixed_term_tenancy(record) @@ -165,7 +185,7 @@ RSpec.describe Validations::TenancyValidations do end end - context "when tenancy length is less than 100" do + context "when tenancy length is greater than 99" do it "adds an error" do record.tenancylength = 100 tenancy_validator.validate_fixed_term_tenancy(record)