Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/pages/conditions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ class Pages::ConditionsController < PagesController
before_action :can_add_page_routing, only: %i[new create]

def routing_page
routing_page_input = Pages::RoutingPageInput.new({ routing_page_id: params[:routing_page_id] })
routing_page_input = Pages::RoutingPageInput.new({ routing_page_id: params[:routing_page_id], form: current_form })
render template: "pages/conditions/routing_page", locals: { form: current_form, routing_page_input: }
end

def set_routing_page
routing_page_id = params[:pages_routing_page_input][:routing_page_id]
routing_page_input = Pages::RoutingPageInput.new({ routing_page_id: })
routing_page_input = Pages::RoutingPageInput.new({ routing_page_id:, form: current_form })

if routing_page_input.valid?
routing_page = PageRepository.find(page_id: routing_page_id, form_id: current_form.id)
Expand Down
8 changes: 7 additions & 1 deletion app/input_objects/base_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ class BaseInput

def set_validation_error_logging_attributes
CurrentLoggingAttributes.validation_errors = errors.map { |error| "#{error.attribute}: #{error.type}" } if errors.any?
form_name = form.name if defined?(form) && form.present?

errors.each do |error|
AnalyticsService.track_validation_errors(input_object_name: self.class.name, field: error.attribute, error_type: error.type, form_name:)
end
end

def form_name
return form.name if defined?(form) && form.present?
return draft_question.form_name if defined?(draft_question) && draft_question.present?

nil
end
end
2 changes: 1 addition & 1 deletion app/input_objects/pages/routing_page_input.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Pages::RoutingPageInput < BaseInput
attr_accessor :routing_page_id
attr_accessor :routing_page_id, :form

validates :routing_page_id, presence: true

Expand Down
4 changes: 4 additions & 0 deletions app/models/draft_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ def answer_settings

raw_settings.deep_symbolize_keys
end

def form_name
Form.find(form_id).name
end
end
40 changes: 40 additions & 0 deletions spec/input_objects/base_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class TestInputWithForm < BaseInput
validates :email, format: { with: /.*@.*/, message: "must be a valid email address" }
end

class TestInputWithDraftQuestion < BaseInput
attr_accessor :name, :email, :draft_question

validates :name, presence: true
validates :email, format: { with: /.*@.*/, message: "must be a valid email address" }
end

RSpec.describe BaseInput do
describe "validation error logging" do
let(:analytics_service) { class_double(AnalyticsService).as_stubbed_const }
Expand Down Expand Up @@ -96,6 +103,39 @@ class TestInputWithForm < BaseInput
end
end
end

context "when the draft_question is defined in the input" do
let(:input) { TestInputWithDraftQuestion.new }

context "when the draft_question is nil" do
it "does not include a form name in the validation errors" do
input.valid?

expect(analytics_service).to have_received(:track_validation_errors)
.with(input_object_name: "TestInputWithDraftQuestion", form_name: nil, field: :name, error_type: :blank)

expect(analytics_service).to have_received(:track_validation_errors)
.with(input_object_name: "TestInputWithDraftQuestion", form_name: nil, field: :email, error_type: :invalid)
end
end

context "when the draft_question is present" do
let(:form_name) { "Apply for a juggling licence" }
let(:form) { create :form, name: form_name }
let(:draft_question) { build :draft_question, form_id: form.id }
let(:input) { TestInputWithDraftQuestion.new(draft_question:) }

it "includes the form name in the validation errors" do
input.valid?

expect(analytics_service).to have_received(:track_validation_errors)
.with(input_object_name: "TestInputWithDraftQuestion", form_name:, field: :name, error_type: :blank)

expect(analytics_service).to have_received(:track_validation_errors)
.with(input_object_name: "TestInputWithDraftQuestion", form_name:, field: :email, error_type: :invalid)
end
end
end
end
end
end
3 changes: 2 additions & 1 deletion spec/input_objects/pages/address_settings_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "rails_helper"

RSpec.describe Pages::AddressSettingsInput, type: :model do
let(:form) { create :form }
let(:address_settings_input) { build :address_settings_input, draft_question: }
let(:draft_question) { build :draft_question, answer_type: "address", form_id: 1 }
let(:draft_question) { build :draft_question, answer_type: "address", form_id: form.id }

it "has a valid factory" do
expect(address_settings_input).to be_valid
Expand Down
3 changes: 2 additions & 1 deletion spec/input_objects/pages/date_settings_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "rails_helper"

