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

Forbid having only 2 duration/dates fields set

Christophe Bliard0cf2ffedb935
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 90e537a41d8..92bc41331f4 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -145,6 +145,8 @@ module WorkPackages
validate :validate_duration_matches_dates
validate :validate_duration_constraint_for_milestone
+ validate :validate_duration_and_dates_are_not_derivable
+
def initialize(work_package, user, options: {})
super
@@ -336,7 +338,7 @@ module WorkPackages
end
def validate_duration_matches_dates
- return unless calculated_duration && model.duration
+ return unless calculated_duration && model.duration && OpenProject::FeatureDecisions.work_packages_duration_field_active?
if calculated_duration > model.duration
errors.add :duration, :smaller_than_dates
@@ -351,6 +353,21 @@ module WorkPackages
end
end
+ def validate_duration_and_dates_are_not_derivable
+ return unless OpenProject::FeatureDecisions.work_packages_duration_field_active?
+
+ %i[start_date due_date duration].each do |field|
+ if not_set_but_others_are_present?(field)
+ errors.add field, :cannot_be_null
+ end
+ end
+ 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?)
+ end
+
def readonly_attributes_unchanged
super.tap do
if already_in_readonly_status? && unauthenticated_changed.any?
@@ -463,9 +480,7 @@ module WorkPackages
end
def calculated_duration
- return nil unless model.due_date && model.start_date
-
- model.due_date - model.start_date + 1
+ @calculated_duration ||= WorkPackages::Shared::Days.for(model).duration(model.start_date, model.due_date)
end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index c5e12589a4d..b0a815d4e2e 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -808,10 +808,12 @@ en:
attributes:
due_date:
not_start_date: "is not on start date, although this is required for milestones."
+ cannot_be_null: "can not be set to null as start date and duration are known."
duration:
larger_than_dates: "is larger than the interval between the start and the finish date."
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."
parent:
cannot_be_milestone: "cannot be a milestone."
cannot_be_self_assigned: "cannot be assigned to itself."
@@ -819,6 +821,7 @@ en:
not_a_valid_parent: "is invalid."
start_date:
violates_relationships: "can only be set to %{soonest_start} or later so as not to violate the work package's relationships."
+ cannot_be_null: "can not be set to null as finish date and duration are known."
status_id:
status_transition_invalid: "is invalid because no valid transition exists from old to new status for the current user's roles."
status_invalid_in_type: "is invalid because the current status does not exist in this type."
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index 3fa64852949..5f9b4c8b6ee 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -450,6 +450,17 @@ describe WorkPackages::BaseContract do
it_behaves_like 'contract is valid'
end
end
+
+ context 'when setting due date and duration without start date',
+ with_flag: { work_packages_duration_field_active: true } do
+ before do
+ work_package.duration = 1
+ work_package.start_date = nil
+ work_package.due_date = Time.zone.today
+ end
+
+ it_behaves_like 'contract is invalid', start_date: :cannot_be_null
+ end
end
describe 'due date' do
@@ -502,6 +513,17 @@ describe WorkPackages::BaseContract do
it_behaves_like 'contract is valid'
end
end
+
+ context 'when setting start date and duration without due date',
+ with_flag: { work_packages_duration_field_active: true } do
+ before do
+ work_package.duration = 1
+ work_package.start_date = Time.zone.today
+ work_package.due_date = nil
+ end
+
+ it_behaves_like 'contract is invalid', due_date: :cannot_be_null
+ end
end
describe 'duration', with_flag: { work_packages_duration_field_active: true } do
@@ -614,6 +636,16 @@ describe WorkPackages::BaseContract do
it_behaves_like 'contract is valid'
end
+
+ context 'when setting start date and due date without duration' do
+ before do
+ work_package.duration = nil
+ work_package.start_date = Time.zone.today
+ work_package.due_date = Time.zone.today
+ end
+
+ it_behaves_like 'contract is invalid', duration: :cannot_be_null
+ end
end
describe 'ignore_non_working_days' do