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