annotate .svn/pristine/11/11c49a871d148b89615d8c37f2abca7b6149921b.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 IssueNestedSetTest < ActiveSupport::TestCase
Chris@1296 21 fixtures :projects, :users, :members, :member_roles, :roles,
Chris@1296 22 :trackers, :projects_trackers,
Chris@1296 23 :versions,
Chris@1296 24 :issue_statuses, :issue_categories, :issue_relations, :workflows,
Chris@1296 25 :enumerations,
Chris@1296 26 :issues,
Chris@1296 27 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
Chris@1296 28 :time_entries
Chris@1296 29
Chris@1296 30 def test_create_root_issue
Chris@1296 31 issue1 = Issue.generate!
Chris@1296 32 issue2 = Issue.generate!
Chris@1296 33 issue1.reload
Chris@1296 34 issue2.reload
Chris@1296 35
Chris@1296 36 assert_equal [issue1.id, nil, 1, 2], [issue1.root_id, issue1.parent_id, issue1.lft, issue1.rgt]
Chris@1296 37 assert_equal [issue2.id, nil, 1, 2], [issue2.root_id, issue2.parent_id, issue2.lft, issue2.rgt]
Chris@1296 38 end
Chris@1296 39
Chris@1296 40 def test_create_child_issue
Chris@1296 41 parent = Issue.generate!
Chris@1296 42 child = Issue.generate!(:parent_issue_id => parent.id)
Chris@1296 43 parent.reload
Chris@1296 44 child.reload
Chris@1296 45
Chris@1296 46 assert_equal [parent.id, nil, 1, 4], [parent.root_id, parent.parent_id, parent.lft, parent.rgt]
Chris@1296 47 assert_equal [parent.id, parent.id, 2, 3], [child.root_id, child.parent_id, child.lft, child.rgt]
Chris@1296 48 end
Chris@1296 49
Chris@1296 50 def test_creating_a_child_in_a_subproject_should_validate
Chris@1296 51 issue = Issue.generate!
Chris@1296 52 child = Issue.new(:project_id => 3, :tracker_id => 2, :author_id => 1,
Chris@1296 53 :subject => 'child', :parent_issue_id => issue.id)
Chris@1296 54 assert_save child
Chris@1296 55 assert_equal issue, child.reload.parent
Chris@1296 56 end
Chris@1296 57
Chris@1296 58 def test_creating_a_child_in_an_invalid_project_should_not_validate
Chris@1296 59 issue = Issue.generate!
Chris@1296 60 child = Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
Chris@1296 61 :subject => 'child', :parent_issue_id => issue.id)
Chris@1296 62 assert !child.save
Chris@1296 63 assert_not_nil child.errors[:parent_issue_id]
Chris@1296 64 end
Chris@1296 65
Chris@1296 66 def test_move_a_root_to_child
Chris@1296 67 parent1 = Issue.generate!
Chris@1296 68 parent2 = Issue.generate!
Chris@1296 69 child = Issue.generate!(:parent_issue_id => parent1.id)
Chris@1296 70
Chris@1296 71 parent2.parent_issue_id = parent1.id
Chris@1296 72 parent2.save!
Chris@1296 73 child.reload
Chris@1296 74 parent1.reload
Chris@1296 75 parent2.reload
Chris@1296 76
Chris@1296 77 assert_equal [parent1.id, 1, 6], [parent1.root_id, parent1.lft, parent1.rgt]
Chris@1296 78 assert_equal [parent1.id, 4, 5], [parent2.root_id, parent2.lft, parent2.rgt]
Chris@1296 79 assert_equal [parent1.id, 2, 3], [child.root_id, child.lft, child.rgt]
Chris@1296 80 end
Chris@1296 81
Chris@1296 82 def test_move_a_child_to_root
Chris@1296 83 parent1 = Issue.generate!
Chris@1296 84 parent2 = Issue.generate!
Chris@1296 85 child = Issue.generate!(:parent_issue_id => parent1.id)
Chris@1296 86
Chris@1296 87 child.parent_issue_id = nil
Chris@1296 88 child.save!
Chris@1296 89 child.reload
Chris@1296 90 parent1.reload
Chris@1296 91 parent2.reload
Chris@1296 92
Chris@1296 93 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
Chris@1296 94 assert_equal [parent2.id, 1, 2], [parent2.root_id, parent2.lft, parent2.rgt]
Chris@1296 95 assert_equal [child.id, 1, 2], [child.root_id, child.lft, child.rgt]
Chris@1296 96 end
Chris@1296 97
Chris@1296 98 def test_move_a_child_to_another_issue
Chris@1296 99 parent1 = Issue.generate!
Chris@1296 100 parent2 = Issue.generate!
Chris@1296 101 child = Issue.generate!(:parent_issue_id => parent1.id)
Chris@1296 102
Chris@1296 103 child.parent_issue_id = parent2.id
Chris@1296 104 child.save!
Chris@1296 105 child.reload
Chris@1296 106 parent1.reload
Chris@1296 107 parent2.reload
Chris@1296 108
Chris@1296 109 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
Chris@1296 110 assert_equal [parent2.id, 1, 4], [parent2.root_id, parent2.lft, parent2.rgt]
Chris@1296 111 assert_equal [parent2.id, 2, 3], [child.root_id, child.lft, child.rgt]
Chris@1296 112 end
Chris@1296 113
Chris@1296 114 def test_move_a_child_with_descendants_to_another_issue
Chris@1296 115 parent1 = Issue.generate!
Chris@1296 116 parent2 = Issue.generate!
Chris@1296 117 child = Issue.generate!(:parent_issue_id => parent1.id)
Chris@1296 118 grandchild = Issue.generate!(:parent_issue_id => child.id)
Chris@1296 119
Chris@1296 120 parent1.reload
Chris@1296 121 parent2.reload
Chris@1296 122 child.reload
Chris@1296 123 grandchild.reload
Chris@1296 124
Chris@1296 125 assert_equal [parent1.id, 1, 6], [parent1.root_id, parent1.lft, parent1.rgt]
Chris@1296 126 assert_equal [parent2.id, 1, 2], [parent2.root_id, parent2.lft, parent2.rgt]
Chris@1296 127 assert_equal [parent1.id, 2, 5], [child.root_id, child.lft, child.rgt]
Chris@1296 128 assert_equal [parent1.id, 3, 4], [grandchild.root_id, grandchild.lft, grandchild.rgt]
Chris@1296 129
Chris@1296 130 child.reload.parent_issue_id = parent2.id
Chris@1296 131 child.save!
Chris@1296 132 child.reload
Chris@1296 133 grandchild.reload
Chris@1296 134 parent1.reload
Chris@1296 135 parent2.reload
Chris@1296 136
Chris@1296 137 assert_equal [parent1.id, 1, 2], [parent1.root_id, parent1.lft, parent1.rgt]
Chris@1296 138 assert_equal [parent2.id, 1, 6], [parent2.root_id, parent2.lft, parent2.rgt]
Chris@1296 139 assert_equal [parent2.id, 2, 5], [child.root_id, child.lft, child.rgt]
Chris@1296 140 assert_equal [parent2.id, 3, 4], [grandchild.root_id, grandchild.lft, grandchild.rgt]
Chris@1296 141 end
Chris@1296 142
Chris@1296 143 def test_move_a_child_with_descendants_to_another_project
Chris@1296 144 parent1 = Issue.generate!
Chris@1296 145 child = Issue.generate!(:parent_issue_id => parent1.id)
Chris@1296 146 grandchild = Issue.generate!(:parent_issue_id => child.id)
Chris@1296 147
Chris@1296 148 child.reload
Chris@1296 149 child.project = Project.find(2)
Chris@1296 150 assert child.save
Chris@1296 151 child.reload
Chris@1296 152 grandchild.reload
Chris@1296 153 parent1.reload
Chris@1296 154
Chris@1296 155 assert_equal [1, parent1.id, 1, 2], [parent1.project_id, parent1.root_id, parent1.lft, parent1.rgt]
Chris@1296 156 assert_equal [2, child.id, 1, 4], [child.project_id, child.root_id, child.lft, child.rgt]
Chris@1296 157 assert_equal [2, child.id, 2, 3], [grandchild.project_id, grandchild.root_id, grandchild.lft, grandchild.rgt]
Chris@1296 158 end
Chris@1296 159
Chris@1296 160 def test_moving_an_issue_to_a_descendant_should_not_validate
Chris@1296 161 parent1 = Issue.generate!
Chris@1296 162 parent2 = Issue.generate!
Chris@1296 163 child = Issue.generate!(:parent_issue_id => parent1.id)
Chris@1296 164 grandchild = Issue.generate!(:parent_issue_id => child.id)
Chris@1296 165
Chris@1296 166 child.reload
Chris@1296 167 child.parent_issue_id = grandchild.id
Chris@1296 168 assert !child.save
Chris@1296 169 assert_not_nil child.errors[:parent_issue_id]
Chris@1296 170 end
Chris@1296 171
Chris@1296 172 def test_moving_an_issue_should_keep_valid_relations_only
Chris@1296 173 issue1 = Issue.generate!
Chris@1296 174 issue2 = Issue.generate!
Chris@1296 175 issue3 = Issue.generate!(:parent_issue_id => issue2.id)
Chris@1296 176 issue4 = Issue.generate!
Chris@1296 177 r1 = IssueRelation.create!(:issue_from => issue1, :issue_to => issue2, :relation_type => IssueRelation::TYPE_PRECEDES)
Chris@1296 178 r2 = IssueRelation.create!(:issue_from => issue1, :issue_to => issue3, :relation_type => IssueRelation::TYPE_PRECEDES)
Chris@1296 179 r3 = IssueRelation.create!(:issue_from => issue2, :issue_to => issue4, :relation_type => IssueRelation::TYPE_PRECEDES)
Chris@1296 180 issue2.reload
Chris@1296 181 issue2.parent_issue_id = issue1.id
Chris@1296 182 issue2.save!
Chris@1296 183 assert !IssueRelation.exists?(r1.id)
Chris@1296 184 assert !IssueRelation.exists?(r2.id)
Chris@1296 185 assert IssueRelation.exists?(r3.id)
Chris@1296 186 end
Chris@1296 187
Chris@1296 188 def test_destroy_should_destroy_children
Chris@1296 189 issue1 = Issue.generate!
Chris@1296 190 issue2 = Issue.generate!
Chris@1296 191 issue3 = Issue.generate!(:parent_issue_id => issue2.id)
Chris@1296 192 issue4 = Issue.generate!(:parent_issue_id => issue1.id)
Chris@1296 193
Chris@1296 194 issue3.init_journal(User.find(2))
Chris@1296 195 issue3.subject = 'child with journal'
Chris@1296 196 issue3.save!
Chris@1296 197
Chris@1296 198 assert_difference 'Issue.count', -2 do
Chris@1296 199 assert_difference 'Journal.count', -1 do
Chris@1296 200 assert_difference 'JournalDetail.count', -1 do
Chris@1296 201 Issue.find(issue2.id).destroy
Chris@1296 202 end
Chris@1296 203 end
Chris@1296 204 end
Chris@1296 205
Chris@1296 206 issue1.reload
Chris@1296 207 issue4.reload
Chris@1296 208 assert !Issue.exists?(issue2.id)
Chris@1296 209 assert !Issue.exists?(issue3.id)
Chris@1296 210 assert_equal [issue1.id, 1, 4], [issue1.root_id, issue1.lft, issue1.rgt]
Chris@1296 211 assert_equal [issue1.id, 2, 3], [issue4.root_id, issue4.lft, issue4.rgt]
Chris@1296 212 end
Chris@1296 213
Chris@1296 214 def test_destroy_child_should_update_parent
Chris@1296 215 issue = Issue.generate!
Chris@1296 216 child1 = Issue.generate!(:parent_issue_id => issue.id)
Chris@1296 217 child2 = Issue.generate!(:parent_issue_id => issue.id)
Chris@1296 218
Chris@1296 219 issue.reload
Chris@1296 220 assert_equal [issue.id, 1, 6], [issue.root_id, issue.lft, issue.rgt]
Chris@1296 221
Chris@1296 222 child2.reload.destroy
Chris@1296 223
Chris@1296 224 issue.reload
Chris@1296 225 assert_equal [issue.id, 1, 4], [issue.root_id, issue.lft, issue.rgt]
Chris@1296 226 end
Chris@1296 227
Chris@1296 228 def test_destroy_parent_issue_updated_during_children_destroy
Chris@1296 229 parent = Issue.generate!
Chris@1296 230 Issue.generate!(:start_date => Date.today, :parent_issue_id => parent.id)
Chris@1296 231 Issue.generate!(:start_date => 2.days.from_now, :parent_issue_id => parent.id)
Chris@1296 232
Chris@1296 233 assert_difference 'Issue.count', -3 do
Chris@1296 234 Issue.find(parent.id).destroy
Chris@1296 235 end
Chris@1296 236 end
Chris@1296 237
Chris@1296 238 def test_destroy_child_issue_with_children
Chris@1296 239 root = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'root')
Chris@1296 240 child = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => root.id)
Chris@1296 241 leaf = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'leaf', :parent_issue_id => child.id)
Chris@1296 242 leaf.init_journal(User.find(2))
Chris@1296 243 leaf.subject = 'leaf with journal'
Chris@1296 244 leaf.save!
Chris@1296 245
Chris@1296 246 assert_difference 'Issue.count', -2 do
Chris@1296 247 assert_difference 'Journal.count', -1 do
Chris@1296 248 assert_difference 'JournalDetail.count', -1 do
Chris@1296 249 Issue.find(child.id).destroy
Chris@1296 250 end
Chris@1296 251 end
Chris@1296 252 end
Chris@1296 253
Chris@1296 254 root = Issue.find(root.id)
Chris@1296 255 assert root.leaf?, "Root issue is not a leaf (lft: #{root.lft}, rgt: #{root.rgt})"
Chris@1296 256 end
Chris@1296 257
Chris@1296 258 def test_destroy_issue_with_grand_child
Chris@1296 259 parent = Issue.generate!
Chris@1296 260 issue = Issue.generate!(:parent_issue_id => parent.id)
Chris@1296 261 child = Issue.generate!(:parent_issue_id => issue.id)
Chris@1296 262 grandchild1 = Issue.generate!(:parent_issue_id => child.id)
Chris@1296 263 grandchild2 = Issue.generate!(:parent_issue_id => child.id)
Chris@1296 264
Chris@1296 265 assert_difference 'Issue.count', -4 do
Chris@1296 266 Issue.find(issue.id).destroy
Chris@1296 267 parent.reload
Chris@1296 268 assert_equal [1, 2], [parent.lft, parent.rgt]
Chris@1296 269 end
Chris@1296 270 end
Chris@1296 271
Chris@1296 272 def test_parent_priority_should_be_the_highest_child_priority
Chris@1296 273 parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal'))
Chris@1296 274 # Create children
Chris@1296 275 child1 = Issue.generate!(:priority => IssuePriority.find_by_name('High'), :parent_issue_id => parent.id)
Chris@1296 276 assert_equal 'High', parent.reload.priority.name
Chris@1296 277 child2 = Issue.generate!(:priority => IssuePriority.find_by_name('Immediate'), :parent_issue_id => child1.id)
Chris@1296 278 assert_equal 'Immediate', child1.reload.priority.name
Chris@1296 279 assert_equal 'Immediate', parent.reload.priority.name
Chris@1296 280 child3 = Issue.generate!(:priority => IssuePriority.find_by_name('Low'), :parent_issue_id => parent.id)
Chris@1296 281 assert_equal 'Immediate', parent.reload.priority.name
Chris@1296 282 # Destroy a child
Chris@1296 283 child1.destroy
Chris@1296 284 assert_equal 'Low', parent.reload.priority.name
Chris@1296 285 # Update a child
Chris@1296 286 child3.reload.priority = IssuePriority.find_by_name('Normal')
Chris@1296 287 child3.save!
Chris@1296 288 assert_equal 'Normal', parent.reload.priority.name
Chris@1296 289 end
Chris@1296 290
Chris@1296 291 def test_parent_dates_should_be_lowest_start_and_highest_due_dates
Chris@1296 292 parent = Issue.generate!
Chris@1296 293 Issue.generate!(:start_date => '2010-01-25', :due_date => '2010-02-15', :parent_issue_id => parent.id)
Chris@1296 294 Issue.generate!( :due_date => '2010-02-13', :parent_issue_id => parent.id)
Chris@1296 295 Issue.generate!(:start_date => '2010-02-01', :due_date => '2010-02-22', :parent_issue_id => parent.id)
Chris@1296 296 parent.reload
Chris@1296 297 assert_equal Date.parse('2010-01-25'), parent.start_date
Chris@1296 298 assert_equal Date.parse('2010-02-22'), parent.due_date
Chris@1296 299 end
Chris@1296 300
Chris@1296 301 def test_parent_done_ratio_should_be_average_done_ratio_of_leaves
Chris@1296 302 parent = Issue.generate!
Chris@1296 303 Issue.generate!(:done_ratio => 20, :parent_issue_id => parent.id)
Chris@1296 304 assert_equal 20, parent.reload.done_ratio
Chris@1296 305 Issue.generate!(:done_ratio => 70, :parent_issue_id => parent.id)
Chris@1296 306 assert_equal 45, parent.reload.done_ratio
Chris@1296 307
Chris@1296 308 child = Issue.generate!(:done_ratio => 0, :parent_issue_id => parent.id)
Chris@1296 309 assert_equal 30, parent.reload.done_ratio
Chris@1296 310
Chris@1296 311 Issue.generate!(:done_ratio => 30, :parent_issue_id => child.id)
Chris@1296 312 assert_equal 30, child.reload.done_ratio
Chris@1296 313 assert_equal 40, parent.reload.done_ratio
Chris@1296 314 end
Chris@1296 315
Chris@1296 316 def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any
Chris@1296 317 parent = Issue.generate!
Chris@1296 318 Issue.generate!(:estimated_hours => 10, :done_ratio => 20, :parent_issue_id => parent.id)
Chris@1296 319 assert_equal 20, parent.reload.done_ratio
Chris@1296 320 Issue.generate!(:estimated_hours => 20, :done_ratio => 50, :parent_issue_id => parent.id)
Chris@1296 321 assert_equal (50 * 20 + 20 * 10) / 30, parent.reload.done_ratio
Chris@1296 322 end
Chris@1296 323
Chris@1296 324 def test_parent_estimate_should_be_sum_of_leaves
Chris@1296 325 parent = Issue.generate!
Chris@1296 326 Issue.generate!(:estimated_hours => nil, :parent_issue_id => parent.id)
Chris@1296 327 assert_equal nil, parent.reload.estimated_hours
Chris@1296 328 Issue.generate!(:estimated_hours => 5, :parent_issue_id => parent.id)
Chris@1296 329 assert_equal 5, parent.reload.estimated_hours
Chris@1296 330 Issue.generate!(:estimated_hours => 7, :parent_issue_id => parent.id)
Chris@1296 331 assert_equal 12, parent.reload.estimated_hours
Chris@1296 332 end
Chris@1296 333
Chris@1296 334 def test_move_parent_updates_old_parent_attributes
Chris@1296 335 first_parent = Issue.generate!
Chris@1296 336 second_parent = Issue.generate!
Chris@1296 337 child = Issue.generate!(:estimated_hours => 5, :parent_issue_id => first_parent.id)
Chris@1296 338 assert_equal 5, first_parent.reload.estimated_hours
Chris@1296 339 child.update_attributes(:estimated_hours => 7, :parent_issue_id => second_parent.id)
Chris@1296 340 assert_equal 7, second_parent.reload.estimated_hours
Chris@1296 341 assert_nil first_parent.reload.estimated_hours
Chris@1296 342 end
Chris@1296 343
Chris@1296 344 def test_reschuling_a_parent_should_reschedule_subtasks
Chris@1296 345 parent = Issue.generate!
Chris@1296 346 c1 = Issue.generate!(:start_date => '2010-05-12', :due_date => '2010-05-18', :parent_issue_id => parent.id)
Chris@1296 347 c2 = Issue.generate!(:start_date => '2010-06-03', :due_date => '2010-06-10', :parent_issue_id => parent.id)
Chris@1296 348 parent.reload
Chris@1296 349 parent.reschedule_on!(Date.parse('2010-06-02'))
Chris@1296 350 c1.reload
Chris@1296 351 assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-08')], [c1.start_date, c1.due_date]
Chris@1296 352 c2.reload
Chris@1296 353 assert_equal [Date.parse('2010-06-03'), Date.parse('2010-06-10')], [c2.start_date, c2.due_date] # no change
Chris@1296 354 parent.reload
Chris@1296 355 assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-10')], [parent.start_date, parent.due_date]
Chris@1296 356 end
Chris@1296 357
Chris@1296 358 def test_project_copy_should_copy_issue_tree
Chris@1296 359 p = Project.create!(:name => 'Tree copy', :identifier => 'tree-copy', :tracker_ids => [1, 2])
Chris@1296 360 i1 = Issue.generate!(:project => p, :subject => 'i1')
Chris@1296 361 i2 = Issue.generate!(:project => p, :subject => 'i2', :parent_issue_id => i1.id)
Chris@1296 362 i3 = Issue.generate!(:project => p, :subject => 'i3', :parent_issue_id => i1.id)
Chris@1296 363 i4 = Issue.generate!(:project => p, :subject => 'i4', :parent_issue_id => i2.id)
Chris@1296 364 i5 = Issue.generate!(:project => p, :subject => 'i5')
Chris@1296 365 c = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1, 2])
Chris@1296 366 c.copy(p, :only => 'issues')
Chris@1296 367 c.reload
Chris@1296 368
Chris@1296 369 assert_equal 5, c.issues.count
Chris@1296 370 ic1, ic2, ic3, ic4, ic5 = c.issues.find(:all, :order => 'subject')
Chris@1296 371 assert ic1.root?
Chris@1296 372 assert_equal ic1, ic2.parent
Chris@1296 373 assert_equal ic1, ic3.parent
Chris@1296 374 assert_equal ic2, ic4.parent
Chris@1296 375 assert ic5.root?
Chris@1296 376 end
Chris@1296 377 end