RSpec.describe Pages::DateSettingsInput, type: :model do
let(:form) { create :form }
let(:date_settings_input) { build :date_settings_input, draft_question: }
let(:draft_question) { build :draft_question, answer_type: "date", form_id: 1 }
let(:draft_question) { build :draft_question, answer_type: "date", form_id: form.id }

it "has a valid factory" do
expect(date_settings_input).to be_valid
Expand Down
3 changes: 2 additions & 1 deletion spec/input_objects/pages/guidance_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "rails_helper"

RSpec.describe Pages::GuidanceInput, type: :model do
let(:form) { create :form }
let(:guidance_input) { build :guidance_input, page_heading:, guidance_markdown:, draft_question: }
let(:draft_question) { build :draft_question, page_heading:, guidance_markdown: }
let(:draft_question) { build :draft_question, page_heading:, guidance_markdown:, form_id: form.id }
let(:page_heading) { "New guidance heading" }
let(:guidance_markdown) { "## Level heading 2" }

Expand Down
3 changes: 2 additions & 1 deletion spec/input_objects/pages/name_settings_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "rails_helper"

RSpec.describe Pages::NameSettingsInput, type: :model do
let(:form) { create :form }
let(:name_settings_input) { build :name_settings_input, draft_question: }
let(:draft_question) { build :draft_question, answer_type: "name", form_id: 1 }
let(:draft_question) { build :draft_question, answer_type: "name", form_id: form.id }

it "has a valid factory" do
expect(name_settings_input).to be_valid
Expand Down
13 changes: 7 additions & 6 deletions spec/input_objects/pages/question_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "rails_helper"

RSpec.describe Pages::QuestionInput, type: :model do
let(:form) { create :form }
let(:question_input) { build :question_input, answer_type:, question_text:, draft_question:, is_optional:, is_repeatable: }
let(:draft_question) { build :draft_question, question_text: }
let(:draft_question) { build :draft_question, question_text:, form_id: form.id }
let(:question_text) { "What is your full name?" }
let(:is_optional) { "false" }
let(:is_repeatable) { "false" }
Expand Down Expand Up @@ -78,7 +79,7 @@
end

describe "#hint_text" do
let(:question_input) { build :question_input, hint_text: }
let(:question_input) { build :question_input, hint_text:, draft_question: }
let(:hint_text) { "Enter your full name as it appears in your passport" }

it "is valid if hint text is empty" do
Expand Down Expand Up @@ -113,7 +114,7 @@
end

describe "#is_optional" do
let(:question_input) { build :question_input, is_optional: }
let(:question_input) { build :question_input, is_optional:, draft_question: }

context "when is_optional is nil" do
let(:is_optional) { nil }
Expand Down Expand Up @@ -156,7 +157,7 @@
end

describe "#is_repeatable" do
let(:question_input) { build :question_input, is_repeatable: }
let(:question_input) { build :question_input, is_repeatable:, draft_question: }

context "and is_repeatable is nil" do
let(:is_repeatable) { nil }
Expand Down Expand Up @@ -209,7 +210,7 @@
describe "selection options" do
let(:selection_options) { (1..31).to_a.map { |i| { name: i.to_s } } }
let(:only_one_option) { "false" }
let(:draft_question) { build :selection_draft_question, answer_settings: { selection_options:, only_one_option: } }
let(:draft_question) { build :selection_draft_question, answer_settings: { selection_options:, only_one_option: }, form_id: form.id }

context "when only_one_option is true" do
let(:only_one_option) { "true" }
Expand Down Expand Up @@ -240,7 +241,7 @@
end

context "when answer type is not selection" do
let(:draft_question) { build :name_draft_question, answer_settings: { selection_options:, only_one_option: } }
let(:draft_question) { build :name_draft_question, answer_settings: { selection_options:, only_one_option: }, form_id: form.id }

it "is valid" do
expect(question_input).to be_valid
Expand Down
3 changes: 2 additions & 1 deletion spec/input_objects/pages/question_text_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "rails_helper"

RSpec.describe Pages::QuestionTextInput, type: :model do
let(:form) { create :form }
let(:question_text_input) { build :question_text_input, draft_question: }
let(:draft_question) { build :draft_question, answer_type: "selection", form_id: 1 }
let(:draft_question) { build :draft_question, answer_type: "selection", form_id: form.id }

