##// END OF EJS Templates
Prevent creation of project with identifier 'new' (#3602)....
Jean-Philippe Lang -
r2716:36d8f3519246
parent child
Show More
@@ -1,407 +1,409
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Project < ActiveRecord::Base
19 19 # Project statuses
20 20 STATUS_ACTIVE = 1
21 21 STATUS_ARCHIVED = 9
22 22
23 23 has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
24 24 has_many :users, :through => :members
25 25 has_many :enabled_modules, :dependent => :delete_all
26 26 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
27 27 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
28 28 has_many :issue_changes, :through => :issues, :source => :journals
29 29 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
30 30 has_many :time_entries, :dependent => :delete_all
31 31 has_many :queries, :dependent => :delete_all
32 32 has_many :documents, :dependent => :destroy
33 33 has_many :news, :dependent => :delete_all, :include => :author
34 34 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
35 35 has_many :boards, :dependent => :destroy, :order => "position ASC"
36 36 has_one :repository, :dependent => :destroy
37 37 has_many :changesets, :through => :repository
38 38 has_one :wiki, :dependent => :destroy
39 39 # Custom field for the project issues
40 40 has_and_belongs_to_many :issue_custom_fields,
41 41 :class_name => 'IssueCustomField',
42 42 :order => "#{CustomField.table_name}.position",
43 43 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
44 44 :association_foreign_key => 'custom_field_id'
45 45
46 46 acts_as_nested_set :order => 'name', :dependent => :destroy
47 47 acts_as_attachable :view_permission => :view_files,
48 48 :delete_permission => :manage_files
49 49
50 50 acts_as_customizable
51 51 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
52 52 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
53 53 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
54 54 :author => nil
55 55
56 56 attr_protected :status, :enabled_module_names
57 57
58 58 validates_presence_of :name, :identifier
59 59 validates_uniqueness_of :name, :identifier
60 60 validates_associated :repository, :wiki
61 61 validates_length_of :name, :maximum => 30
62 62 validates_length_of :homepage, :maximum => 255
63 63 validates_length_of :identifier, :in => 1..20
64 64 # donwcase letters, digits, dashes but not digits only
65 65 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
66
66 # reserved words
67 validates_exclusion_of :identifier, :in => %w( new )
68
67 69 before_destroy :delete_all_members
68 70
69 71 named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
70 72 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
71 73 named_scope :public, { :conditions => { :is_public => true } }
72 74 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
73 75
74 76 def identifier=(identifier)
75 77 super unless identifier_frozen?
76 78 end
77 79
78 80 def identifier_frozen?
79 81 errors[:identifier].nil? && !(new_record? || identifier.blank?)
80 82 end
81 83
82 84 def issues_with_subprojects(include_subprojects=false)
83 85 conditions = nil
84 86 if include_subprojects
85 87 ids = [id] + descendants.collect(&:id)
86 88 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
87 89 end
88 90 conditions ||= ["#{Project.table_name}.id = ?", id]
89 91 # Quick and dirty fix for Rails 2 compatibility
90 92 Issue.send(:with_scope, :find => { :conditions => conditions }) do
91 93 Version.send(:with_scope, :find => { :conditions => conditions }) do
92 94 yield
93 95 end
94 96 end
95 97 end
96 98
97 99 # returns latest created projects
98 100 # non public projects will be returned only if user is a member of those
99 101 def self.latest(user=nil, count=5)
100 102 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
101 103 end
102 104
103 105 # Returns a SQL :conditions string used to find all active projects for the specified user.
104 106 #
105 107 # Examples:
106 108 # Projects.visible_by(admin) => "projects.status = 1"
107 109 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
108 110 def self.visible_by(user=nil)
109 111 user ||= User.current
110 112 if user && user.admin?
111 113 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
112 114 elsif user && user.memberships.any?
113 115 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
114 116 else
115 117 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
116 118 end
117 119 end
118 120
119 121 def self.allowed_to_condition(user, permission, options={})
120 122 statements = []
121 123 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
122 124 if perm = Redmine::AccessControl.permission(permission)
123 125 unless perm.project_module.nil?
124 126 # If the permission belongs to a project module, make sure the module is enabled
125 127 base_statement << " AND EXISTS (SELECT em.id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}' AND em.project_id=#{Project.table_name}.id)"
126 128 end
127 129 end
128 130 if options[:project]
129 131 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
130 132 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
131 133 base_statement = "(#{project_statement}) AND (#{base_statement})"
132 134 end
133 135 if user.admin?
134 136 # no restriction
135 137 else
136 138 statements << "1=0"
137 139 if user.logged?
138 140 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
139 141 allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id}
140 142 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
141 143 elsif Role.anonymous.allowed_to?(permission)
142 144 # anonymous user allowed on public project
143 145 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
144 146 else
145 147 # anonymous user is not authorized
146 148 end
147 149 end
148 150 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
149 151 end
150 152
151 153 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
152 154 #
153 155 # Examples:
154 156 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
155 157 # project.project_condition(false) => "projects.id = 1"
156 158 def project_condition(with_subprojects)
157 159 cond = "#{Project.table_name}.id = #{id}"
158 160 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
159 161 cond
160 162 end
161 163
162 164 def self.find(*args)
163 165 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
164 166 project = find_by_identifier(*args)
165 167 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
166 168 project
167 169 else
168 170 super
169 171 end
170 172 end
171 173
172 174 def to_param
173 175 # id is used for projects with a numeric identifier (compatibility)
174 176 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
175 177 end
176 178
177 179 def active?
178 180 self.status == STATUS_ACTIVE
179 181 end
180 182
181 183 # Archives the project and its descendants recursively
182 184 def archive
183 185 # Archive subprojects if any
184 186 children.each do |subproject|
185 187 subproject.archive
186 188 end
187 189 update_attribute :status, STATUS_ARCHIVED
188 190 end
189 191
190 192 # Unarchives the project
191 193 # All its ancestors must be active
192 194 def unarchive
193 195 return false if ancestors.detect {|a| !a.active?}
194 196 update_attribute :status, STATUS_ACTIVE
195 197 end
196 198
197 199 # Returns an array of projects the project can be moved to
198 200 def possible_parents
199 201 @possible_parents ||= (Project.active.find(:all) - self_and_descendants)
200 202 end
201 203
202 204 # Sets the parent of the project
203 205 # Argument can be either a Project, a String, a Fixnum or nil
204 206 def set_parent!(p)
205 207 unless p.nil? || p.is_a?(Project)
206 208 if p.to_s.blank?
207 209 p = nil
208 210 else
209 211 p = Project.find_by_id(p)
210 212 return false unless p
211 213 end
212 214 end
213 215 if p == parent && !p.nil?
214 216 # Nothing to do
215 217 true
216 218 elsif p.nil? || (p.active? && move_possible?(p))
217 219 # Insert the project so that target's children or root projects stay alphabetically sorted
218 220 sibs = (p.nil? ? self.class.roots : p.children)
219 221 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
220 222 if to_be_inserted_before
221 223 move_to_left_of(to_be_inserted_before)
222 224 elsif p.nil?
223 225 if sibs.empty?
224 226 # move_to_root adds the project in first (ie. left) position
225 227 move_to_root
226 228 else
227 229 move_to_right_of(sibs.last) unless self == sibs.last
228 230 end
229 231 else
230 232 # move_to_child_of adds the project in last (ie.right) position
231 233 move_to_child_of(p)
232 234 end
233 235 true
234 236 else
235 237 # Can not move to the given target
236 238 false
237 239 end
238 240 end
239 241
240 242 # Returns an array of the trackers used by the project and its active sub projects
241 243 def rolled_up_trackers
242 244 @rolled_up_trackers ||=
243 245 Tracker.find(:all, :include => :projects,
244 246 :select => "DISTINCT #{Tracker.table_name}.*",
245 247 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
246 248 :order => "#{Tracker.table_name}.position")
247 249 end
248 250
249 251 # Returns a hash of project users grouped by role
250 252 def users_by_role
251 253 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
252 254 m.roles.each do |r|
253 255 h[r] ||= []
254 256 h[r] << m.user
255 257 end
256 258 h
257 259 end
258 260 end
259 261
260 262 # Deletes all project's members
261 263 def delete_all_members
262 264 me, mr = Member.table_name, MemberRole.table_name
263 265 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
264 266 Member.delete_all(['project_id = ?', id])
265 267 end
266 268
267 269 # Users issues can be assigned to
268 270 def assignable_users
269 271 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort
270 272 end
271 273
272 274 # Returns the mail adresses of users that should be always notified on project events
273 275 def recipients
274 276 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
275 277 end
276 278
277 279 # Returns an array of all custom fields enabled for project issues
278 280 # (explictly associated custom fields and custom fields enabled for all projects)
279 281 def all_issue_custom_fields
280 282 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
281 283 end
282 284
283 285 def project
284 286 self
285 287 end
286 288
287 289 def <=>(project)
288 290 name.downcase <=> project.name.downcase
289 291 end
290 292
291 293 def to_s
292 294 name
293 295 end
294 296
295 297 # Returns a short description of the projects (first lines)
296 298 def short_description(length = 255)
297 299 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
298 300 end
299 301
300 302 # Return true if this project is allowed to do the specified action.
301 303 # action can be:
302 304 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
303 305 # * a permission Symbol (eg. :edit_project)
304 306 def allows_to?(action)
305 307 if action.is_a? Hash
306 308 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
307 309 else
308 310 allowed_permissions.include? action
309 311 end
310 312 end
311 313
312 314 def module_enabled?(module_name)
313 315 module_name = module_name.to_s
314 316 enabled_modules.detect {|m| m.name == module_name}
315 317 end
316 318
317 319 def enabled_module_names=(module_names)
318 320 if module_names && module_names.is_a?(Array)
319 321 module_names = module_names.collect(&:to_s)
320 322 # remove disabled modules
321 323 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
322 324 # add new modules
323 325 module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)}
324 326 else
325 327 enabled_modules.clear
326 328 end
327 329 end
328 330
329 331 # Returns an auto-generated project identifier based on the last identifier used
330 332 def self.next_identifier
331 333 p = Project.find(:first, :order => 'created_on DESC')
332 334 p.nil? ? nil : p.identifier.to_s.succ
333 335 end
334 336
335 337 # Copies and saves the Project instance based on the +project+.
336 338 # Will duplicate the source project's:
337 339 # * Issues
338 340 # * Members
339 341 # * Queries
340 342 def copy(project)
341 343 project = project.is_a?(Project) ? project : Project.find(project)
342 344
343 345 Project.transaction do
344 346 # Issues
345 347 project.issues.each do |issue|
346 348 new_issue = Issue.new
347 349 new_issue.copy_from(issue)
348 350 self.issues << new_issue
349 351 end
350 352
351 353 # Members
352 354 project.members.each do |member|
353 355 new_member = Member.new
354 356 new_member.attributes = member.attributes.dup.except("project_id")
355 357 new_member.role_ids = member.role_ids.dup
356 358 new_member.project = self
357 359 self.members << new_member
358 360 end
359 361
360 362 # Queries
361 363 project.queries.each do |query|
362 364 new_query = Query.new
363 365 new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria")
364 366 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
365 367 new_query.project = self
366 368 self.queries << new_query
367 369 end
368 370
369 371 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
370 372 self.save
371 373 end
372 374 end
373 375
374 376
375 377 # Copies +project+ and returns the new instance. This will not save
376 378 # the copy
377 379 def self.copy_from(project)
378 380 begin
379 381 project = project.is_a?(Project) ? project : Project.find(project)
380 382 if project
381 383 # clear unique attributes
382 384 attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status')
383 385 copy = Project.new(attributes)
384 386 copy.enabled_modules = project.enabled_modules
385 387 copy.trackers = project.trackers
386 388 copy.custom_values = project.custom_values.collect {|v| v.clone}
387 389 return copy
388 390 else
389 391 return nil
390 392 end
391 393 rescue ActiveRecord::RecordNotFound
392 394 return nil
393 395 end
394 396 end
395 397
396 398 private
397 399 def allowed_permissions
398 400 @allowed_permissions ||= begin
399 401 module_names = enabled_modules.collect {|m| m.name}
400 402 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
401 403 end
402 404 end
403 405
404 406 def allowed_actions
405 407 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
406 408 end
407 409 end
@@ -1,342 +1,343
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class ProjectTest < Test::Unit::TestCase
21 21 fixtures :projects, :enabled_modules,
22 22 :issues, :issue_statuses, :journals, :journal_details,
23 23 :users, :members, :member_roles, :roles, :projects_trackers, :trackers, :boards,
24 24 :queries
25 25
26 26 def setup
27 27 @ecookbook = Project.find(1)
28 28 @ecookbook_sub1 = Project.find(3)
29 29 end
30 30
31 31 def test_truth
32 32 assert_kind_of Project, @ecookbook
33 33 assert_equal "eCookbook", @ecookbook.name
34 34 end
35 35
36 36 def test_update
37 37 assert_equal "eCookbook", @ecookbook.name
38 38 @ecookbook.name = "eCook"
39 39 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
40 40 @ecookbook.reload
41 41 assert_equal "eCook", @ecookbook.name
42 42 end
43 43
44 44 def test_validate
45 45 @ecookbook.name = ""
46 46 assert !@ecookbook.save
47 47 assert_equal 1, @ecookbook.errors.count
48 48 assert_equal I18n.translate('activerecord.errors.messages.blank'), @ecookbook.errors.on(:name)
49 49 end
50 50
51 51 def test_validate_identifier
52 52 to_test = {"abc" => true,
53 53 "ab12" => true,
54 54 "ab-12" => true,
55 "12" => false}
55 "12" => false,
56 "new" => false}
56 57
57 58 to_test.each do |identifier, valid|
58 59 p = Project.new
59 60 p.identifier = identifier
60 61 p.valid?
61 62 assert_equal valid, p.errors.on('identifier').nil?
62 63 end
63 64 end
64 65
65 66 def test_archive
66 67 user = @ecookbook.members.first.user
67 68 @ecookbook.archive
68 69 @ecookbook.reload
69 70
70 71 assert !@ecookbook.active?
71 72 assert !user.projects.include?(@ecookbook)
72 73 # Subproject are also archived
73 74 assert !@ecookbook.children.empty?
74 75 assert @ecookbook.descendants.active.empty?
75 76 end
76 77
77 78 def test_unarchive
78 79 user = @ecookbook.members.first.user
79 80 @ecookbook.archive
80 81 # A subproject of an archived project can not be unarchived
81 82 assert !@ecookbook_sub1.unarchive
82 83
83 84 # Unarchive project
84 85 assert @ecookbook.unarchive
85 86 @ecookbook.reload
86 87 assert @ecookbook.active?
87 88 assert user.projects.include?(@ecookbook)
88 89 # Subproject can now be unarchived
89 90 @ecookbook_sub1.reload
90 91 assert @ecookbook_sub1.unarchive
91 92 end
92 93
93 94 def test_destroy
94 95 # 2 active members
95 96 assert_equal 2, @ecookbook.members.size
96 97 # and 1 is locked
97 98 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
98 99 # some boards
99 100 assert @ecookbook.boards.any?
100 101
101 102 @ecookbook.destroy
102 103 # make sure that the project non longer exists
103 104 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
104 105 # make sure related data was removed
105 106 assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
106 107 assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
107 108 end
108 109
109 110 def test_move_an_orphan_project_to_a_root_project
110 111 sub = Project.find(2)
111 112 sub.set_parent! @ecookbook
112 113 assert_equal @ecookbook.id, sub.parent.id
113 114 @ecookbook.reload
114 115 assert_equal 4, @ecookbook.children.size
115 116 end
116 117
117 118 def test_move_an_orphan_project_to_a_subproject
118 119 sub = Project.find(2)
119 120 assert sub.set_parent!(@ecookbook_sub1)
120 121 end
121 122
122 123 def test_move_a_root_project_to_a_project
123 124 sub = @ecookbook
124 125 assert sub.set_parent!(Project.find(2))
125 126 end
126 127
127 128 def test_should_not_move_a_project_to_its_children
128 129 sub = @ecookbook
129 130 assert !(sub.set_parent!(Project.find(3)))
130 131 end
131 132
132 133 def test_set_parent_should_add_roots_in_alphabetical_order
133 134 ProjectCustomField.delete_all
134 135 Project.delete_all
135 136 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
136 137 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
137 138 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
138 139 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
139 140
140 141 assert_equal 4, Project.count
141 142 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
142 143 end
143 144
144 145 def test_set_parent_should_add_children_in_alphabetical_order
145 146 ProjectCustomField.delete_all
146 147 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
147 148 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
148 149 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
149 150 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
150 151 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
151 152
152 153 parent.reload
153 154 assert_equal 4, parent.children.size
154 155 assert_equal parent.children.sort_by(&:name), parent.children
155 156 end
156 157
157 158 def test_rebuild_should_sort_children_alphabetically
158 159 ProjectCustomField.delete_all
159 160 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
160 161 Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
161 162 Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
162 163 Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
163 164 Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
164 165
165 166 Project.update_all("lft = NULL, rgt = NULL")
166 167 Project.rebuild!
167 168
168 169 parent.reload
169 170 assert_equal 4, parent.children.size
170 171 assert_equal parent.children.sort_by(&:name), parent.children
171 172 end
172 173
173 174 def test_parent
174 175 p = Project.find(6).parent
175 176 assert p.is_a?(Project)
176 177 assert_equal 5, p.id
177 178 end
178 179
179 180 def test_ancestors
180 181 a = Project.find(6).ancestors
181 182 assert a.first.is_a?(Project)
182 183 assert_equal [1, 5], a.collect(&:id)
183 184 end
184 185
185 186 def test_root
186 187 r = Project.find(6).root
187 188 assert r.is_a?(Project)
188 189 assert_equal 1, r.id
189 190 end
190 191
191 192 def test_children
192 193 c = Project.find(1).children
193 194 assert c.first.is_a?(Project)
194 195 assert_equal [5, 3, 4], c.collect(&:id)
195 196 end
196 197
197 198 def test_descendants
198 199 d = Project.find(1).descendants
199 200 assert d.first.is_a?(Project)
200 201 assert_equal [5, 6, 3, 4], d.collect(&:id)
201 202 end
202 203
203 204 def test_users_by_role
204 205 users_by_role = Project.find(1).users_by_role
205 206 assert_kind_of Hash, users_by_role
206 207 role = Role.find(1)
207 208 assert_kind_of Array, users_by_role[role]
208 209 assert users_by_role[role].include?(User.find(2))
209 210 end
210 211
211 212 def test_rolled_up_trackers
212 213 parent = Project.find(1)
213 214 parent.trackers = Tracker.find([1,2])
214 215 child = parent.children.find(3)
215 216
216 217 assert_equal [1, 2], parent.tracker_ids
217 218 assert_equal [2, 3], child.trackers.collect(&:id)
218 219
219 220 assert_kind_of Tracker, parent.rolled_up_trackers.first
220 221 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
221 222
222 223 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
223 224 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
224 225 end
225 226
226 227 def test_rolled_up_trackers_should_ignore_archived_subprojects
227 228 parent = Project.find(1)
228 229 parent.trackers = Tracker.find([1,2])
229 230 child = parent.children.find(3)
230 231 child.trackers = Tracker.find([1,3])
231 232 parent.children.each(&:archive)
232 233
233 234 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
234 235 end
235 236
236 237 def test_next_identifier
237 238 ProjectCustomField.delete_all
238 239 Project.create!(:name => 'last', :identifier => 'p2008040')
239 240 assert_equal 'p2008041', Project.next_identifier
240 241 end
241 242
242 243 def test_next_identifier_first_project
243 244 Project.delete_all
244 245 assert_nil Project.next_identifier
245 246 end
246 247
247 248
248 249 def test_enabled_module_names_should_not_recreate_enabled_modules
249 250 project = Project.find(1)
250 251 # Remove one module
251 252 modules = project.enabled_modules.slice(0..-2)
252 253 assert modules.any?
253 254 assert_difference 'EnabledModule.count', -1 do
254 255 project.enabled_module_names = modules.collect(&:name)
255 256 end
256 257 project.reload
257 258 # Ids should be preserved
258 259 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
259 260 end
260 261
261 262 def test_copy_from_existing_project
262 263 source_project = Project.find(1)
263 264 copied_project = Project.copy_from(1)
264 265
265 266 assert copied_project
266 267 # Cleared attributes
267 268 assert copied_project.id.blank?
268 269 assert copied_project.name.blank?
269 270 assert copied_project.identifier.blank?
270 271
271 272 # Duplicated attributes
272 273 assert_equal source_project.description, copied_project.description
273 274 assert_equal source_project.enabled_modules, copied_project.enabled_modules
274 275 assert_equal source_project.trackers, copied_project.trackers
275 276
276 277 # Default attributes
277 278 assert_equal 1, copied_project.status
278 279 end
279 280
280 281 # Context: Project#copy
281 282 def test_copy_should_copy_issues
282 283 # Setup
283 284 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
284 285 source_project = Project.find(2)
285 286 Project.destroy_all :identifier => "copy-test"
286 287 project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
287 288 project.trackers = source_project.trackers
288 289 assert project.valid?
289 290
290 291 assert project.issues.empty?
291 292 assert project.copy(source_project)
292 293
293 294 # Tests
294 295 assert_equal source_project.issues.size, project.issues.size
295 296 project.issues.each do |issue|
296 297 assert issue.valid?
297 298 assert ! issue.assigned_to.blank?
298 299 assert_equal project, issue.project
299 300 end
300 301 end
301 302
302 303 def test_copy_should_copy_members
303 304 # Setup
304 305 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
305 306 source_project = Project.find(2)
306 307 project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
307 308 project.trackers = source_project.trackers
308 309 project.enabled_modules = source_project.enabled_modules
309 310 assert project.valid?
310 311
311 312 assert project.members.empty?
312 313 assert project.copy(source_project)
313 314
314 315 # Tests
315 316 assert_equal source_project.members.size, project.members.size
316 317 project.members.each do |member|
317 318 assert member
318 319 assert_equal project, member.project
319 320 end
320 321 end
321 322
322 323 def test_copy_should_copy_project_level_queries
323 324 # Setup
324 325 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
325 326 source_project = Project.find(2)
326 327 project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
327 328 project.trackers = source_project.trackers
328 329 project.enabled_modules = source_project.enabled_modules
329 330 assert project.valid?
330 331
331 332 assert project.queries.empty?
332 333 assert project.copy(source_project)
333 334
334 335 # Tests
335 336 assert_equal source_project.queries.size, project.queries.size
336 337 project.queries.each do |query|
337 338 assert query
338 339 assert_equal project, query.project
339 340 end
340 341 end
341 342
342 343 end
General Comments 0
You need to be logged in to leave comments. Login now