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

[#49248] Copy FileLinks respecting the location of the files

Pavel Balashoud8b6d046512a
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/folder_files_file_ids_deep_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/folder_files_file_ids_deep_query.rb
new file mode 100644
index 00000000000..1b71a7b28b2
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/folder_files_file_ids_deep_query.rb
@@ -0,0 +1,43 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 Storages::Peripherals::StorageInteraction::Nextcloud
+ class FolderFilesFileIdsDeepQuery
+ def initialize(storage)
+ @query = ::Storages::Peripherals::StorageInteraction::Nextcloud::Internal::PropfindQuery.new(storage)
+ end
+
+ def call(path:)
+ @query.call(
+ depth: 'infinity',
+ path:,
+ props: %w[oc:fileid]
+ )
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_requests.rb
index b74095526de..9bdaed7341c 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -43,6 +43,7 @@ module Storages::Peripherals
files_info_query
files_query
file_ids_query
+ folder_files_file_ids_deep_query
upload_link_query
group_users_query
].freeze
diff --git a/modules/storages/app/contracts/storages/file_links/create_contract.rb b/modules/storages/app/contracts/storages/file_links/create_contract.rb
index ae8529dd6f7..d59936d4467 100644
--- a/modules/storages/app/contracts/storages/file_links/create_contract.rb
+++ b/modules/storages/app/contracts/storages/file_links/create_contract.rb
@@ -58,12 +58,11 @@ class Storages::FileLinks::CreateContract < ModelContract
end
end
- # Check that the current has the permission on the project.
- # model variable is available because the concern is executed inside a contract.
def validate_user_allowed_to_manage
- return if model.container.nil? || user.allowed_to?(:manage_file_links, model.project)
-
- errors.add(:base, :error_unauthorized)
+ container = model.container
+ if container.present? && !user.allowed_to?(:manage_file_links, container.project)
+ errors.add(:base, :error_unauthorized)
+ end
end
def validate_storage_presence
diff --git a/modules/storages/app/models/storages/project_storage.rb b/modules/storages/app/models/storages/project_storage.rb
index 20cd9c9b41f..1502b7e8851 100644
--- a/modules/storages/app/models/storages/project_storage.rb
+++ b/modules/storages/app/models/storages/project_storage.rb
@@ -59,4 +59,20 @@ class Storages::ProjectStorage < ApplicationRecord
def project_folder_path
"#{storage.group_folder}/#{project.name.gsub('/', '|')} (#{project.id})/"
end
+
+ def project_folder_path_escaped
+ escape_path(project_folder_path)
+ end
+
+ def file_inside_project_folder?(escaped_file_path)
+ escaped_file_path.match?(%r|^/#{project_folder_path_escaped}|)
+ end
+
+ private
+
+ def escape_path(path)
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util.escape_path(
+ path
+ )
+ end
end
diff --git a/modules/storages/app/services/projects/copy/file_links_dependent_service.rb b/modules/storages/app/services/projects/copy/file_links_dependent_service.rb
index 59ece00279d..2b051d6c8c7 100644
--- a/modules/storages/app/services/projects/copy/file_links_dependent_service.rb
+++ b/modules/storages/app/services/projects/copy/file_links_dependent_service.rb
@@ -39,31 +39,88 @@ module Projects::Copy
protected
def copy_dependency(*)
- # If no work packages were copied, we cannot copy their attachments
+ # If no work packages were copied, we cannot copy their file_links
return unless state.work_package_id_lookup
- state.work_package_id_lookup.each do |old_wp_id, new_wp_id|
- create_work_package_file_links(old_wp_id, new_wp_id)
- end
- end
+ source_wp_ids = state.work_package_id_lookup.keys
+ Storages::FileLink
+ .where(container_id: source_wp_ids, container_type: "WorkPackage")
+ .group_by(&:storage_id)
+ .each do |(storage_id, source_file_links)|
+ if source_file_links.any?
+ tmp = state
+ .copied_project_storages
+ .find { |item| item["source"].storage_id == storage_id }
+ source_project_storage = tmp['source']
+ target_project_storage = tmp['target']
+ storage_requests = Storages::Peripherals::StorageRequests.new(storage: source_project_storage.storage)
+
+ if source_project_storage.project_folder_mode == 'automatic'
+ files_info_query_result = files_info_query(storage_requests:,
+ file_ids: source_file_links.map(&:origin_id))
+ folder_files_file_ids_deep_query_result = folder_files_file_ids_deep_query(
+ storage_requests:,
+ path: target_project_storage.project_folder_path
+ )
+ source_file_links.each do |old_file_link|
+ attributes = {
+ storage_id: old_file_link.storage_id,
+ creator_id: User.current.id,
+ container_id: state.work_package_id_lookup[old_file_link.container_id.to_s],
+ container_type: 'WorkPackage',
+ origin_name: old_file_link.origin_name,
+ origin_mime_type: old_file_link.origin_mime_type
+ }
- def create_work_package_file_links(old_wp_id, new_wp_id)
- Storages::FileLink.where(container_id: old_wp_id).each do |file_link|
- create_file_link(file_link, new_wp_id)
+ original_file_location = files_info_query_result
+ .find { |i| i.id.to_i == old_file_link.origin_id.to_i }
+ .location
+
+ attributes['origin_id'] =
+ if source_project_storage.file_inside_project_folder?(original_file_location)
+ new_file_location = original_file_location.gsub(
+ source_project_storage.project_folder_path_escaped,
+ target_project_storage.project_folder_path_escaped
+ )
+ new_file_location = CGI.unescape(new_file_location[1..])
+ folder_files_file_ids_deep_query_result[new_file_location]['fileid']
+ else
+ old_file_link.origin_id
+ end
+ Storages::FileLinks::CreateService.new(user: User.current).call(attributes)
+ end
+ else
+ source_file_links.each do |old_file_link|
+ attributes = {
+ storage_id: old_file_link.storage_id,
+ creator_id: User.current.id,
+ container_id: state.work_package_id_lookup[old_file_link.container_id.to_s],
+ container_type: 'WorkPackage',
+ origin_name: old_file_link.origin_name,
+ origin_mime_type: old_file_link.origin_mime_type,
+ origin_id: old_file_link.origin_id
+ }
+ Storages::FileLinks::CreateService.new(user: User.current).call(attributes)
+ end
+ end
+ end
end
end
- def create_file_link(file_link, new_wp_id)
- attributes = file_link
- .attributes.dup.except('id', 'container_id', 'created_at', 'updated_at')
- .merge('container_id' => new_wp_id)
-
- service_result = Storages::FileLinks::CreateService
- .new(user: User.current)
- .call(attributes)
+ def files_info_query(storage_requests:, file_ids:)
+ storage_requests
+ .files_info_query
+ .call(user: User.current, file_ids:)
+ .on_failure { |result| raise "files_info_query failed: #{result}" }
+ .result
+ end
- copied_file_link = service_result.result
- copied_file_link.save
+ def folder_files_file_ids_deep_query(storage_requests:, path:)
+ storage_requests
+ .folder_files_file_ids_deep_query
+ .call(path:)
+ .on_failure { |result| raise "folder_files_file_ids_deep_query failed: #{result}" }
+ .result
end
end
end
diff --git a/modules/storages/app/services/projects/copy/storage_project_folders_dependent_service.rb b/modules/storages/app/services/projects/copy/storage_project_folders_dependent_service.rb
index 72cb355d348..a75fb12df3f 100644
--- a/modules/storages/app/services/projects/copy/storage_project_folders_dependent_service.rb
+++ b/modules/storages/app/services/projects/copy/storage_project_folders_dependent_service.rb
@@ -45,10 +45,16 @@ module Projects::Copy
return unless state.copied_project_storages
state.copied_project_storages.each do |copied_project_storage|
- if copied_project_storage[:source].project_folder_automatic?
- copy_project_folder(copied_project_storage[:source], copied_project_storage[:target])
-
- update_project_folder_id(copied_project_storage[:target])
+ source = copied_project_storage[:source]
+ target = copied_project_storage[:target]
+ if source.project_folder_automatic?
+ copy_project_folder(source, target)
+ update_project_folder_id(target)
+ elsif source.project_folder_manual?
+ target.update!(
+ project_folder_id: source.project_folder_id,
+ project_folder_mode: 'manual'
+ )
end
end
end
@@ -74,7 +80,12 @@ module Projects::Copy
.file_ids_query
.call(path: destination_folder_name)
.match(
- on_success: ->(file_ids) { project_storage.update!(project_folder_id: file_ids.first) },
+ on_success: ->(file_ids) do
+ project_storage.update!(
+ project_folder_id: file_ids.values.first['fileid'],
+ project_folder_mode: 'automatic'
+ )
+ end,
on_failure: ->(error) { add_error!(destination_folder_name, error.to_active_model_errors) }
)
end
diff --git a/modules/storages/app/services/projects/copy/storages_dependent_service.rb b/modules/storages/app/services/projects/copy/storages_dependent_service.rb
index d1b33513a29..0f89e6d56a1 100644
--- a/modules/storages/app/services/projects/copy/storages_dependent_service.rb
+++ b/modules/storages/app/services/projects/copy/storages_dependent_service.rb
@@ -41,29 +41,17 @@ module Projects::Copy
protected
def copy_dependency(*)
- copied_project_storages = []
-
- source.projects_storages.find_each do |project_storage|
- copied_project_storages << { source: project_storage, target: create_project_storage(project_storage) }
+ state.copied_project_storages = source.projects_storages.each_with_object([]) do |source_project_storage, array|
+ project_storage_copy =
+ ::Storages::ProjectStorages::CreateService
+ .new(user: User.current)
+ .call(storage_id: source_project_storage.storage_id,
+ project_id: target.id,
+ project_folder_mode: 'inactive')
+ .on_failure { |result| raise "ProjectStorage creation failed: #{result}" }.result
+
+ array << { source: source_project_storage, target: project_storage_copy }
end
-
- state.copied_project_storages = copied_project_storages
- end
-
- private
-
- def create_project_storage(project_storage)
- attributes = project_storage
- .attributes.dup.except('id', 'project_id', 'created_at', 'updated_at')
- .merge('project_id' => target.id)
-
- service_result = ::Storages::ProjectStorages::CreateService
- .new(user: User.current)
- .call(attributes)
-
- copied_storage = service_result.result
- copied_storage.save
- copied_storage
end
end
end
diff --git a/spec/services/projects/copy_service_integration_spec.rb b/spec/services/projects/copy_service_integration_spec.rb
index 0c0715dadb3..d9502171ad8 100644
--- a/spec/services/projects/copy_service_integration_spec.rb
+++ b/spec/services/projects/copy_service_integration_spec.rb
@@ -28,9 +28,19 @@
require 'spec_helper'
-RSpec.describe Projects::CopyService, 'integration', type: :model, with_ee: %i[readonly_work_packages] do
+RSpec.describe(
+ Projects::CopyService,
+ 'integration',
+ type: :model,
+ webmock: true,
+ with_ee: %i[readonly_work_packages]
+) do
shared_let(:status_locked) { create(:status, is_readonly: true) }
- shared_let(:source) { create(:project, enabled_module_names: %w[wiki work_package_tracking]) }
+ shared_let(:source) do
+ create(:project,
+ name: 'Source Project Name',
+ enabled_module_names: %i[wiki work_package_tracking storages])
+ end
shared_let(:source_wp) { create(:work_package, project: source, subject: 'source wp') }
shared_let(:source_wp_locked) do
create(:work_package, project: source, subject: 'source wp locked', status: status_locked)
@@ -49,49 +59,59 @@ RSpec.describe Projects::CopyService, 'integration', type: :model, with_ee: %i[r
member_in_project: source,
member_through_role: role)
end
- let(:instance) do
- described_class.new(source:, user: current_user)
- end
+ let(:instance) { described_class.new(source:, user: current_user) }
let(:only_args) { nil }
let(:target_project_params) do
- { name: 'Some name', identifier: 'some-identifier' }
+ { name: 'Target Project Name', identifier: 'some-identifier' }
end
let(:params) do
{ target_project_params:, only: only_args, send_notifications: }
end
let(:send_notifications) { true }
- shared_let(:role) { create(:role, permissions: %i[copy_projects view_work_packages work_package_assigned]) }
+ shared_let(:role) do
+ create(:role,
+ permissions: %i[copy_projects
+ view_work_packages
+ work_package_assigned
+ manage_storages_in_project
+ manage_file_links])
+ end
shared_let(:new_project_role) { create(:role, permissions: %i[]) }
before do
allow(Setting)
.to receive(:new_project_user_role_id)
- .and_return(new_project_role.id.to_s)
+ .and_return(new_project_role.id.to_s)
end
describe '.copyable_dependencies' do
- it 'includes dependencies for work packages as well as for their attachments' do
- expect(described_class.copyable_dependencies.pluck(:identifier))
- .to include(Projects::Copy::WorkPackagesDependentService.identifier,
- Projects::Copy::WorkPackageAttachmentsDependentService.identifier)
- end
-
- it 'includes dependencies for wiki as well as for their pages\'s attachments' do
- expect(described_class.copyable_dependencies.pluck(:identifier))
- .to include(Projects::Copy::WikiDependentService.identifier,
- Projects::Copy::WikiPageAttachmentsDependentService.identifier)
+ it 'includes the list of dependencies' do
+ expect(described_class.copyable_dependencies.pluck(:identifier)).to eq(
+ %w(
+ members
+ versions
+ categories
+ work_packages
+ work_package_attachments
+ wiki
+ wiki_page_attachments
+ forums
+ queries
+ boards
+ overview
+ storages
+ storage_project_folders
+ file_links
+ )
+ )
end
end
- describe 'call' do
+ describe '.call' do
subject { instance.call(params) }
- let(:all_modules) do
- %i[members versions categories work_packages work_package_attachments wiki wiki_page_attachments
- forums queries boards overview storages file_links]
- end
- let(:only_args) { all_modules }
+ let(:all_modules) { described_class.copyable_dependencies.pluck(:identifier) }
let(:project_copy) { subject.result }
shared_examples_for 'copies public attribute' do
@@ -194,365 +214,450 @@ RSpec.describe Projects::CopyService, 'integration', type: :model, with_ee: %i[r
end
end
- context 'restricting only to members and categories' do
- let(:only_args) { %w[members categories] }
-
- it 'limits 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
+ context 'with all modules selected' do
+ let(:only_args) { all_modules }
+ let(:storage1) { source_automatic_project_storage.storage }
+ let(:storage2) { source_manual_project_storage.storage }
+ let(:host) { storage1.host }
+ let!(:file_outside_project_folder_link) do
+ create(:file_link,
+ origin_id: "100",
+ origin_name: "file_name1.txt",
+ container: source_wp,
+ storage: storage1)
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
- # normal wp and locked wp
- expect(project_copy.work_packages.count).to eq 2
- 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 2
- expect(project_copy.queries.count).to eq 1
- expect(project_copy.queries[0].views.count).to eq 1
- expect(project_copy.versions.count).to eq 1
- expect(project_copy.wiki.pages.root.text).to eq source_wiki_page.text
- expect(project_copy.wiki.pages.leaves.first.text).to eq source_child_wiki_page.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
-
- # Default role being assigned according to setting
- # merged with the role the user already had.
- member = project_copy.members.last
- expect(member.principal)
- .to eql(current_user)
- expect(member.roles)
- .to contain_exactly(role, new_project_role)
- 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.find_by(subject: source_wp.subject)
- expect(wp.category.name).to eq 'Stock management'
- # Category got copied
- expect(wp.category.id).not_to eq source_category.id
- end
-
- context 'with an assigned version' do
- let!(:assigned_version) { create(:version, name: 'Assigned Issues', project: source, status: 'open') }
-
- before do
- source_wp.update!(version: assigned_version)
- assigned_version.update!(status: 'closed')
+ let!(:file_inside_automatic_project_folder_link) do
+ create(:file_link,
+ origin_id: "101",
+ origin_name: "file_name2.txt",
+ container: source_wp,
+ storage: storage1)
end
-
- it 'will update the version' do
- expect(subject).to be_success
-
- wp = project_copy.work_packages.find_by(subject: source_wp.subject)
- expect(wp.version.name).to eq 'Assigned Issues'
- expect(wp.version).to be_closed
- expect(wp.version.id).not_to eq assigned_version.id
+ let!(:file_inside_manual_project_folder_link) do
+ create(:file_link,
+ origin_id: "102",
+ origin_name: "file_name3.txt",
+ container: source_wp,
+ storage: storage2)
end
- end
-
- context 'with group memberships' do
- let(:only_args) { %w[members] }
+ let!(:oauth_client) do
+ create(:oauth_client,
+ client_id: "nwz34rWsolvJvchfQ1bVHXfMb1ETK89lCBgzrLhWx3ACW5nKfmdcyf5ftlCyKGbk",
+ client_secret: "A08n6CRBOOr41iqkWRynnP6BbmEnau7LeP9t9xrIbiYX46iXgmIZgqhJoDFjUMEq",
+ integration: storage1)
+ end
+ let!(:oauth_client_token) { create(:oauth_client_token, oauth_client:, user: current_user) }
+ let(:destination_url) { %r{#{host}/remote.php/dav/files/OpenProject/OpenProject/Target%20Project%20Name%20} }
+ let(:source_url) { %r{#{host}/remote.php/dav/files/OpenProject/OpenProject/Source%20Project%20Name%20} }
+ let(:new_project_folder_id) { "819" }
- let!(:user) { create(:user) }
- let!(:another_role) { create(:role) }
- let!(:group) do
- create(:group, members: [user])
+ shared_let(:source_automatic_project_storage) do
+ create(:project_storage, project: source, project_folder_id: '123', project_folder_mode: 'automatic')
+ end
+ shared_let(:source_manual_project_storage) do
+ create(:project_storage, project: source, project_folder_id: '345', project_folder_mode: 'manual')
end
- it 'will copy them as well' do
- Members::CreateService
- .new(user: current_user, contract_class: EmptyContract)
- .call(principal: group, roles: [another_role], project: source)
+ before do
+ stub_request(:head, destination_url).to_return(status: 404)
+ stub_request(:copy, source_url).to_return(status: 201)
+ stub_request(:propfind, destination_url).with(headers: { 'Depth' => '1' }).to_return do |_request|
+ project_id = Project.where(name: "Target Project Name").pick(:id)
+ body = <<~XML
+ <?xml version="1.0"?>
+ <d:multistatus
+ xmlns:d="DAV:"
+ xmlns:s="http://sabredav.org/ns"
+ xmlns:oc="http://owncloud.org/ns"
+ xmlns:nc="http://nextcloud.org/ns">
+ <d:response>
+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/Target%20Project%20Name%20(#{project_id})/</d:href>
+ <d:propstat>
+ <d:prop>
+ <oc:fileid>#{new_project_folder_id}</oc:fileid>
+ </d:prop>
+ <d:status>HTTP/1.1 200 OK</d:status>
+ </d:propstat>
+ </d:response>
+ </d:multistatus>
+ XML
+ { status: 200, body:, headers: {} }
+ end
+ stub_request(:propfind, destination_url).with(headers: { 'Depth' => 'infinity' }).to_return do |_request|
+ project_id = Project.where(name: "Target Project Name").pick(:id)
+ body = <<~XML
+ <?xml version="1.0"?>
+ <d:multistatus
+ xmlns:d="DAV:"
+ xmlns:s="http://sabredav.org/ns"
+ xmlns:oc="http://owncloud.org/ns"
+ xmlns:nc="http://nextcloud.org/ns">
+ <d:response>
+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/Target%20Project%20Name%20(#{project_id})/</d:href>
+ <d:propstat>
+ <d:prop>
+ <oc:fileid>#{new_project_folder_id}</oc:fileid>
+ </d:prop>
+ <d:status>HTTP/1.1 200 OK</d:status>
+ </d:propstat>
+ </d:response>
+ <d:response>
+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/Target%20Project%20Name%20(#{project_id})/#{file_inside_automatic_project_folder_link.origin_name}</d:href>
+ <d:propstat>
+ <d:prop>
+ <oc:fileid>430</oc:fileid>
+ </d:prop>
+ <d:status>HTTP/1.1 200 OK</d:status>
+ </d:propstat>
+ </d:response>
+ </d:multistatus>
+ XML
+ { status: 200, body:, headers: {} }
+ end
- source.users.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
+ filesinfo_response_body = <<~JSON
+ {
+ "ocs": {
+ "meta": {
+ "status": "ok",
+ "statuscode": 100,
+ "message": "OK",
+ "totalitems": "",
+ "itemsperpage": ""
+ },
+ "data": {
+ "#{file_outside_project_folder_link.origin_id}": {
+ "status": "OK",
+ "statuscode": 200,
+ "id": #{file_outside_project_folder_link.origin_id},
+ "name": "#{file_outside_project_folder_link.origin_name}",
+ "mtime": 1688632254,
+ "ctime": 0,
+ "mimetype": "application\\/pdf",
+ "size": 15181180,
+ "owner_name": "admin",
+ "owner_id": "admin",
+ "trashed": false,
+ "modifier_name": "admin",
+ "modifier_id": "admin",
+ "dav_permissions": "RGDNVW",
+ "path": "files\\/#{file_outside_project_folder_link.origin_name}"
+ },
+ "#{file_inside_automatic_project_folder_link.origin_id}": {
+ "status": "OK",
+ "statuscode": 200,
+ "id": #{file_inside_automatic_project_folder_link.origin_id},
+ "name": "#{file_inside_automatic_project_folder_link.origin_name}",
+ "mtime": 1689687843,
+ "ctime": 0,
+ "mimetype": "image\\/jpeg",
+ "size": 94064,
+ "owner_name": "admin",
+ "owner_id": "admin",
+ "trashed": false,
+ "modifier_name": null,
+ "modifier_id": null,
+ "dav_permissions": "RMGDNVW",
+ "path": "files\\/OpenProject\\/Source Project Name (#{source.id})\\/#{file_inside_automatic_project_folder_link.origin_name}"
+ }
+ }
+ }
+ }
+ JSON
+ stub_request(:post, "#{host}/ocs/v1.php/apps/integration_openproject/filesinfo")
+ .with(body: '{"fileIds":["100","101"]}')
+ .to_return(status: 200, body: filesinfo_response_body, headers: { 'Content-Type' => 'application/json' })
+ end
+ # rubocop:disable RSpec/ExampleLength
+ # rubocop:disable RSpec/MultipleExpectations
+ it 'copies all dependencies and set attributes' do
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) { create(:work_package, project: source, subject: 'source wp2') }
- let!(:source_relation) { create(:relation, from: source_wp, to: source_wp2, relation_type: 'relates') }
+ expect(project_copy.members.count).to eq 1
+ expect(project_copy.categories.count).to eq 1
+ # normal wp and locked wp
+ expect(project_copy.work_packages.count).to eq 2
+ 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 2
+ expect(project_copy.queries.count).to eq 1
+ expect(project_copy.queries[0].views.count).to eq 1
+ expect(project_copy.versions.count).to eq 1
+ expect(project_copy.wiki.pages.root.text).to eq source_wiki_page.text
+ expect(project_copy.wiki.pages.leaves.first.text).to eq source_child_wiki_page.text
+ expect(project_copy.wiki.start_page).to eq 'Wiki'
- let!(:other_project) { create(:project) }
- let!(:other_wp) { create(:work_package, project: other_project, subject: 'other wp') }
- let!(:cross_relation) { create(:relation, from: source_wp, to: other_wp, relation_type: 'duplicates') }
+ # Cleared attributes
+ expect(project_copy).to be_persisted
+ expect(project_copy.name).to eq 'Target Project Name'
+ expect(project_copy.identifier).to eq 'some-identifier'
- let(:only_args) { %w[work_packages] }
+ # 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
- it 'copies relations' do
- expect(subject).to be_success
+ # Default attributes
+ expect(project_copy).to be_active
- 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.count).to eq 2
- relates_relation = copied_wp.relations.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.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
+ # Default role being assigned according to setting
+ # merged with the role the user already had.
+ member = project_copy.members.last
+ expect(member.principal).to eql(current_user)
+ expect(member.roles).to contain_exactly(role, new_project_role)
+
+ expect(project_copy.projects_storages.count).to eq(2)
+ automatic_project_storage_copy = project_copy.projects_storages.find_by(storage: storage1)
+ expect(automatic_project_storage_copy.id).not_to eq(source_automatic_project_storage.id)
+ expect(automatic_project_storage_copy.project_id).to eq(project_copy.id)
+ expect(automatic_project_storage_copy.creator_id).to eq(current_user.id)
+ expect(automatic_project_storage_copy.project_folder_id).to eq("819")
+ expect(automatic_project_storage_copy.project_folder_mode).to eq('automatic')
+
+ manual_project_storage_copy = project_copy.projects_storages.find_by(storage: storage2)
+ expect(manual_project_storage_copy.id).not_to eq(source_manual_project_storage.id)
+ expect(manual_project_storage_copy.project_id).to eq(project_copy.id)
+ expect(manual_project_storage_copy.creator_id).to eq(current_user.id)
+ expect(manual_project_storage_copy.project_folder_id).to eq("345")
+ expect(manual_project_storage_copy.project_folder_mode).to eq('manual')
+
+ wp_copy = project_copy.work_packages.where(subject: "source wp").first
+ expect(wp_copy.file_links.count).to eq(3)
+ file_outside_project_folder_link_copy = wp_copy.file_links.find_by(origin_name: "file_name1.txt")
+ file_inside_automatic_project_folder_link_copy = wp_copy.file_links.find_by(origin_name: "file_name2.txt")
+ file_inside_manual_project_folder_link_copy = wp_copy.file_links.find_by(origin_name: "file_name3.txt")
+ expect(file_outside_project_folder_link_copy.id).not_to eq(file_outside_project_folder_link.id)
+ expect(file_outside_project_folder_link_copy.origin_id).to eq(file_outside_project_folder_link.origin_id)
+ expect(file_outside_project_folder_link_copy.storage_id).to eq(file_outside_project_folder_link.storage_id)
+ expect(file_inside_automatic_project_folder_link_copy.id).not_to eq(file_inside_automatic_project_folder_link.id)
+ expect(file_inside_automatic_project_folder_link_copy.origin_id).to eq("430")
+ expect(file_inside_automatic_project_folder_link_copy.storage_id).to eq(file_inside_automatic_project_folder_link.storage_id)
+ expect(file_inside_manual_project_folder_link_copy.id).not_to eq(file_inside_manual_project_folder_link.id)
+ expect(file_inside_manual_project_folder_link_copy.origin_id).to eq("102")
+ expect(file_inside_manual_project_folder_link_copy.storage_id).to eq(file_inside_manual_project_folder_link.storage_id)
end
- end
-
- context 'with work package budgets' do
- let!(:budget) { create(:budget, project: source) }
-
- let(:only_args) { %w[work_packages] }
+ # rubocop:enable RSpec/ExampleLength
+ # rubocop:enable RSpec/MultipleExpectations
- it 'copies the work package without budgets' do
- source_wp.update!(budget:)
+ it_behaves_like 'copies public attribute'
+ it_behaves_like 'copies custom fields'
+ end
- expect(subject).to be_success
+ context 'with some modules selected' do
+ context 'with queries' do
+ let(:only_args) { %i[queries] }
- expect(source.work_packages.count).to eq(project_copy.work_packages.count)
- copied_wp = project_copy.work_packages.find_by(subject: 'source wp')
- expect(copied_wp.budget).to be_nil
- end
- end
+ context 'with a filter' do
+ let!(:query) do
+ build(:query, project: source).tap do |q|
+ q.add_filter('subject', '~', ['bogus'])
+ q.save!
- describe '#copy_wiki' do
- it 'will copy menu items' do
- source.wiki.wiki_menu_items << create(:wiki_menu_item_with_parent, wiki: source.wiki)
+ create(:view_work_packages_table, query: q)
+ end
+ end
- expect(subject).to be_success
- expect(project_copy.wiki.wiki_menu_items.count).to eq 3
- end
+ it 'produces a valid query in the new project' do
+ expect(subject).to be_success
+ expect(project_copy.queries.all?(&:valid?)).to be(true)
+ expect(project_copy.queries.count).to eq 2
+ end
+ end
- context 'with attachments' do
- let!(:attachment) { create(:attachment, container: source_wiki_page) }
+ context 'with a filter to be mapped' do
+ let(:only_args) { %w(members work_packages queries) }
+ let!(:query) do
+ build(:query, project: source).tap do |q|
+ q.add_filter('parent', '=', [source_wp.id.to_s])
+ # Not valid due to wp not visible
+ q.save!(validate: false)
- context 'when requested' do
- let(:only_args) { %i[wiki wiki_page_attachments] }
+ create(:view_work_packages_table, query: q)
+ end
+ end
- it 'copies them' do
+ it 'produces a valid query that is mapped in the new project' do
expect(subject).to be_success
- expect(subject.errors).to be_empty
- expect(project_copy.wiki.pages.count).to eq 2
-
- page = project_copy.wiki.pages.find_by(title: source_wiki_page.title)
- expect(page.attachments.count).to eq(1)
- expect(page.attachments.first.author).to eql(current_user)
+ copied_wp = project_copy.work_packages.find_by(subject: 'source wp')
+ copied = project_copy.queries.find_by(name: query.name)
+ expect(copied.filters[1].values).to eq [copied_wp.id.to_s]
end
end
- context 'when not requested' do
- let(:only_args) { %i[wiki] }
+ context 'with query with views' do
+ let!(:query_with_view) do
+ query = build(:query, project: source, name: 'Query with view')
+ query.add_filter('subject', '~', ['bogus'])
+ query.save!
- it 'ignores them' do
- expect(subject).to be_success
- expect(subject.errors).to be_empty
- expect(project_copy.wiki.pages.count).to eq 2
+ create(:view_work_packages_table, query:)
- page = project_copy.wiki.pages.find_by(title: source_wiki_page.title)
- expect(page.attachments.count).to eq(0)
+ query
end
- end
- end
- end
- describe 'valid queries' do
- context 'with a filter' do
- let!(:query) do
- build(:query, project: source).tap do |q|
- q.add_filter('subject', '~', ['bogus'])
- q.save!
+ let!(:query_without_view) do
+ query = build(:query, project: source, name: 'Query without view')
+ query.add_filter('subject', '~', ['bogus'])
+ query.save!
- create(:view_work_packages_table, query: q)
+ query
end
- end
- it 'produces a valid query in the new project' do
- expect(subject).to be_success
- expect(project_copy.queries.all?(&:valid?)).to be(true)
- expect(project_copy.queries.count).to eq 2
+ it 'copies only the query with a view (non viewed queries will have to implement specific copy service)' do
+ expect(subject).to be_success
+ copied_query_with_view = project_copy.queries.find_by(name: 'Query with view')
+ expect(copied_query_with_view).to be_present
+ expect(copied_query_with_view.views.length).to eq 1
+ expect(copied_query_with_view.views[0].type).to eq 'work_packages_table'
+
+ expect(project_copy.queries).not_to exist(name: 'Query without view')
+ end
end
end
- context 'with a filter to be mapped' do
- let!(:query) do
- build(:query, project: source).tap do |q|
- q.add_filter('parent', '=', [source_wp.id.to_s])
- # Not valid due to wp not visible
- q.save!(validate: false)
+ context 'with memeber' do
+ let(:only_args) { %w[members] }
- create(:view_work_packages_table, query: q)
- end
- end
+ let!(:user) { create(:user) }
+ let!(:another_role) { create(:role) }
+ let!(:group) { create(:group, members: [user]) }
+
+ it 'copies them as well' do
+ Members::CreateService
+ .new(user: current_user, contract_class: EmptyContract)
+ .call(principal: group, roles: [another_role], project: source)
+
+ source.users.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
- it 'produces a valid query that is mapped in the new project' do
expect(subject).to be_success
- copied_wp = project_copy.work_packages.find_by(subject: 'source wp')
- copied = project_copy.queries.find_by(name: query.name)
- expect(copied.filters[1].values).to eq [copied_wp.id.to_s]
- end
- end
- end
- describe 'views' do
- let!(:query_with_view) do
- query = build(:query, project: source, name: 'Query with view')
- query.add_filter('subject', '~', ['bogus'])
- query.save!
+ 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
- create(:view_work_packages_table, query:)
+ 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]
- query
+ 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
- let!(:query_without_view) do
- query = build(:query, project: source, name: 'Query without view')
- query.add_filter('subject', '~', ['bogus'])
- query.save!
+ context 'with work_packages' do
+ let(:only_args) { %w[work_packages] }
- query
- end
+ let(:work_package) { create(:work_package, project: source) }
- it 'copies only the query with a view (non viewed queries will have to implement specific copy service)' do
- expect(subject).to be_success
- copied_query_with_view = project_copy.queries.find_by(name: 'Query with view')
- expect(copied_query_with_view).to be_present
- expect(copied_query_with_view.views.length).to eq 1
- expect(copied_query_with_view.views[0].type).to eq 'work_packages_table'
+ # rubocop:disable RSpec/IndexedLet
+ let(:work_package2) { create(:work_package, project: source) }
+ let(:work_package3) { create(:work_package, project: source) }
+ # rubocop:enable RSpec/IndexedLet
- expect(project_copy.queries)
- .not_to exist(name: 'Query without view')
- end
- end
+ it 'does not copy work package budgets' do
+ budget = create(:budget, project: source)
+ source_wp.update!(budget:)
- describe 'work packages' do
- let(:work_package) { create(:work_package, project: source) }
- let(:work_package2) { create(:work_package, project: source) }
- let(:work_package3) { create(:work_package, project: source) }
+ expect(subject).to be_success
- let(:only_args) { %w[work_packages] }
+ expect(source.work_packages.count).to eq(project_copy.work_packages.count)
+ copied_wp = project_copy.work_packages.find_by(subject: 'source wp')
+ expect(copied_wp.budget).to be_nil
+ end
- context 'with attachments' do
- let!(:attachment) { create(:attachment, container: work_package) }
+ context 'if categories are copied' do
+ let(:only_args) { %i[work_packages categories] }
- context 'when requested' do
- let(:only_args) { %i[work_packages work_package_attachments] }
+ it 'copies the work package with category' do
+ source_wp.update!(category: source_category)
- it 'copies them' do
expect(subject).to be_success
- expect(project_copy.work_packages.count).to eq(3)
- wp = project_copy.work_packages.find_by(subject: work_package.subject)
- expect(wp.attachments.count).to eq(1)
- expect(wp.attachments.first.author).to eql(current_user)
+ wp = project_copy.work_packages.find_by(subject: source_wp.subject)
+ expect(wp.category.name).to eq 'Stock management'
+ # Category got copied
+ expect(wp.category.id).not_to eq source_category.id
end
end
- context 'when not requested' do
- it 'ignores them' do
- expect(subject).to be_success
- expect(project_copy.work_packages.count).to eq(3)
+ context 'with an assigned version' do
+ let(:only_args) { %i[work_packages versions] }
+ let!(:assigned_version) { create(:version, name: 'Assigned Issues', project: source, status: 'open') }
- wp = project_copy.work_packages.find_by(subject: work_package.subject)
- expect(wp.attachments.count).to eq(0)
+ before do
+ source_wp.update!(version: assigned_version)
+ assigned_version.update!(status: 'closed')
end
- end
- end
- describe 'in an ordered query (Feature #31317)' do
- let!(:query) do
- create(:query, name: 'Manual query', user: current_user, project: source, show_hierarchies: false).tap do |q|
- q.sort_criteria = [[:manual_sorting, 'asc']]
- q.save!
+ it 'updates the version' do
+ expect(subject).to be_success
- create(:view_work_packages_table, query: q)
+ wp = project_copy.work_packages.find_by(subject: source_wp.subject)
+ 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
- let(:only_args) { %w[work_packages queries] }
- before do
- OrderedWorkPackage.create(query:, work_package:, position: 100)
- OrderedWorkPackage.create(query:, work_package: work_package2, position: 0)
- OrderedWorkPackage.create(query:, work_package: work_package3, position: 50)
- end
+ context 'with attachments' do
+ before do
+ create(:attachment, container: work_package)
+ expect(work_package.attachments.count).to eq(1) # rubocop:disable RSpec/ExpectInHook
+ end
- it 'copies the query and order' do
- expect(subject).to be_success
- expect(project_copy.work_packages.count).to eq(5)
- expect(project_copy.queries.count).to eq(2)
+ context 'when requested' do
+ let(:only_args) { %i[work_packages work_package_attachments] }
- manual_query = project_copy.queries.find_by name: 'Manual query'
- expect(manual_query).to be_manually_sorted
+ it 'copies them' do
+ expect(subject).to be_success
+ expect(project_copy.work_packages.count).to eq(3)
+
+ wp = project_copy.work_packages.find_by(subject: work_package.subject)
+ expect(wp.attachments.count).to eq(1)
+ expect(wp.attachments.first.author).to eql(current_user)
+ end
+ end
- expect(query.ordered_work_packages.count).to eq 3
- original_order = query.ordered_work_packages.map { |ow| ow.work_package.subject }
- copied_order = manual_query.ordered_work_packages.map { |ow| ow.work_package.subject }
+ context 'when not requested' do
+ it 'ignores them' do
+ expect(subject).to be_success
+ expect(project_copy.work_packages.count).to eq(3)
- expect(copied_order).to eq(original_order)
+ wp = project_copy.work_packages.find_by(subject: work_package.subject)
+ expect(wp.attachments.count).to eq(0)
+ end
+ end
end
- context 'if one work package is a cross project reference' do
- let(:other_project) { create(:project) }
+ context 'with an ordered query (Feature #31317)' do
+ let!(:query) do
+ create(:query, name: 'Manual query', user: current_user, project: source, show_hierarchies: false).tap do |q|
+ q.sort_criteria = [[:manual_sorting, 'asc']]
+ q.save!
+
+ create(:view_work_packages_table, query: q)
+ end
+ end
let(:only_args) { %w[work_packages queries] }
before do
- work_package2.update! project: other_project
+ OrderedWorkPackage.create(query:, work_package:, position: 100)
+ OrderedWorkPackage.create(query:, work_package: work_package2, position: 0)
+ OrderedWorkPackage.create(query:, work_package: work_package3, position: 50)
end
it 'copies the query and order' do
expect(subject).to be_success
- # Only 4 out of the 5 work packages got copied this time
- expect(project_copy.work_packages.count).to eq(4)
+ expect(project_copy.work_packages.count).to eq(5)
expect(project_copy.queries.count).to eq(2)
manual_query = project_copy.queries.find_by name: 'Manual query'
@@ -563,100 +668,120 @@ RSpec.describe Projects::CopyService, 'integration', type: :model, with_ee: %i[r
copied_order = manual_query.ordered_work_packages.map { |ow| ow.work_package.subject }
expect(copied_order).to eq(original_order)
-
- # Expect reference to the original work package
- referenced = query.ordered_work_packages.detect { |ow| ow.work_package == work_package2 }
- expect(referenced).to be_present
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
+ context 'if one work package is a cross project reference' do
+ let(:other_project) { create(:project) }
+ let(:only_args) { %w[work_packages queries] }
- it do
- expect(subject).to be_success
+ before do
+ work_package2.update! project: other_project
+ end
- 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)
+ it 'copies the query and order' do
+ expect(subject).to be_success
+ # Only 4 out of the 5 work packages got copied this time
+ expect(project_copy.work_packages.count).to eq(4)
+ expect(project_copy.queries.count).to eq(2)
- [grandparent_wp_copy,
- parent_wp_copy,
- child_wp_copy].each do |wp|
- expect(wp).to be_present
- end
+ manual_query = project_copy.queries.find_by name: 'Manual query'
+ expect(manual_query).to be_manually_sorted
- expect(child_wp_copy.parent).to eq(parent_wp_copy)
- expect(parent_wp_copy.parent).to eq(grandparent_wp_copy)
+ expect(query.ordered_work_packages.count).to eq 3
+ original_order = query.ordered_work_packages.map { |ow| ow.work_package.subject }
+ copied_order = manual_query.ordered_work_packages.map { |ow| ow.work_package.subject }
+
+ expect(copied_order).to eq(original_order)
+
+ # Expect reference to the original work package
+ referenced = query.ordered_work_packages.detect { |ow| ow.work_package == work_package2 }
+ expect(referenced).to be_present
+ end
+ end
end
- end
- describe '#category' do
- let(:only_args) { %w[work_packages categories] }
+ context 'with parent work_package' do
+ before do
+ work_package.parent = work_package2
+ work_package.save!
+ work_package2.parent = work_package3
+ work_package2.save!
+ end
- before do
- wp = work_package
- wp.category = create(:category, project: source)
- wp.save
+ it do
+ expect(subject).to be_success
- source.work_packages << wp.reload
- end
+ 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)
- it do
- expect(subject).to be_success
- wp = project_copy.work_packages.find_by(subject: work_package.subject)
- expect(cat = wp.category).not_to be_nil
- expect(cat.project).to eq(project_copy)
+ expect([grandparent_wp_copy, parent_wp_copy, child_wp_copy]).to all be_present
+ expect(child_wp_copy.parent).to eq(parent_wp_copy)
+ expect(parent_wp_copy.parent).to eq(grandparent_wp_copy)
+ end
end
- end
-
- describe '#watchers' do
- let(:watcher_role) { create(:role, permissions: [:view_work_packages]) }
- let(:watcher) { create(:user, member_in_project: source, member_through_role: watcher_role) }
- let(:only_args) { %w[work_packages members] }
+ context 'with category' do
+ let(:only_args) { %w[work_packages categories] }
- describe '#active_watcher' do
before do
wp = work_package
- wp.add_watcher watcher
+ wp.category = create(:category, project: source)
wp.save
source.work_packages << wp
end
- it 'does copy active watchers but does not add the copying user as a watcher' do
+ it do
expect(subject).to be_success
- expect(project_copy.work_packages[0].watcher_users)
- .to contain_exactly(watcher)
+ wp = project_copy.work_packages.find_by(subject: work_package.subject)
+ expect(cat = wp.category).not_to be_nil
+ expect(cat.project).to eq(project_copy)
end
end
- describe '#locked_watcher' do
- before do
- user = watcher
- wp = work_package
- wp.add_watcher user
- wp.save
+ context 'with watchers' do
+ let(:watcher_role) { create(:role, permissions: [:view_work_packages]) }
+ let(:watcher) { create(:user, member_in_project: source, member_through_role: watcher_role) }
- user.locked!
+ let(:only_args) { %w[work_packages members] }
- source.work_packages << wp
+ context 'with active watcher' do
+ before do
+ wp = work_package
+ wp.add_watcher watcher
+ wp.save
+
+ source.work_packages << wp
+ end
+
+ it 'does copy active watchers but does not add the copying user as a watcher' do
+ expect(subject).to be_success
+ expect(project_copy.work_packages[0].watcher_users)
+ .to contain_exactly(watcher)
+ end
end
- it 'does not copy locked watchers and does not add the copying user as a watcher' do
- expect(subject).to be_success
- expect(project_copy.work_packages[0].watcher_users).to be_empty
+ context 'with locked watcher' do
+ before do
+ user = watcher
+ wp = work_package
+ wp.add_watcher user
+ wp.save
+
+ user.locked!
+
+ source.work_packages << wp
+ end
+
+ it 'does not copy locked watchers and does not add the copying user as a watcher' do
+ expect(subject).to be_success
+ expect(project_copy.work_packages[0].watcher_users).to be_empty
+ end
end
end
- describe 'versions' do
+ context 'with versions' do
let(:version) { create(:version, project: source) }
let(:version2) { create(:version, project: source) }
@@ -678,130 +803,195 @@ RSpec.describe Projects::CopyService, 'integration', type: :model, with_ee: %i[r
.to be_nil
end
end
- end
- describe 'assigned_to' do
- let(:assigned_user) do
- create(:user,
- member_in_project: source,
- member_through_role: role)
- end
+ context 'when work_package is assigned to somebody' do
+ let(:assigned_user) do
+ create(:user,
+ member_in_project: source,
+ member_through_role: role)
+ end
- before do
- work_package.update_column(:assigned_to_id, assigned_user.id)
- end
+ before do
+ work_package.update_column(:assigned_to_id, assigned_user.id)
+ end
- context 'with the members being copied' do
- let(:only_args) { %w[members work_packages] }
+ 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.work_packages[0].assigned_to)
+ .to eql assigned_user
+ # The assignee of the new work package receives a notification
+ expect { perform_enqueued_jobs }
+ .to change(Notification.where(recipient: assigned_user), :count)
+ .from(0)
+ .to(1)
+ end
+ end
- it 'copies the assigned_to' do
- expect(subject).to be_success
- expect(project_copy.work_packages[0].assigned_to)
- .to eql assigned_user
- # The assignee of the new work package receives a notification
- expect { perform_enqueued_jobs }
- .to change(Notification.where(recipient: assigned_user), :count)
- .from(0)
- .to(1)
+ context 'with the member being not copied' 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
+ # No notification is sent out
+ expect { perform_enqueued_jobs }
+ .not_to change(Notification.where(recipient: assigned_user), :count)
+ end
end
end
- context 'with the assignee not being a member' do
- let(:only_args) { %w[work_packages] }
+ context 'when work_package has a responsible person' do
+ let(:responsible_user) do
+ create(:user,
+ member_in_project: source,
+ member_through_role: role)
+ end
- it 'nils the assigned_to' do
- expect(subject).to be_success
- expect(project_copy.work_packages[0].assigned_to)
- .to be_nil
- # No notification is sent out
- expect { perform_enqueued_jobs }
- .not_to change(Notification.where(recipient: assigned_user), :count)
+ before do
+ work_package.update_column(:responsible_id, responsible_user.id)
end
- end
- end
- describe 'responsible' do
- let(:responsible_user) do
- create(:user,
- member_in_project: source,
- member_through_role: role)
- 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.work_packages[0].responsible)
+ .to eql responsible_user
+ # The responsible of the new work package receives a notification
+ expect { perform_enqueued_jobs }
+ .to change(Notification.where(recipient: responsible_user), :count)
+ .from(0)
+ .to(1)
+ end
+ end
- before do
- work_package.update_column(:responsible_id, responsible_user.id)
+ context 'with the member being not copied' 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
+ # No notification is sent out
+ expect { perform_enqueued_jobs }
+ .not_to change(Notification.where(recipient: responsible_user), :count)
+ end
+ end
end
- context 'with the members being copied' do
- let(:only_args) { %w[members work_packages] }
+ describe 'work package user custom field' do
+ let(:custom_field) do
+ create(:user_wp_custom_field).tap do |cf|
+ source.work_package_custom_fields << cf
+ work_package.type.custom_fields << cf
+ end
+ end
- it 'copies the responsible' do
- expect(subject).to be_success
- expect(project_copy.work_packages[0].responsible)
- .to eql responsible_user
- # The responsible of the new work package receives a notification
- expect { perform_enqueued_jobs }
- .to change(Notification.where(recipient: responsible_user), :count)
- .from(0)
- .to(1)
+ before do
+ custom_field
+ work_package.reload
+ work_package.send(custom_field.attribute_setter, current_user.id)
+ work_package.save!(validate: false)
+ end
+
+ context 'with the member being copied' 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.attribute_getter)).to eql current_user
+ end
+ end
+
+ context 'with the member being not copied' 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.attribute_getter)).to be_nil
+ end
end
end
- context 'with the responsible not being a member' do
- let(:only_args) { %w[work_packages] }
+ context('with work package relations',
+ with_settings: { cross_project_work_package_relations: '1' }) do
+ let!(:source_wp2) { create(:work_package, project: source, subject: 'source wp2') }
+ let!(:source_relation) { create(:relation, from: source_wp, to: source_wp2, relation_type: 'relates') }
- it 'nils the assigned_to' do
+ let!(:other_project) { create(:project) }
+ let!(:other_wp) { create(:work_package, project: other_project, subject: 'other wp') }
+ let!(:cross_relation) { create(:relation, from: source_wp, to: other_wp, relation_type: 'duplicates') }
+
+ it 'copies relations' do
expect(subject).to be_success
- expect(project_copy.work_packages[0].responsible)
- .to be_nil
- # No notification is sent out
- expect { perform_enqueued_jobs }
- .not_to change(Notification.where(recipient: responsible_user), :count)
+
+ expect(source.work_packages.count).to eq(project_copy.work_packages.count)
+ copied_wp = project_copy.work_packages.find_by(subject: 'source wp')
+ copied_wp2 = project_copy.work_packages.find_by(subject: 'source wp2')
+
+ # First issue with a relation on project
+ # copied relation + reflexive relation
+ expect(copied_wp.relations.count).to eq 2
+ relates_relation = copied_wp.relations.find { |r| r.relation_type == 'relates' }
+ expect(relates_relation.from_id).to eq copied_wp.id
+ expect(relates_relation.to_id).to eq copied_wp2.id
+
+ # Second issue with a cross project relation
+ # copied relation + reflexive relation
+ duplicates_relation = copied_wp.relations.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
end
- describe 'work package user custom field' do
- let(:custom_field) do
- create(:user_wp_custom_field).tap do |cf|
- source.work_package_custom_fields << cf
- work_package.type.custom_fields << cf
- end
- end
+ context 'with wiki' do
+ let(:only_args) { %i[wiki] }
- before do
- custom_field
- work_package.reload
- work_package.send(custom_field.attribute_setter, current_user.id)
- work_package.save!(validate: false)
+ it 'will copy wiki menu items' do
+ source.wiki.wiki_menu_items << 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
- context 'with the value being a member' do
- let(:only_args) { %w[members work_packages] }
+ it 'ignores wiki attachments' do
+ create(:attachment, container: source_wiki_page)
+ expect(source_wiki_page.attachments.count).to eq(1)
- 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.attribute_getter))
- .to eql current_user
- end
+ expect(subject).to be_success
+ expect(subject.errors).to be_empty
+ expect(project_copy.wiki.pages.count).to eq 2
+
+ page = project_copy.wiki.pages.find_by(title: source_wiki_page.title)
+ expect(page.attachments.count).to eq(0)
end
- context 'with the value not being a member' do
- let(:only_args) { %w[work_packages] }
+ context 'when wiki attachments are requested' do
+ let(:only_args) { %i[wiki wiki_page_attachments] }
+
+ it 'copies them' do
+ create(:attachment, container: source_wiki_page)
+ expect(source_wiki_page.attachments.count).to eq(1)
- 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.attribute_getter))
- .to be_nil
+ expect(subject.errors).to be_empty
+ expect(project_copy.wiki.pages.count).to eq 2
+
+ page = project_copy.wiki.pages.find_by(title: source_wiki_page.title)
+ expect(page.attachments.count).to eq(1)
+ expect(page.attachments.first.author).to eql(current_user)
end
end
end
end
- it_behaves_like 'copies public attribute'
- it_behaves_like 'copies custom fields'
-
context 'without anything selected' do
let!(:source_member) { create(:user, member_in_project: source, member_through_role: role) }
let(:only_args) { nil }
@@ -823,8 +1013,8 @@ RSpec.describe Projects::CopyService, 'integration', type: :model, with_ee: %i[r
# 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.name).to eq 'Target Project Name'
+ expect(project_copy.name).to eq 'Target Project Name'
expect(project_copy.identifier).to eq 'some-identifier'
# Duplicated attributes