Fix datetime_champ validation with negative time zone

This commit is contained in:
Christian Lautier 2023-07-05 14:53:57 -10:00 committed by LeSim
parent bcb84d4070
commit 5ce42afe28
6 changed files with 14 additions and 12 deletions

View file

@ -44,6 +44,6 @@ class Champs::DatetimeChamp < Champ
end
def valid_iso8601?
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}\+\d{2}:\d{2})?$/.match?(value)
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}[\+\-]\d{2}:\d{2})?$/.match?(value)
end
end

View file

@ -949,7 +949,7 @@ describe Instructeurs::DossiersController, type: :controller do
expect(champ_multiple_drop_down_list.value).to eq('["val1","val2"]')
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('2019-12-21T13:17:00+01:00')
expect(champ_datetime.value).to eq(Time.zone.parse('2019-12-21T13:17:00').iso8601)
expect(champ_repetition.champs.first.value).to eq('text')
expect(champ_drop_down_list.value).to eq('other value')
expect(dossier.reload.last_champ_private_updated_at).to eq(now)

View file

@ -4,24 +4,26 @@ describe Migrations::BatchUpdateDatetimeValuesJob, type: :job do
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") }
let!(:value) { Time.zone.parse('10/01/2023 13:30').iso8601 }
let!(:datetime_champ) { build(:champ_datetime, value: value) }
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")
expect(datetime_champ.reload.value).to eq(value)
end
end
context "when the value is a date convertible to IS8061" do
let!(:datetime_champ) { build(:champ_datetime, value: "2023-01-10") }
let!(:value) { "2023-01-10" }
let!(:datetime_champ) { build(:champ_datetime, value: value) }
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")
expect(datetime_champ.reload.value).to eq(Time.zone.parse(value).iso8601)
end
end

View file

@ -99,13 +99,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("2017-12-31T10:23:00+01:00") }
it { expect(champ.value).to eq(Time.zone.parse("2017-12-31T10:23:00").iso8601) }
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-31T09:26:00+01:00") }
it { expect(champ.value).to eq(Time.zone.parse("2018-12-31T09:26:00").iso8601) }
end
end

View file

@ -29,19 +29,19 @@ describe Champs::DatetimeChamp do
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")
expect(champ.reload.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
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")
expect(champ.reload.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
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")
expect(champ.reload.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
end
end

View file

@ -54,7 +54,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('2023-01-06T07:05:00+01:00')
expect(champ_value_for('datetime')).to eq(Time.zone.parse('2023-01-06T07:05:00').iso8601)
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')