From b72072f1406a253c84caf4ee164e74e560200dee Mon Sep 17 00:00:00 2001 From: Holger Just Date: Tue, 19 Sep 2023 15:03:07 +0200 Subject: [PATCH] Avoid exception during validation if the spent_on date of a time entry was removed The separate `validates_presence_of :spent_on` rule still ensures that the date was provided. --- app/models/time_entry.rb | 2 +- test/unit/time_entry_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index b327f861f3..38504b7740 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -179,7 +179,7 @@ def validate_time_entry end errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity) - if spent_on_changed? && user + if spent_on && spent_on_changed? && user errors.add :base, I18n.t(:error_spent_on_future_date) if !Setting.timelog_accept_future_dates? && (spent_on > user.today) end end diff --git a/test/unit/time_entry_test.rb b/test/unit/time_entry_test.rb index 7f992e9709..e1013afd72 100644 --- a/test/unit/time_entry_test.rb +++ b/test/unit/time_entry_test.rb @@ -171,6 +171,16 @@ def test_should_not_accept_future_dates_if_disabled end end + def test_should_require_spent_on + with_settings :timelog_accept_future_dates => '0' do + entry = TimeEntry.find(1) + entry.spent_on = '' + + assert !entry.save + assert entry.errors[:spent_on].present? + end + end + def test_spent_on_with_blank c = TimeEntry.new c.spent_on = '' -- 2.39.2