Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Display error in date picker for invalid duration

Christophe Bliard366dc2dc1897
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index fe7065f13fd..869a428f392 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -484,6 +484,7 @@ module WorkPackages
def validate_duration_integer
unless valid_duration?(model.duration_before_type_cast, model.duration)
+ errors.delete(:duration) # delete :greater_than error, because it's not relevant anymore
errors.add :duration, :not_an_integer
end
end
@@ -496,7 +497,7 @@ module WorkPackages
return true if value == "" && duration.nil?
# A string is passed, put the transformed value does not match
- return false if value.to_i.to_s != value
+ return false if value.to_i.to_s != value.strip
end
# duration is valid
@@ -520,6 +521,8 @@ module WorkPackages
end
def validate_duration_and_dates_are_not_derivable
+ return if dates_derivation_impossible?
+
%i[start_date due_date duration].each do |field|
if not_set_but_others_are_present?(field)
errors.add field, :cannot_be_null
@@ -527,6 +530,10 @@ module WorkPackages
end
end
+ def dates_derivation_impossible?
+ model.errors[:duration].any?
+ end
+
def not_set_but_others_are_present?(field)
other_fields = %i[start_date due_date duration].without(field)
model[field].nil? && model.values_at(*other_fields).all?(&:present?)
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index b1aee6855d3..18f007234d5 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -155,14 +155,24 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
days.duration(work_package.start_date, work_package.due_date)
end
when :due_date
+ return if invalid_duration?
+
work_package.due_date = days.due_date(work_package.start_date, work_package.duration)
when :start_date
+ return if invalid_duration?
+
work_package.start_date = days.start_date(work_package.due_date, work_package.duration)
end
end
-
# rubocop:enable Metrics/AbcSize
+ def invalid_duration?
+ return false if work_package.duration.nil?
+ return true unless work_package.duration.is_a?(Integer)
+
+ work_package.duration <= 0
+ end
+
def set_default_attributes(attributes)
set_default_priority
set_default_author
diff --git a/config/locales/en.yml b/config/locales/en.yml
index e89a08b55ba..80885d5c0a2 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1433,6 +1433,7 @@ en:
smaller_than_dates: "is smaller than the interval between the start and the finish date."
not_available_for_milestones: "is not available for milestone typed work packages."
cannot_be_null: "can not be set to null as start date and finish date are known."
+ not_an_integer: "is not a valid duration."
parent:
cannot_be_milestone: "cannot be a milestone."
cannot_be_self_assigned: "cannot be assigned to itself."
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index 8c3315ca4ef..b96d5176c91 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -916,12 +916,20 @@ RSpec.describe WorkPackages::BaseContract do
end
describe "duration" do
- context "when setting duration" do
+ context "when setting duration to a positive integer value" do
before do
work_package.duration = 5
end
- it_behaves_like "contract is valid"
+ include_examples "contract is valid"
+ end
+
+ context "when setting duration to a positive integer value using a string padded with spaces" do
+ before do
+ work_package.duration = " 5 "
+ end
+
+ include_examples "contract is valid"
end
context "when setting duration for a milestone type work package" do
@@ -931,7 +939,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.duration = 5
end
- it_behaves_like "contract is invalid", duration: :not_available_for_milestones
+ include_examples "contract is invalid", duration: :not_available_for_milestones
end
context "when setting duration to nil for a milestone type work package" do
@@ -941,7 +949,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.duration = nil
end
- it_behaves_like "contract is invalid"
+ include_examples "contract is invalid"
end
context "when setting duration to 1 for a milestone type work package" do
@@ -951,7 +959,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.duration = 1
end
- it_behaves_like "contract is valid"
+ include_examples "contract is valid"
end
context "when setting duration to 0" do
@@ -959,7 +967,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.duration = 0
end
- it_behaves_like "contract is invalid", duration: :greater_than
+ include_examples "contract is invalid", duration: :greater_than
end
context "when setting duration to a floating point" do
@@ -967,7 +975,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.duration = 4.5
end
- it_behaves_like "contract is invalid", duration: :not_an_integer
+ include_examples "contract is invalid", duration: :not_an_integer
end
context "when setting duration to a negative value" do
@@ -975,7 +983,33 @@ RSpec.describe WorkPackages::BaseContract do
work_package.duration = -5
end
- it_behaves_like "contract is invalid", duration: :greater_than
+ include_examples "contract is invalid", duration: :greater_than
+ end
+
+ context "when setting duration to an invalid string" do
+ before do
+ work_package.duration = "I am invalid"
+ end
+
+ include_examples "contract is invalid", duration: :not_an_integer
+
+ context "and start date is set" do
+ before do
+ work_package.start_date = Date.current
+ end
+
+ # no extra errors on start and/or due dates
+ include_examples "contract is invalid", duration: :not_an_integer, start_date: nil, due_date: nil
+ end
+
+ context "and due date is set" do
+ before do
+ work_package.due_date = Date.current
+ end
+
+ # no extra errors on start and/or due dates
+ include_examples "contract is invalid", duration: :not_an_integer, start_date: nil, due_date: nil
+ end
end
context "when setting duration and dates" do
@@ -986,7 +1020,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.due_date = Time.zone.today + 1.day
end
- it_behaves_like "contract is valid"
+ include_examples "contract is valid"
end
context "when setting duration and dates while covering non-working days" do
@@ -998,7 +1032,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.due_date = "2022-08-29"
end
- it_behaves_like "contract is valid"
+ include_examples "contract is valid"
end
context "when setting duration and dates and duration is too small" do
@@ -1009,7 +1043,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.due_date = Time.zone.today + 1.day
end
- it_behaves_like "contract is invalid", duration: :smaller_than_dates
+ include_examples "contract is invalid", duration: :smaller_than_dates
end
context "when setting duration and dates while covering non-working days and duration is too small" do
@@ -1021,7 +1055,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.due_date = "2022-08-29"
end
- it_behaves_like "contract is invalid", duration: :smaller_than_dates
+ include_examples "contract is invalid", duration: :smaller_than_dates
end
context "when setting duration and dates and duration is too big" do
@@ -1032,7 +1066,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.due_date = Time.zone.today + 1.day
end
- it_behaves_like "contract is invalid", duration: :larger_than_dates
+ include_examples "contract is invalid", duration: :larger_than_dates
end
context "when setting duration and dates while covering non-working days and duration is too big" do
@@ -1044,7 +1078,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.due_date = "2022-08-29"
end
- it_behaves_like "contract is invalid", duration: :larger_than_dates
+ include_examples "contract is invalid", duration: :larger_than_dates
end
context "when setting start date and due date without duration" do
@@ -1054,7 +1088,7 @@ RSpec.describe WorkPackages::BaseContract do
work_package.due_date = Time.zone.today
end
- it_behaves_like "contract is invalid", duration: :cannot_be_null
+ include_examples "contract is invalid", duration: :cannot_be_null
end
end
diff --git a/spec/features/work_packages/details/date_editor_spec.rb b/spec/features/work_packages/details/date_editor_spec.rb
index e6914488115..8e23f9141aa 100644
--- a/spec/features/work_packages/details/date_editor_spec.rb
+++ b/spec/features/work_packages/details/date_editor_spec.rb
@@ -239,9 +239,14 @@ RSpec.describe "date inplace editor", :js, :selenium, with_settings: { date_form
start_date.datepicker.expect_visible
start_date.datepicker.set_duration -128
- start_date.datepicker.focus_start_date
-
- start_date.datepicker.expect_duration 128
+ start_date.datepicker.expect_duration_error "Must be greater than 0."
+ start_date.datepicker.expect_start_date_error nil
+ start_date.datepicker.expect_due_date_error nil
+
+ start_date.datepicker.set_duration "invalid"
+ start_date.datepicker.expect_duration_error "Is not a valid duration."
+ start_date.datepicker.expect_start_date_error nil
+ start_date.datepicker.expect_due_date_error nil
end
it "saves the date when clearing and then confirming" do
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 80a9eb61f9c..88b3f27290c 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1037,6 +1037,50 @@ RSpec.describe WorkPackages::SetAttributesService,
end
end
+ context "with negative duration" do
+ let(:work_package) do
+ build_stubbed(:work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days)
+ end
+ let(:call_attributes) { { duration: -1 } }
+ let(:expected_attributes) { {} }
+
+ it_behaves_like "service call" do
+ it "keeps the dates and duration values (error to be detected by contract)" do
+ subject
+
+ expect(work_package.start_date)
+ .to eq(Time.zone.today)
+ expect(work_package.due_date)
+ .to eq(Time.zone.today + 5.days)
+ expect(work_package.duration)
+ .to eq(-1)
+ end
+ end
+ end
+
+ context "with invalid duration (when the duration is text)" do
+ let(:work_package) do
+ build_stubbed(:work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days)
+ end
+ let(:call_attributes) { { duration: "I am invalid" } }
+ let(:expected_attributes) { {} }
+
+ it_behaves_like "service call" do
+ it "keeps the dates, sets duration to 0 but remembers the string that was used (error to be detected by contract)" do
+ subject
+
+ expect(work_package.start_date)
+ .to eq(Time.zone.today)
+ expect(work_package.due_date)
+ .to eq(Time.zone.today + 5.days)
+ expect(work_package.duration)
+ .to eq(0)
+ expect(work_package.duration_before_type_cast)
+ .to eq("I am invalid")
+ end
+ end
+ end
+
context "when deriving one value from the two others" do
# rubocop:disable Layout/ExtraSpacing, Layout/SpaceInsideArrayPercentLiteral, Layout/SpaceInsidePercentLiteralDelimiters, Layout/LineLength
all_possible_scenarios = [
diff --git a/spec/support/components/datepicker/work_package_datepicker.rb b/spec/support/components/datepicker/work_package_datepicker.rb
index d731f125047..6f8133409c4 100644
--- a/spec/support/components/datepicker/work_package_datepicker.rb
+++ b/spec/support/components/datepicker/work_package_datepicker.rb
@@ -116,6 +116,18 @@ module Components
expect(container).to have_css('[data-test-selector="op-datepicker-modal--duration-field"][data-qa-highlighted]')
end
+ def expect_start_date_error(expected_error)
+ expect_field_error(start_date_field, expected_error)
+ end
+
+ def expect_due_date_error(expected_error)
+ expect_field_error(due_date_field, expected_error)
+ end
+
+ def expect_duration_error(expected_error)
+ expect_field_error(duration_field, expected_error)
+ end
+
def expect_scheduling_mode(manually)
if manually
expect_manual_scheduling_mode
@@ -167,5 +179,25 @@ module Components
def clear_duration
set_duration("")
end
+
+ private
+
+ def expect_field_error(field, expected_error)
+ input_validation_element = input_aria_related_element(field, describedby: "validation")
+ if expected_error.nil?
+ expect(input_validation_element&.visible?)
+ .to be_falsey, "Expected no error message for #{field['name']} field, " \
+ "got \"#{input_validation_element&.text}\""
+ else
+ expect(input_validation_element).to have_text(expected_error)
+ end
+ end
+
+ def input_aria_related_element(input_element, describedby:)
+ input_element["aria-describedby"]
+ .split
+ .find { _1.start_with?("#{describedby}-") }
+ &.then { |id| find(id:, visible: :all) }
+ end
end
end