diff --git a/Gemfile.lock b/Gemfile.lock index 754c8ec6d..4d6cf10b2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -454,7 +454,7 @@ GEM ast (~> 2.4.1) pdf-core (0.7.0) pg (1.2.3) - phonelib (0.6.43) + phonelib (0.6.45) prawn (2.2.2) pdf-core (~> 0.7.0) ttfunk (~> 1.5) diff --git a/app/models/champs/phone_champ.rb b/app/models/champs/phone_champ.rb index 69099d145..bbcc7691f 100644 --- a/app/models/champs/phone_champ.rb +++ b/app/models/champs/phone_champ.rb @@ -20,5 +20,5 @@ class Champs::PhoneChamp < Champs::TextChamp possible: true, allow_blank: true, message: I18n.t(:not_a_phone, scope: 'activerecord.errors.messages') - } + }, unless: -> { Phonelib.valid_for_country?(value, :pf) } end diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index e8a0ac290..e102e7076 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -698,6 +698,43 @@ describe Users::DossiersController, type: :controller do expect(instructeur.reload.followed_dossiers.with_notifications(instructeur)).to eq([dossier.reload]) end end + + context 'when the champ is a phone number' do + let(:procedure) { create(:procedure, :published, :with_phone) } + let!(:dossier) { create(:dossier, :en_construction, user: user, procedure: procedure) } + let(:first_champ) { dossier.champs.first } + let(:now) { Time.zone.parse('01/01/2100') } + + let(:submit_payload) do + { + id: dossier.id, + dossier: { + champs_attributes: [ + { + id: first_champ.id, + value: value + } + ] + } + } + end + + context 'with a valid value sent as string' do + let(:value) { '0612345678' } + it 'updates the value' do + subject + expect(first_champ.reload.value).to eq('0612345678') + end + end + + context 'with a valid value sent as number' do + let(:value) { '45187272'.to_i } + it 'updates the value' do + subject + expect(first_champ.reload.value).to eq('45187272') + end + end + end end describe '#index' do diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 4466b1bde..3e05e2b55 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -182,6 +182,12 @@ FactoryBot.define do end end + trait :with_phone do + after(:build) do |procedure, _evaluator| + build(:type_de_champ_phone, procedure: procedure) + end + end + trait :published do after(:build) do |procedure, _evaluator| procedure.path = generate(:published_path) diff --git a/spec/models/champs/phone_champ_spec.rb b/spec/models/champs/phone_champ_spec.rb index 0da741ab9..e69356e41 100644 --- a/spec/models/champs/phone_champ_spec.rb +++ b/spec/models/champs/phone_champ_spec.rb @@ -20,6 +20,19 @@ describe Champs::PhoneChamp do expect(build(:champ_phone, value: "+1(0) - 123456789")).to be_valid expect(build(:champ_phone, value: "+49 2109 87654321")).to be_valid expect(build(:champ_phone, value: "012345678")).to be_valid + # polynesian numbers should not return errors in any way + ## landline numbers start with 40 or 45 + expect(build(:champ_phone, value: "45187272")).to be_valid + expect(build(:champ_phone, value: "40 473 500")).to be_valid + expect(build(:champ_phone, value: "40473500")).to be_valid + expect(build(:champ_phone, value: "45473500")).to be_valid + ## +689 is the international indicator + expect(build(:champ_phone, value: "+689 45473500")).to be_valid + expect(build(:champ_phone, value: "0145473500")).to be_valid + ## polynesian mobile numbers start with 87, 88, 89 + expect(build(:champ_phone, value: "87473500")).to be_valid + expect(build(:champ_phone, value: "88473500")).to be_valid + expect(build(:champ_phone, value: "89473500")).to be_valid end end end