annotate .svn/pristine/93/93329f36cebf7e32386d681edc115e3830ef3012.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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