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

use default update service for wps

ulfertsb1e453d662cd
diff --git a/app/services/attachments/replace_attachments.rb b/app/services/attachments/replace_attachments.rb
index 3ae3f2bec97..3f7f2f26de6 100644
--- a/app/services/attachments/replace_attachments.rb
+++ b/app/services/attachments/replace_attachments.rb
@@ -34,13 +34,11 @@ module Attachments
private
def set_attributes(attributes)
- call = super
-
- if call.success? && call.result.attachments_replacements
- call.result.attachments = call.result.attachments_replacements
+ super.tap do |call|
+ if call.success? && call.result.attachments_replacements
+ call.result.attachments = call.result.attachments_replacements
+ end
end
-
- call
end
end
end
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 77afb91e0b6..ef1994a0ea1 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -90,7 +90,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
elsif parent_start_earlier_than_due?
work_package.parent.start_date
elsif Setting.work_package_startdate_is_adddate?
- Date.today
+ Time.zone.today
end
end
@@ -111,7 +111,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
# And the new type has a default text
default_description = work_package.type&.description
- return unless default_description.present?
+ return if default_description.blank?
# And the current description matches ANY current default text
return unless work_package.description.blank? || default_description?
@@ -155,6 +155,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
model.change_by_system do
set_version_to_nil
reassign_category
+ set_parent_to_nil
reassign_type unless work_package.type_id_changed?
end
@@ -194,6 +195,14 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
end
end
+ def set_parent_to_nil
+ if !Setting.cross_project_work_package_relations? &&
+ !work_package.parent_changed?
+
+ work_package.parent = nil
+ end
+ end
+
def reassign_category
# work_package is moved to another project
# reassign to the category with same name if any
diff --git a/app/services/work_packages/set_schedule_service.rb b/app/services/work_packages/set_schedule_service.rb
index dc0d0df4b2f..1c8d5e6f9d1 100644
--- a/app/services/work_packages/set_schedule_service.rb
+++ b/app/services/work_packages/set_schedule_service.rb
@@ -176,9 +176,9 @@ class WorkPackages::SetScheduleService
def date_rescheduling_delta(predecessor)
if predecessor.due_date.present?
- predecessor.due_date - (predecessor.due_date_was || predecessor.due_date)
+ predecessor.due_date - (predecessor.due_date_before_last_save || predecessor.due_date_was || predecessor.due_date)
elsif predecessor.start_date.present?
- predecessor.start_date - (predecessor.start_date_was || predecessor.start_date)
+ predecessor.start_date - (predecessor.start_date_before_last_save || predecessor.start_date_was || predecessor.start_date)
else
0
end
diff --git a/app/services/work_packages/update_service.rb b/app/services/work_packages/update_service.rb
index 4fc505c2693..33ef46869f3 100644
--- a/app/services/work_packages/update_service.rb
+++ b/app/services/work_packages/update_service.rb
@@ -26,97 +26,63 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-# TODO: use default update base class
-class WorkPackages::UpdateService < ::BaseServices::BaseCallable
+class WorkPackages::UpdateService < ::BaseServices::Update
include ::WorkPackages::Shared::UpdateAncestors
- include ::Shared::ServiceContext
+ include Attachments::ReplaceAttachments
- attr_accessor :user,
- :model,
- :contract_class
+ private
- def initialize(user:, model:, contract_class: WorkPackages::UpdateContract)
- self.user = user
- self.model = model
- self.contract_class = contract_class
- end
+ def after_perform(service_call)
+ update_related_work_packages(service_call)
+ cleanup(service_call.result)
- def perform(send_notifications: true, **attributes)
- in_context(model, send_notifications) do
- update(attributes)
- end
+ service_call
end
- private
-
- def update(attributes)
- result = set_attributes(attributes)
-
- if result.success?
- work_package.attachments = work_package.attachments_replacements if work_package.attachments_replacements
- result.merge!(update_dependent)
+ def update_related_work_packages(service_call)
+ update_ancestors([service_call.result]).each do |ancestor_service_call|
+ service_call.merge!(ancestor_service_call)
end
- if save_if_valid(result)
- update_ancestors([work_package]).each do |ancestor_result|
- result.merge!(ancestor_result)
- end
+ update_related(service_call.result).each do |related_service_call|
+ service_call.merge!(related_service_call)
end
-
- result
end
- def save_if_valid(result)
- if result.success?
- result.success = consolidated_results(result)
- .all? { |wp| wp.save(validate: false) }
- end
-
- result.success?
+ def update_related(work_package)
+ consolidated_calls(update_descendants(work_package) + reschedule_related(work_package))
+ .reject { |dependent_call| dependent_call.result.id == work_package.id }
+ .each { |dependent_call| dependent_call.result.save(validate: false) }
end
- def update_dependent
- result = ServiceResult.new(success: true, result: work_package)
-
- result.merge!(update_descendants)
-
- cleanup if result.success?
-
- result.merge!(reschedule_related)
+ def update_descendants(work_package)
+ if work_package.saved_change_to_project_id?
+ attributes = { project: work_package.project }
- result
+ work_package.descendants.map do |descendant|
+ set_descendant_attributes(attributes, descendant)
+ end
+ else
+ []
+ end
end
- def set_attributes(attributes, wp = work_package)
+ def set_descendant_attributes(attributes, descendant)
WorkPackages::SetAttributesService
.new(user:,
- model: wp,
- contract_class:)
+ model: descendant,
+ contract_class: EmptyContract)
.call(attributes)
end
- def update_descendants
- result = ServiceResult.new(success: true, result: work_package)
-
- if work_package.project_id_changed?
- attributes = { project: work_package.project }
-
- work_package.descendants.each do |descendant|
- result.add_dependent!(set_attributes(attributes, descendant))
- end
- end
-
- result
- end
-
- def cleanup
- if work_package.project_id_changed?
+ def cleanup(work_package)
+ if work_package.saved_change_to_project_id?
moved_work_packages = [work_package] + work_package.descendants
delete_relations(moved_work_packages)
move_time_entries(moved_work_packages, work_package.project_id)
end
- if work_package.type_id_changed?
- reset_custom_values
+ if work_package.saved_change_to_type_id?
+ reset_custom_values(work_package)
end
end
@@ -134,60 +100,29 @@ class WorkPackages::UpdateService < ::BaseServices::BaseCallable
.update_all(project_id:)
end
- def reset_custom_values
+ def reset_custom_values(work_package)
work_package.reset_custom_values!
end
- def reschedule_related
- result = ServiceResult.new(success: true, result: work_package)
-
- with_temporarily_persisted_parent_changes do
- if work_package.parent_id_changed? && work_package.parent_id_was
- result.merge!(reschedule_former_siblings)
- end
-
- result.merge!(reschedule(work_package))
- end
-
- result
- end
-
- def with_temporarily_persisted_parent_changes
- # Explicitly using requires_new: true since we are already within a transaction.
- # Because of that, raising ActiveRecord::Rollback would have no effect:
- # https://www.bigbinary.com/learn-rubyonrails-book/activerecord-transactions-in-depth#nested-transactions
- WorkPackage.transaction(requires_new: true) do
- if work_package.parent_id_changed?
- # HACK: we need to persist the parent relation before rescheduling the parent
- # and the former parent since we rely on the database for scheduling.
- # The following will update the parent_id of the work package without that being noticed by the
- # work package instance (work_package) that is already instantiated. That way, the change can be rolled
- # back without any side effects to the instance (e.g. dirty tracking).
- WorkPackage.where(id: work_package.id).update_all(parent_id: work_package.parent_id)
- work_package.rebuild! # using the ClosureTree#rebuild! method to update the transitive hierarchy information
- end
-
- yield
+ def reschedule_related(work_package)
+ rescheduled = if work_package.saved_change_to_parent_id? && work_package.parent_id_before_last_save
+ reschedule_former_siblings(work_package).dependent_results
+ else
+ []
+ end
- # Always rolling back the changes we made in here
- raise ActiveRecord::Rollback
- end
+ rescheduled + reschedule(work_package, [work_package]).dependent_results
end
- # Rescheduling the former siblings will lead to the whole former tree being rescheduled.
- def reschedule_former_siblings
- reschedule(WorkPackage.where(parent_id: work_package.parent_id_was))
+ def reschedule_former_siblings(work_package)
+ reschedule(work_package, WorkPackage.where(parent_id: work_package.parent_id_before_last_save))
end
- def reschedule(work_packages)
+ def reschedule(work_package, work_packages)
WorkPackages::SetScheduleService
.new(user:,
work_package: work_packages)
- .call(changed_attributes)
- end
-
- def changed_attributes
- work_package.changed.map(&:to_sym)
+ .call(work_package.saved_changes.keys.map(&:to_sym))
end
# When multiple services change a work package, we still only want one update to the database due to:
@@ -195,19 +130,15 @@ class WorkPackages::UpdateService < ::BaseServices::BaseCallable
# * having only one journal entry
# * stale object errors
# we thus consolidate the results so that one instance contains the changes made by all the services.
- def consolidated_results(result)
- result.all_results.group_by(&:id).inject([]) do |a, (_, instances)|
- master = instances.pop
-
- instances.each do |instance|
- master.attributes = instance.changes.transform_values(&:last)
+ def consolidated_calls(service_calls)
+ service_calls
+ .group_by { |sc| sc.result.id }
+ .map do |(_, same_work_package_calls)|
+ same_work_package_calls.pop.tap do |master|
+ same_work_package_calls.each do |sc|
+ master.result.attributes = sc.result.changes.transform_values(&:last)
+ end
end
-
- a + [master]
end
end
-
- def work_package
- model
- end
end
diff --git a/modules/backlogs/lib/open_project/backlogs/list.rb b/modules/backlogs/lib/open_project/backlogs/list.rb
index 12b2e8e2988..9b07e1aa6d0 100644
--- a/modules/backlogs/lib/open_project/backlogs/list.rb
+++ b/modules/backlogs/lib/open_project/backlogs/list.rb
@@ -81,8 +81,10 @@ module OpenProject::Backlogs::List
protected
- def assume_bottom_position
- update_columns(position: bottom_position_in_list(self).to_i + 1)
+ # Overwriting acts/list to avoid it calling save.
+ # Calling save will remove the changes/saved_changes information.
+ def set_list_position(new_position, _raise_exception_if_save_fails = false)
+ update_columns(position: new_position)
end
def fix_other_work_package_positions
diff --git a/modules/backlogs/lib/open_project/backlogs/patches/update_service_patch.rb b/modules/backlogs/lib/open_project/backlogs/patches/update_service_patch.rb
index 7740752f3e0..291ac6e54cc 100644
--- a/modules/backlogs/lib/open_project/backlogs/patches/update_service_patch.rb
+++ b/modules/backlogs/lib/open_project/backlogs/patches/update_service_patch.rb
@@ -32,36 +32,44 @@ module OpenProject::Backlogs::Patches::UpdateServicePatch
end
module InstanceMethods
- def update_descendants
+ def update_descendants(work_package)
super_result = super
- if work_package.in_backlogs_type? && work_package.version_id_changed?
- inherit_version_to_descendants(super_result)
+ if work_package.in_backlogs_type? && work_package.saved_change_to_version_id?
+ super_result += inherit_version_to_descendants(work_package)
end
super_result
end
- def inherit_version_to_descendants(result)
- all_descendants = work_package
- .descendants
- .includes(project: :enabled_modules)
- .order_by_ancestors('asc')
- .select('work_packages.*')
- stop_descendants_ids = []
-
- descendant_tasks = all_descendants.reject do |t|
- if stop_descendants_ids.include?(t.parent_id) || !t.is_task?
- stop_descendants_ids << t.id
- end
- end
+ def inherit_version_to_descendants(work_package)
+ all_descendants = sorted_descendants(work_package)
+ descendant_tasks = descendant_tasks_of(all_descendants)
attributes = { version_id: work_package.version_id }
- descendant_tasks.each do |task|
+ descendant_tasks.map do |task|
# Ensure the parent is already moved to new version so that validation errors are avoided.
task.parent = ([work_package] + all_descendants).detect { |d| d.id == task.parent_id }
- result.add_dependent!(set_attributes(attributes, task))
+ set_descendant_attributes(attributes, task)
+ end
+ end
+
+ def sorted_descendants(work_package)
+ work_package
+ .descendants
+ .includes(project: :enabled_modules)
+ .order_by_ancestors('asc')
+ .select('work_packages.*')
+ end
+
+ def descendant_tasks_of(descendants)
+ stop_descendants_ids = []
+
+ descendants.reject do |t|
+ if stop_descendants_ids.include?(t.parent_id) || !t.is_task?
+ stop_descendants_ids << t.id
+ end
end
end
end
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 54fe6cc670c..74785b35a50 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -1096,6 +1096,33 @@ describe WorkPackages::SetAttributesService, type: :model do
end
end
end
+
+ context 'for parent' do
+ let(:parent_work_package) { build_stubbed(:work_package, project:) }
+ let(:work_package) do
+ build_stubbed(:work_package, project:, type: initial_type, parent: parent_work_package)
+ end
+
+ context 'with cross project relations allowed', with_settings: { cross_project_work_package_relations: true } do
+ it 'keeps the parent' do
+ expect(subject)
+ .to be_success
+
+ expect(work_package.parent)
+ .to eql(parent_work_package)
+ end
+ end
+
+ context 'with cross project relations disabled', with_settings: { cross_project_work_package_relations: false } do
+ it 'deletes the parent' do
+ expect(subject)
+ .to be_success
+
+ expect(work_package.parent)
+ .to be_nil
+ end
+ end
+ end
end
context 'when updating project before calling the service' do
diff --git a/spec/services/work_packages/update_service_integration_spec.rb b/spec/services/work_packages/update_service_integration_spec.rb
index 88012e00c73..6b09ef52bfe 100644
--- a/spec/services/work_packages/update_service_integration_spec.rb
+++ b/spec/services/work_packages/update_service_integration_spec.rb
@@ -28,7 +28,6 @@
require 'spec_helper'
-# rubocop:disable RSpec/MultipleMemoizedHelpers
describe WorkPackages::UpdateService, 'integration tests', type: :model, with_mail: false do
let(:user) do
create(:user,
@@ -106,1073 +105,1105 @@ describe WorkPackages::UpdateService, 'integration tests', type: :model, with_ma
instance.call(**attributes.merge(send_notifications: false).symbolize_keys)
end
- describe '#call' do
- describe 'updating subject' do
- let(:attributes) { { subject: 'New subject' } }
+ describe 'updating subject' do
+ let(:attributes) { { subject: 'New subject' } }
- it 'works' do
- expect(subject)
- .to be_success
+ it 'works' do
+ expect(subject)
+ .to be_success
- expect(work_package.subject)
- .to eql(attributes[:subject])
- end
+ expect(work_package.subject)
+ .to eql(attributes[:subject])
end
+ end
- describe 'updating project' do
- let(:target_project) do
- p = create(:project,
- types: target_types,
- parent: target_parent)
-
- create(:member,
- user:,
- project: p,
- roles: [create(:role, permissions: target_permissions)])
-
- p
- end
- let(:attributes) { { project_id: target_project.id } }
- let(:target_permissions) { [:move_work_packages] }
- let(:target_parent) { nil }
- let(:target_types) { [type] }
-
- describe 'with missing permissions' do
- let(:target_permissions) { [] }
+ context 'when updating the project' do
+ let(:target_project) do
+ p = create(:project,
+ types: target_types,
+ parent: target_parent)
- it 'is failure' do
- expect(subject)
- .to be_failure
- end
- end
+ create(:member,
+ user:,
+ project: p,
+ roles: [create(:role, permissions: target_permissions)])
- describe 'time_entries' do
- let!(:time_entries) do
- [create(:time_entry,
- project:,
- work_package:),
- create(:time_entry,
- project:,
- work_package:)]
- end
+ p
+ end
+ let(:attributes) { { project_id: target_project.id } }
+ let(:target_permissions) { [:move_work_packages] }
+ let(:target_parent) { nil }
+ let(:target_types) { [type] }
- it 'moves the time entries along' do
- expect(subject)
- .to be_success
+ it 'is is success and updates the project' do
+ expect(subject)
+ .to be_success
- expect(TimeEntry.where(id: time_entries.map(&:id)).pluck(:project_id).uniq)
- .to match_array [target_project.id]
- end
+ expect(work_package.reload.project)
+ .to eql target_project
+ end
- describe 'categories' do
- let(:category) do
- create(:category,
- project:)
- end
+ context 'with missing permissions' do
+ let(:target_permissions) { [] }
- before do
- work_package.category = category
- work_package.save!
- end
+ it 'is failure' do
+ expect(subject)
+ .to be_failure
+ end
+ end
- context 'with equally named category' do
- let!(:target_category) do
- create(:category,
- name: category.name,
- project: target_project)
- end
+ describe 'time_entries' do
+ let!(:time_entries) do
+ [create(:time_entry,
+ project:,
+ work_package:),
+ create(:time_entry,
+ project:,
+ work_package:)]
+ end
- it 'replaces the current category by the equally named one' do
- expect(subject)
- .to be_success
+ it 'moves the time entries along' do
+ expect(subject)
+ .to be_success
- expect(subject.result.category)
- .to eql target_category
- end
- end
+ expect(TimeEntry.where(id: time_entries.map(&:id)).pluck(:project_id).uniq)
+ .to match_array [target_project.id]
+ end
+ end
- context 'w/o target category' do
- let!(:other_category) do
- create(:category,
- project: target_project)
- end
+ describe 'categories' do
+ let(:category) do
+ create(:category,
+ project:)
+ end
- it 'removes the category' do
- expect(subject)
- .to be_success
+ before do
+ work_package.category = category
+ work_package.save!
+ end
- expect(subject.result.category)
- .to be_nil
- end
- end
+ context 'with equally named category' do
+ let!(:target_category) do
+ create(:category,
+ name: category.name,
+ project: target_project)
end
- describe 'version' do
- let(:sharing) { 'none' }
- let(:version) do
- create(:version,
- status: 'open',
- project:,
- sharing:)
- end
- let(:work_package) do
- create(:work_package,
- version:,
- project:)
- end
-
- context 'unshared version' do
- it 'removes the version' do
- expect(subject)
- .to be_success
-
- expect(subject.result.version)
- .to be_nil
- end
- end
-
- context 'system wide shared version' do
- let(:sharing) { 'system' }
-
- it 'keeps the version' do
- expect(subject)
- .to be_success
-
- expect(subject.result.version)
- .to eql version
- end
- end
-
- context 'move work package in project hierarchy' do
- let(:target_parent) do
- project
- end
-
- context 'unshared version' do
- it 'removes the version' do
- expect(subject)
- .to be_success
+ it 'replaces the current category by the equally named one' do
+ expect(subject)
+ .to be_success
- expect(subject.result.version)
- .to be_nil
- end
- end
+ expect(subject.result.category)
+ .to eql target_category
+ end
+ end
- context 'shared version' do
- let(:sharing) { 'tree' }
+ context 'without a target category' do
+ let!(:other_category) do
+ create(:category,
+ project: target_project)
+ end
- it 'keeps the version' do
- expect(subject)
- .to be_success
+ it 'removes the category' do
+ expect(subject)
+ .to be_success
- expect(subject.result.version)
- .to eql version
- end
- end
- end
+ expect(subject.result.category)
+ .to be_nil
end
+ end
+ end
- describe 'type' do
- let(:target_types) { [type, other_type] }
- let(:other_type) { create(:type) }
- let(:default_type) { type }
- let(:project_types) { [type, other_type] }
- let!(:workflow_type) do
- create(:workflow, type: default_type, role:, old_status_id: status.id)
- end
- let!(:workflow_other_type) do
- create(:workflow, type: other_type, role:, old_status_id: status.id)
- end
+ describe 'version' do
+ let(:sharing) { 'none' }
+ let(:version) do
+ create(:version,
+ status: 'open',
+ project:,
+ sharing:)
+ end
+ let(:work_package) do
+ create(:work_package,
+ version:,
+ project:)
+ end
- context 'with the type existing in the target project' do
- it 'keeps the type' do
- expect(subject)
- .to be_success
+ context 'with an unshared version' do
+ it 'removes the version' do
+ expect(subject)
+ .to be_success
- expect(subject.result.type)
- .to eql type
- end
- end
+ expect(subject.result.version)
+ .to be_nil
+ end
+ end
- context 'with a default type existing in the target project' do
- let(:target_types) { [other_type, default_type] }
+ context 'with a system wide shared version' do
+ let(:sharing) { 'system' }
- it 'uses the default type' do
- expect(subject)
- .to be_success
+ it 'keeps the version' do
+ expect(subject)
+ .to be_success
- expect(subject.result.type)
- .to eql default_type
- end
- end
+ expect(subject.result.version)
+ .to eql version
+ end
+ end
- context 'with only non default types' do
- let(:target_types) { [other_type] }
+ context 'when moving the work package in project hierarchy' do
+ let(:target_parent) do
+ project
+ end
- it 'uses the first type' do
- expect(subject)
- .to be_success
+ context 'with an unshared version' do
+ it 'removes the version' do
+ expect(subject)
+ .to be_success
- expect(subject.result.type)
- .to eql other_type
- end
+ expect(subject.result.version)
+ .to be_nil
end
+ end
- context 'with an invalid type being provided' do
- let(:target_types) { [type] }
+ context 'with a shared version' do
+ let(:sharing) { 'tree' }
- let(:attributes) do
- { project: target_project,
- type: other_type }
- end
+ it 'keeps the version' do
+ expect(subject)
+ .to be_success
- it 'is unsuccessful' do
- expect(subject)
- .to be_failure
- end
+ expect(subject.result.version)
+ .to eql version
end
end
end
end
- describe 'inheriting dates' do
- let(:attributes) { { start_date: Date.today - 8.days, due_date: Date.today + 12.days } }
- let(:sibling1_attributes) do
- work_package_attributes.merge(start_date: Date.today - 5.days,
- due_date: Date.today + 10.days,
- parent: parent_work_package)
- end
- let(:sibling2_attributes) do
- work_package_attributes.merge(due_date: Date.today + 16.days,
- parent: parent_work_package)
+ describe 'type' do
+ let(:target_types) { [type, other_type] }
+ let(:other_type) { create(:type) }
+ let(:default_type) { type }
+ let(:project_types) { [type, other_type] }
+ let!(:workflow_type) do
+ create(:workflow, type: default_type, role:, old_status_id: status.id)
end
-
- before do
- parent_work_package
- grandparent_work_package
- sibling1_work_package
- sibling2_work_package
+ let!(:workflow_other_type) do
+ create(:workflow, type: other_type, role:, old_status_id: status.id)
end
- it 'works and inherits' do
- expect(subject)
- .to be_success
+ context 'with the type existing in the target project' do
+ it 'keeps the type' do
+ expect(subject)
+ .to be_success
- # receives the provided start/finish date
- expect(work_package.start_date)
- .to eql(attributes[:start_date])
- expect(work_package.due_date)
- .to eql(attributes[:due_date])
-
- # receives the min/max of the children's start/finish date
- [parent_work_package,
- grandparent_work_package].each do |wp|
- wp.reload
-
- expect(wp.start_date)
- .to eql(attributes[:start_date])
- expect(wp.due_date)
- .to eql(sibling2_work_package.due_date)
+ expect(subject.result.type)
+ .to eql type
end
-
- # sibling dates are unchanged
- sibling1_work_package.reload
- expect(sibling1_work_package.start_date)
- .to eql(sibling1_attributes[:start_date])
- expect(sibling1_work_package.due_date)
- .to eql(sibling1_attributes[:due_date])
-
- sibling2_work_package.reload
- expect(sibling2_work_package.start_date)
- .to eql(sibling2_attributes[:start_date])
- expect(sibling2_work_package.due_date)
- .to eql(sibling2_attributes[:due_date])
end
- end
- describe 'inheriting done_ratio' do
- let(:attributes) { { done_ratio: 50 } }
- let(:work_package_attributes) do
- { project_id: project.id,
- type_id: type.id,
- author_id: user.id,
- status_id: status.id,
- priority:,
- estimated_hours: 10 }
- end
+ context 'with a default type existing in the target project' do
+ let(:target_types) { [other_type, default_type] }
- let(:sibling1_attributes) do
- work_package_attributes.merge(estimated_hours: nil,
- done_ratio: 20,
- parent: parent_work_package)
- end
- let(:sibling2_attributes) do
- work_package_attributes.merge(done_ratio: 0,
- estimated_hours: 100,
- parent: parent_work_package)
- end
+ it 'uses the default type' do
+ expect(subject)
+ .to be_success
- before do
- parent_work_package
- grandparent_work_package
- sibling1_work_package
- sibling2_work_package
+ expect(subject.result.type)
+ .to eql default_type
+ end
end
- it 'works and inherits average done ratio of leaves weighted by estimated times' do
- expect(subject)
- .to be_success
+ context 'with only non default types' do
+ let(:target_types) { [other_type] }
- # set to the provided values
- expect(work_package.done_ratio)
- .to eql(attributes[:done_ratio])
+ it 'uses the first type' do
+ expect(subject)
+ .to be_success
- # calculated
- # sibling1 not factored in as its estimated_hours are nil
- calculated_ratio = ((work_package.done_ratio * work_package.estimated_hours) +
- (sibling2_work_package.done_ratio * sibling2_work_package.estimated_hours)) /
- (work_package.done_ratio +
- sibling2_work_package.done_ratio)
+ expect(subject.result.type)
+ .to eql other_type
+ end
+ end
- [parent_work_package,
- grandparent_work_package].each do |wp|
- wp.reload
+ context 'with an invalid type being provided' do
+ let(:target_types) { [type] }
- expect(wp.done_ratio)
- .to eql(calculated_ratio.to_i)
+ let(:attributes) do
+ { project: target_project,
+ type: other_type }
end
- # unchanged
- sibling1_work_package.reload
- expect(sibling1_work_package.done_ratio)
- .to eql(sibling1_attributes[:done_ratio])
-
- sibling2_work_package.reload
- expect(sibling2_work_package.done_ratio)
- .to eql(sibling2_attributes[:done_ratio])
+ it 'is unsuccessful' do
+ expect(subject)
+ .to be_failure
+ end
end
end
- describe 'inheriting estimated_hours' do
- let(:attributes) { { estimated_hours: 7 } }
- let(:sibling1_attributes) do
- # no estimated hours
- work_package_attributes.merge(parent: parent_work_package)
- end
- let(:sibling2_attributes) do
- work_package_attributes.merge(estimated_hours: 5,
- parent: parent_work_package)
- end
- let(:child_attributes) do
- work_package_attributes.merge(estimated_hours: 10,
- parent: work_package)
- end
-
- before do
- parent_work_package
- grandparent_work_package
- sibling1_work_package
- sibling2_work_package
- child_work_package
+ describe 'relations' do
+ let!(:relation) do
+ create(:follows_relation,
+ from: work_package,
+ to: create(:work_package,
+ project:))
end
- it 'works and inherits' do
- expect(subject)
- .to be_success
-
- # receives the provided value
- expect(work_package.estimated_hours)
- .to eql(attributes[:estimated_hours].to_f)
+ context 'with cross project relations allowed', with_settings: { cross_project_work_package_relations: true } do
+ it 'keeps the relation' do
+ expect(subject)
+ .to be_success
- # receive the sum of the children's estimated hours
- [parent_work_package,
- grandparent_work_package].each do |wp|
- sum = sibling1_attributes[:estimated_hours].to_f +
- sibling2_attributes[:estimated_hours].to_f +
- attributes[:estimated_hours].to_f +
- child_attributes[:estimated_hours].to_f
+ expect(Relation.find_by(id: relation.id))
+ .to eql(relation)
+ end
+ end
- wp.reload
+ context 'with cross project relations disabled', with_settings: { cross_project_work_package_relations: false } do
+ it 'deletes the relation' do
+ expect(subject)
+ .to be_success
- expect(wp.estimated_hours).to be_nil
- expect(wp.derived_estimated_hours).to eql(sum)
+ expect(Relation.find_by(id: relation.id))
+ .to be_nil
end
+ end
+ end
+ end
- # sibling hours are unchanged
- sibling1_work_package.reload
- expect(sibling1_work_package.estimated_hours)
- .to be_nil
-
- sibling2_work_package.reload
- expect(sibling2_work_package.estimated_hours)
- .to eql(sibling2_attributes[:estimated_hours].to_f)
+ describe 'inheriting dates' do
+ let(:attributes) { { start_date: Time.zone.today - 8.days, due_date: Time.zone.today + 12.days } }
+ let(:sibling1_attributes) do
+ work_package_attributes.merge(start_date: Time.zone.today - 5.days,
+ due_date: Time.zone.today + 10.days,
+ parent: parent_work_package)
+ end
+ let(:sibling2_attributes) do
+ work_package_attributes.merge(due_date: Time.zone.today + 16.days,
+ parent: parent_work_package)
+ end
- # child hours are unchanged
- child_work_package.reload
- expect(child_work_package.estimated_hours)
- .to eql(child_attributes[:estimated_hours].to_f)
- end
+ before do
+ parent_work_package
+ grandparent_work_package
+ sibling1_work_package
+ sibling2_work_package
end
- describe 'closing duplicates on closing status' do
- let(:status_closed) do
- create(:status,
- is_closed: true).tap do |status_closed|
- create(:workflow,
- old_status: status,
- new_status: status_closed,
- type:,
- role:)
- end
- end
- let!(:duplicate_work_package) do
- create(:work_package,
- work_package_attributes).tap do |wp|
- create(:relation, relation_type: Relation::TYPE_DUPLICATES, from: wp, to: work_package)
- end
- end
+ it 'works and inherits' do
+ expect(subject)
+ .to be_success
- let(:attributes) { { status: status_closed } }
+ # receives the provided start/finish date
+ expect(work_package.start_date)
+ .to eql(attributes[:start_date])
+ expect(work_package.due_date)
+ .to eql(attributes[:due_date])
- it 'works and closes duplicates' do
- expect(subject)
- .to be_success
+ # receives the min/max of the children's start/finish date
+ [parent_work_package,
+ grandparent_work_package].each do |wp|
+ wp.reload
- duplicate_work_package.reload
+ expect(wp.start_date)
+ .to eql(attributes[:start_date])
+ expect(wp.due_date)
+ .to eql(sibling2_work_package.due_date)
+ end
+
+ # sibling dates are unchanged
+ sibling1_work_package.reload
+ expect(sibling1_work_package.start_date)
+ .to eql(sibling1_attributes[:start_date])
+ expect(sibling1_work_package.due_date)
+ .to eql(sibling1_attributes[:due_date])
+
+ sibling2_work_package.reload
+ expect(sibling2_work_package.start_date)
+ .to eql(sibling2_attributes[:start_date])
+ expect(sibling2_work_package.due_date)
+ .to eql(sibling2_attributes[:due_date])
+ end
+ end
- expect(work_package.status)
- .to eql(attributes[:status])
- expect(duplicate_work_package.status)
- .to eql(attributes[:status])
- end
+ describe 'inheriting done_ratio' do
+ let(:attributes) { { done_ratio: 50 } }
+ let(:work_package_attributes) do
+ { project_id: project.id,
+ type_id: type.id,
+ author_id: user.id,
+ status_id: status.id,
+ priority:,
+ estimated_hours: 10 }
end
- describe 'rescheduling work packages along follows/hierarchy relations' do
- # layout
- # following_parent_work_package +-follows- following2_parent_work_package following3_parent_work_package
- # | | / |
- # hierarchy hierarchy hierarchy hierarchy
- # | | / |
- # + + + |
- # work_package +-follows- following_work_package following2_work_package +-follows- following3_work_package +
- # following3_sibling_work_package
- let(:work_package_attributes) do
- { project_id: project.id,
- type_id: type.id,
- author_id: user.id,
- status_id: status.id,
- priority:,
- start_date: Date.today,
- due_date: Date.today + 5.days }
- end
- let(:attributes) do
- { start_date: Date.today + 5.days,
- due_date: Date.today + 10.days }
- end
- let(:following_attributes) do
- work_package_attributes.merge(parent: following_parent_work_package,
- subject: 'following',
- start_date: Date.today + 6.days,
- due_date: Date.today + 20.days)
- end
- let(:following_work_package) do
- create(:work_package,
- following_attributes).tap do |wp|
- create(:follows_relation, from: wp, to: work_package)
- end
- end
- let(:following_parent_attributes) do
- work_package_attributes.merge(subject: 'following_parent',
- start_date: Date.today + 6.days,
- due_date: Date.today + 20.days)
- end
- let(:following_parent_work_package) do
- create(:work_package,
- following_parent_attributes)
- end
- let(:following2_attributes) do
- work_package_attributes.merge(parent: following2_parent_work_package,
- subject: 'following2',
- start_date: Date.today + 21.days,
- due_date: Date.today + 25.days)
- end
- let(:following2_work_package) do
- create(:work_package,
- following2_attributes)
- end
- let(:following2_parent_attributes) do
- work_package_attributes.merge(subject: 'following2_parent',
- start_date: Date.today + 21.days,
- due_date: Date.today + 25.days)
- end
- let(:following2_parent_work_package) do
- create(:work_package,
- following2_parent_attributes).tap do |wp|
- create(:follows_relation, from: wp, to: following_parent_work_package)
- end
- end
- let(:following3_attributes) do
- work_package_attributes.merge(subject: 'following3',
- parent: following3_parent_work_package,
- start_date: Date.today + 26.days,
- due_date: Date.today + 30.days)
- end
- let(:following3_work_package) do
- create(:work_package,
- following3_attributes).tap do |wp|
- create(:follows_relation, from: wp, to: following2_work_package)
- end
- end
- let(:following3_parent_attributes) do
- work_package_attributes.merge(subject: 'following3_parent',
- start_date: Date.today + 26.days,
- due_date: Date.today + 36.days)
- end
- let(:following3_parent_work_package) do
- create(:work_package,
- following3_parent_attributes)
- end
- let(:following3_sibling_attributes) do
- work_package_attributes.merge(parent: following3_parent_work_package,
- subject: 'following3_sibling',
- start_date: Date.today + 32.days,
- due_date: Date.today + 36.days)
- end
- let(:following3_sibling_work_package) do
- create(:work_package,
- following3_sibling_attributes)
- end
+ let(:sibling1_attributes) do
+ work_package_attributes.merge(estimated_hours: nil,
+ done_ratio: 20,
+ parent: parent_work_package)
+ end
+ let(:sibling2_attributes) do
+ work_package_attributes.merge(done_ratio: 0,
+ estimated_hours: 100,
+ parent: parent_work_package)
+ end
- before do
- work_package
- following_parent_work_package
- following_work_package
- following2_parent_work_package
- following2_work_package
- following3_parent_work_package
- following3_work_package
- following3_sibling_work_package
- end
+ before do
+ parent_work_package
+ grandparent_work_package
+ sibling1_work_package
+ sibling2_work_package
+ end
- it 'propagates the changes to start/finish date along' do
- expect(subject)
- .to be_success
+ it 'works and inherits average done ratio of leaves weighted by estimated times' do
+ expect(subject)
+ .to be_success
- work_package.reload(select: %i(start_date due_date))
- expect(work_package.start_date)
- .to eql Date.today + 5.days
+ # set to the provided values
+ expect(work_package.done_ratio)
+ .to eql(attributes[:done_ratio])
- expect(work_package.due_date)
- .to eql Date.today + 10.days
+ # calculated
+ # sibling1 not factored in as its estimated_hours are nil
+ calculated_ratio = ((work_package.done_ratio * work_package.estimated_hours) +
+ (sibling2_work_package.done_ratio * sibling2_work_package.estimated_hours)) /
+ (work_package.done_ratio +
+ sibling2_work_package.done_ratio)
- following_work_package.reload(select: %i(start_date due_date))
- expect(following_work_package.start_date)
- .to eql Date.today + 11.days
+ [parent_work_package,
+ grandparent_work_package].each do |wp|
+ wp.reload
- expect(following_work_package.due_date)
- .to eql Date.today + 25.days
+ expect(wp.done_ratio)
+ .to eql(calculated_ratio.to_i)
+ end
- following_parent_work_package.reload(select: %i(start_date due_date))
- expect(following_parent_work_package.start_date)
- .to eql Date.today + 11.days
+ # unchanged
+ sibling1_work_package.reload
+ expect(sibling1_work_package.done_ratio)
+ .to eql(sibling1_attributes[:done_ratio])
- expect(following_parent_work_package.due_date)
- .to eql Date.today + 25.days
+ sibling2_work_package.reload
+ expect(sibling2_work_package.done_ratio)
+ .to eql(sibling2_attributes[:done_ratio])
+ end
+ end
- following2_parent_work_package.reload(select: %i(start_date due_date))
- expect(following2_parent_work_package.start_date)
- .to eql Date.today + 26.days
+ describe 'inheriting estimated_hours' do
+ let(:attributes) { { estimated_hours: 7 } }
+ let(:sibling1_attributes) do
+ # no estimated hours
+ work_package_attributes.merge(parent: parent_work_package)
+ end
+ let(:sibling2_attributes) do
+ work_package_attributes.merge(estimated_hours: 5,
+ parent: parent_work_package)
+ end
+ let(:child_attributes) do
+ work_package_attributes.merge(estimated_hours: 10,
+ parent: work_package)
+ end
- expect(following2_parent_work_package.due_date)
- .to eql Date.today + 30.days
+ before do
+ parent_work_package
+ grandparent_work_package
+ sibling1_work_package
+ sibling2_work_package
+ child_work_package
+ end
- following2_work_package.reload(select: %i(start_date due_date))
- expect(following2_work_package.start_date)
- .to eql Date.today + 26.days
+ it 'works and inherits' do
+ expect(subject)
+ .to be_success
- expect(following2_work_package.due_date)
- .to eql Date.today + 30.days
+ # receives the provided value
+ expect(work_package.estimated_hours)
+ .to eql(attributes[:estimated_hours].to_f)
- following3_work_package.reload(select: %i(start_date due_date))
- expect(following3_work_package.start_date)
- .to eql Date.today + 31.days
+ # receive the sum of the children's estimated hours
+ [parent_work_package,
+ grandparent_work_package].each do |wp|
+ sum = sibling1_attributes[:estimated_hours].to_f +
+ sibling2_attributes[:estimated_hours].to_f +
+ attributes[:estimated_hours].to_f +
+ child_attributes[:estimated_hours].to_f
- expect(following3_work_package.due_date)
- .to eql Date.today + 35.days
+ wp.reload
- following3_parent_work_package.reload(select: %i(start_date due_date))
- expect(following3_parent_work_package.start_date)
- .to eql Date.today + 31.days
+ expect(wp.estimated_hours).to be_nil
+ expect(wp.derived_estimated_hours).to eql(sum)
+ end
- expect(following3_parent_work_package.due_date)
- .to eql Date.today + 36.days
+ # sibling hours are unchanged
+ sibling1_work_package.reload
+ expect(sibling1_work_package.estimated_hours)
+ .to be_nil
- following3_sibling_work_package.reload(select: %i(start_date due_date))
- expect(following3_sibling_work_package.start_date)
- .to eql Date.today + 32.days
+ sibling2_work_package.reload
+ expect(sibling2_work_package.estimated_hours)
+ .to eql(sibling2_attributes[:estimated_hours].to_f)
- expect(following3_sibling_work_package.due_date)
- .to eql Date.today + 36.days
- end
+ # child hours are unchanged
+ child_work_package.reload
+ expect(child_work_package.estimated_hours)
+ .to eql(child_attributes[:estimated_hours].to_f)
end
+ end
- describe 'rescheduling work packages forward follows/hierarchy relations' do
- # layout
- # other_work_package
- # +
- # |
- # follows (delay: 3 days)
- # |
- # following_parent_work_package +-follows- following2_parent_work_package
- # | |
- # hierarchy hierarchy
- # | |
- # + +
- # work_package +-follows- following_work_package following2_work_package +-follows- following3_work_package
- let(:work_package_attributes) do
- { project_id: project.id,
- type_id: type.id,
- author_id: user.id,
- status_id: status.id,
- priority:,
- start_date: Date.today,
- due_date: Date.today + 5.days }
- end
- let(:attributes) do
- { start_date: Date.today - 5.days,
- due_date: Date.today }
- end
- let(:following_attributes) do
- work_package_attributes.merge(parent: following_parent_work_package,
- subject: 'following',
- start_date: Date.today + 6.days,
- due_date: Date.today + 20.days)
- end
- let(:following_work_package) do
- create(:work_package,
- following_attributes).tap do |wp|
- create(:follows_relation, from: wp, to: work_package)
- end
- end
- let(:following_parent_attributes) do
- work_package_attributes.merge(subject: 'following_parent',
- start_date: Date.today + 6.days,
- due_date: Date.today + 20.days)
- end
- let(:following_parent_work_package) do
- create(:work_package,
- following_parent_attributes)
- end
- let(:other_attributes) do
- work_package_attributes.merge(subject: 'other',
- start_date: Date.today + 10.days,
- due_date: Date.today + 18.days)
- end
- let(:other_work_package) do
- create(:work_package,
- other_attributes)
- end
- let(:following2_attributes) do
- work_package_attributes.merge(parent: following2_parent_work_package,
- subject: 'following2',
- start_date: Date.today + 24.days,
- due_date: Date.today + 28.days)
- end
- let(:following2_work_package) do
- create(:work_package,
- following2_attributes)
- end
- let(:following2_parent_attributes) do
- work_package_attributes.merge(subject: 'following2_parent',
- start_date: Date.today + 24.days,
- due_date: Date.today + 28.days)
- end
- let(:following2_parent_work_package) do
- following2 = create(:work_package,
- following2_parent_attributes).tap do |wp|
- create(:follows_relation, from: wp, to: following_parent_work_package)
- end
-
- create(:relation,
- relation_type: Relation::TYPE_FOLLOWS,
- from: following2,
- to: other_work_package,
- delay: 3)
-
- following2
- end
- let(:following3_attributes) do
- work_package_attributes.merge(subject: 'following3',
- start_date: Date.today + 29.days,
- due_date: Date.today + 33.days)
- end
- let(:following3_work_package) do
- create(:work_package,
- following3_attributes).tap do |wp|
- create(:follows_relation, from: wp, to: following2_work_package)
- end
+ describe 'closing duplicates on closing status' do
+ let(:status_closed) do
+ create(:status,
+ is_closed: true).tap do |status_closed|
+ create(:workflow,
+ old_status: status,
+ new_status: status_closed,
+ type:,
+ role:)
end
-
- before do
- work_package
- other_work_package
- following_parent_work_package
- following_work_package
- following2_parent_work_package
- following2_work_package
- following3_work_package
+ end
+ let!(:duplicate_work_package) do
+ create(:work_package,
+ work_package_attributes).tap do |wp|
+ create(:relation, relation_type: Relation::TYPE_DUPLICATES, from: wp, to: work_package)
end
+ end
- it 'propagates the changes to start/finish date along' do
- expect(subject)
- .to be_success
-
- work_package.reload(select: %i(start_date due_date))
- expect(work_package.start_date)
- .to eql Date.today - 5.days
+ let(:attributes) { { status: status_closed } }
- expect(work_package.due_date)
- .to eql Date.today
+ it 'works and closes duplicates' do
+ expect(subject)
+ .to be_success
- following_work_package.reload(select: %i(start_date due_date))
- expect(following_work_package.start_date)
- .to eql Date.today + 1.day
+ duplicate_work_package.reload
- expect(following_work_package.due_date)
- .to eql Date.today + 15.days
+ expect(work_package.status)
+ .to eql(attributes[:status])
+ expect(duplicate_work_package.status)
+ .to eql(attributes[:status])
+ end
+ end
- following_parent_work_package.reload(select: %i(start_date due_date))
- expect(following_parent_work_package.start_date)
- .to eql Date.today + 1.day
+ describe 'rescheduling work packages along follows/hierarchy relations' do
+ # layout
+ # following_parent_work_package +-follows- following2_parent_work_package following3_parent_work_package
+ # | | / |
+ # hierarchy hierarchy hierarchy hierarchy
+ # | | / |
+ # + + + |
+ # work_package +-follows- following_work_package following2_work_package +-follows- following3_work_package +
+ # following3_sibling_work_package
+ let(:work_package_attributes) do
+ { project_id: project.id,
+ type_id: type.id,
+ author_id: user.id,
+ status_id: status.id,
+ priority:,
+ start_date: Time.zone.today,
+ due_date: Time.zone.today + 5.days }
+ end
+ let(:attributes) do
+ { start_date: Time.zone.today + 5.days,
+ due_date: Time.zone.today + 10.days }
+ end
+ let(:following_attributes) do
+ work_package_attributes.merge(parent: following_parent_work_package,
+ subject: 'following',
+ start_date: Time.zone.today + 6.days,
+ due_date: Time.zone.today + 20.days)
+ end
+ let(:following_work_package) do
+ create(:work_package,
+ following_attributes).tap do |wp|
+ create(:follows_relation, from: wp, to: work_package)
+ end
+ end
+ let(:following_parent_attributes) do
+ work_package_attributes.merge(subject: 'following_parent',
+ start_date: Time.zone.today + 6.days,
+ due_date: Time.zone.today + 20.days)
+ end
+ let(:following_parent_work_package) do
+ create(:work_package,
+ following_parent_attributes)
+ end
+ let(:following2_attributes) do
+ work_package_attributes.merge(parent: following2_parent_work_package,
+ subject: 'following2',
+ start_date: Time.zone.today + 21.days,
+ due_date: Time.zone.today + 25.days)
+ end
+ let(:following2_work_package) do
+ create(:work_package,
+ following2_attributes)
+ end
+ let(:following2_parent_attributes) do
+ work_package_attributes.merge(subject: 'following2_parent',
+ start_date: Time.zone.today + 21.days,
+ due_date: Time.zone.today + 25.days)
+ end
+ let(:following2_parent_work_package) do
+ create(:work_package,
+ following2_parent_attributes).tap do |wp|
+ create(:follows_relation, from: wp, to: following_parent_work_package)
+ end
+ end
+ let(:following3_attributes) do
+ work_package_attributes.merge(subject: 'following3',
+ parent: following3_parent_work_package,
+ start_date: Time.zone.today + 26.days,
+ due_date: Time.zone.today + 30.days)
+ end
+ let(:following3_work_package) do
+ create(:work_package,
+ following3_attributes).tap do |wp|
+ create(:follows_relation, from: wp, to: following2_work_package)
+ end
+ end
+ let(:following3_parent_attributes) do
+ work_package_attributes.merge(subject: 'following3_parent',
+ start_date: Time.zone.today + 26.days,
+ due_date: Time.zone.today + 36.days)
+ end
+ let(:following3_parent_work_package) do
+ create(:work_package,
+ following3_parent_attributes)
+ end
+ let(:following3_sibling_attributes) do
+ work_package_attributes.merge(parent: following3_parent_work_package,
+ subject: 'following3_sibling',
+ start_date: Time.zone.today + 32.days,
+ due_date: Time.zone.today + 36.days)
+ end
+ let(:following3_sibling_work_package) do
+ create(:work_package,
+ following3_sibling_attributes)
+ end
- expect(following_parent_work_package.due_date)
- .to eql Date.today + 15.days
+ before do
+ work_package
+ following_parent_work_package
+ following_work_package
+ following2_parent_work_package
+ following2_work_package
+ following3_parent_work_package
+ following3_work_package
+ following3_sibling_work_package
+ end
- following2_parent_work_package.reload(select: %i(start_date due_date))
- expect(following2_parent_work_package.start_date)
- .to eql Date.today + 22.days
+ # rubocop:disable RSpec/ExampleLength
+ # rubocop:disable RSpec/MultipleExpectations
+ it 'propagates the changes to start/finish date along' do
+ expect(subject)
+ .to be_success
+
+ work_package.reload(select: %i(start_date due_date))
+ expect(work_package.start_date)
+ .to eql Time.zone.today + 5.days
+
+ expect(work_package.due_date)
+ .to eql Time.zone.today + 10.days
+
+ following_work_package.reload(select: %i(start_date due_date))
+ expect(following_work_package.start_date)
+ .to eql Time.zone.today + 11.days
+ expect(following_work_package.due_date)
+ .to eql Time.zone.today + 25.days
+
+ following_parent_work_package.reload(select: %i(start_date due_date))
+ expect(following_parent_work_package.start_date)
+ .to eql Time.zone.today + 11.days
+ expect(following_parent_work_package.due_date)
+ .to eql Time.zone.today + 25.days
+
+ following2_parent_work_package.reload(select: %i(start_date due_date))
+ expect(following2_parent_work_package.start_date)
+ .to eql Time.zone.today + 26.days
+ expect(following2_parent_work_package.due_date)
+ .to eql Time.zone.today + 30.days
+
+ following2_work_package.reload(select: %i(start_date due_date))
+ expect(following2_work_package.start_date)
+ .to eql Time.zone.today + 26.days
+ expect(following2_work_package.due_date)
+ .to eql Time.zone.today + 30.days
+
+ following3_work_package.reload(select: %i(start_date due_date))
+ expect(following3_work_package.start_date)
+ .to eql Time.zone.today + 31.days
+ expect(following3_work_package.due_date)
+ .to eql Time.zone.today + 35.days
+
+ following3_parent_work_package.reload(select: %i(start_date due_date))
+ expect(following3_parent_work_package.start_date)
+ .to eql Time.zone.today + 31.days
+ expect(following3_parent_work_package.due_date)
+ .to eql Time.zone.today + 36.days
+
+ following3_sibling_work_package.reload(select: %i(start_date due_date))
+ expect(following3_sibling_work_package.start_date)
+ .to eql Time.zone.today + 32.days
+ expect(following3_sibling_work_package.due_date)
+ .to eql Time.zone.today + 36.days
+ end
+ # rubocop:enable RSpec/ExampleLength
+ # rubocop:enable RSpec/MultipleExpectations
+ end
- expect(following2_parent_work_package.due_date)
- .to eql Date.today + 26.days
+ describe 'rescheduling work packages forward follows/hierarchy relations' do
+ # layout
+ # other_work_package
+ # +
+ # |
+ # follows (delay: 3 days)
+ # |
+ # following_parent_work_package +-follows- following2_parent_work_package
+ # | |
+ # hierarchy hierarchy
+ # | |
+ # + +
+ # work_package +-follows- following_work_package following2_work_package +-follows- following3_work_package
+ let(:work_package_attributes) do
+ { project_id: project.id,
+ type_id: type.id,
+ author_id: user.id,
+ status_id: status.id,
+ priority:,
+ start_date: Time.zone.today,
+ due_date: Time.zone.today + 5.days }
+ end
+ let(:attributes) do
+ {
+ start_date: Time.zone.today - 5.days,
+ due_date: Time.zone.today
+ }
+ end
+ let(:following_attributes) do
+ work_package_attributes.merge(parent: following_parent_work_package,
+ subject: 'following',
+ start_date: Time.zone.today + 6.days,
+ due_date: Time.zone.today + 20.days)
+ end
+ let(:following_work_package) do
+ create(:work_package,
+ following_attributes).tap do |wp|
+ create(:follows_relation, from: wp, to: work_package)
+ end
+ end
+ let(:following_parent_attributes) do
+ work_package_attributes.merge(subject: 'following_parent',
+ start_date: Time.zone.today + 6.days,
+ due_date: Time.zone.today + 20.days)
+ end
+ let(:following_parent_work_package) do
+ create(:work_package,
+ following_parent_attributes)
+ end
+ let(:other_attributes) do
+ work_package_attributes.merge(subject: 'other',
+ start_date: Time.zone.today + 10.days,
+ due_date: Time.zone.today + 18.days)
+ end
+ let(:other_work_package) do
+ create(:work_package,
+ other_attributes)
+ end
+ let(:following2_attributes) do
+ work_package_attributes.merge(parent: following2_parent_work_package,
+ subject: 'following2',
+ start_date: Time.zone.today + 24.days,
+ due_date: Time.zone.today + 28.days)
+ end
+ let(:following2_work_package) do
+ create(:work_package,
+ following2_attributes)
+ end
+ let(:following2_parent_attributes) do
+ work_package_attributes.merge(subject: 'following2_parent',
+ start_date: Time.zone.today + 24.days,
+ due_date: Time.zone.today + 28.days)
+ end
+ let(:following2_parent_work_package) do
+ following2 = create(:work_package,
+ following2_parent_attributes).tap do |wp|
+ create(:follows_relation, from: wp, to: following_parent_work_package)
+ end
- following2_work_package.reload(select: %i(start_date due_date))
- expect(following2_work_package.start_date)
- .to eql Date.today + 22.days
+ create(:relation,
+ relation_type: Relation::TYPE_FOLLOWS,
+ from: following2,
+ to: other_work_package,
+ delay: 3)
- expect(following2_work_package.due_date)
- .to eql Date.today + 26.days
+ following2
+ end
+ let(:following3_attributes) do
+ work_package_attributes.merge(subject: 'following3',
+ start_date: Time.zone.today + 29.days,
+ due_date: Time.zone.today + 33.days)
+ end
+ let(:following3_work_package) do
+ create(:work_package,
+ following3_attributes).tap do |wp|
+ create(:follows_relation, from: wp, to: following2_work_package)
+ end
+ end
- following3_work_package.reload(select: %i(start_date due_date))
- expect(following3_work_package.start_date)
- .to eql Date.today + 27.days
+ before do
+ work_package
+ other_work_package
+ following_parent_work_package
+ following_work_package
+ following2_parent_work_package
+ following2_work_package
+ following3_work_package
+ end
- expect(following3_work_package.due_date)
- .to eql Date.today + 31.days
- end
+ # rubocop:disable RSpec/ExampleLength
+ it 'propagates the changes to start/finish date along' do
+ expect(subject)
+ .to be_success
+
+ work_package.reload(select: %i(start_date due_date))
+ expect(work_package.start_date)
+ .to eql Time.zone.today - 5.days
+ expect(work_package.due_date)
+ .to eql Time.zone.today
+
+ following_work_package.reload(select: %i(start_date due_date))
+ expect(following_work_package.start_date)
+ .to eql Time.zone.today + 1.day
+ expect(following_work_package.due_date)
+ .to eql Time.zone.today + 15.days
+
+ following_parent_work_package.reload(select: %i(start_date due_date))
+ expect(following_parent_work_package.start_date)
+ .to eql Time.zone.today + 1.day
+ expect(following_parent_work_package.due_date)
+ .to eql Time.zone.today + 15.days
+
+ following2_parent_work_package.reload(select: %i(start_date due_date))
+ expect(following2_parent_work_package.start_date)
+ .to eql Time.zone.today + 22.days
+ expect(following2_parent_work_package.due_date)
+ .to eql Time.zone.today + 26.days
+
+ following2_work_package.reload(select: %i(start_date due_date))
+ expect(following2_work_package.start_date)
+ .to eql Time.zone.today + 22.days
+ expect(following2_work_package.due_date)
+ .to eql Time.zone.today + 26.days
+
+ following3_work_package.reload(select: %i(start_date due_date))
+ expect(following3_work_package.start_date)
+ .to eql Time.zone.today + 27.days
+ expect(following3_work_package.due_date)
+ .to eql Time.zone.today + 31.days
end
+ # rubocop:enable RSpec/ExampleLength
+ end
- describe 'changing the parent' do
- let(:former_parent_attributes) do
- {
- subject: 'former parent',
- project_id: project.id,
- type_id: type.id,
- author_id: user.id,
- status_id: status.id,
- priority:,
- start_date: Date.today + 3.days,
- due_date: Date.today + 9.days
- }
- end
- let(:attributes) { { parent: new_parent_work_package } }
- let(:former_parent_work_package) do
- create(:work_package, former_parent_attributes)
- end
+ describe 'changing the parent' do
+ let(:former_parent_attributes) do
+ {
+ subject: 'former parent',
+ project_id: project.id,
+ type_id: type.id,
+ author_id: user.id,
+ status_id: status.id,
+ priority:,
+ start_date: Time.zone.today + 3.days,
+ due_date: Time.zone.today + 9.days
+ }
+ end
+ let(:attributes) { { parent: new_parent_work_package } }
+ let(:former_parent_work_package) do
+ create(:work_package, former_parent_attributes)
+ end
- let(:former_sibling_attributes) do
- work_package_attributes.merge(
- subject: 'former sibling',
- parent: former_parent_work_package,
- start_date: Date.today + 3.days,
- due_date: Date.today + 6.days
- )
- end
- let(:former_sibling_work_package) do
- create(:work_package, former_sibling_attributes)
- end
+ let(:former_sibling_attributes) do
+ work_package_attributes.merge(
+ subject: 'former sibling',
+ parent: former_parent_work_package,
+ start_date: Time.zone.today + 3.days,
+ due_date: Time.zone.today + 6.days
+ )
+ end
+ let(:former_sibling_work_package) do
+ create(:work_package, former_sibling_attributes)
+ end
- let(:work_package_attributes) do
- { project_id: project.id,
- type_id: type.id,
- author_id: user.id,
- status_id: status.id,
- priority:,
- parent: former_parent_work_package,
- start_date: Date.today + 7.days,
- due_date: Date.today + 9.days }
- end
+ let(:work_package_attributes) do
+ { project_id: project.id,
+ type_id: type.id,
+ author_id: user.id,
+ status_id: status.id,
+ priority:,
+ parent: former_parent_work_package,
+ start_date: Time.zone.today + 7.days,
+ due_date: Time.zone.today + 9.days }
+ end
- let(:new_parent_attributes) do
- work_package_attributes.merge(
- subject: 'new parent',
- parent: nil,
- start_date: Date.today + 10.days,
- due_date: Date.today + 12.days
- )
- end
- let(:new_parent_work_package) do
- create(:work_package, new_parent_attributes)
- end
+ let(:new_parent_attributes) do
+ work_package_attributes.merge(
+ subject: 'new parent',
+ parent: nil,
+ start_date: Time.zone.today + 10.days,
+ due_date: Time.zone.today + 12.days
+ )
+ end
+ let(:new_parent_work_package) do
+ create(:work_package, new_parent_attributes)
+ end
- let(:new_sibling_attributes) do
- work_package_attributes.merge(
- subject: 'new sibling',
- parent: new_parent_work_package,
- start_date: Date.today + 10.days,
- due_date: Date.today + 12.days
- )
- end
- let(:new_sibling_work_package) do
- create(:work_package, new_sibling_attributes)
- end
+ let(:new_sibling_attributes) do
+ work_package_attributes.merge(
+ subject: 'new sibling',
+ parent: new_parent_work_package,
+ start_date: Time.zone.today + 10.days,
+ due_date: Time.zone.today + 12.days
+ )
+ end
+ let(:new_sibling_work_package) do
+ create(:work_package, new_sibling_attributes)
+ end
- before do
- work_package.reload
- former_parent_work_package.reload
- former_sibling_work_package.reload
- new_parent_work_package.reload
- new_sibling_work_package.reload
- end
+ before do
+ work_package.reload
+ former_parent_work_package.reload
+ former_sibling_work_package.reload
+ new_parent_work_package.reload
+ new_sibling_work_package.reload
+ end
- it 'changes the parent reference and reschedules former and new parent' do
- expect(subject)
- .to be_success
+ it 'changes the parent reference and reschedules former and new parent' do
+ expect(subject)
+ .to be_success
- # sets the parent and leaves the dates unchanged
- work_package.reload
- expect(work_package.parent)
- .to eql new_parent_work_package
- expect(work_package.start_date)
- .to eql work_package_attributes[:start_date]
- expect(work_package.due_date)
- .to eql work_package_attributes[:due_date]
-
- # updates the former parent's dates based on the only remaining child (former sibling)
- former_parent_work_package.reload
- expect(former_parent_work_package.start_date)
- .to eql former_sibling_attributes[:start_date]
- expect(former_parent_work_package.due_date)
- .to eql former_sibling_attributes[:due_date]
-
- # updates the new parent's dates based on the moved work package and its now sibling
- new_parent_work_package.reload
- expect(new_parent_work_package.start_date)
- .to eql work_package_attributes[:start_date]
- expect(new_parent_work_package.due_date)
- .to eql new_sibling_attributes[:due_date]
- end
+ # sets the parent and leaves the dates unchanged
+ work_package.reload
+ expect(work_package.parent)
+ .to eql new_parent_work_package
+ expect(work_package.start_date)
+ .to eql work_package_attributes[:start_date]
+ expect(work_package.due_date)
+ .to eql work_package_attributes[:due_date]
+
+ # updates the former parent's dates based on the only remaining child (former sibling)
+ former_parent_work_package.reload
+ expect(former_parent_work_package.start_date)
+ .to eql former_sibling_attributes[:start_date]
+ expect(former_parent_work_package.due_date)
+ .to eql former_sibling_attributes[:due_date]
+
+ # updates the new parent's dates based on the moved work package and its now sibling
+ new_parent_work_package.reload
+ expect(new_parent_work_package.start_date)
+ .to eql work_package_attributes[:start_date]
+ expect(new_parent_work_package.due_date)
+ .to eql new_sibling_attributes[:due_date]
end
+ end
- describe 'changing the parent with the parent being restricted in moving to an earlier date' do
- # there is actually some time between the new parent and its predecessor
- let(:new_parent_attributes) do
- work_package_attributes.merge(
- subject: 'new parent',
- parent: nil,
- start_date: Date.today + 8.days,
- due_date: Date.today + 14.days
- )
- end
- let(:attributes) { { parent: new_parent_work_package } }
- let(:new_parent_work_package) do
- create(:work_package, new_parent_attributes)
- end
+ describe 'changing the parent with the parent being restricted in moving to an earlier date' do
+ # there is actually some time between the new parent and its predecessor
+ let(:new_parent_attributes) do
+ work_package_attributes.merge(
+ subject: 'new parent',
+ parent: nil,
+ start_date: Time.zone.today + 8.days,
+ due_date: Time.zone.today + 14.days
+ )
+ end
+ let(:attributes) { { parent: new_parent_work_package } }
+ let(:new_parent_work_package) do
+ create(:work_package, new_parent_attributes)
+ end
- let(:new_parent_predecessor_attributes) do
- work_package_attributes.merge(
- subject: 'new parent predecessor',
- parent: nil,
- start_date: Date.today + 1.day,
- due_date: Date.today + 4.days
- )
- end
- let(:new_parent_predecessor_work_package) do
- create(:work_package, new_parent_predecessor_attributes).tap do |wp|
- create(:follows_relation, from: new_parent_work_package, to: wp)
- end
+ let(:new_parent_predecessor_attributes) do
+ work_package_attributes.merge(
+ subject: 'new parent predecessor',
+ parent: nil,
+ start_date: Time.zone.today + 1.day,
+ due_date: Time.zone.today + 4.days
+ )
+ end
+ let(:new_parent_predecessor_work_package) do
+ create(:work_package, new_parent_predecessor_attributes).tap do |wp|
+ create(:follows_relation, from: new_parent_work_package, to: wp)
end
+ end
- let(:work_package_attributes) do
- { project_id: project.id,
- type_id: type.id,
- author_id: user.id,
- status_id: status.id,
- priority:,
- start_date: Date.today,
- due_date: Date.today + 3.days }
- end
+ let(:work_package_attributes) do
+ { project_id: project.id,
+ type_id: type.id,
+ author_id: user.id,
+ status_id: status.id,
+ priority:,
+ start_date: Time.zone.today,
+ due_date: Time.zone.today + 3.days }
+ end
- before do
- work_package.reload
- new_parent_work_package.reload
- new_parent_predecessor_work_package.reload
- end
+ before do
+ work_package.reload
+ new_parent_work_package.reload
+ new_parent_predecessor_work_package.reload
+ end
- it 'reschedules the parent and the work package while adhering to the limitation imposed by the predecessor' do
- expect(subject)
- .to be_success
+ it 'reschedules the parent and the work package while adhering to the limitation imposed by the predecessor' do
+ expect(subject)
+ .to be_success
- # sets the parent and adapts the dates
- # The dates are overwritten as the new parent is unable
- # to move to the dates of its new child because of the follows relation.
- work_package.reload
- expect(work_package.parent)
- .to eql new_parent_work_package
- expect(work_package.start_date)
- .to eql new_parent_predecessor_attributes[:due_date] + 1.day
- expect(work_package.due_date)
- .to eql new_parent_predecessor_attributes[:due_date] + 4.days
-
- # adapts the parent's dates but adheres to its limitations
- # due to the follows relationship
- new_parent_work_package.reload
- expect(new_parent_work_package.start_date)
- .to eql new_parent_predecessor_attributes[:due_date] + 1.day
- expect(new_parent_work_package.due_date)
- .to eql new_parent_predecessor_attributes[:due_date] + 4.days
-
- # leaves the parent's predecessor unchanged
- new_parent_work_package.reload
- expect(new_parent_work_package.start_date)
- .to eql new_parent_predecessor_attributes[:due_date] + 1.day
- expect(new_parent_work_package.due_date)
- .to eql new_parent_predecessor_attributes[:due_date] + 4.days
- end
+ # sets the parent and adapts the dates
+ # The dates are overwritten as the new parent is unable
+ # to move to the dates of its new child because of the follows relation.
+ work_package.reload
+ expect(work_package.parent)
+ .to eql new_parent_work_package
+ expect(work_package.start_date)
+ .to eql new_parent_predecessor_attributes[:due_date] + 1.day
+ expect(work_package.due_date)
+ .to eql new_parent_predecessor_attributes[:due_date] + 4.days
+
+ # adapts the parent's dates but adheres to its limitations
+ # due to the follows relationship
+ new_parent_work_package.reload
+ expect(new_parent_work_package.start_date)
+ .to eql new_parent_predecessor_attributes[:due_date] + 1.day
+ expect(new_parent_work_package.due_date)
+ .to eql new_parent_predecessor_attributes[:due_date] + 4.days
+
+ # leaves the parent's predecessor unchanged
+ new_parent_work_package.reload
+ expect(new_parent_work_package.start_date)
+ .to eql new_parent_predecessor_attributes[:due_date] + 1.day
+ expect(new_parent_work_package.due_date)
+ .to eql new_parent_predecessor_attributes[:due_date] + 4.days
end
+ end
- describe 'removing the parent on a work package which precedes its sibling' do
- let(:work_package_attributes) do
- { project_id: project.id,
- type_id: type.id,
- author_id: user.id,
- status_id: status.id,
- priority:,
- parent: parent_work_package,
- start_date: Date.today,
- due_date: Date.today + 3.days }
- end
- let(:attributes) { { parent: nil } }
-
- let(:parent_attributes) do
- { project_id: project.id,
- subject: 'parent',
- type_id: type.id,
- author_id: user.id,
- status_id: status.id,
- priority:,
- start_date: Date.today,
- due_date: Date.today + 10.days }
- end
+ describe 'removing the parent on a work package which precedes its sibling' do
+ let(:work_package_attributes) do
+ { project_id: project.id,
+ type_id: type.id,
+ author_id: user.id,
+ status_id: status.id,
+ priority:,
+ parent: parent_work_package,
+ start_date: Time.zone.today,
+ due_date: Time.zone.today + 3.days }
+ end
+ let(:attributes) { { parent: nil } }
+
+ let(:parent_attributes) do
+ { project_id: project.id,
+ subject: 'parent',
+ type_id: type.id,
+ author_id: user.id,
+ status_id: status.id,
+ priority:,
+ start_date: Time.zone.today,
+ due_date: Time.zone.today + 10.days }
+ end
- let(:parent_work_package) do
- create(:work_package, parent_attributes)
- end
+ let(:parent_work_package) do
+ create(:work_package, parent_attributes)
+ end
- let(:sibling_attributes) do
- work_package_attributes.merge(
- subject: 'sibling',
- start_date: Date.today + 4.days,
- due_date: Date.today + 10.days
- )
- end
+ let(:sibling_attributes) do
+ work_package_attributes.merge(
+ subject: 'sibling',
+ start_date: Time.zone.today + 4.days,
+ due_date: Time.zone.today + 10.days
+ )
+ end
- let(:sibling_work_package) do
- create(:work_package, sibling_attributes).tap do |wp|
- create(:follows_relation, from: wp, to: work_package)
- end
+ let(:sibling_work_package) do
+ create(:work_package, sibling_attributes).tap do |wp|
+ create(:follows_relation, from: wp, to: work_package)
end
+ end
- before do
- work_package.reload
- parent_work_package.reload
- sibling_work_package.reload
- end
+ before do
+ work_package.reload
+ parent_work_package.reload
+ sibling_work_package.reload
+ end
- it 'removes the parent and reschedules it' do
- expect(subject)
- .to be_success
+ it 'removes the parent and reschedules it' do
+ expect(subject)
+ .to be_success
- # work package itself is unchanged (except for the parent)
- work_package.reload
- expect(work_package.parent)
- .to be_nil
- expect(work_package.start_date)
- .to eql work_package_attributes[:start_date]
- expect(work_package.due_date)
- .to eql work_package_attributes[:due_date]
-
- parent_work_package.reload
- expect(parent_work_package.start_date)
- .to eql sibling_attributes[:start_date]
- expect(parent_work_package.due_date)
- .to eql sibling_attributes[:due_date]
- end
+ # work package itself is unchanged (except for the parent)
+ work_package.reload
+ expect(work_package.parent)
+ .to be_nil
+ expect(work_package.start_date)
+ .to eql work_package_attributes[:start_date]
+ expect(work_package.due_date)
+ .to eql work_package_attributes[:due_date]
+
+ parent_work_package.reload
+ expect(parent_work_package.start_date)
+ .to eql sibling_attributes[:start_date]
+ expect(parent_work_package.due_date)
+ .to eql sibling_attributes[:due_date]
end
+ end
- describe 'replacing the attachments' do
- let!(:old_attachment) do
- create(:attachment, container: work_package)
- end
- let!(:other_users_attachment) do
- create(:attachment, container: nil, author: create(:user))
- end
- let!(:new_attachment) do
- create(:attachment, container: nil, author: user)
- end
+ describe 'replacing the attachments' do
+ let!(:old_attachment) do
+ create(:attachment, container: work_package)
+ end
+ let!(:other_users_attachment) do
+ create(:attachment, container: nil, author: create(:user))
+ end
+ let!(:new_attachment) do
+ create(:attachment, container: nil, author: user)
+ end
- it 'reports on invalid attachments and replaces the existent with the new if everything is valid' do
- work_package.attachments.reload
+ # rubocop:disable RSpec/ExampleLength
+ it 'reports on invalid attachments and replaces the existent with the new if everything is valid' do
+ work_package.attachments.reload
- result = instance.call(attachment_ids: [other_users_attachment.id])
+ result = instance.call(attachment_ids: [other_users_attachment.id])
- expect(result)
- .to be_failure
+ expect(result)
+ .to be_failure
- expect(result.errors.symbols_for(:attachments))
- .to match_array [:does_not_exist]
+ expect(result.errors.symbols_for(:attachments))
+ .to match_array [:does_not_exist]
- expect(work_package.attachments.reload)
- .to match_array [old_attachment]
+ expect(work_package.attachments.reload)
+ .to match_array [old_attachment]
- expect(other_users_attachment.reload.container)
- .to be_nil
+ expect(other_users_attachment.reload.container)
+ .to be_nil
- result = instance.call(attachment_ids: [new_attachment.id])
+ result = instance.call(attachment_ids: [new_attachment.id])
- expect(result)
- .to be_success
+ expect(result)
+ .to be_success
- expect(work_package.attachments.reload)
- .to match_array [new_attachment]
+ expect(work_package.attachments.reload)
+ .to match_array [new_attachment]
- expect(new_attachment.reload.container)
- .to eql work_package
+ expect(new_attachment.reload.container)
+ .to eql work_package
- expect(Attachment.find_by(id: old_attachment.id))
- .to be_nil
+ expect(Attachment.find_by(id: old_attachment.id))
+ .to be_nil
- result = instance.call(attachment_ids: [])
+ result = instance.call(attachment_ids: [])
- expect(result)
- .to be_success
+ expect(result)
+ .to be_success
- expect(work_package.attachments.reload)
- .to be_empty
+ expect(work_package.attachments.reload)
+ .to be_empty
- expect(Attachment.all)
- .to match_array [other_users_attachment]
- end
+ expect(Attachment.all)
+ .to match_array [other_users_attachment]
end
+ # rubocop:enable RSpec/ExampleLength
end
##
@@ -1206,26 +1237,27 @@ describe WorkPackages::UpdateService, 'integration tests', type: :model, with_ma
let(:project_types) { [type, new_type] }
let(:attributes) { { type: new_type } }
- context 'work package does NOT have default status' do
+ context 'when the work package does NOT have default status' do
let(:status) { create(:status) }
it 'assigns the default status' do
- expect(work_package).to receive(:status=).and_call_original
expect(subject).to be_success
expect(work_package.status).to eq(Status.default)
end
end
- context 'work package does have default status' do
+ context 'when the work package does have default status' do
let(:status) { create :default_status }
let!(:workflow_type) do
create(:workflow, type: new_type, role:, old_status_id: status.id)
end
it 'does not set the status' do
- expect(work_package).not_to receive(:status=)
expect(subject).to be_success
+
+ expect(work_package)
+ .not_to be_saved_change_to_status_id
end
end
end
@@ -1236,7 +1268,7 @@ describe WorkPackages::UpdateService, 'integration tests', type: :model, with_ma
let!(:parent) do
create(:work_package,
type: project.types.first,
- project: project,
+ project:,
start_date: Time.zone.today - 1.day,
due_date: Time.zone.today + 5.days)
end
@@ -1249,8 +1281,8 @@ describe WorkPackages::UpdateService, 'integration tests', type: :model, with_ma
let!(:sibling) do
create(:work_package,
type: project.types.first,
- project: project,
- parent: parent,
+ project:,
+ parent:,
start_date: Time.zone.today + 1.day,
due_date: Time.zone.today + 5.days,
"custom_field_#{custom_field.id}": 5)
@@ -1261,9 +1293,9 @@ describe WorkPackages::UpdateService, 'integration tests', type: :model, with_ma
{
start_date: Time.zone.today - 1.day,
due_date: Time.zone.today + 1.day,
- project: project,
+ project:,
type: project.types.first,
- parent: parent,
+ parent:,
"custom_field_#{custom_field.id}": 8
}
end
@@ -1279,5 +1311,71 @@ describe WorkPackages::UpdateService, 'integration tests', type: :model, with_ma
.to eql(sibling.due_date)
end
end
+
+ describe 'updating an invalid work package' do
+ # The work package does not have a required custom field set.
+ let(:custom_field) do
+ create(:int_wp_custom_field, is_required: true, is_for_all: true, default_value: nil).tap do |cf|
+ project.types.first.custom_fields << cf
+ project.work_package_custom_fields << cf
+ end
+ end
+ let(:attributes) { { subject: 'A new subject' } }
+
+ let(:work_package_attributes) do
+ {
+ subject: 'The old subject',
+ project:,
+ type: project.types.first
+ }
+ end
+
+ before do
+ # Creating the custom field after the work package is already saved.
+ work_package
+ custom_field
+ end
+
+ it 'is a failure and does not save the change' do
+ expect(subject).to be_failure
+
+ expect(work_package.reload.subject)
+ .to eql work_package_attributes[:subject]
+ end
+ end
+
+ describe 'updating the type (custom field resetting)' do
+ let(:project_types) { [type, new_type] }
+ let(:new_type) { create(:type) }
+ let!(:custom_field_of_current_type) do
+ create(:int_wp_custom_field, default_value: nil).tap do |cf|
+ type.custom_fields << cf
+ project.work_package_custom_fields << cf
+ end
+ end
+ let!(:custom_field_of_new_type) do
+ create(:int_wp_custom_field, default_value: 8).tap do |cf|
+ new_type.custom_fields << cf
+ project.work_package_custom_fields << cf
+ end
+ end
+ let(:attributes) do
+ { type: new_type }
+ end
+
+ let(:work_package_attributes) do
+ {
+ type:,
+ project:,
+ "custom_field_#{custom_field_of_current_type.id}": 5
+ }
+ end
+
+ it 'is success, removes the existing custom field value and sets the default for the new one' do
+ expect(subject).to be_success
+
+ expect(work_package.reload.custom_values.pluck(:custom_field_id, :value))
+ .to eq [[custom_field_of_new_type.id, "8"]]
+ end
+ end
end
-# rubocop:enable RSpec/MultipleMemoizedHelpers
diff --git a/spec/services/work_packages/update_service_spec.rb b/spec/services/work_packages/update_service_spec.rb
index 2102c69b443..f15cc24af7a 100644
--- a/spec/services/work_packages/update_service_spec.rb
+++ b/spec/services/work_packages/update_service_spec.rb
@@ -27,238 +27,15 @@
#++
require 'spec_helper'
+require 'services/base_services/behaves_like_update_service'
describe WorkPackages::UpdateService, type: :model do
- let(:user) { build_stubbed(:user) }
- let(:project) do
- p = build_stubbed(:project)
- allow(p).to receive(:shared_versions).and_return([])
-
- p
- end
- let(:work_package) do
- wp = build_stubbed(:work_package, project:)
- wp.type = build_stubbed(:type)
- wp.send(:clear_changes_information)
-
- wp
- end
- let(:instance) do
- described_class.new(user:,
- model: work_package)
- end
-
- before do
- # Stub update_ancestors because it messes with the jouralizing expectations
- allow(instance).to receive(:update_ancestors).and_return []
- end
-
- describe 'call' do
- let(:set_attributes_service) do
- service = double("WorkPackages::SetAttributesService",
- new: set_attributes_service_instance)
-
- stub_const('WorkPackages::SetAttributesService', service)
-
- service
- end
- let(:send_notifications) { true }
- let(:set_attributes_service_instance) do
- instance = double("WorkPackages::SetAttributesServiceInstance")
-
- allow(instance)
- .to receive(:call) do |attributes|
- work_package.attributes = attributes
-
- set_service_results
- end
-
- instance
- end
- let(:errors) { [] }
- let(:set_service_results) { ServiceResult.new success: true, result: work_package }
- let(:work_package_save_result) { true }
-
+ # This is now only a very basic test testing the structure of the service.
+ # The domain tests are in the update_service_integration_spec.rb
+ it_behaves_like 'BaseServices update service' do
before do
- set_attributes_service
- end
-
- before do
- expect(Journal::NotificationConfiguration)
- .to receive(:with)
- .with(send_notifications)
- .and_yield
-
- allow(work_package)
- .to receive(:save)
- .and_return work_package_save_result
- end
-
- shared_examples_for 'service call' do
- subject { instance.call(**call_attributes.merge(send_notifications:).symbolize_keys) }
-
- it 'is successful' do
- expect(subject.success?).to be_truthy
- end
-
- it 'sets the value' do
- subject
-
- attributes.each do |attribute, key|
- expect(work_package.send(attribute)).to eql key
- end
- end
-
- it 'has no errors' do
- expect(subject.errors.all?(&:empty?)).to be_truthy
- end
-
- context 'when setting the attributes is unsuccessful (invalid)' do
- let(:errors) { ActiveModel::Errors.new(work_package) }
- let(:set_service_results) { ServiceResult.new success: false, errors:, result: work_package }
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it 'does not persist the changes' do
- subject
-
- expect(work_package).not_to receive(:save)
- end
-
- it 'exposes the errors' do
- errors.add(:base, 'This is a custom error!')
-
- subject
-
- expect(subject.errors).to eql errors
- expect(subject.errors[:base]).to include 'This is a custom error!'
- end
- end
-
- context 'when the saving is unsuccessful' do
- let(:work_package_save_result) { false }
- let(:saving_errors) { ActiveModel::Errors.new(work_package) }
-
- before do
- allow(work_package)
- .to receive(:errors)
- .and_return(saving_errors)
- end
-
- it 'is unsuccessful' do
- expect(subject.success?).to be_falsey
- end
-
- it 'leaves the value unchanged' do
- subject
-
- expect(work_package.changed?).to be_truthy
- end
-
- it 'exposes the errors, but is a new instance' do
- saving_errors.add(:base, 'This is a custom error!')
-
- subject
-
- expect(subject.errors).not_to eql saving_errors
- expect(subject.errors[:base]).to include 'This is a custom error!'
- end
- end
- end
-
- context 'update subject before calling the service' do
- let(:call_attributes) { {} }
- let(:attributes) { { subject: 'blubs blubs' } }
-
- before do
- work_package.attributes = attributes
- end
-
- it_behaves_like 'service call'
- end
-
- context 'updating subject via attributes' do
- let(:call_attributes) { attributes }
- let(:attributes) { { subject: 'blubs blubs' } }
-
- it_behaves_like 'service call'
- end
-
- context 'when switching the project' do
- let(:target_project) do
- build_stubbed(:project)
- end
- let(:call_attributes) { attributes }
- let(:attributes) { { project: target_project } }
-
- it_behaves_like 'service call'
-
- context 'relations' do
- let!(:scope) do
- instance_double(ActiveRecord::Relation).tap do |relations|
- allow(Relation)
- .to receive(:of_work_package)
- .with([work_package])
- .and_return(relations)
- allow(relations)
- .to receive(:destroy_all)
- end
- end
-
- it 'removes the relations if the setting does not permit cross project relations' do
- allow(Setting)
- .to receive(:cross_project_work_package_relations?)
- .and_return false
-
- instance.call(project: target_project)
-
- expect(scope)
- .to have_received(:destroy_all)
- end
-
- it 'leaves the relations unchanged if the setting allows cross project relations' do
- allow(Setting)
- .to receive(:cross_project_work_package_relations?)
- .and_return true
-
- instance.call(project: target_project)
-
- expect(scope)
- .not_to have_received(:destroy_all)
- end
- end
-
- context 'time_entries' do
- it 'moves the time entries' do
- scope = double('scope')
-
- expect(TimeEntry)
- .to receive(:on_work_packages)
- .with([work_package])
- .and_return(scope)
-
- expect(scope)
- .to receive(:update_all)
- .with(project_id: target_project.id)
-
- instance.call(project: target_project)
- end
- end
- end
-
- context 'when switching the type' do
- let(:target_type) { build_stubbed(:type) }
-
- context 'custom_values' do
- it 'resets the custom values' do
- expect(work_package)
- .to receive(:reset_custom_values!)
-
- instance.call(type: target_type)
- end
- end
+ allow(set_attributes_errors)
+ .to receive(:merge!)
end
end
end