annotate test/unit/project_test.rb @ 36:de76cd3e8c8e cc-branches

* Probably abortive experiments in extracting the branch from Hg
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 20 Oct 2010 10:07:29 +0100
parents 40f7cfd4df19
children 94944d00e43c
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require File.dirname(__FILE__) + '/../test_helper'
Chris@0 19
Chris@0 20 class ProjectTest < ActiveSupport::TestCase
Chris@0 21 fixtures :all
Chris@0 22
Chris@0 23 def setup
Chris@0 24 @ecookbook = Project.find(1)
Chris@0 25 @ecookbook_sub1 = Project.find(3)
Chris@0 26 User.current = nil
Chris@0 27 end
Chris@0 28
Chris@0 29 should_validate_presence_of :name
Chris@0 30 should_validate_presence_of :identifier
Chris@0 31
Chris@0 32 should_validate_uniqueness_of :name
Chris@0 33 should_validate_uniqueness_of :identifier
Chris@0 34
Chris@0 35 context "associations" do
Chris@0 36 should_have_many :members
Chris@0 37 should_have_many :users, :through => :members
Chris@0 38 should_have_many :member_principals
Chris@0 39 should_have_many :principals, :through => :member_principals
Chris@0 40 should_have_many :enabled_modules
Chris@0 41 should_have_many :issues
Chris@0 42 should_have_many :issue_changes, :through => :issues
Chris@0 43 should_have_many :versions
Chris@0 44 should_have_many :time_entries
Chris@0 45 should_have_many :queries
Chris@0 46 should_have_many :documents
Chris@0 47 should_have_many :news
Chris@0 48 should_have_many :issue_categories
Chris@0 49 should_have_many :boards
Chris@0 50 should_have_many :changesets, :through => :repository
Chris@0 51
Chris@0 52 should_have_one :repository
Chris@0 53 should_have_one :wiki
Chris@0 54
Chris@0 55 should_have_and_belong_to_many :trackers
Chris@0 56 should_have_and_belong_to_many :issue_custom_fields
Chris@0 57 end
Chris@0 58
Chris@0 59 def test_truth
Chris@0 60 assert_kind_of Project, @ecookbook
Chris@0 61 assert_equal "eCookbook", @ecookbook.name
Chris@0 62 end
Chris@0 63
Chris@0 64 def test_update
Chris@0 65 assert_equal "eCookbook", @ecookbook.name
Chris@0 66 @ecookbook.name = "eCook"
Chris@0 67 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
Chris@0 68 @ecookbook.reload
Chris@0 69 assert_equal "eCook", @ecookbook.name
Chris@0 70 end
Chris@0 71
Chris@0 72 def test_validate_identifier
Chris@0 73 to_test = {"abc" => true,
Chris@0 74 "ab12" => true,
Chris@0 75 "ab-12" => true,
Chris@0 76 "12" => false,
Chris@0 77 "new" => false}
Chris@0 78
Chris@0 79 to_test.each do |identifier, valid|
Chris@0 80 p = Project.new
Chris@0 81 p.identifier = identifier
Chris@0 82 p.valid?
Chris@0 83 assert_equal valid, p.errors.on('identifier').nil?
Chris@0 84 end
Chris@0 85 end
Chris@0 86
Chris@0 87 def test_members_should_be_active_users
Chris@0 88 Project.all.each do |project|
Chris@0 89 assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) }
Chris@0 90 end
Chris@0 91 end
Chris@0 92
Chris@0 93 def test_users_should_be_active_users
Chris@0 94 Project.all.each do |project|
Chris@0 95 assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) }
Chris@0 96 end
Chris@0 97 end
Chris@0 98
Chris@0 99 def test_archive
Chris@0 100 user = @ecookbook.members.first.user
Chris@0 101 @ecookbook.archive
Chris@0 102 @ecookbook.reload
Chris@0 103
Chris@0 104 assert !@ecookbook.active?
Chris@0 105 assert !user.projects.include?(@ecookbook)
Chris@0 106 # Subproject are also archived
Chris@0 107 assert !@ecookbook.children.empty?
Chris@0 108 assert @ecookbook.descendants.active.empty?
Chris@0 109 end
Chris@0 110
Chris@0 111 def test_archive_should_fail_if_versions_are_used_by_non_descendant_projects
Chris@0 112 # Assign an issue of a project to a version of a child project
Chris@0 113 Issue.find(4).update_attribute :fixed_version_id, 4
Chris@0 114
Chris@0 115 assert_no_difference "Project.count(:all, :conditions => 'status = #{Project::STATUS_ARCHIVED}')" do
Chris@0 116 assert_equal false, @ecookbook.archive
Chris@0 117 end
Chris@0 118 @ecookbook.reload
Chris@0 119 assert @ecookbook.active?
Chris@0 120 end
Chris@0 121
Chris@0 122 def test_unarchive
Chris@0 123 user = @ecookbook.members.first.user
Chris@0 124 @ecookbook.archive
Chris@0 125 # A subproject of an archived project can not be unarchived
Chris@0 126 assert !@ecookbook_sub1.unarchive
Chris@0 127
Chris@0 128 # Unarchive project
Chris@0 129 assert @ecookbook.unarchive
Chris@0 130 @ecookbook.reload
Chris@0 131 assert @ecookbook.active?
Chris@0 132 assert user.projects.include?(@ecookbook)
Chris@0 133 # Subproject can now be unarchived
Chris@0 134 @ecookbook_sub1.reload
Chris@0 135 assert @ecookbook_sub1.unarchive
Chris@0 136 end
Chris@0 137
Chris@0 138 def test_destroy
Chris@0 139 # 2 active members
Chris@0 140 assert_equal 2, @ecookbook.members.size
Chris@0 141 # and 1 is locked
Chris@0 142 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
Chris@0 143 # some boards
Chris@0 144 assert @ecookbook.boards.any?
Chris@0 145
Chris@0 146 @ecookbook.destroy
Chris@0 147 # make sure that the project non longer exists
Chris@0 148 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
Chris@0 149 # make sure related data was removed
Chris@0 150 assert_nil Member.first(:conditions => {:project_id => @ecookbook.id})
Chris@0 151 assert_nil Board.first(:conditions => {:project_id => @ecookbook.id})
Chris@0 152 assert_nil Issue.first(:conditions => {:project_id => @ecookbook.id})
Chris@0 153 end
Chris@0 154
Chris@0 155 def test_move_an_orphan_project_to_a_root_project
Chris@0 156 sub = Project.find(2)
Chris@0 157 sub.set_parent! @ecookbook
Chris@0 158 assert_equal @ecookbook.id, sub.parent.id
Chris@0 159 @ecookbook.reload
Chris@0 160 assert_equal 4, @ecookbook.children.size
Chris@0 161 end
Chris@0 162
Chris@0 163 def test_move_an_orphan_project_to_a_subproject
Chris@0 164 sub = Project.find(2)
Chris@0 165 assert sub.set_parent!(@ecookbook_sub1)
Chris@0 166 end
Chris@0 167
Chris@0 168 def test_move_a_root_project_to_a_project
Chris@0 169 sub = @ecookbook
Chris@0 170 assert sub.set_parent!(Project.find(2))
Chris@0 171 end
Chris@0 172
Chris@0 173 def test_should_not_move_a_project_to_its_children
Chris@0 174 sub = @ecookbook
Chris@0 175 assert !(sub.set_parent!(Project.find(3)))
Chris@0 176 end
Chris@0 177
Chris@0 178 def test_set_parent_should_add_roots_in_alphabetical_order
Chris@0 179 ProjectCustomField.delete_all
Chris@0 180 Project.delete_all
Chris@0 181 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
Chris@0 182 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
Chris@0 183 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
Chris@0 184 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
Chris@0 185
Chris@0 186 assert_equal 4, Project.count
Chris@0 187 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
Chris@0 188 end
Chris@0 189
Chris@0 190 def test_set_parent_should_add_children_in_alphabetical_order
Chris@0 191 ProjectCustomField.delete_all
Chris@0 192 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
Chris@0 193 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
Chris@0 194 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
Chris@0 195 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
Chris@0 196 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
Chris@0 197
Chris@0 198 parent.reload
Chris@0 199 assert_equal 4, parent.children.size
Chris@0 200 assert_equal parent.children.sort_by(&:name), parent.children
Chris@0 201 end
Chris@0 202
Chris@0 203 def test_rebuild_should_sort_children_alphabetically
Chris@0 204 ProjectCustomField.delete_all
Chris@0 205 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
Chris@0 206 Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
Chris@0 207 Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
Chris@0 208 Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
Chris@0 209 Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
Chris@0 210
Chris@0 211 Project.update_all("lft = NULL, rgt = NULL")
Chris@0 212 Project.rebuild!
Chris@0 213
Chris@0 214 parent.reload
Chris@0 215 assert_equal 4, parent.children.size
Chris@0 216 assert_equal parent.children.sort_by(&:name), parent.children
Chris@0 217 end
Chris@0 218
Chris@0 219
Chris@0 220 def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy
Chris@0 221 # Parent issue with a hierarchy project's fixed version
Chris@0 222 parent_issue = Issue.find(1)
Chris@0 223 parent_issue.update_attribute(:fixed_version_id, 4)
Chris@0 224 parent_issue.reload
Chris@0 225 assert_equal 4, parent_issue.fixed_version_id
Chris@0 226
Chris@0 227 # Should keep fixed versions for the issues
Chris@0 228 issue_with_local_fixed_version = Issue.find(5)
Chris@0 229 issue_with_local_fixed_version.update_attribute(:fixed_version_id, 4)
Chris@0 230 issue_with_local_fixed_version.reload
Chris@0 231 assert_equal 4, issue_with_local_fixed_version.fixed_version_id
Chris@0 232
Chris@0 233 # Local issue with hierarchy fixed_version
Chris@0 234 issue_with_hierarchy_fixed_version = Issue.find(13)
Chris@0 235 issue_with_hierarchy_fixed_version.update_attribute(:fixed_version_id, 6)
Chris@0 236 issue_with_hierarchy_fixed_version.reload
Chris@0 237 assert_equal 6, issue_with_hierarchy_fixed_version.fixed_version_id
Chris@0 238
Chris@0 239 # Move project out of the issue's hierarchy
Chris@0 240 moved_project = Project.find(3)
Chris@0 241 moved_project.set_parent!(Project.find(2))
Chris@0 242 parent_issue.reload
Chris@0 243 issue_with_local_fixed_version.reload
Chris@0 244 issue_with_hierarchy_fixed_version.reload
Chris@0 245
Chris@0 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"
Chris@0 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"
Chris@0 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."
Chris@0 249 end
Chris@0 250
Chris@0 251 def test_parent
Chris@0 252 p = Project.find(6).parent
Chris@0 253 assert p.is_a?(Project)
Chris@0 254 assert_equal 5, p.id
Chris@0 255 end
Chris@0 256
Chris@0 257 def test_ancestors
Chris@0 258 a = Project.find(6).ancestors
Chris@0 259 assert a.first.is_a?(Project)
Chris@0 260 assert_equal [1, 5], a.collect(&:id)
Chris@0 261 end
Chris@0 262
Chris@0 263 def test_root
Chris@0 264 r = Project.find(6).root
Chris@0 265 assert r.is_a?(Project)
Chris@0 266 assert_equal 1, r.id
Chris@0 267 end
Chris@0 268
Chris@0 269 def test_children
Chris@0 270 c = Project.find(1).children
Chris@0 271 assert c.first.is_a?(Project)
Chris@0 272 assert_equal [5, 3, 4], c.collect(&:id)
Chris@0 273 end
Chris@0 274
Chris@0 275 def test_descendants
Chris@0 276 d = Project.find(1).descendants
Chris@0 277 assert d.first.is_a?(Project)
Chris@0 278 assert_equal [5, 6, 3, 4], d.collect(&:id)
Chris@0 279 end
Chris@0 280
Chris@0 281 def test_allowed_parents_should_be_empty_for_non_member_user
Chris@0 282 Role.non_member.add_permission!(:add_project)
Chris@0 283 user = User.find(9)
Chris@0 284 assert user.memberships.empty?
Chris@0 285 User.current = user
Chris@0 286 assert Project.new.allowed_parents.compact.empty?
Chris@0 287 end
Chris@0 288
Chris@0 289 def test_allowed_parents_with_add_subprojects_permission
Chris@0 290 Role.find(1).remove_permission!(:add_project)
Chris@0 291 Role.find(1).add_permission!(:add_subprojects)
Chris@0 292 User.current = User.find(2)
Chris@0 293 # new project
Chris@0 294 assert !Project.new.allowed_parents.include?(nil)
Chris@0 295 assert Project.new.allowed_parents.include?(Project.find(1))
Chris@0 296 # existing root project
Chris@0 297 assert Project.find(1).allowed_parents.include?(nil)
Chris@0 298 # existing child
Chris@0 299 assert Project.find(3).allowed_parents.include?(Project.find(1))
Chris@0 300 assert !Project.find(3).allowed_parents.include?(nil)
Chris@0 301 end
Chris@0 302
Chris@0 303 def test_allowed_parents_with_add_project_permission
Chris@0 304 Role.find(1).add_permission!(:add_project)
Chris@0 305 Role.find(1).remove_permission!(:add_subprojects)
Chris@0 306 User.current = User.find(2)
Chris@0 307 # new project
Chris@0 308 assert Project.new.allowed_parents.include?(nil)
Chris@0 309 assert !Project.new.allowed_parents.include?(Project.find(1))
Chris@0 310 # existing root project
Chris@0 311 assert Project.find(1).allowed_parents.include?(nil)
Chris@0 312 # existing child
Chris@0 313 assert Project.find(3).allowed_parents.include?(Project.find(1))
Chris@0 314 assert Project.find(3).allowed_parents.include?(nil)
Chris@0 315 end
Chris@0 316
Chris@0 317 def test_allowed_parents_with_add_project_and_subprojects_permission
Chris@0 318 Role.find(1).add_permission!(:add_project)
Chris@0 319 Role.find(1).add_permission!(:add_subprojects)
Chris@0 320 User.current = User.find(2)
Chris@0 321 # new project
Chris@0 322 assert Project.new.allowed_parents.include?(nil)
Chris@0 323 assert Project.new.allowed_parents.include?(Project.find(1))
Chris@0 324 # existing root project
Chris@0 325 assert Project.find(1).allowed_parents.include?(nil)
Chris@0 326 # existing child
Chris@0 327 assert Project.find(3).allowed_parents.include?(Project.find(1))
Chris@0 328 assert Project.find(3).allowed_parents.include?(nil)
Chris@0 329 end
Chris@0 330
Chris@0 331 def test_users_by_role
Chris@0 332 users_by_role = Project.find(1).users_by_role
Chris@0 333 assert_kind_of Hash, users_by_role
Chris@0 334 role = Role.find(1)
Chris@0 335 assert_kind_of Array, users_by_role[role]
Chris@0 336 assert users_by_role[role].include?(User.find(2))
Chris@0 337 end
Chris@0 338
Chris@0 339 def test_rolled_up_trackers
Chris@0 340 parent = Project.find(1)
Chris@0 341 parent.trackers = Tracker.find([1,2])
Chris@0 342 child = parent.children.find(3)
Chris@0 343
Chris@0 344 assert_equal [1, 2], parent.tracker_ids
Chris@0 345 assert_equal [2, 3], child.trackers.collect(&:id)
Chris@0 346
Chris@0 347 assert_kind_of Tracker, parent.rolled_up_trackers.first
Chris@0 348 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
Chris@0 349
Chris@0 350 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
Chris@0 351 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
Chris@0 352 end
Chris@0 353
Chris@0 354 def test_rolled_up_trackers_should_ignore_archived_subprojects
Chris@0 355 parent = Project.find(1)
Chris@0 356 parent.trackers = Tracker.find([1,2])
Chris@0 357 child = parent.children.find(3)
Chris@0 358 child.trackers = Tracker.find([1,3])
Chris@0 359 parent.children.each(&:archive)
Chris@0 360
Chris@0 361 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
Chris@0 362 end
Chris@0 363
Chris@0 364 context "#rolled_up_versions" do
Chris@0 365 setup do
Chris@0 366 @project = Project.generate!
Chris@0 367 @parent_version_1 = Version.generate!(:project => @project)
Chris@0 368 @parent_version_2 = Version.generate!(:project => @project)
Chris@0 369 end
Chris@0 370
Chris@0 371 should "include the versions for the current project" do
Chris@0 372 assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
Chris@0 373 end
Chris@0 374
Chris@0 375 should "include versions for a subproject" do
Chris@0 376 @subproject = Project.generate!
Chris@0 377 @subproject.set_parent!(@project)
Chris@0 378 @subproject_version = Version.generate!(:project => @subproject)
Chris@0 379
Chris@0 380 assert_same_elements [
Chris@0 381 @parent_version_1,
Chris@0 382 @parent_version_2,
Chris@0 383 @subproject_version
Chris@0 384 ], @project.rolled_up_versions
Chris@0 385 end
Chris@0 386
Chris@0 387 should "include versions for a sub-subproject" do
Chris@0 388 @subproject = Project.generate!
Chris@0 389 @subproject.set_parent!(@project)
Chris@0 390 @sub_subproject = Project.generate!
Chris@0 391 @sub_subproject.set_parent!(@subproject)
Chris@0 392 @sub_subproject_version = Version.generate!(:project => @sub_subproject)
Chris@0 393
Chris@0 394 @project.reload
Chris@0 395
Chris@0 396 assert_same_elements [
Chris@0 397 @parent_version_1,
Chris@0 398 @parent_version_2,
Chris@0 399 @sub_subproject_version
Chris@0 400 ], @project.rolled_up_versions
Chris@0 401 end
Chris@0 402
Chris@0 403
Chris@0 404 should "only check active projects" do
Chris@0 405 @subproject = Project.generate!
Chris@0 406 @subproject.set_parent!(@project)
Chris@0 407 @subproject_version = Version.generate!(:project => @subproject)
Chris@0 408 assert @subproject.archive
Chris@0 409
Chris@0 410 @project.reload
Chris@0 411
Chris@0 412 assert !@subproject.active?
Chris@0 413 assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
Chris@0 414 end
Chris@0 415 end
Chris@0 416
Chris@0 417 def test_shared_versions_none_sharing
Chris@0 418 p = Project.find(5)
Chris@0 419 v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none')
Chris@0 420 assert p.shared_versions.include?(v)
Chris@0 421 assert !p.children.first.shared_versions.include?(v)
Chris@0 422 assert !p.root.shared_versions.include?(v)
Chris@0 423 assert !p.siblings.first.shared_versions.include?(v)
Chris@0 424 assert !p.root.siblings.first.shared_versions.include?(v)
Chris@0 425 end
Chris@0 426
Chris@0 427 def test_shared_versions_descendants_sharing
Chris@0 428 p = Project.find(5)
Chris@0 429 v = Version.create!(:name => 'descendants_sharing', :project => p, :sharing => 'descendants')
Chris@0 430 assert p.shared_versions.include?(v)
Chris@0 431 assert p.children.first.shared_versions.include?(v)
Chris@0 432 assert !p.root.shared_versions.include?(v)
Chris@0 433 assert !p.siblings.first.shared_versions.include?(v)
Chris@0 434 assert !p.root.siblings.first.shared_versions.include?(v)
Chris@0 435 end
Chris@0 436
Chris@0 437 def test_shared_versions_hierarchy_sharing
Chris@0 438 p = Project.find(5)
Chris@0 439 v = Version.create!(:name => 'hierarchy_sharing', :project => p, :sharing => 'hierarchy')
Chris@0 440 assert p.shared_versions.include?(v)
Chris@0 441 assert p.children.first.shared_versions.include?(v)
Chris@0 442 assert p.root.shared_versions.include?(v)
Chris@0 443 assert !p.siblings.first.shared_versions.include?(v)
Chris@0 444 assert !p.root.siblings.first.shared_versions.include?(v)
Chris@0 445 end
Chris@0 446
Chris@0 447 def test_shared_versions_tree_sharing
Chris@0 448 p = Project.find(5)
Chris@0 449 v = Version.create!(:name => 'tree_sharing', :project => p, :sharing => 'tree')
Chris@0 450 assert p.shared_versions.include?(v)
Chris@0 451 assert p.children.first.shared_versions.include?(v)
Chris@0 452 assert p.root.shared_versions.include?(v)
Chris@0 453 assert p.siblings.first.shared_versions.include?(v)
Chris@0 454 assert !p.root.siblings.first.shared_versions.include?(v)
Chris@0 455 end
Chris@0 456
Chris@0 457 def test_shared_versions_system_sharing
Chris@0 458 p = Project.find(5)
Chris@0 459 v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system')
Chris@0 460 assert p.shared_versions.include?(v)
Chris@0 461 assert p.children.first.shared_versions.include?(v)
Chris@0 462 assert p.root.shared_versions.include?(v)
Chris@0 463 assert p.siblings.first.shared_versions.include?(v)
Chris@0 464 assert p.root.siblings.first.shared_versions.include?(v)
Chris@0 465 end
Chris@0 466
Chris@0 467 def test_shared_versions
Chris@0 468 parent = Project.find(1)
Chris@0 469 child = parent.children.find(3)
Chris@0 470 private_child = parent.children.find(5)
Chris@0 471
Chris@0 472 assert_equal [1,2,3], parent.version_ids.sort
Chris@0 473 assert_equal [4], child.version_ids
Chris@0 474 assert_equal [6], private_child.version_ids
Chris@0 475 assert_equal [7], Version.find_all_by_sharing('system').collect(&:id)
Chris@0 476
Chris@0 477 assert_equal 6, parent.shared_versions.size
Chris@0 478 parent.shared_versions.each do |version|
Chris@0 479 assert_kind_of Version, version
Chris@0 480 end
Chris@0 481
Chris@0 482 assert_equal [1,2,3,4,6,7], parent.shared_versions.collect(&:id).sort
Chris@0 483 end
Chris@0 484
Chris@0 485 def test_shared_versions_should_ignore_archived_subprojects
Chris@0 486 parent = Project.find(1)
Chris@0 487 child = parent.children.find(3)
Chris@0 488 child.archive
Chris@0 489 parent.reload
Chris@0 490
Chris@0 491 assert_equal [1,2,3], parent.version_ids.sort
Chris@0 492 assert_equal [4], child.version_ids
Chris@0 493 assert !parent.shared_versions.collect(&:id).include?(4)
Chris@0 494 end
Chris@0 495
Chris@0 496 def test_shared_versions_visible_to_user
Chris@0 497 user = User.find(3)
Chris@0 498 parent = Project.find(1)
Chris@0 499 child = parent.children.find(5)
Chris@0 500
Chris@0 501 assert_equal [1,2,3], parent.version_ids.sort
Chris@0 502 assert_equal [6], child.version_ids
Chris@0 503
Chris@0 504 versions = parent.shared_versions.visible(user)
Chris@0 505
Chris@0 506 assert_equal 4, versions.size
Chris@0 507 versions.each do |version|
Chris@0 508 assert_kind_of Version, version
Chris@0 509 end
Chris@0 510
Chris@0 511 assert !versions.collect(&:id).include?(6)
Chris@0 512 end
Chris@0 513
Chris@0 514
Chris@0 515 def test_next_identifier
Chris@0 516 ProjectCustomField.delete_all
Chris@0 517 Project.create!(:name => 'last', :identifier => 'p2008040')
Chris@0 518 assert_equal 'p2008041', Project.next_identifier
Chris@0 519 end
Chris@0 520
Chris@0 521 def test_next_identifier_first_project
Chris@0 522 Project.delete_all
Chris@0 523 assert_nil Project.next_identifier
Chris@0 524 end
Chris@0 525
Chris@0 526
Chris@0 527 def test_enabled_module_names_should_not_recreate_enabled_modules
Chris@0 528 project = Project.find(1)
Chris@0 529 # Remove one module
Chris@0 530 modules = project.enabled_modules.slice(0..-2)
Chris@0 531 assert modules.any?
Chris@0 532 assert_difference 'EnabledModule.count', -1 do
Chris@0 533 project.enabled_module_names = modules.collect(&:name)
Chris@0 534 end
Chris@0 535 project.reload
Chris@0 536 # Ids should be preserved
Chris@0 537 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
Chris@0 538 end
Chris@0 539
Chris@0 540 def test_copy_from_existing_project
Chris@0 541 source_project = Project.find(1)
Chris@0 542 copied_project = Project.copy_from(1)
Chris@0 543
Chris@0 544 assert copied_project
Chris@0 545 # Cleared attributes
Chris@0 546 assert copied_project.id.blank?
Chris@0 547 assert copied_project.name.blank?
Chris@0 548 assert copied_project.identifier.blank?
Chris@0 549
Chris@0 550 # Duplicated attributes
Chris@0 551 assert_equal source_project.description, copied_project.description
Chris@0 552 assert_equal source_project.enabled_modules, copied_project.enabled_modules
Chris@0 553 assert_equal source_project.trackers, copied_project.trackers
Chris@0 554
Chris@0 555 # Default attributes
Chris@0 556 assert_equal 1, copied_project.status
Chris@0 557 end
Chris@0 558
Chris@0 559 def test_activities_should_use_the_system_activities
Chris@0 560 project = Project.find(1)
Chris@0 561 assert_equal project.activities, TimeEntryActivity.find(:all, :conditions => {:active => true} )
Chris@0 562 end
Chris@0 563
Chris@0 564
Chris@0 565 def test_activities_should_use_the_project_specific_activities
Chris@0 566 project = Project.find(1)
Chris@0 567 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project})
Chris@0 568 assert overridden_activity.save!
Chris@0 569
Chris@0 570 assert project.activities.include?(overridden_activity), "Project specific Activity not found"
Chris@0 571 end
Chris@0 572
Chris@0 573 def test_activities_should_not_include_the_inactive_project_specific_activities
Chris@0 574 project = Project.find(1)
Chris@0 575 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
Chris@0 576 assert overridden_activity.save!
Chris@0 577
Chris@0 578 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found"
Chris@0 579 end
Chris@0 580
Chris@0 581 def test_activities_should_not_include_project_specific_activities_from_other_projects
Chris@0 582 project = Project.find(1)
Chris@0 583 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)})
Chris@0 584 assert overridden_activity.save!
Chris@0 585
Chris@0 586 assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project"
Chris@0 587 end
Chris@0 588
Chris@0 589 def test_activities_should_handle_nils
Chris@0 590 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(1), :parent => TimeEntryActivity.find(:first)})
Chris@0 591 TimeEntryActivity.delete_all
Chris@0 592
Chris@0 593 # No activities
Chris@0 594 project = Project.find(1)
Chris@0 595 assert project.activities.empty?
Chris@0 596
Chris@0 597 # No system, one overridden
Chris@0 598 assert overridden_activity.save!
Chris@0 599 project.reload
Chris@0 600 assert_equal [overridden_activity], project.activities
Chris@0 601 end
Chris@0 602
Chris@0 603 def test_activities_should_override_system_activities_with_project_activities
Chris@0 604 project = Project.find(1)
Chris@0 605 parent_activity = TimeEntryActivity.find(:first)
Chris@0 606 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity})
Chris@0 607 assert overridden_activity.save!
Chris@0 608
Chris@0 609 assert project.activities.include?(overridden_activity), "Project specific Activity not found"
Chris@0 610 assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden"
Chris@0 611 end
Chris@0 612
Chris@0 613 def test_activities_should_include_inactive_activities_if_specified
Chris@0 614 project = Project.find(1)
Chris@0 615 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
Chris@0 616 assert overridden_activity.save!
Chris@0 617
Chris@0 618 assert project.activities(true).include?(overridden_activity), "Inactive Project specific Activity not found"
Chris@0 619 end
Chris@0 620
Chris@0 621 test 'activities should not include active System activities if the project has an override that is inactive' do
Chris@0 622 project = Project.find(1)
Chris@0 623 system_activity = TimeEntryActivity.find_by_name('Design')
Chris@0 624 assert system_activity.active?
Chris@0 625 overridden_activity = TimeEntryActivity.generate!(:project => project, :parent => system_activity, :active => false)
Chris@0 626 assert overridden_activity.save!
Chris@0 627
Chris@0 628 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found"
Chris@0 629 assert !project.activities.include?(system_activity), "System activity found when the project has an inactive override"
Chris@0 630 end
Chris@0 631
Chris@0 632 def test_close_completed_versions
Chris@0 633 Version.update_all("status = 'open'")
Chris@0 634 project = Project.find(1)
Chris@0 635 assert_not_nil project.versions.detect {|v| v.completed? && v.status == 'open'}
Chris@0 636 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
Chris@0 637 project.close_completed_versions
Chris@0 638 project.reload
Chris@0 639 assert_nil project.versions.detect {|v| v.completed? && v.status != 'closed'}
Chris@0 640 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
Chris@0 641 end
Chris@0 642
Chris@0 643 context "Project#copy" do
Chris@0 644 setup do
Chris@0 645 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
Chris@0 646 Project.destroy_all :identifier => "copy-test"
Chris@0 647 @source_project = Project.find(2)
Chris@0 648 @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
Chris@0 649 @project.trackers = @source_project.trackers
Chris@0 650 @project.enabled_module_names = @source_project.enabled_modules.collect(&:name)
Chris@0 651 end
Chris@0 652
Chris@0 653 should "copy issues" do
Chris@0 654 @source_project.issues << Issue.generate!(:status => IssueStatus.find_by_name('Closed'),
Chris@0 655 :subject => "copy issue status",
Chris@0 656 :tracker_id => 1,
Chris@0 657 :assigned_to_id => 2,
Chris@0 658 :project_id => @source_project.id)
Chris@0 659 assert @project.valid?
Chris@0 660 assert @project.issues.empty?
Chris@0 661 assert @project.copy(@source_project)
Chris@0 662
Chris@0 663 assert_equal @source_project.issues.size, @project.issues.size
Chris@0 664 @project.issues.each do |issue|
Chris@0 665 assert issue.valid?
Chris@0 666 assert ! issue.assigned_to.blank?
Chris@0 667 assert_equal @project, issue.project
Chris@0 668 end
Chris@0 669
Chris@0 670 copied_issue = @project.issues.first(:conditions => {:subject => "copy issue status"})
Chris@0 671 assert copied_issue
Chris@0 672 assert copied_issue.status
Chris@0 673 assert_equal "Closed", copied_issue.status.name
Chris@0 674 end
Chris@0 675
Chris@0 676 should "change the new issues to use the copied version" do
Chris@0 677 User.current = User.find(1)
Chris@0 678 assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open')
Chris@0 679 @source_project.versions << assigned_version
Chris@0 680 assert_equal 3, @source_project.versions.size
Chris@0 681 Issue.generate_for_project!(@source_project,
Chris@0 682 :fixed_version_id => assigned_version.id,
Chris@0 683 :subject => "change the new issues to use the copied version",
Chris@0 684 :tracker_id => 1,
Chris@0 685 :project_id => @source_project.id)
Chris@0 686
Chris@0 687 assert @project.copy(@source_project)
Chris@0 688 @project.reload
Chris@0 689 copied_issue = @project.issues.first(:conditions => {:subject => "change the new issues to use the copied version"})
Chris@0 690
Chris@0 691 assert copied_issue
Chris@0 692 assert copied_issue.fixed_version
Chris@0 693 assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
Chris@0 694 assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record
Chris@0 695 end
Chris@0 696
Chris@0 697 should "copy issue relations" do
Chris@0 698 Setting.cross_project_issue_relations = '1'
Chris@0 699
Chris@0 700 second_issue = Issue.generate!(:status_id => 5,
Chris@0 701 :subject => "copy issue relation",
Chris@0 702 :tracker_id => 1,
Chris@0 703 :assigned_to_id => 2,
Chris@0 704 :project_id => @source_project.id)
Chris@0 705 source_relation = IssueRelation.generate!(:issue_from => Issue.find(4),
Chris@0 706 :issue_to => second_issue,
Chris@0 707 :relation_type => "relates")
Chris@0 708 source_relation_cross_project = IssueRelation.generate!(:issue_from => Issue.find(1),
Chris@0 709 :issue_to => second_issue,
Chris@0 710 :relation_type => "duplicates")
Chris@0 711
Chris@0 712 assert @project.copy(@source_project)
Chris@0 713 assert_equal @source_project.issues.count, @project.issues.count
Chris@0 714 copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4
Chris@0 715 copied_second_issue = @project.issues.find_by_subject("copy issue relation")
Chris@0 716
Chris@0 717 # First issue with a relation on project
Chris@0 718 assert_equal 1, copied_issue.relations.size, "Relation not copied"
Chris@0 719 copied_relation = copied_issue.relations.first
Chris@0 720 assert_equal "relates", copied_relation.relation_type
Chris@0 721 assert_equal copied_second_issue.id, copied_relation.issue_to_id
Chris@0 722 assert_not_equal source_relation.id, copied_relation.id
Chris@0 723
Chris@0 724 # Second issue with a cross project relation
Chris@0 725 assert_equal 2, copied_second_issue.relations.size, "Relation not copied"
Chris@0 726 copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first
Chris@0 727 assert_equal "duplicates", copied_relation.relation_type
Chris@0 728 assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept"
Chris@0 729 assert_not_equal source_relation_cross_project.id, copied_relation.id
Chris@0 730 end
Chris@0 731
Chris@0 732 should "copy memberships" do
Chris@0 733 assert @project.valid?
Chris@0 734 assert @project.members.empty?
Chris@0 735 assert @project.copy(@source_project)
Chris@0 736
Chris@0 737 assert_equal @source_project.memberships.size, @project.memberships.size
Chris@0 738 @project.memberships.each do |membership|
Chris@0 739 assert membership
Chris@0 740 assert_equal @project, membership.project
Chris@0 741 end
Chris@0 742 end
Chris@0 743
Chris@0 744 should "copy project specific queries" do
Chris@0 745 assert @project.valid?
Chris@0 746 assert @project.queries.empty?
Chris@0 747 assert @project.copy(@source_project)
Chris@0 748
Chris@0 749 assert_equal @source_project.queries.size, @project.queries.size
Chris@0 750 @project.queries.each do |query|
Chris@0 751 assert query
Chris@0 752 assert_equal @project, query.project
Chris@0 753 end
Chris@0 754 end
Chris@0 755
Chris@0 756 should "copy versions" do
Chris@0 757 @source_project.versions << Version.generate!
Chris@0 758 @source_project.versions << Version.generate!
Chris@0 759
Chris@0 760 assert @project.versions.empty?
Chris@0 761 assert @project.copy(@source_project)
Chris@0 762
Chris@0 763 assert_equal @source_project.versions.size, @project.versions.size
Chris@0 764 @project.versions.each do |version|
Chris@0 765 assert version
Chris@0 766 assert_equal @project, version.project
Chris@0 767 end
Chris@0 768 end
Chris@0 769
Chris@0 770 should "copy wiki" do
Chris@0 771 assert_difference 'Wiki.count' do
Chris@0 772 assert @project.copy(@source_project)
Chris@0 773 end
Chris@0 774
Chris@0 775 assert @project.wiki
Chris@0 776 assert_not_equal @source_project.wiki, @project.wiki
Chris@0 777 assert_equal "Start page", @project.wiki.start_page
Chris@0 778 end
Chris@0 779
Chris@0 780 should "copy wiki pages and content with hierarchy" do
Chris@0 781 assert_difference 'WikiPage.count', @source_project.wiki.pages.size do
Chris@0 782 assert @project.copy(@source_project)
Chris@0 783 end
Chris@0 784
Chris@0 785 assert @project.wiki
Chris@0 786 assert_equal @source_project.wiki.pages.size, @project.wiki.pages.size
Chris@0 787
Chris@0 788 @project.wiki.pages.each do |wiki_page|
Chris@0 789 assert wiki_page.content
Chris@0 790 assert !@source_project.wiki.pages.include?(wiki_page)
Chris@0 791 end
Chris@0 792
Chris@0 793 parent = @project.wiki.find_page('Parent_page')
Chris@0 794 child1 = @project.wiki.find_page('Child_page_1')
Chris@0 795 child2 = @project.wiki.find_page('Child_page_2')
Chris@0 796 assert_equal parent, child1.parent
Chris@0 797 assert_equal parent, child2.parent
Chris@0 798 end
Chris@0 799
Chris@0 800 should "copy issue categories" do
Chris@0 801 assert @project.copy(@source_project)
Chris@0 802
Chris@0 803 assert_equal 2, @project.issue_categories.size
Chris@0 804 @project.issue_categories.each do |issue_category|
Chris@0 805 assert !@source_project.issue_categories.include?(issue_category)
Chris@0 806 end
Chris@0 807 end
Chris@0 808
Chris@0 809 should "copy boards" do
Chris@0 810 assert @project.copy(@source_project)
Chris@0 811
Chris@0 812 assert_equal 1, @project.boards.size
Chris@0 813 @project.boards.each do |board|
Chris@0 814 assert !@source_project.boards.include?(board)
Chris@0 815 end
Chris@0 816 end
Chris@0 817
Chris@0 818 should "change the new issues to use the copied issue categories" do
Chris@0 819 issue = Issue.find(4)
Chris@0 820 issue.update_attribute(:category_id, 3)
Chris@0 821
Chris@0 822 assert @project.copy(@source_project)
Chris@0 823
Chris@0 824 @project.issues.each do |issue|
Chris@0 825 assert issue.category
Chris@0 826 assert_equal "Stock management", issue.category.name # Same name
Chris@0 827 assert_not_equal IssueCategory.find(3), issue.category # Different record
Chris@0 828 end
Chris@0 829 end
Chris@0 830
Chris@0 831 should "limit copy with :only option" do
Chris@0 832 assert @project.members.empty?
Chris@0 833 assert @project.issue_categories.empty?
Chris@0 834 assert @source_project.issues.any?
Chris@0 835
Chris@0 836 assert @project.copy(@source_project, :only => ['members', 'issue_categories'])
Chris@0 837
Chris@0 838 assert @project.members.any?
Chris@0 839 assert @project.issue_categories.any?
Chris@0 840 assert @project.issues.empty?
Chris@0 841 end
Chris@0 842
Chris@0 843 end
Chris@0 844
chris@22 845 context "#start_date" do
chris@22 846 setup do
chris@22 847 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
chris@22 848 @project = Project.generate!(:identifier => 'test0')
chris@22 849 @project.trackers << Tracker.generate!
chris@22 850 end
chris@22 851
chris@22 852 should "be nil if there are no issues on the project" do
chris@22 853 assert_nil @project.start_date
chris@22 854 end
chris@22 855
chris@22 856 should "be nil if issue tracking is disabled" do
chris@22 857 Issue.generate_for_project!(@project, :start_date => Date.today)
chris@22 858 @project.enabled_modules.find_all_by_name('issue_tracking').each {|m| m.destroy}
chris@22 859 @project.reload
chris@22 860
chris@22 861 assert_nil @project.start_date
chris@22 862 end
chris@22 863
chris@22 864 should "be the earliest start date of it's issues" do
chris@22 865 early = 7.days.ago.to_date
chris@22 866 Issue.generate_for_project!(@project, :start_date => Date.today)
chris@22 867 Issue.generate_for_project!(@project, :start_date => early)
chris@22 868
chris@22 869 assert_equal early, @project.start_date
chris@22 870 end
chris@22 871
chris@22 872 end
chris@22 873
chris@22 874 context "#due_date" do
chris@22 875 setup do
chris@22 876 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
chris@22 877 @project = Project.generate!(:identifier => 'test0')
chris@22 878 @project.trackers << Tracker.generate!
chris@22 879 end
chris@22 880
chris@22 881 should "be nil if there are no issues on the project" do
chris@22 882 assert_nil @project.due_date
chris@22 883 end
chris@22 884
chris@22 885 should "be nil if issue tracking is disabled" do
chris@22 886 Issue.generate_for_project!(@project, :due_date => Date.today)
chris@22 887 @project.enabled_modules.find_all_by_name('issue_tracking').each {|m| m.destroy}
chris@22 888 @project.reload
chris@22 889
chris@22 890 assert_nil @project.due_date
chris@22 891 end
chris@22 892
chris@22 893 should "be the latest due date of it's issues" do
chris@22 894 future = 7.days.from_now.to_date
chris@22 895 Issue.generate_for_project!(@project, :due_date => future)
chris@22 896 Issue.generate_for_project!(@project, :due_date => Date.today)
chris@22 897
chris@22 898 assert_equal future, @project.due_date
chris@22 899 end
chris@22 900
chris@22 901 should "be the latest due date of it's versions" do
chris@22 902 future = 7.days.from_now.to_date
chris@22 903 @project.versions << Version.generate!(:effective_date => future)
chris@22 904 @project.versions << Version.generate!(:effective_date => Date.today)
chris@22 905
chris@22 906
chris@22 907 assert_equal future, @project.due_date
chris@22 908
chris@22 909 end
chris@22 910
chris@22 911 should "pick the latest date from it's issues and versions" do
chris@22 912 future = 7.days.from_now.to_date
chris@22 913 far_future = 14.days.from_now.to_date
chris@22 914 Issue.generate_for_project!(@project, :due_date => far_future)
chris@22 915 @project.versions << Version.generate!(:effective_date => future)
chris@22 916
chris@22 917 assert_equal far_future, @project.due_date
chris@22 918 end
chris@22 919
chris@22 920 end
chris@22 921
chris@22 922 context "Project#completed_percent" do
chris@22 923 setup do
chris@22 924 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
chris@22 925 @project = Project.generate!(:identifier => 'test0')
chris@22 926 @project.trackers << Tracker.generate!
chris@22 927 end
chris@22 928
chris@22 929 context "no versions" do
chris@22 930 should "be 100" do
chris@22 931 assert_equal 100, @project.completed_percent
chris@22 932 end
chris@22 933 end
chris@22 934
chris@22 935 context "with versions" do
chris@22 936 should "return 0 if the versions have no issues" do
chris@22 937 Version.generate!(:project => @project)
chris@22 938 Version.generate!(:project => @project)
chris@22 939
chris@22 940 assert_equal 0, @project.completed_percent
chris@22 941 end
chris@22 942
chris@22 943 should "return 100 if the version has only closed issues" do
chris@22 944 v1 = Version.generate!(:project => @project)
chris@22 945 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v1)
chris@22 946 v2 = Version.generate!(:project => @project)
chris@22 947 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v2)
chris@22 948
chris@22 949 assert_equal 100, @project.completed_percent
chris@22 950 end
chris@22 951
chris@22 952 should "return the averaged completed percent of the versions (not weighted)" do
chris@22 953 v1 = Version.generate!(:project => @project)
chris@22 954 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v1)
chris@22 955 v2 = Version.generate!(:project => @project)
chris@22 956 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v2)
chris@22 957
chris@22 958 assert_equal 50, @project.completed_percent
chris@22 959 end
chris@22 960
chris@22 961 end
chris@22 962 end
Chris@0 963 end