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