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

Correctly calculate parent dates when switching to automatic scheduling

Christophe Bliarde30808a1b6e8
diff --git a/app/services/work_packages/get_rescheduled_children_dates_service.rb b/app/services/work_packages/get_rescheduled_children_dates_service.rb
index 7c5ef3ae43b..2aee61808ed 100644
--- a/app/services/work_packages/get_rescheduled_children_dates_service.rb
+++ b/app/services/work_packages/get_rescheduled_children_dates_service.rb
@@ -48,8 +48,8 @@ class WorkPackages::GetRescheduledChildrenDatesService
end
children = @dependencies.children_by_parent_id(work_package.id)
- start_date = children.min_by(&:start_date).start_date
- due_date = children.max_by(&:due_date).due_date
+ dates = children.pluck(:start_date, :due_date).flatten.compact
+ start_date, due_date = dates.minmax
ServiceResult.success(result: ParentDates.new(start_date:, due_date:))
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 88b3f27290c..4d234b34e7d 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1844,7 +1844,7 @@ RSpec.describe WorkPackages::SetAttributesService,
end
end
- context "when switching back to automatic scheduling" do
+ context "when switching back to automatic scheduling for a successor" do
shared_let(:project) { create(:project) }
let(:predecessor) do
create(:work_package,
@@ -1988,6 +1988,45 @@ RSpec.describe WorkPackages::SetAttributesService,
end
end
+ context "when switching back to automatic scheduling for a parent" do
+ shared_let(:project) { create(:project) }
+ let!(:work_package) do
+ create(:work_package,
+ subject: "work_package",
+ project:,
+ ignore_non_working_days: true,
+ schedule_manually: true,
+ start_date: Time.zone.today,
+ due_date: Time.zone.today + 5.days)
+ end
+ let!(:child) do
+ create(:work_package,
+ subject: "child",
+ project:,
+ parent: work_package,
+ ignore_non_working_days: true,
+ schedule_manually: true,
+ start_date: child_start_date,
+ due_date: child_due_date)
+ end
+ let(:call_attributes) { { schedule_manually: false } }
+ let(:expected_attributes) { {} }
+
+ context "when the child has dates" do
+ let(:child_start_date) { Time.zone.today + 2.days }
+ let(:child_due_date) { Time.zone.today + 3.days }
+
+ include_examples "service call", description: "sets the parent dates to the child dates" do
+ let(:expected_attributes) do
+ {
+ start_date: child_start_date,
+ due_date: child_due_date
+ }
+ end
+ end
+ end
+ end
+
context "when the type defines a pattern for an attribute" do
let(:type) { build_stubbed(:type, patterns: { subject: { blueprint: "{{type}} {{project_name}}", enabled: true } }) }
let(:work_package) { WorkPackage.new(type:) }
diff --git a/spec/services/work_packages/update_service_integration_spec.rb b/spec/services/work_packages/update_service_integration_spec.rb
index c68d5ecb836..77f18bead0d 100644
--- a/spec/services/work_packages/update_service_integration_spec.rb
+++ b/spec/services/work_packages/update_service_integration_spec.rb
@@ -1065,8 +1065,8 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
TABLE
let(:attributes) { { parent: new_parent } }
- it "sets work package and child start date to be soonest start, " \
- "and parent's start and due dates to be work package start date" do
+ it "sets child start date to be soonest start, " \
+ "and parent and work package start and due dates to be child start date" do
expect(subject).to be_success
expect(work_package.reload.parent).to eq new_parent
expect(subject.all_results.map(&:subject)).to contain_exactly("child", "work_package", "new_parent")
@@ -1075,7 +1075,7 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
subject | MTWTFSS | scheduling mode
new_parent_predecessor | XX | manual
new_parent | X | automatic
- work_package | [ | automatic
+ work_package | X | automatic
child | [ | automatic
TABLE
end
@@ -1155,6 +1155,90 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
TABLE
end
end
+
+ context "when the work package has two children with dates" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode
+ | work_package | XXX | manual
+ | child1 | X | manual
+ | child2 | | manual
+ | child3 | XX | manual
+ TABLE
+
+ it "sets the parent start and due dates to the children earliest and latest dates" do
+ expect(subject).to be_success
+ expect(subject.all_results).to contain_exactly(work_package)
+
+ expect_work_packages_after_reload([work_package, child1, child2, child3], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | work_package | XXXX | automatic
+ | child1 | X | manual
+ | child2 | | manual
+ | child3 | XX | manual
+ TABLE
+ end
+ end
+
+ context "when the work package has two children with start dates only (no due dates)" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode
+ | work_package | XXX | manual
+ | child1 | [ | manual
+ | child2 | [ | manual
+ TABLE
+
+ it "sets the parent start and due dates to the children earliest and latest start dates" do
+ expect(subject).to be_success
+ expect(subject.all_results).to contain_exactly(work_package)
+
+ expect_work_packages_after_reload([work_package, child1, child2], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | work_package | XXX | automatic
+ | child1 | [ | manual
+ | child2 | [ | manual
+ TABLE
+ end
+ end
+
+ context "when the work package has two children with due dates only (no start dates)" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode
+ | work_package | XXX | manual
+ | child1 | ] | manual
+ | child2 | ] | manual
+ TABLE
+
+ it "sets the parent start and due dates to the children earliest and latest due dates" do
+ expect(subject).to be_success
+ expect(subject.all_results).to contain_exactly(work_package)
+
+ expect_work_packages_after_reload([work_package, child1, child2], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | work_package | XXXX | automatic
+ | child1 | ] | manual
+ | child2 | ] | manual
+ TABLE
+ end
+ end
+
+ context "when the work package has one child without dates" do
+ let_work_packages(<<~TABLE)
+ | hierarchy | MTWTFSS | scheduling mode
+ | work_package | XXX | manual
+ | child | | manual
+ TABLE
+
+ it "clears the parent dates" do
+ expect(subject).to be_success
+ expect(subject.all_results).to contain_exactly(work_package)
+
+ expect_work_packages_after_reload([work_package, child], <<~TABLE)
+ | subject | MTWTFSS | scheduling mode
+ | work_package | | automatic
+ | child | | manual
+ TABLE
+ end
+ end
end
context "when setting dates" do