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

robustness upon an invalid wp parent_id

ulferts17d9388bb85b
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 92bc41331f4..336add5580f 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -252,15 +252,14 @@ module WorkPackages
end
def validate_parent_exists
- if model.parent.is_a?(WorkPackage::InexistentWorkPackage)
-
+ if model.parent.is_a?(WorkPackage::InexistentWorkPackage) ||
+ (model.parent_id && model.parent.nil?)
errors.add :parent, :does_not_exist
end
end
def validate_parent_not_self
if model.parent == model
-
errors.add :parent, :cannot_be_self_assigned
end
end
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 862734a01bf..c801922094d 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -345,7 +345,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
def new_start_date_from_parent
return unless work_package.parent_id_changed? &&
- work_package.parent_id
+ work_package.parent
work_package.parent.soonest_start
end
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index 6167ca731b9..38be97e26cd 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -835,6 +835,18 @@ describe WorkPackages::BaseContract do
.to include(:cant_link_a_work_package_with_a_descendant)
end
end
+
+ context 'when an invalid parent_id is set' do
+ before do
+ work_package.parent = nil
+ work_package.parent_id = -1
+ end
+
+ it 'is invalid' do
+ expect(subject)
+ .to include(:does_not_exist)
+ end
+ end
end
describe 'type' do
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 38b3eda92ee..405323ae536 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -565,6 +565,27 @@ describe WorkPackages::SetAttributesService,
end
end
end
+
+ context 'with providing a parent_id that is invalid' do
+ let(:call_attributes) { { parent_id: -1 } }
+ let(:work_package) { build_stubbed(:work_package, start_date: Time.zone.today, due_date: Time.zone.today + 2.days) }
+
+ it_behaves_like 'service call' do
+ it "sets the start_date to the parent`s start_date" do
+ subject
+
+ expect(work_package.start_date)
+ .to eql Time.zone.today
+ end
+
+ it "sets the due_date to the parent`s due_date" do
+ subject
+
+ expect(work_package.due_date)
+ .to eql Time.zone.today + 2.days
+ end
+ end
+ end
end
context 'with no value set for a new work package and with default setting active',