annotate .svn/pristine/93/93329f36cebf7e32386d681edc115e3830ef3012.svn-base @ 1295:622f24f53b42 redmine-2.3

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