annotate test/unit/project_test.rb @ 1480:75fd8eace091 issue_556

Close obsolete branch issue_556
author Chris Cannam
date Sat, 13 Jul 2013 15:26:30 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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@909 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@909 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@119 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class ProjectTest < ActiveSupport::TestCase
Chris@909 21 fixtures :projects, :trackers, :issue_statuses, :issues,
Chris@1115 22 :journals, :journal_details,
Chris@909 23 :enumerations, :users, :issue_categories,
Chris@909 24 :projects_trackers,
Chris@1115 25 :custom_fields,
Chris@1115 26 :custom_fields_projects,
Chris@1115 27 :custom_fields_trackers,
Chris@1115 28 :custom_values,
Chris@909 29 :roles,
Chris@909 30 :member_roles,
Chris@909 31 :members,
Chris@909 32 :enabled_modules,
Chris@909 33 :workflows,
Chris@909 34 :versions,
Chris@909 35 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
Chris@909 36 :groups_users,
Chris@1115 37 :boards, :messages,
Chris@1115 38 :repositories,
Chris@1115 39 :news, :comments,
Chris@1115 40 :documents
Chris@0 41
Chris@0 42 def setup
Chris@0 43 @ecookbook = Project.find(1)
Chris@0 44 @ecookbook_sub1 = Project.find(3)
Chris@1115 45 set_tmp_attachments_directory
Chris@0 46 User.current = nil
Chris@0 47 end
Chris@909 48
Chris@0 49 def test_truth
Chris@0 50 assert_kind_of Project, @ecookbook
Chris@0 51 assert_equal "eCookbook", @ecookbook.name
Chris@0 52 end
Chris@909 53
Chris@119 54 def test_default_attributes
Chris@119 55 with_settings :default_projects_public => '1' do
Chris@119 56 assert_equal true, Project.new.is_public
Chris@119 57 assert_equal false, Project.new(:is_public => false).is_public
Chris@119 58 end
Chris@119 59
Chris@119 60 with_settings :default_projects_public => '0' do
Chris@119 61 assert_equal false, Project.new.is_public
Chris@119 62 assert_equal true, Project.new(:is_public => true).is_public
Chris@119 63 end
Chris@119 64
Chris@119 65 with_settings :sequential_project_identifiers => '1' do
Chris@119 66 assert !Project.new.identifier.blank?
Chris@119 67 assert Project.new(:identifier => '').identifier.blank?
Chris@119 68 end
Chris@119 69
Chris@119 70 with_settings :sequential_project_identifiers => '0' do
Chris@119 71 assert Project.new.identifier.blank?
Chris@119 72 assert !Project.new(:identifier => 'test').blank?
Chris@119 73 end
Chris@119 74
Chris@119 75 with_settings :default_projects_modules => ['issue_tracking', 'repository'] do
Chris@119 76 assert_equal ['issue_tracking', 'repository'], Project.new.enabled_module_names
Chris@119 77 end
Chris@909 78
Chris@929 79 assert_equal Tracker.all.sort, Project.new.trackers.sort
Chris@929 80 assert_equal Tracker.find(1, 3).sort, Project.new(:tracker_ids => [1, 3]).trackers.sort
Chris@119 81 end
Chris@909 82
Chris@0 83 def test_update
Chris@0 84 assert_equal "eCookbook", @ecookbook.name
Chris@0 85 @ecookbook.name = "eCook"
Chris@0 86 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
Chris@0 87 @ecookbook.reload
Chris@0 88 assert_equal "eCook", @ecookbook.name
Chris@0 89 end
Chris@909 90
Chris@0 91 def test_validate_identifier
Chris@0 92 to_test = {"abc" => true,
Chris@0 93 "ab12" => true,
Chris@0 94 "ab-12" => true,
Chris@1115 95 "ab_12" => true,
Chris@0 96 "12" => false,
Chris@0 97 "new" => false}
Chris@909 98
Chris@0 99 to_test.each do |identifier, valid|
Chris@0 100 p = Project.new
Chris@0 101 p.identifier = identifier
Chris@0 102 p.valid?
Chris@1115 103 if valid
Chris@1115 104 assert p.errors['identifier'].blank?, "identifier #{identifier} was not valid"
Chris@1115 105 else
Chris@1115 106 assert p.errors['identifier'].present?, "identifier #{identifier} was valid"
Chris@1115 107 end
Chris@0 108 end
Chris@0 109 end
Chris@0 110
Chris@1115 111 def test_identifier_should_not_be_frozen_for_a_new_project
Chris@1115 112 assert_equal false, Project.new.identifier_frozen?
Chris@1115 113 end
Chris@1115 114
Chris@1115 115 def test_identifier_should_not_be_frozen_for_a_saved_project_with_blank_identifier
Chris@1115 116 Project.update_all(["identifier = ''"], "id = 1")
Chris@1115 117
Chris@1115 118 assert_equal false, Project.find(1).identifier_frozen?
Chris@1115 119 end
Chris@1115 120
Chris@1115 121 def test_identifier_should_be_frozen_for_a_saved_project_with_valid_identifier
Chris@1115 122 assert_equal true, Project.find(1).identifier_frozen?
Chris@1115 123 end
Chris@1115 124
Chris@0 125 def test_members_should_be_active_users
Chris@0 126 Project.all.each do |project|
Chris@0 127 assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) }
Chris@0 128 end
Chris@0 129 end
Chris@909 130
Chris@0 131 def test_users_should_be_active_users
Chris@0 132 Project.all.each do |project|
Chris@0 133 assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) }
Chris@0 134 end
Chris@0 135 end
Chris@909 136
Chris@1115 137 def test_open_scope_on_issues_association
Chris@1115 138 assert_kind_of Issue, Project.find(1).issues.open.first
Chris@1115 139 end
Chris@1115 140
Chris@0 141 def test_archive
Chris@0 142 user = @ecookbook.members.first.user
Chris@0 143 @ecookbook.archive
Chris@0 144 @ecookbook.reload
Chris@909 145
Chris@0 146 assert !@ecookbook.active?
chris@37 147 assert @ecookbook.archived?
Chris@0 148 assert !user.projects.include?(@ecookbook)
Chris@0 149 # Subproject are also archived
Chris@0 150 assert !@ecookbook.children.empty?
Chris@0 151 assert @ecookbook.descendants.active.empty?
Chris@0 152 end
Chris@909 153
Chris@0 154 def test_archive_should_fail_if_versions_are_used_by_non_descendant_projects
Chris@0 155 # Assign an issue of a project to a version of a child project
Chris@0 156 Issue.find(4).update_attribute :fixed_version_id, 4
Chris@909 157
Chris@0 158 assert_no_difference "Project.count(:all, :conditions => 'status = #{Project::STATUS_ARCHIVED}')" do
Chris@0 159 assert_equal false, @ecookbook.archive
Chris@0 160 end
Chris@0 161 @ecookbook.reload
Chris@0 162 assert @ecookbook.active?
Chris@0 163 end
Chris@909 164
Chris@0 165 def test_unarchive
Chris@0 166 user = @ecookbook.members.first.user
Chris@0 167 @ecookbook.archive
Chris@0 168 # A subproject of an archived project can not be unarchived
Chris@0 169 assert !@ecookbook_sub1.unarchive
Chris@909 170
Chris@0 171 # Unarchive project
Chris@0 172 assert @ecookbook.unarchive
Chris@0 173 @ecookbook.reload
Chris@0 174 assert @ecookbook.active?
chris@37 175 assert !@ecookbook.archived?
Chris@0 176 assert user.projects.include?(@ecookbook)
Chris@0 177 # Subproject can now be unarchived
Chris@0 178 @ecookbook_sub1.reload
Chris@0 179 assert @ecookbook_sub1.unarchive
Chris@0 180 end
Chris@909 181
Chris@0 182 def test_destroy
Chris@0 183 # 2 active members
Chris@0 184 assert_equal 2, @ecookbook.members.size
Chris@0 185 # and 1 is locked
Chris@0 186 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
Chris@0 187 # some boards
Chris@0 188 assert @ecookbook.boards.any?
Chris@909 189
Chris@0 190 @ecookbook.destroy
Chris@0 191 # make sure that the project non longer exists
Chris@0 192 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
Chris@0 193 # make sure related data was removed
Chris@0 194 assert_nil Member.first(:conditions => {:project_id => @ecookbook.id})
Chris@0 195 assert_nil Board.first(:conditions => {:project_id => @ecookbook.id})
Chris@0 196 assert_nil Issue.first(:conditions => {:project_id => @ecookbook.id})
Chris@0 197 end
Chris@909 198
Chris@1115 199 def test_destroy_should_destroy_subtasks
Chris@1115 200 issues = (0..2).to_a.map {Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :subject => 'test')}
Chris@1115 201 issues[0].update_attribute :parent_issue_id, issues[1].id
Chris@1115 202 issues[2].update_attribute :parent_issue_id, issues[1].id
Chris@1115 203 assert_equal 2, issues[1].children.count
Chris@1115 204
Chris@1115 205 assert_nothing_raised do
Chris@1115 206 Project.find(1).destroy
Chris@1115 207 end
Chris@1115 208 assert Issue.find_all_by_id(issues.map(&:id)).empty?
Chris@1115 209 end
Chris@1115 210
Chris@441 211 def test_destroying_root_projects_should_clear_data
Chris@441 212 Project.roots.each do |root|
Chris@441 213 root.destroy
Chris@441 214 end
Chris@909 215
Chris@441 216 assert_equal 0, Project.count, "Projects were not deleted: #{Project.all.inspect}"
Chris@441 217 assert_equal 0, Member.count, "Members were not deleted: #{Member.all.inspect}"
Chris@441 218 assert_equal 0, MemberRole.count
Chris@441 219 assert_equal 0, Issue.count
Chris@441 220 assert_equal 0, Journal.count
Chris@441 221 assert_equal 0, JournalDetail.count
Chris@1115 222 assert_equal 0, Attachment.count, "Attachments were not deleted: #{Attachment.all.inspect}"
Chris@441 223 assert_equal 0, EnabledModule.count
Chris@441 224 assert_equal 0, IssueCategory.count
Chris@441 225 assert_equal 0, IssueRelation.count
Chris@441 226 assert_equal 0, Board.count
Chris@441 227 assert_equal 0, Message.count
Chris@441 228 assert_equal 0, News.count
Chris@441 229 assert_equal 0, Query.count(:conditions => "project_id IS NOT NULL")
Chris@441 230 assert_equal 0, Repository.count
Chris@441 231 assert_equal 0, Changeset.count
Chris@441 232 assert_equal 0, Change.count
Chris@441 233 assert_equal 0, Comment.count
Chris@441 234 assert_equal 0, TimeEntry.count
Chris@441 235 assert_equal 0, Version.count
Chris@441 236 assert_equal 0, Watcher.count
Chris@441 237 assert_equal 0, Wiki.count
Chris@441 238 assert_equal 0, WikiPage.count
Chris@441 239 assert_equal 0, WikiContent.count
Chris@441 240 assert_equal 0, WikiContent::Version.count
Chris@441 241 assert_equal 0, Project.connection.select_all("SELECT * FROM projects_trackers").size
Chris@441 242 assert_equal 0, Project.connection.select_all("SELECT * FROM custom_fields_projects").size
Chris@441 243 assert_equal 0, CustomValue.count(:conditions => {:customized_type => ['Project', 'Issue', 'TimeEntry', 'Version']})
Chris@441 244 end
Chris@909 245
Chris@0 246 def test_move_an_orphan_project_to_a_root_project
Chris@0 247 sub = Project.find(2)
Chris@0 248 sub.set_parent! @ecookbook
Chris@0 249 assert_equal @ecookbook.id, sub.parent.id
Chris@0 250 @ecookbook.reload
Chris@0 251 assert_equal 4, @ecookbook.children.size
Chris@0 252 end
Chris@909 253
Chris@0 254 def test_move_an_orphan_project_to_a_subproject
Chris@0 255 sub = Project.find(2)
Chris@0 256 assert sub.set_parent!(@ecookbook_sub1)
Chris@0 257 end
Chris@909 258
Chris@0 259 def test_move_a_root_project_to_a_project
Chris@0 260 sub = @ecookbook
Chris@0 261 assert sub.set_parent!(Project.find(2))
Chris@0 262 end
Chris@909 263
Chris@0 264 def test_should_not_move_a_project_to_its_children
Chris@0 265 sub = @ecookbook
Chris@0 266 assert !(sub.set_parent!(Project.find(3)))
Chris@0 267 end
Chris@909 268
Chris@0 269 def test_set_parent_should_add_roots_in_alphabetical_order
Chris@0 270 ProjectCustomField.delete_all
Chris@0 271 Project.delete_all
Chris@0 272 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
Chris@0 273 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
Chris@0 274 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
Chris@0 275 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
Chris@909 276
Chris@0 277 assert_equal 4, Project.count
Chris@0 278 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
Chris@0 279 end
Chris@909 280
Chris@0 281 def test_set_parent_should_add_children_in_alphabetical_order
Chris@0 282 ProjectCustomField.delete_all
Chris@0 283 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
Chris@0 284 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
Chris@0 285 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
Chris@0 286 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
Chris@0 287 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
Chris@909 288
Chris@0 289 parent.reload
Chris@0 290 assert_equal 4, parent.children.size
Chris@1115 291 assert_equal parent.children.all.sort_by(&:name), parent.children.all
Chris@0 292 end
Chris@909 293
Chris@0 294 def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy
Chris@0 295 # Parent issue with a hierarchy project's fixed version
Chris@0 296 parent_issue = Issue.find(1)
Chris@0 297 parent_issue.update_attribute(:fixed_version_id, 4)
Chris@0 298 parent_issue.reload
Chris@0 299 assert_equal 4, parent_issue.fixed_version_id
Chris@0 300
Chris@0 301 # Should keep fixed versions for the issues
Chris@0 302 issue_with_local_fixed_version = Issue.find(5)
Chris@0 303 issue_with_local_fixed_version.update_attribute(:fixed_version_id, 4)
Chris@0 304 issue_with_local_fixed_version.reload
Chris@0 305 assert_equal 4, issue_with_local_fixed_version.fixed_version_id
Chris@0 306
Chris@0 307 # Local issue with hierarchy fixed_version
Chris@0 308 issue_with_hierarchy_fixed_version = Issue.find(13)
Chris@0 309 issue_with_hierarchy_fixed_version.update_attribute(:fixed_version_id, 6)
Chris@0 310 issue_with_hierarchy_fixed_version.reload
Chris@0 311 assert_equal 6, issue_with_hierarchy_fixed_version.fixed_version_id
Chris@909 312
Chris@0 313 # Move project out of the issue's hierarchy
Chris@0 314 moved_project = Project.find(3)
Chris@0 315 moved_project.set_parent!(Project.find(2))
Chris@0 316 parent_issue.reload
Chris@0 317 issue_with_local_fixed_version.reload
Chris@0 318 issue_with_hierarchy_fixed_version.reload
Chris@909 319
Chris@0 320 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 321 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 322 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 323 end
Chris@909 324
Chris@0 325 def test_parent
Chris@0 326 p = Project.find(6).parent
Chris@0 327 assert p.is_a?(Project)
Chris@0 328 assert_equal 5, p.id
Chris@0 329 end
Chris@909 330
Chris@0 331 def test_ancestors
Chris@0 332 a = Project.find(6).ancestors
Chris@0 333 assert a.first.is_a?(Project)
Chris@0 334 assert_equal [1, 5], a.collect(&:id)
Chris@0 335 end
Chris@909 336
Chris@0 337 def test_root
Chris@0 338 r = Project.find(6).root
Chris@0 339 assert r.is_a?(Project)
Chris@0 340 assert_equal 1, r.id
Chris@0 341 end
Chris@909 342
Chris@0 343 def test_children
Chris@0 344 c = Project.find(1).children
Chris@0 345 assert c.first.is_a?(Project)
Chris@0 346 assert_equal [5, 3, 4], c.collect(&:id)
Chris@0 347 end
Chris@909 348
Chris@0 349 def test_descendants
Chris@0 350 d = Project.find(1).descendants
Chris@0 351 assert d.first.is_a?(Project)
Chris@0 352 assert_equal [5, 6, 3, 4], d.collect(&:id)
Chris@0 353 end
Chris@909 354
Chris@0 355 def test_allowed_parents_should_be_empty_for_non_member_user
Chris@0 356 Role.non_member.add_permission!(:add_project)
Chris@0 357 user = User.find(9)
Chris@0 358 assert user.memberships.empty?
Chris@0 359 User.current = user
Chris@0 360 assert Project.new.allowed_parents.compact.empty?
Chris@0 361 end
Chris@909 362
Chris@0 363 def test_allowed_parents_with_add_subprojects_permission
Chris@0 364 Role.find(1).remove_permission!(:add_project)
Chris@0 365 Role.find(1).add_permission!(:add_subprojects)
Chris@0 366 User.current = User.find(2)
Chris@0 367 # new project
Chris@0 368 assert !Project.new.allowed_parents.include?(nil)
Chris@0 369 assert Project.new.allowed_parents.include?(Project.find(1))
Chris@0 370 # existing root project
Chris@0 371 assert Project.find(1).allowed_parents.include?(nil)
Chris@0 372 # existing child
Chris@0 373 assert Project.find(3).allowed_parents.include?(Project.find(1))
Chris@0 374 assert !Project.find(3).allowed_parents.include?(nil)
Chris@0 375 end
Chris@0 376
Chris@0 377 def test_allowed_parents_with_add_project_permission
Chris@0 378 Role.find(1).add_permission!(:add_project)
Chris@0 379 Role.find(1).remove_permission!(:add_subprojects)
Chris@0 380 User.current = User.find(2)
Chris@0 381 # new project
Chris@0 382 assert Project.new.allowed_parents.include?(nil)
Chris@0 383 assert !Project.new.allowed_parents.include?(Project.find(1))
Chris@0 384 # existing root project
Chris@0 385 assert Project.find(1).allowed_parents.include?(nil)
Chris@0 386 # existing child
Chris@0 387 assert Project.find(3).allowed_parents.include?(Project.find(1))
Chris@0 388 assert Project.find(3).allowed_parents.include?(nil)
Chris@0 389 end
Chris@0 390
Chris@0 391 def test_allowed_parents_with_add_project_and_subprojects_permission
Chris@0 392 Role.find(1).add_permission!(:add_project)
Chris@0 393 Role.find(1).add_permission!(:add_subprojects)
Chris@0 394 User.current = User.find(2)
Chris@0 395 # new project
Chris@0 396 assert Project.new.allowed_parents.include?(nil)
Chris@0 397 assert Project.new.allowed_parents.include?(Project.find(1))
Chris@0 398 # existing root project
Chris@0 399 assert Project.find(1).allowed_parents.include?(nil)
Chris@0 400 # existing child
Chris@0 401 assert Project.find(3).allowed_parents.include?(Project.find(1))
Chris@0 402 assert Project.find(3).allowed_parents.include?(nil)
Chris@0 403 end
Chris@909 404
Chris@0 405 def test_users_by_role
Chris@0 406 users_by_role = Project.find(1).users_by_role
Chris@0 407 assert_kind_of Hash, users_by_role
Chris@0 408 role = Role.find(1)
Chris@0 409 assert_kind_of Array, users_by_role[role]
Chris@0 410 assert users_by_role[role].include?(User.find(2))
Chris@0 411 end
Chris@909 412
Chris@0 413 def test_rolled_up_trackers
Chris@0 414 parent = Project.find(1)
Chris@0 415 parent.trackers = Tracker.find([1,2])
Chris@0 416 child = parent.children.find(3)
Chris@909 417
Chris@0 418 assert_equal [1, 2], parent.tracker_ids
Chris@0 419 assert_equal [2, 3], child.trackers.collect(&:id)
Chris@909 420
Chris@0 421 assert_kind_of Tracker, parent.rolled_up_trackers.first
Chris@0 422 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
Chris@909 423
Chris@0 424 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
Chris@0 425 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
Chris@0 426 end
Chris@909 427
Chris@0 428 def test_rolled_up_trackers_should_ignore_archived_subprojects
Chris@0 429 parent = Project.find(1)
Chris@0 430 parent.trackers = Tracker.find([1,2])
Chris@0 431 child = parent.children.find(3)
Chris@0 432 child.trackers = Tracker.find([1,3])
Chris@0 433 parent.children.each(&:archive)
Chris@909 434
Chris@0 435 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
Chris@0 436 end
Chris@0 437
Chris@0 438 context "#rolled_up_versions" do
Chris@0 439 setup do
Chris@0 440 @project = Project.generate!
Chris@0 441 @parent_version_1 = Version.generate!(:project => @project)
Chris@0 442 @parent_version_2 = Version.generate!(:project => @project)
Chris@0 443 end
Chris@909 444
Chris@0 445 should "include the versions for the current project" do
Chris@0 446 assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
Chris@0 447 end
Chris@909 448
Chris@0 449 should "include versions for a subproject" do
Chris@0 450 @subproject = Project.generate!
Chris@0 451 @subproject.set_parent!(@project)
Chris@0 452 @subproject_version = Version.generate!(:project => @subproject)
Chris@0 453
Chris@0 454 assert_same_elements [
Chris@0 455 @parent_version_1,
Chris@0 456 @parent_version_2,
Chris@0 457 @subproject_version
Chris@0 458 ], @project.rolled_up_versions
Chris@0 459 end
Chris@909 460
Chris@0 461 should "include versions for a sub-subproject" do
Chris@0 462 @subproject = Project.generate!
Chris@0 463 @subproject.set_parent!(@project)
Chris@0 464 @sub_subproject = Project.generate!
Chris@0 465 @sub_subproject.set_parent!(@subproject)
Chris@0 466 @sub_subproject_version = Version.generate!(:project => @sub_subproject)
Chris@0 467
Chris@0 468 @project.reload
Chris@0 469
Chris@0 470 assert_same_elements [
Chris@0 471 @parent_version_1,
Chris@0 472 @parent_version_2,
Chris@0 473 @sub_subproject_version
Chris@0 474 ], @project.rolled_up_versions
Chris@0 475 end
Chris@0 476
Chris@0 477 should "only check active projects" do
Chris@0 478 @subproject = Project.generate!
Chris@0 479 @subproject.set_parent!(@project)
Chris@0 480 @subproject_version = Version.generate!(:project => @subproject)
Chris@0 481 assert @subproject.archive
Chris@0 482
Chris@0 483 @project.reload
Chris@0 484
Chris@0 485 assert !@subproject.active?
Chris@0 486 assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
Chris@0 487 end
Chris@0 488 end
Chris@909 489
Chris@0 490 def test_shared_versions_none_sharing
Chris@0 491 p = Project.find(5)
Chris@0 492 v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none')
Chris@0 493 assert p.shared_versions.include?(v)
Chris@0 494 assert !p.children.first.shared_versions.include?(v)
Chris@0 495 assert !p.root.shared_versions.include?(v)
Chris@0 496 assert !p.siblings.first.shared_versions.include?(v)
Chris@0 497 assert !p.root.siblings.first.shared_versions.include?(v)
Chris@0 498 end
Chris@0 499
Chris@0 500 def test_shared_versions_descendants_sharing
Chris@0 501 p = Project.find(5)
Chris@0 502 v = Version.create!(:name => 'descendants_sharing', :project => p, :sharing => 'descendants')
Chris@0 503 assert p.shared_versions.include?(v)
Chris@0 504 assert p.children.first.shared_versions.include?(v)
Chris@0 505 assert !p.root.shared_versions.include?(v)
Chris@0 506 assert !p.siblings.first.shared_versions.include?(v)
Chris@0 507 assert !p.root.siblings.first.shared_versions.include?(v)
Chris@0 508 end
Chris@909 509
Chris@0 510 def test_shared_versions_hierarchy_sharing
Chris@0 511 p = Project.find(5)
Chris@0 512 v = Version.create!(:name => 'hierarchy_sharing', :project => p, :sharing => 'hierarchy')
Chris@0 513 assert p.shared_versions.include?(v)
Chris@0 514 assert p.children.first.shared_versions.include?(v)
Chris@0 515 assert p.root.shared_versions.include?(v)
Chris@0 516 assert !p.siblings.first.shared_versions.include?(v)
Chris@0 517 assert !p.root.siblings.first.shared_versions.include?(v)
Chris@0 518 end
Chris@0 519
Chris@0 520 def test_shared_versions_tree_sharing
Chris@0 521 p = Project.find(5)
Chris@0 522 v = Version.create!(:name => 'tree_sharing', :project => p, :sharing => 'tree')
Chris@0 523 assert p.shared_versions.include?(v)
Chris@0 524 assert p.children.first.shared_versions.include?(v)
Chris@0 525 assert p.root.shared_versions.include?(v)
Chris@0 526 assert p.siblings.first.shared_versions.include?(v)
Chris@0 527 assert !p.root.siblings.first.shared_versions.include?(v)
Chris@0 528 end
Chris@0 529
Chris@0 530 def test_shared_versions_system_sharing
Chris@0 531 p = Project.find(5)
Chris@0 532 v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system')
Chris@0 533 assert p.shared_versions.include?(v)
Chris@0 534 assert p.children.first.shared_versions.include?(v)
Chris@0 535 assert p.root.shared_versions.include?(v)
Chris@0 536 assert p.siblings.first.shared_versions.include?(v)
Chris@0 537 assert p.root.siblings.first.shared_versions.include?(v)
Chris@0 538 end
Chris@0 539
Chris@0 540 def test_shared_versions
Chris@0 541 parent = Project.find(1)
Chris@0 542 child = parent.children.find(3)
Chris@0 543 private_child = parent.children.find(5)
Chris@909 544
Chris@0 545 assert_equal [1,2,3], parent.version_ids.sort
Chris@0 546 assert_equal [4], child.version_ids
Chris@0 547 assert_equal [6], private_child.version_ids
Chris@0 548 assert_equal [7], Version.find_all_by_sharing('system').collect(&:id)
Chris@0 549
Chris@0 550 assert_equal 6, parent.shared_versions.size
Chris@0 551 parent.shared_versions.each do |version|
Chris@0 552 assert_kind_of Version, version
Chris@0 553 end
Chris@0 554
Chris@0 555 assert_equal [1,2,3,4,6,7], parent.shared_versions.collect(&:id).sort
Chris@0 556 end
Chris@0 557
Chris@0 558 def test_shared_versions_should_ignore_archived_subprojects
Chris@0 559 parent = Project.find(1)
Chris@0 560 child = parent.children.find(3)
Chris@0 561 child.archive
Chris@0 562 parent.reload
Chris@909 563
Chris@0 564 assert_equal [1,2,3], parent.version_ids.sort
Chris@0 565 assert_equal [4], child.version_ids
Chris@0 566 assert !parent.shared_versions.collect(&:id).include?(4)
Chris@0 567 end
Chris@0 568
Chris@0 569 def test_shared_versions_visible_to_user
Chris@0 570 user = User.find(3)
Chris@0 571 parent = Project.find(1)
Chris@0 572 child = parent.children.find(5)
Chris@909 573
Chris@0 574 assert_equal [1,2,3], parent.version_ids.sort
Chris@0 575 assert_equal [6], child.version_ids
Chris@0 576
Chris@0 577 versions = parent.shared_versions.visible(user)
Chris@909 578
Chris@0 579 assert_equal 4, versions.size
Chris@0 580 versions.each do |version|
Chris@0 581 assert_kind_of Version, version
Chris@0 582 end
Chris@0 583
Chris@0 584 assert !versions.collect(&:id).include?(6)
Chris@0 585 end
Chris@0 586
Chris@929 587 def test_shared_versions_for_new_project_should_include_system_shared_versions
Chris@929 588 p = Project.find(5)
Chris@929 589 v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system')
Chris@929 590
Chris@929 591 assert_include v, Project.new.shared_versions
Chris@929 592 end
Chris@929 593
Chris@0 594 def test_next_identifier
Chris@0 595 ProjectCustomField.delete_all
Chris@0 596 Project.create!(:name => 'last', :identifier => 'p2008040')
Chris@0 597 assert_equal 'p2008041', Project.next_identifier
Chris@0 598 end
Chris@0 599
Chris@0 600 def test_next_identifier_first_project
Chris@0 601 Project.delete_all
Chris@0 602 assert_nil Project.next_identifier
Chris@0 603 end
Chris@909 604
Chris@441 605 def test_enabled_module_names
Chris@441 606 with_settings :default_projects_modules => ['issue_tracking', 'repository'] do
Chris@441 607 project = Project.new
Chris@909 608
Chris@441 609 project.enabled_module_names = %w(issue_tracking news)
Chris@441 610 assert_equal %w(issue_tracking news), project.enabled_module_names.sort
Chris@441 611 end
Chris@441 612 end
Chris@0 613
Chris@507 614 context "enabled_modules" do
Chris@507 615 setup do
Chris@507 616 @project = Project.find(1)
Chris@507 617 end
Chris@507 618
Chris@507 619 should "define module by names and preserve ids" do
Chris@507 620 # Remove one module
Chris@507 621 modules = @project.enabled_modules.slice(0..-2)
Chris@507 622 assert modules.any?
Chris@507 623 assert_difference 'EnabledModule.count', -1 do
Chris@507 624 @project.enabled_module_names = modules.collect(&:name)
Chris@507 625 end
Chris@507 626 @project.reload
Chris@507 627 # Ids should be preserved
Chris@507 628 assert_equal @project.enabled_module_ids.sort, modules.collect(&:id).sort
Chris@507 629 end
Chris@507 630
Chris@507 631 should "enable a module" do
Chris@507 632 @project.enabled_module_names = []
Chris@507 633 @project.reload
Chris@507 634 assert_equal [], @project.enabled_module_names
Chris@507 635 #with string
Chris@507 636 @project.enable_module!("issue_tracking")
Chris@507 637 assert_equal ["issue_tracking"], @project.enabled_module_names
Chris@507 638 #with symbol
Chris@507 639 @project.enable_module!(:gantt)
Chris@507 640 assert_equal ["issue_tracking", "gantt"], @project.enabled_module_names
Chris@507 641 #don't add a module twice
Chris@507 642 @project.enable_module!("issue_tracking")
Chris@507 643 assert_equal ["issue_tracking", "gantt"], @project.enabled_module_names
Chris@507 644 end
Chris@507 645
Chris@507 646 should "disable a module" do
Chris@507 647 #with string
Chris@507 648 assert @project.enabled_module_names.include?("issue_tracking")
Chris@507 649 @project.disable_module!("issue_tracking")
Chris@507 650 assert ! @project.reload.enabled_module_names.include?("issue_tracking")
Chris@507 651 #with symbol
Chris@507 652 assert @project.enabled_module_names.include?("gantt")
Chris@507 653 @project.disable_module!(:gantt)
Chris@507 654 assert ! @project.reload.enabled_module_names.include?("gantt")
Chris@507 655 #with EnabledModule object
Chris@507 656 first_module = @project.enabled_modules.first
Chris@507 657 @project.disable_module!(first_module)
Chris@507 658 assert ! @project.reload.enabled_module_names.include?(first_module.name)
Chris@507 659 end
Chris@507 660 end
Chris@507 661
Chris@0 662 def test_enabled_module_names_should_not_recreate_enabled_modules
Chris@0 663 project = Project.find(1)
Chris@0 664 # Remove one module
Chris@0 665 modules = project.enabled_modules.slice(0..-2)
Chris@0 666 assert modules.any?
Chris@0 667 assert_difference 'EnabledModule.count', -1 do
Chris@0 668 project.enabled_module_names = modules.collect(&:name)
Chris@0 669 end
Chris@0 670 project.reload
Chris@0 671 # Ids should be preserved
Chris@0 672 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
Chris@0 673 end
Chris@0 674
Chris@0 675 def test_copy_from_existing_project
Chris@0 676 source_project = Project.find(1)
Chris@0 677 copied_project = Project.copy_from(1)
Chris@0 678
Chris@0 679 assert copied_project
Chris@0 680 # Cleared attributes
Chris@0 681 assert copied_project.id.blank?
Chris@0 682 assert copied_project.name.blank?
Chris@0 683 assert copied_project.identifier.blank?
Chris@909 684
Chris@0 685 # Duplicated attributes
Chris@0 686 assert_equal source_project.description, copied_project.description
Chris@0 687 assert_equal source_project.enabled_modules, copied_project.enabled_modules
Chris@0 688 assert_equal source_project.trackers, copied_project.trackers
Chris@0 689
Chris@0 690 # Default attributes
Chris@0 691 assert_equal 1, copied_project.status
Chris@0 692 end
Chris@0 693
Chris@0 694 def test_activities_should_use_the_system_activities
Chris@0 695 project = Project.find(1)
Chris@0 696 assert_equal project.activities, TimeEntryActivity.find(:all, :conditions => {:active => true} )
Chris@0 697 end
Chris@0 698
Chris@0 699
Chris@0 700 def test_activities_should_use_the_project_specific_activities
Chris@0 701 project = Project.find(1)
Chris@0 702 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project})
Chris@0 703 assert overridden_activity.save!
Chris@0 704
Chris@0 705 assert project.activities.include?(overridden_activity), "Project specific Activity not found"
Chris@0 706 end
Chris@0 707
Chris@0 708 def test_activities_should_not_include_the_inactive_project_specific_activities
Chris@0 709 project = Project.find(1)
Chris@0 710 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
Chris@0 711 assert overridden_activity.save!
Chris@0 712
Chris@0 713 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found"
Chris@0 714 end
Chris@0 715
Chris@0 716 def test_activities_should_not_include_project_specific_activities_from_other_projects
Chris@0 717 project = Project.find(1)
Chris@0 718 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)})
Chris@0 719 assert overridden_activity.save!
Chris@0 720
Chris@0 721 assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project"
Chris@0 722 end
Chris@0 723
Chris@0 724 def test_activities_should_handle_nils
Chris@0 725 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(1), :parent => TimeEntryActivity.find(:first)})
Chris@0 726 TimeEntryActivity.delete_all
Chris@0 727
Chris@0 728 # No activities
Chris@0 729 project = Project.find(1)
Chris@0 730 assert project.activities.empty?
Chris@0 731
Chris@0 732 # No system, one overridden
Chris@0 733 assert overridden_activity.save!
Chris@0 734 project.reload
Chris@0 735 assert_equal [overridden_activity], project.activities
Chris@0 736 end
Chris@0 737
Chris@0 738 def test_activities_should_override_system_activities_with_project_activities
Chris@0 739 project = Project.find(1)
Chris@0 740 parent_activity = TimeEntryActivity.find(:first)
Chris@0 741 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity})
Chris@0 742 assert overridden_activity.save!
Chris@0 743
Chris@0 744 assert project.activities.include?(overridden_activity), "Project specific Activity not found"
Chris@0 745 assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden"
Chris@0 746 end
Chris@0 747
Chris@0 748 def test_activities_should_include_inactive_activities_if_specified
Chris@0 749 project = Project.find(1)
Chris@0 750 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
Chris@0 751 assert overridden_activity.save!
Chris@0 752
Chris@0 753 assert project.activities(true).include?(overridden_activity), "Inactive Project specific Activity not found"
Chris@0 754 end
Chris@0 755
Chris@0 756 test 'activities should not include active System activities if the project has an override that is inactive' do
Chris@0 757 project = Project.find(1)
Chris@0 758 system_activity = TimeEntryActivity.find_by_name('Design')
Chris@0 759 assert system_activity.active?
Chris@1115 760 overridden_activity = TimeEntryActivity.create!(:name => "Project", :project => project, :parent => system_activity, :active => false)
Chris@0 761 assert overridden_activity.save!
Chris@909 762
Chris@0 763 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found"
Chris@0 764 assert !project.activities.include?(system_activity), "System activity found when the project has an inactive override"
Chris@0 765 end
Chris@909 766
Chris@0 767 def test_close_completed_versions
Chris@0 768 Version.update_all("status = 'open'")
Chris@0 769 project = Project.find(1)
Chris@0 770 assert_not_nil project.versions.detect {|v| v.completed? && v.status == 'open'}
Chris@0 771 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
Chris@0 772 project.close_completed_versions
Chris@0 773 project.reload
Chris@0 774 assert_nil project.versions.detect {|v| v.completed? && v.status != 'closed'}
Chris@0 775 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
Chris@0 776 end
Chris@0 777
Chris@0 778 context "Project#copy" do
Chris@0 779 setup do
Chris@0 780 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
Chris@0 781 Project.destroy_all :identifier => "copy-test"
Chris@0 782 @source_project = Project.find(2)
Chris@0 783 @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
Chris@0 784 @project.trackers = @source_project.trackers
Chris@0 785 @project.enabled_module_names = @source_project.enabled_modules.collect(&:name)
Chris@0 786 end
Chris@0 787
Chris@0 788 should "copy issues" do
Chris@0 789 @source_project.issues << Issue.generate!(:status => IssueStatus.find_by_name('Closed'),
Chris@0 790 :subject => "copy issue status",
Chris@0 791 :tracker_id => 1,
Chris@0 792 :assigned_to_id => 2,
Chris@0 793 :project_id => @source_project.id)
Chris@0 794 assert @project.valid?
Chris@0 795 assert @project.issues.empty?
Chris@0 796 assert @project.copy(@source_project)
Chris@0 797
Chris@0 798 assert_equal @source_project.issues.size, @project.issues.size
Chris@0 799 @project.issues.each do |issue|
Chris@0 800 assert issue.valid?
Chris@0 801 assert ! issue.assigned_to.blank?
Chris@0 802 assert_equal @project, issue.project
Chris@0 803 end
Chris@909 804
Chris@0 805 copied_issue = @project.issues.first(:conditions => {:subject => "copy issue status"})
Chris@0 806 assert copied_issue
Chris@0 807 assert copied_issue.status
Chris@0 808 assert_equal "Closed", copied_issue.status.name
Chris@0 809 end
Chris@0 810
Chris@1115 811 should "copy issues assigned to a locked version" do
Chris@1115 812 User.current = User.find(1)
Chris@1115 813 assigned_version = Version.generate!(:name => "Assigned Issues")
Chris@1115 814 @source_project.versions << assigned_version
Chris@1115 815 Issue.generate!(:project => @source_project,
Chris@1115 816 :fixed_version_id => assigned_version.id,
Chris@1115 817 :subject => "copy issues assigned to a locked version")
Chris@1115 818 assigned_version.update_attribute :status, 'locked'
Chris@1115 819
Chris@1115 820 assert @project.copy(@source_project)
Chris@1115 821 @project.reload
Chris@1115 822 copied_issue = @project.issues.first(:conditions => {:subject => "copy issues assigned to a locked version"})
Chris@1115 823
Chris@1115 824 assert copied_issue
Chris@1115 825 assert copied_issue.fixed_version
Chris@1115 826 assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
Chris@1115 827 assert_equal 'locked', copied_issue.fixed_version.status
Chris@1115 828 end
Chris@1115 829
Chris@0 830 should "change the new issues to use the copied version" do
Chris@0 831 User.current = User.find(1)
Chris@0 832 assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open')
Chris@0 833 @source_project.versions << assigned_version
Chris@0 834 assert_equal 3, @source_project.versions.size
Chris@1115 835 Issue.generate!(:project => @source_project,
Chris@1115 836 :fixed_version_id => assigned_version.id,
Chris@1115 837 :subject => "change the new issues to use the copied version")
Chris@909 838
Chris@0 839 assert @project.copy(@source_project)
Chris@0 840 @project.reload
Chris@0 841 copied_issue = @project.issues.first(:conditions => {:subject => "change the new issues to use the copied version"})
Chris@0 842
Chris@0 843 assert copied_issue
Chris@0 844 assert copied_issue.fixed_version
Chris@0 845 assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
Chris@0 846 assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record
Chris@0 847 end
Chris@0 848
Chris@1115 849 should "keep target shared versions from other project" do
Chris@1115 850 assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open', :project_id => 1, :sharing => 'system')
Chris@1115 851 issue = Issue.generate!(:project => @source_project,
Chris@1115 852 :fixed_version => assigned_version,
Chris@1115 853 :subject => "keep target shared versions")
Chris@1115 854
Chris@1115 855 assert @project.copy(@source_project)
Chris@1115 856 @project.reload
Chris@1115 857 copied_issue = @project.issues.first(:conditions => {:subject => "keep target shared versions"})
Chris@1115 858
Chris@1115 859 assert copied_issue
Chris@1115 860 assert_equal assigned_version, copied_issue.fixed_version
Chris@1115 861 end
Chris@1115 862
Chris@0 863 should "copy issue relations" do
Chris@0 864 Setting.cross_project_issue_relations = '1'
Chris@0 865
Chris@0 866 second_issue = Issue.generate!(:status_id => 5,
Chris@0 867 :subject => "copy issue relation",
Chris@0 868 :tracker_id => 1,
Chris@0 869 :assigned_to_id => 2,
Chris@0 870 :project_id => @source_project.id)
Chris@1115 871 source_relation = IssueRelation.create!(:issue_from => Issue.find(4),
Chris@0 872 :issue_to => second_issue,
Chris@0 873 :relation_type => "relates")
Chris@1115 874 source_relation_cross_project = IssueRelation.create!(:issue_from => Issue.find(1),
Chris@0 875 :issue_to => second_issue,
Chris@0 876 :relation_type => "duplicates")
Chris@0 877
Chris@0 878 assert @project.copy(@source_project)
Chris@0 879 assert_equal @source_project.issues.count, @project.issues.count
Chris@0 880 copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4
Chris@0 881 copied_second_issue = @project.issues.find_by_subject("copy issue relation")
Chris@0 882
Chris@0 883 # First issue with a relation on project
Chris@0 884 assert_equal 1, copied_issue.relations.size, "Relation not copied"
Chris@0 885 copied_relation = copied_issue.relations.first
Chris@0 886 assert_equal "relates", copied_relation.relation_type
Chris@0 887 assert_equal copied_second_issue.id, copied_relation.issue_to_id
Chris@0 888 assert_not_equal source_relation.id, copied_relation.id
Chris@0 889
Chris@0 890 # Second issue with a cross project relation
Chris@0 891 assert_equal 2, copied_second_issue.relations.size, "Relation not copied"
Chris@0 892 copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first
Chris@0 893 assert_equal "duplicates", copied_relation.relation_type
Chris@0 894 assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept"
Chris@0 895 assert_not_equal source_relation_cross_project.id, copied_relation.id
Chris@0 896 end
Chris@0 897
Chris@1115 898 should "copy issue attachments" do
Chris@1115 899 issue = Issue.generate!(:subject => "copy with attachment", :tracker_id => 1, :project_id => @source_project.id)
Chris@1115 900 Attachment.create!(:container => issue, :file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 1)
Chris@1115 901 @source_project.issues << issue
Chris@1115 902 assert @project.copy(@source_project)
Chris@1115 903
Chris@1115 904 copied_issue = @project.issues.first(:conditions => {:subject => "copy with attachment"})
Chris@1115 905 assert_not_nil copied_issue
Chris@1115 906 assert_equal 1, copied_issue.attachments.count, "Attachment not copied"
Chris@1115 907 assert_equal "testfile.txt", copied_issue.attachments.first.filename
Chris@1115 908 end
Chris@1115 909
Chris@0 910 should "copy memberships" do
Chris@0 911 assert @project.valid?
Chris@0 912 assert @project.members.empty?
Chris@0 913 assert @project.copy(@source_project)
Chris@0 914
Chris@0 915 assert_equal @source_project.memberships.size, @project.memberships.size
Chris@0 916 @project.memberships.each do |membership|
Chris@0 917 assert membership
Chris@0 918 assert_equal @project, membership.project
Chris@0 919 end
Chris@0 920 end
Chris@909 921
Chris@119 922 should "copy memberships with groups and additional roles" do
Chris@119 923 group = Group.create!(:lastname => "Copy group")
Chris@909 924 user = User.find(7)
Chris@119 925 group.users << user
Chris@119 926 # group role
Chris@119 927 Member.create!(:project_id => @source_project.id, :principal => group, :role_ids => [2])
Chris@119 928 member = Member.find_by_user_id_and_project_id(user.id, @source_project.id)
Chris@119 929 # additional role
Chris@119 930 member.role_ids = [1]
Chris@119 931
Chris@119 932 assert @project.copy(@source_project)
Chris@119 933 member = Member.find_by_user_id_and_project_id(user.id, @project.id)
Chris@119 934 assert_not_nil member
Chris@119 935 assert_equal [1, 2], member.role_ids.sort
Chris@119 936 end
Chris@0 937
Chris@0 938 should "copy project specific queries" do
Chris@0 939 assert @project.valid?
Chris@0 940 assert @project.queries.empty?
Chris@0 941 assert @project.copy(@source_project)
Chris@0 942
Chris@0 943 assert_equal @source_project.queries.size, @project.queries.size
Chris@0 944 @project.queries.each do |query|
Chris@0 945 assert query
Chris@0 946 assert_equal @project, query.project
Chris@0 947 end
Chris@909 948 assert_equal @source_project.queries.map(&:user_id).sort, @project.queries.map(&:user_id).sort
Chris@0 949 end
Chris@0 950
Chris@0 951 should "copy versions" do
Chris@0 952 @source_project.versions << Version.generate!
Chris@0 953 @source_project.versions << Version.generate!
Chris@0 954
Chris@0 955 assert @project.versions.empty?
Chris@0 956 assert @project.copy(@source_project)
Chris@0 957
Chris@0 958 assert_equal @source_project.versions.size, @project.versions.size
Chris@0 959 @project.versions.each do |version|
Chris@0 960 assert version
Chris@0 961 assert_equal @project, version.project
Chris@0 962 end
Chris@0 963 end
Chris@0 964
Chris@0 965 should "copy wiki" do
Chris@0 966 assert_difference 'Wiki.count' do
Chris@0 967 assert @project.copy(@source_project)
Chris@0 968 end
Chris@0 969
Chris@0 970 assert @project.wiki
Chris@0 971 assert_not_equal @source_project.wiki, @project.wiki
Chris@0 972 assert_equal "Start page", @project.wiki.start_page
Chris@0 973 end
Chris@0 974
Chris@0 975 should "copy wiki pages and content with hierarchy" do
Chris@0 976 assert_difference 'WikiPage.count', @source_project.wiki.pages.size do
Chris@0 977 assert @project.copy(@source_project)
Chris@0 978 end
Chris@909 979
Chris@0 980 assert @project.wiki
Chris@0 981 assert_equal @source_project.wiki.pages.size, @project.wiki.pages.size
Chris@0 982
Chris@0 983 @project.wiki.pages.each do |wiki_page|
Chris@0 984 assert wiki_page.content
Chris@0 985 assert !@source_project.wiki.pages.include?(wiki_page)
Chris@0 986 end
Chris@909 987
Chris@0 988 parent = @project.wiki.find_page('Parent_page')
Chris@0 989 child1 = @project.wiki.find_page('Child_page_1')
Chris@0 990 child2 = @project.wiki.find_page('Child_page_2')
Chris@0 991 assert_equal parent, child1.parent
Chris@0 992 assert_equal parent, child2.parent
Chris@0 993 end
Chris@0 994
Chris@0 995 should "copy issue categories" do
Chris@0 996 assert @project.copy(@source_project)
Chris@0 997
Chris@0 998 assert_equal 2, @project.issue_categories.size
Chris@0 999 @project.issue_categories.each do |issue_category|
Chris@0 1000 assert !@source_project.issue_categories.include?(issue_category)
Chris@0 1001 end
Chris@0 1002 end
Chris@0 1003
Chris@0 1004 should "copy boards" do
Chris@0 1005 assert @project.copy(@source_project)
Chris@0 1006
Chris@0 1007 assert_equal 1, @project.boards.size
Chris@0 1008 @project.boards.each do |board|
Chris@0 1009 assert !@source_project.boards.include?(board)
Chris@0 1010 end
Chris@0 1011 end
Chris@0 1012
Chris@0 1013 should "change the new issues to use the copied issue categories" do
Chris@0 1014 issue = Issue.find(4)
Chris@0 1015 issue.update_attribute(:category_id, 3)
Chris@0 1016
Chris@0 1017 assert @project.copy(@source_project)
Chris@0 1018
Chris@0 1019 @project.issues.each do |issue|
Chris@0 1020 assert issue.category
Chris@0 1021 assert_equal "Stock management", issue.category.name # Same name
Chris@0 1022 assert_not_equal IssueCategory.find(3), issue.category # Different record
Chris@0 1023 end
Chris@0 1024 end
Chris@909 1025
Chris@0 1026 should "limit copy with :only option" do
Chris@0 1027 assert @project.members.empty?
Chris@0 1028 assert @project.issue_categories.empty?
Chris@0 1029 assert @source_project.issues.any?
Chris@909 1030
Chris@0 1031 assert @project.copy(@source_project, :only => ['members', 'issue_categories'])
Chris@0 1032
Chris@0 1033 assert @project.members.any?
Chris@0 1034 assert @project.issue_categories.any?
Chris@0 1035 assert @project.issues.empty?
Chris@0 1036 end
Chris@1115 1037 end
Chris@909 1038
Chris@1115 1039 def test_copy_should_copy_subtasks
Chris@1115 1040 source = Project.generate!(:tracker_ids => [1])
Chris@1115 1041 issue = Issue.generate_with_descendants!(:project => source)
Chris@1115 1042 project = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1])
Chris@1115 1043
Chris@1115 1044 assert_difference 'Project.count' do
Chris@1115 1045 assert_difference 'Issue.count', 1+issue.descendants.count do
Chris@1115 1046 assert project.copy(source.reload)
Chris@1115 1047 end
Chris@1115 1048 end
Chris@1115 1049 copy = Issue.where(:parent_id => nil).order("id DESC").first
Chris@1115 1050 assert_equal project, copy.project
Chris@1115 1051 assert_equal issue.descendants.count, copy.descendants.count
Chris@1115 1052 child_copy = copy.children.detect {|c| c.subject == 'Child1'}
Chris@1115 1053 assert child_copy.descendants.any?
Chris@0 1054 end
Chris@0 1055
chris@22 1056 context "#start_date" do
chris@22 1057 setup do
chris@22 1058 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
chris@22 1059 @project = Project.generate!(:identifier => 'test0')
chris@22 1060 @project.trackers << Tracker.generate!
chris@22 1061 end
Chris@909 1062
chris@22 1063 should "be nil if there are no issues on the project" do
chris@22 1064 assert_nil @project.start_date
chris@22 1065 end
Chris@909 1066
chris@37 1067 should "be tested when issues have no start date"
chris@22 1068
chris@22 1069 should "be the earliest start date of it's issues" do
chris@22 1070 early = 7.days.ago.to_date
Chris@1115 1071 Issue.generate!(:project => @project, :start_date => Date.today)
Chris@1115 1072 Issue.generate!(:project => @project, :start_date => early)
chris@22 1073
chris@22 1074 assert_equal early, @project.start_date
chris@22 1075 end
chris@22 1076
chris@22 1077 end
chris@22 1078
chris@22 1079 context "#due_date" do
chris@22 1080 setup do
chris@22 1081 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
chris@22 1082 @project = Project.generate!(:identifier => 'test0')
chris@22 1083 @project.trackers << Tracker.generate!
chris@22 1084 end
Chris@909 1085
chris@22 1086 should "be nil if there are no issues on the project" do
chris@22 1087 assert_nil @project.due_date
chris@22 1088 end
Chris@909 1089
chris@37 1090 should "be tested when issues have no due date"
chris@22 1091
chris@22 1092 should "be the latest due date of it's issues" do
chris@22 1093 future = 7.days.from_now.to_date
Chris@1115 1094 Issue.generate!(:project => @project, :due_date => future)
Chris@1115 1095 Issue.generate!(:project => @project, :due_date => Date.today)
chris@22 1096
chris@22 1097 assert_equal future, @project.due_date
chris@22 1098 end
chris@22 1099
chris@22 1100 should "be the latest due date of it's versions" do
chris@22 1101 future = 7.days.from_now.to_date
chris@22 1102 @project.versions << Version.generate!(:effective_date => future)
chris@22 1103 @project.versions << Version.generate!(:effective_date => Date.today)
Chris@909 1104
chris@22 1105
chris@22 1106 assert_equal future, @project.due_date
chris@22 1107
chris@22 1108 end
chris@22 1109
chris@22 1110 should "pick the latest date from it's issues and versions" do
chris@22 1111 future = 7.days.from_now.to_date
chris@22 1112 far_future = 14.days.from_now.to_date
Chris@1115 1113 Issue.generate!(:project => @project, :due_date => far_future)
chris@22 1114 @project.versions << Version.generate!(:effective_date => future)
Chris@909 1115
chris@22 1116 assert_equal far_future, @project.due_date
chris@22 1117 end
chris@22 1118
chris@22 1119 end
chris@22 1120
chris@22 1121 context "Project#completed_percent" do
chris@22 1122 setup do
chris@22 1123 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
chris@22 1124 @project = Project.generate!(:identifier => 'test0')
chris@22 1125 @project.trackers << Tracker.generate!
chris@22 1126 end
chris@22 1127
chris@22 1128 context "no versions" do
chris@22 1129 should "be 100" do
chris@22 1130 assert_equal 100, @project.completed_percent
chris@22 1131 end
chris@22 1132 end
chris@22 1133
chris@22 1134 context "with versions" do
chris@22 1135 should "return 0 if the versions have no issues" do
chris@22 1136 Version.generate!(:project => @project)
chris@22 1137 Version.generate!(:project => @project)
chris@22 1138
chris@22 1139 assert_equal 0, @project.completed_percent
chris@22 1140 end
chris@22 1141
chris@22 1142 should "return 100 if the version has only closed issues" do
chris@22 1143 v1 = Version.generate!(:project => @project)
Chris@1115 1144 Issue.generate!(:project => @project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v1)
chris@22 1145 v2 = Version.generate!(:project => @project)
Chris@1115 1146 Issue.generate!(:project => @project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v2)
chris@22 1147
chris@22 1148 assert_equal 100, @project.completed_percent
chris@22 1149 end
chris@22 1150
chris@22 1151 should "return the averaged completed percent of the versions (not weighted)" do
chris@22 1152 v1 = Version.generate!(:project => @project)
Chris@1115 1153 Issue.generate!(:project => @project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v1)
chris@22 1154 v2 = Version.generate!(:project => @project)
Chris@1115 1155 Issue.generate!(:project => @project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v2)
chris@22 1156
chris@22 1157 assert_equal 50, @project.completed_percent
chris@22 1158 end
chris@22 1159
chris@22 1160 end
chris@22 1161 end
chris@37 1162
chris@37 1163 context "#notified_users" do
chris@37 1164 setup do
chris@37 1165 @project = Project.generate!
chris@37 1166 @role = Role.generate!
Chris@909 1167
chris@37 1168 @user_with_membership_notification = User.generate!(:mail_notification => 'selected')
Chris@1115 1169 Member.create!(:project => @project, :roles => [@role], :principal => @user_with_membership_notification, :mail_notification => true)
chris@37 1170
chris@37 1171 @all_events_user = User.generate!(:mail_notification => 'all')
Chris@1115 1172 Member.create!(:project => @project, :roles => [@role], :principal => @all_events_user)
chris@37 1173
chris@37 1174 @no_events_user = User.generate!(:mail_notification => 'none')
Chris@1115 1175 Member.create!(:project => @project, :roles => [@role], :principal => @no_events_user)
chris@37 1176
chris@37 1177 @only_my_events_user = User.generate!(:mail_notification => 'only_my_events')
Chris@1115 1178 Member.create!(:project => @project, :roles => [@role], :principal => @only_my_events_user)
chris@37 1179
chris@37 1180 @only_assigned_user = User.generate!(:mail_notification => 'only_assigned')
Chris@1115 1181 Member.create!(:project => @project, :roles => [@role], :principal => @only_assigned_user)
chris@37 1182
chris@37 1183 @only_owned_user = User.generate!(:mail_notification => 'only_owner')
Chris@1115 1184 Member.create!(:project => @project, :roles => [@role], :principal => @only_owned_user)
chris@37 1185 end
Chris@909 1186
chris@37 1187 should "include members with a mail notification" do
chris@37 1188 assert @project.notified_users.include?(@user_with_membership_notification)
chris@37 1189 end
Chris@909 1190
chris@37 1191 should "include users with the 'all' notification option" do
chris@37 1192 assert @project.notified_users.include?(@all_events_user)
chris@37 1193 end
Chris@909 1194
chris@37 1195 should "not include users with the 'none' notification option" do
chris@37 1196 assert !@project.notified_users.include?(@no_events_user)
chris@37 1197 end
Chris@909 1198
chris@37 1199 should "not include users with the 'only_my_events' notification option" do
chris@37 1200 assert !@project.notified_users.include?(@only_my_events_user)
chris@37 1201 end
Chris@909 1202
chris@37 1203 should "not include users with the 'only_assigned' notification option" do
chris@37 1204 assert !@project.notified_users.include?(@only_assigned_user)
chris@37 1205 end
Chris@909 1206
chris@37 1207 should "not include users with the 'only_owner' notification option" do
chris@37 1208 assert !@project.notified_users.include?(@only_owned_user)
chris@37 1209 end
chris@37 1210 end
Chris@909 1211
Chris@0 1212 end