From e50bafd64ef1406a59fa59c52709ab6651113b3f Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Tue, 19 Jul 2022 13:35:21 +0100 Subject: [PATCH] Check if a question is enabled before validating it's answer (#753) --- app/controllers/form_controller.rb | 11 +++++++++-- spec/requests/form_controller_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index aed35e7a1..936551749 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -138,8 +138,15 @@ private end def question_is_required?(question) - CaseLog::OPTIONAL_FIELDS.exclude?(question.id) && - @page.subsection.applicable_questions(@case_log).map(&:id).include?(question.id) + CaseLog::OPTIONAL_FIELDS.exclude?(question.id) && required_questions.include?(question.id) + end + + def required_questions + @required_questions ||= begin + log = @case_log + log.assign_attributes(responses_for_page(@page)) + @page.subsection.applicable_questions(log).select { |q| q.enabled?(log) }.map(&:id) + end end def question_missing_response?(responses_for_page, question) diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 9e220486f..0ad7494e0 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -263,6 +263,33 @@ RSpec.describe FormController, type: :request do expect(whodunnit_actor.id).to eq(user.id) end end + + context "when the question has a conditional question" do + context "and the conditional question is not enabled" do + context "but is applicable because it has an inferred check answers display value" do + let(:page_id) { "property_postcode" } + let(:valid_params) do + { + id: case_log.id, + case_log: { + page: page_id, + postcode_known: "0", + postcode_full: "", + }, + } + end + + before do + case_log.update!(postcode_known: 1, postcode_full: "NW1 8RR") + post "/logs/#{case_log.id}/form", params: valid_params + end + + it "does not require you to answer that question" do + expect(response).to redirect_to("/logs/#{case_log.id}/do-you-know-the-local-authority") + end + end + end + end end context "with checkbox questions" do