Make date and datetime prefillable (#8304)

* Make date and datetime prefillable

* Format in ISO8601 format
This commit is contained in:
Damien Le Thiec 2023-01-12 17:42:02 +01:00 committed by GitHub
parent d5bfe84381
commit 5e26acb0e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 268 additions and 46 deletions

View file

@ -805,7 +805,7 @@ describe Instructeurs::DossiersController, type: :controller do
expect(champ_multiple_drop_down_list.value).to eq('["un", "deux"]')
expect(champ_linked_drop_down_list.primary_value).to eq('primary')
expect(champ_linked_drop_down_list.secondary_value).to eq('secondary')
expect(champ_datetime.value).to eq('21/12/2019 13:17')
expect(champ_datetime.value).to eq('2019-12-21T13:17:00+01:00')
expect(champ_repetition.champs.first.value).to eq('text')
expect(dossier.reload.last_champ_private_updated_at).to eq(now)
expect(response).to redirect_to(annotations_privees_instructeur_dossier_path(dossier.procedure, dossier))

View file

@ -0,0 +1,63 @@
describe Migrations::BatchUpdateDatetimeValuesJob, type: :job do
before do
datetime_champ.save(validate: false)
end
context "when the value is a valid ISO8601 date" do
let!(:datetime_champ) { build(:champ_datetime, value: "2023-01-10T00:00:00+01:00") }
subject { described_class.perform_now([datetime_champ.id]) }
it "keeps the existing value" do
subject
expect(datetime_champ.reload.value).to eq("2023-01-10T00:00:00+01:00")
end
end
context "when the value is a date convertible to IS8061" do
let!(:datetime_champ) { build(:champ_datetime, value: "2023-01-10") }
subject { described_class.perform_now([datetime_champ.id]) }
it "updates the value to ISO8601" do
subject
expect(datetime_champ.reload.value).to eq("2023-01-10T00:00:00+01:00")
end
end
context "when the value is a date not convertible to IS8061" do
let!(:datetime_champ) { build(:champ_datetime, value: "blabla") }
subject { described_class.perform_now([datetime_champ.id]) }
it "updates the value to nil" do
subject
expect(datetime_champ.reload.value).to be_nil
end
end
context "when the value is a date not convertible to IS8061 and the champ is required" do
let!(:datetime_champ) { build(:champ_datetime, value: "blabla") }
subject { described_class.perform_now([datetime_champ.id]) }
it "keeps the existing value" do
allow_any_instance_of(Champs::DatetimeChamp).to receive(:required?).and_return(true)
subject
expect(datetime_champ.reload.value).to eq("blabla")
end
end
context "when the value is nil" do
let!(:datetime_champ) { build(:champ_datetime, value: nil) }
subject { described_class.perform_now([datetime_champ.id]) }
it "keeps the value to nil" do
subject
expect(datetime_champ.reload.value).to be_nil
end
end
end

View file

@ -53,29 +53,4 @@ shared_examples 'champ_spec' do
it { expect(champ.mandatory_blank?).to be(false) }
end
end
context "when type_champ=date" do
let(:champ) { build(:champ_date) }
it "should convert %d/%m/%Y format to ISO" do
champ.value = "31/12/2017"
champ.save
champ.reload
expect(champ.value).to eq("2017-12-31")
end
it "should convert to nil if date parse failed" do
champ.value = "bla"
champ.save
champ.reload
expect(champ.value).to be(nil)
end
it "should convert empty string to nil" do
champ.value = ""
champ.save
champ.reload
expect(champ.value).to be(nil)
end
end
end

View file

@ -108,13 +108,13 @@ describe Champ do
context 'when the value is sent by a modern browser' do
let(:value) { '2017-12-31 10:23' }
it { expect(champ.value).to eq(value) }
it { expect(champ.value).to eq("2017-12-31T10:23:00+01:00") }
end
context 'when the value is sent by a old browser' do
let(:value) { '31/12/2018 09:26' }
it { expect(champ.value).to eq('2018-12-31 09:26') }
it { expect(champ.value).to eq("2018-12-31T09:26:00+01:00") }
end
end

View file

@ -0,0 +1,45 @@
describe Champs::DateChamp do
let(:date_champ) { build(:champ_date) }
describe '#convert_to_iso8601' do
it 'preserves nil' do
champ = champ_with_value(nil)
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if empty string' do
champ = champ_with_value("")
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if not ISO8601' do
champ = champ_with_value("12-21-2023")
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if not date' do
champ = champ_with_value("value")
champ.save
expect(champ.reload.value).to be_nil
end
it "converts %d/%m/%Y format to ISO" do
champ = champ_with_value("31/12/2017")
champ.save
expect(champ.reload.value).to eq("2017-12-31")
end
it 'preserves if ISO8601' do
champ = champ_with_value("2023-12-21")
champ.save
expect(champ.reload.value).to eq("2023-12-21")
end
end
def champ_with_value(number)
date_champ.tap { |c| c.value = number }
end
end

View file

@ -0,0 +1,51 @@
describe Champs::DatetimeChamp do
let(:datetime_champ) { build(:champ_datetime) }
describe '#convert_to_iso8601' do
it 'preserves nil' do
champ = champ_with_value(nil)
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if empty string' do
champ = champ_with_value("")
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if not ISO8601' do
champ = champ_with_value("12-21-2023 03:20")
champ.save
expect(champ.reload.value).to be_nil
end
it 'converts to nil if not datetime' do
champ = champ_with_value("value")
champ.save
expect(champ.reload.value).to be_nil
end
it 'preserves if ISO8601' do
champ = champ_with_value("2023-12-21T03:20")
champ.save
expect(champ.reload.value).to eq("2023-12-21T03:20:00+01:00")
end
it 'converts to ISO8601 if form format' do
champ = champ_with_value("{3=>21, 2=>12, 1=>2023, 4=>3, 5=>20}")
champ.save
expect(champ.reload.value).to eq("2023-12-21T03:20:00+01:00")
end
it 'converts to ISO8601 if old browser form format' do
champ = champ_with_value("21/12/2023 03:20")
champ.save
expect(champ.reload.value).to eq("2023-12-21T03:20:00+01:00")
end
end
def champ_with_value(number)
datetime_champ.tap { |c| c.value = number }
end
end

View file

@ -273,7 +273,7 @@ describe Dossier do
})
procedure.draft_revision.remove_type_de_champ(yes_no_type_de_champ.stable_id)
datetime_champ.update(value: Date.today.to_s)
datetime_champ.update(value: Time.zone.now.to_s)
text_champ.update(value: 'bonjour')
# Add two rows then remove previous to last row in order to create a "hole" in the sequence
repetition_champ.add_row(repetition_champ.dossier.revision)

