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

[#48351] Unchecked copy options are still copied in the new project

Dombi Attila355c11672c57
diff --git a/app/services/projects/copy/dependency.rb b/app/services/projects/copy/dependency.rb
index 1af657c7177..f107d0bc6e7 100644
--- a/app/services/projects/copy/dependency.rb
+++ b/app/services/projects/copy/dependency.rb
@@ -41,7 +41,7 @@ module Projects::Copy
# Check whether this dependency should be copied
# as it was selected
def self.should_copy?(params, check)
- return true if params[:only].blank?
+ return false if params[:only].blank?
params[:only].any? { |key| key.to_sym == check }
end
diff --git a/modules/overviews/spec/services/copy_service_integration_spec.rb b/modules/overviews/spec/services/copy_service_integration_spec.rb
index 02065a2996c..ce9d3d4fd1f 100644
--- a/modules/overviews/spec/services/copy_service_integration_spec.rb
+++ b/modules/overviews/spec/services/copy_service_integration_spec.rb
@@ -111,6 +111,8 @@ describe Projects::CopyService, 'integration', type: :model do
end
describe 'copy' do
+ let(:only_args) { %i[versions categories overview] }
+
before do
original_overview
diff --git a/spec/services/projects/copy_service_integration_spec.rb b/spec/services/projects/copy_service_integration_spec.rb
index dfd74e53846..ca131ba589b 100644
--- a/spec/services/projects/copy_service_integration_spec.rb
+++ b/spec/services/projects/copy_service_integration_spec.rb
@@ -60,8 +60,8 @@ describe Projects::CopyService, 'integration', type: :model do
{ target_project_params:, only: only_args, send_notifications: }
end
let(:send_notifications) { true }
- let(:role) { create(:role, permissions: %i[copy_projects view_work_packages work_package_assigned]) }
+ shared_let(:role) { create(:role, permissions: %i[copy_projects view_work_packages work_package_assigned]) }
shared_let(:new_project_role) { create(:role, permissions: %i[]) }
before do
@@ -89,8 +89,113 @@ describe Projects::CopyService, 'integration', type: :model 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(:project_copy) { subject.result }
+ shared_examples_for 'copies public attribute' do
+ describe '#public' do
+ before do
+ source.update!(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
+ end
+
+ shared_examples_for 'copies custom fields' do
+ describe 'project custom fields' do
+ context 'with user project CF' do
+ let(:user_custom_field) { create(:user_project_custom_field) }
+ let(:user_value) do
+ create(:user,
+ member_in_project: source,
+ member_through_role: role)
+ end
+
+ before do
+ source.custom_values << CustomValue.new(custom_field: user_custom_field, value: user_value.id.to_s)
+ end
+
+ it 'copies the custom_field' do
+ expect(subject).to be_success
+
+ cv = project_copy.custom_values.reload.find_by(custom_field: user_custom_field)
+ expect(cv).to be_present
+ expect(cv.value).to eq user_value.id.to_s
+ expect(cv.typed_value).to eq user_value
+ end
+ end
+
+ context 'with multi selection project list CF' do
+ let(:list_custom_field) { create(:list_project_custom_field, multi_value: true) }
+
+ before do
+ source.custom_values << CustomValue.new(custom_field: list_custom_field, value: list_custom_field.value_of('A'))
+ source.custom_values << CustomValue.new(custom_field: list_custom_field, value: list_custom_field.value_of('B'))
+
+ source.save!
+ end
+
+ it 'copies the custom_field' do
+ expect(subject).to be_success
+
+ cv = project_copy.custom_values.reload.where(custom_field: list_custom_field).to_a
+ expect(cv).to be_a Array
+ expect(cv.count).to eq 2
+ expect(cv.map(&:formatted_value)).to contain_exactly('A', 'B')
+ end
+ end
+
+ context 'with disabled work package custom field' do
+ it 'is still disabled in the copy' do
+ custom_field = create(:text_wp_custom_field)
+ create(:type_task,
+ projects: [source],
+ custom_fields: [custom_field])
+
+ expect(subject).to be_success
+
+ expect(source.work_package_custom_fields).to eq([])
+ expect(project_copy.work_package_custom_fields).to match_array(source.work_package_custom_fields)
+ end
+ end
+
+ context 'with enabled work package custom field' do
+ it 'is still enabled in the copy' do
+ custom_field = create(:text_wp_custom_field, projects: [source])
+ create(:type_task,
+ projects: [source],
+ custom_fields: [custom_field])
+
+ expect(subject).to be_success
+
+ expect(source.work_package_custom_fields).to eq([custom_field])
+ expect(project_copy.work_package_custom_fields).to match_array(source.work_package_custom_fields)
+ end
+ end
+ end
+ end
+
context 'restricting only to members and categories' do
let(:only_args) { %w[members categories] }
@@ -141,11 +246,11 @@ describe Projects::CopyService, 'integration', type: :model do
# Default role being assigned according to setting
# merged with the role the user already had.
- member = project_copy.members.first
+ member = project_copy.members.last
expect(member.principal)
.to eql(current_user)
expect(member.roles)
- .to match_array [role, new_project_role]
+ .to contain_exactly(role, new_project_role)
end
it 'will copy the work package with category' do
@@ -159,30 +264,6 @@ describe Projects::CopyService, 'integration', type: :model do
expect(wp.category.id).not_to eq source_category.id
end
- describe '#public' do
- before do
- source.update!(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) { create(:version, name: 'Assigned Issues', project: source, status: 'open') }
@@ -322,6 +403,8 @@ describe Projects::CopyService, 'integration', type: :model do
end
context 'when not requested' do
+ let(:only_args) { %i[wiki] }
+
it 'ignores them' do
expect(subject).to be_success
expect(subject.errors).to be_empty
@@ -562,7 +645,7 @@ describe Projects::CopyService, 'integration', type: :model do
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 match_array([watcher])
+ .to contain_exactly(watcher)
end
end
@@ -727,78 +810,86 @@ describe Projects::CopyService, 'integration', type: :model do
end
end
- describe 'project custom fields' do
- context 'with user project CF' do
- let(:user_custom_field) { create(:user_project_custom_field) }
- let(:only_args) { %w[wiki] }
- let(:user_value) do
- create(:user,
- member_in_project: source,
- member_through_role: role)
- end
+ it_behaves_like 'copies public attribute'
+ it_behaves_like 'copies custom fields'
- before do
- source.custom_values << CustomValue.new(custom_field: user_custom_field, value: user_value.id.to_s)
- end
-
- it 'copies the custom_field' do
- expect(subject).to be_success
+ context 'without anything selected' do
+ let!(:source_member) { create(:user, member_in_project: source, member_through_role: role) }
+ let(:only_args) { nil }
- cv = project_copy.custom_values.reload.find_by(custom_field: user_custom_field)
- expect(cv).to be_present
- expect(cv.value).to eq user_value.id.to_s
- expect(cv.typed_value).to eq user_value
- end
- end
+ # rubocop:disable RSpec/MultipleExpectations
+ it 'will set attributes only without copying dependencies' do
+ expect(subject).to be_success
- context 'with multi selection project list CF' do
- let(:list_custom_field) { create(:list_project_custom_field, multi_value: true) }
- let(:only_args) { %w[wiki] }
+ expect(project_copy.members.count).to eq 1
+ expect(project_copy.categories.count).to eq 0
+ expect(project_copy.work_packages.count).to eq 0
+ expect(project_copy.forums.count).to eq 0
+ # Default wiki page
+ expect(project_copy.wiki).to be_present
+ expect(project_copy.wiki.pages.count).to eq 0
+ expect(project_copy.wiki.wiki_menu_items.count).to eq 1
+ expect(project_copy.queries.count).to eq 0
+ expect(project_copy.versions.count).to eq 0
- before do
- source.custom_values << CustomValue.new(custom_field: list_custom_field, value: list_custom_field.value_of('A'))
- source.custom_values << CustomValue.new(custom_field: list_custom_field, value: list_custom_field.value_of('B'))
+ # 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
+
+ # Copy only the current_user as we do not copy any members
+ # Only the default role is being assigned according to setting
+ member = project_copy.reload.members.first
+ expect(member.principal)
+ .to eql(current_user)
+ expect(member.roles)
+ .to contain_exactly(new_project_role)
+ end
+ # rubocop:enable RSpec/MultipleExpectations
- source.save!
+ context 'with group memberships' do
+ let!(:user) { create(:user) }
+ let!(:another_role) { create(:role) }
+ let!(:group) do
+ create(:group, members: [user])
end
- it 'copies the custom_field' do
- expect(subject).to be_success
-
- cv = project_copy.custom_values.reload.where(custom_field: list_custom_field).to_a
- expect(cv).to be_a Array
- expect(cv.count).to eq 2
- expect(cv.map(&:formatted_value)).to contain_exactly('A', 'B')
- end
- end
+ it 'will not copy group members' do
+ Members::CreateService
+ .new(user: current_user, contract_class: EmptyContract)
+ .call(principal: group, roles: [another_role], project: source)
- context 'with disabled work package custom field' do
- it 'is still disabled in the copy' do
- custom_field = create(:text_wp_custom_field)
- create(:type_task,
- projects: [source],
- custom_fields: [custom_field])
+ source.users.reload
+ expect(source.users).to include current_user
+ expect(source.users).to include user
+ expect(project_copy.groups).to be_empty
+ expect(source.member_principals.count).to eq 4
expect(subject).to be_success
- expect(source.work_package_custom_fields).to eq([])
- expect(project_copy.work_package_custom_fields).to match_array(source.work_package_custom_fields)
- end
- end
+ expect(project_copy.member_principals.count).to eq 1
+ expect(project_copy.groups).to be_empty
+ expect(project_copy.users).to contain_exactly current_user
- context 'with enabled work package custom field' do
- it 'is still enabled in the copy' do
- custom_field = create(:text_wp_custom_field, projects: [source])
- create(:type_task,
- projects: [source],
- custom_fields: [custom_field])
+ group_member = Member.find_by(user_id: group.id, project_id: project_copy.id)
+ expect(group_member).to be_nil
- expect(subject).to be_success
-
- expect(source.work_package_custom_fields).to eq([custom_field])
- expect(project_copy.work_package_custom_fields).to match_array(source.work_package_custom_fields)
+ member = Member.find_by(user_id: user.id, project_id: project_copy.id)
+ expect(member).to be_nil
end
end
+
+ it_behaves_like 'copies public attribute'
+ it_behaves_like 'copies custom fields'
end
end
end