Skip to content
Open
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
1 change: 1 addition & 0 deletions app/controllers/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def current_archived_welsh_form

def load_form
@current_form = Form.find(params[:form_id])
@current_form.set_task_status_service(TaskStatusService.new(form: @current_form, current_user:))
@initial_form_state = @current_form.state
end

Expand Down
13 changes: 6 additions & 7 deletions app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Form < ApplicationRecord
after_update :update_draft_form_document
ATTRIBUTES_NOT_IN_FORM_DOCUMENT = %i[state external_id pages question_section_completed declaration_section_completed share_preview_completed welsh_completed].freeze

attr_accessor :task_status_service

def save_question_changes!
self.question_section_completed = false
# Make sure the updated_at is updated as we use this to determine if the form has changed in forms-runner.
Expand Down Expand Up @@ -218,6 +220,10 @@ def draft_created?(previous_state)
(previous_state.to_sym == :archived && archived_with_draft?)
end

def set_task_status_service(service)
self.task_status_service = service
end

private

def set_external_id
Expand All @@ -228,13 +234,6 @@ def update_draft_form_document
FormDocumentSyncService.new(self).update_draft_form_document
end

def task_status_service
# TODO: refactor this in favour of dependency injection
# https://trello.com/c/TVN9BNxQ/3448-refactor-formtaskstatusservice-to-use-dependency-injection
# it can also lead to use of `allow_any_instance_of` in testing
@task_status_service ||= TaskStatusService.new(form: self)
end

def has_routing_conditions
pages.filter { |p| p.routing_conditions.any? }.any?
end
Expand Down
1 change: 1 addition & 0 deletions app/services/form_task_list_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def call(**args)
def initialize(form:, current_user:)
@current_user = current_user
@form = form
@form.set_task_status_service(TaskStatusService.new(form:, current_user:))
@task_statuses = form.all_task_statuses
@task_counts = status_counts
end
Expand Down
5 changes: 3 additions & 2 deletions app/services/task_status_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class TaskStatusService
def initialize(form:)
def initialize(form:, current_user:)
@form = form
@current_user = current_user
end

def mandatory_tasks_completed?(ignore_missing_welsh: false)
Expand Down Expand Up @@ -144,7 +145,7 @@ def make_live_status_for_draft

def welsh_translations_invalid
@welsh_translations_invalid ||= begin
translation_input = Forms::WelshTranslationInput.new(form: @form, mark_complete: true).assign_form_values
translation_input = Forms::WelshTranslationInput.new(form: @form, mark_complete: true, current_user: @current_user).assign_form_values
translation_input.invalid?
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/input_objects/forms/make_live_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

RSpec.describe Forms::MakeLiveInput, type: :model do
let(:error_message) { I18n.t("activemodel.errors.models.forms/make_live_input.attributes.confirm.blank") }
let(:form) { create :form, :ready_for_live }
let(:user) { build :user }

before do
form.set_task_status_service(TaskStatusService.new(form:, current_user: user))
end

describe "validations" do
it "is invalid if blank" do
Expand Down
90 changes: 61 additions & 29 deletions spec/models/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,14 @@
RSpec.describe Form, type: :model do
subject(:form) { described_class.new }

let(:current_user) { build :user }

describe "factory" do
it "has a valid factory" do
form = create :form
expect(form).to be_valid
end

it "has a ready for live trait" do
form = build :form, :ready_for_live, :with_group
expect(form.ready_for_live).to be true
expect(form.incomplete_tasks).to be_empty
expect(form.task_statuses).to include(
declaration_status: :completed,
make_live_status: :not_started,
name_status: :completed,
pages_status: :completed,
privacy_policy_status: :completed,
support_contact_details_status: :completed,
what_happens_next_status: :completed,
)
end

it "has a live trait" do
form = build :form, :live
expect(form.state).to eq "live"
Expand All @@ -50,9 +37,36 @@
expect(form.pages.map(&:position)).to eq [1, 2, 3, 4, 5]
end

it "has a missing pages trait" do
form = build :form, :missing_pages
expect(form.incomplete_tasks).to eq %i[missing_pages]
describe "task status traits" do
before do
form.set_task_status_service(TaskStatusService.new(form:, current_user:))
end

describe "ready for live trait" do
let(:form) { build :form, :ready_for_live, :with_group }

it "creates a form that is ready to be made live" do
expect(form.ready_for_live).to be true
expect(form.incomplete_tasks).to be_empty
expect(form.task_statuses).to include(
declaration_status: :completed,
make_live_status: :not_started,
name_status: :completed,
pages_status: :completed,
privacy_policy_status: :completed,
support_contact_details_status: :completed,
what_happens_next_status: :completed,
)
end
end

describe "missing pages trait" do
let(:form) { build :form, :missing_pages }

it "creates a form with missing pages" do
expect(form.incomplete_tasks).to eq %i[missing_pages]
end
end
end
end

Expand Down Expand Up @@ -503,6 +517,10 @@
end

describe "FormStateMachine" do
before do
form.set_task_status_service(TaskStatusService.new(form:, current_user:))
end

describe "#make_live!" do
let(:form) { create :form, :ready_for_live }

Expand Down Expand Up @@ -861,16 +879,20 @@
end

