1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
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.
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.
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.
18 require File.dirname(__FILE__) + '/../test_helper'
20 class ProjectTest < ActiveSupport::TestCase
24 @ecookbook = Project.find(1)
25 @ecookbook_sub1 = Project.find(3)
29 should_validate_presence_of :name
30 should_validate_presence_of :identifier
32 should_validate_uniqueness_of :name
33 should_validate_uniqueness_of :identifier
35 context "associations" do
36 should_have_many :members
37 should_have_many :users, :through => :members
38 should_have_many :member_principals
39 should_have_many :principals, :through => :member_principals
40 should_have_many :enabled_modules
41 should_have_many :issues
42 should_have_many :issue_changes, :through => :issues
43 should_have_many :versions
44 should_have_many :time_entries
45 should_have_many :queries
46 should_have_many :documents
47 should_have_many :news
48 should_have_many :issue_categories
49 should_have_many :boards
50 should_have_many :changesets, :through => :repository
52 should_have_one :repository
55 should_have_and_belong_to_many :trackers
56 should_have_and_belong_to_many :issue_custom_fields
60 assert_kind_of Project, @ecookbook
61 assert_equal "eCookbook", @ecookbook.name
65 assert_equal "eCookbook", @ecookbook.name
66 @ecookbook.name = "eCook"
67 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
69 assert_equal "eCook", @ecookbook.name
72 def test_validate_identifier
73 to_test = {"abc" => true,
79 to_test.each do |identifier, valid|
81 p.identifier = identifier
83 assert_equal valid, p.errors.on('identifier').nil?
87 def test_members_should_be_active_users
88 Project.all.each do |project|
89 assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) }
93 def test_users_should_be_active_users
94 Project.all.each do |project|
95 assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) }
100 user = @ecookbook.members.first.user
104 assert !@ecookbook.active?
105 assert !user.projects.include?(@ecookbook)
106 # Subproject are also archived
107 assert !@ecookbook.children.empty?
108 assert @ecookbook.descendants.active.empty?
111 def test_archive_should_fail_if_versions_are_used_by_non_descendant_projects
112 # Assign an issue of a project to a version of a child project
113 Issue.find(4).update_attribute :fixed_version_id, 4
115 assert_no_difference "Project.count(:all, :conditions => 'status = #{Project::STATUS_ARCHIVED}')" do
116 assert_equal false, @ecookbook.archive
119 assert @ecookbook.active?
123 user = @ecookbook.members.first.user
125 # A subproject of an archived project can not be unarchived
126 assert !@ecookbook_sub1.unarchive
129 assert @ecookbook.unarchive
131 assert @ecookbook.active?
132 assert user.projects.include?(@ecookbook)
133 # Subproject can now be unarchived
134 @ecookbook_sub1.reload
135 assert @ecookbook_sub1.unarchive
140 assert_equal 2, @ecookbook.members.size
142 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
144 assert @ecookbook.boards.any?
147 # make sure that the project non longer exists
148 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
149 # make sure related data was removed
150 assert_nil Member.first(:conditions => {:project_id => @ecookbook.id})
151 assert_nil Board.first(:conditions => {:project_id => @ecookbook.id})
152 assert_nil Issue.first(:conditions => {:project_id => @ecookbook.id})
155 def test_move_an_orphan_project_to_a_root_project
156 sub = Project.find(2)
157 sub.set_parent! @ecookbook
158 assert_equal @ecookbook.id, sub.parent.id
160 assert_equal 4, @ecookbook.children.size
163 def test_move_an_orphan_project_to_a_subproject
164 sub = Project.find(2)
165 assert sub.set_parent!(@ecookbook_sub1)
168 def test_move_a_root_project_to_a_project
170 assert sub.set_parent!(Project.find(2))
173 def test_should_not_move_a_project_to_its_children
175 assert !(sub.set_parent!(Project.find(3)))
178 def test_set_parent_should_add_roots_in_alphabetical_order
179 ProjectCustomField.delete_all
181 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
182 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
183 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
184 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
186 assert_equal 4, Project.count
187 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
190 def test_set_parent_should_add_children_in_alphabetical_order
191 ProjectCustomField.delete_all
192 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
193 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
194 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
195 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
196 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
199 assert_equal 4, parent.children.size
200 assert_equal parent.children.sort_by(&:name), parent.children
203 def test_rebuild_should_sort_children_alphabetically
204 ProjectCustomField.delete_all
205 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
206 Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
207 Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
208 Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
209 Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
211 Project.update_all("lft = NULL, rgt = NULL")
215 assert_equal 4, parent.children.size
216 assert_equal parent.children.sort_by(&:name), parent.children
220 def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy
221 # Parent issue with a hierarchy project's fixed version
222 parent_issue = Issue.find(1)
223 parent_issue.update_attribute(:fixed_version_id, 4)
225 assert_equal 4, parent_issue.fixed_version_id
227 # Should keep fixed versions for the issues
228 issue_with_local_fixed_version = Issue.find(5)
229 issue_with_local_fixed_version.update_attribute(:fixed_version_id, 4)
230 issue_with_local_fixed_version.reload
231 assert_equal 4, issue_with_local_fixed_version.fixed_version_id
233 # Local issue with hierarchy fixed_version
234 issue_with_hierarchy_fixed_version = Issue.find(13)
235 issue_with_hierarchy_fixed_version.update_attribute(:fixed_version_id, 6)
236 issue_with_hierarchy_fixed_version.reload
237 assert_equal 6, issue_with_hierarchy_fixed_version.fixed_version_id
239 # Move project out of the issue's hierarchy
240 moved_project = Project.find(3)
241 moved_project.set_parent!(Project.find(2))
243 issue_with_local_fixed_version.reload
244 issue_with_hierarchy_fixed_version.reload
246 assert_equal 4, issue_with_local_fixed_version.fixed_version_id, "Fixed version was not keep on an issue local to the moved project"
247 assert_equal nil, issue_with_hierarchy_fixed_version.fixed_version_id, "Fixed version is still set after moving the Project out of the hierarchy where the version is defined in"
248 assert_equal nil, parent_issue.fixed_version_id, "Fixed version is still set after moving the Version out of the hierarchy for the issue."
252 p = Project.find(6).parent
253 assert p.is_a?(Project)
258 a = Project.find(6).ancestors
259 assert a.first.is_a?(Project)
260 assert_equal [1, 5], a.collect(&:id)
264 r = Project.find(6).root
265 assert r.is_a?(Project)
270 c = Project.find(1).children
271 assert c.first.is_a?(Project)
272 assert_equal [5, 3, 4], c.collect(&:id)
276 d = Project.find(1).descendants
277 assert d.first.is_a?(Project)
278 assert_equal [5, 6, 3, 4], d.collect(&:id)
281 def test_allowed_parents_should_be_empty_for_non_member_user
282 Role.non_member.add_permission!(:add_project)
284 assert user.memberships.empty?
286 assert Project.new.allowed_parents.compact.empty?
289 def test_allowed_parents_with_add_subprojects_permission
290 Role.find(1).remove_permission!(:add_project)
291 Role.find(1).add_permission!(:add_subprojects)
292 User.current = User.find(2)
294 assert !Project.new.allowed_parents.include?(nil)
295 assert Project.new.allowed_parents.include?(Project.find(1))
296 # existing root project
297 assert Project.find(1).allowed_parents.include?(nil)
299 assert Project.find(3).allowed_parents.include?(Project.find(1))
300 assert !Project.find(3).allowed_parents.include?(nil)
303 def test_allowed_parents_with_add_project_permission
304 Role.find(1).add_permission!(:add_project)
305 Role.find(1).remove_permission!(:add_subprojects)
306 User.current = User.find(2)
308 assert Project.new.allowed_parents.include?(nil)
309 assert !Project.new.allowed_parents.include?(Project.find(1))
310 # existing root project
311 assert Project.find(1).allowed_parents.include?(nil)
313 assert Project.find(3).allowed_parents.include?(Project.find(1))
314 assert Project.find(3).allowed_parents.include?(nil)
317 def test_allowed_parents_with_add_project_and_subprojects_permission
318 Role.find(1).add_permission!(:add_project)
319 Role.find(1).add_permission!(:add_subprojects)
320 User.current = User.find(2)
322 assert Project.new.allowed_parents.include?(nil)
323 assert Project.new.allowed_parents.include?(Project.find(1))
324 # existing root project
325 assert Project.find(1).allowed_parents.include?(nil)
327 assert Project.find(3).allowed_parents.include?(Project.find(1))
328 assert Project.find(3).allowed_parents.include?(nil)
331 def test_users_by_role
332 users_by_role = Project.find(1).users_by_role
333 assert_kind_of Hash, users_by_role
335 assert_kind_of Array, users_by_role[role]
336 assert users_by_role[role].include?(User.find(2))
339 def test_rolled_up_trackers
340 parent = Project.find(1)
341 parent.trackers = Tracker.find([1,2])
342 child = parent.children.find(3)
344 assert_equal [1, 2], parent.tracker_ids
345 assert_equal [2, 3], child.trackers.collect(&:id)
347 assert_kind_of Tracker, parent.rolled_up_trackers.first
348 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
350 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
351 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
354 def test_rolled_up_trackers_should_ignore_archived_subprojects
355 parent = Project.find(1)
356 parent.trackers = Tracker.find([1,2])
357 child = parent.children.find(3)
358 child.trackers = Tracker.find([1,3])
359 parent.children.each(&:archive)
361 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
364 context "#rolled_up_versions" do
366 @project = Project.generate!
367 @parent_version_1 = Version.generate!(:project => @project)
368 @parent_version_2 = Version.generate!(:project => @project)
371 should "include the versions for the current project" do
372 assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
375 should "include versions for a subproject" do
376 @subproject = Project.generate!
377 @subproject.set_parent!(@project)
378 @subproject_version = Version.generate!(:project => @subproject)
380 assert_same_elements [
384 ], @project.rolled_up_versions
387 should "include versions for a sub-subproject" do
388 @subproject = Project.generate!
389 @subproject.set_parent!(@project)
390 @sub_subproject = Project.generate!
391 @sub_subproject.set_parent!(@subproject)
392 @sub_subproject_version = Version.generate!(:project => @sub_subproject)
396 assert_same_elements [
399 @sub_subproject_version
400 ], @project.rolled_up_versions
404 should "only check active projects" do
405 @subproject = Project.generate!
406 @subproject.set_parent!(@project)
407 @subproject_version = Version.generate!(:project => @subproject)
408 assert @subproject.archive
412 assert !@subproject.active?
413 assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
417 def test_shared_versions_none_sharing
419 v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none')
420 assert p.shared_versions.include?(v)
421 assert !p.children.first.shared_versions.include?(v)
422 assert !p.root.shared_versions.include?(v)
423 assert !p.siblings.first.shared_versions.include?(v)
424 assert !p.root.siblings.first.shared_versions.include?(v)
427 def test_shared_versions_descendants_sharing
429 v = Version.create!(:name => 'descendants_sharing', :project => p, :sharing => 'descendants')
430 assert p.shared_versions.include?(v)
431 assert p.children.first.shared_versions.include?(v)
432 assert !p.root.shared_versions.include?(v)
433 assert !p.siblings.first.shared_versions.include?(v)
434 assert !p.root.siblings.first.shared_versions.include?(v)
437 def test_shared_versions_hierarchy_sharing
439 v = Version.create!(:name => 'hierarchy_sharing', :project => p, :sharing => 'hierarchy')
440 assert p.shared_versions.include?(v)
441 assert p.children.first.shared_versions.include?(v)
442 assert p.root.shared_versions.include?(v)
443 assert !p.siblings.first.shared_versions.include?(v)
444 assert !p.root.siblings.first.shared_versions.include?(v)
447 def test_shared_versions_tree_sharing
449 v = Version.create!(:name => 'tree_sharing', :project => p, :sharing => 'tree')
450 assert p.shared_versions.include?(v)
451 assert p.children.first.shared_versions.include?(v)
452 assert p.root.shared_versions.include?(v)
453 assert p.siblings.first.shared_versions.include?(v)
454 assert !p.root.siblings.first.shared_versions.include?(v)
457 def test_shared_versions_system_sharing
459 v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system')
460 assert p.shared_versions.include?(v)
461 assert p.children.first.shared_versions.include?(v)
462 assert p.root.shared_versions.include?(v)
463 assert p.siblings.first.shared_versions.include?(v)
464 assert p.root.siblings.first.shared_versions.include?(v)
467 def test_shared_versions
468 parent = Project.find(1)
469 child = parent.children.find(3)
470 private_child = parent.children.find(5)
472 assert_equal [1,2,3], parent.version_ids.sort
473 assert_equal [4], child.version_ids
474 assert_equal [6], private_child.version_ids
475 assert_equal [7], Version.find_all_by_sharing('system').collect(&:id)
477 assert_equal 6, parent.shared_versions.size
478 parent.shared_versions.each do |version|
479 assert_kind_of Version, version
482 assert_equal [1,2,3,4,6,7], parent.shared_versions.collect(&:id).sort
485 def test_shared_versions_should_ignore_archived_subprojects
486 parent = Project.find(1)
487 child = parent.children.find(3)
491 assert_equal [1,2,3], parent.version_ids.sort
492 assert_equal [4], child.version_ids
493 assert !parent.shared_versions.collect(&:id).include?(4)
496 def test_shared_versions_visible_to_user
498 parent = Project.find(1)
499 child = parent.children.find(5)
501 assert_equal [1,2,3], parent.version_ids.sort
502 assert_equal [6], child.version_ids
504 versions = parent.shared_versions.visible(user)
506 assert_equal 4, versions.size
507 versions.each do |version|
508 assert_kind_of Version, version
511 assert !versions.collect(&:id).include?(6)
515 def test_next_identifier
516 ProjectCustomField.delete_all
517 Project.create!(:name => 'last', :identifier => 'p2008040')
518 assert_equal 'p2008041', Project.next_identifier
521 def test_next_identifier_first_project
523 assert_nil Project.next_identifier
527 def test_enabled_module_names_should_not_recreate_enabled_modules
528 project = Project.find(1)
530 modules = project.enabled_modules.slice(0..-2)
532 assert_difference 'EnabledModule.count', -1 do
533 project.enabled_module_names = modules.collect(&:name)
536 # Ids should be preserved
537 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
540 def test_copy_from_existing_project
541 source_project = Project.find(1)
542 copied_project = Project.copy_from(1)
544 assert copied_project
546 assert copied_project.id.blank?
547 assert copied_project.name.blank?
548 assert copied_project.identifier.blank?
550 # Duplicated attributes
551 assert_equal source_project.description, copied_project.description
552 assert_equal source_project.enabled_modules, copied_project.enabled_modules
553 assert_equal source_project.trackers, copied_project.trackers
556 assert_equal 1, copied_project.status
559 def test_activities_should_use_the_system_activities
560 project = Project.find(1)
561 assert_equal project.activities, TimeEntryActivity.find(:all, :conditions => {:active => true} )
565 def test_activities_should_use_the_project_specific_activities
566 project = Project.find(1)
567 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project})
568 assert overridden_activity.save!
570 assert project.activities.include?(overridden_activity), "Project specific Activity not found"
573 def test_activities_should_not_include_the_inactive_project_specific_activities
574 project = Project.find(1)
575 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
576 assert overridden_activity.save!
578 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found"
581 def test_activities_should_not_include_project_specific_activities_from_other_projects
582 project = Project.find(1)
583 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)})
584 assert overridden_activity.save!
586 assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project"
589 def test_activities_should_handle_nils
590 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(1), :parent => TimeEntryActivity.find(:first)})
591 TimeEntryActivity.delete_all
594 project = Project.find(1)
595 assert project.activities.empty?
597 # No system, one overridden
598 assert overridden_activity.save!
600 assert_equal [overridden_activity], project.activities
603 def test_activities_should_override_system_activities_with_project_activities
604 project = Project.find(1)
605 parent_activity = TimeEntryActivity.find(:first)
606 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity})
607 assert overridden_activity.save!
609 assert project.activities.include?(overridden_activity), "Project specific Activity not found"
610 assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden"
613 def test_activities_should_include_inactive_activities_if_specified
614 project = Project.find(1)
615 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
616 assert overridden_activity.save!
618 assert project.activities(true).include?(overridden_activity), "Inactive Project specific Activity not found"
621 test 'activities should not include active System activities if the project has an override that is inactive' do
622 project = Project.find(1)
623 system_activity = TimeEntryActivity.find_by_name('Design')
624 assert system_activity.active?
625 overridden_activity = TimeEntryActivity.generate!(:project => project, :parent => system_activity, :active => false)
626 assert overridden_activity.save!
628 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found"
629 assert !project.activities.include?(system_activity), "System activity found when the project has an inactive override"
632 def test_close_completed_versions
633 Version.update_all("status = 'open'")
634 project = Project.find(1)
635 assert_not_nil project.versions.detect {|v| v.completed? && v.status == 'open'}
636 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
637 project.close_completed_versions
639 assert_nil project.versions.detect {|v| v.completed? && v.status != 'closed'}
640 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
643 context "Project#copy" do
645 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
646 Project.destroy_all :identifier => "copy-test"
647 @source_project = Project.find(2)
648 @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
649 @project.trackers = @source_project.trackers
650 @project.enabled_module_names = @source_project.enabled_modules.collect(&:name)
653 should "copy issues" do
654 @source_project.issues << Issue.generate!(:status => IssueStatus.find_by_name('Closed'),
655 :subject => "copy issue status",
657 :assigned_to_id => 2,
658 :project_id => @source_project.id)
659 assert @project.valid?
660 assert @project.issues.empty?
661 assert @project.copy(@source_project)
663 assert_equal @source_project.issues.size, @project.issues.size
664 @project.issues.each do |issue|
666 assert ! issue.assigned_to.blank?
667 assert_equal @project, issue.project
670 copied_issue = @project.issues.first(:conditions => {:subject => "copy issue status"})
672 assert copied_issue.status
673 assert_equal "Closed", copied_issue.status.name
676 should "change the new issues to use the copied version" do
677 User.current = User.find(1)
678 assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open')
679 @source_project.versions << assigned_version
680 assert_equal 3, @source_project.versions.size
681 Issue.generate_for_project!(@source_project,
682 :fixed_version_id => assigned_version.id,
683 :subject => "change the new issues to use the copied version",
685 :project_id => @source_project.id)
687 assert @project.copy(@source_project)
689 copied_issue = @project.issues.first(:conditions => {:subject => "change the new issues to use the copied version"})
692 assert copied_issue.fixed_version
693 assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
694 assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record
697 should "copy issue relations" do
698 Setting.cross_project_issue_relations = '1'
700 second_issue = Issue.generate!(:status_id => 5,
701 :subject => "copy issue relation",
703 :assigned_to_id => 2,
704 :project_id => @source_project.id)
705 source_relation = IssueRelation.generate!(:issue_from => Issue.find(4),
706 :issue_to => second_issue,
707 :relation_type => "relates")
708 source_relation_cross_project = IssueRelation.generate!(:issue_from => Issue.find(1),
709 :issue_to => second_issue,
710 :relation_type => "duplicates")
712 assert @project.copy(@source_project)
713 assert_equal @source_project.issues.count, @project.issues.count
714 copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4
715 copied_second_issue = @project.issues.find_by_subject("copy issue relation")
717 # First issue with a relation on project
718 assert_equal 1, copied_issue.relations.size, "Relation not copied"
719 copied_relation = copied_issue.relations.first
720 assert_equal "relates", copied_relation.relation_type
721 assert_equal copied_second_issue.id, copied_relation.issue_to_id
722 assert_not_equal source_relation.id, copied_relation.id
724 # Second issue with a cross project relation
725 assert_equal 2, copied_second_issue.relations.size, "Relation not copied"
726 copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first
727 assert_equal "duplicates", copied_relation.relation_type
728 assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept"
729 assert_not_equal source_relation_cross_project.id, copied_relation.id
732 should "copy memberships" do
733 assert @project.valid?
734 assert @project.members.empty?
735 assert @project.copy(@source_project)
737 assert_equal @source_project.memberships.size, @project.memberships.size
738 @project.memberships.each do |membership|
740 assert_equal @project, membership.project
744 should "copy project specific queries" do
745 assert @project.valid?
746 assert @project.queries.empty?
747 assert @project.copy(@source_project)
749 assert_equal @source_project.queries.size, @project.queries.size
750 @project.queries.each do |query|
752 assert_equal @project, query.project
756 should "copy versions" do
757 @source_project.versions << Version.generate!
758 @source_project.versions << Version.generate!
760 assert @project.versions.empty?
761 assert @project.copy(@source_project)
763 assert_equal @source_project.versions.size, @project.versions.size
764 @project.versions.each do |version|
766 assert_equal @project, version.project
770 should "copy wiki" do
771 assert_difference 'Wiki.count' do
772 assert @project.copy(@source_project)
776 assert_not_equal @source_project.wiki, @project.wiki
777 assert_equal "Start page", @project.wiki.start_page
780 should "copy wiki pages and content with hierarchy" do
781 assert_difference 'WikiPage.count', @source_project.wiki.pages.size do
782 assert @project.copy(@source_project)
786 assert_equal @source_project.wiki.pages.size, @project.wiki.pages.size
788 @project.wiki.pages.each do |wiki_page|
789 assert wiki_page.content
790 assert !@source_project.wiki.pages.include?(wiki_page)
793 parent = @project.wiki.find_page('Parent_page')
794 child1 = @project.wiki.find_page('Child_page_1')
795 child2 = @project.wiki.find_page('Child_page_2')
796 assert_equal parent, child1.parent
797 assert_equal parent, child2.parent
800 should "copy issue categories" do
801 assert @project.copy(@source_project)
803 assert_equal 2, @project.issue_categories.size
804 @project.issue_categories.each do |issue_category|
805 assert !@source_project.issue_categories.include?(issue_category)
809 should "copy boards" do
810 assert @project.copy(@source_project)
812 assert_equal 1, @project.boards.size
813 @project.boards.each do |board|
814 assert !@source_project.boards.include?(board)
818 should "change the new issues to use the copied issue categories" do
819 issue = Issue.find(4)
820 issue.update_attribute(:category_id, 3)
822 assert @project.copy(@source_project)
824 @project.issues.each do |issue|
825 assert issue.category
826 assert_equal "Stock management", issue.category.name # Same name
827 assert_not_equal IssueCategory.find(3), issue.category # Different record
831 should "limit copy with :only option" do
832 assert @project.members.empty?
833 assert @project.issue_categories.empty?
834 assert @source_project.issues.any?
836 assert @project.copy(@source_project, :only => ['members', 'issue_categories'])
838 assert @project.members.any?
839 assert @project.issue_categories.any?
840 assert @project.issues.empty?
845 context "#start_date" do
847 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
848 @project = Project.generate!(:identifier => 'test0')
849 @project.trackers << Tracker.generate!
852 should "be nil if there are no issues on the project" do
853 assert_nil @project.start_date
856 should "be nil if issue tracking is disabled" do
857 Issue.generate_for_project!(@project, :start_date => Date.today)
858 @project.enabled_modules.find_all_by_name('issue_tracking').each {|m| m.destroy}
861 assert_nil @project.start_date
864 should "be tested when issues have no start date"
866 should "be the earliest start date of it's issues" do
867 early = 7.days.ago.to_date
868 Issue.generate_for_project!(@project, :start_date => Date.today)
869 Issue.generate_for_project!(@project, :start_date => early)
871 assert_equal early, @project.start_date
876 context "#due_date" do
878 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
879 @project = Project.generate!(:identifier => 'test0')
880 @project.trackers << Tracker.generate!
883 should "be nil if there are no issues on the project" do
884 assert_nil @project.due_date
887 should "be nil if issue tracking is disabled" do
888 Issue.generate_for_project!(@project, :due_date => Date.today)
889 @project.enabled_modules.find_all_by_name('issue_tracking').each {|m| m.destroy}
892 assert_nil @project.due_date
895 should "be tested when issues have no due date"
897 should "be the latest due date of it's issues" do
898 future = 7.days.from_now.to_date
899 Issue.generate_for_project!(@project, :due_date => future)
900 Issue.generate_for_project!(@project, :due_date => Date.today)
902 assert_equal future, @project.due_date
905 should "be the latest due date of it's versions" do
906 future = 7.days.from_now.to_date
907 @project.versions << Version.generate!(:effective_date => future)
908 @project.versions << Version.generate!(:effective_date => Date.today)
911 assert_equal future, @project.due_date
915 should "pick the latest date from it's issues and versions" do
916 future = 7.days.from_now.to_date
917 far_future = 14.days.from_now.to_date
918 Issue.generate_for_project!(@project, :due_date => far_future)
919 @project.versions << Version.generate!(:effective_date => future)
921 assert_equal far_future, @project.due_date
926 context "Project#completed_percent" do
928 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
929 @project = Project.generate!(:identifier => 'test0')
930 @project.trackers << Tracker.generate!
933 context "no versions" do
935 assert_equal 100, @project.completed_percent
939 context "with versions" do
940 should "return 0 if the versions have no issues" do
941 Version.generate!(:project => @project)
942 Version.generate!(:project => @project)
944 assert_equal 0, @project.completed_percent
947 should "return 100 if the version has only closed issues" do
948 v1 = Version.generate!(:project => @project)
949 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v1)
950 v2 = Version.generate!(:project => @project)
951 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v2)
953 assert_equal 100, @project.completed_percent
956 should "return the averaged completed percent of the versions (not weighted)" do
957 v1 = Version.generate!(:project => @project)
958 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v1)
959 v2 = Version.generate!(:project => @project)
960 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v2)
962 assert_equal 50, @project.completed_percent
968 context "#notified_users" do
970 @project = Project.generate!
971 @role = Role.generate!
973 @user_with_membership_notification = User.generate!(:mail_notification => 'selected')
974 Member.generate!(:project => @project, :roles => [@role], :principal => @user_with_membership_notification, :mail_notification => true)
976 @all_events_user = User.generate!(:mail_notification => 'all')
977 Member.generate!(:project => @project, :roles => [@role], :principal => @all_events_user)
979 @no_events_user = User.generate!(:mail_notification => 'none')
980 Member.generate!(:project => @project, :roles => [@role], :principal => @no_events_user)
982 @only_my_events_user = User.generate!(:mail_notification => 'only_my_events')
983 Member.generate!(:project => @project, :roles => [@role], :principal => @only_my_events_user)
985 @only_assigned_user = User.generate!(:mail_notification => 'only_assigned')
986 Member.generate!(:project => @project, :roles => [@role], :principal => @only_assigned_user)
988 @only_owned_user = User.generate!(:mail_notification => 'only_owner')
989 Member.generate!(:project => @project, :roles => [@role], :principal => @only_owned_user)
992 should "include members with a mail notification" do
993 assert @project.notified_users.include?(@user_with_membership_notification)
996 should "include users with the 'all' notification option" do
997 assert @project.notified_users.include?(@all_events_user)
1000 should "not include users with the 'none' notification option" do
1001 assert !@project.notified_users.include?(@no_events_user)
1004 should "not include users with the 'only_my_events' notification option" do
1005 assert !@project.notified_users.include?(@only_my_events_user)
1008 should "not include users with the 'only_assigned' notification option" do
1009 assert !@project.notified_users.include?(@only_assigned_user)
1012 should "not include users with the 'only_owner' notification option" do
1013 assert !@project.notified_users.include?(@only_owned_user)