| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2013-01-12 09:29:31 +00:00
										 |  |  | # Copyright (C) 2006-2013  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-12 23:24:34 +00:00
										 |  |  | require File.expand_path('../../test_helper', __FILE__) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class IssueNestedSetTest < ActiveSupport::TestCase | 
					
						
							| 
									
										
										
										
											2013-02-03 14:27:19 +00:00
										 |  |  |   fixtures :projects, :users, :roles, | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |            :trackers, :projects_trackers, | 
					
						
							| 
									
										
										
										
											2013-02-03 14:27:19 +00:00
										 |  |  |            :issue_statuses, :issue_categories, :issue_relations, | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |            :enumerations, | 
					
						
							| 
									
										
										
										
											2013-02-03 14:27:19 +00:00
										 |  |  |            :issues | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 17:54:47 +00:00
										 |  |  |   def test_new_record_is_leaf | 
					
						
							|  |  |  |     i = Issue.new | 
					
						
							|  |  |  |     assert i.leaf? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_create_root_issue | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft1 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     issue1 = Issue.generate! | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft2 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     issue2 = Issue.generate! | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     issue1.reload | 
					
						
							|  |  |  |     issue2.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [issue1.id, nil, lft1, lft1 + 1], [issue1.root_id, issue1.parent_id, issue1.lft, issue1.rgt] | 
					
						
							|  |  |  |     assert_equal [issue2.id, nil, lft2, lft2 + 1], [issue2.root_id, issue2.parent_id, issue2.lft, issue2.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_create_child_issue | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     child =  Issue.generate!(:parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     parent.reload | 
					
						
							|  |  |  |     child.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [parent.id, nil,       lft,     lft + 3], [parent.root_id, parent.parent_id, parent.lft, parent.rgt] | 
					
						
							|  |  |  |     assert_equal [parent.id, parent.id, lft + 1, lft + 2], [child.root_id, child.parent_id, child.lft, child.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-10 17:38:17 +00:00
										 |  |  |   def test_creating_a_child_in_a_subproject_should_validate | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     issue = Issue.generate! | 
					
						
							| 
									
										
										
										
											2012-10-10 17:38:17 +00:00
										 |  |  |     child = Issue.new(:project_id => 3, :tracker_id => 2, :author_id => 1, | 
					
						
							|  |  |  |                       :subject => 'child', :parent_issue_id => issue.id) | 
					
						
							|  |  |  |     assert_save child | 
					
						
							|  |  |  |     assert_equal issue, child.reload.parent | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_creating_a_child_in_an_invalid_project_should_not_validate | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     issue = Issue.generate! | 
					
						
							| 
									
										
										
										
											2011-10-07 04:17:47 +00:00
										 |  |  |     child = Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1, | 
					
						
							|  |  |  |                       :subject => 'child', :parent_issue_id => issue.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert !child.save | 
					
						
							| 
									
										
										
										
											2013-07-30 08:23:54 +00:00
										 |  |  |     assert_not_equal [], child.errors[:parent_issue_id] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_move_a_root_to_child | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent1 = Issue.generate! | 
					
						
							|  |  |  |     parent2 = Issue.generate! | 
					
						
							|  |  |  |     child = Issue.generate!(:parent_issue_id => parent1.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     parent2.parent_issue_id = parent1.id | 
					
						
							|  |  |  |     parent2.save! | 
					
						
							|  |  |  |     child.reload | 
					
						
							|  |  |  |     parent1.reload | 
					
						
							|  |  |  |     parent2.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [parent1.id, lft,     lft + 5], [parent1.root_id, parent1.lft, parent1.rgt] | 
					
						
							|  |  |  |     assert_equal [parent1.id, lft + 3, lft + 4], [parent2.root_id, parent2.lft, parent2.rgt] | 
					
						
							|  |  |  |     assert_equal [parent1.id, lft + 1, lft + 2], [child.root_id, child.lft, child.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_move_a_child_to_root | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft1 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent1 = Issue.generate! | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft2 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent2 = Issue.generate! | 
					
						
							|  |  |  |     child =   Issue.generate!(:parent_issue_id => parent1.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     child.parent_issue_id = nil | 
					
						
							|  |  |  |     child.save! | 
					
						
							|  |  |  |     child.reload | 
					
						
							|  |  |  |     parent1.reload | 
					
						
							|  |  |  |     parent2.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [parent1.id, lft1, lft1 + 1], [parent1.root_id, parent1.lft, parent1.rgt] | 
					
						
							|  |  |  |     assert_equal [parent2.id, lft2, lft2 + 1], [parent2.root_id, parent2.lft, parent2.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal [child.id, 1, 2], [child.root_id, child.lft, child.rgt] | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_move_a_child_to_another_issue | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft1 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent1 = Issue.generate! | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft2 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent2 = Issue.generate! | 
					
						
							|  |  |  |     child =   Issue.generate!(:parent_issue_id => parent1.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     child.parent_issue_id = parent2.id | 
					
						
							|  |  |  |     child.save! | 
					
						
							|  |  |  |     child.reload | 
					
						
							|  |  |  |     parent1.reload | 
					
						
							|  |  |  |     parent2.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [parent1.id, lft1,     lft1 + 1], [parent1.root_id, parent1.lft, parent1.rgt] | 
					
						
							|  |  |  |     assert_equal [parent2.id, lft2,     lft2 + 3], [parent2.root_id, parent2.lft, parent2.rgt] | 
					
						
							|  |  |  |     assert_equal [parent2.id, lft2 + 1, lft2 + 2], [child.root_id,   child.lft,   child.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_move_a_child_with_descendants_to_another_issue | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft1 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent1 = Issue.generate! | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft2 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent2 = Issue.generate! | 
					
						
							|  |  |  |     child =   Issue.generate!(:parent_issue_id => parent1.id) | 
					
						
							|  |  |  |     grandchild = Issue.generate!(:parent_issue_id => child.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     parent1.reload | 
					
						
							|  |  |  |     parent2.reload | 
					
						
							|  |  |  |     child.reload | 
					
						
							|  |  |  |     grandchild.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [parent1.id, lft1,     lft1 + 5], [parent1.root_id, parent1.lft, parent1.rgt] | 
					
						
							|  |  |  |     assert_equal [parent2.id, lft2,     lft2 + 1], [parent2.root_id, parent2.lft, parent2.rgt] | 
					
						
							|  |  |  |     assert_equal [parent1.id, lft1 + 1, lft1 + 4], [child.root_id, child.lft, child.rgt] | 
					
						
							|  |  |  |     assert_equal [parent1.id, lft1 + 2, lft1 + 3], [grandchild.root_id, grandchild.lft, grandchild.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     child.reload.parent_issue_id = parent2.id | 
					
						
							|  |  |  |     child.save! | 
					
						
							|  |  |  |     child.reload | 
					
						
							|  |  |  |     grandchild.reload | 
					
						
							|  |  |  |     parent1.reload | 
					
						
							|  |  |  |     parent2.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [parent1.id, lft1,     lft1 + 1], [parent1.root_id, parent1.lft, parent1.rgt] | 
					
						
							|  |  |  |     assert_equal [parent2.id, lft2,     lft2 + 5], [parent2.root_id, parent2.lft, parent2.rgt] | 
					
						
							|  |  |  |     assert_equal [parent2.id, lft2 + 1, lft2 + 4], [child.root_id, child.lft, child.rgt] | 
					
						
							|  |  |  |     assert_equal [parent2.id, lft2 + 2, lft2 + 3], [grandchild.root_id, grandchild.lft, grandchild.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_move_a_child_with_descendants_to_another_project | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft1 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent1 = Issue.generate! | 
					
						
							|  |  |  |     child =   Issue.generate!(:parent_issue_id => parent1.id) | 
					
						
							|  |  |  |     grandchild = Issue.generate!(:parent_issue_id => child.id) | 
					
						
							| 
									
										
										
										
											2012-01-07 18:02:02 +00:00
										 |  |  |     child.reload | 
					
						
							|  |  |  |     child.project = Project.find(2) | 
					
						
							|  |  |  |     assert child.save | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     child.reload | 
					
						
							|  |  |  |     grandchild.reload | 
					
						
							|  |  |  |     parent1.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [1, parent1.id, lft1, lft1 + 1], [parent1.project_id, parent1.root_id, parent1.lft, parent1.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal [2, child.id, 1, 4], [child.project_id, child.root_id, child.lft, child.rgt] | 
					
						
							|  |  |  |     assert_equal [2, child.id, 2, 3], [grandchild.project_id, grandchild.root_id, grandchild.lft, grandchild.rgt] | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_moving_an_issue_to_a_descendant_should_not_validate | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent1 = Issue.generate! | 
					
						
							|  |  |  |     parent2 = Issue.generate! | 
					
						
							|  |  |  |     child =   Issue.generate!(:parent_issue_id => parent1.id) | 
					
						
							|  |  |  |     grandchild = Issue.generate!(:parent_issue_id => child.id) | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     child.reload | 
					
						
							|  |  |  |     child.parent_issue_id = grandchild.id | 
					
						
							|  |  |  |     assert !child.save | 
					
						
							| 
									
										
										
										
											2013-07-30 08:23:54 +00:00
										 |  |  |     assert_not_equal [], child.errors[:parent_issue_id] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 09:25:14 +00:00
										 |  |  |   def test_updating_a_root_issue_should_not_trigger_update_nested_set_attributes_on_parent_change | 
					
						
							|  |  |  |     issue = Issue.find(Issue.generate!.id) | 
					
						
							|  |  |  |     issue.parent_issue_id = "" | 
					
						
							|  |  |  |     issue.expects(:update_nested_set_attributes_on_parent_change).never | 
					
						
							|  |  |  |     issue.save! | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_updating_a_child_issue_should_not_trigger_update_nested_set_attributes_on_parent_change | 
					
						
							|  |  |  |     issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id) | 
					
						
							|  |  |  |     issue.parent_issue_id = "1" | 
					
						
							|  |  |  |     issue.expects(:update_nested_set_attributes_on_parent_change).never | 
					
						
							|  |  |  |     issue.save! | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_moving_a_root_issue_should_trigger_update_nested_set_attributes_on_parent_change | 
					
						
							|  |  |  |     issue = Issue.find(Issue.generate!.id) | 
					
						
							|  |  |  |     issue.parent_issue_id = "1" | 
					
						
							|  |  |  |     issue.expects(:update_nested_set_attributes_on_parent_change).once | 
					
						
							|  |  |  |     issue.save! | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_moving_a_child_issue_to_another_parent_should_trigger_update_nested_set_attributes_on_parent_change | 
					
						
							|  |  |  |     issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id) | 
					
						
							|  |  |  |     issue.parent_issue_id = "2" | 
					
						
							|  |  |  |     issue.expects(:update_nested_set_attributes_on_parent_change).once | 
					
						
							|  |  |  |     issue.save! | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_moving_a_child_issue_to_root_should_trigger_update_nested_set_attributes_on_parent_change | 
					
						
							|  |  |  |     issue = Issue.find(Issue.generate!(:parent_issue_id => 1).id) | 
					
						
							|  |  |  |     issue.parent_issue_id = "" | 
					
						
							|  |  |  |     issue.expects(:update_nested_set_attributes_on_parent_change).once | 
					
						
							|  |  |  |     issue.save! | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_destroy_should_destroy_children | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft1 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     issue1 = Issue.generate! | 
					
						
							|  |  |  |     issue2 = Issue.generate! | 
					
						
							|  |  |  |     issue3 = Issue.generate!(:parent_issue_id => issue2.id) | 
					
						
							|  |  |  |     issue4 = Issue.generate!(:parent_issue_id => issue1.id) | 
					
						
							| 
									
										
										
										
											2011-01-22 11:46:15 +00:00
										 |  |  |     issue3.init_journal(User.find(2)) | 
					
						
							|  |  |  |     issue3.subject = 'child with journal' | 
					
						
							|  |  |  |     issue3.save! | 
					
						
							|  |  |  |     assert_difference 'Issue.count', -2 do | 
					
						
							|  |  |  |       assert_difference 'Journal.count', -1 do | 
					
						
							|  |  |  |         assert_difference 'JournalDetail.count', -1 do | 
					
						
							|  |  |  |           Issue.find(issue2.id).destroy | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     issue1.reload | 
					
						
							|  |  |  |     issue4.reload | 
					
						
							|  |  |  |     assert !Issue.exists?(issue2.id) | 
					
						
							|  |  |  |     assert !Issue.exists?(issue3.id) | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [issue1.id, lft1,     lft1 + 3], [issue1.root_id, issue1.lft, issue1.rgt] | 
					
						
							|  |  |  |     assert_equal [issue1.id, lft1 + 1, lft1 + 2], [issue4.root_id, issue4.lft, issue4.rgt] | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-15 19:55:21 +00:00
										 |  |  |   def test_destroy_child_should_update_parent | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft1 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     issue = Issue.generate! | 
					
						
							|  |  |  |     child1 = Issue.generate!(:parent_issue_id => issue.id) | 
					
						
							|  |  |  |     child2 = Issue.generate!(:parent_issue_id => issue.id) | 
					
						
							| 
									
										
										
										
											2011-11-15 19:55:21 +00:00
										 |  |  |     issue.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [issue.id, lft1, lft1 + 5], [issue.root_id, issue.lft, issue.rgt] | 
					
						
							| 
									
										
										
										
											2011-11-15 19:55:21 +00:00
										 |  |  |     child2.reload.destroy | 
					
						
							|  |  |  |     issue.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     assert_equal [issue.id, lft1, lft1 + 3], [issue.root_id, issue.lft, issue.rgt] | 
					
						
							| 
									
										
										
										
											2011-11-15 19:55:21 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-01 18:42:41 +00:00
										 |  |  |   def test_destroy_parent_issue_updated_during_children_destroy | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     Issue.generate!(:start_date => Date.today, :parent_issue_id => parent.id) | 
					
						
							|  |  |  |     Issue.generate!(:start_date => 2.days.from_now, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-01 18:42:41 +00:00
										 |  |  |     assert_difference 'Issue.count', -3 do | 
					
						
							|  |  |  |       Issue.find(parent.id).destroy | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-22 11:46:15 +00:00
										 |  |  |   def test_destroy_child_issue_with_children | 
					
						
							|  |  |  |     root = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'root') | 
					
						
							|  |  |  |     child = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => root.id) | 
					
						
							|  |  |  |     leaf = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'leaf', :parent_issue_id => child.id) | 
					
						
							|  |  |  |     leaf.init_journal(User.find(2)) | 
					
						
							|  |  |  |     leaf.subject = 'leaf with journal' | 
					
						
							|  |  |  |     leaf.save! | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-22 11:46:15 +00:00
										 |  |  |     assert_difference 'Issue.count', -2 do | 
					
						
							|  |  |  |       assert_difference 'Journal.count', -1 do | 
					
						
							|  |  |  |         assert_difference 'JournalDetail.count', -1 do | 
					
						
							|  |  |  |           Issue.find(child.id).destroy | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-22 11:46:15 +00:00
										 |  |  |     root = Issue.find(root.id) | 
					
						
							|  |  |  |     assert root.leaf?, "Root issue is not a leaf (lft: #{root.lft}, rgt: #{root.rgt})" | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-29 14:07:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-24 08:35:48 +00:00
										 |  |  |   def test_destroy_issue_with_grand_child | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |     lft1 = new_issue_lft | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     issue = Issue.generate!(:parent_issue_id => parent.id) | 
					
						
							|  |  |  |     child = Issue.generate!(:parent_issue_id => issue.id) | 
					
						
							|  |  |  |     grandchild1 = Issue.generate!(:parent_issue_id => child.id) | 
					
						
							|  |  |  |     grandchild2 = Issue.generate!(:parent_issue_id => child.id) | 
					
						
							| 
									
										
										
										
											2011-07-24 08:35:48 +00:00
										 |  |  |     assert_difference 'Issue.count', -4 do | 
					
						
							|  |  |  |       Issue.find(issue.id).destroy | 
					
						
							|  |  |  |       parent.reload | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |       assert_equal [lft1, lft1 + 1], [parent.lft, parent.rgt] | 
					
						
							| 
									
										
										
										
											2011-07-24 08:35:48 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_parent_priority_should_be_the_highest_child_priority | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate!(:priority => IssuePriority.find_by_name('Normal')) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     # Create children | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     child1 = Issue.generate!(:priority => IssuePriority.find_by_name('High'), :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 'High', parent.reload.priority.name | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     child2 = Issue.generate!(:priority => IssuePriority.find_by_name('Immediate'), :parent_issue_id => child1.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 'Immediate', child1.reload.priority.name | 
					
						
							|  |  |  |     assert_equal 'Immediate', parent.reload.priority.name | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     child3 = Issue.generate!(:priority => IssuePriority.find_by_name('Low'), :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 'Immediate', parent.reload.priority.name | 
					
						
							|  |  |  |     # Destroy a child | 
					
						
							|  |  |  |     child1.destroy | 
					
						
							|  |  |  |     assert_equal 'Low', parent.reload.priority.name | 
					
						
							|  |  |  |     # Update a child | 
					
						
							|  |  |  |     child3.reload.priority = IssuePriority.find_by_name('Normal') | 
					
						
							|  |  |  |     child3.save! | 
					
						
							|  |  |  |     assert_equal 'Normal', parent.reload.priority.name | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_parent_dates_should_be_lowest_start_and_highest_due_dates | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     Issue.generate!(:start_date => '2010-01-25', :due_date => '2010-02-15', :parent_issue_id => parent.id) | 
					
						
							|  |  |  |     Issue.generate!(                             :due_date => '2010-02-13', :parent_issue_id => parent.id) | 
					
						
							|  |  |  |     Issue.generate!(:start_date => '2010-02-01', :due_date => '2010-02-22', :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     parent.reload | 
					
						
							|  |  |  |     assert_equal Date.parse('2010-01-25'), parent.start_date | 
					
						
							|  |  |  |     assert_equal Date.parse('2010-02-22'), parent.due_date | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_parent_done_ratio_should_be_average_done_ratio_of_leaves | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     Issue.generate!(:done_ratio => 20, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 20, parent.reload.done_ratio | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     Issue.generate!(:done_ratio => 70, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 45, parent.reload.done_ratio | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     child = Issue.generate!(:done_ratio => 0, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 30, parent.reload.done_ratio | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     Issue.generate!(:done_ratio => 30, :parent_issue_id => child.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 30, child.reload.done_ratio | 
					
						
							|  |  |  |     assert_equal 40, parent.reload.done_ratio | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_parent_done_ratio_should_be_weighted_by_estimated_times_if_any | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     Issue.generate!(:estimated_hours => 10, :done_ratio => 20, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 20, parent.reload.done_ratio | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     Issue.generate!(:estimated_hours => 20, :done_ratio => 50, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal (50 * 20 + 20 * 10) / 30, parent.reload.done_ratio | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-11 22:00:40 +00:00
										 |  |  |   def test_parent_done_ratio_with_child_estimate_to_0_should_reach_100 | 
					
						
							|  |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     issue1 = Issue.generate!(:parent_issue_id => parent.id) | 
					
						
							|  |  |  |     issue2 = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 0) | 
					
						
							|  |  |  |     assert_equal 0, parent.reload.done_ratio | 
					
						
							|  |  |  |     issue1.reload.update_attribute :status_id, 5
 | 
					
						
							|  |  |  |     assert_equal 50, parent.reload.done_ratio | 
					
						
							|  |  |  |     issue2.reload.update_attribute :status_id, 5
 | 
					
						
							|  |  |  |     assert_equal 100, parent.reload.done_ratio | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_parent_estimate_should_be_sum_of_leaves | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     Issue.generate!(:estimated_hours => nil, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal nil, parent.reload.estimated_hours | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     Issue.generate!(:estimated_hours => 5, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 5, parent.reload.estimated_hours | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     Issue.generate!(:estimated_hours => 7, :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 12, parent.reload.estimated_hours | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-03-13 15:29:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-30 02:45:34 +00:00
										 |  |  |   def test_move_parent_updates_old_parent_attributes | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     first_parent = Issue.generate! | 
					
						
							|  |  |  |     second_parent = Issue.generate! | 
					
						
							|  |  |  |     child = Issue.generate!(:estimated_hours => 5, :parent_issue_id => first_parent.id) | 
					
						
							| 
									
										
										
										
											2010-06-30 02:45:34 +00:00
										 |  |  |     assert_equal 5, first_parent.reload.estimated_hours | 
					
						
							|  |  |  |     child.update_attributes(:estimated_hours => 7, :parent_issue_id => second_parent.id) | 
					
						
							|  |  |  |     assert_equal 7, second_parent.reload.estimated_hours | 
					
						
							|  |  |  |     assert_nil first_parent.reload.estimated_hours | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-03-13 15:29:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def test_reschuling_a_parent_should_reschedule_subtasks | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     parent = Issue.generate! | 
					
						
							|  |  |  |     c1 = Issue.generate!(:start_date => '2010-05-12', :due_date => '2010-05-18', :parent_issue_id => parent.id) | 
					
						
							|  |  |  |     c2 = Issue.generate!(:start_date => '2010-06-03', :due_date => '2010-06-10', :parent_issue_id => parent.id) | 
					
						
							| 
									
										
										
										
											2010-03-13 15:29:34 +00:00
										 |  |  |     parent.reload | 
					
						
							| 
									
										
										
										
											2012-10-29 10:06:30 +00:00
										 |  |  |     parent.reschedule_on!(Date.parse('2010-06-02')) | 
					
						
							| 
									
										
										
										
											2010-03-13 15:29:34 +00:00
										 |  |  |     c1.reload | 
					
						
							|  |  |  |     assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-08')], [c1.start_date, c1.due_date] | 
					
						
							|  |  |  |     c2.reload | 
					
						
							|  |  |  |     assert_equal [Date.parse('2010-06-03'), Date.parse('2010-06-10')], [c2.start_date, c2.due_date] # no change | 
					
						
							|  |  |  |     parent.reload | 
					
						
							|  |  |  |     assert_equal [Date.parse('2010-06-02'), Date.parse('2010-06-10')], [parent.start_date, parent.due_date] | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |   def test_project_copy_should_copy_issue_tree | 
					
						
							|  |  |  |     p = Project.create!(:name => 'Tree copy', :identifier => 'tree-copy', :tracker_ids => [1, 2]) | 
					
						
							| 
									
										
										
										
											2012-10-12 09:02:25 +00:00
										 |  |  |     i1 = Issue.generate!(:project => p, :subject => 'i1') | 
					
						
							|  |  |  |     i2 = Issue.generate!(:project => p, :subject => 'i2', :parent_issue_id => i1.id) | 
					
						
							|  |  |  |     i3 = Issue.generate!(:project => p, :subject => 'i3', :parent_issue_id => i1.id) | 
					
						
							|  |  |  |     i4 = Issue.generate!(:project => p, :subject => 'i4', :parent_issue_id => i2.id) | 
					
						
							|  |  |  |     i5 = Issue.generate!(:project => p, :subject => 'i5') | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     c = Project.new(:name => 'Copy', :identifier => 'copy', :tracker_ids => [1, 2]) | 
					
						
							|  |  |  |     c.copy(p, :only => 'issues') | 
					
						
							|  |  |  |     c.reload | 
					
						
							| 
									
										
										
										
											2011-05-17 01:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert_equal 5, c.issues.count | 
					
						
							| 
									
										
										
										
											2012-12-02 20:23:48 +00:00
										 |  |  |     ic1, ic2, ic3, ic4, ic5 = c.issues.order('subject').all | 
					
						
							| 
									
										
										
										
											2010-03-13 14:56:49 +00:00
										 |  |  |     assert ic1.root? | 
					
						
							|  |  |  |     assert_equal ic1, ic2.parent | 
					
						
							|  |  |  |     assert_equal ic1, ic3.parent | 
					
						
							|  |  |  |     assert_equal ic2, ic4.parent | 
					
						
							|  |  |  |     assert ic5.root? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |