##// END OF EJS Templates
Error message when editing a child project without add project/subprojects permissions (#20282)....
Jean-Philippe Lang -
r14237:64a80066bd86
parent child
Show More
@@ -1,1040 +1,1042
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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 include Redmine::NestedSet::ProjectNestedSet
21 21
22 22 # Project statuses
23 23 STATUS_ACTIVE = 1
24 24 STATUS_CLOSED = 5
25 25 STATUS_ARCHIVED = 9
26 26
27 27 # Maximum length for project identifiers
28 28 IDENTIFIER_MAX_LENGTH = 100
29 29
30 30 # Specific overridden Activities
31 31 has_many :time_entry_activities
32 32 has_many :memberships, :class_name => 'Member', :inverse_of => :project
33 33 # Memberships of active users only
34 34 has_many :members,
35 35 lambda { joins(:principal).where(:users => {:type => 'User', :status => Principal::STATUS_ACTIVE}) }
36 36 has_many :enabled_modules, :dependent => :delete_all
37 37 has_and_belongs_to_many :trackers, lambda {order(:position)}
38 38 has_many :issues, :dependent => :destroy
39 39 has_many :issue_changes, :through => :issues, :source => :journals
40 40 has_many :versions, lambda {order("#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC")}, :dependent => :destroy
41 41 has_many :time_entries, :dependent => :destroy
42 42 has_many :queries, :class_name => 'IssueQuery', :dependent => :delete_all
43 43 has_many :documents, :dependent => :destroy
44 44 has_many :news, lambda {includes(:author)}, :dependent => :destroy
45 45 has_many :issue_categories, lambda {order("#{IssueCategory.table_name}.name")}, :dependent => :delete_all
46 46 has_many :boards, lambda {order("position ASC")}, :dependent => :destroy
47 47 has_one :repository, lambda {where(["is_default = ?", true])}
48 48 has_many :repositories, :dependent => :destroy
49 49 has_many :changesets, :through => :repository
50 50 has_one :wiki, :dependent => :destroy
51 51 # Custom field for the project issues
52 52 has_and_belongs_to_many :issue_custom_fields,
53 53 lambda {order("#{CustomField.table_name}.position")},
54 54 :class_name => 'IssueCustomField',
55 55 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
56 56 :association_foreign_key => 'custom_field_id'
57 57
58 58 acts_as_attachable :view_permission => :view_files,
59 59 :edit_permission => :manage_files,
60 60 :delete_permission => :manage_files
61 61
62 62 acts_as_customizable
63 63 acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => "#{Project.table_name}.id", :permission => nil
64 64 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
65 65 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}},
66 66 :author => nil
67 67
68 68 attr_protected :status
69 69
70 70 validates_presence_of :name, :identifier
71 71 validates_uniqueness_of :identifier, :if => Proc.new {|p| p.identifier_changed?}
72 72 validates_length_of :name, :maximum => 255
73 73 validates_length_of :homepage, :maximum => 255
74 74 validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH
75 75 # downcase letters, digits, dashes but not digits only
76 76 validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :if => Proc.new { |p| p.identifier_changed? }
77 77 # reserved words
78 78 validates_exclusion_of :identifier, :in => %w( new )
79 79 validate :validate_parent
80 80
81 81 after_save :update_inherited_members, :if => Proc.new {|project| project.inherit_members_changed?}
82 82 after_save :remove_inherited_member_roles, :add_inherited_member_roles, :if => Proc.new {|project| project.parent_id_changed?}
83 83 after_update :update_versions_from_hierarchy_change, :if => Proc.new {|project| project.parent_id_changed?}
84 84 before_destroy :delete_all_members
85 85
86 86 scope :has_module, lambda {|mod|
87 87 where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s)
88 88 }
89 89 scope :active, lambda { where(:status => STATUS_ACTIVE) }
90 90 scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
91 91 scope :all_public, lambda { where(:is_public => true) }
92 92 scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) }
93 93 scope :allowed_to, lambda {|*args|
94 94 user = User.current
95 95 permission = nil
96 96 if args.first.is_a?(Symbol)
97 97 permission = args.shift
98 98 else
99 99 user = args.shift
100 100 permission = args.shift
101 101 end
102 102 where(Project.allowed_to_condition(user, permission, *args))
103 103 }
104 104 scope :like, lambda {|arg|
105 105 if arg.blank?
106 106 where(nil)
107 107 else
108 108 pattern = "%#{arg.to_s.strip.downcase}%"
109 109 where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", :p => pattern)
110 110 end
111 111 }
112 112 scope :sorted, lambda {order(:lft)}
113 113 scope :having_trackers, lambda {
114 114 where("#{Project.table_name}.id IN (SELECT DISTINCT project_id FROM #{table_name_prefix}projects_trackers#{table_name_suffix})")
115 115 }
116 116
117 117 def initialize(attributes=nil, *args)
118 118 super
119 119
120 120 initialized = (attributes || {}).stringify_keys
121 121 if !initialized.key?('identifier') && Setting.sequential_project_identifiers?
122 122 self.identifier = Project.next_identifier
123 123 end
124 124 if !initialized.key?('is_public')
125 125 self.is_public = Setting.default_projects_public?
126 126 end
127 127 if !initialized.key?('enabled_module_names')
128 128 self.enabled_module_names = Setting.default_projects_modules
129 129 end
130 130 if !initialized.key?('trackers') && !initialized.key?('tracker_ids')
131 131 default = Setting.default_projects_tracker_ids
132 132 if default.is_a?(Array)
133 133 self.trackers = Tracker.where(:id => default.map(&:to_i)).sorted.to_a
134 134 else
135 135 self.trackers = Tracker.sorted.to_a
136 136 end
137 137 end
138 138 end
139 139
140 140 def identifier=(identifier)
141 141 super unless identifier_frozen?
142 142 end
143 143
144 144 def identifier_frozen?
145 145 errors[:identifier].blank? && !(new_record? || identifier.blank?)
146 146 end
147 147
148 148 # returns latest created projects
149 149 # non public projects will be returned only if user is a member of those
150 150 def self.latest(user=nil, count=5)
151 151 visible(user).limit(count).order("created_on DESC").to_a
152 152 end
153 153
154 154 # Returns true if the project is visible to +user+ or to the current user.
155 155 def visible?(user=User.current)
156 156 user.allowed_to?(:view_project, self)
157 157 end
158 158
159 159 # Returns a SQL conditions string used to find all projects visible by the specified user.
160 160 #
161 161 # Examples:
162 162 # Project.visible_condition(admin) => "projects.status = 1"
163 163 # Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))"
164 164 # Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))"
165 165 def self.visible_condition(user, options={})
166 166 allowed_to_condition(user, :view_project, options)
167 167 end
168 168
169 169 # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+
170 170 #
171 171 # Valid options:
172 172 # * :project => limit the condition to project
173 173 # * :with_subprojects => limit the condition to project and its subprojects
174 174 # * :member => limit the condition to the user projects
175 175 def self.allowed_to_condition(user, permission, options={})
176 176 perm = Redmine::AccessControl.permission(permission)
177 177 base_statement = (perm && perm.read? ? "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED}" : "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}")
178 178 if perm && perm.project_module
179 179 # If the permission belongs to a project module, make sure the module is enabled
180 180 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
181 181 end
182 182 if project = options[:project]
183 183 project_statement = project.project_condition(options[:with_subprojects])
184 184 base_statement = "(#{project_statement}) AND (#{base_statement})"
185 185 end
186 186
187 187 if user.admin?
188 188 base_statement
189 189 else
190 190 statement_by_role = {}
191 191 unless options[:member]
192 192 role = user.builtin_role
193 193 if role.allowed_to?(permission)
194 194 s = "#{Project.table_name}.is_public = #{connection.quoted_true}"
195 195 if user.id
196 196 s = "(#{s} AND #{Project.table_name}.id NOT IN (SELECT project_id FROM #{Member.table_name} WHERE user_id = #{user.id}))"
197 197 end
198 198 statement_by_role[role] = s
199 199 end
200 200 end
201 201 user.projects_by_role.each do |role, projects|
202 202 if role.allowed_to?(permission) && projects.any?
203 203 statement_by_role[role] = "#{Project.table_name}.id IN (#{projects.collect(&:id).join(',')})"
204 204 end
205 205 end
206 206 if statement_by_role.empty?
207 207 "1=0"
208 208 else
209 209 if block_given?
210 210 statement_by_role.each do |role, statement|
211 211 if s = yield(role, user)
212 212 statement_by_role[role] = "(#{statement} AND (#{s}))"
213 213 end
214 214 end
215 215 end
216 216 "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))"
217 217 end
218 218 end
219 219 end
220 220
221 221 def override_roles(role)
222 222 @override_members ||= memberships.
223 223 joins(:principal).
224 224 where(:users => {:type => ['GroupAnonymous', 'GroupNonMember']}).to_a
225 225
226 226 group_class = role.anonymous? ? GroupAnonymous : GroupNonMember
227 227 member = @override_members.detect {|m| m.principal.is_a? group_class}
228 228 member ? member.roles.to_a : [role]
229 229 end
230 230
231 231 def principals
232 232 @principals ||= Principal.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).uniq
233 233 end
234 234
235 235 def users
236 236 @users ||= User.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).uniq
237 237 end
238 238
239 239 # Returns the Systemwide and project specific activities
240 240 def activities(include_inactive=false)
241 241 t = TimeEntryActivity.table_name
242 242 scope = TimeEntryActivity.where("#{t}.project_id IS NULL OR #{t}.project_id = ?", id)
243 243
244 244 overridden_activity_ids = self.time_entry_activities.pluck(:parent_id).compact
245 245 if overridden_activity_ids.any?
246 246 scope = scope.where("#{t}.id NOT IN (?)", overridden_activity_ids)
247 247 end
248 248 unless include_inactive
249 249 scope = scope.active
250 250 end
251 251 scope
252 252 end
253 253
254 254 # Will create a new Project specific Activity or update an existing one
255 255 #
256 256 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
257 257 # does not successfully save.
258 258 def update_or_create_time_entry_activity(id, activity_hash)
259 259 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
260 260 self.create_time_entry_activity_if_needed(activity_hash)
261 261 else
262 262 activity = project.time_entry_activities.find_by_id(id.to_i)
263 263 activity.update_attributes(activity_hash) if activity
264 264 end
265 265 end
266 266
267 267 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
268 268 #
269 269 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
270 270 # does not successfully save.
271 271 def create_time_entry_activity_if_needed(activity)
272 272 if activity['parent_id']
273 273 parent_activity = TimeEntryActivity.find(activity['parent_id'])
274 274 activity['name'] = parent_activity.name
275 275 activity['position'] = parent_activity.position
276 276 if Enumeration.overriding_change?(activity, parent_activity)
277 277 project_activity = self.time_entry_activities.create(activity)
278 278 if project_activity.new_record?
279 279 raise ActiveRecord::Rollback, "Overriding TimeEntryActivity was not successfully saved"
280 280 else
281 281 self.time_entries.
282 282 where(["activity_id = ?", parent_activity.id]).
283 283 update_all("activity_id = #{project_activity.id}")
284 284 end
285 285 end
286 286 end
287 287 end
288 288
289 289 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
290 290 #
291 291 # Examples:
292 292 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
293 293 # project.project_condition(false) => "projects.id = 1"
294 294 def project_condition(with_subprojects)
295 295 cond = "#{Project.table_name}.id = #{id}"
296 296 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
297 297 cond
298 298 end
299 299
300 300 def self.find(*args)
301 301 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
302 302 project = find_by_identifier(*args)
303 303 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
304 304 project
305 305 else
306 306 super
307 307 end
308 308 end
309 309
310 310 def self.find_by_param(*args)
311 311 self.find(*args)
312 312 end
313 313
314 314 alias :base_reload :reload
315 315 def reload(*args)
316 316 @principals = nil
317 317 @users = nil
318 318 @shared_versions = nil
319 319 @rolled_up_versions = nil
320 320 @rolled_up_trackers = nil
321 321 @all_issue_custom_fields = nil
322 322 @all_time_entry_custom_fields = nil
323 323 @to_param = nil
324 324 @allowed_parents = nil
325 325 @allowed_permissions = nil
326 326 @actions_allowed = nil
327 327 @start_date = nil
328 328 @due_date = nil
329 329 @override_members = nil
330 330 @assignable_users = nil
331 331 base_reload(*args)
332 332 end
333 333
334 334 def to_param
335 335 if new_record?
336 336 nil
337 337 else
338 338 # id is used for projects with a numeric identifier (compatibility)
339 339 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id.to_s : identifier)
340 340 end
341 341 end
342 342
343 343 def active?
344 344 self.status == STATUS_ACTIVE
345 345 end
346 346
347 347 def archived?
348 348 self.status == STATUS_ARCHIVED
349 349 end
350 350
351 351 # Archives the project and its descendants
352 352 def archive
353 353 # Check that there is no issue of a non descendant project that is assigned
354 354 # to one of the project or descendant versions
355 355 version_ids = self_and_descendants.joins(:versions).pluck("#{Version.table_name}.id")
356 356
357 357 if version_ids.any? &&
358 358 Issue.
359 359 includes(:project).
360 360 where("#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?", lft, rgt).
361 361 where(:fixed_version_id => version_ids).
362 362 exists?
363 363 return false
364 364 end
365 365 Project.transaction do
366 366 archive!
367 367 end
368 368 true
369 369 end
370 370
371 371 # Unarchives the project
372 372 # All its ancestors must be active
373 373 def unarchive
374 374 return false if ancestors.detect {|a| !a.active?}
375 375 update_attribute :status, STATUS_ACTIVE
376 376 end
377 377
378 378 def close
379 379 self_and_descendants.status(STATUS_ACTIVE).update_all :status => STATUS_CLOSED
380 380 end
381 381
382 382 def reopen
383 383 self_and_descendants.status(STATUS_CLOSED).update_all :status => STATUS_ACTIVE
384 384 end
385 385
386 386 # Returns an array of projects the project can be moved to
387 387 # by the current user
388 388 def allowed_parents(user=User.current)
389 389 return @allowed_parents if @allowed_parents
390 390 @allowed_parents = Project.allowed_to(user, :add_subprojects).to_a
391 391 @allowed_parents = @allowed_parents - self_and_descendants
392 392 if user.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?)
393 393 @allowed_parents << nil
394 394 end
395 395 unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent)
396 396 @allowed_parents << parent
397 397 end
398 398 @allowed_parents
399 399 end
400 400
401 401 # Sets the parent of the project with authorization check
402 402 def set_allowed_parent!(p)
403 403 ActiveSupport::Deprecation.warn "Project#set_allowed_parent! is deprecated and will be removed in Redmine 4, use #safe_attributes= instead."
404 404 p = p.id if p.is_a?(Project)
405 405 send :safe_attributes, {:project_id => p}
406 406 save
407 407 end
408 408
409 409 # Sets the parent of the project and saves the project
410 410 # Argument can be either a Project, a String, a Fixnum or nil
411 411 def set_parent!(p)
412 412 if p.is_a?(Project)
413 413 self.parent = p
414 414 else
415 415 self.parent_id = p
416 416 end
417 417 save
418 418 end
419 419
420 420 # Returns an array of the trackers used by the project and its active sub projects
421 421 def rolled_up_trackers
422 422 @rolled_up_trackers ||=
423 423 Tracker.
424 424 joins(:projects).
425 425 joins("JOIN #{EnabledModule.table_name} ON #{EnabledModule.table_name}.project_id = #{Project.table_name}.id AND #{EnabledModule.table_name}.name = 'issue_tracking'").
426 426 where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED).
427 427 uniq.
428 428 sorted.
429 429 to_a
430 430 end
431 431
432 432 # Closes open and locked project versions that are completed
433 433 def close_completed_versions
434 434 Version.transaction do
435 435 versions.where(:status => %w(open locked)).each do |version|
436 436 if version.completed?
437 437 version.update_attribute(:status, 'closed')
438 438 end
439 439 end
440 440 end
441 441 end
442 442
443 443 # Returns a scope of the Versions on subprojects
444 444 def rolled_up_versions
445 445 @rolled_up_versions ||=
446 446 Version.
447 447 joins(:project).
448 448 where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED)
449 449 end
450 450
451 451 # Returns a scope of the Versions used by the project
452 452 def shared_versions
453 453 if new_record?
454 454 Version.
455 455 joins(:project).
456 456 preload(:project).
457 457 where("#{Project.table_name}.status <> ? AND #{Version.table_name}.sharing = 'system'", STATUS_ARCHIVED)
458 458 else
459 459 @shared_versions ||= begin
460 460 r = root? ? self : root
461 461 Version.
462 462 joins(:project).
463 463 preload(:project).
464 464 where("#{Project.table_name}.id = #{id}" +
465 465 " OR (#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND (" +
466 466 " #{Version.table_name}.sharing = 'system'" +
467 467 " OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" +
468 468 " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
469 469 " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
470 470 "))")
471 471 end
472 472 end
473 473 end
474 474
475 475 # Returns a hash of project users grouped by role
476 476 def users_by_role
477 477 members.includes(:user, :roles).inject({}) do |h, m|
478 478 m.roles.each do |r|
479 479 h[r] ||= []
480 480 h[r] << m.user
481 481 end
482 482 h
483 483 end
484 484 end
485 485
486 486 # Adds user as a project member with the default role
487 487 # Used for when a non-admin user creates a project
488 488 def add_default_member(user)
489 489 role = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
490 490 member = Member.new(:project => self, :principal => user, :roles => [role])
491 491 self.members << member
492 492 member
493 493 end
494 494
495 495 # Deletes all project's members
496 496 def delete_all_members
497 497 me, mr = Member.table_name, MemberRole.table_name
498 498 self.class.connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
499 499 Member.delete_all(['project_id = ?', id])
500 500 end
501 501
502 502 # Return a Principal scope of users/groups issues can be assigned to
503 503 def assignable_users
504 504 types = ['User']
505 505 types << 'Group' if Setting.issue_group_assignment?
506 506
507 507 @assignable_users ||= Principal.
508 508 active.
509 509 joins(:members => :roles).
510 510 where(:type => types, :members => {:project_id => id}, :roles => {:assignable => true}).
511 511 uniq.
512 512 sorted
513 513 end
514 514
515 515 # Returns the mail addresses of users that should be always notified on project events
516 516 def recipients
517 517 notified_users.collect {|user| user.mail}
518 518 end
519 519
520 520 # Returns the users that should be notified on project events
521 521 def notified_users
522 522 # TODO: User part should be extracted to User#notify_about?
523 523 members.select {|m| m.principal.present? && (m.mail_notification? || m.principal.mail_notification == 'all')}.collect {|m| m.principal}
524 524 end
525 525
526 526 # Returns a scope of all custom fields enabled for project issues
527 527 # (explicitly associated custom fields and custom fields enabled for all projects)
528 528 def all_issue_custom_fields
529 529 if new_record?
530 530 @all_issue_custom_fields ||= IssueCustomField.
531 531 sorted.
532 532 where("is_for_all = ? OR id IN (?)", true, issue_custom_field_ids)
533 533 else
534 534 @all_issue_custom_fields ||= IssueCustomField.
535 535 sorted.
536 536 where("is_for_all = ? OR id IN (SELECT DISTINCT cfp.custom_field_id" +
537 537 " FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} cfp" +
538 538 " WHERE cfp.project_id = ?)", true, id)
539 539 end
540 540 end
541 541
542 542 def project
543 543 self
544 544 end
545 545
546 546 def <=>(project)
547 547 name.casecmp(project.name)
548 548 end
549 549
550 550 def to_s
551 551 name
552 552 end
553 553
554 554 # Returns a short description of the projects (first lines)
555 555 def short_description(length = 255)
556 556 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
557 557 end
558 558
559 559 def css_classes
560 560 s = 'project'
561 561 s << ' root' if root?
562 562 s << ' child' if child?
563 563 s << (leaf? ? ' leaf' : ' parent')
564 564 unless active?
565 565 if archived?
566 566 s << ' archived'
567 567 else
568 568 s << ' closed'
569 569 end
570 570 end
571 571 s
572 572 end
573 573
574 574 # The earliest start date of a project, based on it's issues and versions
575 575 def start_date
576 576 @start_date ||= [
577 577 issues.minimum('start_date'),
578 578 shared_versions.minimum('effective_date'),
579 579 Issue.fixed_version(shared_versions).minimum('start_date')
580 580 ].compact.min
581 581 end
582 582
583 583 # The latest due date of an issue or version
584 584 def due_date
585 585 @due_date ||= [
586 586 issues.maximum('due_date'),
587 587 shared_versions.maximum('effective_date'),
588 588 Issue.fixed_version(shared_versions).maximum('due_date')
589 589 ].compact.max
590 590 end
591 591
592 592 def overdue?
593 593 active? && !due_date.nil? && (due_date < Date.today)
594 594 end
595 595
596 596 # Returns the percent completed for this project, based on the
597 597 # progress on it's versions.
598 598 def completed_percent(options={:include_subprojects => false})
599 599 if options.delete(:include_subprojects)
600 600 total = self_and_descendants.collect(&:completed_percent).sum
601 601
602 602 total / self_and_descendants.count
603 603 else
604 604 if versions.count > 0
605 605 total = versions.collect(&:completed_percent).sum
606 606
607 607 total / versions.count
608 608 else
609 609 100
610 610 end
611 611 end
612 612 end
613 613
614 614 # Return true if this project allows to do the specified action.
615 615 # action can be:
616 616 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
617 617 # * a permission Symbol (eg. :edit_project)
618 618 def allows_to?(action)
619 619 if archived?
620 620 # No action allowed on archived projects
621 621 return false
622 622 end
623 623 unless active? || Redmine::AccessControl.read_action?(action)
624 624 # No write action allowed on closed projects
625 625 return false
626 626 end
627 627 # No action allowed on disabled modules
628 628 if action.is_a? Hash
629 629 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
630 630 else
631 631 allowed_permissions.include? action
632 632 end
633 633 end
634 634
635 635 # Return the enabled module with the given name
636 636 # or nil if the module is not enabled for the project
637 637 def enabled_module(name)
638 638 name = name.to_s
639 639 enabled_modules.detect {|m| m.name == name}
640 640 end
641 641
642 642 # Return true if the module with the given name is enabled
643 643 def module_enabled?(name)
644 644 enabled_module(name).present?
645 645 end
646 646
647 647 def enabled_module_names=(module_names)
648 648 if module_names && module_names.is_a?(Array)
649 649 module_names = module_names.collect(&:to_s).reject(&:blank?)
650 650 self.enabled_modules = module_names.collect {|name| enabled_modules.detect {|mod| mod.name == name} || EnabledModule.new(:name => name)}
651 651 else
652 652 enabled_modules.clear
653 653 end
654 654 end
655 655
656 656 # Returns an array of the enabled modules names
657 657 def enabled_module_names
658 658 enabled_modules.collect(&:name)
659 659 end
660 660
661 661 # Enable a specific module
662 662 #
663 663 # Examples:
664 664 # project.enable_module!(:issue_tracking)
665 665 # project.enable_module!("issue_tracking")
666 666 def enable_module!(name)
667 667 enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name)
668 668 end
669 669
670 670 # Disable a module if it exists
671 671 #
672 672 # Examples:
673 673 # project.disable_module!(:issue_tracking)
674 674 # project.disable_module!("issue_tracking")
675 675 # project.disable_module!(project.enabled_modules.first)
676 676 def disable_module!(target)
677 677 target = enabled_modules.detect{|mod| target.to_s == mod.name} unless enabled_modules.include?(target)
678 678 target.destroy unless target.blank?
679 679 end
680 680
681 681 safe_attributes 'name',
682 682 'description',
683 683 'homepage',
684 684 'is_public',
685 685 'identifier',
686 686 'custom_field_values',
687 687 'custom_fields',
688 688 'tracker_ids',
689 689 'issue_custom_field_ids',
690 690 'parent_id'
691 691
692 692 safe_attributes 'enabled_module_names',
693 693 :if => lambda {|project, user| project.new_record? || user.allowed_to?(:select_project_modules, project) }
694 694
695 695 safe_attributes 'inherit_members',
696 696 :if => lambda {|project, user| project.parent.nil? || project.parent.visible?(user)}
697 697
698 698 def safe_attributes=(attrs, user=User.current)
699 699 return unless attrs.is_a?(Hash)
700 700 attrs = attrs.deep_dup
701 701
702 702 @unallowed_parent_id = nil
703 parent_id_param = attrs['parent_id'].to_s
704 if parent_id_param.blank? || parent_id_param != parent_id.to_s
705 p = parent_id_param.present? ? Project.find_by_id(parent_id_param) : nil
706 unless allowed_parents(user).include?(p)
707 attrs.delete('parent_id')
708 @unallowed_parent_id = true
703 if new_record? || attrs.key?('parent_id')
704 parent_id_param = attrs['parent_id'].to_s
705 if new_record? || parent_id_param != parent_id.to_s
706 p = parent_id_param.present? ? Project.find_by_id(parent_id_param) : nil
707 unless allowed_parents(user).include?(p)
708 attrs.delete('parent_id')
709 @unallowed_parent_id = true
710 end
709 711 end
710 712 end
711 713
712 714 super(attrs, user)
713 715 end
714 716
715 717 # Returns an auto-generated project identifier based on the last identifier used
716 718 def self.next_identifier
717 719 p = Project.order('id DESC').first
718 720 p.nil? ? nil : p.identifier.to_s.succ
719 721 end
720 722
721 723 # Copies and saves the Project instance based on the +project+.
722 724 # Duplicates the source project's:
723 725 # * Wiki
724 726 # * Versions
725 727 # * Categories
726 728 # * Issues
727 729 # * Members
728 730 # * Queries
729 731 #
730 732 # Accepts an +options+ argument to specify what to copy
731 733 #
732 734 # Examples:
733 735 # project.copy(1) # => copies everything
734 736 # project.copy(1, :only => 'members') # => copies members only
735 737 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
736 738 def copy(project, options={})
737 739 project = project.is_a?(Project) ? project : Project.find(project)
738 740
739 741 to_be_copied = %w(wiki versions issue_categories issues members queries boards)
740 742 to_be_copied = to_be_copied & Array.wrap(options[:only]) unless options[:only].nil?
741 743
742 744 Project.transaction do
743 745 if save
744 746 reload
745 747 to_be_copied.each do |name|
746 748 send "copy_#{name}", project
747 749 end
748 750 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
749 751 save
750 752 else
751 753 false
752 754 end
753 755 end
754 756 end
755 757
756 758 def member_principals
757 759 ActiveSupport::Deprecation.warn "Project#member_principals is deprecated and will be removed in Redmine 4.0. Use #memberships.active instead."
758 760 memberships.active
759 761 end
760 762
761 763 # Returns a new unsaved Project instance with attributes copied from +project+
762 764 def self.copy_from(project)
763 765 project = project.is_a?(Project) ? project : Project.find(project)
764 766 # clear unique attributes
765 767 attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt')
766 768 copy = Project.new(attributes)
767 769 copy.enabled_module_names = project.enabled_module_names
768 770 copy.trackers = project.trackers
769 771 copy.custom_values = project.custom_values.collect {|v| v.clone}
770 772 copy.issue_custom_fields = project.issue_custom_fields
771 773 copy
772 774 end
773 775
774 776 # Yields the given block for each project with its level in the tree
775 777 def self.project_tree(projects, &block)
776 778 ancestors = []
777 779 projects.sort_by(&:lft).each do |project|
778 780 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
779 781 ancestors.pop
780 782 end
781 783 yield project, ancestors.size
782 784 ancestors << project
783 785 end
784 786 end
785 787
786 788 private
787 789
788 790 def update_inherited_members
789 791 if parent
790 792 if inherit_members? && !inherit_members_was
791 793 remove_inherited_member_roles
792 794 add_inherited_member_roles
793 795 elsif !inherit_members? && inherit_members_was
794 796 remove_inherited_member_roles
795 797 end
796 798 end
797 799 end
798 800
799 801 def remove_inherited_member_roles
800 802 member_roles = memberships.map(&:member_roles).flatten
801 803 member_role_ids = member_roles.map(&:id)
802 804 member_roles.each do |member_role|
803 805 if member_role.inherited_from && !member_role_ids.include?(member_role.inherited_from)
804 806 member_role.destroy
805 807 end
806 808 end
807 809 end
808 810
809 811 def add_inherited_member_roles
810 812 if inherit_members? && parent
811 813 parent.memberships.each do |parent_member|
812 814 member = Member.find_or_new(self.id, parent_member.user_id)
813 815 parent_member.member_roles.each do |parent_member_role|
814 816 member.member_roles << MemberRole.new(:role => parent_member_role.role, :inherited_from => parent_member_role.id)
815 817 end
816 818 member.save!
817 819 end
818 820 memberships.reset
819 821 end
820 822 end
821 823
822 824 def update_versions_from_hierarchy_change
823 825 Issue.update_versions_from_hierarchy_change(self)
824 826 end
825 827
826 828 def validate_parent
827 829 if @unallowed_parent_id
828 830 errors.add(:parent_id, :invalid)
829 831 elsif parent_id_changed?
830 832 unless parent.nil? || (parent.active? && move_possible?(parent))
831 833 errors.add(:parent_id, :invalid)
832 834 end
833 835 end
834 836 end
835 837
836 838 # Copies wiki from +project+
837 839 def copy_wiki(project)
838 840 # Check that the source project has a wiki first
839 841 unless project.wiki.nil?
840 842 wiki = self.wiki || Wiki.new
841 843 wiki.attributes = project.wiki.attributes.dup.except("id", "project_id")
842 844 wiki_pages_map = {}
843 845 project.wiki.pages.each do |page|
844 846 # Skip pages without content
845 847 next if page.content.nil?
846 848 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on"))
847 849 new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id"))
848 850 new_wiki_page.content = new_wiki_content
849 851 wiki.pages << new_wiki_page
850 852 wiki_pages_map[page.id] = new_wiki_page
851 853 end
852 854
853 855 self.wiki = wiki
854 856 wiki.save
855 857 # Reproduce page hierarchy
856 858 project.wiki.pages.each do |page|
857 859 if page.parent_id && wiki_pages_map[page.id]
858 860 wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id]
859 861 wiki_pages_map[page.id].save
860 862 end
861 863 end
862 864 end
863 865 end
864 866
865 867 # Copies versions from +project+
866 868 def copy_versions(project)
867 869 project.versions.each do |version|
868 870 new_version = Version.new
869 871 new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on")
870 872 self.versions << new_version
871 873 end
872 874 end
873 875
874 876 # Copies issue categories from +project+
875 877 def copy_issue_categories(project)
876 878 project.issue_categories.each do |issue_category|
877 879 new_issue_category = IssueCategory.new
878 880 new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id")
879 881 self.issue_categories << new_issue_category
880 882 end
881 883 end
882 884
883 885 # Copies issues from +project+
884 886 def copy_issues(project)
885 887 # Stores the source issue id as a key and the copied issues as the
886 888 # value. Used to map the two together for issue relations.
887 889 issues_map = {}
888 890
889 891 # Store status and reopen locked/closed versions
890 892 version_statuses = versions.reject(&:open?).map {|version| [version, version.status]}
891 893 version_statuses.each do |version, status|
892 894 version.update_attribute :status, 'open'
893 895 end
894 896
895 897 # Get issues sorted by root_id, lft so that parent issues
896 898 # get copied before their children
897 899 project.issues.reorder('root_id, lft').each do |issue|
898 900 new_issue = Issue.new
899 901 new_issue.copy_from(issue, :subtasks => false, :link => false)
900 902 new_issue.project = self
901 903 # Changing project resets the custom field values
902 904 # TODO: handle this in Issue#project=
903 905 new_issue.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
904 906 # Reassign fixed_versions by name, since names are unique per project
905 907 if issue.fixed_version && issue.fixed_version.project == project
906 908 new_issue.fixed_version = self.versions.detect {|v| v.name == issue.fixed_version.name}
907 909 end
908 910 # Reassign version custom field values
909 911 new_issue.custom_field_values.each do |custom_value|
910 912 if custom_value.custom_field.field_format == 'version' && custom_value.value.present?
911 913 versions = Version.where(:id => custom_value.value).to_a
912 914 new_value = versions.map do |version|
913 915 if version.project == project
914 916 self.versions.detect {|v| v.name == version.name}.try(:id)
915 917 else
916 918 version.id
917 919 end
918 920 end
919 921 new_value.compact!
920 922 new_value = new_value.first unless custom_value.custom_field.multiple?
921 923 custom_value.value = new_value
922 924 end
923 925 end
924 926 # Reassign the category by name, since names are unique per project
925 927 if issue.category
926 928 new_issue.category = self.issue_categories.detect {|c| c.name == issue.category.name}
927 929 end
928 930 # Parent issue
929 931 if issue.parent_id
930 932 if copied_parent = issues_map[issue.parent_id]
931 933 new_issue.parent_issue_id = copied_parent.id
932 934 end
933 935 end
934 936
935 937 self.issues << new_issue
936 938 if new_issue.new_record?
937 939 logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info?
938 940 else
939 941 issues_map[issue.id] = new_issue unless new_issue.new_record?
940 942 end
941 943 end
942 944
943 945 # Restore locked/closed version statuses
944 946 version_statuses.each do |version, status|
945 947 version.update_attribute :status, status
946 948 end
947 949
948 950 # Relations after in case issues related each other
949 951 project.issues.each do |issue|
950 952 new_issue = issues_map[issue.id]
951 953 unless new_issue
952 954 # Issue was not copied
953 955 next
954 956 end
955 957
956 958 # Relations
957 959 issue.relations_from.each do |source_relation|
958 960 new_issue_relation = IssueRelation.new
959 961 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
960 962 new_issue_relation.issue_to = issues_map[source_relation.issue_to_id]
961 963 if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations?
962 964 new_issue_relation.issue_to = source_relation.issue_to
963 965 end
964 966 new_issue.relations_from << new_issue_relation
965 967 end
966 968
967 969 issue.relations_to.each do |source_relation|
968 970 new_issue_relation = IssueRelation.new
969 971 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
970 972 new_issue_relation.issue_from = issues_map[source_relation.issue_from_id]
971 973 if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations?
972 974 new_issue_relation.issue_from = source_relation.issue_from
973 975 end
974 976 new_issue.relations_to << new_issue_relation
975 977 end
976 978 end
977 979 end
978 980
979 981 # Copies members from +project+
980 982 def copy_members(project)
981 983 # Copy users first, then groups to handle members with inherited and given roles
982 984 members_to_copy = []
983 985 members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)}
984 986 members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)}
985 987
986 988 members_to_copy.each do |member|
987 989 new_member = Member.new
988 990 new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on")
989 991 # only copy non inherited roles
990 992 # inherited roles will be added when copying the group membership
991 993 role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id)
992 994 next if role_ids.empty?
993 995 new_member.role_ids = role_ids
994 996 new_member.project = self
995 997 self.members << new_member
996 998 end
997 999 end
998 1000
999 1001 # Copies queries from +project+
1000 1002 def copy_queries(project)
1001 1003 project.queries.each do |query|
1002 1004 new_query = IssueQuery.new
1003 1005 new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria", "user_id", "type")
1004 1006 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
1005 1007 new_query.project = self
1006 1008 new_query.user_id = query.user_id
1007 1009 new_query.role_ids = query.role_ids if query.visibility == IssueQuery::VISIBILITY_ROLES
1008 1010 self.queries << new_query
1009 1011 end
1010 1012 end
1011 1013
1012 1014 # Copies boards from +project+
1013 1015 def copy_boards(project)
1014 1016 project.boards.each do |board|
1015 1017 new_board = Board.new
1016 1018 new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
1017 1019 new_board.project = self
1018 1020 self.boards << new_board
1019 1021 end
1020 1022 end
1021 1023
1022 1024 def allowed_permissions
1023 1025 @allowed_permissions ||= begin
1024 1026 module_names = enabled_modules.loaded? ? enabled_modules.map(&:name) : enabled_modules.pluck(:name)
1025 1027 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
1026 1028 end
1027 1029 end
1028 1030
1029 1031 def allowed_actions
1030 1032 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
1031 1033 end
1032 1034
1033 1035 # Archives subprojects recursively
1034 1036 def archive!
1035 1037 children.each do |subproject|
1036 1038 subproject.send :archive!
1037 1039 end
1038 1040 update_attribute :status, STATUS_ARCHIVED
1039 1041 end
1040 1042 end
@@ -1,707 +1,718
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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 ProjectsControllerTest < ActionController::TestCase
21 21 fixtures :projects, :versions, :users, :email_addresses, :roles, :members,
22 22 :member_roles, :issues, :journals, :journal_details,
23 23 :trackers, :projects_trackers, :issue_statuses,
24 24 :enabled_modules, :enumerations, :boards, :messages,
25 25 :attachments, :custom_fields, :custom_values, :time_entries,
26 26 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
27 27
28 28 def setup
29 29 @request.session[:user_id] = nil
30 30 Setting.default_language = 'en'
31 31 end
32 32
33 33 def test_index_by_anonymous_should_not_show_private_projects
34 34 get :index
35 35 assert_response :success
36 36 assert_template 'index'
37 37 projects = assigns(:projects)
38 38 assert_not_nil projects
39 39 assert projects.all?(&:is_public?)
40 40
41 41 assert_select 'ul' do
42 42 assert_select 'li' do
43 43 assert_select 'a', :text => 'eCookbook'
44 44 assert_select 'ul' do
45 45 assert_select 'a', :text => 'Child of private child'
46 46 end
47 47 end
48 48 end
49 49 assert_select 'a', :text => /Private child of eCookbook/, :count => 0
50 50 end
51 51
52 52 def test_index_atom
53 53 get :index, :format => 'atom'
54 54 assert_response :success
55 55 assert_template 'common/feed'
56 56 assert_select 'feed>title', :text => 'Redmine: Latest projects'
57 57 assert_select 'feed>entry', :count => Project.visible(User.current).count
58 58 end
59 59
60 60 test "#index by non-admin user with view_time_entries permission should show overall spent time link" do
61 61 @request.session[:user_id] = 3
62 62 get :index
63 63 assert_template 'index'
64 64 assert_select 'a[href=?]', '/time_entries'
65 65 end
66 66
67 67 test "#index by non-admin user without view_time_entries permission should not show overall spent time link" do
68 68 Role.find(2).remove_permission! :view_time_entries
69 69 Role.non_member.remove_permission! :view_time_entries
70 70 Role.anonymous.remove_permission! :view_time_entries
71 71 @request.session[:user_id] = 3
72 72
73 73 get :index
74 74 assert_template 'index'
75 75 assert_select 'a[href=?]', '/time_entries', 0
76 76 end
77 77
78 78 test "#index by non-admin user with permission should show add project link" do
79 79 Role.find(1).add_permission! :add_project
80 80 @request.session[:user_id] = 2
81 81 get :index
82 82 assert_template 'index'
83 83 assert_select 'a[href=?]', '/projects/new'
84 84 end
85 85
86 86 test "#new by admin user should accept get" do
87 87 @request.session[:user_id] = 1
88 88
89 89 get :new
90 90 assert_response :success
91 91 assert_template 'new'
92 92 end
93 93
94 94 test "#new by non-admin user with add_project permission should accept get" do
95 95 Role.non_member.add_permission! :add_project
96 96 @request.session[:user_id] = 9
97 97
98 98 get :new
99 99 assert_response :success
100 100 assert_template 'new'
101 101 assert_select 'select[name=?]', 'project[parent_id]', 0
102 102 end
103 103
104 104 test "#new by non-admin user with add_subprojects permission should accept get" do
105 105 Role.find(1).remove_permission! :add_project
106 106 Role.find(1).add_permission! :add_subprojects
107 107 @request.session[:user_id] = 2
108 108
109 109 get :new, :parent_id => 'ecookbook'
110 110 assert_response :success
111 111 assert_template 'new'
112 112
113 113 assert_select 'select[name=?]', 'project[parent_id]' do
114 114 # parent project selected
115 115 assert_select 'option[value="1"][selected=selected]'
116 116 # no empty value
117 117 assert_select 'option[value=""]', 0
118 118 end
119 119 end
120 120
121 121 def test_new_should_not_display_invalid_search_link
122 122 @request.session[:user_id] = 1
123 123
124 124 get :new
125 125 assert_response :success
126 126 assert_select '#quick-search form[action=?]', '/search'
127 127 assert_select '#quick-search a[href=?]', '/search'
128 128 end
129 129
130 130 test "#create by admin user should create a new project" do
131 131 @request.session[:user_id] = 1
132 132
133 133 post :create,
134 134 :project => {
135 135 :name => "blog",
136 136 :description => "weblog",
137 137 :homepage => 'http://weblog',
138 138 :identifier => "blog",
139 139 :is_public => 1,
140 140 :custom_field_values => { '3' => 'Beta' },
141 141 :tracker_ids => ['1', '3'],
142 142 # an issue custom field that is not for all project
143 143 :issue_custom_field_ids => ['9'],
144 144 :enabled_module_names => ['issue_tracking', 'news', 'repository']
145 145 }
146 146 assert_redirected_to '/projects/blog/settings'
147 147
148 148 project = Project.find_by_name('blog')
149 149 assert_kind_of Project, project
150 150 assert project.active?
151 151 assert_equal 'weblog', project.description
152 152 assert_equal 'http://weblog', project.homepage
153 153 assert_equal true, project.is_public?
154 154 assert_nil project.parent
155 155 assert_equal 'Beta', project.custom_value_for(3).value
156 156 assert_equal [1, 3], project.trackers.map(&:id).sort
157 157 assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort
158 158 assert project.issue_custom_fields.include?(IssueCustomField.find(9))
159 159 end
160 160
161 161 test "#create by admin user should create a new subproject" do
162 162 @request.session[:user_id] = 1
163 163
164 164 assert_difference 'Project.count' do
165 165 post :create, :project => { :name => "blog",
166 166 :description => "weblog",
167 167 :identifier => "blog",
168 168 :is_public => 1,
169 169 :custom_field_values => { '3' => 'Beta' },
170 170 :parent_id => 1
171 171 }
172 172 assert_redirected_to '/projects/blog/settings'
173 173 end
174 174
175 175 project = Project.find_by_name('blog')
176 176 assert_kind_of Project, project
177 177 assert_equal Project.find(1), project.parent
178 178 end
179 179
180 180 test "#create by admin user should continue" do
181 181 @request.session[:user_id] = 1
182 182
183 183 assert_difference 'Project.count' do
184 184 post :create, :project => {:name => "blog", :identifier => "blog"}, :continue => 'Create and continue'
185 185 end
186 186 assert_redirected_to '/projects/new'
187 187 end
188 188
189 189 test "#create by non-admin user with add_project permission should create a new project" do
190 190 Role.non_member.add_permission! :add_project
191 191 @request.session[:user_id] = 9
192 192
193 193 post :create, :project => { :name => "blog",
194 194 :description => "weblog",
195 195 :identifier => "blog",
196 196 :is_public => 1,
197 197 :custom_field_values => { '3' => 'Beta' },
198 198 :tracker_ids => ['1', '3'],
199 199 :enabled_module_names => ['issue_tracking', 'news', 'repository']
200 200 }
201 201
202 202 assert_redirected_to '/projects/blog/settings'
203 203
204 204 project = Project.find_by_name('blog')
205 205 assert_kind_of Project, project
206 206 assert_equal 'weblog', project.description
207 207 assert_equal true, project.is_public?
208 208 assert_equal [1, 3], project.trackers.map(&:id).sort
209 209 assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort
210 210
211 211 # User should be added as a project member
212 212 assert User.find(9).member_of?(project)
213 213 assert_equal 1, project.members.size
214 214 end
215 215
216 216 test "#create by non-admin user with add_project permission should fail with parent_id" do
217 217 Role.non_member.add_permission! :add_project
218 218 @request.session[:user_id] = 9
219 219
220 220 assert_no_difference 'Project.count' do
221 221 post :create, :project => { :name => "blog",
222 222 :description => "weblog",
223 223 :identifier => "blog",
224 224 :is_public => 1,
225 225 :custom_field_values => { '3' => 'Beta' },
226 226 :parent_id => 1
227 227 }
228 228 end
229 229 assert_response :success
230 230 project = assigns(:project)
231 231 assert_kind_of Project, project
232 232 assert_not_equal [], project.errors[:parent_id]
233 233 end
234 234
235 235 test "#create by non-admin user with add_subprojects permission should create a project with a parent_id" do
236 236 Role.find(1).remove_permission! :add_project
237 237 Role.find(1).add_permission! :add_subprojects
238 238 @request.session[:user_id] = 2
239 239
240 240 post :create, :project => { :name => "blog",
241 241 :description => "weblog",
242 242 :identifier => "blog",
243 243 :is_public => 1,
244 244 :custom_field_values => { '3' => 'Beta' },
245 245 :parent_id => 1
246 246 }
247 247 assert_redirected_to '/projects/blog/settings'
248 248 project = Project.find_by_name('blog')
249 249 end
250 250
251 251 test "#create by non-admin user with add_subprojects permission should fail without parent_id" do
252 252 Role.find(1).remove_permission! :add_project
253 253 Role.find(1).add_permission! :add_subprojects
254 254 @request.session[:user_id] = 2
255 255
256 256 assert_no_difference 'Project.count' do
257 257 post :create, :project => { :name => "blog",
258 258 :description => "weblog",
259 259 :identifier => "blog",
260 260 :is_public => 1,
261 261 :custom_field_values => { '3' => 'Beta' }
262 262 }
263 263 end
264 264 assert_response :success
265 265 project = assigns(:project)
266 266 assert_kind_of Project, project
267 267 assert_not_equal [], project.errors[:parent_id]
268 268 end
269 269
270 270 test "#create by non-admin user with add_subprojects permission should fail with unauthorized parent_id" do
271 271 Role.find(1).remove_permission! :add_project
272 272 Role.find(1).add_permission! :add_subprojects
273 273 @request.session[:user_id] = 2
274 274
275 275 assert !User.find(2).member_of?(Project.find(6))
276 276 assert_no_difference 'Project.count' do
277 277 post :create, :project => { :name => "blog",
278 278 :description => "weblog",
279 279 :identifier => "blog",
280 280 :is_public => 1,
281 281 :custom_field_values => { '3' => 'Beta' },
282 282 :parent_id => 6
283 283 }
284 284 end
285 285 assert_response :success
286 286 project = assigns(:project)
287 287 assert_kind_of Project, project
288 288 assert_not_equal [], project.errors[:parent_id]
289 289 end
290 290
291 291 def test_create_subproject_with_inherit_members_should_inherit_members
292 292 Role.find_by_name('Manager').add_permission! :add_subprojects
293 293 parent = Project.find(1)
294 294 @request.session[:user_id] = 2
295 295
296 296 assert_difference 'Project.count' do
297 297 post :create, :project => {
298 298 :name => 'inherited', :identifier => 'inherited', :parent_id => parent.id, :inherit_members => '1'
299 299 }
300 300 assert_response 302
301 301 end
302 302
303 303 project = Project.order('id desc').first
304 304 assert_equal 'inherited', project.name
305 305 assert_equal parent, project.parent
306 306 assert project.memberships.count > 0
307 307 assert_equal parent.memberships.count, project.memberships.count
308 308 end
309 309
310 310 def test_create_should_preserve_modules_on_validation_failure
311 311 with_settings :default_projects_modules => ['issue_tracking', 'repository'] do
312 312 @request.session[:user_id] = 1
313 313 assert_no_difference 'Project.count' do
314 314 post :create, :project => {
315 315 :name => "blog",
316 316 :identifier => "",
317 317 :enabled_module_names => %w(issue_tracking news)
318 318 }
319 319 end
320 320 assert_response :success
321 321 project = assigns(:project)
322 322 assert_equal %w(issue_tracking news), project.enabled_module_names.sort
323 323 end
324 324 end
325 325
326 326 def test_show_by_id
327 327 get :show, :id => 1
328 328 assert_response :success
329 329 assert_template 'show'
330 330 assert_not_nil assigns(:project)
331 331 end
332 332
333 333 def test_show_by_identifier
334 334 get :show, :id => 'ecookbook'
335 335 assert_response :success
336 336 assert_template 'show'
337 337 assert_not_nil assigns(:project)
338 338 assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
339 339
340 340 assert_select 'li', :text => /Development status/
341 341 end
342 342
343 343 def test_show_should_not_display_empty_sidebar
344 344 p = Project.find(1)
345 345 p.enabled_module_names = []
346 346 p.save!
347 347
348 348 get :show, :id => 'ecookbook'
349 349 assert_response :success
350 350 assert_select '#main.nosidebar'
351 351 end
352 352
353 353 def test_show_should_not_display_hidden_custom_fields
354 354 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
355 355 get :show, :id => 'ecookbook'
356 356 assert_response :success
357 357 assert_template 'show'
358 358 assert_not_nil assigns(:project)
359 359
360 360 assert_select 'li', :text => /Development status/, :count => 0
361 361 end
362 362
363 363 def test_show_should_not_display_blank_custom_fields_with_multiple_values
364 364 f1 = ProjectCustomField.generate! :field_format => 'list', :possible_values => %w(Foo Bar), :multiple => true
365 365 f2 = ProjectCustomField.generate! :field_format => 'list', :possible_values => %w(Baz Qux), :multiple => true
366 366 project = Project.generate!(:custom_field_values => {f2.id.to_s => %w(Qux)})
367 367
368 368 get :show, :id => project.id
369 369 assert_response :success
370 370
371 371 assert_select 'li', :text => /#{f1.name}/, :count => 0
372 372 assert_select 'li', :text => /#{f2.name}/
373 373 end
374 374
375 375 def test_show_should_not_display_blank_text_custom_fields
376 376 f1 = ProjectCustomField.generate! :field_format => 'text'
377 377
378 378 get :show, :id => 1
379 379 assert_response :success
380 380
381 381 assert_select 'li', :text => /#{f1.name}/, :count => 0
382 382 end
383 383
384 384 def test_show_should_not_fail_when_custom_values_are_nil
385 385 project = Project.find_by_identifier('ecookbook')
386 386 project.custom_values.first.update_attribute(:value, nil)
387 387 get :show, :id => 'ecookbook'
388 388 assert_response :success
389 389 assert_template 'show'
390 390 assert_not_nil assigns(:project)
391 391 assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
392 392 end
393 393
394 394 def show_archived_project_should_be_denied
395 395 project = Project.find_by_identifier('ecookbook')
396 396 project.archive!
397 397
398 398 get :show, :id => 'ecookbook'
399 399 assert_response 403
400 400 assert_nil assigns(:project)
401 401 assert_select 'p', :text => /archived/
402 402 end
403 403
404 404 def test_show_should_not_show_private_subprojects_that_are_not_visible
405 405 get :show, :id => 'ecookbook'
406 406 assert_response :success
407 407 assert_template 'show'
408 408 assert_select 'a', :text => /Private child/, :count => 0
409 409 end
410 410
411 411 def test_show_should_show_private_subprojects_that_are_visible
412 412 @request.session[:user_id] = 2 # manager who is a member of the private subproject
413 413 get :show, :id => 'ecookbook'
414 414 assert_response :success
415 415 assert_template 'show'
416 416 assert_select 'a', :text => /Private child/
417 417 end
418 418
419 419 def test_settings
420 420 @request.session[:user_id] = 2 # manager
421 421 get :settings, :id => 1
422 422 assert_response :success
423 423 assert_template 'settings'
424 424 end
425 425
426 426 def test_settings_of_subproject
427 427 @request.session[:user_id] = 2
428 428 get :settings, :id => 'private-child'
429 429 assert_response :success
430 430 assert_template 'settings'
431 431
432 432 assert_select 'input[type=checkbox][name=?]', 'project[inherit_members]'
433 433 end
434 434
435 435 def test_settings_should_be_denied_for_member_on_closed_project
436 436 Project.find(1).close
437 437 @request.session[:user_id] = 2 # manager
438 438
439 439 get :settings, :id => 1
440 440 assert_response 403
441 441 end
442 442
443 443 def test_settings_should_be_denied_for_anonymous_on_closed_project
444 444 Project.find(1).close
445 445
446 446 get :settings, :id => 1
447 447 assert_response 302
448 448 end
449 449
450 450 def test_setting_with_wiki_module_and_no_wiki
451 451 Project.find(1).wiki.destroy
452 452 Role.find(1).add_permission! :manage_wiki
453 453 @request.session[:user_id] = 2
454 454
455 455 get :settings, :id => 1
456 456 assert_response :success
457 457 assert_template 'settings'
458 458
459 459 assert_select 'form[action=?]', '/projects/ecookbook/wiki' do
460 460 assert_select 'input[name=?]', 'wiki[start_page]'
461 461 end
462 462 end
463 463
464 464 def test_update
465 465 @request.session[:user_id] = 2 # manager
466 466 post :update, :id => 1, :project => {:name => 'Test changed name',
467 467 :issue_custom_field_ids => ['']}
468 468 assert_redirected_to '/projects/ecookbook/settings'
469 469 project = Project.find(1)
470 470 assert_equal 'Test changed name', project.name
471 471 end
472 472
473 473 def test_update_with_failure
474 474 @request.session[:user_id] = 2 # manager
475 475 post :update, :id => 1, :project => {:name => ''}
476 476 assert_response :success
477 477 assert_template 'settings'
478 478 assert_select_error /name cannot be blank/i
479 479 end
480 480
481 481 def test_update_should_be_denied_for_member_on_closed_project
482 482 Project.find(1).close
483 483 @request.session[:user_id] = 2 # manager
484 484
485 485 post :update, :id => 1, :project => {:name => 'Closed'}
486 486 assert_response 403
487 487 assert_equal 'eCookbook', Project.find(1).name
488 488 end
489 489
490 490 def test_update_should_be_denied_for_anonymous_on_closed_project
491 491 Project.find(1).close
492 492
493 493 post :update, :id => 1, :project => {:name => 'Closed'}
494 494 assert_response 302
495 495 assert_equal 'eCookbook', Project.find(1).name
496 496 end
497 497
498 def test_update_child_project_without_parent_permission_should_not_show_validation_error
499 child = Project.generate_with_parent!
500 user = User.generate!
501 User.add_to_project(user, child, Role.generate!(:permissions => [:edit_project]))
502 @request.session[:user_id] = user.id
503
504 post :update, :id => child.id, :project => {:name => 'Updated'}
505 assert_response 302
506 assert_match /Successful update/, flash[:notice]
507 end
508
498 509 def test_modules
499 510 @request.session[:user_id] = 2
500 511 Project.find(1).enabled_module_names = ['issue_tracking', 'news']
501 512
502 513 post :modules, :id => 1, :enabled_module_names => ['issue_tracking', 'repository', 'documents']
503 514 assert_redirected_to '/projects/ecookbook/settings/modules'
504 515 assert_equal ['documents', 'issue_tracking', 'repository'], Project.find(1).enabled_module_names.sort
505 516 end
506 517
507 518 def test_destroy_leaf_project_without_confirmation_should_show_confirmation
508 519 @request.session[:user_id] = 1 # admin
509 520
510 521 assert_no_difference 'Project.count' do
511 522 delete :destroy, :id => 2
512 523 assert_response :success
513 524 assert_template 'destroy'
514 525 end
515 526 end
516 527
517 528 def test_destroy_without_confirmation_should_show_confirmation_with_subprojects
518 529 @request.session[:user_id] = 1 # admin
519 530
520 531 assert_no_difference 'Project.count' do
521 532 delete :destroy, :id => 1
522 533 assert_response :success
523 534 assert_template 'destroy'
524 535 end
525 536 assert_select 'strong',
526 537 :text => ['Private child of eCookbook',
527 538 'Child of private child, eCookbook Subproject 1',
528 539 'eCookbook Subproject 2'].join(', ')
529 540 end
530 541
531 542 def test_destroy_with_confirmation_should_destroy_the_project_and_subprojects
532 543 @request.session[:user_id] = 1 # admin
533 544
534 545 assert_difference 'Project.count', -5 do
535 546 delete :destroy, :id => 1, :confirm => 1
536 547 assert_redirected_to '/admin/projects'
537 548 end
538 549 assert_nil Project.find_by_id(1)
539 550 end
540 551
541 552 def test_archive
542 553 @request.session[:user_id] = 1 # admin
543 554 post :archive, :id => 1
544 555 assert_redirected_to '/admin/projects'
545 556 assert !Project.find(1).active?
546 557 end
547 558
548 559 def test_archive_with_failure
549 560 @request.session[:user_id] = 1
550 561 Project.any_instance.stubs(:archive).returns(false)
551 562 post :archive, :id => 1
552 563 assert_redirected_to '/admin/projects'
553 564 assert_match /project cannot be archived/i, flash[:error]
554 565 end
555 566
556 567 def test_unarchive
557 568 @request.session[:user_id] = 1 # admin
558 569 Project.find(1).archive
559 570 post :unarchive, :id => 1
560 571 assert_redirected_to '/admin/projects'
561 572 assert Project.find(1).active?
562 573 end
563 574
564 575 def test_close
565 576 @request.session[:user_id] = 2
566 577 post :close, :id => 1
567 578 assert_redirected_to '/projects/ecookbook'
568 579 assert_equal Project::STATUS_CLOSED, Project.find(1).status
569 580 end
570 581
571 582 def test_reopen
572 583 Project.find(1).close
573 584 @request.session[:user_id] = 2
574 585 post :reopen, :id => 1
575 586 assert_redirected_to '/projects/ecookbook'
576 587 assert Project.find(1).active?
577 588 end
578 589
579 590 def test_project_breadcrumbs_should_be_limited_to_3_ancestors
580 591 CustomField.delete_all
581 592 parent = nil
582 593 6.times do |i|
583 594 p = Project.generate_with_parent!(parent)
584 595 get :show, :id => p
585 596 assert_select '#header h1' do
586 597 assert_select 'a', :count => [i, 3].min
587 598 end
588 599
589 600 parent = p
590 601 end
591 602 end
592 603
593 604 def test_get_copy
594 605 @request.session[:user_id] = 1 # admin
595 606 get :copy, :id => 1
596 607 assert_response :success
597 608 assert_template 'copy'
598 609 assert assigns(:project)
599 610 assert_equal Project.find(1).description, assigns(:project).description
600 611 assert_nil assigns(:project).id
601 612
602 613 assert_select 'input[name=?][value=?]', 'project[enabled_module_names][]', 'issue_tracking', 1
603 614 end
604 615
605 616 def test_get_copy_with_invalid_source_should_respond_with_404
606 617 @request.session[:user_id] = 1
607 618 get :copy, :id => 99
608 619 assert_response 404
609 620 end
610 621
611 622 def test_get_copy_should_preselect_custom_fields
612 623 field1 = IssueCustomField.generate!(:is_for_all => false)
613 624 field2 = IssueCustomField.generate!(:is_for_all => false)
614 625 source = Project.generate!(:issue_custom_fields => [field1])
615 626 @request.session[:user_id] = 1
616 627
617 628 get :copy, :id => source.id
618 629 assert_response :success
619 630 assert_select 'fieldset#project_issue_custom_fields' do
620 631 assert_select 'input[type=checkbox][value=?][checked=checked]', field1.id.to_s
621 632 assert_select 'input[type=checkbox][value=?]:not([checked])', field2.id.to_s
622 633 end
623 634 end
624 635
625 636 def test_post_copy_should_copy_requested_items
626 637 @request.session[:user_id] = 1 # admin
627 638 CustomField.delete_all
628 639
629 640 assert_difference 'Project.count' do
630 641 post :copy, :id => 1,
631 642 :project => {
632 643 :name => 'Copy',
633 644 :identifier => 'unique-copy',
634 645 :tracker_ids => ['1', '2', '3', ''],
635 646 :enabled_module_names => %w(issue_tracking time_tracking)
636 647 },
637 648 :only => %w(issues versions)
638 649 end
639 650 project = Project.find('unique-copy')
640 651 source = Project.find(1)
641 652 assert_equal %w(issue_tracking time_tracking), project.enabled_module_names.sort
642 653
643 654 assert_equal source.versions.count, project.versions.count, "All versions were not copied"
644 655 assert_equal source.issues.count, project.issues.count, "All issues were not copied"
645 656 assert_equal 0, project.members.count
646 657 end
647 658
648 659 def test_post_copy_should_redirect_to_settings_when_successful
649 660 @request.session[:user_id] = 1 # admin
650 661 post :copy, :id => 1, :project => {:name => 'Copy', :identifier => 'unique-copy'}
651 662 assert_response :redirect
652 663 assert_redirected_to :controller => 'projects', :action => 'settings', :id => 'unique-copy'
653 664 end
654 665
655 666 def test_post_copy_with_failure
656 667 @request.session[:user_id] = 1
657 668 post :copy, :id => 1, :project => {:name => 'Copy', :identifier => ''}
658 669 assert_response :success
659 670 assert_template 'copy'
660 671 end
661 672
662 673 def test_jump_should_redirect_to_active_tab
663 674 get :show, :id => 1, :jump => 'issues'
664 675 assert_redirected_to '/projects/ecookbook/issues'
665 676 end
666 677
667 678 def test_jump_should_not_redirect_to_inactive_tab
668 679 get :show, :id => 3, :jump => 'documents'
669 680 assert_response :success
670 681 assert_template 'show'
671 682 end
672 683
673 684 def test_jump_should_not_redirect_to_unknown_tab
674 685 get :show, :id => 3, :jump => 'foobar'
675 686 assert_response :success
676 687 assert_template 'show'
677 688 end
678 689
679 690 def test_body_should_have_project_css_class
680 691 get :show, :id => 1
681 692 assert_select 'body.project-ecookbook'
682 693 end
683 694
684 695 def test_project_menu_should_include_new_issue_link
685 696 @request.session[:user_id] = 2
686 697 get :show, :id => 1
687 698 assert_select '#main-menu a.new-issue[href="/projects/ecookbook/issues/new"]', :text => 'New issue'
688 699 end
689 700
690 701 def test_project_menu_should_not_include_new_issue_link_for_project_without_trackers
691 702 Project.find(1).trackers.clear
692 703
693 704 @request.session[:user_id] = 2
694 705 get :show, :id => 1
695 706 assert_select '#main-menu a.new-issue', 0
696 707 end
697 708
698 709 def test_project_menu_should_not_include_new_issue_link_for_users_with_copy_issues_permission_only
699 710 role = Role.find(1)
700 711 role.remove_permission! :add_issues
701 712 role.add_permission! :copy_issues
702 713
703 714 @request.session[:user_id] = 2
704 715 get :show, :id => 1
705 716 assert_select '#main-menu a.new-issue', 0
706 717 end
707 718 end
@@ -1,261 +1,264
1 1 module ObjectHelpers
2 2 def User.generate!(attributes={})
3 3 @generated_user_login ||= 'user0'
4 4 @generated_user_login.succ!
5 5 user = User.new(attributes)
6 6 user.login = @generated_user_login.dup if user.login.blank?
7 7 user.mail = "#{@generated_user_login}@example.com" if user.mail.blank?
8 8 user.firstname = "Bob" if user.firstname.blank?
9 9 user.lastname = "Doe" if user.lastname.blank?
10 10 yield user if block_given?
11 11 user.save!
12 12 user
13 13 end
14 14
15 15 def User.add_to_project(user, project, roles=nil)
16 16 roles = Role.find(1) if roles.nil?
17 17 roles = [roles] if roles.is_a?(Role)
18 18 Member.create!(:principal => user, :project => project, :roles => roles)
19 19 end
20 20
21 21 def Group.generate!(attributes={})
22 22 @generated_group_name ||= 'Group 0'
23 23 @generated_group_name.succ!
24 24 group = Group.new(attributes)
25 25 group.name = @generated_group_name.dup if group.name.blank?
26 26 yield group if block_given?
27 27 group.save!
28 28 group
29 29 end
30 30
31 31 def Project.generate!(attributes={})
32 32 @generated_project_identifier ||= 'project-0000'
33 33 @generated_project_identifier.succ!
34 34 project = Project.new(attributes)
35 35 project.name = @generated_project_identifier.dup if project.name.blank?
36 36 project.identifier = @generated_project_identifier.dup if project.identifier.blank?
37 37 yield project if block_given?
38 38 project.save!
39 39 project
40 40 end
41 41
42 def Project.generate_with_parent!(parent, attributes={})
42 def Project.generate_with_parent!(*args)
43 attributes = args.last.is_a?(Hash) ? args.pop : {}
44 parent = args.size > 0 ? args.first : Project.generate!
45
43 46 project = Project.generate!(attributes) do |p|
44 47 p.parent = parent
45 48 end
46 49 parent.reload if parent
47 50 project
48 51 end
49 52
50 53 def IssueStatus.generate!(attributes={})
51 54 @generated_status_name ||= 'Status 0'
52 55 @generated_status_name.succ!
53 56 status = IssueStatus.new(attributes)
54 57 status.name = @generated_status_name.dup if status.name.blank?
55 58 yield status if block_given?
56 59 status.save!
57 60 status
58 61 end
59 62
60 63 def Tracker.generate!(attributes={})
61 64 @generated_tracker_name ||= 'Tracker 0'
62 65 @generated_tracker_name.succ!
63 66 tracker = Tracker.new(attributes)
64 67 tracker.name = @generated_tracker_name.dup if tracker.name.blank?
65 68 tracker.default_status ||= IssueStatus.order('position').first || IssueStatus.generate!
66 69 yield tracker if block_given?
67 70 tracker.save!
68 71 tracker
69 72 end
70 73
71 74 def Role.generate!(attributes={})
72 75 @generated_role_name ||= 'Role 0'
73 76 @generated_role_name.succ!
74 77 role = Role.new(attributes)
75 78 role.name = @generated_role_name.dup if role.name.blank?
76 79 yield role if block_given?
77 80 role.save!
78 81 role
79 82 end
80 83
81 84 # Generates an unsaved Issue
82 85 def Issue.generate(attributes={})
83 86 issue = Issue.new(attributes)
84 87 issue.project ||= Project.find(1)
85 88 issue.tracker ||= issue.project.trackers.first
86 89 issue.subject = 'Generated' if issue.subject.blank?
87 90 issue.author ||= User.find(2)
88 91 yield issue if block_given?
89 92 issue
90 93 end
91 94
92 95 # Generates a saved Issue
93 96 def Issue.generate!(attributes={}, &block)
94 97 issue = Issue.generate(attributes, &block)
95 98 issue.save!
96 99 issue
97 100 end
98 101
99 102 # Generates an issue with 2 children and a grandchild
100 103 def Issue.generate_with_descendants!(attributes={})
101 104 issue = Issue.generate!(attributes)
102 105 child = Issue.generate!(:project => issue.project, :subject => 'Child1', :parent_issue_id => issue.id)
103 106 Issue.generate!(:project => issue.project, :subject => 'Child2', :parent_issue_id => issue.id)
104 107 Issue.generate!(:project => issue.project, :subject => 'Child11', :parent_issue_id => child.id)
105 108 issue.reload
106 109 end
107 110
108 111 def Issue.generate_with_child!(attributes={})
109 112 issue = Issue.generate!(attributes)
110 113 Issue.generate!(:parent_issue_id => issue.id)
111 114 issue.reload
112 115 end
113 116
114 117 def Journal.generate!(attributes={})
115 118 journal = Journal.new(attributes)
116 119 journal.user ||= User.first
117 120 journal.journalized ||= Issue.first
118 121 yield journal if block_given?
119 122 journal.save!
120 123 journal
121 124 end
122 125
123 126 def Version.generate!(attributes={})
124 127 @generated_version_name ||= 'Version 0'
125 128 @generated_version_name.succ!
126 129 version = Version.new(attributes)
127 130 version.name = @generated_version_name.dup if version.name.blank?
128 131 version.project ||= Project.find(1)
129 132 yield version if block_given?
130 133 version.save!
131 134 version
132 135 end
133 136
134 137 def TimeEntry.generate!(attributes={})
135 138 entry = TimeEntry.new(attributes)
136 139 entry.user ||= User.find(2)
137 140 entry.issue ||= Issue.find(1) unless entry.project
138 141 entry.project ||= entry.issue.project
139 142 entry.activity ||= TimeEntryActivity.first
140 143 entry.spent_on ||= Date.today
141 144 entry.hours ||= 1.0
142 145 entry.save!
143 146 entry
144 147 end
145 148
146 149 def AuthSource.generate!(attributes={})
147 150 @generated_auth_source_name ||= 'Auth 0'
148 151 @generated_auth_source_name.succ!
149 152 source = AuthSource.new(attributes)
150 153 source.name = @generated_auth_source_name.dup if source.name.blank?
151 154 yield source if block_given?
152 155 source.save!
153 156 source
154 157 end
155 158
156 159 def Board.generate!(attributes={})
157 160 @generated_board_name ||= 'Forum 0'
158 161 @generated_board_name.succ!
159 162 board = Board.new(attributes)
160 163 board.name = @generated_board_name.dup if board.name.blank?
161 164 board.description = @generated_board_name.dup if board.description.blank?
162 165 yield board if block_given?
163 166 board.save!
164 167 board
165 168 end
166 169
167 170 def Attachment.generate!(attributes={})
168 171 @generated_filename ||= 'testfile0'
169 172 @generated_filename.succ!
170 173 attributes = attributes.dup
171 174 attachment = Attachment.new(attributes)
172 175 attachment.container ||= Issue.find(1)
173 176 attachment.author ||= User.find(2)
174 177 attachment.filename = @generated_filename.dup if attachment.filename.blank?
175 178 attachment.save!
176 179 attachment
177 180 end
178 181
179 182 def CustomField.generate!(attributes={})
180 183 @generated_custom_field_name ||= 'Custom field 0'
181 184 @generated_custom_field_name.succ!
182 185 field = new(attributes)
183 186 field.name = @generated_custom_field_name.dup if field.name.blank?
184 187 field.field_format = 'string' if field.field_format.blank?
185 188 yield field if block_given?
186 189 field.save!
187 190 field
188 191 end
189 192
190 193 def Changeset.generate!(attributes={})
191 194 @generated_changeset_rev ||= '123456'
192 195 @generated_changeset_rev.succ!
193 196 changeset = new(attributes)
194 197 changeset.repository ||= Project.find(1).repository
195 198 changeset.revision ||= @generated_changeset_rev
196 199 changeset.committed_on ||= Time.now
197 200 yield changeset if block_given?
198 201 changeset.save!
199 202 changeset
200 203 end
201 204
202 205 def Query.generate!(attributes={})
203 206 query = new(attributes)
204 207 query.name = "Generated query" if query.name.blank?
205 208 query.user ||= User.find(1)
206 209 query.save!
207 210 query
208 211 end
209 212
210 213 def generate_import(fixture_name='import_issues.csv')
211 214 import = IssueImport.new
212 215 import.user_id = 2
213 216 import.file = uploaded_test_file(fixture_name, 'text/csv')
214 217 import.save!
215 218 import
216 219 end
217 220
218 221 def generate_import_with_mapping(fixture_name='import_issues.csv')
219 222 import = generate_import(fixture_name)
220 223
221 224 import.settings = {
222 225 'separator' => ";", 'wrapper' => '"', 'encoding' => "UTF-8",
223 226 'mapping' => {'project_id' => '1', 'tracker_id' => '2', 'subject' => '1'}
224 227 }
225 228 import.save!
226 229 import
227 230 end
228 231 end
229 232
230 233 module TrackerObjectHelpers
231 234 def generate_transitions!(*args)
232 235 options = args.last.is_a?(Hash) ? args.pop : {}
233 236 if args.size == 1
234 237 args << args.first
235 238 end
236 239 if options[:clear]
237 240 WorkflowTransition.where(:tracker_id => id).delete_all
238 241 end
239 242 args.each_cons(2) do |old_status_id, new_status_id|
240 243 WorkflowTransition.create!(
241 244 :tracker => self,
242 245 :role_id => (options[:role_id] || 1),
243 246 :old_status_id => old_status_id,
244 247 :new_status_id => new_status_id
245 248 )
246 249 end
247 250 end
248 251 end
249 252 Tracker.send :include, TrackerObjectHelpers
250 253
251 254 module IssueObjectHelpers
252 255 def close!
253 256 self.status = IssueStatus.where(:is_closed => true).first
254 257 save!
255 258 end
256 259
257 260 def generate_child!(attributes={})
258 261 Issue.generate!(attributes.merge(:parent_issue_id => self.id))
259 262 end
260 263 end
261 264 Issue.send :include, IssueObjectHelpers
General Comments 0
You need to be logged in to leave comments. Login now