it "has a valid factory" do
expect(question_text_input).to be_valid
Expand Down
2 changes: 1 addition & 1 deletion spec/input_objects/pages/routing_page_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.describe Pages::RoutingPageInput, type: :model do
let(:routing_page_input) { described_class.new({ routing_page_id: }) }
let(:routing_page_input) { described_class.new({ routing_page_id:, form: }) }
let(:form) { create :form, :ready_for_routing }
let(:pages) { form.pages }
let(:routing_page_id) { pages.first.id }
Expand Down
5 changes: 3 additions & 2 deletions spec/input_objects/pages/selection/bulk_options_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require "rails_helper"

RSpec.describe Pages::Selection::BulkOptionsInput, type: :model do
let(:form) { create :form }
let(:bulk_options_input) { build :bulk_options_input, draft_question: }
let(:only_one_option) { "true" }
let(:draft_question) { build :draft_question, answer_type: "selection", answer_settings: { only_one_option: } }
let(:draft_question) { create :draft_question, answer_type: "selection", answer_settings: { only_one_option: }, form_id: form.id }

it "has a valid factory" do
expect(bulk_options_input).to be_valid
Expand Down Expand Up @@ -75,7 +76,7 @@
end

context "when include_none_of_the_above is not 'true' or 'false'" do
let(:bulk_options_input) { build :bulk_options_input, include_none_of_the_above: nil }
let(:bulk_options_input) { build :bulk_options_input, include_none_of_the_above: nil, draft_question: }

it "is invalid" do
error_message = I18n.t("activemodel.errors.models.pages/selection/bulk_options_input.attributes.include_none_of_the_above.inclusion")
Expand Down
3 changes: 2 additions & 1 deletion spec/input_objects/pages/selection/options_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "rails_helper"

RSpec.describe Pages::Selection::OptionsInput do
let(:form) { create :form }
let(:only_one_option) { "true" }
let(:draft_question) { build :draft_question, answer_type: "selection", answer_settings: { only_one_option: } }
let(:draft_question) { build :draft_question, answer_type: "selection", answer_settings: { only_one_option: }, form_id: form.id }
let(:selection_options) { [{ name: "option 1" }, { name: "option 2" }] }

describe "validations" do
Expand Down
4 changes: 3 additions & 1 deletion spec/input_objects/pages/selection/type_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
RSpec.describe Pages::Selection::TypeInput do
subject(:input) { described_class.new(draft_question:, only_one_option:) }

let(:draft_question) { build :draft_question, answer_type: "selection" }
let(:form) { create :form }

let(:draft_question) { build :draft_question, answer_type: "selection", form_id: form.id }
let(:only_one_option) { nil }

describe "validations" do
Expand Down
3 changes: 2 additions & 1 deletion spec/input_objects/pages/text_settings_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "rails_helper"

RSpec.describe Pages::TextSettingsInput, type: :model do
let(:form) { create :form }
let(:text_settings_input) { build :text_settings_input, draft_question: }
let(:draft_question) { build :draft_question, answer_type: "text", form_id: 1 }
let(:draft_question) { build :draft_question, answer_type: "text", form_id: form.id }

it "has a valid factory" do
expect(text_settings_input).to be_valid
Expand Down
17 changes: 17 additions & 0 deletions spec/models/draft_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,21 @@
end
end
end

describe "form_name" do
let(:form) { create :form }
let(:draft_question) { create :draft_question, form_id: form.id }

it "returns the name of the associated form" do
expect(draft_question.form_name).to eq(form.name)
end

context "when the associated form does not exist" do
let(:draft_question) { create :draft_question, form_id: 0 }

it "throws a NotFoundError" do
expect { draft_question.form_name }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/requests/pages/guidance_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:form) { create :form, id: 1 }
let(:pages) { create_list :page, 5, form_id: form.id }
let(:page) { pages.first }
let(:draft_question) { build :draft_question }
let(:draft_question) { build :draft_question, form_id: form.id }
let(:page_heading) { "Page heading" }
let(:guidance_markdown) { "## Heading level 2" }

Expand Down
2 changes: 1 addition & 1 deletion spec/views/pages/conditions/routing_page.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "pages/conditions/routing_page.html.erb" do
let(:form) { create :form, :ready_for_routing }
let(:pages) { form.pages }
let(:routing_page_input) { Pages::RoutingPageInput.new }
let(:routing_page_input) { Pages::RoutingPageInput.new(form:) }
let(:allowed_to_create_routes) { true }
let(:all_routes_created) { false }
let(:group) do
Expand Down