@@ -1,411 +1,407 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006 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 | # Project statuses |
|
20 | 20 | STATUS_ACTIVE = 1 |
|
21 | 21 | STATUS_ARCHIVED = 9 |
|
22 | 22 | |
|
23 | 23 | has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}" |
|
24 | 24 | has_many :users, :through => :members |
|
25 | 25 | has_many :enabled_modules, :dependent => :delete_all |
|
26 | 26 | has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" |
|
27 | 27 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] |
|
28 | 28 | has_many :issue_changes, :through => :issues, :source => :journals |
|
29 | 29 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" |
|
30 | 30 | has_many :time_entries, :dependent => :delete_all |
|
31 | 31 | has_many :queries, :dependent => :delete_all |
|
32 | 32 | has_many :documents, :dependent => :destroy |
|
33 | 33 | has_many :news, :dependent => :delete_all, :include => :author |
|
34 | 34 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" |
|
35 | 35 | has_many :boards, :dependent => :destroy, :order => "position ASC" |
|
36 | 36 | has_one :repository, :dependent => :destroy |
|
37 | 37 | has_many :changesets, :through => :repository |
|
38 | 38 | has_one :wiki, :dependent => :destroy |
|
39 | 39 | # Custom field for the project issues |
|
40 | 40 | has_and_belongs_to_many :issue_custom_fields, |
|
41 | 41 | :class_name => 'IssueCustomField', |
|
42 | 42 | :order => "#{CustomField.table_name}.position", |
|
43 | 43 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", |
|
44 | 44 | :association_foreign_key => 'custom_field_id' |
|
45 | 45 | |
|
46 | 46 | acts_as_nested_set :order => 'name', :dependent => :destroy |
|
47 | 47 | acts_as_attachable :view_permission => :view_files, |
|
48 | 48 | :delete_permission => :manage_files |
|
49 | 49 | |
|
50 | 50 | acts_as_customizable |
|
51 | 51 | acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil |
|
52 | 52 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, |
|
53 | 53 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}, |
|
54 | 54 | :author => nil |
|
55 | 55 | |
|
56 | 56 | attr_protected :status, :enabled_module_names |
|
57 | 57 | |
|
58 | 58 | validates_presence_of :name, :identifier |
|
59 | 59 | validates_uniqueness_of :name, :identifier |
|
60 | 60 | validates_associated :repository, :wiki |
|
61 | 61 | validates_length_of :name, :maximum => 30 |
|
62 | 62 | validates_length_of :homepage, :maximum => 255 |
|
63 | 63 | validates_length_of :identifier, :in => 1..20 |
|
64 | validates_format_of :identifier, :with => /^[a-z0-9\-]*$/ | |
|
64 | # donwcase letters, digits, dashes but not digits only | |
|
65 | validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? } | |
|
65 | 66 | |
|
66 | 67 | before_destroy :delete_all_members |
|
67 | 68 | |
|
68 | 69 | 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] } } |
|
69 | 70 | named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"} |
|
70 | 71 | named_scope :public, { :conditions => { :is_public => true } } |
|
71 | 72 | named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } |
|
72 | 73 | |
|
73 | 74 | def identifier=(identifier) |
|
74 | 75 | super unless identifier_frozen? |
|
75 | 76 | end |
|
76 | 77 | |
|
77 | 78 | def identifier_frozen? |
|
78 | 79 | errors[:identifier].nil? && !(new_record? || identifier.blank?) |
|
79 | 80 | end |
|
80 | 81 | |
|
81 | 82 | def issues_with_subprojects(include_subprojects=false) |
|
82 | 83 | conditions = nil |
|
83 | 84 | if include_subprojects |
|
84 | 85 | ids = [id] + descendants.collect(&:id) |
|
85 | 86 | conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"] |
|
86 | 87 | end |
|
87 | 88 | conditions ||= ["#{Project.table_name}.id = ?", id] |
|
88 | 89 | # Quick and dirty fix for Rails 2 compatibility |
|
89 | 90 | Issue.send(:with_scope, :find => { :conditions => conditions }) do |
|
90 | 91 | Version.send(:with_scope, :find => { :conditions => conditions }) do |
|
91 | 92 | yield |
|
92 | 93 | end |
|
93 | 94 | end |
|
94 | 95 | end |
|
95 | 96 | |
|
96 | 97 | # returns latest created projects |
|
97 | 98 | # non public projects will be returned only if user is a member of those |
|
98 | 99 | def self.latest(user=nil, count=5) |
|
99 | 100 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") |
|
100 | 101 | end |
|
101 | 102 | |
|
102 | 103 | # Returns a SQL :conditions string used to find all active projects for the specified user. |
|
103 | 104 | # |
|
104 | 105 | # Examples: |
|
105 | 106 | # Projects.visible_by(admin) => "projects.status = 1" |
|
106 | 107 | # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1" |
|
107 | 108 | def self.visible_by(user=nil) |
|
108 | 109 | user ||= User.current |
|
109 | 110 | if user && user.admin? |
|
110 | 111 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
111 | 112 | elsif user && user.memberships.any? |
|
112 | 113 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))" |
|
113 | 114 | else |
|
114 | 115 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}" |
|
115 | 116 | end |
|
116 | 117 | end |
|
117 | 118 | |
|
118 | 119 | def self.allowed_to_condition(user, permission, options={}) |
|
119 | 120 | statements = [] |
|
120 | 121 | base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
121 | 122 | if perm = Redmine::AccessControl.permission(permission) |
|
122 | 123 | unless perm.project_module.nil? |
|
123 | 124 | # If the permission belongs to a project module, make sure the module is enabled |
|
124 | 125 | base_statement << " AND EXISTS (SELECT em.id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}' AND em.project_id=#{Project.table_name}.id)" |
|
125 | 126 | end |
|
126 | 127 | end |
|
127 | 128 | if options[:project] |
|
128 | 129 | project_statement = "#{Project.table_name}.id = #{options[:project].id}" |
|
129 | 130 | project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects] |
|
130 | 131 | base_statement = "(#{project_statement}) AND (#{base_statement})" |
|
131 | 132 | end |
|
132 | 133 | if user.admin? |
|
133 | 134 | # no restriction |
|
134 | 135 | else |
|
135 | 136 | statements << "1=0" |
|
136 | 137 | if user.logged? |
|
137 | 138 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission) |
|
138 | 139 | allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id} |
|
139 | 140 | statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any? |
|
140 | 141 | elsif Role.anonymous.allowed_to?(permission) |
|
141 | 142 | # anonymous user allowed on public project |
|
142 | 143 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" |
|
143 | 144 | else |
|
144 | 145 | # anonymous user is not authorized |
|
145 | 146 | end |
|
146 | 147 | end |
|
147 | 148 | statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))" |
|
148 | 149 | end |
|
149 | 150 | |
|
150 | 151 | # Returns a :conditions SQL string that can be used to find the issues associated with this project. |
|
151 | 152 | # |
|
152 | 153 | # Examples: |
|
153 | 154 | # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))" |
|
154 | 155 | # project.project_condition(false) => "projects.id = 1" |
|
155 | 156 | def project_condition(with_subprojects) |
|
156 | 157 | cond = "#{Project.table_name}.id = #{id}" |
|
157 | 158 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects |
|
158 | 159 | cond |
|
159 | 160 | end |
|
160 | 161 | |
|
161 | 162 | def self.find(*args) |
|
162 | 163 | if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) |
|
163 | 164 | project = find_by_identifier(*args) |
|
164 | 165 | raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? |
|
165 | 166 | project |
|
166 | 167 | else |
|
167 | 168 | super |
|
168 | 169 | end |
|
169 | 170 | end |
|
170 | 171 | |
|
171 | 172 | def to_param |
|
172 | 173 | # id is used for projects with a numeric identifier (compatibility) |
|
173 | 174 | @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier) |
|
174 | 175 | end |
|
175 | 176 | |
|
176 | 177 | def active? |
|
177 | 178 | self.status == STATUS_ACTIVE |
|
178 | 179 | end |
|
179 | 180 | |
|
180 | 181 | # Archives the project and its descendants recursively |
|
181 | 182 | def archive |
|
182 | 183 | # Archive subprojects if any |
|
183 | 184 | children.each do |subproject| |
|
184 | 185 | subproject.archive |
|
185 | 186 | end |
|
186 | 187 | update_attribute :status, STATUS_ARCHIVED |
|
187 | 188 | end |
|
188 | 189 | |
|
189 | 190 | # Unarchives the project |
|
190 | 191 | # All its ancestors must be active |
|
191 | 192 | def unarchive |
|
192 | 193 | return false if ancestors.detect {|a| !a.active?} |
|
193 | 194 | update_attribute :status, STATUS_ACTIVE |
|
194 | 195 | end |
|
195 | 196 | |
|
196 | 197 | # Returns an array of projects the project can be moved to |
|
197 | 198 | def possible_parents |
|
198 | 199 | @possible_parents ||= (Project.active.find(:all) - self_and_descendants) |
|
199 | 200 | end |
|
200 | 201 | |
|
201 | 202 | # Sets the parent of the project |
|
202 | 203 | # Argument can be either a Project, a String, a Fixnum or nil |
|
203 | 204 | def set_parent!(p) |
|
204 | 205 | unless p.nil? || p.is_a?(Project) |
|
205 | 206 | if p.to_s.blank? |
|
206 | 207 | p = nil |
|
207 | 208 | else |
|
208 | 209 | p = Project.find_by_id(p) |
|
209 | 210 | return false unless p |
|
210 | 211 | end |
|
211 | 212 | end |
|
212 | 213 | if p == parent && !p.nil? |
|
213 | 214 | # Nothing to do |
|
214 | 215 | true |
|
215 | 216 | elsif p.nil? || (p.active? && move_possible?(p)) |
|
216 | 217 | # Insert the project so that target's children or root projects stay alphabetically sorted |
|
217 | 218 | sibs = (p.nil? ? self.class.roots : p.children) |
|
218 | 219 | to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase } |
|
219 | 220 | if to_be_inserted_before |
|
220 | 221 | move_to_left_of(to_be_inserted_before) |
|
221 | 222 | elsif p.nil? |
|
222 | 223 | if sibs.empty? |
|
223 | 224 | # move_to_root adds the project in first (ie. left) position |
|
224 | 225 | move_to_root |
|
225 | 226 | else |
|
226 | 227 | move_to_right_of(sibs.last) unless self == sibs.last |
|
227 | 228 | end |
|
228 | 229 | else |
|
229 | 230 | # move_to_child_of adds the project in last (ie.right) position |
|
230 | 231 | move_to_child_of(p) |
|
231 | 232 | end |
|
232 | 233 | true |
|
233 | 234 | else |
|
234 | 235 | # Can not move to the given target |
|
235 | 236 | false |
|
236 | 237 | end |
|
237 | 238 | end |
|
238 | 239 | |
|
239 | 240 | # Returns an array of the trackers used by the project and its active sub projects |
|
240 | 241 | def rolled_up_trackers |
|
241 | 242 | @rolled_up_trackers ||= |
|
242 | 243 | Tracker.find(:all, :include => :projects, |
|
243 | 244 | :select => "DISTINCT #{Tracker.table_name}.*", |
|
244 | 245 | :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt], |
|
245 | 246 | :order => "#{Tracker.table_name}.position") |
|
246 | 247 | end |
|
247 | 248 | |
|
248 | 249 | # Returns a hash of project users grouped by role |
|
249 | 250 | def users_by_role |
|
250 | 251 | members.find(:all, :include => [:user, :roles]).inject({}) do |h, m| |
|
251 | 252 | m.roles.each do |r| |
|
252 | 253 | h[r] ||= [] |
|
253 | 254 | h[r] << m.user |
|
254 | 255 | end |
|
255 | 256 | h |
|
256 | 257 | end |
|
257 | 258 | end |
|
258 | 259 | |
|
259 | 260 | # Deletes all project's members |
|
260 | 261 | def delete_all_members |
|
261 | 262 | me, mr = Member.table_name, MemberRole.table_name |
|
262 | 263 | connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})") |
|
263 | 264 | Member.delete_all(['project_id = ?', id]) |
|
264 | 265 | end |
|
265 | 266 | |
|
266 | 267 | # Users issues can be assigned to |
|
267 | 268 | def assignable_users |
|
268 | 269 | members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort |
|
269 | 270 | end |
|
270 | 271 | |
|
271 | 272 | # Returns the mail adresses of users that should be always notified on project events |
|
272 | 273 | def recipients |
|
273 | 274 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} |
|
274 | 275 | end |
|
275 | 276 | |
|
276 | 277 | # Returns an array of all custom fields enabled for project issues |
|
277 | 278 | # (explictly associated custom fields and custom fields enabled for all projects) |
|
278 | 279 | def all_issue_custom_fields |
|
279 | 280 | @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort |
|
280 | 281 | end |
|
281 | 282 | |
|
282 | 283 | def project |
|
283 | 284 | self |
|
284 | 285 | end |
|
285 | 286 | |
|
286 | 287 | def <=>(project) |
|
287 | 288 | name.downcase <=> project.name.downcase |
|
288 | 289 | end |
|
289 | 290 | |
|
290 | 291 | def to_s |
|
291 | 292 | name |
|
292 | 293 | end |
|
293 | 294 | |
|
294 | 295 | # Returns a short description of the projects (first lines) |
|
295 | 296 | def short_description(length = 255) |
|
296 | 297 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description |
|
297 | 298 | end |
|
298 | 299 | |
|
299 | 300 | # Return true if this project is allowed to do the specified action. |
|
300 | 301 | # action can be: |
|
301 | 302 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
302 | 303 | # * a permission Symbol (eg. :edit_project) |
|
303 | 304 | def allows_to?(action) |
|
304 | 305 | if action.is_a? Hash |
|
305 | 306 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
306 | 307 | else |
|
307 | 308 | allowed_permissions.include? action |
|
308 | 309 | end |
|
309 | 310 | end |
|
310 | 311 | |
|
311 | 312 | def module_enabled?(module_name) |
|
312 | 313 | module_name = module_name.to_s |
|
313 | 314 | enabled_modules.detect {|m| m.name == module_name} |
|
314 | 315 | end |
|
315 | 316 | |
|
316 | 317 | def enabled_module_names=(module_names) |
|
317 | 318 | if module_names && module_names.is_a?(Array) |
|
318 | 319 | module_names = module_names.collect(&:to_s) |
|
319 | 320 | # remove disabled modules |
|
320 | 321 | enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} |
|
321 | 322 | # add new modules |
|
322 | 323 | module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)} |
|
323 | 324 | else |
|
324 | 325 | enabled_modules.clear |
|
325 | 326 | end |
|
326 | 327 | end |
|
327 | 328 | |
|
328 | 329 | # Returns an auto-generated project identifier based on the last identifier used |
|
329 | 330 | def self.next_identifier |
|
330 | 331 | p = Project.find(:first, :order => 'created_on DESC') |
|
331 | 332 | p.nil? ? nil : p.identifier.to_s.succ |
|
332 | 333 | end |
|
333 | 334 | |
|
334 | 335 | # Copies and saves the Project instance based on the +project+. |
|
335 | 336 | # Will duplicate the source project's: |
|
336 | 337 | # * Issues |
|
337 | 338 | # * Members |
|
338 | 339 | # * Queries |
|
339 | 340 | def copy(project) |
|
340 | 341 | project = project.is_a?(Project) ? project : Project.find(project) |
|
341 | 342 | |
|
342 | 343 | Project.transaction do |
|
343 | 344 | # Issues |
|
344 | 345 | project.issues.each do |issue| |
|
345 | 346 | new_issue = Issue.new |
|
346 | 347 | new_issue.copy_from(issue) |
|
347 | 348 | self.issues << new_issue |
|
348 | 349 | end |
|
349 | 350 | |
|
350 | 351 | # Members |
|
351 | 352 | project.members.each do |member| |
|
352 | 353 | new_member = Member.new |
|
353 | 354 | new_member.attributes = member.attributes.dup.except("project_id") |
|
354 | 355 | new_member.role_ids = member.role_ids.dup |
|
355 | 356 | new_member.project = self |
|
356 | 357 | self.members << new_member |
|
357 | 358 | end |
|
358 | 359 | |
|
359 | 360 | # Queries |
|
360 | 361 | project.queries.each do |query| |
|
361 | 362 | new_query = Query.new |
|
362 | 363 | new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria") |
|
363 | 364 | new_query.sort_criteria = query.sort_criteria if query.sort_criteria |
|
364 | 365 | new_query.project = self |
|
365 | 366 | self.queries << new_query |
|
366 | 367 | end |
|
367 | 368 | |
|
368 | 369 | Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self) |
|
369 | 370 | self.save |
|
370 | 371 | end |
|
371 | 372 | end |
|
372 | 373 | |
|
373 | 374 | |
|
374 | 375 | # Copies +project+ and returns the new instance. This will not save |
|
375 | 376 | # the copy |
|
376 | 377 | def self.copy_from(project) |
|
377 | 378 | begin |
|
378 | 379 | project = project.is_a?(Project) ? project : Project.find(project) |
|
379 | 380 | if project |
|
380 | 381 | # clear unique attributes |
|
381 | 382 | attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status') |
|
382 | 383 | copy = Project.new(attributes) |
|
383 | 384 | copy.enabled_modules = project.enabled_modules |
|
384 | 385 | copy.trackers = project.trackers |
|
385 | 386 | copy.custom_values = project.custom_values.collect {|v| v.clone} |
|
386 | 387 | return copy |
|
387 | 388 | else |
|
388 | 389 | return nil |
|
389 | 390 | end |
|
390 | 391 | rescue ActiveRecord::RecordNotFound |
|
391 | 392 | return nil |
|
392 | 393 | end |
|
393 | 394 | end |
|
394 | 395 | |
|
395 | protected | |
|
396 | def validate | |
|
397 | errors.add(:identifier, :invalid) if !identifier.blank? && identifier.match(/^\d*$/) | |
|
398 | end | |
|
399 | ||
|
400 | 396 | private |
|
401 | 397 | def allowed_permissions |
|
402 | 398 | @allowed_permissions ||= begin |
|
403 | 399 | module_names = enabled_modules.collect {|m| m.name} |
|
404 | 400 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} |
|
405 | 401 | end |
|
406 | 402 | end |
|
407 | 403 | |
|
408 | 404 | def allowed_actions |
|
409 | 405 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
410 | 406 | end |
|
411 | 407 | end |
@@ -1,328 +1,342 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper' |
|
19 | 19 | |
|
20 | 20 | class ProjectTest < Test::Unit::TestCase |
|
21 | 21 | fixtures :projects, :enabled_modules, |
|
22 | 22 | :issues, :issue_statuses, :journals, :journal_details, |
|
23 | 23 | :users, :members, :member_roles, :roles, :projects_trackers, :trackers, :boards, |
|
24 | 24 | :queries |
|
25 | 25 | |
|
26 | 26 | def setup |
|
27 | 27 | @ecookbook = Project.find(1) |
|
28 | 28 | @ecookbook_sub1 = Project.find(3) |
|
29 | 29 | end |
|
30 | 30 | |
|
31 | 31 | def test_truth |
|
32 | 32 | assert_kind_of Project, @ecookbook |
|
33 | 33 | assert_equal "eCookbook", @ecookbook.name |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | def test_update |
|
37 | 37 | assert_equal "eCookbook", @ecookbook.name |
|
38 | 38 | @ecookbook.name = "eCook" |
|
39 | 39 | assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ") |
|
40 | 40 | @ecookbook.reload |
|
41 | 41 | assert_equal "eCook", @ecookbook.name |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def test_validate |
|
45 | 45 | @ecookbook.name = "" |
|
46 | 46 | assert !@ecookbook.save |
|
47 | 47 | assert_equal 1, @ecookbook.errors.count |
|
48 | 48 | assert_equal I18n.translate('activerecord.errors.messages.blank'), @ecookbook.errors.on(:name) |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | def test_validate_identifier | |
|
52 | to_test = {"abc" => true, | |
|
53 | "ab12" => true, | |
|
54 | "ab-12" => true, | |
|
55 | "12" => false} | |
|
56 | ||
|
57 | to_test.each do |identifier, valid| | |
|
58 | p = Project.new | |
|
59 | p.identifier = identifier | |
|
60 | p.valid? | |
|
61 | assert_equal valid, p.errors.on('identifier').nil? | |
|
62 | end | |
|
63 | end | |
|
64 | ||
|
51 | 65 | def test_archive |
|
52 | 66 | user = @ecookbook.members.first.user |
|
53 | 67 | @ecookbook.archive |
|
54 | 68 | @ecookbook.reload |
|
55 | 69 | |
|
56 | 70 | assert !@ecookbook.active? |
|
57 | 71 | assert !user.projects.include?(@ecookbook) |
|
58 | 72 | # Subproject are also archived |
|
59 | 73 | assert !@ecookbook.children.empty? |
|
60 | 74 | assert @ecookbook.descendants.active.empty? |
|
61 | 75 | end |
|
62 | 76 | |
|
63 | 77 | def test_unarchive |
|
64 | 78 | user = @ecookbook.members.first.user |
|
65 | 79 | @ecookbook.archive |
|
66 | 80 | # A subproject of an archived project can not be unarchived |
|
67 | 81 | assert !@ecookbook_sub1.unarchive |
|
68 | 82 | |
|
69 | 83 | # Unarchive project |
|
70 | 84 | assert @ecookbook.unarchive |
|
71 | 85 | @ecookbook.reload |
|
72 | 86 | assert @ecookbook.active? |
|
73 | 87 | assert user.projects.include?(@ecookbook) |
|
74 | 88 | # Subproject can now be unarchived |
|
75 | 89 | @ecookbook_sub1.reload |
|
76 | 90 | assert @ecookbook_sub1.unarchive |
|
77 | 91 | end |
|
78 | 92 | |
|
79 | 93 | def test_destroy |
|
80 | 94 | # 2 active members |
|
81 | 95 | assert_equal 2, @ecookbook.members.size |
|
82 | 96 | # and 1 is locked |
|
83 | 97 | assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size |
|
84 | 98 | # some boards |
|
85 | 99 | assert @ecookbook.boards.any? |
|
86 | 100 | |
|
87 | 101 | @ecookbook.destroy |
|
88 | 102 | # make sure that the project non longer exists |
|
89 | 103 | assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) } |
|
90 | 104 | # make sure related data was removed |
|
91 | 105 | assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? |
|
92 | 106 | assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? |
|
93 | 107 | end |
|
94 | 108 | |
|
95 | 109 | def test_move_an_orphan_project_to_a_root_project |
|
96 | 110 | sub = Project.find(2) |
|
97 | 111 | sub.set_parent! @ecookbook |
|
98 | 112 | assert_equal @ecookbook.id, sub.parent.id |
|
99 | 113 | @ecookbook.reload |
|
100 | 114 | assert_equal 4, @ecookbook.children.size |
|
101 | 115 | end |
|
102 | 116 | |
|
103 | 117 | def test_move_an_orphan_project_to_a_subproject |
|
104 | 118 | sub = Project.find(2) |
|
105 | 119 | assert sub.set_parent!(@ecookbook_sub1) |
|
106 | 120 | end |
|
107 | 121 | |
|
108 | 122 | def test_move_a_root_project_to_a_project |
|
109 | 123 | sub = @ecookbook |
|
110 | 124 | assert sub.set_parent!(Project.find(2)) |
|
111 | 125 | end |
|
112 | 126 | |
|
113 | 127 | def test_should_not_move_a_project_to_its_children |
|
114 | 128 | sub = @ecookbook |
|
115 | 129 | assert !(sub.set_parent!(Project.find(3))) |
|
116 | 130 | end |
|
117 | 131 | |
|
118 | 132 | def test_set_parent_should_add_roots_in_alphabetical_order |
|
119 | 133 | ProjectCustomField.delete_all |
|
120 | 134 | Project.delete_all |
|
121 | 135 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil) |
|
122 | 136 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil) |
|
123 | 137 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil) |
|
124 | 138 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil) |
|
125 | 139 | |
|
126 | 140 | assert_equal 4, Project.count |
|
127 | 141 | assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft) |
|
128 | 142 | end |
|
129 | 143 | |
|
130 | 144 | def test_set_parent_should_add_children_in_alphabetical_order |
|
131 | 145 | ProjectCustomField.delete_all |
|
132 | 146 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') |
|
133 | 147 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent) |
|
134 | 148 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent) |
|
135 | 149 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent) |
|
136 | 150 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent) |
|
137 | 151 | |
|
138 | 152 | parent.reload |
|
139 | 153 | assert_equal 4, parent.children.size |
|
140 | 154 | assert_equal parent.children.sort_by(&:name), parent.children |
|
141 | 155 | end |
|
142 | 156 | |
|
143 | 157 | def test_rebuild_should_sort_children_alphabetically |
|
144 | 158 | ProjectCustomField.delete_all |
|
145 | 159 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') |
|
146 | 160 | Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent) |
|
147 | 161 | Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent) |
|
148 | 162 | Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent) |
|
149 | 163 | Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent) |
|
150 | 164 | |
|
151 | 165 | Project.update_all("lft = NULL, rgt = NULL") |
|
152 | 166 | Project.rebuild! |
|
153 | 167 | |
|
154 | 168 | parent.reload |
|
155 | 169 | assert_equal 4, parent.children.size |
|
156 | 170 | assert_equal parent.children.sort_by(&:name), parent.children |
|
157 | 171 | end |
|
158 | 172 | |
|
159 | 173 | def test_parent |
|
160 | 174 | p = Project.find(6).parent |
|
161 | 175 | assert p.is_a?(Project) |
|
162 | 176 | assert_equal 5, p.id |
|
163 | 177 | end |
|
164 | 178 | |
|
165 | 179 | def test_ancestors |
|
166 | 180 | a = Project.find(6).ancestors |
|
167 | 181 | assert a.first.is_a?(Project) |
|
168 | 182 | assert_equal [1, 5], a.collect(&:id) |
|
169 | 183 | end |
|
170 | 184 | |
|
171 | 185 | def test_root |
|
172 | 186 | r = Project.find(6).root |
|
173 | 187 | assert r.is_a?(Project) |
|
174 | 188 | assert_equal 1, r.id |
|
175 | 189 | end |
|
176 | 190 | |
|
177 | 191 | def test_children |
|
178 | 192 | c = Project.find(1).children |
|
179 | 193 | assert c.first.is_a?(Project) |
|
180 | 194 | assert_equal [5, 3, 4], c.collect(&:id) |
|
181 | 195 | end |
|
182 | 196 | |
|
183 | 197 | def test_descendants |
|
184 | 198 | d = Project.find(1).descendants |
|
185 | 199 | assert d.first.is_a?(Project) |
|
186 | 200 | assert_equal [5, 6, 3, 4], d.collect(&:id) |
|
187 | 201 | end |
|
188 | 202 | |
|
189 | 203 | def test_users_by_role |
|
190 | 204 | users_by_role = Project.find(1).users_by_role |
|
191 | 205 | assert_kind_of Hash, users_by_role |
|
192 | 206 | role = Role.find(1) |
|
193 | 207 | assert_kind_of Array, users_by_role[role] |
|
194 | 208 | assert users_by_role[role].include?(User.find(2)) |
|
195 | 209 | end |
|
196 | 210 | |
|
197 | 211 | def test_rolled_up_trackers |
|
198 | 212 | parent = Project.find(1) |
|
199 | 213 | parent.trackers = Tracker.find([1,2]) |
|
200 | 214 | child = parent.children.find(3) |
|
201 | 215 | |
|
202 | 216 | assert_equal [1, 2], parent.tracker_ids |
|
203 | 217 | assert_equal [2, 3], child.trackers.collect(&:id) |
|
204 | 218 | |
|
205 | 219 | assert_kind_of Tracker, parent.rolled_up_trackers.first |
|
206 | 220 | assert_equal Tracker.find(1), parent.rolled_up_trackers.first |
|
207 | 221 | |
|
208 | 222 | assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id) |
|
209 | 223 | assert_equal [2, 3], child.rolled_up_trackers.collect(&:id) |
|
210 | 224 | end |
|
211 | 225 | |
|
212 | 226 | def test_rolled_up_trackers_should_ignore_archived_subprojects |
|
213 | 227 | parent = Project.find(1) |
|
214 | 228 | parent.trackers = Tracker.find([1,2]) |
|
215 | 229 | child = parent.children.find(3) |
|
216 | 230 | child.trackers = Tracker.find([1,3]) |
|
217 | 231 | parent.children.each(&:archive) |
|
218 | 232 | |
|
219 | 233 | assert_equal [1,2], parent.rolled_up_trackers.collect(&:id) |
|
220 | 234 | end |
|
221 | 235 | |
|
222 | 236 | def test_next_identifier |
|
223 | 237 | ProjectCustomField.delete_all |
|
224 | 238 | Project.create!(:name => 'last', :identifier => 'p2008040') |
|
225 | 239 | assert_equal 'p2008041', Project.next_identifier |
|
226 | 240 | end |
|
227 | 241 | |
|
228 | 242 | def test_next_identifier_first_project |
|
229 | 243 | Project.delete_all |
|
230 | 244 | assert_nil Project.next_identifier |
|
231 | 245 | end |
|
232 | 246 | |
|
233 | 247 | |
|
234 | 248 | def test_enabled_module_names_should_not_recreate_enabled_modules |
|
235 | 249 | project = Project.find(1) |
|
236 | 250 | # Remove one module |
|
237 | 251 | modules = project.enabled_modules.slice(0..-2) |
|
238 | 252 | assert modules.any? |
|
239 | 253 | assert_difference 'EnabledModule.count', -1 do |
|
240 | 254 | project.enabled_module_names = modules.collect(&:name) |
|
241 | 255 | end |
|
242 | 256 | project.reload |
|
243 | 257 | # Ids should be preserved |
|
244 | 258 | assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort |
|
245 | 259 | end |
|
246 | 260 | |
|
247 | 261 | def test_copy_from_existing_project |
|
248 | 262 | source_project = Project.find(1) |
|
249 | 263 | copied_project = Project.copy_from(1) |
|
250 | 264 | |
|
251 | 265 | assert copied_project |
|
252 | 266 | # Cleared attributes |
|
253 | 267 | assert copied_project.id.blank? |
|
254 | 268 | assert copied_project.name.blank? |
|
255 | 269 | assert copied_project.identifier.blank? |
|
256 | 270 | |
|
257 | 271 | # Duplicated attributes |
|
258 | 272 | assert_equal source_project.description, copied_project.description |
|
259 | 273 | assert_equal source_project.enabled_modules, copied_project.enabled_modules |
|
260 | 274 | assert_equal source_project.trackers, copied_project.trackers |
|
261 | 275 | |
|
262 | 276 | # Default attributes |
|
263 | 277 | assert_equal 1, copied_project.status |
|
264 | 278 | end |
|
265 | 279 | |
|
266 | 280 | # Context: Project#copy |
|
267 | 281 | def test_copy_should_copy_issues |
|
268 | 282 | # Setup |
|
269 | 283 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests |
|
270 | 284 | source_project = Project.find(2) |
|
271 | 285 | Project.destroy_all :identifier => "copy-test" |
|
272 | 286 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') |
|
273 | 287 | project.trackers = source_project.trackers |
|
274 | 288 | assert project.valid? |
|
275 | 289 | |
|
276 | 290 | assert project.issues.empty? |
|
277 | 291 | assert project.copy(source_project) |
|
278 | 292 | |
|
279 | 293 | # Tests |
|
280 | 294 | assert_equal source_project.issues.size, project.issues.size |
|
281 | 295 | project.issues.each do |issue| |
|
282 | 296 | assert issue.valid? |
|
283 | 297 | assert ! issue.assigned_to.blank? |
|
284 | 298 | assert_equal project, issue.project |
|
285 | 299 | end |
|
286 | 300 | end |
|
287 | 301 | |
|
288 | 302 | def test_copy_should_copy_members |
|
289 | 303 | # Setup |
|
290 | 304 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests |
|
291 | 305 | source_project = Project.find(2) |
|
292 | 306 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') |
|
293 | 307 | project.trackers = source_project.trackers |
|
294 | 308 | project.enabled_modules = source_project.enabled_modules |
|
295 | 309 | assert project.valid? |
|
296 | 310 | |
|
297 | 311 | assert project.members.empty? |
|
298 | 312 | assert project.copy(source_project) |
|
299 | 313 | |
|
300 | 314 | # Tests |
|
301 | 315 | assert_equal source_project.members.size, project.members.size |
|
302 | 316 | project.members.each do |member| |
|
303 | 317 | assert member |
|
304 | 318 | assert_equal project, member.project |
|
305 | 319 | end |
|
306 | 320 | end |
|
307 | 321 | |
|
308 | 322 | def test_copy_should_copy_project_level_queries |
|
309 | 323 | # Setup |
|
310 | 324 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests |
|
311 | 325 | source_project = Project.find(2) |
|
312 | 326 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') |
|
313 | 327 | project.trackers = source_project.trackers |
|
314 | 328 | project.enabled_modules = source_project.enabled_modules |
|
315 | 329 | assert project.valid? |
|
316 | 330 | |
|
317 | 331 | assert project.queries.empty? |
|
318 | 332 | assert project.copy(source_project) |
|
319 | 333 | |
|
320 | 334 | # Tests |
|
321 | 335 | assert_equal source_project.queries.size, project.queries.size |
|
322 | 336 | project.queries.each do |query| |
|
323 | 337 | assert query |
|
324 | 338 | assert_equal project, query.project |
|
325 | 339 | end |
|
326 | 340 | end |
|
327 | 341 | |
|
328 | 342 | end |
General Comments 0
You need to be logged in to leave comments.
Login now