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