##// END OF EJS Templates
Fixed: news comments not deleted when deleting a project (#7904)....
Jean-Philippe Lang -
r5056:0b3f2bc65004
parent child
Show More
@@ -1,840 +1,840
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 include Redmine::SafeAttributes
20 20
21 21 # Project statuses
22 22 STATUS_ACTIVE = 1
23 23 STATUS_ARCHIVED = 9
24 24
25 25 # Maximum length for project identifiers
26 26 IDENTIFIER_MAX_LENGTH = 100
27 27
28 28 # Specific overidden Activities
29 29 has_many :time_entry_activities
30 30 has_many :members, :include => [:user, :roles], :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
31 31 has_many :memberships, :class_name => 'Member'
32 32 has_many :member_principals, :class_name => 'Member',
33 33 :include => :principal,
34 34 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
35 35 has_many :users, :through => :members
36 36 has_many :principals, :through => :member_principals, :source => :principal
37 37
38 38 has_many :enabled_modules, :dependent => :delete_all
39 39 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
40 40 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
41 41 has_many :issue_changes, :through => :issues, :source => :journals
42 42 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
43 43 has_many :time_entries, :dependent => :delete_all
44 44 has_many :queries, :dependent => :delete_all
45 45 has_many :documents, :dependent => :destroy
46 has_many :news, :dependent => :delete_all, :include => :author
46 has_many :news, :dependent => :destroy, :include => :author
47 47 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
48 48 has_many :boards, :dependent => :destroy, :order => "position ASC"
49 49 has_one :repository, :dependent => :destroy
50 50 has_many :changesets, :through => :repository
51 51 has_one :wiki, :dependent => :destroy
52 52 # Custom field for the project issues
53 53 has_and_belongs_to_many :issue_custom_fields,
54 54 :class_name => 'IssueCustomField',
55 55 :order => "#{CustomField.table_name}.position",
56 56 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
57 57 :association_foreign_key => 'custom_field_id'
58 58
59 59 acts_as_nested_set :order => 'name', :dependent => :destroy
60 60 acts_as_attachable :view_permission => :view_files,
61 61 :delete_permission => :manage_files
62 62
63 63 acts_as_customizable
64 64 acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => 'id', :permission => nil
65 65 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
66 66 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}},
67 67 :author => nil
68 68
69 69 attr_protected :status
70 70
71 71 validates_presence_of :name, :identifier
72 72 validates_uniqueness_of :identifier
73 73 validates_associated :repository, :wiki
74 74 validates_length_of :name, :maximum => 255
75 75 validates_length_of :homepage, :maximum => 255
76 76 validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH
77 77 # donwcase letters, digits, dashes but not digits only
78 78 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
79 79 # reserved words
80 80 validates_exclusion_of :identifier, :in => %w( new )
81 81
82 82 before_destroy :delete_all_members
83 83
84 84 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] } }
85 85 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
86 86 named_scope :all_public, { :conditions => { :is_public => true } }
87 87 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
88 88
89 89 def initialize(attributes = nil)
90 90 super
91 91
92 92 initialized = (attributes || {}).stringify_keys
93 93 if !initialized.key?('identifier') && Setting.sequential_project_identifiers?
94 94 self.identifier = Project.next_identifier
95 95 end
96 96 if !initialized.key?('is_public')
97 97 self.is_public = Setting.default_projects_public?
98 98 end
99 99 if !initialized.key?('enabled_module_names')
100 100 self.enabled_module_names = Setting.default_projects_modules
101 101 end
102 102 if !initialized.key?('trackers') && !initialized.key?('tracker_ids')
103 103 self.trackers = Tracker.all
104 104 end
105 105 end
106 106
107 107 def identifier=(identifier)
108 108 super unless identifier_frozen?
109 109 end
110 110
111 111 def identifier_frozen?
112 112 errors[:identifier].nil? && !(new_record? || identifier.blank?)
113 113 end
114 114
115 115 # returns latest created projects
116 116 # non public projects will be returned only if user is a member of those
117 117 def self.latest(user=nil, count=5)
118 118 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
119 119 end
120 120
121 121 # Returns a SQL :conditions string used to find all active projects for the specified user.
122 122 #
123 123 # Examples:
124 124 # Projects.visible_by(admin) => "projects.status = 1"
125 125 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
126 126 def self.visible_by(user=nil)
127 127 user ||= User.current
128 128 if user && user.admin?
129 129 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
130 130 elsif user && user.memberships.any?
131 131 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(',')}))"
132 132 else
133 133 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
134 134 end
135 135 end
136 136
137 137 def self.allowed_to_condition(user, permission, options={})
138 138 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
139 139 if perm = Redmine::AccessControl.permission(permission)
140 140 unless perm.project_module.nil?
141 141 # If the permission belongs to a project module, make sure the module is enabled
142 142 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
143 143 end
144 144 end
145 145 if options[:project]
146 146 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
147 147 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
148 148 base_statement = "(#{project_statement}) AND (#{base_statement})"
149 149 end
150 150
151 151 if user.admin?
152 152 base_statement
153 153 else
154 154 statement_by_role = {}
155 155 if user.logged?
156 156 if Role.non_member.allowed_to?(permission) && !options[:member]
157 157 statement_by_role[Role.non_member] = "#{Project.table_name}.is_public = #{connection.quoted_true}"
158 158 end
159 159 user.projects_by_role.each do |role, projects|
160 160 if role.allowed_to?(permission)
161 161 statement_by_role[role] = "#{Project.table_name}.id IN (#{projects.collect(&:id).join(',')})"
162 162 end
163 163 end
164 164 else
165 165 if Role.anonymous.allowed_to?(permission) && !options[:member]
166 166 statement_by_role[Role.anonymous] = "#{Project.table_name}.is_public = #{connection.quoted_true}"
167 167 end
168 168 end
169 169 if statement_by_role.empty?
170 170 "1=0"
171 171 else
172 172 "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))"
173 173 end
174 174 end
175 175 end
176 176
177 177 # Returns the Systemwide and project specific activities
178 178 def activities(include_inactive=false)
179 179 if include_inactive
180 180 return all_activities
181 181 else
182 182 return active_activities
183 183 end
184 184 end
185 185
186 186 # Will create a new Project specific Activity or update an existing one
187 187 #
188 188 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
189 189 # does not successfully save.
190 190 def update_or_create_time_entry_activity(id, activity_hash)
191 191 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
192 192 self.create_time_entry_activity_if_needed(activity_hash)
193 193 else
194 194 activity = project.time_entry_activities.find_by_id(id.to_i)
195 195 activity.update_attributes(activity_hash) if activity
196 196 end
197 197 end
198 198
199 199 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
200 200 #
201 201 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
202 202 # does not successfully save.
203 203 def create_time_entry_activity_if_needed(activity)
204 204 if activity['parent_id']
205 205
206 206 parent_activity = TimeEntryActivity.find(activity['parent_id'])
207 207 activity['name'] = parent_activity.name
208 208 activity['position'] = parent_activity.position
209 209
210 210 if Enumeration.overridding_change?(activity, parent_activity)
211 211 project_activity = self.time_entry_activities.create(activity)
212 212
213 213 if project_activity.new_record?
214 214 raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved"
215 215 else
216 216 self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id])
217 217 end
218 218 end
219 219 end
220 220 end
221 221
222 222 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
223 223 #
224 224 # Examples:
225 225 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
226 226 # project.project_condition(false) => "projects.id = 1"
227 227 def project_condition(with_subprojects)
228 228 cond = "#{Project.table_name}.id = #{id}"
229 229 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
230 230 cond
231 231 end
232 232
233 233 def self.find(*args)
234 234 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
235 235 project = find_by_identifier(*args)
236 236 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
237 237 project
238 238 else
239 239 super
240 240 end
241 241 end
242 242
243 243 def to_param
244 244 # id is used for projects with a numeric identifier (compatibility)
245 245 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
246 246 end
247 247
248 248 def active?
249 249 self.status == STATUS_ACTIVE
250 250 end
251 251
252 252 def archived?
253 253 self.status == STATUS_ARCHIVED
254 254 end
255 255
256 256 # Archives the project and its descendants
257 257 def archive
258 258 # Check that there is no issue of a non descendant project that is assigned
259 259 # to one of the project or descendant versions
260 260 v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten
261 261 if v_ids.any? && Issue.find(:first, :include => :project,
262 262 :conditions => ["(#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?)" +
263 263 " AND #{Issue.table_name}.fixed_version_id IN (?)", lft, rgt, v_ids])
264 264 return false
265 265 end
266 266 Project.transaction do
267 267 archive!
268 268 end
269 269 true
270 270 end
271 271
272 272 # Unarchives the project
273 273 # All its ancestors must be active
274 274 def unarchive
275 275 return false if ancestors.detect {|a| !a.active?}
276 276 update_attribute :status, STATUS_ACTIVE
277 277 end
278 278
279 279 # Returns an array of projects the project can be moved to
280 280 # by the current user
281 281 def allowed_parents
282 282 return @allowed_parents if @allowed_parents
283 283 @allowed_parents = Project.find(:all, :conditions => Project.allowed_to_condition(User.current, :add_subprojects))
284 284 @allowed_parents = @allowed_parents - self_and_descendants
285 285 if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?)
286 286 @allowed_parents << nil
287 287 end
288 288 unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent)
289 289 @allowed_parents << parent
290 290 end
291 291 @allowed_parents
292 292 end
293 293
294 294 # Sets the parent of the project with authorization check
295 295 def set_allowed_parent!(p)
296 296 unless p.nil? || p.is_a?(Project)
297 297 if p.to_s.blank?
298 298 p = nil
299 299 else
300 300 p = Project.find_by_id(p)
301 301 return false unless p
302 302 end
303 303 end
304 304 if p.nil?
305 305 if !new_record? && allowed_parents.empty?
306 306 return false
307 307 end
308 308 elsif !allowed_parents.include?(p)
309 309 return false
310 310 end
311 311 set_parent!(p)
312 312 end
313 313
314 314 # Sets the parent of the project
315 315 # Argument can be either a Project, a String, a Fixnum or nil
316 316 def set_parent!(p)
317 317 unless p.nil? || p.is_a?(Project)
318 318 if p.to_s.blank?
319 319 p = nil
320 320 else
321 321 p = Project.find_by_id(p)
322 322 return false unless p
323 323 end
324 324 end
325 325 if p == parent && !p.nil?
326 326 # Nothing to do
327 327 true
328 328 elsif p.nil? || (p.active? && move_possible?(p))
329 329 # Insert the project so that target's children or root projects stay alphabetically sorted
330 330 sibs = (p.nil? ? self.class.roots : p.children)
331 331 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
332 332 if to_be_inserted_before
333 333 move_to_left_of(to_be_inserted_before)
334 334 elsif p.nil?
335 335 if sibs.empty?
336 336 # move_to_root adds the project in first (ie. left) position
337 337 move_to_root
338 338 else
339 339 move_to_right_of(sibs.last) unless self == sibs.last
340 340 end
341 341 else
342 342 # move_to_child_of adds the project in last (ie.right) position
343 343 move_to_child_of(p)
344 344 end
345 345 Issue.update_versions_from_hierarchy_change(self)
346 346 true
347 347 else
348 348 # Can not move to the given target
349 349 false
350 350 end
351 351 end
352 352
353 353 # Returns an array of the trackers used by the project and its active sub projects
354 354 def rolled_up_trackers
355 355 @rolled_up_trackers ||=
356 356 Tracker.find(:all, :include => :projects,
357 357 :select => "DISTINCT #{Tracker.table_name}.*",
358 358 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
359 359 :order => "#{Tracker.table_name}.position")
360 360 end
361 361
362 362 # Closes open and locked project versions that are completed
363 363 def close_completed_versions
364 364 Version.transaction do
365 365 versions.find(:all, :conditions => {:status => %w(open locked)}).each do |version|
366 366 if version.completed?
367 367 version.update_attribute(:status, 'closed')
368 368 end
369 369 end
370 370 end
371 371 end
372 372
373 373 # Returns a scope of the Versions on subprojects
374 374 def rolled_up_versions
375 375 @rolled_up_versions ||=
376 376 Version.scoped(:include => :project,
377 377 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt])
378 378 end
379 379
380 380 # Returns a scope of the Versions used by the project
381 381 def shared_versions
382 382 @shared_versions ||=
383 383 Version.scoped(:include => :project,
384 384 :conditions => "#{Project.table_name}.id = #{id}" +
385 385 " OR (#{Project.table_name}.status = #{Project::STATUS_ACTIVE} AND (" +
386 386 " #{Version.table_name}.sharing = 'system'" +
387 387 " OR (#{Project.table_name}.lft >= #{root.lft} AND #{Project.table_name}.rgt <= #{root.rgt} AND #{Version.table_name}.sharing = 'tree')" +
388 388 " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
389 389 " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
390 390 "))")
391 391 end
392 392
393 393 # Returns a hash of project users grouped by role
394 394 def users_by_role
395 395 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
396 396 m.roles.each do |r|
397 397 h[r] ||= []
398 398 h[r] << m.user
399 399 end
400 400 h
401 401 end
402 402 end
403 403
404 404 # Deletes all project's members
405 405 def delete_all_members
406 406 me, mr = Member.table_name, MemberRole.table_name
407 407 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
408 408 Member.delete_all(['project_id = ?', id])
409 409 end
410 410
411 411 # Users issues can be assigned to
412 412 def assignable_users
413 413 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort
414 414 end
415 415
416 416 # Returns the mail adresses of users that should be always notified on project events
417 417 def recipients
418 418 notified_users.collect {|user| user.mail}
419 419 end
420 420
421 421 # Returns the users that should be notified on project events
422 422 def notified_users
423 423 # TODO: User part should be extracted to User#notify_about?
424 424 members.select {|m| m.mail_notification? || m.user.mail_notification == 'all'}.collect {|m| m.user}
425 425 end
426 426
427 427 # Returns an array of all custom fields enabled for project issues
428 428 # (explictly associated custom fields and custom fields enabled for all projects)
429 429 def all_issue_custom_fields
430 430 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
431 431 end
432 432
433 433 def project
434 434 self
435 435 end
436 436
437 437 def <=>(project)
438 438 name.downcase <=> project.name.downcase
439 439 end
440 440
441 441 def to_s
442 442 name
443 443 end
444 444
445 445 # Returns a short description of the projects (first lines)
446 446 def short_description(length = 255)
447 447 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
448 448 end
449 449
450 450 def css_classes
451 451 s = 'project'
452 452 s << ' root' if root?
453 453 s << ' child' if child?
454 454 s << (leaf? ? ' leaf' : ' parent')
455 455 s
456 456 end
457 457
458 458 # The earliest start date of a project, based on it's issues and versions
459 459 def start_date
460 460 [
461 461 issues.minimum('start_date'),
462 462 shared_versions.collect(&:effective_date),
463 463 shared_versions.collect(&:start_date)
464 464 ].flatten.compact.min
465 465 end
466 466
467 467 # The latest due date of an issue or version
468 468 def due_date
469 469 [
470 470 issues.maximum('due_date'),
471 471 shared_versions.collect(&:effective_date),
472 472 shared_versions.collect {|v| v.fixed_issues.maximum('due_date')}
473 473 ].flatten.compact.max
474 474 end
475 475
476 476 def overdue?
477 477 active? && !due_date.nil? && (due_date < Date.today)
478 478 end
479 479
480 480 # Returns the percent completed for this project, based on the
481 481 # progress on it's versions.
482 482 def completed_percent(options={:include_subprojects => false})
483 483 if options.delete(:include_subprojects)
484 484 total = self_and_descendants.collect(&:completed_percent).sum
485 485
486 486 total / self_and_descendants.count
487 487 else
488 488 if versions.count > 0
489 489 total = versions.collect(&:completed_pourcent).sum
490 490
491 491 total / versions.count
492 492 else
493 493 100
494 494 end
495 495 end
496 496 end
497 497
498 498 # Return true if this project is allowed to do the specified action.
499 499 # action can be:
500 500 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
501 501 # * a permission Symbol (eg. :edit_project)
502 502 def allows_to?(action)
503 503 if action.is_a? Hash
504 504 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
505 505 else
506 506 allowed_permissions.include? action
507 507 end
508 508 end
509 509
510 510 def module_enabled?(module_name)
511 511 module_name = module_name.to_s
512 512 enabled_modules.detect {|m| m.name == module_name}
513 513 end
514 514
515 515 def enabled_module_names=(module_names)
516 516 if module_names && module_names.is_a?(Array)
517 517 module_names = module_names.collect(&:to_s).reject(&:blank?)
518 518 # remove disabled modules
519 519 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
520 520 # add new modules
521 521 module_names.reject {|name| module_enabled?(name)}.each {|name| enabled_modules << EnabledModule.new(:name => name)}
522 522 else
523 523 enabled_modules.clear
524 524 end
525 525 end
526 526
527 527 # Returns an array of the enabled modules names
528 528 def enabled_module_names
529 529 enabled_modules.collect(&:name)
530 530 end
531 531
532 532 safe_attributes 'name',
533 533 'description',
534 534 'homepage',
535 535 'is_public',
536 536 'identifier',
537 537 'custom_field_values',
538 538 'custom_fields',
539 539 'tracker_ids',
540 540 'issue_custom_field_ids'
541 541
542 542 safe_attributes 'enabled_module_names',
543 543 :if => lambda {|project, user| project.new_record? || user.allowed_to?(:select_project_modules, project) }
544 544
545 545 # Returns an array of projects that are in this project's hierarchy
546 546 #
547 547 # Example: parents, children, siblings
548 548 def hierarchy
549 549 parents = project.self_and_ancestors || []
550 550 descendants = project.descendants || []
551 551 project_hierarchy = parents | descendants # Set union
552 552 end
553 553
554 554 # Returns an auto-generated project identifier based on the last identifier used
555 555 def self.next_identifier
556 556 p = Project.find(:first, :order => 'created_on DESC')
557 557 p.nil? ? nil : p.identifier.to_s.succ
558 558 end
559 559
560 560 # Copies and saves the Project instance based on the +project+.
561 561 # Duplicates the source project's:
562 562 # * Wiki
563 563 # * Versions
564 564 # * Categories
565 565 # * Issues
566 566 # * Members
567 567 # * Queries
568 568 #
569 569 # Accepts an +options+ argument to specify what to copy
570 570 #
571 571 # Examples:
572 572 # project.copy(1) # => copies everything
573 573 # project.copy(1, :only => 'members') # => copies members only
574 574 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
575 575 def copy(project, options={})
576 576 project = project.is_a?(Project) ? project : Project.find(project)
577 577
578 578 to_be_copied = %w(wiki versions issue_categories issues members queries boards)
579 579 to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
580 580
581 581 Project.transaction do
582 582 if save
583 583 reload
584 584 to_be_copied.each do |name|
585 585 send "copy_#{name}", project
586 586 end
587 587 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
588 588 save
589 589 end
590 590 end
591 591 end
592 592
593 593
594 594 # Copies +project+ and returns the new instance. This will not save
595 595 # the copy
596 596 def self.copy_from(project)
597 597 begin
598 598 project = project.is_a?(Project) ? project : Project.find(project)
599 599 if project
600 600 # clear unique attributes
601 601 attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt')
602 602 copy = Project.new(attributes)
603 603 copy.enabled_modules = project.enabled_modules
604 604 copy.trackers = project.trackers
605 605 copy.custom_values = project.custom_values.collect {|v| v.clone}
606 606 copy.issue_custom_fields = project.issue_custom_fields
607 607 return copy
608 608 else
609 609 return nil
610 610 end
611 611 rescue ActiveRecord::RecordNotFound
612 612 return nil
613 613 end
614 614 end
615 615
616 616 # Yields the given block for each project with its level in the tree
617 617 def self.project_tree(projects, &block)
618 618 ancestors = []
619 619 projects.sort_by(&:lft).each do |project|
620 620 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
621 621 ancestors.pop
622 622 end
623 623 yield project, ancestors.size
624 624 ancestors << project
625 625 end
626 626 end
627 627
628 628 private
629 629
630 630 # Copies wiki from +project+
631 631 def copy_wiki(project)
632 632 # Check that the source project has a wiki first
633 633 unless project.wiki.nil?
634 634 self.wiki ||= Wiki.new
635 635 wiki.attributes = project.wiki.attributes.dup.except("id", "project_id")
636 636 wiki_pages_map = {}
637 637 project.wiki.pages.each do |page|
638 638 # Skip pages without content
639 639 next if page.content.nil?
640 640 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on"))
641 641 new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id"))
642 642 new_wiki_page.content = new_wiki_content
643 643 wiki.pages << new_wiki_page
644 644 wiki_pages_map[page.id] = new_wiki_page
645 645 end
646 646 wiki.save
647 647 # Reproduce page hierarchy
648 648 project.wiki.pages.each do |page|
649 649 if page.parent_id && wiki_pages_map[page.id]
650 650 wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id]
651 651 wiki_pages_map[page.id].save
652 652 end
653 653 end
654 654 end
655 655 end
656 656
657 657 # Copies versions from +project+
658 658 def copy_versions(project)
659 659 project.versions.each do |version|
660 660 new_version = Version.new
661 661 new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on")
662 662 self.versions << new_version
663 663 end
664 664 end
665 665
666 666 # Copies issue categories from +project+
667 667 def copy_issue_categories(project)
668 668 project.issue_categories.each do |issue_category|
669 669 new_issue_category = IssueCategory.new
670 670 new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id")
671 671 self.issue_categories << new_issue_category
672 672 end
673 673 end
674 674
675 675 # Copies issues from +project+
676 676 def copy_issues(project)
677 677 # Stores the source issue id as a key and the copied issues as the
678 678 # value. Used to map the two togeather for issue relations.
679 679 issues_map = {}
680 680
681 681 # Get issues sorted by root_id, lft so that parent issues
682 682 # get copied before their children
683 683 project.issues.find(:all, :order => 'root_id, lft').each do |issue|
684 684 new_issue = Issue.new
685 685 new_issue.copy_from(issue)
686 686 new_issue.project = self
687 687 # Reassign fixed_versions by name, since names are unique per
688 688 # project and the versions for self are not yet saved
689 689 if issue.fixed_version
690 690 new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first
691 691 end
692 692 # Reassign the category by name, since names are unique per
693 693 # project and the categories for self are not yet saved
694 694 if issue.category
695 695 new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first
696 696 end
697 697 # Parent issue
698 698 if issue.parent_id
699 699 if copied_parent = issues_map[issue.parent_id]
700 700 new_issue.parent_issue_id = copied_parent.id
701 701 end
702 702 end
703 703
704 704 self.issues << new_issue
705 705 if new_issue.new_record?
706 706 logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info
707 707 else
708 708 issues_map[issue.id] = new_issue unless new_issue.new_record?
709 709 end
710 710 end
711 711
712 712 # Relations after in case issues related each other
713 713 project.issues.each do |issue|
714 714 new_issue = issues_map[issue.id]
715 715 unless new_issue
716 716 # Issue was not copied
717 717 next
718 718 end
719 719
720 720 # Relations
721 721 issue.relations_from.each do |source_relation|
722 722 new_issue_relation = IssueRelation.new
723 723 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
724 724 new_issue_relation.issue_to = issues_map[source_relation.issue_to_id]
725 725 if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations?
726 726 new_issue_relation.issue_to = source_relation.issue_to
727 727 end
728 728 new_issue.relations_from << new_issue_relation
729 729 end
730 730
731 731 issue.relations_to.each do |source_relation|
732 732 new_issue_relation = IssueRelation.new
733 733 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
734 734 new_issue_relation.issue_from = issues_map[source_relation.issue_from_id]
735 735 if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations?
736 736 new_issue_relation.issue_from = source_relation.issue_from
737 737 end
738 738 new_issue.relations_to << new_issue_relation
739 739 end
740 740 end
741 741 end
742 742
743 743 # Copies members from +project+
744 744 def copy_members(project)
745 745 # Copy users first, then groups to handle members with inherited and given roles
746 746 members_to_copy = []
747 747 members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)}
748 748 members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)}
749 749
750 750 members_to_copy.each do |member|
751 751 new_member = Member.new
752 752 new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on")
753 753 # only copy non inherited roles
754 754 # inherited roles will be added when copying the group membership
755 755 role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id)
756 756 next if role_ids.empty?
757 757 new_member.role_ids = role_ids
758 758 new_member.project = self
759 759 self.members << new_member
760 760 end
761 761 end
762 762
763 763 # Copies queries from +project+
764 764 def copy_queries(project)
765 765 project.queries.each do |query|
766 766 new_query = Query.new
767 767 new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria")
768 768 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
769 769 new_query.project = self
770 770 self.queries << new_query
771 771 end
772 772 end
773 773
774 774 # Copies boards from +project+
775 775 def copy_boards(project)
776 776 project.boards.each do |board|
777 777 new_board = Board.new
778 778 new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
779 779 new_board.project = self
780 780 self.boards << new_board
781 781 end
782 782 end
783 783
784 784 def allowed_permissions
785 785 @allowed_permissions ||= begin
786 786 module_names = enabled_modules.all(:select => :name).collect {|m| m.name}
787 787 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
788 788 end
789 789 end
790 790
791 791 def allowed_actions
792 792 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
793 793 end
794 794
795 795 # Returns all the active Systemwide and project specific activities
796 796 def active_activities
797 797 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
798 798
799 799 if overridden_activity_ids.empty?
800 800 return TimeEntryActivity.shared.active
801 801 else
802 802 return system_activities_and_project_overrides
803 803 end
804 804 end
805 805
806 806 # Returns all the Systemwide and project specific activities
807 807 # (inactive and active)
808 808 def all_activities
809 809 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
810 810
811 811 if overridden_activity_ids.empty?
812 812 return TimeEntryActivity.shared
813 813 else
814 814 return system_activities_and_project_overrides(true)
815 815 end
816 816 end
817 817
818 818 # Returns the systemwide active activities merged with the project specific overrides
819 819 def system_activities_and_project_overrides(include_inactive=false)
820 820 if include_inactive
821 821 return TimeEntryActivity.shared.
822 822 find(:all,
823 823 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
824 824 self.time_entry_activities
825 825 else
826 826 return TimeEntryActivity.shared.active.
827 827 find(:all,
828 828 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
829 829 self.time_entry_activities.active
830 830 end
831 831 end
832 832
833 833 # Archives subprojects recursively
834 834 def archive!
835 835 children.each do |subproject|
836 836 subproject.send :archive!
837 837 end
838 838 update_attribute :status, STATUS_ARCHIVED
839 839 end
840 840 end
@@ -1,1082 +1,1082
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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.expand_path('../../test_helper', __FILE__)
19 19
20 20 class ProjectTest < ActiveSupport::TestCase
21 21 fixtures :all
22 22
23 23 def setup
24 24 @ecookbook = Project.find(1)
25 25 @ecookbook_sub1 = Project.find(3)
26 26 User.current = nil
27 27 end
28 28
29 29 should_validate_presence_of :name
30 30 should_validate_presence_of :identifier
31 31
32 32 should_validate_uniqueness_of :identifier
33 33
34 34 context "associations" do
35 35 should_have_many :members
36 36 should_have_many :users, :through => :members
37 37 should_have_many :member_principals
38 38 should_have_many :principals, :through => :member_principals
39 39 should_have_many :enabled_modules
40 40 should_have_many :issues
41 41 should_have_many :issue_changes, :through => :issues
42 42 should_have_many :versions
43 43 should_have_many :time_entries
44 44 should_have_many :queries
45 45 should_have_many :documents
46 46 should_have_many :news
47 47 should_have_many :issue_categories
48 48 should_have_many :boards
49 49 should_have_many :changesets, :through => :repository
50 50
51 51 should_have_one :repository
52 52 should_have_one :wiki
53 53
54 54 should_have_and_belong_to_many :trackers
55 55 should_have_and_belong_to_many :issue_custom_fields
56 56 end
57 57
58 58 def test_truth
59 59 assert_kind_of Project, @ecookbook
60 60 assert_equal "eCookbook", @ecookbook.name
61 61 end
62 62
63 63 def test_default_attributes
64 64 with_settings :default_projects_public => '1' do
65 65 assert_equal true, Project.new.is_public
66 66 assert_equal false, Project.new(:is_public => false).is_public
67 67 end
68 68
69 69 with_settings :default_projects_public => '0' do
70 70 assert_equal false, Project.new.is_public
71 71 assert_equal true, Project.new(:is_public => true).is_public
72 72 end
73 73
74 74 with_settings :sequential_project_identifiers => '1' do
75 75 assert !Project.new.identifier.blank?
76 76 assert Project.new(:identifier => '').identifier.blank?
77 77 end
78 78
79 79 with_settings :sequential_project_identifiers => '0' do
80 80 assert Project.new.identifier.blank?
81 81 assert !Project.new(:identifier => 'test').blank?
82 82 end
83 83
84 84 with_settings :default_projects_modules => ['issue_tracking', 'repository'] do
85 85 assert_equal ['issue_tracking', 'repository'], Project.new.enabled_module_names
86 86 end
87 87
88 88 assert_equal Tracker.all, Project.new.trackers
89 89 assert_equal Tracker.find(1, 3), Project.new(:tracker_ids => [1, 3]).trackers
90 90 end
91 91
92 92 def test_update
93 93 assert_equal "eCookbook", @ecookbook.name
94 94 @ecookbook.name = "eCook"
95 95 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
96 96 @ecookbook.reload
97 97 assert_equal "eCook", @ecookbook.name
98 98 end
99 99
100 100 def test_validate_identifier
101 101 to_test = {"abc" => true,
102 102 "ab12" => true,
103 103 "ab-12" => true,
104 104 "12" => false,
105 105 "new" => false}
106 106
107 107 to_test.each do |identifier, valid|
108 108 p = Project.new
109 109 p.identifier = identifier
110 110 p.valid?
111 111 assert_equal valid, p.errors.on('identifier').nil?
112 112 end
113 113 end
114 114
115 115 def test_members_should_be_active_users
116 116 Project.all.each do |project|
117 117 assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) }
118 118 end
119 119 end
120 120
121 121 def test_users_should_be_active_users
122 122 Project.all.each do |project|
123 123 assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) }
124 124 end
125 125 end
126 126
127 127 def test_archive
128 128 user = @ecookbook.members.first.user
129 129 @ecookbook.archive
130 130 @ecookbook.reload
131 131
132 132 assert !@ecookbook.active?
133 133 assert @ecookbook.archived?
134 134 assert !user.projects.include?(@ecookbook)
135 135 # Subproject are also archived
136 136 assert !@ecookbook.children.empty?
137 137 assert @ecookbook.descendants.active.empty?
138 138 end
139 139
140 140 def test_archive_should_fail_if_versions_are_used_by_non_descendant_projects
141 141 # Assign an issue of a project to a version of a child project
142 142 Issue.find(4).update_attribute :fixed_version_id, 4
143 143
144 144 assert_no_difference "Project.count(:all, :conditions => 'status = #{Project::STATUS_ARCHIVED}')" do
145 145 assert_equal false, @ecookbook.archive
146 146 end
147 147 @ecookbook.reload
148 148 assert @ecookbook.active?
149 149 end
150 150
151 151 def test_unarchive
152 152 user = @ecookbook.members.first.user
153 153 @ecookbook.archive
154 154 # A subproject of an archived project can not be unarchived
155 155 assert !@ecookbook_sub1.unarchive
156 156
157 157 # Unarchive project
158 158 assert @ecookbook.unarchive
159 159 @ecookbook.reload
160 160 assert @ecookbook.active?
161 161 assert !@ecookbook.archived?
162 162 assert user.projects.include?(@ecookbook)
163 163 # Subproject can now be unarchived
164 164 @ecookbook_sub1.reload
165 165 assert @ecookbook_sub1.unarchive
166 166 end
167 167
168 168 def test_destroy
169 169 # 2 active members
170 170 assert_equal 2, @ecookbook.members.size
171 171 # and 1 is locked
172 172 assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
173 173 # some boards
174 174 assert @ecookbook.boards.any?
175 175
176 176 @ecookbook.destroy
177 177 # make sure that the project non longer exists
178 178 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
179 179 # make sure related data was removed
180 180 assert_nil Member.first(:conditions => {:project_id => @ecookbook.id})
181 181 assert_nil Board.first(:conditions => {:project_id => @ecookbook.id})
182 182 assert_nil Issue.first(:conditions => {:project_id => @ecookbook.id})
183 183 end
184 184
185 185 def test_destroying_root_projects_should_clear_data
186 186 Project.roots.each do |root|
187 187 root.destroy
188 188 end
189 189
190 190 assert_equal 0, Project.count, "Projects were not deleted: #{Project.all.inspect}"
191 191 assert_equal 0, Member.count, "Members were not deleted: #{Member.all.inspect}"
192 192 assert_equal 0, MemberRole.count
193 193 assert_equal 0, Issue.count
194 194 assert_equal 0, Journal.count
195 195 assert_equal 0, JournalDetail.count
196 196 assert_equal 0, Attachment.count
197 197 assert_equal 0, EnabledModule.count
198 198 assert_equal 0, IssueCategory.count
199 199 assert_equal 0, IssueRelation.count
200 200 assert_equal 0, Board.count
201 201 assert_equal 0, Message.count
202 202 assert_equal 0, News.count
203 203 assert_equal 0, Query.count(:conditions => "project_id IS NOT NULL")
204 204 assert_equal 0, Repository.count
205 205 assert_equal 0, Changeset.count
206 206 assert_equal 0, Change.count
207 #assert_equal 0, Comment.count
207 assert_equal 0, Comment.count
208 208 assert_equal 0, TimeEntry.count
209 209 assert_equal 0, Version.count
210 210 assert_equal 0, Watcher.count
211 211 assert_equal 0, Wiki.count
212 212 assert_equal 0, WikiPage.count
213 213 assert_equal 0, WikiContent.count
214 214 assert_equal 0, WikiContent::Version.count
215 215 assert_equal 0, Project.connection.select_all("SELECT * FROM projects_trackers").size
216 216 assert_equal 0, Project.connection.select_all("SELECT * FROM custom_fields_projects").size
217 217 assert_equal 0, CustomValue.count(:conditions => {:customized_type => ['Project', 'Issue', 'TimeEntry', 'Version']})
218 218 end
219 219
220 220 def test_move_an_orphan_project_to_a_root_project
221 221 sub = Project.find(2)
222 222 sub.set_parent! @ecookbook
223 223 assert_equal @ecookbook.id, sub.parent.id
224 224 @ecookbook.reload
225 225 assert_equal 4, @ecookbook.children.size
226 226 end
227 227
228 228 def test_move_an_orphan_project_to_a_subproject
229 229 sub = Project.find(2)
230 230 assert sub.set_parent!(@ecookbook_sub1)
231 231 end
232 232
233 233 def test_move_a_root_project_to_a_project
234 234 sub = @ecookbook
235 235 assert sub.set_parent!(Project.find(2))
236 236 end
237 237
238 238 def test_should_not_move_a_project_to_its_children
239 239 sub = @ecookbook
240 240 assert !(sub.set_parent!(Project.find(3)))
241 241 end
242 242
243 243 def test_set_parent_should_add_roots_in_alphabetical_order
244 244 ProjectCustomField.delete_all
245 245 Project.delete_all
246 246 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
247 247 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
248 248 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
249 249 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
250 250
251 251 assert_equal 4, Project.count
252 252 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
253 253 end
254 254
255 255 def test_set_parent_should_add_children_in_alphabetical_order
256 256 ProjectCustomField.delete_all
257 257 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
258 258 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
259 259 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
260 260 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
261 261 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
262 262
263 263 parent.reload
264 264 assert_equal 4, parent.children.size
265 265 assert_equal parent.children.sort_by(&:name), parent.children
266 266 end
267 267
268 268 def test_rebuild_should_sort_children_alphabetically
269 269 ProjectCustomField.delete_all
270 270 parent = Project.create!(:name => 'Parent', :identifier => 'parent')
271 271 Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
272 272 Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
273 273 Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
274 274 Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
275 275
276 276 Project.update_all("lft = NULL, rgt = NULL")
277 277 Project.rebuild!
278 278
279 279 parent.reload
280 280 assert_equal 4, parent.children.size
281 281 assert_equal parent.children.sort_by(&:name), parent.children
282 282 end
283 283
284 284
285 285 def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy
286 286 # Parent issue with a hierarchy project's fixed version
287 287 parent_issue = Issue.find(1)
288 288 parent_issue.update_attribute(:fixed_version_id, 4)
289 289 parent_issue.reload
290 290 assert_equal 4, parent_issue.fixed_version_id
291 291
292 292 # Should keep fixed versions for the issues
293 293 issue_with_local_fixed_version = Issue.find(5)
294 294 issue_with_local_fixed_version.update_attribute(:fixed_version_id, 4)
295 295 issue_with_local_fixed_version.reload
296 296 assert_equal 4, issue_with_local_fixed_version.fixed_version_id
297 297
298 298 # Local issue with hierarchy fixed_version
299 299 issue_with_hierarchy_fixed_version = Issue.find(13)
300 300 issue_with_hierarchy_fixed_version.update_attribute(:fixed_version_id, 6)
301 301 issue_with_hierarchy_fixed_version.reload
302 302 assert_equal 6, issue_with_hierarchy_fixed_version.fixed_version_id
303 303
304 304 # Move project out of the issue's hierarchy
305 305 moved_project = Project.find(3)
306 306 moved_project.set_parent!(Project.find(2))
307 307 parent_issue.reload
308 308 issue_with_local_fixed_version.reload
309 309 issue_with_hierarchy_fixed_version.reload
310 310
311 311 assert_equal 4, issue_with_local_fixed_version.fixed_version_id, "Fixed version was not keep on an issue local to the moved project"
312 312 assert_equal nil, issue_with_hierarchy_fixed_version.fixed_version_id, "Fixed version is still set after moving the Project out of the hierarchy where the version is defined in"
313 313 assert_equal nil, parent_issue.fixed_version_id, "Fixed version is still set after moving the Version out of the hierarchy for the issue."
314 314 end
315 315
316 316 def test_parent
317 317 p = Project.find(6).parent
318 318 assert p.is_a?(Project)
319 319 assert_equal 5, p.id
320 320 end
321 321
322 322 def test_ancestors
323 323 a = Project.find(6).ancestors
324 324 assert a.first.is_a?(Project)
325 325 assert_equal [1, 5], a.collect(&:id)
326 326 end
327 327
328 328 def test_root
329 329 r = Project.find(6).root
330 330 assert r.is_a?(Project)
331 331 assert_equal 1, r.id
332 332 end
333 333
334 334 def test_children
335 335 c = Project.find(1).children
336 336 assert c.first.is_a?(Project)
337 337 assert_equal [5, 3, 4], c.collect(&:id)
338 338 end
339 339
340 340 def test_descendants
341 341 d = Project.find(1).descendants
342 342 assert d.first.is_a?(Project)
343 343 assert_equal [5, 6, 3, 4], d.collect(&:id)
344 344 end
345 345
346 346 def test_allowed_parents_should_be_empty_for_non_member_user
347 347 Role.non_member.add_permission!(:add_project)
348 348 user = User.find(9)
349 349 assert user.memberships.empty?
350 350 User.current = user
351 351 assert Project.new.allowed_parents.compact.empty?
352 352 end
353 353
354 354 def test_allowed_parents_with_add_subprojects_permission
355 355 Role.find(1).remove_permission!(:add_project)
356 356 Role.find(1).add_permission!(:add_subprojects)
357 357 User.current = User.find(2)
358 358 # new project
359 359 assert !Project.new.allowed_parents.include?(nil)
360 360 assert Project.new.allowed_parents.include?(Project.find(1))
361 361 # existing root project
362 362 assert Project.find(1).allowed_parents.include?(nil)
363 363 # existing child
364 364 assert Project.find(3).allowed_parents.include?(Project.find(1))
365 365 assert !Project.find(3).allowed_parents.include?(nil)
366 366 end
367 367
368 368 def test_allowed_parents_with_add_project_permission
369 369 Role.find(1).add_permission!(:add_project)
370 370 Role.find(1).remove_permission!(:add_subprojects)
371 371 User.current = User.find(2)
372 372 # new project
373 373 assert Project.new.allowed_parents.include?(nil)
374 374 assert !Project.new.allowed_parents.include?(Project.find(1))
375 375 # existing root project
376 376 assert Project.find(1).allowed_parents.include?(nil)
377 377 # existing child
378 378 assert Project.find(3).allowed_parents.include?(Project.find(1))
379 379 assert Project.find(3).allowed_parents.include?(nil)
380 380 end
381 381
382 382 def test_allowed_parents_with_add_project_and_subprojects_permission
383 383 Role.find(1).add_permission!(:add_project)
384 384 Role.find(1).add_permission!(:add_subprojects)
385 385 User.current = User.find(2)
386 386 # new project
387 387 assert Project.new.allowed_parents.include?(nil)
388 388 assert Project.new.allowed_parents.include?(Project.find(1))
389 389 # existing root project
390 390 assert Project.find(1).allowed_parents.include?(nil)
391 391 # existing child
392 392 assert Project.find(3).allowed_parents.include?(Project.find(1))
393 393 assert Project.find(3).allowed_parents.include?(nil)
394 394 end
395 395
396 396 def test_users_by_role
397 397 users_by_role = Project.find(1).users_by_role
398 398 assert_kind_of Hash, users_by_role
399 399 role = Role.find(1)
400 400 assert_kind_of Array, users_by_role[role]
401 401 assert users_by_role[role].include?(User.find(2))
402 402 end
403 403
404 404 def test_rolled_up_trackers
405 405 parent = Project.find(1)
406 406 parent.trackers = Tracker.find([1,2])
407 407 child = parent.children.find(3)
408 408
409 409 assert_equal [1, 2], parent.tracker_ids
410 410 assert_equal [2, 3], child.trackers.collect(&:id)
411 411
412 412 assert_kind_of Tracker, parent.rolled_up_trackers.first
413 413 assert_equal Tracker.find(1), parent.rolled_up_trackers.first
414 414
415 415 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
416 416 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
417 417 end
418 418
419 419 def test_rolled_up_trackers_should_ignore_archived_subprojects
420 420 parent = Project.find(1)
421 421 parent.trackers = Tracker.find([1,2])
422 422 child = parent.children.find(3)
423 423 child.trackers = Tracker.find([1,3])
424 424 parent.children.each(&:archive)
425 425
426 426 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
427 427 end
428 428
429 429 context "#rolled_up_versions" do
430 430 setup do
431 431 @project = Project.generate!
432 432 @parent_version_1 = Version.generate!(:project => @project)
433 433 @parent_version_2 = Version.generate!(:project => @project)
434 434 end
435 435
436 436 should "include the versions for the current project" do
437 437 assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
438 438 end
439 439
440 440 should "include versions for a subproject" do
441 441 @subproject = Project.generate!
442 442 @subproject.set_parent!(@project)
443 443 @subproject_version = Version.generate!(:project => @subproject)
444 444
445 445 assert_same_elements [
446 446 @parent_version_1,
447 447 @parent_version_2,
448 448 @subproject_version
449 449 ], @project.rolled_up_versions
450 450 end
451 451
452 452 should "include versions for a sub-subproject" do
453 453 @subproject = Project.generate!
454 454 @subproject.set_parent!(@project)
455 455 @sub_subproject = Project.generate!
456 456 @sub_subproject.set_parent!(@subproject)
457 457 @sub_subproject_version = Version.generate!(:project => @sub_subproject)
458 458
459 459 @project.reload
460 460
461 461 assert_same_elements [
462 462 @parent_version_1,
463 463 @parent_version_2,
464 464 @sub_subproject_version
465 465 ], @project.rolled_up_versions
466 466 end
467 467
468 468
469 469 should "only check active projects" do
470 470 @subproject = Project.generate!
471 471 @subproject.set_parent!(@project)
472 472 @subproject_version = Version.generate!(:project => @subproject)
473 473 assert @subproject.archive
474 474
475 475 @project.reload
476 476
477 477 assert !@subproject.active?
478 478 assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
479 479 end
480 480 end
481 481
482 482 def test_shared_versions_none_sharing
483 483 p = Project.find(5)
484 484 v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none')
485 485 assert p.shared_versions.include?(v)
486 486 assert !p.children.first.shared_versions.include?(v)
487 487 assert !p.root.shared_versions.include?(v)
488 488 assert !p.siblings.first.shared_versions.include?(v)
489 489 assert !p.root.siblings.first.shared_versions.include?(v)
490 490 end
491 491
492 492 def test_shared_versions_descendants_sharing
493 493 p = Project.find(5)
494 494 v = Version.create!(:name => 'descendants_sharing', :project => p, :sharing => 'descendants')
495 495 assert p.shared_versions.include?(v)
496 496 assert p.children.first.shared_versions.include?(v)
497 497 assert !p.root.shared_versions.include?(v)
498 498 assert !p.siblings.first.shared_versions.include?(v)
499 499 assert !p.root.siblings.first.shared_versions.include?(v)
500 500 end
501 501
502 502 def test_shared_versions_hierarchy_sharing
503 503 p = Project.find(5)
504 504 v = Version.create!(:name => 'hierarchy_sharing', :project => p, :sharing => 'hierarchy')
505 505 assert p.shared_versions.include?(v)
506 506 assert p.children.first.shared_versions.include?(v)
507 507 assert p.root.shared_versions.include?(v)
508 508 assert !p.siblings.first.shared_versions.include?(v)
509 509 assert !p.root.siblings.first.shared_versions.include?(v)
510 510 end
511 511
512 512 def test_shared_versions_tree_sharing
513 513 p = Project.find(5)
514 514 v = Version.create!(:name => 'tree_sharing', :project => p, :sharing => 'tree')
515 515 assert p.shared_versions.include?(v)
516 516 assert p.children.first.shared_versions.include?(v)
517 517 assert p.root.shared_versions.include?(v)
518 518 assert p.siblings.first.shared_versions.include?(v)
519 519 assert !p.root.siblings.first.shared_versions.include?(v)
520 520 end
521 521
522 522 def test_shared_versions_system_sharing
523 523 p = Project.find(5)
524 524 v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system')
525 525 assert p.shared_versions.include?(v)
526 526 assert p.children.first.shared_versions.include?(v)
527 527 assert p.root.shared_versions.include?(v)
528 528 assert p.siblings.first.shared_versions.include?(v)
529 529 assert p.root.siblings.first.shared_versions.include?(v)
530 530 end
531 531
532 532 def test_shared_versions
533 533 parent = Project.find(1)
534 534 child = parent.children.find(3)
535 535 private_child = parent.children.find(5)
536 536
537 537 assert_equal [1,2,3], parent.version_ids.sort
538 538 assert_equal [4], child.version_ids
539 539 assert_equal [6], private_child.version_ids
540 540 assert_equal [7], Version.find_all_by_sharing('system').collect(&:id)
541 541
542 542 assert_equal 6, parent.shared_versions.size
543 543 parent.shared_versions.each do |version|
544 544 assert_kind_of Version, version
545 545 end
546 546
547 547 assert_equal [1,2,3,4,6,7], parent.shared_versions.collect(&:id).sort
548 548 end
549 549
550 550 def test_shared_versions_should_ignore_archived_subprojects
551 551 parent = Project.find(1)
552 552 child = parent.children.find(3)
553 553 child.archive
554 554 parent.reload
555 555
556 556 assert_equal [1,2,3], parent.version_ids.sort
557 557 assert_equal [4], child.version_ids
558 558 assert !parent.shared_versions.collect(&:id).include?(4)
559 559 end
560 560
561 561 def test_shared_versions_visible_to_user
562 562 user = User.find(3)
563 563 parent = Project.find(1)
564 564 child = parent.children.find(5)
565 565
566 566 assert_equal [1,2,3], parent.version_ids.sort
567 567 assert_equal [6], child.version_ids
568 568
569 569 versions = parent.shared_versions.visible(user)
570 570
571 571 assert_equal 4, versions.size
572 572 versions.each do |version|
573 573 assert_kind_of Version, version
574 574 end
575 575
576 576 assert !versions.collect(&:id).include?(6)
577 577 end
578 578
579 579
580 580 def test_next_identifier
581 581 ProjectCustomField.delete_all
582 582 Project.create!(:name => 'last', :identifier => 'p2008040')
583 583 assert_equal 'p2008041', Project.next_identifier
584 584 end
585 585
586 586 def test_next_identifier_first_project
587 587 Project.delete_all
588 588 assert_nil Project.next_identifier
589 589 end
590 590
591 591
592 592 def test_enabled_module_names_should_not_recreate_enabled_modules
593 593 project = Project.find(1)
594 594 # Remove one module
595 595 modules = project.enabled_modules.slice(0..-2)
596 596 assert modules.any?
597 597 assert_difference 'EnabledModule.count', -1 do
598 598 project.enabled_module_names = modules.collect(&:name)
599 599 end
600 600 project.reload
601 601 # Ids should be preserved
602 602 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
603 603 end
604 604
605 605 def test_copy_from_existing_project
606 606 source_project = Project.find(1)
607 607 copied_project = Project.copy_from(1)
608 608
609 609 assert copied_project
610 610 # Cleared attributes
611 611 assert copied_project.id.blank?
612 612 assert copied_project.name.blank?
613 613 assert copied_project.identifier.blank?
614 614
615 615 # Duplicated attributes
616 616 assert_equal source_project.description, copied_project.description
617 617 assert_equal source_project.enabled_modules, copied_project.enabled_modules
618 618 assert_equal source_project.trackers, copied_project.trackers
619 619
620 620 # Default attributes
621 621 assert_equal 1, copied_project.status
622 622 end
623 623
624 624 def test_activities_should_use_the_system_activities
625 625 project = Project.find(1)
626 626 assert_equal project.activities, TimeEntryActivity.find(:all, :conditions => {:active => true} )
627 627 end
628 628
629 629
630 630 def test_activities_should_use_the_project_specific_activities
631 631 project = Project.find(1)
632 632 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project})
633 633 assert overridden_activity.save!
634 634
635 635 assert project.activities.include?(overridden_activity), "Project specific Activity not found"
636 636 end
637 637
638 638 def test_activities_should_not_include_the_inactive_project_specific_activities
639 639 project = Project.find(1)
640 640 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
641 641 assert overridden_activity.save!
642 642
643 643 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found"
644 644 end
645 645
646 646 def test_activities_should_not_include_project_specific_activities_from_other_projects
647 647 project = Project.find(1)
648 648 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)})
649 649 assert overridden_activity.save!
650 650
651 651 assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project"
652 652 end
653 653
654 654 def test_activities_should_handle_nils
655 655 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(1), :parent => TimeEntryActivity.find(:first)})
656 656 TimeEntryActivity.delete_all
657 657
658 658 # No activities
659 659 project = Project.find(1)
660 660 assert project.activities.empty?
661 661
662 662 # No system, one overridden
663 663 assert overridden_activity.save!
664 664 project.reload
665 665 assert_equal [overridden_activity], project.activities
666 666 end
667 667
668 668 def test_activities_should_override_system_activities_with_project_activities
669 669 project = Project.find(1)
670 670 parent_activity = TimeEntryActivity.find(:first)
671 671 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity})
672 672 assert overridden_activity.save!
673 673
674 674 assert project.activities.include?(overridden_activity), "Project specific Activity not found"
675 675 assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden"
676 676 end
677 677
678 678 def test_activities_should_include_inactive_activities_if_specified
679 679 project = Project.find(1)
680 680 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
681 681 assert overridden_activity.save!
682 682
683 683 assert project.activities(true).include?(overridden_activity), "Inactive Project specific Activity not found"
684 684 end
685 685
686 686 test 'activities should not include active System activities if the project has an override that is inactive' do
687 687 project = Project.find(1)
688 688 system_activity = TimeEntryActivity.find_by_name('Design')
689 689 assert system_activity.active?
690 690 overridden_activity = TimeEntryActivity.generate!(:project => project, :parent => system_activity, :active => false)
691 691 assert overridden_activity.save!
692 692
693 693 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found"
694 694 assert !project.activities.include?(system_activity), "System activity found when the project has an inactive override"
695 695 end
696 696
697 697 def test_close_completed_versions
698 698 Version.update_all("status = 'open'")
699 699 project = Project.find(1)
700 700 assert_not_nil project.versions.detect {|v| v.completed? && v.status == 'open'}
701 701 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
702 702 project.close_completed_versions
703 703 project.reload
704 704 assert_nil project.versions.detect {|v| v.completed? && v.status != 'closed'}
705 705 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
706 706 end
707 707
708 708 context "Project#copy" do
709 709 setup do
710 710 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
711 711 Project.destroy_all :identifier => "copy-test"
712 712 @source_project = Project.find(2)
713 713 @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
714 714 @project.trackers = @source_project.trackers
715 715 @project.enabled_module_names = @source_project.enabled_modules.collect(&:name)
716 716 end
717 717
718 718 should "copy issues" do
719 719 @source_project.issues << Issue.generate!(:status => IssueStatus.find_by_name('Closed'),
720 720 :subject => "copy issue status",
721 721 :tracker_id => 1,
722 722 :assigned_to_id => 2,
723 723 :project_id => @source_project.id)
724 724 assert @project.valid?
725 725 assert @project.issues.empty?
726 726 assert @project.copy(@source_project)
727 727
728 728 assert_equal @source_project.issues.size, @project.issues.size
729 729 @project.issues.each do |issue|
730 730 assert issue.valid?
731 731 assert ! issue.assigned_to.blank?
732 732 assert_equal @project, issue.project
733 733 end
734 734
735 735 copied_issue = @project.issues.first(:conditions => {:subject => "copy issue status"})
736 736 assert copied_issue
737 737 assert copied_issue.status
738 738 assert_equal "Closed", copied_issue.status.name
739 739 end
740 740
741 741 should "change the new issues to use the copied version" do
742 742 User.current = User.find(1)
743 743 assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open')
744 744 @source_project.versions << assigned_version
745 745 assert_equal 3, @source_project.versions.size
746 746 Issue.generate_for_project!(@source_project,
747 747 :fixed_version_id => assigned_version.id,
748 748 :subject => "change the new issues to use the copied version",
749 749 :tracker_id => 1,
750 750 :project_id => @source_project.id)
751 751
752 752 assert @project.copy(@source_project)
753 753 @project.reload
754 754 copied_issue = @project.issues.first(:conditions => {:subject => "change the new issues to use the copied version"})
755 755
756 756 assert copied_issue
757 757 assert copied_issue.fixed_version
758 758 assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
759 759 assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record
760 760 end
761 761
762 762 should "copy issue relations" do
763 763 Setting.cross_project_issue_relations = '1'
764 764
765 765 second_issue = Issue.generate!(:status_id => 5,
766 766 :subject => "copy issue relation",
767 767 :tracker_id => 1,
768 768 :assigned_to_id => 2,
769 769 :project_id => @source_project.id)
770 770 source_relation = IssueRelation.generate!(:issue_from => Issue.find(4),
771 771 :issue_to => second_issue,
772 772 :relation_type => "relates")
773 773 source_relation_cross_project = IssueRelation.generate!(:issue_from => Issue.find(1),
774 774 :issue_to => second_issue,
775 775 :relation_type => "duplicates")
776 776
777 777 assert @project.copy(@source_project)
778 778 assert_equal @source_project.issues.count, @project.issues.count
779 779 copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4
780 780 copied_second_issue = @project.issues.find_by_subject("copy issue relation")
781 781
782 782 # First issue with a relation on project
783 783 assert_equal 1, copied_issue.relations.size, "Relation not copied"
784 784 copied_relation = copied_issue.relations.first
785 785 assert_equal "relates", copied_relation.relation_type
786 786 assert_equal copied_second_issue.id, copied_relation.issue_to_id
787 787 assert_not_equal source_relation.id, copied_relation.id
788 788
789 789 # Second issue with a cross project relation
790 790 assert_equal 2, copied_second_issue.relations.size, "Relation not copied"
791 791 copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first
792 792 assert_equal "duplicates", copied_relation.relation_type
793 793 assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept"
794 794 assert_not_equal source_relation_cross_project.id, copied_relation.id
795 795 end
796 796
797 797 should "copy memberships" do
798 798 assert @project.valid?
799 799 assert @project.members.empty?
800 800 assert @project.copy(@source_project)
801 801
802 802 assert_equal @source_project.memberships.size, @project.memberships.size
803 803 @project.memberships.each do |membership|
804 804 assert membership
805 805 assert_equal @project, membership.project
806 806 end
807 807 end
808 808
809 809 should "copy memberships with groups and additional roles" do
810 810 group = Group.create!(:lastname => "Copy group")
811 811 user = User.find(7)
812 812 group.users << user
813 813 # group role
814 814 Member.create!(:project_id => @source_project.id, :principal => group, :role_ids => [2])
815 815 member = Member.find_by_user_id_and_project_id(user.id, @source_project.id)
816 816 # additional role
817 817 member.role_ids = [1]
818 818
819 819 assert @project.copy(@source_project)
820 820 member = Member.find_by_user_id_and_project_id(user.id, @project.id)
821 821 assert_not_nil member
822 822 assert_equal [1, 2], member.role_ids.sort
823 823 end
824 824
825 825 should "copy project specific queries" do
826 826 assert @project.valid?
827 827 assert @project.queries.empty?
828 828 assert @project.copy(@source_project)
829 829
830 830 assert_equal @source_project.queries.size, @project.queries.size
831 831 @project.queries.each do |query|
832 832 assert query
833 833 assert_equal @project, query.project
834 834 end
835 835 end
836 836
837 837 should "copy versions" do
838 838 @source_project.versions << Version.generate!
839 839 @source_project.versions << Version.generate!
840 840
841 841 assert @project.versions.empty?
842 842 assert @project.copy(@source_project)
843 843
844 844 assert_equal @source_project.versions.size, @project.versions.size
845 845 @project.versions.each do |version|
846 846 assert version
847 847 assert_equal @project, version.project
848 848 end
849 849 end
850 850
851 851 should "copy wiki" do
852 852 assert_difference 'Wiki.count' do
853 853 assert @project.copy(@source_project)
854 854 end
855 855
856 856 assert @project.wiki
857 857 assert_not_equal @source_project.wiki, @project.wiki
858 858 assert_equal "Start page", @project.wiki.start_page
859 859 end
860 860
861 861 should "copy wiki pages and content with hierarchy" do
862 862 assert_difference 'WikiPage.count', @source_project.wiki.pages.size do
863 863 assert @project.copy(@source_project)
864 864 end
865 865
866 866 assert @project.wiki
867 867 assert_equal @source_project.wiki.pages.size, @project.wiki.pages.size
868 868
869 869 @project.wiki.pages.each do |wiki_page|
870 870 assert wiki_page.content
871 871 assert !@source_project.wiki.pages.include?(wiki_page)
872 872 end
873 873
874 874 parent = @project.wiki.find_page('Parent_page')
875 875 child1 = @project.wiki.find_page('Child_page_1')
876 876 child2 = @project.wiki.find_page('Child_page_2')
877 877 assert_equal parent, child1.parent
878 878 assert_equal parent, child2.parent
879 879 end
880 880
881 881 should "copy issue categories" do
882 882 assert @project.copy(@source_project)
883 883
884 884 assert_equal 2, @project.issue_categories.size
885 885 @project.issue_categories.each do |issue_category|
886 886 assert !@source_project.issue_categories.include?(issue_category)
887 887 end
888 888 end
889 889
890 890 should "copy boards" do
891 891 assert @project.copy(@source_project)
892 892
893 893 assert_equal 1, @project.boards.size
894 894 @project.boards.each do |board|
895 895 assert !@source_project.boards.include?(board)
896 896 end
897 897 end
898 898
899 899 should "change the new issues to use the copied issue categories" do
900 900 issue = Issue.find(4)
901 901 issue.update_attribute(:category_id, 3)
902 902
903 903 assert @project.copy(@source_project)
904 904
905 905 @project.issues.each do |issue|
906 906 assert issue.category
907 907 assert_equal "Stock management", issue.category.name # Same name
908 908 assert_not_equal IssueCategory.find(3), issue.category # Different record
909 909 end
910 910 end
911 911
912 912 should "limit copy with :only option" do
913 913 assert @project.members.empty?
914 914 assert @project.issue_categories.empty?
915 915 assert @source_project.issues.any?
916 916
917 917 assert @project.copy(@source_project, :only => ['members', 'issue_categories'])
918 918
919 919 assert @project.members.any?
920 920 assert @project.issue_categories.any?
921 921 assert @project.issues.empty?
922 922 end
923 923
924 924 end
925 925
926 926 context "#start_date" do
927 927 setup do
928 928 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
929 929 @project = Project.generate!(:identifier => 'test0')
930 930 @project.trackers << Tracker.generate!
931 931 end
932 932
933 933 should "be nil if there are no issues on the project" do
934 934 assert_nil @project.start_date
935 935 end
936 936
937 937 should "be tested when issues have no start date"
938 938
939 939 should "be the earliest start date of it's issues" do
940 940 early = 7.days.ago.to_date
941 941 Issue.generate_for_project!(@project, :start_date => Date.today)
942 942 Issue.generate_for_project!(@project, :start_date => early)
943 943
944 944 assert_equal early, @project.start_date
945 945 end
946 946
947 947 end
948 948
949 949 context "#due_date" do
950 950 setup do
951 951 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
952 952 @project = Project.generate!(:identifier => 'test0')
953 953 @project.trackers << Tracker.generate!
954 954 end
955 955
956 956 should "be nil if there are no issues on the project" do
957 957 assert_nil @project.due_date
958 958 end
959 959
960 960 should "be tested when issues have no due date"
961 961
962 962 should "be the latest due date of it's issues" do
963 963 future = 7.days.from_now.to_date
964 964 Issue.generate_for_project!(@project, :due_date => future)
965 965 Issue.generate_for_project!(@project, :due_date => Date.today)
966 966
967 967 assert_equal future, @project.due_date
968 968 end
969 969
970 970 should "be the latest due date of it's versions" do
971 971 future = 7.days.from_now.to_date
972 972 @project.versions << Version.generate!(:effective_date => future)
973 973 @project.versions << Version.generate!(:effective_date => Date.today)
974 974
975 975
976 976 assert_equal future, @project.due_date
977 977
978 978 end
979 979
980 980 should "pick the latest date from it's issues and versions" do
981 981 future = 7.days.from_now.to_date
982 982 far_future = 14.days.from_now.to_date
983 983 Issue.generate_for_project!(@project, :due_date => far_future)
984 984 @project.versions << Version.generate!(:effective_date => future)
985 985
986 986 assert_equal far_future, @project.due_date
987 987 end
988 988
989 989 end
990 990
991 991 context "Project#completed_percent" do
992 992 setup do
993 993 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
994 994 @project = Project.generate!(:identifier => 'test0')
995 995 @project.trackers << Tracker.generate!
996 996 end
997 997
998 998 context "no versions" do
999 999 should "be 100" do
1000 1000 assert_equal 100, @project.completed_percent
1001 1001 end
1002 1002 end
1003 1003
1004 1004 context "with versions" do
1005 1005 should "return 0 if the versions have no issues" do
1006 1006 Version.generate!(:project => @project)
1007 1007 Version.generate!(:project => @project)
1008 1008
1009 1009 assert_equal 0, @project.completed_percent
1010 1010 end
1011 1011
1012 1012 should "return 100 if the version has only closed issues" do
1013 1013 v1 = Version.generate!(:project => @project)
1014 1014 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v1)
1015 1015 v2 = Version.generate!(:project => @project)
1016 1016 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v2)
1017 1017
1018 1018 assert_equal 100, @project.completed_percent
1019 1019 end
1020 1020
1021 1021 should "return the averaged completed percent of the versions (not weighted)" do
1022 1022 v1 = Version.generate!(:project => @project)
1023 1023 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v1)
1024 1024 v2 = Version.generate!(:project => @project)
1025 1025 Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v2)
1026 1026
1027 1027 assert_equal 50, @project.completed_percent
1028 1028 end
1029 1029
1030 1030 end
1031 1031 end
1032 1032
1033 1033 context "#notified_users" do
1034 1034 setup do
1035 1035 @project = Project.generate!
1036 1036 @role = Role.generate!
1037 1037
1038 1038 @user_with_membership_notification = User.generate!(:mail_notification => 'selected')
1039 1039 Member.generate!(:project => @project, :roles => [@role], :principal => @user_with_membership_notification, :mail_notification => true)
1040 1040
1041 1041 @all_events_user = User.generate!(:mail_notification => 'all')
1042 1042 Member.generate!(:project => @project, :roles => [@role], :principal => @all_events_user)
1043 1043
1044 1044 @no_events_user = User.generate!(:mail_notification => 'none')
1045 1045 Member.generate!(:project => @project, :roles => [@role], :principal => @no_events_user)
1046 1046
1047 1047 @only_my_events_user = User.generate!(:mail_notification => 'only_my_events')
1048 1048 Member.generate!(:project => @project, :roles => [@role], :principal => @only_my_events_user)
1049 1049
1050 1050 @only_assigned_user = User.generate!(:mail_notification => 'only_assigned')
1051 1051 Member.generate!(:project => @project, :roles => [@role], :principal => @only_assigned_user)
1052 1052
1053 1053 @only_owned_user = User.generate!(:mail_notification => 'only_owner')
1054 1054 Member.generate!(:project => @project, :roles => [@role], :principal => @only_owned_user)
1055 1055 end
1056 1056
1057 1057 should "include members with a mail notification" do
1058 1058 assert @project.notified_users.include?(@user_with_membership_notification)
1059 1059 end
1060 1060
1061 1061 should "include users with the 'all' notification option" do
1062 1062 assert @project.notified_users.include?(@all_events_user)
1063 1063 end
1064 1064
1065 1065 should "not include users with the 'none' notification option" do
1066 1066 assert !@project.notified_users.include?(@no_events_user)
1067 1067 end
1068 1068
1069 1069 should "not include users with the 'only_my_events' notification option" do
1070 1070 assert !@project.notified_users.include?(@only_my_events_user)
1071 1071 end
1072 1072
1073 1073 should "not include users with the 'only_assigned' notification option" do
1074 1074 assert !@project.notified_users.include?(@only_assigned_user)
1075 1075 end
1076 1076
1077 1077 should "not include users with the 'only_owner' notification option" do
1078 1078 assert !@project.notified_users.include?(@only_owned_user)
1079 1079 end
1080 1080 end
1081 1081
1082 1082 end
General Comments 0
You need to be logged in to leave comments. Login now