Skip to content

Commit

Permalink
Merge pull request #2430 from DFE-Digital/AQTS-600-age-range-validation
Browse files Browse the repository at this point in the history
[AQTS-600] Assessor interface age range validations
  • Loading branch information
Hassanmir92 authored Oct 11, 2024
2 parents 25ed0b8 + 529217d commit 1864aae
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ def edit_age_range_subjects
assessment:,
age_range_min: assessment.age_range_min,
age_range_max: assessment.age_range_max,
age_range_note: assessment.age_range_note,
subject_1: assessment.subjects.first,
subject_2: assessment.subjects.second,
subject_3: assessment.subjects.third,
subjects_note: assessment.subjects_note,
)
end

Expand Down
15 changes: 15 additions & 0 deletions app/forms/concerns/assessor_interface/age_range_subjects_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module AssessorInterface::AgeRangeSubjectsForm
attribute :age_range_max, :integer
attribute :age_range_note, :string

validates :age_range_note,
presence: true,
if: :age_range_changed_from_application_form?
validates :age_range_min,
presence: true,
numericality: {
Expand All @@ -19,6 +22,7 @@ module AssessorInterface::AgeRangeSubjectsForm
presence: true,
numericality: {
only_integer: true,
less_than_or_equal_to: 19,
greater_than_or_equal_to: :age_range_min,
allow_nil: true,
}
Expand Down Expand Up @@ -60,4 +64,15 @@ def save

true
end

private

delegate :application_form, to: :assessment

def age_range_changed_from_application_form?
return unless assessment

application_form.age_range_min != age_range_min ||
application_form.age_range_max != age_range_max
end
end
7 changes: 6 additions & 1 deletion app/views/shared/_age_range_subjects_form_fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<%= f.govuk_fieldset legend: { text: "What age range is the applicant qualified to teach?" } do %>
<p class="govuk-body">Based on the evidence the applicant has provided, you can either copy the age range they entered if you agree, or enter a new range.</p>
<p class="govuk-body">You can edit the age range below if:</p>

<ul class="govuk-list govuk-list--bullet">
<li>based on evidence, you do not agree with the age range the applicant has submitted</li>
<li>the 'To' age is greater than 19 and will not be accepted by DQT</li>
</ul>

<%= f.govuk_number_field :age_range_min, width: 3 %>
<%= f.govuk_number_field :age_range_max, width: 3 %>
Expand Down
14 changes: 14 additions & 0 deletions config/locales/assessor_interface.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,26 @@ en:
blank: Enter a note to the applicant
written_statement_recent_notes:
blank: Enter a note to the applicant
assessor_interface/check_age_range_subjects_form:
attributes:
age_range_min:
blank: Enter the minimum age
age_range_max:
blank: Enter the maximum age
less_than_or_equal_to: We cannot accept an age greater than 19
greater_than_or_equal_to: ‘To’ must be greater than ‘From’
age_range_note:
blank: Explain why you have entered a new age range
assessor_interface/confirm_age_range_subjects_form:
attributes:
age_range_min:
blank: Enter the minimum age
age_range_max:
blank: Enter the maximum age
less_than_or_equal_to: We cannot accept an age greater than 19
greater_than_or_equal_to: ‘To’ must be greater than ‘From’
age_range_note:
blank: Explain why you have entered a new age range
subject_1:
blank: Enter the subject
assessor_interface/consent_method_form:
Expand Down
4 changes: 2 additions & 2 deletions config/locales/helpers.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ en:
assessor_interface_assessment_section_form:
age_range_max: To
age_range_min: From
age_range_note: '<strong class="govuk-!-font-weight-bold">Internal note:</strong> If you''ve entered a new range please explain why (optional)'
age_range_note: '<strong class="govuk-!-font-weight-bold">Internal note:</strong> If you''ve entered a new range please explain why'
failure_reason_notes:
decline: Note to the applicant (optional)
document: Note to the applicant
Expand All @@ -112,7 +112,7 @@ en:
assessor_interface_confirm_age_range_subjects_form:
age_range_min: From
age_range_max: To
age_range_note: '<strong class="govuk-!-font-weight-bold">Internal note:</strong> If you''ve entered a new range please explain why (optional)'
age_range_note: '<strong class="govuk-!-font-weight-bold">Internal note:</strong> If you''ve entered a new range please explain why'
subject_1: Subject 1
subject_2: Subject 2 (optional)
subject_3: Subject 3 (optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,4 @@
let(:assessment) { assessment_section.assessment }
let(:attributes) { { passed: true } }
end

describe "validations" do
it { is_expected.to validate_presence_of(:assessment_section) }
it { is_expected.to validate_presence_of(:user) }
it { is_expected.to allow_values(true, false).for(:passed) }
end

describe "#save" do
subject(:save) { form.save }

describe "when invalid attributes" do
it { is_expected.to be false }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,4 @@
it { is_expected.to validate_presence_of(:assessment) }
it { is_expected.to validate_presence_of(:user) }
end

describe "#save" do
subject(:save) { form.save }

describe "when invalid attributes" do
it { is_expected.to be false }
end
end
end
80 changes: 58 additions & 22 deletions spec/support/shared_examples/age_range_subjects_form.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,70 @@
# frozen_string_literal: true

RSpec.shared_examples_for "an age range subjects form" do
it { is_expected.to validate_presence_of(:age_range_min) }
it { is_expected.to validate_presence_of(:age_range_max) }

describe "validations" do
it { is_expected.to validate_presence_of(:age_range_min) }
it { is_expected.to validate_presence_of(:age_range_max) }
let(:age_range_subjects_attributes) do
{
age_range_min: "5",
age_range_max: "16",
age_range_note: "Note",
subject_1: "Subject",
subject_1_raw: "Subject",
}
end

context "with a minimum too low" do
let(:age_range_subjects_attributes) { { age_range_min: "1" } }
before do
assessment.application_form.update(age_range_min: 5, age_range_max: 16)
end

context "when the minimum has changed but a note has not been added" do
before do
age_range_subjects_attributes[:age_range_min] = "6"
age_range_subjects_attributes[:age_range_note] = ""
end

it { is_expected.to be_invalid }
end

context "with a minimum too high" do
let(:age_range_subjects_attributes) { { age_range_min: "20" } }
context "with a minimum too low" do
before { age_range_subjects_attributes[:age_range_min] = "-1" }

it { is_expected.to be_invalid }
end

context "when minimum is set" do
context "with a maximum too low" do
let(:age_range_subjects_attributes) do
{ age_range_min: "7", age_range_max: "1" }
end
context "with a minimum between 0 and 19" do
before { age_range_subjects_attributes[:age_range_min] = "8" }

it { is_expected.to be_invalid }
it { is_expected.to be_valid }
end

context "when the maximum has changed but a note has not been added" do
before do
age_range_subjects_attributes[:age_range_max] = "18"
age_range_subjects_attributes[:age_range_note] = ""
end

context "with a maximum too high" do
let(:age_range_subjects_attributes) do
{ age_range_min: "7", age_range_max: "20" }
end
it { is_expected.to be_invalid }
end

context "with a maximum lower than the minimum" do
before { age_range_subjects_attributes[:age_range_max] = "4" }

it { is_expected.to be_invalid }
end
it { is_expected.to be_invalid }
end

context "with a maximum greater than min but below 20" do
before { age_range_subjects_attributes[:age_range_max] = "19" }

it { is_expected.to be_valid }
end

context "with a maximum too high" do
before { age_range_subjects_attributes[:age_range_max] = "20" }

it { is_expected.to be_invalid }
end

it { is_expected.not_to validate_presence_of(:age_range_note) }
Expand All @@ -47,15 +79,19 @@
describe "#save" do
subject(:save) { form.save }

before do
assessment.application_form.update(age_range_min: 5, age_range_max: 16)
end

describe "when invalid attributes" do
it { is_expected.to be false }
end

describe "with valid attributes and no note" do
let(:age_range_subjects_attributes) do
{
age_range_min: "7",
age_range_max: "11",
age_range_min: "5",
age_range_max: "16",
subject_1: "Subject",
subject_1_raw: "Subject",
}
Expand All @@ -66,8 +102,8 @@
it "sets the attributes" do
save # rubocop:disable Rails/SaveBang

expect(assessment.age_range_min).to eq(7)
expect(assessment.age_range_max).to eq(11)
expect(assessment.age_range_min).to eq(5)
expect(assessment.age_range_max).to eq(16)
expect(assessment.age_range_note).to be_blank

expect(assessment.subjects).to eq(%w[Subject])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def given_there_is_a_verifiable_application_form
:assessment,
age_range_max: 11,
age_range_min: 8,
age_range_note: "Explanation",
application_form:,
subjects: %w[mathematics],
)
Expand Down

0 comments on commit 1864aae

Please sign in to comment.