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