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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
introduce wp bulk services
ulfertsb66a49136702
diff --git a/app/controllers/work_packages/bulk_controller.rb b/app/controllers/work_packages/bulk_controller.rbindex 4d77c70efa8..7a7e6b6aaa4 100644--- a/app/controllers/work_packages/bulk_controller.rb+++ b/app/controllers/work_packages/bulk_controller.rb@@ -46,7 +46,7 @@ class WorkPackages::BulkController < ApplicationControllerdef update@call = ::WorkPackages::Bulk::UpdateService.new(user: current_user, work_packages: @work_packages)- .call(params: params)+ .call(attributes_for_update)if @call.success?flash[:notice] = t(:notice_successful_update)@@ -88,8 +88,7 @@ class WorkPackages::BulkController < ApplicationControllerdef setup_edit@available_statuses = @projects.map { |p| Workflow.available_statuses(p) }.inject(&:&)@custom_fields = @projects.map(&:all_work_package_custom_fields).inject(&:&)- @assignables = possible_assignees- @responsibles = @assignables+ @assignables = @responsibles = possible_assignees@types = @projects.map(&:types).inject(&:&)end@@ -111,6 +110,17 @@ class WorkPackages::BulkController < ApplicationControllerendend+ def attributes_for_update+ return {} unless params.has_key? :work_package++ permitted_params+ .update_work_package+ .tap { |attributes| attributes[:custom_field_values]&.reject! { |_k, v| v.blank? } }+ .reject { |_k, v| v.blank? }+ .transform_values { |v| v == 'none' ? '' : v }+ .to_h+ end+def usercurrent_userenddiff --git a/app/controllers/work_packages/moves_controller.rb b/app/controllers/work_packages/moves_controller.rbindex ab6904889a8..0a5dd214e18 100644--- a/app/controllers/work_packages/moves_controller.rb+++ b/app/controllers/work_packages/moves_controller.rb@@ -42,37 +42,31 @@ class WorkPackages::MovesController < ApplicationControllerdef createprepare_for_work_package_move- new_type = params[:new_type_id].blank? ? nil : @target_project.types.find_by(id: params[:new_type_id])+ result = modify_call- result = ServiceResult.new success: true, result: true-- @work_packages.each do |work_package|- work_package.reload-- call_hook(:controller_work_packages_move_before_save,- params: params,- work_package: work_package,- target_project: @target_project,- copy: !!@copy)+ set_flash_from_bulk_work_package_save(@work_packages, result)- service_call = WorkPackages::MoveService- .new(work_package, current_user)- .call(@target_project,- new_type,- copy: @copy,- attributes: permitted_create_params,- journal_note: @notes)+ redirect_after_create(result)+ end- result.add_dependent!(service_call)- end+ private- result.result = false if result.failure?+ def modify_call+ klass = if @copy+ WorkPackages::Bulk::CopyService+ else+ WorkPackages::Bulk::MoveService+ end- set_flash_from_bulk_work_package_save(@work_packages, result)+ klass+ .new(user: current_user, work_packages: @work_packages)+ .call(permitted_create_params)+ end+ def redirect_after_create(result)if params[:follow]if result.success? && @work_packages.size == 1- redirect_to work_package_path(@work_packages.first)+ redirect_to work_package_path(result.dependent_results.first.result)elseredirect_to project_work_packages_path(@target_project || @project)end@@ -81,8 +75,6 @@ class WorkPackages::MovesController < ApplicationControllerendend- private-def set_flash_from_bulk_work_package_save(work_packages, service_result)if service_result.success? && work_packages.any?flash[:notice] = @copy ? I18n.t(:notice_successful_create) : I18n.t(:notice_successful_update)@@ -116,22 +108,6 @@ class WorkPackages::MovesController < ApplicationController@available_statuses = Workflow.available_statuses(@project)@notes = params[:notes]@notes ||= ''-- # When copying, avoid copying elements when any of their ancestors- # are in the set to be copied- if @copy- @work_packages = remove_hierarchy_duplicates- end- end-- # Check if a parent work package is also selected for copying- def remove_hierarchy_duplicates- # Get all ancestors of the work_packages to copy- selected_ids = @work_packages.pluck(:id)-- @work_packages.reject do |wp|- wp.ancestors.where(id: selected_ids).exists?- endenddef permitted_create_params@@ -144,6 +120,9 @@ class WorkPackages::MovesController < ApplicationController:version_id,:priority_id).to_h+ .merge(type_id: params[:new_type_id],+ project_id: params[:new_project_id],+ journal_notes: params[:notes]).reject { |_, v| v.blank? }endenddiff --git a/app/services/work_packages/bulk/bulked_service.rb b/app/services/work_packages/bulk/bulked_service.rbnew file mode 100644index 00000000000..a718d526c8a--- /dev/null+++ b/app/services/work_packages/bulk/bulked_service.rb@@ -0,0 +1,83 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module WorkPackages+ module Bulk+ class BulkedService+ include ::Shared::ServiceContext+ include ::HookHelper++ attr_accessor :user, :work_packages++ def initialize(user:, work_packages:)+ self.user = user+ self.work_packages = work_packages+ end++ def call(params)+ # This is not an all or nothing service. We currently accept that+ # one work package might be moved while another one fails.+ # Personally, I'd rather wrap it in a transaction.+ without_context_transaction(params[:send_notification] == '1') do+ bulk(params)+ end+ end++ private++ def bulk(params)+ result = ServiceResult.new success: true, result: true++ work_packages.each do |work_package|+ # As updating one work package might have already saved another one,+ # e.g. by changing the start/due date or the version+ # we need to reload the work packages to avoid running into stale object errors.+ work_package.reload++ call_move_hook(work_package, params)++ result.add_dependent!(alter_work_package(work_package, params))+ end++ result.result = false if result.failure?++ result+ end++ def alter_work_package(_work_package, _params)+ raise NotImplementedError+ end++ def call_move_hook(_work_package, _params)+ raise NotImplementedError+ end+ end+ end+enddiff --git a/app/services/work_packages/bulk/copy_service.rb b/app/services/work_packages/bulk/copy_service.rbnew file mode 100644index 00000000000..e0cbc36c647--- /dev/null+++ b/app/services/work_packages/bulk/copy_service.rb@@ -0,0 +1,102 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module WorkPackages+ module Bulk+ class CopyService < BulkedService+ def initialize(user:, work_packages:)+ super++ self.work_packages = remove_hierarchy_duplicates(work_packages)+ end++ private++ def alter_work_package(work_package, attributes)+ ancestors = {}+ result = copy_with_updated_parent_id(work_package, attributes, ancestors)++ work_package+ .descendants+ .order_by_ancestors('asc')+ .each do |wp|+ copied = copy_with_updated_parent_id(wp, attributes, ancestors)++ result.add_dependent!(copied)+ end++ result+ end++ def call_move_hook(work_package, params)+ return if OpenProject::Hook.hook_listeners(:controller_work_packages_move_before_save).empty?++ call_hook(:controller_work_packages_move_before_save,+ params: params,+ work_package: work_package,+ target_project: params[:project_id] ? Project.find_by(id: params[:project_id]) : nil,+ copy: true)+ end++ def copy_with_updated_parent_id(work_package, attributes, ancestors)+ with_updated_parent_id(work_package, attributes, ancestors) do |overridden_attributes|+ WorkPackages::CopyService+ .new(user: user,+ work_package: work_package)+ .call(**overridden_attributes.symbolize_keys)+ end+ end++ def with_updated_parent_id(work_package, attributes, ancestors)+ # avoid modifying attributes which could carry over+ # to the next work_package+ overridden_attributes = attributes.dup++ overridden_attributes[:parent_id] = ancestors[work_package.parent_id] || work_package.parent_id if work_package.parent_id++ copied = yield overridden_attributes++ ancestors[work_package.id] = copied.result.id++ copied+ end++ # Check if a parent work package is also selected for copying+ def remove_hierarchy_duplicates(work_packages)+ # Get all ancestors of the work_packages to copy+ selected_ids = work_packages.pluck(:id)++ work_packages.reject do |wp|+ wp.ancestors.exists?(id: selected_ids)+ end+ end+ end+ end+enddiff --git a/app/services/work_packages/bulk/move_service.rb b/app/services/work_packages/bulk/move_service.rbnew file mode 100644index 00000000000..4762184623d--- /dev/null+++ b/app/services/work_packages/bulk/move_service.rb@@ -0,0 +1,54 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module WorkPackages+ module Bulk+ class MoveService < BulkedService+ private++ def alter_work_package(work_package, params)+ WorkPackages::UpdateService+ .new(user: user,+ model: work_package)+ .call(**params.symbolize_keys)+ end++ def call_move_hook(work_package, params)+ return if OpenProject::Hook.hook_listeners(:controller_work_packages_move_before_save).empty?++ call_hook(:controller_work_packages_move_before_save,+ params: params,+ work_package: work_package,+ target_project: params[:project_id] ? Project.find_by(id: params[:project_id]) : nil,+ copy: false)+ end+ end+ end+enddiff --git a/app/services/work_packages/bulk/update_service.rb b/app/services/work_packages/bulk/update_service.rbindex a87dc4f4ba0..84b96a9d839 100644--- a/app/services/work_packages/bulk/update_service.rb+++ b/app/services/work_packages/bulk/update_service.rb@@ -30,64 +30,19 @@module WorkPackagesmodule Bulk- class UpdateService- include ::Shared::ServiceContext- include ::HookHelper-- attr_accessor :user, :work_packages, :permitted_params-- def initialize(user:, work_packages:)- self.user = user- self.work_packages = work_packages- end-- def call(params:)- self.permitted_params = PermittedParams.new(params, user)- in_user_context(params[:send_notification] == '1') do- bulk_update(params)- end- end-+ class UpdateService < BulkedServiceprivate- def bulk_update(params)- result = ServiceResult.new success: true, result: true-- work_packages.each do |work_package|- # As updating one work package might have already saved another one,- # e.g. by changing the start/due date or the version- # we need to reload the work packages to avoid running into stale object errors.- work_package.reload-- # filter parameters by whitelist and add defaults- attributes = parse_params_for_bulk_work_package_attributes params, work_package.project-- call_hook(:controller_work_packages_bulk_edit_before_save, params: params, work_package: work_package)-- service_call = WorkPackages::UpdateService- .new(user: user, model: work_package)- .call(**attributes.symbolize_keys)-- result.add_dependent!(service_call)- end-- result.result = false if result.failure?-- result+ def alter_work_package(work_package, attributes)+ WorkPackages::UpdateService+ .new(user: user, model: work_package)+ .call(**attributes.symbolize_keys)end- # TODO: move params transformation out of here as this is not the responsibility of a service- def parse_params_for_bulk_work_package_attributes(params, project)- return {} unless params.has_key? :work_package-- safe_params = permitted_params.update_work_package project: project- attributes = safe_params.reject { |_k, v| v.blank? }- attributes.each_key do |k|- attributes[k] = '' if attributes[k] == 'none'- end- attributes[:custom_field_values]&.reject! { |_k, v| v.blank? }- attributes.delete :custom_field_values if not attributes.has_key?(:custom_field_values) or attributes[:custom_field_values].empty?- attributes.to_h+ def call_move_hook(work_package, params)+ call_hook(:controller_work_packages_bulk_edit_before_save,+ params: params,+ work_package: work_package)endendenddiff --git a/app/services/work_packages/move_service.rb b/app/services/work_packages/move_service.rbdeleted file mode 100644index 373f5f3914b..00000000000--- a/app/services/work_packages/move_service.rb+++ /dev/null@@ -1,74 +0,0 @@-# Moves/copies an work_package to a new project and type--class WorkPackages::MoveService- attr_accessor :work_package,- :user-- def initialize(work_package, user)- self.work_package = work_package- self.user = user- end-- def call(new_project, new_type = nil, options = {})- attributes = options[:attributes] || {}- attributes[:project] = new_project- attributes[:type] = new_type if new_type- attributes[:journal_notes] = options[:journal_note] if options[:journal_note]-- if options[:copy]- copy_with_descendants(attributes)- else- update(attributes)- end- end-- def copy_with_descendants(attributes)- ancestors = {}- result = copy_with_updated_parent_id(work_package, attributes, ancestors)-- work_package- .descendants- .order_by_ancestors('asc')- .each do |wp|- copied = copy_with_updated_parent_id(wp, attributes, ancestors)-- result.add_dependent!(copied)- end-- result- end-- def copy(wp, attributes)- WorkPackages::CopyService- .new(user: user,- work_package: wp)- .call(**attributes.symbolize_keys)- end-- def update(attributes)- WorkPackages::UpdateService- .new(user: user,- model: work_package)- .call(**attributes.symbolize_keys)- end-- def copy_with_updated_parent_id(wp, attributes, ancestors)- with_updated_parent_id(wp, attributes, ancestors) do |overridden_attributes|- copy(wp, overridden_attributes)- end- end-- def with_updated_parent_id(work_package, attributes, ancestors)- # avoid modifying attributes which could carry over- # to the next work_package- overridden_attributes = attributes.dup-- overridden_attributes[:parent_id] = ancestors[work_package.parent_id] || work_package.parent_id if work_package.parent_id-- copied = yield overridden_attributes-- ancestors[work_package.id] = copied.result.id-- copied- end-enddiff --git a/app/views/work_packages/bulk/edit.html.erb b/app/views/work_packages/bulk/edit.html.erbindex baddb27f532..8cfbe5101bb 100644--- a/app/views/work_packages/bulk/edit.html.erb+++ b/app/views/work_packages/bulk/edit.html.erb@@ -171,8 +171,8 @@ See COPYRIGHT and LICENSE files for more details.</fieldset><fieldset class="form--fieldset"><legend class="form--fieldset-legend"><%= Journal.human_attribute_name(:notes) %></legend>- <%= label_tag 'notes', Journal.human_attribute_name(:notes), class: 'hidden-for-sighted' %>- <%= styled_text_area_tag 'notes', @notes, class: 'wiki-edit', with_text_formatting: true %>+ <%= label_tag 'work_package_journal_notes', Journal.human_attribute_name(:notes), class: 'hidden-for-sighted' %>+ <%= styled_text_area_tag 'work_package[journal_notes]', @notes, class: 'wiki-edit', with_text_formatting: true %><p><%= send_notification_option %></p></fieldset></section>diff --git a/modules/backlogs/spec/models/issue_position_spec.rb b/modules/backlogs/spec/models/issue_position_spec.rbindex a35f073caaa..9920e3e3633 100644--- a/modules/backlogs/spec/models/issue_position_spec.rb+++ b/modules/backlogs/spec/models/issue_position_spec.rb@@ -267,9 +267,9 @@ describe WorkPackage, type: :model doshared_let(:admin) { FactoryBot.create :admin }def move_to_project(work_package, project)- service = WorkPackages::MoveService.new(work_package, admin)-- service.call(project)+ WorkPackages::UpdateService+ .new(model: work_package, user: admin)+ .call(project: project)endbefore dodiff --git a/modules/backlogs/spec/models/version_spec.rb b/modules/backlogs/spec/models/version_spec.rbindex 638185d4bdb..e6dde991ec3 100644--- a/modules/backlogs/spec/models/version_spec.rb+++ b/modules/backlogs/spec/models/version_spec.rb@@ -57,9 +57,9 @@ describe Version, type: :model doshared_let(:admin) { FactoryBot.create :admin }def move_to_project(work_package, project)- service = WorkPackages::MoveService.new(work_package, admin)-- service.call(project)+ WorkPackages::UpdateService+ .new(model: work_package, user: admin)+ .call(project: project)endbefore dodiff --git a/modules/costs/spec/models/work_package_spec.rb b/modules/costs/spec/models/work_package_spec.rbindex 38d760d1243..4355efb0016 100644--- a/modules/costs/spec/models/work_package_spec.rb+++ b/modules/costs/spec/models/work_package_spec.rb@@ -48,9 +48,9 @@ describe WorkPackage, type: :model dolet!(:budget) { FactoryBot.create(:budget, project: project) }def move_to_project(work_package, project)- service = WorkPackages::MoveService.new(work_package, user)-- service.call(project)+ WorkPackages::UpdateService+ .new(model: work_package, user: user)+ .call(project: project)endit 'should update cost entries on move' dodiff --git a/spec/controllers/work_packages/bulk_controller_spec.rb b/spec/controllers/work_packages/bulk_controller_spec.rbindex 9679d741fcd..f81147966a7 100644--- a/spec/controllers/work_packages/bulk_controller_spec.rb+++ b/spec/controllers/work_packages/bulk_controller_spec.rb@@ -222,65 +222,16 @@ describe WorkPackages::BulkController, type: :controller, with_settings: { journendend- context 'when updating two work packages with differing whitelisted params' do- let!(:work_package_ids) { [work_package_1.id, work_package_3.id] }-- let!(:role_with_permission_to_add_watchers) do- FactoryBot.create(:role, permissions: role.permissions + [:add_work_package_watchers])- end- let!(:other_user) { FactoryBot.create :user }-- let!(:other_member_1) do- FactoryBot.create(:member,- project: project_1,- principal: other_user,- roles: [role_with_permission_to_add_watchers])- end- let!(:other_member_2) do- FactoryBot.create(:member,- project: project_2,- principal: other_user,- roles: [role])- end-- let(:description) { 'Text' }- let(:work_package_params) do- { description: description, watcher_user_ids: [user.id] }- end-- before do- # create user memberships to allow the user to watch work packages- member1_p1- member1_p2- # let other_user perform the bulk update- allow(User).to receive(:current).and_return other_user- put :update, params: { ids: work_package_ids, work_package: work_package_params }- end-- it 'updates the description if whitelisted' do- expect(work_package_1.reload.description).to eq(description)- expect(work_package_3.reload.description).to eq(description)- end-- it 'updates the watchers if the watcher user ids are whitelisted' do- expect(work_package_1.reload.watcher_users).to include user- end-- it 'does not update the watchers if the watcher user ids are not whitelisted' do- expect(work_package_3.reload.watcher_users).not_to include user- end- end-shared_context 'update_request' dobefore doput :update,params: {ids: work_package_ids,- notes: 'Bulk editing',work_package: { priority_id: priority.id,assigned_to_id: group_id,responsible_id: responsible_id,- send_notification: send_notification }+ send_notification: send_notification,+ journal_notes: 'Bulk editing' }}endend@@ -401,10 +352,14 @@ describe WorkPackages::BulkController, type: :controller, with_settings: { journendcontext 'not allowed' do+ render_views+include_context 'update_request'it 'does not succeed' do- expect(assigns[:bulk_errors].keys).to match_array(work_package_ids)+ expect(flash[:error])+ .to include(I18n.t(:'work_packages.bulk.none_could_be_saved',+ total: 2))expect(subject).to match_array [user.id]endenddiff --git a/spec/controllers/work_packages/moves_controller_spec.rb b/spec/controllers/work_packages/moves_controller_spec.rbindex d9b50f541f6..80dd46cfd7a 100644--- a/spec/controllers/work_packages/moves_controller_spec.rb+++ b/spec/controllers/work_packages/moves_controller_spec.rb@@ -292,7 +292,7 @@ describe WorkPackages::MovesController, type: :controller, with_settings: { jourendit 'redirects to the work package copy' do- copy = WorkPackage.order(Arel.sql('id desc')).first+ copy = WorkPackage.order(id: :desc).firstis_expected.to redirect_to(work_package_path(copy))endend@@ -456,29 +456,6 @@ describe WorkPackages::MovesController, type: :controller, with_settings: { jourendendend-- context 'on create' do- it 'only copies the parent work package' do- expect(::WorkPackages::MoveService)- .to receive(:new)- .with(work_package, current_user)- .and_call_original-- expect(::WorkPackages::MoveService)- .not_to receive(:new)- .with(child_wp, current_user)-- expect do- post :create,- params: {- ids: [work_package.id, child_wp.id],- copy: ''- }- end.to change(WorkPackage, :count).by(2)-- expect(flash[:notice]).to eq(I18n.t(:notice_successful_create))- end- endendcontext 'child work package from one project to other' do@@ -519,6 +496,8 @@ describe WorkPackages::MovesController, type: :controller, with_settings: { jourendcontext 'when cross_project_work_package_relations is disabled' do+ render_views+before doallow(Setting).to receive(:cross_project_work_package_relations?).and_return(false)@@ -527,10 +506,8 @@ describe WorkPackages::MovesController, type: :controller, with_settings: { jourit 'is unsuccessful' doexpect(flash[:error])- .to eq(I18n.t(:notice_failed_to_save_work_packages,- count: 1,- total: 1,- ids: "##{child_wp.id}"))+ .to include(I18n.t(:'work_packages.bulk.none_could_be_saved',+ total: 1))endenddiff --git a/spec/features/work_packages/bulk/copy_work_package_spec.rb b/spec/features/work_packages/bulk/copy_work_package_spec.rbindex b29e876c021..5db7536dafa 100644--- a/spec/features/work_packages/bulk/copy_work_package_spec.rb+++ b/spec/features/work_packages/bulk/copy_work_package_spec.rb@@ -45,6 +45,7 @@ describe 'Copy work packages through Rails view', js: true dolet(:wp_table) { ::Pages::WorkPackagesTable.new(project) }let(:context_menu) { Components::WorkPackages::ContextMenu.new }let(:display_representation) { ::Components::WorkPackages::DisplayRepresentation.new }+ let(:notes) { ::Components::WysiwygEditor.new }before dologin_as current_user@@ -73,8 +74,9 @@ describe 'Copy work packages through Rails view', js: true doexpect(page).to have_select('Project', selected: 'Target')end- it 'sets the version on copy' do+ it 'sets the version on copy and leaves a note' doselect version.name, from: 'version_id'+ notes.set_markdown 'A note on copy'click_on 'Copy and follow'wp_table.expect_work_package_count 2@@ -88,24 +90,36 @@ describe 'Copy work packages through Rails view', js: true docopied_wps = WorkPackage.last(2)expect(copied_wps.map(&:project_id).uniq).to eq([project2.id])expect(copied_wps.map(&:version_id).uniq).to eq([version.id])+ expect(copied_wps.map { |wp| wp.journals.last.notes }.uniq).to eq(['A note on copy'])end- it 'moves parent and child wp to a new project' do- click_on 'Copy and follow'+ context 'with a work package having a child' do+ let!(:child) do+ FactoryBot.create(:work_package,+ author: dev,+ project: project,+ type: type,+ parent: work_package)+ end- expect_angular_frontend_initialized- wp_table.expect_work_package_count 2- expect(page).to have_selector('#projects-menu', text: 'Target')+ it 'moves parent and child wp to a new project with the hierarchy amended' do+ click_on 'Copy and follow'- # Should not move the sources- work_package2.reload- work_package.reload- expect(work_package.project_id).to eq(project.id)- expect(work_package2.project_id).to eq(project.id)+ expect_angular_frontend_initialized+ wp_table.expect_work_package_count 3+ expect(page).to have_selector('#projects-menu', text: 'Target')- # Check project of last two created wps- copied_wps = WorkPackage.last(2)- expect(copied_wps.map(&:project_id)).to eq([project2.id, project2.id])+ # Should not move the sources+ expect(work_package.reload.project_id).to eq(project.id)+ expect(work_package2.reload.project_id).to eq(project.id)++ # Check project of last two created wps+ copied_wps = WorkPackage.last(3)+ expect(copied_wps.map(&:project_id).uniq).to eq([project2.id])++ expect(project2.work_packages.find_by(subject: child.subject).parent)+ .to eq project2.work_packages.find_by(subject: work_package.subject)+ endendcontext 'when the target project does not have the type' dodiff --git a/spec/features/work_packages/bulk/move_work_package_spec.rb b/spec/features/work_packages/bulk/move_work_package_spec.rbindex 676f6882bc9..59a2ade73da 100644--- a/spec/features/work_packages/bulk/move_work_package_spec.rb+++ b/spec/features/work_packages/bulk/move_work_package_spec.rb@@ -146,9 +146,12 @@ describe 'Moving a work package through Rails view', js: true doexpect(page).to have_selector('.flash.error',text: I18n.t('work_packages.bulk.could_not_be_saved'))+expect(page)- .to have_selector('.flash.error',- text: I18n.t('activerecord.errors.messages.error_readonly'))+ .to have_selector(+ '.flash.error',+ text: "#{work_package2.id}: Project #{I18n.t('activerecord.errors.messages.error_readonly')}"+ )expect(page).to have_selector('.flash.error',diff --git a/spec/features/work_packages/bulk/update_work_package_spec.rb b/spec/features/work_packages/bulk/update_work_package_spec.rbindex 3ff2d87a5c1..b92c73ba44a 100644--- a/spec/features/work_packages/bulk/update_work_package_spec.rb+++ b/spec/features/work_packages/bulk/update_work_package_spec.rb@@ -60,6 +60,7 @@ describe 'Bulk update work packages through Rails view', js: true dolet(:wp_table) { ::Pages::WorkPackagesTable.new(project) }let(:context_menu) { Components::WorkPackages::ContextMenu.new }let(:display_representation) { ::Components::WorkPackages::DisplayRepresentation.new }+ let(:notes) { ::Components::WysiwygEditor.new }before dologin_as current_user@@ -82,19 +83,21 @@ describe 'Bulk update work packages through Rails view', js: true do# On work packages edit pageexpect(page).to have_selector('#work_package_status_id')select status2.name, from: 'work_package_status_id'+ notes.set_markdown('The typed note')end- it 'sets two statuses' do+ it 'sets status and leaves a note' doclick_on 'Submit'expect_angular_frontend_initializedwp_table.expect_work_package_count 2# Should update the status- work_package2.reload- work_package.reload- expect(work_package.status_id).to eq(status2.id)- expect(work_package2.status_id).to eq(status2.id)+ expect([work_package.reload.status_id, work_package2.reload.status_id].uniq)+ .to eq([status2.id])++ expect([work_package.journals.last.notes, work_package2.journals.last.notes].uniq)+ .to eq(['The typed note'])endcontext 'when making an error in the form' dodiff --git a/spec/services/work_packages/move_service_spec.rb b/spec/services/work_packages/move_service_spec.rbdeleted file mode 100644index 11458240527..00000000000--- a/spec/services/work_packages/move_service_spec.rb+++ /dev/null@@ -1,167 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2021 the OpenProject GmbH-#-# This program is free software; you can redistribute it and/or-# modify it under the terms of the GNU General Public License version 3.-#-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:-# Copyright (C) 2006-2013 Jean-Philippe Lang-# Copyright (C) 2010-2013 the ChiliProject Team-#-# This program is free software; you can redistribute it and/or-# modify it under the terms of the GNU General Public License-# as published by the Free Software Foundation; either version 2-# of the License, or (at your option) any later version.-#-# This program is distributed in the hope that it will be useful,-# but WITHOUT ANY WARRANTY; without even the implied warranty of-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-# GNU General Public License for more details.-#-# You should have received a copy of the GNU General Public License-# along with this program; if not, write to the Free Software-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.-#-# See COPYRIGHT and LICENSE files for more details.-#++--require 'spec_helper'--describe WorkPackages::MoveService, type: :model do- let(:user) { FactoryBot.build_stubbed(:user) }- let(:work_package) do- FactoryBot.build_stubbed(:stubbed_work_package)- end- let(:type) { FactoryBot.build_stubbed(:type) }- let(:project) { FactoryBot.build_stubbed(:project) }-- let(:instance) { described_class.new(work_package, user) }- let(:child_service_result_work_package) { work_package }- let(:child_service_result) do- ServiceResult.new success: true,- result: child_service_result_work_package- end-- context 'when copying' do- let(:expected_attributes) { { project: project } }- let(:child_service_result_work_package) { FactoryBot.build_stubbed(:stubbed_work_package) }-- before do- copy_double = double('copy service double')-- expect(WorkPackages::CopyService)- .to receive(:new)- .with(user: user,- work_package: work_package)- .and_return(copy_double)-- expect(copy_double)- .to receive(:call)- .with(expected_attributes)- .and_return(child_service_result)-- allow(work_package)- .to receive_message_chain(:self_and_descendants, :order_by_ancestors)- .and_return [work_package]- end-- it 'calls the copy service and merges its result' do- expect(instance.call(project, nil, copy: true).result)- .to eql child_service_result.result- end-- context 'when providing a type and attributes' do- let(:expected_attributes) do- { project: project,- type: type,- subject: 'blubs' }- end-- it 'calls the copy service and merges its result' do- expect(instance.call(project, type, attributes: { subject: 'blubs' }, copy: true).result)- .to eql child_service_result.result- end- end-- context 'for a parent' do- let(:child_work_package) do- FactoryBot.build_stubbed(:stubbed_work_package, parent: work_package)- end- let(:copied_child_work_package) do- FactoryBot.build_stubbed(:stubbed_work_package)- end- let(:child_child_service_result) do- ServiceResult.new success: true,- result: copied_child_work_package- end-- let(:expected_child_attributes) { { project: project, parent_id: child_service_result_work_package.id } }-- before do- copy_double = double('copy child service double')-- expect(WorkPackages::CopyService)- .to receive(:new)- .with(user: user,- work_package: child_work_package)- .and_return(copy_double)-- expect(copy_double)- .to receive(:call)- .with(expected_child_attributes)- .and_return(child_child_service_result)-- allow(work_package)- .to receive_message_chain(:descendants, :order_by_ancestors)- .and_return [child_work_package]- end-- it 'calls the copy service twice and merges its result' do- call = instance.call(project, nil, copy: true)-- expect(call.result)- .to eql child_service_result.result- expect(call.dependent_results)- .to match_array [child_child_service_result]- end- end- end-- context 'when moving' do- let(:expected_attributes) { { project: project } }-- before do- update_double = double('update service double')-- expect(WorkPackages::UpdateService)- .to receive(:new)- .with(user: user,- model: work_package)- .and_return(update_double)-- expect(update_double)- .to receive(:call)- .with(expected_attributes)- .and_return(child_service_result)- end-- it 'calls the update service and returns its result' do- expect(instance.call(project))- .to eql child_service_result- end-- context 'when providing a type and attributes' do- let(:expected_attributes) do- { project: project,- type: type,- subject: 'blubs' }- end-- it 'calls the update service and returns its result' do- expect(instance.call(project, type, attributes: { subject: 'blubs' }))- .to eql child_service_result- end- end- end-end