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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex fe7065f13fd..869a428f392 100644--- a/app/contracts/work_packages/base_contract.rb+++ b/app/contracts/work_packages/base_contract.rb@@ -484,6 +484,7 @@ module WorkPackagesdef validate_duration_integerunless valid_duration?(model.duration_before_type_cast, model.duration)+ errors.delete(:duration) # delete :greater_than error, because it's not relevant anymoreerrors.add :duration, :not_an_integerendend@@ -496,7 +497,7 @@ module WorkPackagesreturn 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.stripend# duration is valid@@ -520,6 +521,8 @@ module WorkPackagesenddef 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 WorkPackagesendend+ 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.rbindex 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::SetAttributesdays.duration(work_package.start_date, work_package.due_date)endwhen :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)endend-# 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_priorityset_default_authordiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 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.rbindex 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 doenddescribe "duration" do- context "when setting duration" do+ context "when setting duration to a positive integer value" dobefore dowork_package.duration = 5end- 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"endcontext "when setting duration for a milestone type work package" do@@ -931,7 +939,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.duration = 5end- it_behaves_like "contract is invalid", duration: :not_available_for_milestones+ include_examples "contract is invalid", duration: :not_available_for_milestonesendcontext "when setting duration to nil for a milestone type work package" do@@ -941,7 +949,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.duration = nilend- it_behaves_like "contract is invalid"+ include_examples "contract is invalid"endcontext "when setting duration to 1 for a milestone type work package" do@@ -951,7 +959,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.duration = 1end- it_behaves_like "contract is valid"+ include_examples "contract is valid"endcontext "when setting duration to 0" do@@ -959,7 +967,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.duration = 0end- it_behaves_like "contract is invalid", duration: :greater_than+ include_examples "contract is invalid", duration: :greater_thanendcontext "when setting duration to a floating point" do@@ -967,7 +975,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.duration = 4.5end- it_behaves_like "contract is invalid", duration: :not_an_integer+ include_examples "contract is invalid", duration: :not_an_integerendcontext "when setting duration to a negative value" do@@ -975,7 +983,33 @@ RSpec.describe WorkPackages::BaseContract dowork_package.duration = -5end- 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+ endendcontext "when setting duration and dates" do@@ -986,7 +1020,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.due_date = Time.zone.today + 1.dayend- it_behaves_like "contract is valid"+ include_examples "contract is valid"endcontext "when setting duration and dates while covering non-working days" do@@ -998,7 +1032,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.due_date = "2022-08-29"end- it_behaves_like "contract is valid"+ include_examples "contract is valid"endcontext "when setting duration and dates and duration is too small" do@@ -1009,7 +1043,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.due_date = Time.zone.today + 1.dayend- it_behaves_like "contract is invalid", duration: :smaller_than_dates+ include_examples "contract is invalid", duration: :smaller_than_datesendcontext "when setting duration and dates while covering non-working days and duration is too small" do@@ -1021,7 +1055,7 @@ RSpec.describe WorkPackages::BaseContract dowork_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_datesendcontext "when setting duration and dates and duration is too big" do@@ -1032,7 +1066,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.due_date = Time.zone.today + 1.dayend- it_behaves_like "contract is invalid", duration: :larger_than_dates+ include_examples "contract is invalid", duration: :larger_than_datesendcontext "when setting duration and dates while covering non-working days and duration is too big" do@@ -1044,7 +1078,7 @@ RSpec.describe WorkPackages::BaseContract dowork_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_datesendcontext "when setting start date and due date without duration" do@@ -1054,7 +1088,7 @@ RSpec.describe WorkPackages::BaseContract dowork_package.due_date = Time.zone.todayend- it_behaves_like "contract is invalid", duration: :cannot_be_null+ include_examples "contract is invalid", duration: :cannot_be_nullendenddiff --git a/spec/features/work_packages/details/date_editor_spec.rb b/spec/features/work_packages/details/date_editor_spec.rbindex 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_formstart_date.datepicker.expect_visiblestart_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 nilendit "saves the date when clearing and then confirming" dodiff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rbindex 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,endend+ 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/LineLengthall_possible_scenarios = [diff --git a/spec/support/components/datepicker/work_package_datepicker.rb b/spec/support/components/datepicker/work_package_datepicker.rbindex 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 Componentsexpect(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 manuallyexpect_manual_scheduling_mode@@ -167,5 +179,25 @@ module Componentsdef clear_durationset_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) }+ endendend