describe "#ready_for_live" do
before do
form.set_task_status_service(TaskStatusService.new(form:, current_user:))
end

context "when a form is complete and ready to be made live" do
let(:completed_form) { create(:form, :live) }
let(:form) { create(:form, :live) }

it "returns true" do
expect(completed_form.ready_for_live).to be true
expect(form.ready_for_live).to be true
end
end

context "when a form is incomplete and should still be in draft state" do
let(:new_form) { build :form, :new_form }
let(:form) { build :form, :new_form }

[
{
Expand All @@ -891,27 +913,31 @@
},
].each do |scenario|
it "returns false if #{scenario[:attribute]} is missing" do
new_form.send("#{scenario[:attribute]}=", scenario[:attribute_value])
expect(new_form.ready_for_live).to be false
form.send("#{scenario[:attribute]}=", scenario[:attribute_value])
expect(form.ready_for_live).to be false
end
end
end
end

describe "#all_incomplete_tasks" do
before do
form.set_task_status_service(TaskStatusService.new(form:, current_user:))
end

context "when a form is complete and ready to be made live" do
let(:completed_form) { build :form, :live }
let(:form) { build :form, :live }

it "returns no missing sections" do
expect(completed_form.all_incomplete_tasks).to be_empty
expect(form.all_incomplete_tasks).to be_empty
end
end

context "when a form is incomplete and should still be in draft state" do
let(:new_form) { build :form, :new_form }
let(:form) { build :form, :new_form }

it "returns a set of keys related to missing fields" do
expect(new_form.all_incomplete_tasks).to match_array(%i[missing_pages missing_submission_email missing_privacy_policy_url missing_contact_details missing_what_happens_next share_preview_not_completed])
expect(form.all_incomplete_tasks).to match_array(%i[missing_pages missing_submission_email missing_privacy_policy_url missing_contact_details missing_what_happens_next share_preview_not_completed])
end
end
end
Expand Down Expand Up @@ -975,6 +1001,8 @@
task_status_service = instance_double(TaskStatusService)
allow(TaskStatusService).to receive(:new).and_return(task_status_service)
allow(task_status_service).to receive(:mandatory_tasks_completed?).and_return(mandatory_tasks_completed)

form.set_task_status_service(task_status_service)
end

context "when not all mandatory tasks have been completed" do
Expand All @@ -996,7 +1024,11 @@

describe "#all_task_statuses" do
let(:group) { create :group }
let(:completed_form) { build :form, :live, :with_group, group: }
let(:form) { build :form, :live, :with_group, group: }

before do
form.set_task_status_service(TaskStatusService.new(form:, current_user:))
end

it "returns a hash with each of the task statuses" do
expected_hash = {
Expand All @@ -1015,7 +1047,7 @@
share_preview_status: :completed,
make_live_status: :completed,
}
expect(completed_form.all_task_statuses).to eq expected_hash
expect(form.all_task_statuses).to eq expected_hash
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/requests/forms/make_live_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
let(:group_role) { :group_admin }
let(:group) { create(:group, organisation:, status: :active) }

before do
form.set_task_status_service(TaskStatusService.new(form:, current_user: user))
end

describe "#new" do
before do
Membership.create!(group_id: group.id, user:, added_by: user, role: group_role)
Expand Down
4 changes: 4 additions & 0 deletions spec/services/make_form_live_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
let(:live_form_document) { current_form.live_form_document }
let(:current_user) { build :user }

before do
current_form.set_task_status_service(TaskStatusService.new(form: current_form, current_user:))
end

describe "#make_live" do
it "makes the form live" do
expect {
Expand Down
1 change: 1 addition & 0 deletions spec/services/org_admin_alerts_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

before do
GroupForm.create!(form: form, group:)
form.set_task_status_service(TaskStatusService.new(form:, current_user:))
end

describe "#form_made_live" do
Expand Down
6 changes: 6 additions & 0 deletions spec/services/step_summary_card_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

let(:pages) { form.pages }

let(:current_user) { build :user }

before do
form.set_task_status_service(TaskStatusService.new(form:, current_user:))
end

describe "#all_options_for_answer_type" do
context "with uk and international address" do
let(:page) { create :page, :with_address_settings, form:, uk_address: "true", international_address: "true" }
Expand Down
6 changes: 6 additions & 0 deletions spec/services/step_summary_table_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
let(:form_document_step) { FormDocument::Step.new(page.as_form_document_step(nil)) }
let(:welsh_form_document) { FormDocument::Content.from_form_document(form.live_welsh_form_document) }

let(:current_user) { build :user }

before do
form.set_task_status_service(TaskStatusService.new(form:, current_user:))
end

def create_form(attributes = {})
default_attributes = {
id: 1,
Expand Down
9 changes: 5 additions & 4 deletions spec/services/task_status_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require "rails_helper"

describe TaskStatusService do
let(:group) { create :group }
let(:task_status_service) do
described_class.new(form:)
subject(:task_status_service) do
described_class.new(form:, current_user:)
end

let(:current_user) { build(:user, role: :editor) }
let(:group) { create :group }

let(:current_user) { build(:user, role: :standard) }

describe "statuses" do
describe "name status" do
Expand Down