You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project_copy_test.rb 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class ProjectCopyTest < ActiveSupport::TestCase
  19. fixtures :projects, :trackers, :issue_statuses, :issues,
  20. :journals, :journal_details,
  21. :enumerations, :users, :issue_categories,
  22. :projects_trackers,
  23. :custom_fields,
  24. :custom_fields_projects,
  25. :custom_fields_trackers,
  26. :custom_values,
  27. :roles,
  28. :member_roles,
  29. :members,
  30. :enabled_modules,
  31. :versions,
  32. :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
  33. :groups_users,
  34. :boards, :messages,
  35. :repositories,
  36. :news, :comments,
  37. :documents, :attachments
  38. def setup
  39. User.current = nil
  40. ProjectCustomField.destroy_all
  41. @source_project = Project.find(2)
  42. @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
  43. @project.trackers = @source_project.trackers
  44. @project.enabled_module_names = @source_project.enabled_modules.collect(&:name)
  45. end
  46. def test_copy_should_return_false_if_save_fails
  47. project = Project.new(:name => 'Copy', :identifier => nil)
  48. assert_equal false, project.copy(@source_project)
  49. end
  50. test "#copy should copy project attachments" do
  51. Attachment.create!(:container => @source_project, :file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 1)
  52. assert @project.copy(@source_project)
  53. assert_equal 1, @project.attachments.count, "Attachment not copied"
  54. assert_equal "testfile.txt", @project.attachments.first.filename
  55. end
  56. test "#copy should copy issues" do
  57. @source_project.issues << Issue.generate!(:status => IssueStatus.find_by_name('Closed'),
  58. :subject => "copy issue status",
  59. :tracker_id => 1,
  60. :assigned_to_id => 2,
  61. :project_id => @source_project.id)
  62. assert @project.valid?
  63. assert @project.issues.empty?
  64. assert @project.copy(@source_project)
  65. assert_equal @source_project.issues.size, @project.issues.size
  66. @project.issues.each do |issue|
  67. assert issue.valid?
  68. assert ! issue.assigned_to.blank?
  69. assert_equal @project, issue.project
  70. end
  71. copied_issue = @project.issues.where(:subject => "copy issue status").first
  72. assert copied_issue
  73. assert copied_issue.status
  74. assert_equal "Closed", copied_issue.status.name
  75. end
  76. test "#copy should copy issues custom values" do
  77. field = IssueCustomField.generate!(:is_for_all => true, :trackers => Tracker.all)
  78. issue = Issue.generate!(:project => @source_project, :subject => 'Custom field copy')
  79. issue.custom_field_values = {field.id => 'custom'}
  80. issue.save!
  81. assert_equal 'custom', issue.reload.custom_field_value(field)
  82. assert @project.copy(@source_project)
  83. copy = @project.issues.find_by_subject('Custom field copy')
  84. assert copy
  85. assert_equal 'custom', copy.reload.custom_field_value(field)
  86. end
  87. test "#copy should copy issues assigned to a locked version" do
  88. User.current = User.find(1)
  89. assigned_version = Version.generate!(:name => "Assigned Issues")
  90. @source_project.versions << assigned_version
  91. Issue.generate!(:project => @source_project,
  92. :fixed_version_id => assigned_version.id,
  93. :subject => "copy issues assigned to a locked version")
  94. assigned_version.update_attribute :status, 'locked'
  95. assert @project.copy(@source_project)
  96. @project.reload
  97. copied_issue = @project.issues.where(:subject => "copy issues assigned to a locked version").first
  98. assert copied_issue
  99. assert copied_issue.fixed_version
  100. assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
  101. assert_equal 'locked', copied_issue.fixed_version.status
  102. end
  103. test "#copy should change the new issues to use the copied version" do
  104. User.current = User.find(1)
  105. assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open')
  106. @source_project.versions << assigned_version
  107. assert_equal 3, @source_project.versions.size
  108. Issue.generate!(:project => @source_project,
  109. :fixed_version_id => assigned_version.id,
  110. :subject => "change the new issues to use the copied version")
  111. assert @project.copy(@source_project)
  112. @project.reload
  113. copied_issue = @project.issues.where(:subject => "change the new issues to use the copied version").first
  114. assert copied_issue
  115. assert copied_issue.fixed_version
  116. assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
  117. assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record
  118. end
  119. test "#copy should keep target shared versions from other project" do
  120. assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open', :project_id => 1, :sharing => 'system')
  121. issue = Issue.generate!(:project => @source_project,
  122. :fixed_version => assigned_version,
  123. :subject => "keep target shared versions")
  124. assert @project.copy(@source_project)
  125. @project.reload
  126. copied_issue = @project.issues.where(:subject => "keep target shared versions").first
  127. assert copied_issue
  128. assert_equal assigned_version, copied_issue.fixed_version
  129. end
  130. def test_copy_issues_should_reassign_version_custom_fields_to_copied_versions
  131. User.current = User.find(1)
  132. CustomField.destroy_all
  133. field = IssueCustomField.generate!(:field_format => 'version', :is_for_all => true, :trackers => Tracker.all)
  134. source_project = Project.generate!(:trackers => Tracker.all)
  135. source_version = Version.generate!(:project => source_project)
  136. source_issue = Issue.generate!(:project => source_project) do |issue|
  137. issue.custom_field_values = {field.id.to_s => source_version.id.to_s}
  138. end
  139. assert_equal source_version.id.to_s, source_issue.custom_field_value(field)
  140. project = Project.new(:name => 'Copy Test', :identifier => 'copy-test', :trackers => Tracker.all)
  141. assert project.copy(source_project)
  142. assert_equal 1, project.issues.count
  143. issue = project.issues.first
  144. assert_equal 1, project.versions.count
  145. version = project.versions.first
  146. assert_equal version.id.to_s, issue.custom_field_value(field)
  147. end
  148. test "#copy should copy issue relations" do
  149. Setting.cross_project_issue_relations = '1'
  150. second_issue = Issue.generate!(:status_id => 5,
  151. :subject => "copy issue relation",
  152. :tracker_id => 1,
  153. :assigned_to_id => 2,
  154. :project_id => @source_project.id)
  155. source_relation = IssueRelation.create!(:issue_from => Issue.find(4),
  156. :issue_to => second_issue,
  157. :relation_type => "relates")
  158. source_relation_cross_project = IssueRelation.create!(:issue_from => Issue.find(1),
  159. :issue_to => second_issue,
  160. :relation_type => "duplicates")
  161. assert @project.copy(@source_project)
  162. assert_equal @source_project.issues.count, @project.issues.count
  163. copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4
  164. copied_second_issue = @project.issues.find_by_subject("copy issue relation")
  165. # First issue with a relation on project
  166. assert_equal 1, copied_issue.relations.size, "Relation not copied"
  167. copied_relation = copied_issue.relations.first
  168. assert_equal "relates", copied_relation.relation_type
  169. assert_equal copied_second_issue.id, copied_relation.issue_to_id
  170. assert_not_equal source_relation.id, copied_relation.id
  171. # Second issue with a cross project relation
  172. assert_equal 2, copied_second_issue.relations.size, "Relation not copied"
  173. copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first
  174. assert_equal "duplicates", copied_relation.relation_type
  175. assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept"
  176. assert_not_equal source_relation_cross_project.id, copied_relation.id
  177. end
  178. test "#copy should copy issue attachments" do
  179. issue = Issue.generate!(:subject => "copy with attachment", :tracker_id => 1, :project_id => @source_project.id)
  180. Attachment.create!(:container => issue, :file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 1)
  181. @source_project.issues << issue
  182. assert @project.copy(@source_project)
  183. copied_issue = @project.issues.where(:subject => "copy with attachment").first
  184. assert_not_nil copied_issue
  185. assert_equal 1, copied_issue.attachments.count, "Attachment not copied"
  186. assert_equal "testfile.txt", copied_issue.attachments.first.filename
  187. end
  188. test "#copy should copy memberships" do
  189. assert @project.valid?
  190. assert @project.members.empty?
  191. assert @project.copy(@source_project)
  192. assert_equal @source_project.memberships.size, @project.memberships.size
  193. @project.memberships.each do |membership|
  194. assert membership
  195. assert_equal @project, membership.project
  196. end
  197. end
  198. test "#copy should copy memberships with groups and additional roles" do
  199. group = Group.create!(:lastname => "Copy group")
  200. user = User.find(7)
  201. group.users << user
  202. # group role
  203. Member.create!(:project_id => @source_project.id, :principal => group, :role_ids => [2])
  204. member = Member.find_by_user_id_and_project_id(user.id, @source_project.id)
  205. # additional role
  206. member.role_ids = [1]
  207. assert @project.copy(@source_project)
  208. member = Member.find_by_user_id_and_project_id(user.id, @project.id)
  209. assert_not_nil member
  210. assert_equal [1, 2], member.role_ids.sort
  211. end
  212. def test_copy_should_copy_project_specific_issue_queries
  213. source = Project.generate!
  214. target = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
  215. IssueQuery.generate!(:project => source, :user => User.find(2))
  216. assert target.copy(source)
  217. assert_equal 1, target.queries.size
  218. query = target.queries.first
  219. assert_kind_of IssueQuery, query
  220. assert_equal 2, query.user_id
  221. end
  222. def test_copy_should_copy_project_specific_time_entry_queries
  223. source = Project.generate!
  224. target = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
  225. TimeEntryQuery.generate!(:project => source, :user => User.find(2))
  226. assert target.copy(source)
  227. assert_equal 1, target.queries.size
  228. query = target.queries.first
  229. assert_kind_of TimeEntryQuery, query
  230. assert_equal 2, query.user_id
  231. end
  232. def test_copy_should_copy_queries_roles_visibility
  233. source = Project.generate!
  234. target = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
  235. IssueQuery.generate!(:project => source, :visibility => Query::VISIBILITY_ROLES, :roles => Role.where(:id => [1, 3]).to_a)
  236. assert target.copy(source)
  237. assert_equal 1, target.queries.size
  238. query = target.queries.first
  239. assert_equal [1, 3], query.role_ids.sort
  240. end
  241. test "#copy should copy versions" do
  242. @source_project.versions << Version.generate!
  243. @source_project.versions << Version.generate!
  244. assert @project.versions.empty?
  245. assert @project.copy(@source_project)
  246. assert_equal @source_project.versions.size, @project.versions.size
  247. @project.versions.each do |version|
  248. assert version
  249. assert_equal @project, version.project
  250. end
  251. end
  252. test "#copy should copy version attachments" do
  253. version = Version.generate!(:name => "copy with attachment")
  254. Attachment.create!(:container => version, :file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 1)
  255. @source_project.versions << version
  256. assert @project.copy(@source_project)
  257. copied_version = @project.versions.where(:name => "copy with attachment").first
  258. assert_not_nil copied_version
  259. assert_equal 1, copied_version.attachments.count, "Attachment not copied"
  260. assert_equal "testfile.txt", copied_version.attachments.first.filename
  261. end
  262. test "#copy should copy wiki" do
  263. assert_difference 'Wiki.count' do
  264. assert @project.copy(@source_project)
  265. end
  266. assert @project.wiki
  267. assert_not_equal @source_project.wiki, @project.wiki
  268. assert_equal "Start page", @project.wiki.start_page
  269. end
  270. test "#copy should copy wiki without wiki module" do
  271. project = Project.new(:name => 'Copy Test', :identifier => 'copy-test', :enabled_module_names => [])
  272. assert_difference 'Wiki.count' do
  273. assert project.copy(@source_project)
  274. end
  275. assert project.wiki
  276. end
  277. test "#copy should copy wiki pages, attachment and content with hierarchy" do
  278. @source_project.wiki.pages.first.attachments << Attachment.first.copy
  279. assert_difference 'WikiPage.count', @source_project.wiki.pages.size do
  280. assert @project.copy(@source_project)
  281. end
  282. assert @project.wiki
  283. assert_equal @source_project.wiki.pages.size, @project.wiki.pages.size
  284. assert_equal @source_project.wiki.pages.first.attachments.first.filename, @project.wiki.pages.first.attachments.first.filename
  285. @project.wiki.pages.each do |wiki_page|
  286. assert wiki_page.content
  287. assert !@source_project.wiki.pages.include?(wiki_page)
  288. end
  289. parent = @project.wiki.find_page('Parent_page')
  290. child1 = @project.wiki.find_page('Child_page_1')
  291. child2 = @project.wiki.find_page('Child_page_2')
  292. assert_equal parent, child1.parent
  293. assert_equal parent, child2.parent
  294. end
  295. test "#copy should copy issue categories" do
  296. assert @project.copy(@source_project)
  297. assert_equal 2, @project.issue_categories.size
  298. @project.issue_categories.each do |issue_category|
  299. assert !@source_project.issue_categories.include?(issue_category)
  300. end
  301. end
  302. test "#copy should copy boards" do
  303. assert @project.copy(@source_project)
  304. assert_equal 1, @project.boards.size
  305. @project.boards.each do |board|
  306. assert !@source_project.boards.include?(board)
  307. end
  308. end
  309. test "#copy should copy documents" do
  310. source_project = Project.find(1)
  311. assert @project.copy(source_project)
  312. assert_equal 2, @project.documents.size
  313. @project.documents.each do |document|
  314. assert !source_project.documents.include?(document)
  315. end
  316. end
  317. test "#copy should copy document attachments" do
  318. document = Document.generate!(:title => "copy with attachment", :category_id => 1, :project_id => @source_project.id)
  319. Attachment.create!(:container => document, :file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 1)
  320. @source_project.documents << document
  321. assert @project.copy(@source_project)
  322. copied_document = @project.documents.where(:title => "copy with attachment").first
  323. assert_not_nil copied_document
  324. assert_equal 1, copied_document.attachments.count, "Attachment not copied"
  325. assert_equal "testfile.txt", copied_document.attachments.first.filename
  326. end
  327. test "#copy should change the new issues to use the copied issue categories" do
  328. issue = Issue.find(4)
  329. issue.update_attribute(:category_id, 3)
  330. assert @project.copy(@source_project)
  331. @project.issues.each do |issue|
  332. assert issue.category
  333. assert_equal "Stock management", issue.category.name # Same name
  334. assert_not_equal IssueCategory.find(3), issue.category # Different record
  335. end
  336. end
  337. test "#copy should limit copy with :only option" do
  338. assert @project.members.empty?
  339. assert @project.issue_categories.empty?
  340. assert @source_project.issues.any?
  341. assert @project.copy(@source_project, :only => ['members', 'issue_categories'])
  342. assert @project.members.any?
  343. assert @project.issue_categories.any?
  344. assert @project.issues.empty?
  345. end
  346. test "#copy should copy subtasks" do
  347. source = Project.generate!(:tracker_ids => [1])
  348. issue = Issue.generate_with_descendants!(:project => source)
  349. project = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1])
  350. assert_difference 'Project.count' do
  351. assert_difference 'Issue.count', 1+issue.descendants.count do
  352. assert project.copy(source.reload)
  353. end
  354. end
  355. copy = Issue.where(:parent_id => nil).order("id DESC").first
  356. assert_equal project, copy.project
  357. assert_equal issue.descendants.count, copy.descendants.count
  358. child_copy = copy.children.detect {|c| c.subject == 'Child1'}
  359. assert child_copy.descendants.any?
  360. end
  361. end