@@ -1,319 +1,323 | |||
|
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 => 2..20 |
|
64 | 64 | validates_format_of :identifier, :with => /^[a-z0-9\-]*$/ |
|
65 | 65 | |
|
66 | 66 | before_destroy :delete_all_members |
|
67 | 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 | 69 | named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"} |
|
70 | 70 | named_scope :public, { :conditions => { :is_public => true } } |
|
71 | 71 | named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } |
|
72 | 72 | |
|
73 | 73 | def identifier=(identifier) |
|
74 | 74 | super unless identifier_frozen? |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def identifier_frozen? |
|
78 | 78 | errors[:identifier].nil? && !(new_record? || identifier.blank?) |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def issues_with_subprojects(include_subprojects=false) |
|
82 | 82 | conditions = nil |
|
83 | 83 | if include_subprojects |
|
84 | 84 | ids = [id] + descendants.collect(&:id) |
|
85 | 85 | conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"] |
|
86 | 86 | end |
|
87 | 87 | conditions ||= ["#{Project.table_name}.id = ?", id] |
|
88 | 88 | # Quick and dirty fix for Rails 2 compatibility |
|
89 | 89 | Issue.send(:with_scope, :find => { :conditions => conditions }) do |
|
90 | 90 | Version.send(:with_scope, :find => { :conditions => conditions }) do |
|
91 | 91 | yield |
|
92 | 92 | end |
|
93 | 93 | end |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | # returns latest created projects |
|
97 | 97 | # non public projects will be returned only if user is a member of those |
|
98 | 98 | def self.latest(user=nil, count=5) |
|
99 | 99 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") |
|
100 | 100 | end |
|
101 | 101 | |
|
102 | 102 | def self.visible_by(user=nil) |
|
103 | 103 | user ||= User.current |
|
104 | 104 | if user && user.admin? |
|
105 | 105 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
106 | 106 | elsif user && user.memberships.any? |
|
107 | 107 | 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(',')}))" |
|
108 | 108 | else |
|
109 | 109 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}" |
|
110 | 110 | end |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | def self.allowed_to_condition(user, permission, options={}) |
|
114 | 114 | statements = [] |
|
115 | 115 | base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
116 | 116 | if perm = Redmine::AccessControl.permission(permission) |
|
117 | 117 | unless perm.project_module.nil? |
|
118 | 118 | # If the permission belongs to a project module, make sure the module is enabled |
|
119 | 119 | 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)" |
|
120 | 120 | end |
|
121 | 121 | end |
|
122 | 122 | if options[:project] |
|
123 | 123 | project_statement = "#{Project.table_name}.id = #{options[:project].id}" |
|
124 | 124 | project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects] |
|
125 | 125 | base_statement = "(#{project_statement}) AND (#{base_statement})" |
|
126 | 126 | end |
|
127 | 127 | if user.admin? |
|
128 | 128 | # no restriction |
|
129 | 129 | else |
|
130 | 130 | statements << "1=0" |
|
131 | 131 | if user.logged? |
|
132 | 132 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission) |
|
133 | 133 | allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id} |
|
134 | 134 | statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any? |
|
135 | 135 | elsif Role.anonymous.allowed_to?(permission) |
|
136 | 136 | # anonymous user allowed on public project |
|
137 | 137 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" |
|
138 | 138 | else |
|
139 | 139 | # anonymous user is not authorized |
|
140 | 140 | end |
|
141 | 141 | end |
|
142 | 142 | statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))" |
|
143 | 143 | end |
|
144 | 144 | |
|
145 | 145 | def project_condition(with_subprojects) |
|
146 | 146 | cond = "#{Project.table_name}.id = #{id}" |
|
147 | 147 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects |
|
148 | 148 | cond |
|
149 | 149 | end |
|
150 | 150 | |
|
151 | 151 | def self.find(*args) |
|
152 | 152 | if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) |
|
153 | 153 | project = find_by_identifier(*args) |
|
154 | 154 | raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? |
|
155 | 155 | project |
|
156 | 156 | else |
|
157 | 157 | super |
|
158 | 158 | end |
|
159 | 159 | end |
|
160 | 160 | |
|
161 | 161 | def to_param |
|
162 | 162 | # id is used for projects with a numeric identifier (compatibility) |
|
163 | 163 | @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier) |
|
164 | 164 | end |
|
165 | 165 | |
|
166 | 166 | def active? |
|
167 | 167 | self.status == STATUS_ACTIVE |
|
168 | 168 | end |
|
169 | 169 | |
|
170 | 170 | # Archives the project and its descendants recursively |
|
171 | 171 | def archive |
|
172 | 172 | # Archive subprojects if any |
|
173 | 173 | children.each do |subproject| |
|
174 | 174 | subproject.archive |
|
175 | 175 | end |
|
176 | 176 | update_attribute :status, STATUS_ARCHIVED |
|
177 | 177 | end |
|
178 | 178 | |
|
179 | 179 | # Unarchives the project |
|
180 | 180 | # All its ancestors must be active |
|
181 | 181 | def unarchive |
|
182 | 182 | return false if ancestors.detect {|a| !a.active?} |
|
183 | 183 | update_attribute :status, STATUS_ACTIVE |
|
184 | 184 | end |
|
185 | 185 | |
|
186 | 186 | # Returns an array of projects the project can be moved to |
|
187 | 187 | def possible_parents |
|
188 | 188 | @possible_parents ||= (Project.active.find(:all) - self_and_descendants) |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | # Sets the parent of the project |
|
192 | 192 | # Argument can be either a Project, a String, a Fixnum or nil |
|
193 | 193 | def set_parent!(p) |
|
194 | 194 | unless p.nil? || p.is_a?(Project) |
|
195 | 195 | if p.to_s.blank? |
|
196 | 196 | p = nil |
|
197 | 197 | else |
|
198 | 198 | p = Project.find_by_id(p) |
|
199 | 199 | return false unless p |
|
200 | 200 | end |
|
201 | 201 | end |
|
202 | 202 | if p == parent && !p.nil? |
|
203 | 203 | # Nothing to do |
|
204 | 204 | true |
|
205 | 205 | elsif p.nil? || (p.active? && move_possible?(p)) |
|
206 | 206 | # Insert the project so that target's children or root projects stay alphabetically sorted |
|
207 | 207 | sibs = (p.nil? ? self.class.roots : p.children) |
|
208 | 208 | to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase } |
|
209 | 209 | if to_be_inserted_before |
|
210 | 210 | move_to_left_of(to_be_inserted_before) |
|
211 | 211 | elsif p.nil? |
|
212 | 212 | if sibs.empty? |
|
213 | 213 | # move_to_root adds the project in first (ie. left) position |
|
214 | 214 | move_to_root |
|
215 | 215 | else |
|
216 | 216 | move_to_right_of(sibs.last) unless self == sibs.last |
|
217 | 217 | end |
|
218 | 218 | else |
|
219 | 219 | # move_to_child_of adds the project in last (ie.right) position |
|
220 | 220 | move_to_child_of(p) |
|
221 | 221 | end |
|
222 | 222 | true |
|
223 | 223 | else |
|
224 | 224 | # Can not move to the given target |
|
225 | 225 | false |
|
226 | 226 | end |
|
227 | 227 | end |
|
228 | 228 | |
|
229 | 229 | # Returns an array of the trackers used by the project and its active sub projects |
|
230 | 230 | def rolled_up_trackers |
|
231 | 231 | @rolled_up_trackers ||= |
|
232 | 232 | Tracker.find(:all, :include => :projects, |
|
233 | 233 | :select => "DISTINCT #{Tracker.table_name}.*", |
|
234 | 234 | :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt], |
|
235 | 235 | :order => "#{Tracker.table_name}.position") |
|
236 | 236 | end |
|
237 | 237 | |
|
238 | 238 | # Deletes all project's members |
|
239 | 239 | def delete_all_members |
|
240 | 240 | Member.delete_all(['project_id = ?', id]) |
|
241 | 241 | end |
|
242 | 242 | |
|
243 | 243 | # Users issues can be assigned to |
|
244 | 244 | def assignable_users |
|
245 | 245 | members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort |
|
246 | 246 | end |
|
247 | 247 | |
|
248 | 248 | # Returns the mail adresses of users that should be always notified on project events |
|
249 | 249 | def recipients |
|
250 | 250 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} |
|
251 | 251 | end |
|
252 | 252 | |
|
253 | 253 | # Returns an array of all custom fields enabled for project issues |
|
254 | 254 | # (explictly associated custom fields and custom fields enabled for all projects) |
|
255 | 255 | def all_issue_custom_fields |
|
256 | 256 | @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort |
|
257 | 257 | end |
|
258 | 258 | |
|
259 | 259 | def project |
|
260 | 260 | self |
|
261 | 261 | end |
|
262 | 262 | |
|
263 | 263 | def <=>(project) |
|
264 | 264 | name.downcase <=> project.name.downcase |
|
265 | 265 | end |
|
266 | 266 | |
|
267 | 267 | def to_s |
|
268 | 268 | name |
|
269 | 269 | end |
|
270 | 270 | |
|
271 | 271 | # Returns a short description of the projects (first lines) |
|
272 | 272 | def short_description(length = 255) |
|
273 | 273 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description |
|
274 | 274 | end |
|
275 | 275 | |
|
276 | 276 | def allows_to?(action) |
|
277 | 277 | if action.is_a? Hash |
|
278 | 278 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
279 | 279 | else |
|
280 | 280 | allowed_permissions.include? action |
|
281 | 281 | end |
|
282 | 282 | end |
|
283 | 283 | |
|
284 | 284 | def module_enabled?(module_name) |
|
285 | 285 | module_name = module_name.to_s |
|
286 | 286 | enabled_modules.detect {|m| m.name == module_name} |
|
287 | 287 | end |
|
288 | 288 | |
|
289 | 289 | def enabled_module_names=(module_names) |
|
290 | enabled_modules.clear | |
|
291 | module_names = [] unless module_names && module_names.is_a?(Array) | |
|
292 | module_names.each do |name| | |
|
293 | enabled_modules << EnabledModule.new(:name => name.to_s) | |
|
290 | if module_names && module_names.is_a?(Array) | |
|
291 | module_names = module_names.collect(&:to_s) | |
|
292 | # remove disabled modules | |
|
293 | enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} | |
|
294 | # add new modules | |
|
295 | module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)} | |
|
296 | else | |
|
297 | enabled_modules.clear | |
|
294 | 298 | end |
|
295 | 299 | end |
|
296 | 300 | |
|
297 | 301 | # Returns an auto-generated project identifier based on the last identifier used |
|
298 | 302 | def self.next_identifier |
|
299 | 303 | p = Project.find(:first, :order => 'created_on DESC') |
|
300 | 304 | p.nil? ? nil : p.identifier.to_s.succ |
|
301 | 305 | end |
|
302 | 306 | |
|
303 | 307 | protected |
|
304 | 308 | def validate |
|
305 | 309 | errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/) |
|
306 | 310 | end |
|
307 | 311 | |
|
308 | 312 | private |
|
309 | 313 | def allowed_permissions |
|
310 | 314 | @allowed_permissions ||= begin |
|
311 | 315 | module_names = enabled_modules.collect {|m| m.name} |
|
312 | 316 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} |
|
313 | 317 | end |
|
314 | 318 | end |
|
315 | 319 | |
|
316 | 320 | def allowed_actions |
|
317 | 321 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
318 | 322 | end |
|
319 | 323 | end |
@@ -1,221 +1,236 | |||
|
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 | fixtures :projects, :issues, :issue_statuses, :journals, :journal_details, :users, :members, :roles, :projects_trackers, :trackers, :boards | |
|
21 | fixtures :projects, :enabled_modules, | |
|
22 | :issues, :issue_statuses, :journals, :journal_details, | |
|
23 | :users, :members, :roles, :projects_trackers, :trackers, :boards | |
|
22 | 24 | |
|
23 | 25 | def setup |
|
24 | 26 | @ecookbook = Project.find(1) |
|
25 | 27 | @ecookbook_sub1 = Project.find(3) |
|
26 | 28 | end |
|
27 | 29 | |
|
28 | 30 | def test_truth |
|
29 | 31 | assert_kind_of Project, @ecookbook |
|
30 | 32 | assert_equal "eCookbook", @ecookbook.name |
|
31 | 33 | end |
|
32 | 34 | |
|
33 | 35 | def test_update |
|
34 | 36 | assert_equal "eCookbook", @ecookbook.name |
|
35 | 37 | @ecookbook.name = "eCook" |
|
36 | 38 | assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ") |
|
37 | 39 | @ecookbook.reload |
|
38 | 40 | assert_equal "eCook", @ecookbook.name |
|
39 | 41 | end |
|
40 | 42 | |
|
41 | 43 | def test_validate |
|
42 | 44 | @ecookbook.name = "" |
|
43 | 45 | assert !@ecookbook.save |
|
44 | 46 | assert_equal 1, @ecookbook.errors.count |
|
45 | 47 | assert_equal "activerecord_error_blank", @ecookbook.errors.on(:name) |
|
46 | 48 | end |
|
47 | 49 | |
|
48 | 50 | def test_archive |
|
49 | 51 | user = @ecookbook.members.first.user |
|
50 | 52 | @ecookbook.archive |
|
51 | 53 | @ecookbook.reload |
|
52 | 54 | |
|
53 | 55 | assert !@ecookbook.active? |
|
54 | 56 | assert !user.projects.include?(@ecookbook) |
|
55 | 57 | # Subproject are also archived |
|
56 | 58 | assert !@ecookbook.children.empty? |
|
57 | 59 | assert @ecookbook.descendants.active.empty? |
|
58 | 60 | end |
|
59 | 61 | |
|
60 | 62 | def test_unarchive |
|
61 | 63 | user = @ecookbook.members.first.user |
|
62 | 64 | @ecookbook.archive |
|
63 | 65 | # A subproject of an archived project can not be unarchived |
|
64 | 66 | assert !@ecookbook_sub1.unarchive |
|
65 | 67 | |
|
66 | 68 | # Unarchive project |
|
67 | 69 | assert @ecookbook.unarchive |
|
68 | 70 | @ecookbook.reload |
|
69 | 71 | assert @ecookbook.active? |
|
70 | 72 | assert user.projects.include?(@ecookbook) |
|
71 | 73 | # Subproject can now be unarchived |
|
72 | 74 | @ecookbook_sub1.reload |
|
73 | 75 | assert @ecookbook_sub1.unarchive |
|
74 | 76 | end |
|
75 | 77 | |
|
76 | 78 | def test_destroy |
|
77 | 79 | # 2 active members |
|
78 | 80 | assert_equal 2, @ecookbook.members.size |
|
79 | 81 | # and 1 is locked |
|
80 | 82 | assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size |
|
81 | 83 | # some boards |
|
82 | 84 | assert @ecookbook.boards.any? |
|
83 | 85 | |
|
84 | 86 | @ecookbook.destroy |
|
85 | 87 | # make sure that the project non longer exists |
|
86 | 88 | assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) } |
|
87 | 89 | # make sure related data was removed |
|
88 | 90 | assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? |
|
89 | 91 | assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? |
|
90 | 92 | end |
|
91 | 93 | |
|
92 | 94 | def test_move_an_orphan_project_to_a_root_project |
|
93 | 95 | sub = Project.find(2) |
|
94 | 96 | sub.set_parent! @ecookbook |
|
95 | 97 | assert_equal @ecookbook.id, sub.parent.id |
|
96 | 98 | @ecookbook.reload |
|
97 | 99 | assert_equal 4, @ecookbook.children.size |
|
98 | 100 | end |
|
99 | 101 | |
|
100 | 102 | def test_move_an_orphan_project_to_a_subproject |
|
101 | 103 | sub = Project.find(2) |
|
102 | 104 | assert sub.set_parent!(@ecookbook_sub1) |
|
103 | 105 | end |
|
104 | 106 | |
|
105 | 107 | def test_move_a_root_project_to_a_project |
|
106 | 108 | sub = @ecookbook |
|
107 | 109 | assert sub.set_parent!(Project.find(2)) |
|
108 | 110 | end |
|
109 | 111 | |
|
110 | 112 | def test_should_not_move_a_project_to_its_children |
|
111 | 113 | sub = @ecookbook |
|
112 | 114 | assert !(sub.set_parent!(Project.find(3))) |
|
113 | 115 | end |
|
114 | 116 | |
|
115 | 117 | def test_set_parent_should_add_roots_in_alphabetical_order |
|
116 | 118 | ProjectCustomField.delete_all |
|
117 | 119 | Project.delete_all |
|
118 | 120 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil) |
|
119 | 121 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil) |
|
120 | 122 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil) |
|
121 | 123 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil) |
|
122 | 124 | |
|
123 | 125 | assert_equal 4, Project.count |
|
124 | 126 | assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft) |
|
125 | 127 | end |
|
126 | 128 | |
|
127 | 129 | def test_set_parent_should_add_children_in_alphabetical_order |
|
128 | 130 | ProjectCustomField.delete_all |
|
129 | 131 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') |
|
130 | 132 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent) |
|
131 | 133 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent) |
|
132 | 134 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent) |
|
133 | 135 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent) |
|
134 | 136 | |
|
135 | 137 | parent.reload |
|
136 | 138 | assert_equal 4, parent.children.size |
|
137 | 139 | assert_equal parent.children.sort_by(&:name), parent.children |
|
138 | 140 | end |
|
139 | 141 | |
|
140 | 142 | def test_rebuild_should_sort_children_alphabetically |
|
141 | 143 | ProjectCustomField.delete_all |
|
142 | 144 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') |
|
143 | 145 | Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent) |
|
144 | 146 | Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent) |
|
145 | 147 | Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent) |
|
146 | 148 | Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent) |
|
147 | 149 | |
|
148 | 150 | Project.update_all("lft = NULL, rgt = NULL") |
|
149 | 151 | Project.rebuild! |
|
150 | 152 | |
|
151 | 153 | parent.reload |
|
152 | 154 | assert_equal 4, parent.children.size |
|
153 | 155 | assert_equal parent.children.sort_by(&:name), parent.children |
|
154 | 156 | end |
|
155 | 157 | |
|
156 | 158 | def test_parent |
|
157 | 159 | p = Project.find(6).parent |
|
158 | 160 | assert p.is_a?(Project) |
|
159 | 161 | assert_equal 5, p.id |
|
160 | 162 | end |
|
161 | 163 | |
|
162 | 164 | def test_ancestors |
|
163 | 165 | a = Project.find(6).ancestors |
|
164 | 166 | assert a.first.is_a?(Project) |
|
165 | 167 | assert_equal [1, 5], a.collect(&:id) |
|
166 | 168 | end |
|
167 | 169 | |
|
168 | 170 | def test_root |
|
169 | 171 | r = Project.find(6).root |
|
170 | 172 | assert r.is_a?(Project) |
|
171 | 173 | assert_equal 1, r.id |
|
172 | 174 | end |
|
173 | 175 | |
|
174 | 176 | def test_children |
|
175 | 177 | c = Project.find(1).children |
|
176 | 178 | assert c.first.is_a?(Project) |
|
177 | 179 | assert_equal [5, 3, 4], c.collect(&:id) |
|
178 | 180 | end |
|
179 | 181 | |
|
180 | 182 | def test_descendants |
|
181 | 183 | d = Project.find(1).descendants |
|
182 | 184 | assert d.first.is_a?(Project) |
|
183 | 185 | assert_equal [5, 6, 3, 4], d.collect(&:id) |
|
184 | 186 | end |
|
185 | 187 | |
|
186 | 188 | def test_rolled_up_trackers |
|
187 | 189 | parent = Project.find(1) |
|
188 | 190 | parent.trackers = Tracker.find([1,2]) |
|
189 | 191 | child = parent.children.find(3) |
|
190 | 192 | |
|
191 | 193 | assert_equal [1, 2], parent.tracker_ids |
|
192 | 194 | assert_equal [2, 3], child.tracker_ids |
|
193 | 195 | |
|
194 | 196 | assert_kind_of Tracker, parent.rolled_up_trackers.first |
|
195 | 197 | assert_equal Tracker.find(1), parent.rolled_up_trackers.first |
|
196 | 198 | |
|
197 | 199 | assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id) |
|
198 | 200 | assert_equal [2, 3], child.rolled_up_trackers.collect(&:id) |
|
199 | 201 | end |
|
200 | 202 | |
|
201 | 203 | def test_rolled_up_trackers_should_ignore_archived_subprojects |
|
202 | 204 | parent = Project.find(1) |
|
203 | 205 | parent.trackers = Tracker.find([1,2]) |
|
204 | 206 | child = parent.children.find(3) |
|
205 | 207 | child.trackers = Tracker.find([1,3]) |
|
206 | 208 | parent.children.each(&:archive) |
|
207 | 209 | |
|
208 | 210 | assert_equal [1,2], parent.rolled_up_trackers.collect(&:id) |
|
209 | 211 | end |
|
210 | 212 | |
|
211 | 213 | def test_next_identifier |
|
212 | 214 | ProjectCustomField.delete_all |
|
213 | 215 | Project.create!(:name => 'last', :identifier => 'p2008040') |
|
214 | 216 | assert_equal 'p2008041', Project.next_identifier |
|
215 | 217 | end |
|
216 | 218 | |
|
217 | 219 | def test_next_identifier_first_project |
|
218 | 220 | Project.delete_all |
|
219 | 221 | assert_nil Project.next_identifier |
|
220 | 222 | end |
|
223 | ||
|
224 | def test_enabled_module_names_should_not_recreate_enabled_modules | |
|
225 | project = Project.find(1) | |
|
226 | # Remove one module | |
|
227 | modules = project.enabled_modules.slice(0..-2) | |
|
228 | assert modules.any? | |
|
229 | assert_difference 'EnabledModule.count', -1 do | |
|
230 | project.enabled_module_names = modules.collect(&:name) | |
|
231 | end | |
|
232 | project.reload | |
|
233 | # Ids should be preserved | |
|
234 | assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort | |
|
235 | end | |
|
221 | 236 | end |
General Comments 0
You need to be logged in to leave comments.
Login now