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

[44900] Set due date to start date for new milestone work packages (#11650)

Christophe Bliard41673971c618
diff --git a/.rubocop.yml b/.rubocop.yml
index 668407da28e..4016ed01383 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -222,13 +222,14 @@ RSpec/NamedSubject:
RSpec/ContextWording:
Prefixes:
- as
+ - for
+ - if
- 'on'
+ - to
+ - unless
- when
- with
- without
- - if
- - unless
- - for
RSpec/FactoryBot/SyntaxMethods:
Enabled: true
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index d29b08fab1d..9d7aab4b1b8 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -53,6 +53,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
def set_calculated_attributes(attributes)
if work_package.new_record?
set_default_attributes(attributes)
+ unify_milestone_dates
else
update_dates
end
@@ -172,9 +173,9 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
end
def set_default_start_date(attributes)
- work_package.start_date ||= if attributes.has_key?(:start_date)
- nil
- elsif parent_start_earlier_than_due?
+ return if attributes.has_key?(:start_date)
+
+ work_package.start_date ||= if parent_start_earlier_than_due?
work_package.parent.start_date
elsif Setting.work_package_startdate_is_adddate?
Time.zone.today
@@ -182,9 +183,9 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
end
def set_default_due_date(attributes)
- work_package.due_date ||= if attributes.has_key?(:due_date)
- nil
- elsif parent_due_later_than_start?
+ return if attributes.has_key?(:due_date)
+
+ work_package.due_date ||= if parent_due_later_than_start?
work_package.parent.due_date
end
end
@@ -249,7 +250,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
end
def update_dates
- unify_dates if work_package_now_milestone?
+ unify_milestone_dates
min_start = new_start_date
@@ -259,7 +260,9 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
work_package.start_date = min_start
end
- def unify_dates
+ def unify_milestone_dates
+ return unless work_package_now_milestone?
+
unified_date = work_package.due_date || work_package.start_date
work_package.start_date = work_package.due_date = unified_date
end
diff --git a/modules/team_planner/spec/support/pages/team_planner.rb b/modules/team_planner/spec/support/pages/team_planner.rb
index cb8763461c4..cf066d8a751 100644
--- a/modules/team_planner/spec/support/pages/team_planner.rb
+++ b/modules/team_planner/spec/support/pages/team_planner.rb
@@ -86,8 +86,10 @@ module Pages
def switch_view_mode(text)
find('[data-qa-selector="op-team-planner--view-select-dropdown"]').click
- within('#op-team-planner--view-select-dropdown') do
- click_button(text)
+ retry_block do
+ within('#op-team-planner--view-select-dropdown') do
+ click_button(text)
+ end
end
expect_view_mode(text)
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 73c621c2398..7b28f4867a3 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1295,17 +1295,13 @@ describe WorkPackages::SetAttributesService,
end
context 'when switching the type' do
- let(:target_type) { build_stubbed(:type) }
+ let(:target_type) { build_stubbed(:type, is_milestone:) }
let(:work_package) do
build_stubbed(:work_package, start_date: Time.zone.today - 6.days, due_date: Time.zone.today)
end
- context 'with a type that is no milestone' do
- before do
- allow(target_type)
- .to receive(:is_milestone?)
- .and_return(false)
- end
+ context 'to a non-milestone type' do
+ let(:is_milestone) { false }
it 'keeps the start date' do
instance.call(type: target_type)
@@ -1328,59 +1324,74 @@ describe WorkPackages::SetAttributesService,
end
end
- context 'with a type that is a milestone and with both dates set' do
- before do
- allow(target_type)
- .to receive(:is_milestone?)
- .and_return(true)
- end
+ context 'to a milestone type' do
+ let(:is_milestone) { true }
- it 'sets the start date to the due date' do
- instance.call(type: target_type)
+ context 'with both dates set' do
+ it 'sets the start date to the due date' do
+ instance.call(type: target_type)
- expect(work_package.start_date).to eql Time.zone.today
- end
+ expect(work_package.start_date).to eq work_package.due_date
+ end
- it 'keeps the due date' do
- instance.call(type: target_type)
+ it 'keeps the due date' do
+ instance.call(type: target_type)
- expect(work_package.due_date).to eql Time.zone.today
- end
+ expect(work_package.due_date).to eql Time.zone.today
+ end
- it 'sets the duration to 1 (to be changed to 0 later on)' do
- instance.call(type: target_type)
+ it 'sets the duration to 1 (to be changed to 0 later on)' do
+ instance.call(type: target_type)
- expect(work_package.duration).to eq 1
+ expect(work_package.duration).to eq 1
+ end
end
- end
- context 'with a type that is a milestone and with only the start date set' do
- let(:work_package) do
- build_stubbed(:work_package, start_date: Time.zone.today - 6.days)
- end
+ context 'with only the start date set' do
+ let(:work_package) do
+ build_stubbed(:work_package, start_date: Time.zone.today - 6.days)
+ end
- before do
- allow(target_type)
- .to receive(:is_milestone?)
- .and_return(true)
- end
+ it 'keeps the start date' do
+ instance.call(type: target_type)
- it 'keeps the start date' do
- instance.call(type: target_type)
+ expect(work_package.start_date).to eql Time.zone.today - 6.days
+ end
- expect(work_package.start_date).to eql Time.zone.today - 6.days
- end
+ it 'set the due date to the start date' do
+ instance.call(type: target_type)
- it 'set the due date to the start date' do
- instance.call(type: target_type)
+ expect(work_package.due_date).to eql work_package.start_date
+ end
- expect(work_package.due_date).to eql Time.zone.today - 6.days
- end
+ it 'keeps the duration at 1 (to be changed to 0 later on)' do
+ instance.call(type: target_type)
- it 'keeps the duration at 1 (to be changed to 0 later on)' do
- instance.call(type: target_type)
+ expect(work_package.duration).to eq 1
+ end
- expect(work_package.duration).to eq 1
+ context 'with a new work package' do
+ let(:work_package) do
+ build(:work_package, start_date: Time.zone.today - 6.days)
+ end
+ let(:call_attributes) { { type: target_type, start_date: Time.zone.today - 6.days, due_date: nil, duration: nil } }
+
+ before do
+ instance.call(call_attributes)
+ end
+
+ it 'keeps the start date' do
+ expect(work_package.start_date).to eq Time.zone.today - 6.days
+ end
+
+ it 'set the due date to the start date' do
+ expect(work_package.due_date).to eq work_package.start_date
+ end
+
+ it 'keeps the duration at 1 (to be changed to 0 later on)' do
+ expect(work_package.duration).to eq 1
+ end
+ end
end
end
end