View file

@ -110,6 +110,8 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ public value that is authorized", :phone, "value"
it_behaves_like "a champ public value that is authorized", :iban, "value"
it_behaves_like "a champ public value that is authorized", :civilite, "M."
it_behaves_like "a champ public value that is authorized", :date, "2022-12-22"
it_behaves_like "a champ public value that is authorized", :datetime, "2022-12-22T10:30"
it_behaves_like "a champ public value that is authorized", :yes_no, "true"
it_behaves_like "a champ public value that is authorized", :yes_no, "false"
it_behaves_like "a champ public value that is authorized", :checkbox, "true"
@ -123,6 +125,8 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ private value that is authorized", :phone, "value"
it_behaves_like "a champ private value that is authorized", :iban, "value"
it_behaves_like "a champ private value that is authorized", :civilite, "M."
it_behaves_like "a champ private value that is authorized", :date, "2022-12-22"
it_behaves_like "a champ private value that is authorized", :datetime, "2022-12-22T10:30"
it_behaves_like "a champ private value that is authorized", :yes_no, "true"
it_behaves_like "a champ private value that is authorized", :yes_no, "false"
it_behaves_like "a champ private value that is authorized", :checkbox, "true"
@ -137,6 +141,7 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ public value that is unauthorized", :civilite, "value"
it_behaves_like "a champ public value that is unauthorized", :date, "value"
it_behaves_like "a champ public value that is unauthorized", :datetime, "value"
it_behaves_like "a champ public value that is unauthorized", :datetime, "12-22-2022T10:30"
it_behaves_like "a champ public value that is unauthorized", :drop_down_list, "value"
it_behaves_like "a champ public value that is unauthorized", :multiple_drop_down_list, "value"
it_behaves_like "a champ public value that is unauthorized", :linked_drop_down_list, "value"

View file

@ -242,6 +242,8 @@ describe TypeDeChamp do
it_behaves_like "a prefillable type de champ", :type_de_champ_email
it_behaves_like "a prefillable type de champ", :type_de_champ_phone
it_behaves_like "a prefillable type de champ", :type_de_champ_iban
it_behaves_like "a prefillable type de champ", :type_de_champ_date
it_behaves_like "a prefillable type de champ", :type_de_champ_datetime
it_behaves_like "a prefillable type de champ", :type_de_champ_civilite
it_behaves_like "a prefillable type de champ", :type_de_champ_yes_no
it_behaves_like "a prefillable type de champ", :type_de_champ_checkbox
@ -250,8 +252,6 @@ describe TypeDeChamp do
it_behaves_like "a non-prefillable type de champ", :type_de_champ_communes
it_behaves_like "a non-prefillable type de champ", :type_de_champ_dossier_link
it_behaves_like "a non-prefillable type de champ", :type_de_champ_titre_identite
it_behaves_like "a non-prefillable type de champ", :type_de_champ_date
it_behaves_like "a non-prefillable type de champ", :type_de_champ_datetime
it_behaves_like "a non-prefillable type de champ", :type_de_champ_drop_down_list
it_behaves_like "a non-prefillable type de champ", :type_de_champ_multiple_drop_down_list
it_behaves_like "a non-prefillable type de champ", :type_de_champ_linked_drop_down_list

View file

@ -57,7 +57,7 @@ describe 'The user' do
expect(champ_value_for('text')).to eq('super texte')
expect(champ_value_for('textarea')).to eq('super textarea')
expect(champ_value_for('date')).to eq('2012-12-12')
expect(champ_value_for('datetime')).to eq('06/01/2030 07:05')
expect(champ_value_for('datetime')).to eq('2030-01-06T07:05:00+01:00')
expect(champ_value_for('number')).to eq('42')
expect(champ_value_for('decimal_number')).to eq('17')
expect(champ_value_for('integer_number')).to eq('12')