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
Move legacy project copy spec to service spec
Oliver Günther95d1549858ba
diff --git a/app/services/copy/dependency.rb b/app/services/copy/dependency.rbindex 121613df2f4..f4188de82fb 100644--- a/app/services/copy/dependency.rb+++ b/app/services/copy/dependency.rb@@ -48,7 +48,9 @@ module Copy@source = source@target = target@user = user- @result = ServiceResult.new(result: target, success: true)+ # Create a result with an empty error set+ # that we can merge! so that not the target.errors object is reused.+ @result = ServiceResult.new(result: target, success: true, errors: ActiveModel::Errors.new(target))enddef call(params:, state:)diff --git a/app/services/projects/copy/wiki_dependent_service.rb b/app/services/projects/copy/wiki_dependent_service.rbindex ca1c222cf65..bd3a9757e02 100644--- a/app/services/projects/copy/wiki_dependent_service.rb+++ b/app/services/projects/copy/wiki_dependent_service.rb@@ -41,12 +41,12 @@ module Projects::Copytarget.wiki = target.build_wiki(source.wiki.attributes.dup.except('id', 'project_id'))target.wiki.wiki_menu_items.delete_all- copy_wiki_pages- copy_wiki_menu_items+ copy_wiki_pages(params)+ copy_wiki_menu_items(params)end# Copies wiki pages from +project+, requires a wiki to be already set- def copy_wiki_pages+ def copy_wiki_pages(params)wiki_pages_map = {}source.wiki.pages.find_each do |page|@@ -81,7 +81,7 @@ module Projects::Copyend# Copies wiki_menu_items from +project+, requires a wiki to be already set- def copy_wiki_menu_items+ def copy_wiki_menu_items(params)wiki_menu_items_map = {}source.wiki.wiki_menu_items.each do |item|diff --git a/app/services/projects/copy/work_packages_dependent_service.rb b/app/services/projects/copy/work_packages_dependent_service.rbindex 8dfae420a47..efc882fd2dd 100644--- a/app/services/projects/copy/work_packages_dependent_service.rb+++ b/app/services/projects/copy/work_packages_dependent_service.rb@@ -122,7 +122,7 @@ module Projects::Copydef copy_work_package_attribute_overrides(source_work_package, parent_id, user_cf_ids)custom_value_attributes = source_work_package.custom_value_attributes.map do |id, value|- if user_cf_ids.include?(id) && !users.detect { |u| u.id.to_s == value }+ if user_cf_ids.include?(id) && !target.users.detect { |u| u.id.to_s == value }[id, nil]else[id, value]@@ -143,15 +143,15 @@ module Projects::Copyenddef work_package_version(source_work_package)- source_work_package.version && versions.detect { |v| v.name == source_work_package.version.name }+ source_work_package.version && target.versions.detect { |v| v.name == source_work_package.version.name }enddef work_package_assigned_to(source_work_package)- source_work_package.assigned_to && possible_assignees.detect { |u| u.id == source_work_package.assigned_to_id }+ source_work_package.assigned_to && target.possible_assignees.detect { |u| u.id == source_work_package.assigned_to_id }enddef work_package_responsible(source_work_package)- source_work_package.responsible && possible_responsibles.detect { |u| u.id == source_work_package.responsible_id }+ source_work_package.responsible && target.possible_responsibles.detect { |u| u.id == source_work_package.responsible_id }endendenddiff --git a/app/services/projects/copy_service.rb b/app/services/projects/copy_service.rbindex 2105a96992a..1888c2e3020 100644--- a/app/services/projects/copy_service.rb+++ b/app/services/projects/copy_service.rb@@ -39,6 +39,7 @@ module Projects::Projects::Copy::CategoriesDependentService,::Projects::Copy::WorkPackagesDependentService,::Projects::Copy::WikiDependentService,+ ::Projects::Copy::ForumsDependentService,::Projects::Copy::QueriesDependentService]end@@ -56,27 +57,37 @@ module Projectstarget.custom_field_values = source.custom_value_attributestarget.custom_values = source.custom_values.map(&:dup)- cleanup_target_project_attributes(target)- cleanup_target_project_params(params)+ # Additional input target params+ target_project_params = params[:target_project_params].with_indifferent_access++ cleanup_target_project_params(source, target, target_project_params)# Assign additional params from user- Projects::SetAttributesService+ call = Projects::SetAttributesService.new(user: user,model: target,contract_class: Projects::CopyContract,contract_options: { copied_from: source })- .call(params[:target_project_params])+ .call(target_project_params)++ cleanup_target_project_attributes(source, target, target_project_params)++ callend- def cleanup_target_project_params(params)- if (parent_id = params[:target_project_params]["parent_id"]) && (parent = Project.find_by(id: parent_id))- params[:target_project_params].delete("parent_id") unless user.allowed_to?(:add_subprojects, parent)+ def cleanup_target_project_params(_source, _target, target_project_params)+ if (parent_id = target_project_params[:parent_id]) && (parent = Project.find_by(id: parent_id))+ target_project_params.delete(:parent_id) unless user.allowed_to?(:add_subprojects, parent)endend- def cleanup_target_project_attributes(target_project)- if target_project.parent- target_project.parent = nil unless user.allowed_to?(:add_subprojects, target_project.parent)+ def cleanup_target_project_attributes(source, target, target_project_params)+ # Ensure we keep the public value of the source project+ # which might get overridden by the SetAttributesService+ target.public = source.public unless target_project_params.key?(:public)++ if target.parent+ target.parent = nil unless user.allowed_to?(:add_subprojects, target.parent)endenddiff --git a/spec/lib/copy_model/copy_model_spec.rb b/spec/lib/copy_model/copy_model_spec.rbdeleted file mode 100644index f42daff58e5..00000000000--- a/spec/lib/copy_model/copy_model_spec.rb+++ /dev/null@@ -1,178 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.-#++--require 'spec_helper'--describe 'Copying Models' do- let(:dummy_class) do- Class.new do- include CopyModel-- attr_accessor :call_order-- copy_precedence([:association2, :association1])-- attr_accessor :association1,- :association2,- :association3-- def attributes- { 'attribute1' => 'attribute1',- 'attribute2' => 'attribute2' }- end-- def attributes=(_)- end-- def self.reflect_on_all_associations- [OpenStruct.new(name: 'association1', valid?: true),- OpenStruct.new(name: 'association2', valid?: true),- OpenStruct.new(name: 'association3', valid?: true)]- end-- def copy_association1(other, selected_copies = [])- call_order << :association1- self.association1 = other.association1- end-- def copy_association2(other, selected_copies = [])- call_order << :association2- self.association2 = other.association2- end-- # used to test error handling- def copy_relation5(_other)- raise 'Could not be copied!'- end-- def call_order- @call_order ||= []- end-- def save- true- end-- def reload- true- end-- def errors- @errors ||= begin- Class.new(Array) do- def add(attribute, value)- push(attribute: attribute, value: value)- end- end.new- end- end- end- end-- let(:attributes) do- { 'attribute1' => 'attribute1',- 'attribute2' => 'attribute2' }- end- let(:dummy) {- dummy_class.new- }- let(:copied) {- copied = dummy_class.new- allow(dummy_class)- .to receive(:new)- .and_return(copied)-- copied- }-- before do- expect(dummy_class)- .to receive(:transaction)- .and_yield- end-- describe 'copying attributes' do- it 'should copy safe attributes' do- expect(copied).to receive(:attributes=).with(dummy.attributes)-- dummy_class.copy(dummy)- end-- it 'should not copy attributes flagged by not_to_copy' do- dummy_class.not_to_copy :attribute1-- attributes_left = attributes.except('attribute1')-- expect(copied).to receive(:attributes=).with(attributes_left)-- dummy_class.copy(dummy)- end- end-- describe 'copying associations' do- let(:copy) { dummy_class.copy(dummy) }- let(:association1_value) { double('association1_value', valid?: true) }- let(:association2_value) { double('association2_value', valid?: true) }- let(:association3_value) { double('association3_value', valid?: true) }-- before do- dummy.association1 = association1_value- dummy.association2 = association2_value- dummy.association3 = association3_value- end-- it 'should copy associations, for which there are methods in our model' do- copy-- expect(copy.association1).to eq(association1_value)- expect(copy.association2).to eq(association2_value)- end-- it 'should not copy associations, for which there are no methods in our model' do- copy-- expect(copy.association3).to be_nil- end-- it 'should copy stuff within order (ordered by #copy_precedence)' do- copy-- expect(copy.call_order).to eq(copy.copy_precedence)- end-- it 'should produce some errors when failing to copy associations' do- dummy_class.send(:define_method, :copy_association1) do |_|- raise "But I don't want to be copied"- end-- copy-- expect(copy.errors.count).to eq(1)- end- end-enddiff --git a/spec/models/projects/copy_spec.rb b/spec/models/projects/copy_spec.rbdeleted file mode 100644index bacccda5698..00000000000--- a/spec/models/projects/copy_spec.rb+++ /dev/null@@ -1,597 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.-#++--require 'spec_helper'--describe Projects::Copy, type: :model, with_mail: false do- describe '#copy' do- let(:project) { FactoryBot.create(:project_with_types) }- let(:copy) { Project.new }-- before do- copy.name = 'foo'- copy.identifier = 'foo'- copy.copy(project)- end-- subject { copy }-- it 'should be able to be copied' do- expect(copy).to be_valid- expect(copy).not_to be_new_record- end- end-- describe '#copy_attributes' do- let(:project) { FactoryBot.create(:project_with_types) }-- let(:copy) do- copy = Project.new- copy.name = 'foo'- copy.identifier = 'foo'- copy- end-- before do- copy.send :copy_attributes, project- copy.save- end-- describe '#types' do- subject { copy.types }-- it { is_expected.to eq(project.types) }- end-- describe '#work_package_custom_fields' do- let(:project) do- project = FactoryBot.create(:project_with_types)- work_package_custom_field = FactoryBot.create(:work_package_custom_field)- project.work_package_custom_fields << work_package_custom_field- project.save- project- end-- subject { copy.work_package_custom_fields }-- it { is_expected.to eq(project.work_package_custom_fields) }- end-- describe '#public' do- describe '#non_public' do- let(:project) do- project = FactoryBot.create(:project_with_types)- project.public = false- project.save- project- end-- subject { copy.public }-- it { expect(copy.public?).to eq(project.public?) }- end-- describe '#public' do- let(:project) do- project = FactoryBot.create(:project_with_types)- project.public = true- project.save- project- end-- subject { copy.public }-- it { expect(copy.public?).to eq(project.public?) }- end- end- end-- describe '#copy_associations' do- let(:project) { FactoryBot.create(:project_with_types) }- let(:copy) do- copy = Project.new- copy.name = 'foo'- copy.identifier = 'foo'- copy.copy_attributes(project)- copy.save- copy- end-- describe '#copy_work_packages' do- let(:work_package) { FactoryBot.create(:work_package, project: project) }- let(:work_package2) { FactoryBot.create(:work_package, project: project) }- let(:work_package3) { FactoryBot.create(:work_package, project: project) }- let(:user) { FactoryBot.create(:admin) }-- before do- login_as(user)- end-- describe '#attachments' do- let!(:attachment) { FactoryBot.create(:attachment, container: work_package) }-- before do- copy.send :copy_work_packages, project, only- end-- context 'when requested' do- let(:only) { [:work_package_attachments] }- it 'copies them' do- expect(copy.work_packages.count).to eq(1)-- wp = copy.work_packages.first- expect(wp.attachments.count).to eq(1)- end- end-- context 'when not requested' do- let(:only) { [] }- it 'ignores them' do- expect(copy.work_packages.count).to eq(1)-- wp = copy.work_packages.first- expect(wp.attachments.count).to eq(0)- end- end- end-- describe '#relation' do- before do- FactoryBot.create(:relation, from: work_package, to: work_package2)- [work_package, work_package2].each { |wp| project.work_packages << wp }-- copy.send :copy_work_packages, project- copy.save- end-- it do- copy.work_packages.each { |wp| expect(wp).to(be_valid) }- expect(copy.work_packages.count).to eq(project.work_packages.count)- end- end-- describe '#parent' do- before do- work_package.parent = work_package2- work_package.save!- work_package2.parent = work_package3- work_package2.save!-- copy.send :copy_work_packages, project- copy.save- end-- it do- grandparent_wp_copy = copy.work_packages.find_by(subject: work_package3.subject)- parent_wp_copy = copy.work_packages.find_by(subject: work_package2.subject)- child_wp_copy = copy.work_packages.find_by(subject: work_package.subject)-- [grandparent_wp_copy,- parent_wp_copy,- child_wp_copy].each do |wp|- expect(wp).to be_present- end-- expect(child_wp_copy.parent).to eq(parent_wp_copy)- expect(parent_wp_copy.parent).to eq(grandparent_wp_copy)- end- end-- describe '#category' do- before do- wp = work_package- wp.category = FactoryBot.create(:category, project: project)- wp.save-- project.work_packages << wp.reload-- copy.send :copy_categories, project- copy.send :copy_work_packages, project- copy.save- end-- it do- expect(cat = copy.work_packages[0].category).not_to eq(nil)- expect(cat.project).to eq(copy)- end- end-- describe '#watchers' do- let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }- let(:watcher) { FactoryBot.create(:user, member_in_project: project, member_through_role: role) }-- describe '#active_watcher' do- before do- wp = work_package- wp.add_watcher watcher- wp.save-- project.work_packages << wp-- copy.send :copy_members, project- copy.send :copy_work_packages, project- copy.save- end-- it 'does copy active watchers' do- expect(copy.work_packages[0].watchers.first.user).to eq(watcher)- end- end-- describe '#locked_watcher' do- before do- user = watcher- wp = work_package- wp.add_watcher user- wp.save-- user.lock!-- project.work_packages << wp-- copy.send :copy_members, project- copy.send :copy_work_packages, project- copy.save- end-- it 'does not copy locked watchers' do- expect(copy.work_packages[0].watchers).to eq([])- end- end-- describe 'versions' do- let(:version) { FactoryBot.create(:version, project: project) }- let(:version2) { FactoryBot.create(:version, project: project) }-- before do- work_package.update_column(:version_id, version.id)- work_package2.update_column(:version_id, version2.id)- work_package3-- copy.send :copy_versions, project- copy.send :copy_work_packages, project- copy.save- end-- it 'assigns the work packages to copies of the versions' do- expect(copy.work_packages.detect { |wp| wp.subject == work_package.subject }.version.name)- .to eql version.name- expect(copy.work_packages.detect { |wp| wp.subject == work_package2.subject }.version.name)- .to eql version2.name- expect(copy.work_packages.detect { |wp| wp.subject == work_package3.subject }.version)- .to be_nil- end- end- end-- describe 'assigned_to' do- let(:member?) { true }- let(:assignee) do- FactoryBot.create(:user).tap do |u|- if member?- copy.add_member!(u, role)- end- end- end- let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }-- before do- work_package.update_column(:assigned_to_id, assignee.id)-- copy.send :copy_work_packages, project- end-- context 'with the assignee being a member' do- it 'copies the assigned_to' do- expect(copy.work_packages[0].assigned_to)- .to eql assignee- end- end-- context 'with the assignee not being a member' do- let(:member?) { false }-- it 'nils the assigned_to' do- expect(copy.work_packages[0].assigned_to)- .to be_nil- end- end- end-- describe 'responsible' do- let(:member?) { true }- let(:responsible) do- FactoryBot.create(:user).tap do |u|- if member?- copy.add_member!(u, role)- end- end- end- let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }-- before do- work_package.update_column(:responsible_id, responsible.id)-- copy.send :copy_work_packages, project- end-- context 'with the responsible being a member' do- it 'copies the responsible' do- expect(copy.work_packages[0].responsible)- .to eql responsible- end- end-- context 'with the responsible not being a member' do- let(:member?) { false }-- it 'nils the responsible' do- expect(copy.work_packages[0].responsible)- .to be_nil- end- end- end-- describe 'user custom field' do- let(:member?) { true }- let(:value) do- FactoryBot.create(:user).tap do |u|- project.add_member!(u, role)- if member?- copy.add_member!(u, role)- end- end- end- let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }- let(:custom_field) do- FactoryBot.create(:user_wp_custom_field).tap do |cf|- project.work_package_custom_fields << cf- copy.work_package_custom_fields << cf- work_package.type.custom_fields << cf- end- end-- before do- custom_field- work_package.reload- work_package.send(:"custom_field_#{custom_field.id}=", value.id)- work_package.save(validate: false)-- copy.send :copy_work_packages, project- end-- context 'with the value being a member' do- it 'copies the custom_field' do- expect(copy.work_packages[0].send(:"custom_field_#{custom_field.id}"))- .to eql value- end- end-- context 'with the value not being a member' do- let(:member?) { false }-- it 'nils the custom_field' do- expect(copy.work_packages[0].send(:"custom_field_#{custom_field.id}"))- .to be_nil- end- end- end- end-- describe '#copy_queries' do- let(:query) { FactoryBot.create(:query, project: project) }-- before do- query-- copy.send(:copy_queries, project)- copy.save- end-- subject { copy.queries.count }-- it { is_expected.to eq(project.queries.count) }-- context 'with a filter' do- let(:query) do- query = FactoryBot.build(:query, project: project)- query.add_filter('subject', '~', ['bogus'])- query.save!- end-- subject { copy.queries }-- it 'produces a valid query in the new project' do- expect(subject.all?(&:valid?)).to eq(true)- expect(subject.count).to eq(1)- end- end-- context 'with a query menu item' do- let(:query) do- query = FactoryBot.build(:query, project: project)- query.add_filter('subject', '~', ['bogus'])- query.save!-- MenuItems::QueryMenuItem.create(- navigatable_id: query.id,- name: 'some-uuid',- title: 'My query title'- )-- query- end- subject { copy.queries.first }-- it 'copies the menu item' do- expect(subject).to be_valid- expect(subject.query_menu_item.title).to eq('My query title')- end- end- end-- describe '#copy_members' do- describe '#with_user' do- before do- role = FactoryBot.create(:role)- user = FactoryBot.create(:user, member_in_project: project, member_through_role: role)-- copy.send(:copy_members, project)- copy.save- end-- subject { copy.members.count }-- it { is_expected.to eq(project.members.count) }- end-- describe '#with_group' do- before do- project.add_member! FactoryBot.create(:group), FactoryBot.create(:role)-- copy.send(:copy_members, project)- copy.save- end-- subject { copy.principals.count }-- it { is_expected.to eq(project.principals.count) }- end- end-- describe '#copy_wiki' do- before do- project.wiki = FactoryBot.create(:wiki, project: project)- project.save-- copy.send(:copy_wiki, project)- copy.save- end-- subject { copy.wiki }-- it { is_expected.not_to eq(nil) }- it { is_expected.to be_valid }-- describe '#copy_wiki_pages' do- describe '#dont_copy_wiki_page_without_content' do- before do- project.wiki.pages << FactoryBot.create(:wiki_page)-- copy.send(:copy_wiki_pages, project)- copy.save- end-- subject { copy.wiki.pages.count }-- it { is_expected.to eq(0) }- end-- describe '#copy_wiki_page_with_content' do- before do- project.wiki.pages << FactoryBot.create(:wiki_page_with_content)-- copy.send(:copy_wiki_pages, project)- copy.save- end-- subject { copy.wiki.pages.count }-- it { is_expected.to eq(project.wiki.pages.count) }- end- end- describe '#copy_wiki_menu_items' do- before do- project.wiki.wiki_menu_items << FactoryBot.create(:wiki_menu_item_with_parent, wiki: project.wiki)- copy.send(:copy_wiki_menu_items, project)- copy.save- end-- subject { copy.wiki.wiki_menu_items.count }-- it { is_expected.to eq(project.wiki.wiki_menu_items.count) }- end- end-- describe '#copy_forums' do- let(:forum) { FactoryBot.create(:forum, project: project) }-- context 'forums are copied' do- before do- copy.send(:copy_forums, project)- copy.save- end-- subject { copy.forums.count }-- it { is_expected.to eq(project.forums.count) }- end-- context 'forum topics are copied' do- before do- topic = FactoryBot.create(:message, forum: forum)- message = FactoryBot.create(:message, forum: forum, parent_id: topic.id)-- copy.send(:copy_forums, project)- copy.save- end-- it 'should copy topics without replies' do- expect(copy.forums.first.topics.count).to eq(project.forums.first.topics.count)- expect(copy.forums.first.messages.count).not_to eq(project.forums.first.messages.count)- end- end- end-- describe '#copy_versions' do- before do- FactoryBot.create(:version, project: project)-- copy.send(:copy_versions, project)- copy.save- end-- subject { copy.versions.count }-- it { is_expected.to eq(project.versions.count) }- end-- describe '#copy_categories' do- before do- FactoryBot.create(:category, project: project)-- copy.send(:copy_categories, project)- copy.save- end-- subject { copy.categories.count }-- it { is_expected.to eq(project.categories.count) }- end- end-enddiff --git a/spec/services/projects/copy_service_integration_spec.rb b/spec/services/projects/copy_service_integration_spec.rbnew file mode 100644index 00000000000..2ddc7ea495d--- /dev/null+++ b/spec/services/projects/copy_service_integration_spec.rb@@ -0,0 +1,518 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++describe Projects::CopyService, 'integration', type: :model do+ shared_let(:source) { FactoryBot.create :project, enabled_module_names: %w[wiki work_package_tracking] }+ shared_let(:source_wp) { FactoryBot.create :work_package, project: source, subject: 'source wp' }+ shared_let(:source_query) { FactoryBot.create :query, project: source, name: 'My query' }+ shared_let(:source_category) { FactoryBot.create :category, project: source, name: 'Stock management' }+ shared_let(:source_version) { FactoryBot.create :version, project: source, name: 'Version A' }+ shared_let(:source_wiki_page) { FactoryBot.create(:wiki_page_with_content, wiki: source.wiki) }+ shared_let(:source_forum) { FactoryBot.create(:forum, project: source) }+ shared_let(:source_topic) { FactoryBot.create(:message, forum: source_forum) }++ let(:current_user) do+ FactoryBot.create(:user,+ member_in_project: source,+ member_through_role: role)+ end+ let(:role) { FactoryBot.create :role, permissions: %i[copy_projects] }+ let(:instance) do+ described_class.new(source: source, user: current_user)+ end+ let(:only_args) { nil }+ let(:target_project_params) do+ { name: 'Some name', identifier: 'some-identifier' }+ end+ let(:params) do+ { target_project_params: target_project_params, only: only_args }+ end++ describe 'call' do+ subject { instance.call(params) }+ let(:project_copy) { subject.result }++ context 'restricting only to members and categories' do+ let(:only_args) { %w[members categories] }++ it 'should limit copying' do+ expect(subject).to be_success++ expect(project_copy.members.count).to eq 1+ expect(project_copy.categories.count).to eq 1+ expect(project_copy.work_packages.count).to eq 0+ expect(project_copy.forums.count).to eq 0+ expect(project_copy.wiki.pages.count).to eq 0+ expect(project_copy.versions.count).to eq 0+ expect(project_copy.queries.count).to eq 0+ end+ end++ it 'will copy all dependencies and set attributes' do+ expect(subject).to be_success++ expect(project_copy.members.count).to eq 1+ expect(project_copy.categories.count).to eq 1+ expect(project_copy.work_packages.count).to eq 1+ expect(project_copy.forums.count).to eq 1+ expect(project_copy.forums.first.messages.count).to eq 1+ expect(project_copy.wiki).to be_present+ expect(project_copy.wiki.pages.count).to eq 1+ expect(project_copy.queries.count).to eq 1+ expect(project_copy.versions.count).to eq 1+ expect(project_copy.wiki.pages.first.content.text).to eq source_wiki_page.content.text+ expect(project_copy.wiki.start_page).to eq 'Wiki'++ # Cleared attributes+ expect(project_copy).to be_persisted+ expect(project_copy.name).to eq 'Some name'+ expect(project_copy.name).to eq 'Some name'+ expect(project_copy.identifier).to eq 'some-identifier'++ # Duplicated attributes+ expect(project_copy.description).to eq source.description+ expect(source.enabled_module_names.sort - %w[repository]).to eq project_copy.enabled_module_names.sort+ expect(project_copy.types).to eq source.types++ # Default attributes+ expect(project_copy).to be_active+ end++ it 'will copy the work package with category' do+ source_wp.update!(category: source_category)++ expect(subject).to be_success++ wp = project_copy.work_packages.first+ expect(wp.category.name).to eq 'Stock management'+ # Category got copied+ expect(wp.category.id).not_to eq source_category.id+ end++ describe '#public' do+ before do+ source.update!(public: public)+ end++ context 'when not public' do+ let(:public) { false }++ it 'copies correctly' do+ expect(subject).to be_success+ expect(project_copy.public).to eq public+ end+ end++ context 'when public' do+ let(:public) { true }++ it 'copies correctly' do+ expect(subject).to be_success+ expect(project_copy.public).to eq public+ end+ end+ end++ context 'with an assigned version' do+ let!(:assigned_version) { FactoryBot.create(:version, name: 'Assigned Issues', project: source, status: 'open') }++ before do+ source_wp.update!(version: assigned_version)+ assigned_version.update!(status: 'closed')+ end++ it 'will update the version' do+ expect(subject).to be_success++ wp = project_copy.work_packages.first+ expect(wp.version.name).to eq 'Assigned Issues'+ expect(wp.version).to be_closed+ expect(wp.version.id).not_to eq assigned_version.id+ end+ end++ context 'with group memberships' do+ let(:only_args) { %w[members] }++ let!(:user) { FactoryBot.create :user }+ let!(:another_role) { FactoryBot.create(:role) }+ let!(:group) { FactoryBot.create :group, members: [user]+ }++ it 'will copy them as well' do+ source.add_member! group, another_role+ source.reload+ expect(source.users).to include current_user+ expect(source.users).to include user+ expect(project_copy.groups).to include group+ expect(source.member_principals.count).to eq 3++ expect(subject).to be_success++ expect(project_copy.member_principals.count).to eq 3+ expect(project_copy.groups).to include group+ expect(project_copy.users).to include current_user+ expect(project_copy.users).to include user++ group_member = Member.find_by(user_id: group.id, project_id: project_copy.id)+ expect(group_member).to be_present+ expect(group_member.roles.map(&:id)).to eq [another_role.id]++ member = Member.find_by(user_id: user.id, project_id: project_copy.id)+ expect(member).to be_present+ expect(member.roles.map(&:id)).to eq [another_role.id]+ expect(member.member_roles.first.inherited_from).to eq group_member.member_roles.first.id+ end+ end++ context 'with work package relations', with_settings: { cross_project_work_package_relations: '1' } do+ let!(:source_wp2) { FactoryBot.create(:work_package, project: source, subject: 'source wp2') }+ let!(:source_relation) { FactoryBot.create(:relation, from: source_wp, to: source_wp2, relation_type: 'relates') }++ let!(:other_project) { FactoryBot.create(:project) }+ let!(:other_wp) { FactoryBot.create(:work_package, project: other_project, subject: 'other wp') }+ let!(:cross_relation) { FactoryBot.create(:relation, from: source_wp, to: other_wp, relation_type: 'duplicates') }++ let(:only_args) { %w[work_packages] }++ it 'should copy issue relations' do+ expect(subject).to be_success++ expect(source.work_packages.count).to eq(project_copy.work_packages.count)+ copied_wp = project_copy.work_packages.find_by(subject: 'source wp')+ copied_wp_2 = project_copy.work_packages.find_by(subject: 'source wp2')++ # First issue with a relation on project+ # copied relation + reflexive relation+ expect(copied_wp.relations.direct.count).to eq 2+ relates_relation = copied_wp.relations.direct.find { |r| r.relation_type == 'relates' }+ expect(relates_relation.from_id).to eq copied_wp.id+ expect(relates_relation.to_id).to eq copied_wp_2.id++ # Second issue with a cross project relation+ # copied relation + reflexive relation+ duplicates_relation = copied_wp.relations.direct.find { |r| r.relation_type == 'duplicates' }+ expect(duplicates_relation.from_id).to eq copied_wp.id+ expect(duplicates_relation.to_id).to eq other_wp.id+ end+ end++ describe '#copy_wiki' do+ it 'will not copy wiki pages without content' do+ source.wiki.pages << FactoryBot.create(:wiki_page)+ expect(source.wiki.pages.count).to eq 2++ expect(subject).to be_success+ expect(project_copy.wiki.pages.count).to eq 1+ end++ it 'will copy menu items' do+ source.wiki.wiki_menu_items << FactoryBot.create(:wiki_menu_item_with_parent, wiki: source.wiki)++ expect(subject).to be_success+ expect(project_copy.wiki.wiki_menu_items.count).to eq 3+ end+ end++ describe 'valid queries' do+ context 'with a filter' do+ let!(:query) do+ query = FactoryBot.build(:query, project: source)+ query.add_filter('subject', '~', ['bogus'])+ query.save!+ end++ it 'produces a valid query in the new project' do+ expect(subject).to be_success+ expect(project_copy.queries.all?(&:valid?)).to eq(true)+ expect(project_copy.queries.count).to eq 2+ end+ end+ end++ describe 'query menu items' do+ let!(:query) do+ query = FactoryBot.build(:query, project: source, name: 'Query with item')+ query.add_filter('subject', '~', ['bogus'])+ query.save!++ MenuItems::QueryMenuItem.create(+ navigatable_id: query.id,+ name: 'some-uuid',+ title: 'My query title'+ )++ query+ end++ it 'copies the menu item' do+ expect(subject).to be_success+ query = project_copy.queries.find_by(name: 'Query with item')+ expect(query).to be_present+ expect(query.query_menu_item.title).to eq('My query title')+ end+ end++ describe 'work packages' do+ let(:work_package) { FactoryBot.create(:work_package, project: source) }+ let(:work_package2) { FactoryBot.create(:work_package, project: source) }+ let(:work_package3) { FactoryBot.create(:work_package, project: source) }++ let(:only_args) { %w[work_packages] }++ describe '#attachments' do+ let!(:attachment) { FactoryBot.create(:attachment, container: work_package) }++ context 'when requested' do+ let(:only_args) { %i[work_packages work_package_attachments] }+ it 'copies them' do+ expect(subject).to be_success+ expect(project_copy.work_packages.count).to eq(2)++ wp = project_copy.work_packages.find_by(subject: work_package.subject)+ expect(wp.attachments.count).to eq(1)+ end+ end++ context 'when not requested' do+ it 'ignores them' do+ expect(subject).to be_success+ expect(project_copy.work_packages.count).to eq(2)++ wp = project_copy.work_packages.find_by(subject: work_package.subject)+ expect(wp.attachments.count).to eq(0)+ end+ end+ end++ describe '#parent' do+ before do+ work_package.parent = work_package2+ work_package.save!+ work_package2.parent = work_package3+ work_package2.save!+ end++ it do+ expect(subject).to be_success++ grandparent_wp_copy = project_copy.work_packages.find_by(subject: work_package3.subject)+ parent_wp_copy = project_copy.work_packages.find_by(subject: work_package2.subject)+ child_wp_copy = project_copy.work_packages.find_by(subject: work_package.subject)++ [grandparent_wp_copy,+ parent_wp_copy,+ child_wp_copy].each do |wp|+ expect(wp).to be_present+ end++ expect(child_wp_copy.parent).to eq(parent_wp_copy)+ expect(parent_wp_copy.parent).to eq(grandparent_wp_copy)+ end+ end++ describe '#category' do+ let(:only_args) { %w[work_packages categories] }++ before do+ wp = work_package+ wp.category = FactoryBot.create(:category, project: source)+ wp.save++ source.work_packages << wp.reload+ end++ it do+ expect(subject).to be_success+ wp = project_copy.work_packages.find_by(subject: work_package.subject)+ expect(cat = wp.category).not_to eq(nil)+ expect(cat.project).to eq(project_copy)+ end+ end++ describe '#watchers' do+ let(:watcher_role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }+ let(:watcher) { FactoryBot.create(:user, member_in_project: source, member_through_role: watcher_role) }++ let(:only_args) { %w[work_packages members] }++ describe '#active_watcher' do+ before do+ wp = work_package+ wp.add_watcher watcher+ wp.save++ source.work_packages << wp+ end++ it 'does copy active watchers' do+ expect(subject).to be_success+ expect(project_copy.work_packages[0].watchers.first.user).to eq(watcher)+ end+ end++ describe '#locked_watcher' do+ before do+ user = watcher+ wp = work_package+ wp.add_watcher user+ wp.save++ user.lock!++ source.work_packages << wp+ end++ it 'does not copy locked watchers' do+ expect(subject).to be_success+ expect(project_copy.work_packages[0].watchers).to eq([])+ end+ end++ describe 'versions' do+ let(:version) { FactoryBot.create(:version, project: source) }+ let(:version2) { FactoryBot.create(:version, project: source) }++ let(:only_args) { %w[versions work_packages] }++ before do+ work_package.update_column(:version_id, version.id)+ work_package2.update_column(:version_id, version2.id)+ work_package3+ end++ it 'assigns the work packages to copies of the versions' do+ expect(subject).to be_success+ expect(project_copy.work_packages.detect { |wp| wp.subject == work_package.subject }.version.name)+ .to eql version.name+ expect(project_copy.work_packages.detect { |wp| wp.subject == work_package2.subject }.version.name)+ .to eql version2.name+ expect(project_copy.work_packages.detect { |wp| wp.subject == work_package3.subject }.version)+ .to be_nil+ end+ end+ end++ describe 'assigned_to' do+ before do+ work_package.update_column(:assigned_to_id, current_user.id)+ end++ context 'with the members being copied' do+ let(:only_args) { %w[members work_packages] }++ it 'copies the assigned_to' do+ expect(subject).to be_success+ expect(project_copy.users).to include current_user+ expect(project_copy.work_packages[0].assigned_to)+ .to eql current_user+ end+ end++ context 'with the assignee not being a member' do+ let(:only_args) { %w[work_packages] }++ it 'nils the assigned_to' do+ expect(subject).to be_success+ expect(project_copy.work_packages[0].assigned_to)+ .to be_nil+ end+ end+ end++ describe 'responsible' do+ before do+ work_package.update_column(:responsible_id, current_user.id)+ end++ context 'with the members being copied' do+ let(:only_args) { %w[members work_packages] }++ it 'copies the responsible' do+ expect(subject).to be_success+ expect(project_copy.users).to include current_user+ expect(project_copy.work_packages[0].responsible)+ .to eql current_user+ end+ end++ context 'with the responsible not being a member' do+ let(:only_args) { %w[work_packages] }++ it 'nils the assigned_to' do+ expect(subject).to be_success+ expect(project_copy.work_packages[0].responsible)+ .to be_nil+ end+ end+ end++ describe 'user custom field' do+ let(:custom_field) do+ FactoryBot.create(:user_wp_custom_field).tap do |cf|+ source.work_package_custom_fields << cf+ work_package.type.custom_fields << cf+ end+ end++ before do+ custom_field+ work_package.reload+ work_package.send(:"custom_field_#{custom_field.id}=", current_user.id)+ work_package.save!(validate: false)+ end++ context 'with the value being a member' do+ let(:only_args) { %w[members work_packages] }++ it 'copies the custom_field' do+ expect(subject).to be_success+ wp = project_copy.work_packages.find_by(subject: work_package.subject)+ expect(wp.send(:"custom_field_#{custom_field.id}"))+ .to eql current_user+ end+ end++ context 'with the value not being a member' do+ let(:only_args) { %w[work_packages] }++ it 'nils the custom_field' do+ expect(subject).to be_success+ wp = project_copy.work_packages.find_by(subject: work_package.subject)+ expect(wp.send(:"custom_field_#{custom_field.id}"))+ .to be_nil+ end+ end+ end+ end+ end+enddiff --git a/spec/services/work_packages/update_service_spec.rb b/spec/services/work_packages/update_service_spec.rbindex 6e78e9d87d4..63841d134e4 100644--- a/spec/services/work_packages/update_service_spec.rb+++ b/spec/services/work_packages/update_service_spec.rb@@ -120,6 +120,10 @@ describe WorkPackages::UpdateService, type: :model dolet(:errors) { double('set errors', empty?: false) }let(:set_service_results) { ServiceResult.new success: false, errors: errors, result: work_package }+ before do+ allow(errors).to receive(:merge!)+ end+it 'is unsuccessful' doexpect(subject.success?).to be_falseyend@@ -142,6 +146,7 @@ describe WorkPackages::UpdateService, type: :model dolet(:saving_errors) { double('saving_errors', empty?: false) }before do+ allow(saving_errors).to receive(:merge!)allow(work_package).to receive(:errors).and_return(saving_errors)diff --git a/spec_legacy/unit/project_spec.rb b/spec_legacy/unit/project_spec.rbindex e4b1a7b982f..87940d25371 100644--- a/spec_legacy/unit/project_spec.rb+++ b/spec_legacy/unit/project_spec.rb@@ -306,25 +306,6 @@ describe Project, type: :model doassert_equal project.enabled_module_ids.sort, modules.map(&:id).sortend- it 'should copy from existing project' do- source_project = Project.find(1)- copied_project = Project.copy(1)-- assert copied_project- # Cleared attributes- assert copied_project.id.blank?- assert copied_project.name.blank?- assert copied_project.identifier.blank?-- # Duplicated attributes- assert_equal source_project.description, copied_project.description- assert_equal (source_project.enabled_module_names.sort - %w[repository]), copied_project.enabled_module_names.sort- assert_equal source_project.types, copied_project.types-- # Default attributes- assert copied_project.active- end-it 'should close completed versions' doVersion.update_all("status = 'open'")project = Project.find(1)@@ -340,253 +321,4 @@ describe Project, type: :model doproject = Project.find(1)assert project.allows_to?(:export_work_packages)end-- context 'Project#copy' do- before do- ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests- Project.where(identifier: 'copy-test').destroy_all- @source_project = Project.find(2)- @project = Project.new(name: 'Copy Test', identifier: 'copy-test')- @project.types = @source_project.types- @project.enabled_module_names = @source_project.enabled_modules.map(&:name)- end-- it 'should copy work units' do- work_package = FactoryBot.create(:work_package,- status: Status.find_by_name('Closed'),- subject: 'copy issue status',- type_id: 1,- assigned_to_id: 2,- project_id: @source_project.id)-- @source_project.work_packages << work_package- assert @project.valid?- assert @project.work_packages.empty?- assert @project.copy(@source_project)-- assert_equal @source_project.work_packages.size, @project.work_packages.size- @project.work_packages.each do |issue|- assert issue.valid?- assert !issue.assigned_to.blank?- assert_equal @project, issue.project- end-- copied_issue = @project.work_packages.find_by(subject: 'copy issue status')- assert copied_issue- assert copied_issue.status- assert_equal 'Closed', copied_issue.status.name- end-- it 'should change the new issues to use the copied version' do- User.current = User.find(1)- assigned_version = FactoryBot.create(:version, name: 'Assigned Issues', status: 'open')- @source_project.versions << assigned_version- assert_equal 3, @source_project.versions.size- FactoryBot.create(:work_package, project: @source_project,- version_id: assigned_version.id,- subject: 'change the new issues to use the copied version',- type_id: 1,- project_id: @source_project.id)-- assert @project.copy(@source_project)- @project.reload- copied_issue = @project.work_packages.find_by(subject: 'change the new issues to use the copied version')-- assert copied_issue- assert copied_issue.version- assert_equal 'Assigned Issues', copied_issue.version.name # Same name- refute_equal assigned_version.id, copied_issue.version.id # Different record- end-- it 'should change the new issues to use the copied closed version' do- User.current = User.find(1)- assigned_version = FactoryBot.create(:version, name: 'Assigned Issues', status: 'open')- @source_project.versions << assigned_version- assert_equal 3, @source_project.versions.size- FactoryBot.create(:work_package, project: @source_project,- version_id: assigned_version.id,- subject: 'change the new issues to use the copied version',- type_id: 1,- project_id: @source_project.id)- assigned_version.update_attribute(:status, 'closed')-- assert @project.copy(@source_project)- @project.reload- copied_issue = @project.work_packages.find_by(subject: 'change the new issues to use the copied version')-- assert copied_issue- assert copied_issue.version- assert_equal 'Assigned Issues', copied_issue.version.name # Same name- refute_equal assigned_version.id, copied_issue.version.id # Different record- end-- it 'should copy issue relations' do- Setting.cross_project_work_package_relations = '1'-- second_issue = FactoryBot.create(:work_package, status_id: 5,- subject: 'copy issue relation',- type_id: 1,- assigned_to_id: 2,- project_id: @source_project.id)- source_relation = FactoryBot.create(:relation, from: WorkPackage.find(4),- to: second_issue,- relation_type: 'relates')- source_relation_cross_project = FactoryBot.create(:relation, from: WorkPackage.find(1),- to: second_issue,- relation_type: 'duplicates')-- assert @project.copy(@source_project)- assert_equal @source_project.work_packages.count, @project.work_packages.count- copied_issue = @project.work_packages.find_by(subject: 'Issue on project 2') # Was #4- copied_second_issue = @project.work_packages.find_by(subject: 'copy issue relation')-- # First issue with a relation on project- # copied relation + reflexive relation- assert_equal 2, copied_issue.relations.size, 'Relation not copied'- copied_relation = copied_issue.relations.direct.first- assert_equal 'relates', copied_relation.relation_type- assert_equal copied_second_issue.id, copied_relation.to_id- refute_equal source_relation.id, copied_relation.id-- # Second issue with a cross project relation- # copied relation + reflexive relation- assert_equal 3, copied_second_issue.relations.size, 'Relation not copied'- copied_relation = copied_second_issue.relations.direct.find { |r| r.relation_type == 'duplicates' }- assert_equal 'duplicates', copied_relation.relation_type- assert_equal 1, copied_relation.from_id, 'Cross project relation not kept'- refute_equal source_relation_cross_project.id, copied_relation.id- end-- it 'should copy memberships' do- assert @project.valid?- assert @project.members.empty?- assert @project.copy(@source_project)-- assert_equal @source_project.memberships.size, @project.memberships.size- @project.memberships.each do |membership|- assert membership- assert_equal @project, membership.project- end- end-- it 'should copy memberships with groups and additional roles' do- user = User.find(7)- group = FactoryBot.create :group, lastname: 'Copy group', members: user-- # group role- (Member.new.tap do |m|- m.attributes = { project_id: @source_project.id,- principal: group,- role_ids: [2] }- end).save!-- member = Member.find_by(user_id: user.id, project_id: @source_project.id)- # additional role- member.role_ids = [1]-- assert @project.copy(@source_project)- member = Member.find_by(user_id: user.id, project_id: @project.id)- refute_nil member- assert_equal [1, 2], member.roles.all.map(&:id).sort- end-- it 'should copy project specific queries' do- assert @project.valid?- assert @project.queries.empty?- assert @project.copy(@source_project)-- assert_equal @source_project.queries.size, @project.queries.size- @project.queries.each do |query|- assert query- assert_equal @project, query.project- end- end-- it 'should copy versions' do- @source_project.versions << FactoryBot.create(:version)- @source_project.versions << FactoryBot.create(:version)-- assert @project.versions.empty?- assert @project.copy(@source_project)-- assert_equal @source_project.versions.size, @project.versions.size- @project.versions.each do |version|- assert version- assert_equal @project, version.project- end- end-- it 'should copy wiki' do- assert_difference 'Wiki.count' do- assert @project.copy(@source_project)- end-- assert @project.wiki- refute_equal @source_project.wiki, @project.wiki- assert_equal 'Start page', @project.wiki.start_page- end-- it 'should copy wiki pages and content with hierarchy' do- assert_difference 'WikiPage.count', @source_project.wiki.pages.size do- assert @project.copy(@source_project)- end-- assert @project.wiki- assert_equal @source_project.wiki.pages.size, @project.wiki.pages.size-- @project.wiki.pages.each do |wiki_page|- assert wiki_page.content- assert !@source_project.wiki.pages.include?(wiki_page)- end-- parent = @project.wiki.find_page('Parent page')- child1 = @project.wiki.find_page('Child page 1')- child2 = @project.wiki.find_page('Child page 2')- assert_equal parent, child1.parent- assert_equal parent, child2.parent- end-- it 'should copy issue categories' do- assert @project.copy(@source_project)-- assert_equal 2, @project.categories.size- @project.categories.each do |category|- assert !@source_project.categories.include?(category)- end- end-- it 'should copy forums' do- assert @project.copy(@source_project)-- assert_equal 1, @project.forums.size- @project.forums.each do |forum|- assert !@source_project.forums.include?(forum)- end- end-- it 'should change the new issues to use the copied issue categories' do- issue = WorkPackage.find(4)- issue.update_attribute(:category_id, 3)-- assert @project.copy(@source_project)-- @project.work_packages.each do |issue|- assert issue.category- assert_equal 'Stock management', issue.category.name # Same name- refute_equal Category.find(3), issue.category # Different record- end- end-- it 'should limit copy with :only option' do- assert @project.members.empty?- assert @project.categories.empty?- assert @source_project.work_packages.any?-- assert @project.copy(@source_project, only: ['members', 'categories'])-- assert @project.members.any?- assert @project.categories.any?- assert @project.work_packages.empty?- end- endend