From ea5340d71a55c0d0075c656f53ec2d78def57599 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 14 Nov 2024 14:13:58 +0100 Subject: [PATCH] fix(champ): do not validate champs with changed type --- .../concerns/champs_validate_concern.rb | 2 +- .../concerns/champs_validate_concern_spec.rb | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 spec/models/concerns/champs_validate_concern_spec.rb diff --git a/app/models/concerns/champs_validate_concern.rb b/app/models/concerns/champs_validate_concern.rb index ab5b2595b..ac8d0e1aa 100644 --- a/app/models/concerns/champs_validate_concern.rb +++ b/app/models/concerns/champs_validate_concern.rb @@ -29,7 +29,7 @@ module ChampsValidateConcern end def in_dossier_revision? - dossier.revision.types_de_champ.any? { _1.stable_id == stable_id } + dossier.revision.types_de_champ.any? { _1.stable_id == stable_id } && is_type?(type_de_champ.type_champ) end end end diff --git a/spec/models/concerns/champs_validate_concern_spec.rb b/spec/models/concerns/champs_validate_concern_spec.rb new file mode 100644 index 000000000..edf0e5224 --- /dev/null +++ b/spec/models/concerns/champs_validate_concern_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +RSpec.describe ChampsValidateConcern do + let(:procedure) { create(:procedure, :published, types_de_champ_public:) } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + let(:type_de_champ) { dossier.revision.types_de_champ_public.first } + + let(:types_de_champ_public) { [{ type: :email }] } + + def update_champ(value) + dossier.update_champs_attributes({ + type_de_champ.stable_id.to_s => { value: } + }, :public, updated_by: 'test') + dossier.save + end + + context 'when in revision' do + context 'valid' do + before { + update_champ('test@test.com') + dossier.validate(:champs_public_value) + } + it { + expect(dossier.champs).not_to be_empty + expect(dossier.errors).to be_empty + } + end + + context 'invalid' do + before { + update_champ('test') + dossier.validate(:champs_public_value) + } + it { + expect(dossier.champs).not_to be_empty + expect(dossier.errors).not_to be_empty + } + end + end + + context 'when not in revision' do + context 'do not validate champs not on current revision' do + before { + update_champ('test') + dossier.revision.revision_types_de_champ.delete_all + dossier.validate(:champs_public_value) + } + it { + expect(dossier.revision.revision_types_de_champ).to be_empty + expect(dossier.champs).not_to be_empty + expect(dossier.errors).to be_empty + } + end + end + + context 'when type changed' do + context 'do not validate with old champ type' do + before { + update_champ('test') + type_de_champ.update(type_champ: :text) + dossier.validate(:champs_public_value) + } + it { + expect(dossier.champs.first.last_write_type_champ).to eq('email') + expect(type_de_champ.type_champ).to eq('text') + expect(dossier.champs).not_to be_empty + expect(dossier.errors).to be_empty + } + end + end +end