annotate .svn/pristine/42/42d1c2a743c5cbab9037c2757ccb337f6873d714.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents dffacf8a6908
children
rev   line source
Chris@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 require File.expand_path('../../test_helper', __FILE__)
Chris@1517 19
Chris@1517 20 class ProjectNestedSetTest < ActiveSupport::TestCase
Chris@1517 21
Chris@1517 22 def setup
Chris@1517 23 Project.delete_all
Chris@1517 24
Chris@1517 25 @a = Project.create!(:name => 'A', :identifier => 'projecta')
Chris@1517 26 @a1 = Project.create!(:name => 'A1', :identifier => 'projecta1')
Chris@1517 27 @a1.set_parent!(@a)
Chris@1517 28 @a2 = Project.create!(:name => 'A2', :identifier => 'projecta2')
Chris@1517 29 @a2.set_parent!(@a)
Chris@1517 30
Chris@1517 31 @c = Project.create!(:name => 'C', :identifier => 'projectc')
Chris@1517 32 @c1 = Project.create!(:name => 'C1', :identifier => 'projectc1')
Chris@1517 33 @c1.set_parent!(@c)
Chris@1517 34
Chris@1517 35 @b = Project.create!(:name => 'B', :identifier => 'projectb')
Chris@1517 36 @b2 = Project.create!(:name => 'B2', :identifier => 'projectb2')
Chris@1517 37 @b2.set_parent!(@b)
Chris@1517 38 @b1 = Project.create!(:name => 'B1', :identifier => 'projectb1')
Chris@1517 39 @b1.set_parent!(@b)
Chris@1517 40 @b11 = Project.create!(:name => 'B11', :identifier => 'projectb11')
Chris@1517 41 @b11.set_parent!(@b1)
Chris@1517 42
Chris@1517 43 @a, @a1, @a2, @b, @b1, @b11, @b2, @c, @c1 = *(Project.all.sort_by(&:name))
Chris@1517 44 end
Chris@1517 45
Chris@1517 46 def test_valid_tree
Chris@1517 47 assert_valid_nested_set
Chris@1517 48 end
Chris@1517 49
Chris@1517 50 def test_rebuild_should_build_valid_tree
Chris@1517 51 Project.update_all "lft = NULL, rgt = NULL"
Chris@1517 52
Chris@1517 53 Project.rebuild_tree!
Chris@1517 54 assert_valid_nested_set
Chris@1517 55 end
Chris@1517 56
Chris@1517 57 def test_rebuild_tree_should_build_valid_tree_even_with_valid_lft_rgt_values
Chris@1517 58 Project.where({:id => @a.id }).update_all("name = 'YY'")
Chris@1517 59 # lft and rgt values are still valid (Project.rebuild! would not update anything)
Chris@1517 60 # but projects are not ordered properly (YY is in the first place)
Chris@1517 61
Chris@1517 62 Project.rebuild_tree!
Chris@1517 63 assert_valid_nested_set
Chris@1517 64 end
Chris@1517 65
Chris@1517 66 def test_moving_a_child_to_a_different_parent_should_keep_valid_tree
Chris@1517 67 assert_no_difference 'Project.count' do
Chris@1517 68 Project.find_by_name('B1').set_parent!(Project.find_by_name('A2'))
Chris@1517 69 end
Chris@1517 70 assert_valid_nested_set
Chris@1517 71 end
Chris@1517 72
Chris@1517 73 def test_renaming_a_root_to_first_position_should_update_nested_set_order
Chris@1517 74 @c.name = '1'
Chris@1517 75 @c.save!
Chris@1517 76 assert_valid_nested_set
Chris@1517 77 end
Chris@1517 78
Chris@1517 79 def test_renaming_a_root_to_middle_position_should_update_nested_set_order
Chris@1517 80 @a.name = 'BA'
Chris@1517 81 @a.save!
Chris@1517 82 assert_valid_nested_set
Chris@1517 83 end
Chris@1517 84
Chris@1517 85 def test_renaming_a_root_to_last_position_should_update_nested_set_order
Chris@1517 86 @a.name = 'D'
Chris@1517 87 @a.save!
Chris@1517 88 assert_valid_nested_set
Chris@1517 89 end
Chris@1517 90
Chris@1517 91 def test_renaming_a_root_to_same_position_should_update_nested_set_order
Chris@1517 92 @c.name = 'D'
Chris@1517 93 @c.save!
Chris@1517 94 assert_valid_nested_set
Chris@1517 95 end
Chris@1517 96
Chris@1517 97 def test_renaming_a_child_should_update_nested_set_order
Chris@1517 98 @a1.name = 'A3'
Chris@1517 99 @a1.save!
Chris@1517 100 assert_valid_nested_set
Chris@1517 101 end
Chris@1517 102
Chris@1517 103 def test_renaming_a_child_with_child_should_update_nested_set_order
Chris@1517 104 @b1.name = 'B3'
Chris@1517 105 @b1.save!
Chris@1517 106 assert_valid_nested_set
Chris@1517 107 end
Chris@1517 108
Chris@1517 109 def test_adding_a_root_to_first_position_should_update_nested_set_order
Chris@1517 110 project = Project.create!(:name => '1', :identifier => 'projectba')
Chris@1517 111 assert_valid_nested_set
Chris@1517 112 end
Chris@1517 113
Chris@1517 114 def test_adding_a_root_to_middle_position_should_update_nested_set_order
Chris@1517 115 project = Project.create!(:name => 'BA', :identifier => 'projectba')
Chris@1517 116 assert_valid_nested_set
Chris@1517 117 end
Chris@1517 118
Chris@1517 119 def test_adding_a_root_to_last_position_should_update_nested_set_order
Chris@1517 120 project = Project.create!(:name => 'Z', :identifier => 'projectba')
Chris@1517 121 assert_valid_nested_set
Chris@1517 122 end
Chris@1517 123
Chris@1517 124 def test_destroying_a_root_with_children_should_keep_valid_tree
Chris@1517 125 assert_difference 'Project.count', -4 do
Chris@1517 126 Project.find_by_name('B').destroy
Chris@1517 127 end
Chris@1517 128 assert_valid_nested_set
Chris@1517 129 end
Chris@1517 130
Chris@1517 131 def test_destroying_a_child_with_children_should_keep_valid_tree
Chris@1517 132 assert_difference 'Project.count', -2 do
Chris@1517 133 Project.find_by_name('B1').destroy
Chris@1517 134 end
Chris@1517 135 assert_valid_nested_set
Chris@1517 136 end
Chris@1517 137
Chris@1517 138 private
Chris@1517 139
Chris@1517 140 def assert_nested_set_values(h)
Chris@1517 141 assert Project.valid?
Chris@1517 142 h.each do |project, expected|
Chris@1517 143 project.reload
Chris@1517 144 assert_equal expected, [project.parent_id, project.lft, project.rgt], "Unexpected nested set values for #{project.name}"
Chris@1517 145 end
Chris@1517 146 end
Chris@1517 147
Chris@1517 148 def assert_valid_nested_set
Chris@1517 149 projects = Project.all
Chris@1517 150 lft_rgt = projects.map {|p| [p.lft, p.rgt]}.flatten
Chris@1517 151 assert_equal projects.size * 2, lft_rgt.uniq.size
Chris@1517 152 assert_equal 1, lft_rgt.min
Chris@1517 153 assert_equal projects.size * 2, lft_rgt.max
Chris@1517 154
Chris@1517 155 projects.each do |project|
Chris@1517 156 # lft should always be < rgt
Chris@1517 157 assert project.lft < project.rgt, "lft=#{project.lft} was not < rgt=#{project.rgt} for project #{project.name}"
Chris@1517 158 if project.parent_id
Chris@1517 159 # child lft/rgt values must be greater/lower
Chris@1517 160 assert_not_nil project.parent, "parent was nil for project #{project.name}"
Chris@1517 161 assert project.lft > project.parent.lft, "lft=#{project.lft} was not > parent.lft=#{project.parent.lft} for project #{project.name}"
Chris@1517 162 assert project.rgt < project.parent.rgt, "rgt=#{project.rgt} was not < parent.rgt=#{project.parent.rgt} for project #{project.name}"
Chris@1517 163 end
Chris@1517 164 # no overlapping lft/rgt values
Chris@1517 165 overlapping = projects.detect {|other|
Chris@1517 166 other != project && (
Chris@1517 167 (other.lft > project.lft && other.lft < project.rgt && other.rgt > project.rgt) ||
Chris@1517 168 (other.rgt > project.lft && other.rgt < project.rgt && other.lft < project.lft)
Chris@1517 169 )
Chris@1517 170 }
Chris@1517 171 assert_nil overlapping, (overlapping && "Project #{overlapping.name} (#{overlapping.lft}/#{overlapping.rgt}) overlapped #{project.name} (#{project.lft}/#{project.rgt})")
Chris@1517 172 end
Chris@1517 173
Chris@1517 174 # root projects sorted alphabetically
Chris@1517 175 assert_equal Project.roots.map(&:name).sort, Project.roots.sort_by(&:lft).map(&:name), "Root projects were not properly sorted"
Chris@1517 176 projects.each do |project|
Chris@1517 177 if project.children.any?
Chris@1517 178 # sibling projects sorted alphabetically
Chris@1517 179 assert_equal project.children.map(&:name).sort, project.children.order('lft').map(&:name), "Project #{project.name}'s children were not properly sorted"
Chris@1517 180 end
Chris@1517 181 end
Chris@1517 182 end
Chris@1517 183 end