2022-11-28 15:54:14 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-01 09:37:00 +01:00
|
|
|
RSpec.describe DossierPrefillableConcern do
|
2022-11-28 15:54:14 +01:00
|
|
|
describe '.prefill!' do
|
2023-09-07 10:10:07 +02:00
|
|
|
let(:procedure) { create(:procedure, :published, :for_individual, types_de_champ_public:, types_de_champ_private:) }
|
|
|
|
let(:dossier) { create(:dossier, :brouillon, :with_individual, procedure: procedure) }
|
2022-12-21 19:20:47 +01:00
|
|
|
let(:types_de_champ_public) { [] }
|
2023-06-14 11:23:04 +02:00
|
|
|
let(:types_de_champ_private) { [] }
|
2023-09-07 10:03:17 +02:00
|
|
|
let(:identity_attributes) { {} }
|
|
|
|
let(:values) { [] }
|
2022-11-28 15:54:14 +01:00
|
|
|
|
2023-01-11 15:07:16 +01:00
|
|
|
subject(:fill) do
|
2023-09-07 10:03:17 +02:00
|
|
|
dossier.prefill!(values, identity_attributes)
|
2023-01-11 15:07:16 +01:00
|
|
|
dossier.reload
|
|
|
|
end
|
2022-11-28 15:54:14 +01:00
|
|
|
|
2023-01-03 14:46:10 +01:00
|
|
|
shared_examples 'a dossier marked as prefilled' do
|
|
|
|
it 'marks the dossier as prefilled' do
|
|
|
|
expect { fill }.to change { dossier.reload.prefilled }.from(nil).to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
context "when dossier is for individual" do
|
|
|
|
let(:procedure) { create(:procedure, :published, :for_individual, types_de_champ_public:, types_de_champ_private:) }
|
|
|
|
let(:dossier) { create(:dossier, :brouillon, :with_individual, procedure: procedure) }
|
2022-11-28 15:54:14 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
context "when identity_attributes is present" do
|
|
|
|
let(:identity_attributes) { { "prenom" => "Prénom", "nom" => "Nom", "gender" => "Mme" } }
|
|
|
|
|
|
|
|
it_behaves_like 'a dossier marked as prefilled'
|
|
|
|
|
|
|
|
it "updates the individual" do
|
|
|
|
fill
|
|
|
|
expect(dossier.individual.prenom).to eq("Prénom")
|
|
|
|
expect(dossier.individual.nom).to eq("Nom")
|
|
|
|
expect(dossier.individual.gender).to eq("Mme")
|
|
|
|
end
|
2023-01-19 11:51:56 +01:00
|
|
|
end
|
2023-01-03 14:46:10 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
context 'when champs_attributes is empty' do
|
|
|
|
it "doesn't mark the dossier as prefilled" do
|
|
|
|
expect { fill }.not_to change { dossier.reload.prefilled }.from(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change champs_public" do
|
|
|
|
expect { fill }.not_to change { dossier.champs_public.to_a }
|
|
|
|
end
|
2022-11-28 15:54:14 +01:00
|
|
|
end
|
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
context 'when champs_attributes has values' do
|
|
|
|
context 'when the champs are valid' do
|
|
|
|
let(:types_de_champ_public) { [{ type: :text }, { type: :phone }] }
|
|
|
|
let(:types_de_champ_private) { [{ type: :text }] }
|
2023-06-14 11:23:04 +02:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first }
|
|
|
|
let(:value_1) { "any value" }
|
|
|
|
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
2022-11-28 15:54:14 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
let(:type_de_champ_2) { procedure.published_revision.types_de_champ_public.second }
|
|
|
|
let(:value_2) { "33612345678" }
|
|
|
|
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
|
2022-11-28 15:54:14 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
let(:type_de_champ_3) { procedure.published_revision.types_de_champ_private.first }
|
|
|
|
let(:value_3) { "some value" }
|
|
|
|
let(:champ_id_3) { find_champ_by_stable_id(dossier, type_de_champ_3.stable_id).id }
|
2023-06-14 11:23:04 +02:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
let(:values) { [{ id: champ_id_1, value: value_1 }, { id: champ_id_2, value: value_2 }, { id: champ_id_3, value: value_3 }] }
|
2022-11-28 15:54:14 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
it_behaves_like 'a dossier marked as prefilled'
|
2023-01-03 14:46:10 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
it "updates the champs with the new values and mark them as prefilled" do
|
|
|
|
fill
|
2022-12-01 12:00:21 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
expect(dossier.champs_public.first.value).to eq(value_1)
|
|
|
|
expect(dossier.champs_public.first.prefilled).to eq(true)
|
|
|
|
expect(dossier.champs_public.last.value).to eq(value_2)
|
|
|
|
expect(dossier.champs_public.last.prefilled).to eq(true)
|
|
|
|
expect(dossier.champs_private.first.value).to eq(value_3)
|
|
|
|
expect(dossier.champs_private.first.prefilled).to eq(true)
|
|
|
|
end
|
2022-11-28 15:54:14 +01:00
|
|
|
end
|
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
context 'when a champ is invalid' do
|
|
|
|
let(:types_de_champ_public) { [{ type: :phone }] }
|
|
|
|
let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first }
|
|
|
|
let(:value) { "a non phone value" }
|
|
|
|
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
2022-11-28 15:54:14 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
let(:values) { [{ id: champ_id, value: value }] }
|
2022-11-28 15:54:14 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
it_behaves_like 'a dossier marked as prefilled'
|
|
|
|
|
|
|
|
it "still updates the champ" do
|
|
|
|
expect { fill }.to change { dossier.champs_public.first.value }.from(nil).to(value)
|
|
|
|
end
|
2023-01-03 14:46:10 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
it "still marks it as prefilled" do
|
|
|
|
expect { fill }.to change { dossier.champs_public.first.prefilled }.from(nil).to(true)
|
|
|
|
end
|
2022-11-28 15:54:14 +01:00
|
|
|
end
|
2023-09-07 10:03:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-18 16:24:55 +02:00
|
|
|
context "when dossier is for etablissement" do
|
|
|
|
let(:procedure) { create(:procedure, :published, types_de_champ_public:, types_de_champ_private:) }
|
|
|
|
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
|
|
|
|
|
|
|
|
context 'when champs_attributes has values' do
|
|
|
|
context 'when the champs are valid' do
|
|
|
|
let(:types_de_champ_public) { [{ type: :text }] }
|
|
|
|
let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first }
|
|
|
|
let(:value_1) { "any value" }
|
|
|
|
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
|
|
|
let(:values) { [{ id: champ_id_1, value: value_1 }] }
|
|
|
|
|
|
|
|
it "updates the champs with the new values and mark them as prefilled" do
|
|
|
|
fill
|
|
|
|
expect(dossier.champs_public.first.value).to eq(value_1)
|
|
|
|
expect(dossier.individual).to be_nil # Fix #9486
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a dossier marked as prefilled'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
private
|
2022-12-01 11:17:48 +01:00
|
|
|
|
2023-09-07 10:03:17 +02:00
|
|
|
def find_champ_by_stable_id(dossier, stable_id)
|
|
|
|
dossier.champs.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
|
|
|
end
|
2022-12-01 11:17:48 +01:00
|
|
|
end
|
2022-11-28 15:54:14 +01:00
|
|
|
end
|