##// END OF EJS Templates
Prevent NoMethodError on nil class if custom_fields params is not present in IssuesController#new (#969)....
Jean-Philippe Lang -
r1302:e4da9d6f10ed
parent child
Show More
@@ -1,417 +1,419
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 class IssuesController < ApplicationController
19 19 layout 'base'
20 20 menu_item :new_issue, :only => :new
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :destroy_attachment]
23 23 before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
24 24 before_filter :find_project, :only => [:new, :update_form, :preview]
25 25 before_filter :authorize, :except => [:index, :changes, :preview, :update_form, :context_menu]
26 26 before_filter :find_optional_project, :only => [:index, :changes]
27 27 accept_key_auth :index, :changes
28 28
29 29 helper :journals
30 30 helper :projects
31 31 include ProjectsHelper
32 32 helper :custom_fields
33 33 include CustomFieldsHelper
34 34 helper :ifpdf
35 35 include IfpdfHelper
36 36 helper :issue_relations
37 37 include IssueRelationsHelper
38 38 helper :watchers
39 39 include WatchersHelper
40 40 helper :attachments
41 41 include AttachmentsHelper
42 42 helper :queries
43 43 helper :sort
44 44 include SortHelper
45 45 include IssuesHelper
46 46
47 47 def index
48 48 sort_init "#{Issue.table_name}.id", "desc"
49 49 sort_update
50 50 retrieve_query
51 51 if @query.valid?
52 52 limit = per_page_option
53 53 respond_to do |format|
54 54 format.html { }
55 55 format.atom { }
56 56 format.csv { limit = Setting.issues_export_limit.to_i }
57 57 format.pdf { limit = Setting.issues_export_limit.to_i }
58 58 end
59 59 @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement)
60 60 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
61 61 @issues = Issue.find :all, :order => sort_clause,
62 62 :include => [ :assigned_to, :status, :tracker, :project, :priority, :category, :fixed_version ],
63 63 :conditions => @query.statement,
64 64 :limit => limit,
65 65 :offset => @issue_pages.current.offset
66 66 respond_to do |format|
67 67 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
68 68 format.atom { render_feed(@issues, :title => l(:label_issue_plural)) }
69 69 format.csv { send_data(issues_to_csv(@issues, @project).read, :type => 'text/csv; header=present', :filename => 'export.csv') }
70 70 format.pdf { send_data(render(:template => 'issues/index.rfpdf', :layout => false), :type => 'application/pdf', :filename => 'export.pdf') }
71 71 end
72 72 else
73 73 # Send html if the query is not valid
74 74 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
75 75 end
76 76 rescue ActiveRecord::RecordNotFound
77 77 render_404
78 78 end
79 79
80 80 def changes
81 81 sort_init "#{Issue.table_name}.id", "desc"
82 82 sort_update
83 83 retrieve_query
84 84 if @query.valid?
85 85 @journals = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ],
86 86 :conditions => @query.statement,
87 87 :limit => 25,
88 88 :order => "#{Journal.table_name}.created_on DESC"
89 89 end
90 90 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
91 91 render :layout => false, :content_type => 'application/atom+xml'
92 92 rescue ActiveRecord::RecordNotFound
93 93 render_404
94 94 end
95 95
96 96 def show
97 97 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) }
98 98 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
99 99 @journals.each_with_index {|j,i| j.indice = i+1}
100 100 @journals.reverse! if User.current.wants_comments_in_reverse_order?
101 101 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
102 102 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
103 103 @activities = Enumeration::get_values('ACTI')
104 104 @priorities = Enumeration::get_values('IPRI')
105 105 respond_to do |format|
106 106 format.html { render :template => 'issues/show.rhtml' }
107 107 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
108 108 format.pdf { send_data(render(:template => 'issues/show.rfpdf', :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
109 109 end
110 110 end
111 111
112 112 # Add a new issue
113 113 # The new issue will be created from an existing one if copy_from parameter is given
114 114 def new
115 115 @issue = params[:copy_from] ? Issue.new.copy_from(params[:copy_from]) : Issue.new(params[:issue])
116 116 @issue.project = @project
117 117 @issue.author = User.current
118 118 @issue.tracker ||= @project.trackers.find(params[:tracker_id] ? params[:tracker_id] : :first)
119 119 if @issue.tracker.nil?
120 120 flash.now[:error] = 'No tracker is associated to this project. Please check the Project settings.'
121 121 render :nothing => true, :layout => true
122 122 return
123 123 end
124 124
125 125 default_status = IssueStatus.default
126 126 unless default_status
127 127 flash.now[:error] = 'No default issue status is defined. Please check your configuration (Go to "Administration -> Issue statuses").'
128 128 render :nothing => true, :layout => true
129 129 return
130 130 end
131 131 @issue.status = default_status
132 132 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker)).uniq
133 133
134 134 if request.get? || request.xhr?
135 135 @issue.start_date ||= Date.today
136 136 @custom_values = @issue.custom_values.empty? ?
137 137 @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) } :
138 138 @issue.custom_values
139 139 else
140 140 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
141 141 # Check that the user is allowed to apply the requested status
142 142 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
143 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
143 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x,
144 :customized => @issue,
145 :value => (params[:custom_fields] ? params[:custom_fields][x.id.to_s] : nil)) }
144 146 @issue.custom_values = @custom_values
145 147 if @issue.save
146 148 attach_files(@issue, params[:attachments])
147 149 flash[:notice] = l(:notice_successful_create)
148 150 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
149 151 redirect_to :controller => 'issues', :action => 'show', :id => @issue, :project_id => @project
150 152 return
151 153 end
152 154 end
153 155 @priorities = Enumeration::get_values('IPRI')
154 156 render :layout => !request.xhr?
155 157 end
156 158
157 159 # Attributes that can be updated on workflow transition (without :edit permission)
158 160 # TODO: make it configurable (at least per role)
159 161 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
160 162
161 163 def edit
162 164 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
163 165 @activities = Enumeration::get_values('ACTI')
164 166 @priorities = Enumeration::get_values('IPRI')
165 167 @custom_values = []
166 168 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
167 169
168 170 @notes = params[:notes]
169 171 journal = @issue.init_journal(User.current, @notes)
170 172 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
171 173 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
172 174 attrs = params[:issue].dup
173 175 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
174 176 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
175 177 @issue.attributes = attrs
176 178 end
177 179
178 180 if request.get?
179 181 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) }
180 182 else
181 183 # Update custom fields if user has :edit permission
182 184 if @edit_allowed && params[:custom_fields]
183 185 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
184 186 @issue.custom_values = @custom_values
185 187 end
186 188 attachments = attach_files(@issue, params[:attachments])
187 189 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
188 190 if @issue.save
189 191 # Log spend time
190 192 if current_role.allowed_to?(:log_time)
191 193 @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
192 194 @time_entry.attributes = params[:time_entry]
193 195 @time_entry.save
194 196 end
195 197 if !journal.new_record?
196 198 # Only send notification if something was actually changed
197 199 flash[:notice] = l(:notice_successful_update)
198 200 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
199 201 end
200 202 redirect_to(params[:back_to] || {:action => 'show', :id => @issue})
201 203 end
202 204 end
203 205 rescue ActiveRecord::StaleObjectError
204 206 # Optimistic locking exception
205 207 flash.now[:error] = l(:notice_locking_conflict)
206 208 end
207 209
208 210 # Bulk edit a set of issues
209 211 def bulk_edit
210 212 if request.post?
211 213 status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
212 214 priority = params[:priority_id].blank? ? nil : Enumeration.find_by_id(params[:priority_id])
213 215 assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
214 216 category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
215 217 fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.versions.find_by_id(params[:fixed_version_id])
216 218
217 219 unsaved_issue_ids = []
218 220 @issues.each do |issue|
219 221 journal = issue.init_journal(User.current, params[:notes])
220 222 issue.priority = priority if priority
221 223 issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
222 224 issue.category = category if category || params[:category_id] == 'none'
223 225 issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none'
224 226 issue.start_date = params[:start_date] unless params[:start_date].blank?
225 227 issue.due_date = params[:due_date] unless params[:due_date].blank?
226 228 issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
227 229 # Don't save any change to the issue if the user is not authorized to apply the requested status
228 230 if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
229 231 # Send notification for each issue (if changed)
230 232 Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated')
231 233 else
232 234 # Keep unsaved issue ids to display them in flash error
233 235 unsaved_issue_ids << issue.id
234 236 end
235 237 end
236 238 if unsaved_issue_ids.empty?
237 239 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
238 240 else
239 241 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
240 242 end
241 243 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
242 244 return
243 245 end
244 246 # Find potential statuses the user could be allowed to switch issues to
245 247 @available_statuses = Workflow.find(:all, :include => :new_status,
246 248 :conditions => {:role_id => current_role.id}).collect(&:new_status).compact.uniq
247 249 end
248 250
249 251 def move
250 252 @allowed_projects = []
251 253 # find projects to which the user is allowed to move the issue
252 254 if User.current.admin?
253 255 # admin is allowed to move issues to any active (visible) project
254 256 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current), :order => 'name')
255 257 else
256 258 User.current.memberships.each {|m| @allowed_projects << m.project if m.role.allowed_to?(:move_issues)}
257 259 end
258 260 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
259 261 @target_project ||= @project
260 262 @trackers = @target_project.trackers
261 263 if request.post?
262 264 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
263 265 unsaved_issue_ids = []
264 266 @issues.each do |issue|
265 267 unsaved_issue_ids << issue.id unless issue.move_to(@target_project, new_tracker)
266 268 end
267 269 if unsaved_issue_ids.empty?
268 270 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
269 271 else
270 272 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
271 273 end
272 274 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
273 275 return
274 276 end
275 277 render :layout => false if request.xhr?
276 278 end
277 279
278 280 def destroy
279 281 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
280 282 if @hours > 0
281 283 case params[:todo]
282 284 when 'destroy'
283 285 # nothing to do
284 286 when 'nullify'
285 287 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
286 288 when 'reassign'
287 289 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
288 290 if reassign_to.nil?
289 291 flash.now[:error] = l(:error_issue_not_found_in_project)
290 292 return
291 293 else
292 294 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
293 295 end
294 296 else
295 297 # display the destroy form
296 298 return
297 299 end
298 300 end
299 301 @issues.each(&:destroy)
300 302 redirect_to :action => 'index', :project_id => @project
301 303 end
302 304
303 305 def destroy_attachment
304 306 a = @issue.attachments.find(params[:attachment_id])
305 307 a.destroy
306 308 journal = @issue.init_journal(User.current)
307 309 journal.details << JournalDetail.new(:property => 'attachment',
308 310 :prop_key => a.id,
309 311 :old_value => a.filename)
310 312 journal.save
311 313 redirect_to :action => 'show', :id => @issue
312 314 end
313 315
314 316 def context_menu
315 317 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
316 318 if (@issues.size == 1)
317 319 @issue = @issues.first
318 320 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
319 321 @assignables = @issue.assignable_users
320 322 @assignables << @issue.assigned_to if @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
321 323 end
322 324 projects = @issues.collect(&:project).compact.uniq
323 325 @project = projects.first if projects.size == 1
324 326
325 327 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
326 328 :update => (@issue && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && !@allowed_statuses.empty?))),
327 329 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
328 330 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
329 331 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
330 332 }
331 333
332 334 @priorities = Enumeration.get_values('IPRI').reverse
333 335 @statuses = IssueStatus.find(:all, :order => 'position')
334 336 @back = request.env['HTTP_REFERER']
335 337
336 338 render :layout => false
337 339 end
338 340
339 341 def update_form
340 342 @issue = Issue.new(params[:issue])
341 343 render :action => :new, :layout => false
342 344 end
343 345
344 346 def preview
345 347 issue = @project.issues.find_by_id(params[:id])
346 348 @attachements = issue.attachments if issue
347 349 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
348 350 render :partial => 'common/preview'
349 351 end
350 352
351 353 private
352 354 def find_issue
353 355 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
354 356 @project = @issue.project
355 357 rescue ActiveRecord::RecordNotFound
356 358 render_404
357 359 end
358 360
359 361 # Filter for bulk operations
360 362 def find_issues
361 363 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
362 364 raise ActiveRecord::RecordNotFound if @issues.empty?
363 365 projects = @issues.collect(&:project).compact.uniq
364 366 if projects.size == 1
365 367 @project = projects.first
366 368 else
367 369 # TODO: let users bulk edit/move/destroy issues from different projects
368 370 render_error 'Can not bulk edit/move/destroy issues from different projects' and return false
369 371 end
370 372 rescue ActiveRecord::RecordNotFound
371 373 render_404
372 374 end
373 375
374 376 def find_project
375 377 @project = Project.find(params[:project_id])
376 378 rescue ActiveRecord::RecordNotFound
377 379 render_404
378 380 end
379 381
380 382 def find_optional_project
381 383 return true unless params[:project_id]
382 384 @project = Project.find(params[:project_id])
383 385 authorize
384 386 rescue ActiveRecord::RecordNotFound
385 387 render_404
386 388 end
387 389
388 390 # Retrieve query from session or build a new query
389 391 def retrieve_query
390 392 if !params[:query_id].blank?
391 393 cond = "project_id IS NULL"
392 394 cond << " OR project_id = #{@project.id}" if @project
393 395 @query = Query.find(params[:query_id], :conditions => cond)
394 396 @query.project = @project
395 397 session[:query] = {:id => @query.id, :project_id => @query.project_id}
396 398 else
397 399 if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
398 400 # Give it a name, required to be valid
399 401 @query = Query.new(:name => "_")
400 402 @query.project = @project
401 403 if params[:fields] and params[:fields].is_a? Array
402 404 params[:fields].each do |field|
403 405 @query.add_filter(field, params[:operators][field], params[:values][field])
404 406 end
405 407 else
406 408 @query.available_filters.keys.each do |field|
407 409 @query.add_short_filter(field, params[field]) if params[field]
408 410 end
409 411 end
410 412 session[:query] = {:project_id => @query.project_id, :filters => @query.filters}
411 413 else
412 414 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
413 415 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters])
414 416 end
415 417 end
416 418 end
417 419 end
@@ -1,496 +1,506
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 require 'issues_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class IssuesController; def rescue_action(e) raise e end; end
23 23
24 24 class IssuesControllerTest < Test::Unit::TestCase
25 25 fixtures :projects,
26 26 :users,
27 27 :roles,
28 28 :members,
29 29 :issues,
30 30 :issue_statuses,
31 31 :trackers,
32 32 :projects_trackers,
33 33 :issue_categories,
34 34 :enabled_modules,
35 35 :enumerations,
36 36 :attachments,
37 37 :workflows,
38 38 :custom_fields,
39 39 :custom_values,
40 40 :custom_fields_trackers,
41 41 :time_entries
42 42
43 43 def setup
44 44 @controller = IssuesController.new
45 45 @request = ActionController::TestRequest.new
46 46 @response = ActionController::TestResponse.new
47 47 User.current = nil
48 48 end
49 49
50 50 def test_index
51 51 get :index
52 52 assert_response :success
53 53 assert_template 'index.rhtml'
54 54 assert_not_nil assigns(:issues)
55 55 assert_nil assigns(:project)
56 56 end
57 57
58 58 def test_index_with_project
59 59 get :index, :project_id => 1
60 60 assert_response :success
61 61 assert_template 'index.rhtml'
62 62 assert_not_nil assigns(:issues)
63 63 end
64 64
65 65 def test_index_with_project_and_filter
66 66 get :index, :project_id => 1, :set_filter => 1
67 67 assert_response :success
68 68 assert_template 'index.rhtml'
69 69 assert_not_nil assigns(:issues)
70 70 end
71 71
72 72 def test_index_csv_with_project
73 73 get :index, :format => 'csv'
74 74 assert_response :success
75 75 assert_not_nil assigns(:issues)
76 76 assert_equal 'text/csv', @response.content_type
77 77
78 78 get :index, :project_id => 1, :format => 'csv'
79 79 assert_response :success
80 80 assert_not_nil assigns(:issues)
81 81 assert_equal 'text/csv', @response.content_type
82 82 end
83 83
84 84 def test_index_pdf
85 85 get :index, :format => 'pdf'
86 86 assert_response :success
87 87 assert_not_nil assigns(:issues)
88 88 assert_equal 'application/pdf', @response.content_type
89 89
90 90 get :index, :project_id => 1, :format => 'pdf'
91 91 assert_response :success
92 92 assert_not_nil assigns(:issues)
93 93 assert_equal 'application/pdf', @response.content_type
94 94 end
95 95
96 96 def test_changes
97 97 get :changes, :project_id => 1
98 98 assert_response :success
99 99 assert_not_nil assigns(:journals)
100 100 assert_equal 'application/atom+xml', @response.content_type
101 101 end
102 102
103 103 def test_show_by_anonymous
104 104 get :show, :id => 1
105 105 assert_response :success
106 106 assert_template 'show.rhtml'
107 107 assert_not_nil assigns(:issue)
108 108 assert_equal Issue.find(1), assigns(:issue)
109 109
110 110 # anonymous role is allowed to add a note
111 111 assert_tag :tag => 'form',
112 112 :descendant => { :tag => 'fieldset',
113 113 :child => { :tag => 'legend',
114 114 :content => /Notes/ } }
115 115 end
116 116
117 117 def test_show_by_manager
118 118 @request.session[:user_id] = 2
119 119 get :show, :id => 1
120 120 assert_response :success
121 121
122 122 assert_tag :tag => 'form',
123 123 :descendant => { :tag => 'fieldset',
124 124 :child => { :tag => 'legend',
125 125 :content => /Change properties/ } },
126 126 :descendant => { :tag => 'fieldset',
127 127 :child => { :tag => 'legend',
128 128 :content => /Log time/ } },
129 129 :descendant => { :tag => 'fieldset',
130 130 :child => { :tag => 'legend',
131 131 :content => /Notes/ } }
132 132 end
133 133
134 134 def test_get_new
135 135 @request.session[:user_id] = 2
136 136 get :new, :project_id => 1, :tracker_id => 1
137 137 assert_response :success
138 138 assert_template 'new'
139 139
140 140 assert_tag :tag => 'input', :attributes => { :name => 'custom_fields[2]',
141 141 :value => 'Default string' }
142 142 end
143 143
144 144 def test_get_new_without_tracker_id
145 145 @request.session[:user_id] = 2
146 146 get :new, :project_id => 1
147 147 assert_response :success
148 148 assert_template 'new'
149 149
150 150 issue = assigns(:issue)
151 151 assert_not_nil issue
152 152 assert_equal Project.find(1).trackers.first, issue.tracker
153 153 end
154 154
155 155 def test_update_new_form
156 156 @request.session[:user_id] = 2
157 157 xhr :post, :new, :project_id => 1,
158 158 :issue => {:tracker_id => 2,
159 159 :subject => 'This is the test_new issue',
160 160 :description => 'This is the description',
161 161 :priority_id => 5}
162 162 assert_response :success
163 163 assert_template 'new'
164 164 end
165 165
166 166 def test_post_new
167 167 @request.session[:user_id] = 2
168 168 post :new, :project_id => 1,
169 169 :issue => {:tracker_id => 1,
170 170 :subject => 'This is the test_new issue',
171 171 :description => 'This is the description',
172 172 :priority_id => 5},
173 173 :custom_fields => {'2' => 'Value for field 2'}
174 174 assert_redirected_to 'issues/show'
175 175
176 176 issue = Issue.find_by_subject('This is the test_new issue')
177 177 assert_not_nil issue
178 178 assert_equal 2, issue.author_id
179 179 v = issue.custom_values.find_by_custom_field_id(2)
180 180 assert_not_nil v
181 181 assert_equal 'Value for field 2', v.value
182 182 end
183 183
184 def test_post_new_without_custom_fields_param
185 @request.session[:user_id] = 2
186 post :new, :project_id => 1,
187 :issue => {:tracker_id => 1,
188 :subject => 'This is the test_new issue',
189 :description => 'This is the description',
190 :priority_id => 5}
191 assert_redirected_to 'issues/show'
192 end
193
184 194 def test_copy_issue
185 195 @request.session[:user_id] = 2
186 196 get :new, :project_id => 1, :copy_from => 1
187 197 assert_template 'new'
188 198 assert_not_nil assigns(:issue)
189 199 orig = Issue.find(1)
190 200 assert_equal orig.subject, assigns(:issue).subject
191 201 end
192 202
193 203 def test_get_edit
194 204 @request.session[:user_id] = 2
195 205 get :edit, :id => 1
196 206 assert_response :success
197 207 assert_template 'edit'
198 208 assert_not_nil assigns(:issue)
199 209 assert_equal Issue.find(1), assigns(:issue)
200 210 end
201 211
202 212 def test_get_edit_with_params
203 213 @request.session[:user_id] = 2
204 214 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
205 215 assert_response :success
206 216 assert_template 'edit'
207 217
208 218 issue = assigns(:issue)
209 219 assert_not_nil issue
210 220
211 221 assert_equal 5, issue.status_id
212 222 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
213 223 :child => { :tag => 'option',
214 224 :content => 'Closed',
215 225 :attributes => { :selected => 'selected' } }
216 226
217 227 assert_equal 7, issue.priority_id
218 228 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
219 229 :child => { :tag => 'option',
220 230 :content => 'Urgent',
221 231 :attributes => { :selected => 'selected' } }
222 232 end
223 233
224 234 def test_post_edit
225 235 @request.session[:user_id] = 2
226 236 ActionMailer::Base.deliveries.clear
227 237
228 238 issue = Issue.find(1)
229 239 old_subject = issue.subject
230 240 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
231 241
232 242 post :edit, :id => 1, :issue => {:subject => new_subject}
233 243 assert_redirected_to 'issues/show/1'
234 244 issue.reload
235 245 assert_equal new_subject, issue.subject
236 246
237 247 mail = ActionMailer::Base.deliveries.last
238 248 assert_kind_of TMail::Mail, mail
239 249 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
240 250 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
241 251 end
242 252
243 253 def test_post_edit_with_status_and_assignee_change
244 254 issue = Issue.find(1)
245 255 assert_equal 1, issue.status_id
246 256 @request.session[:user_id] = 2
247 257 post :edit,
248 258 :id => 1,
249 259 :issue => { :status_id => 2, :assigned_to_id => 3 },
250 260 :notes => 'Assigned to dlopper'
251 261 assert_redirected_to 'issues/show/1'
252 262 issue.reload
253 263 assert_equal 2, issue.status_id
254 264 j = issue.journals.find(:first, :order => 'id DESC')
255 265 assert_equal 'Assigned to dlopper', j.notes
256 266 assert_equal 2, j.details.size
257 267
258 268 mail = ActionMailer::Base.deliveries.last
259 269 assert mail.body.include?("Status changed from New to Assigned")
260 270 end
261 271
262 272 def test_post_edit_with_note_only
263 273 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
264 274 # anonymous user
265 275 post :edit,
266 276 :id => 1,
267 277 :notes => notes
268 278 assert_redirected_to 'issues/show/1'
269 279 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
270 280 assert_equal notes, j.notes
271 281 assert_equal 0, j.details.size
272 282 assert_equal User.anonymous, j.user
273 283
274 284 mail = ActionMailer::Base.deliveries.last
275 285 assert mail.body.include?(notes)
276 286 end
277 287
278 288 def test_post_edit_with_note_and_spent_time
279 289 @request.session[:user_id] = 2
280 290 spent_hours_before = Issue.find(1).spent_hours
281 291 post :edit,
282 292 :id => 1,
283 293 :notes => '2.5 hours added',
284 294 :time_entry => { :hours => '2.5', :comments => '', :activity_id => Enumeration.get_values('ACTI').first }
285 295 assert_redirected_to 'issues/show/1'
286 296
287 297 issue = Issue.find(1)
288 298
289 299 j = issue.journals.find(:first, :order => 'id DESC')
290 300 assert_equal '2.5 hours added', j.notes
291 301 assert_equal 0, j.details.size
292 302
293 303 t = issue.time_entries.find(:first, :order => 'id DESC')
294 304 assert_not_nil t
295 305 assert_equal 2.5, t.hours
296 306 assert_equal spent_hours_before + 2.5, issue.spent_hours
297 307 end
298 308
299 309 def test_post_edit_with_attachment_only
300 310 # anonymous user
301 311 post :edit,
302 312 :id => 1,
303 313 :notes => '',
304 314 :attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
305 315 assert_redirected_to 'issues/show/1'
306 316 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
307 317 assert j.notes.blank?
308 318 assert_equal 1, j.details.size
309 319 assert_equal 'testfile.txt', j.details.first.value
310 320 assert_equal User.anonymous, j.user
311 321
312 322 mail = ActionMailer::Base.deliveries.last
313 323 assert mail.body.include?('testfile.txt')
314 324 end
315 325
316 326 def test_post_edit_with_no_change
317 327 issue = Issue.find(1)
318 328 issue.journals.clear
319 329 ActionMailer::Base.deliveries.clear
320 330
321 331 post :edit,
322 332 :id => 1,
323 333 :notes => ''
324 334 assert_redirected_to 'issues/show/1'
325 335
326 336 issue.reload
327 337 assert issue.journals.empty?
328 338 # No email should be sent
329 339 assert ActionMailer::Base.deliveries.empty?
330 340 end
331 341
332 342 def test_bulk_edit
333 343 @request.session[:user_id] = 2
334 344 # update issues priority
335 345 post :bulk_edit, :ids => [1, 2], :priority_id => 7, :notes => 'Bulk editing', :assigned_to_id => ''
336 346 assert_response 302
337 347 # check that the issues were updated
338 348 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
339 349 assert_equal 'Bulk editing', Issue.find(1).journals.find(:first, :order => 'created_on DESC').notes
340 350 end
341 351
342 352 def test_bulk_unassign
343 353 assert_not_nil Issue.find(2).assigned_to
344 354 @request.session[:user_id] = 2
345 355 # unassign issues
346 356 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :assigned_to_id => 'none'
347 357 assert_response 302
348 358 # check that the issues were updated
349 359 assert_nil Issue.find(2).assigned_to
350 360 end
351 361
352 362 def test_move_one_issue_to_another_project
353 363 @request.session[:user_id] = 1
354 364 post :move, :id => 1, :new_project_id => 2
355 365 assert_redirected_to 'projects/ecookbook/issues'
356 366 assert_equal 2, Issue.find(1).project_id
357 367 end
358 368
359 369 def test_bulk_move_to_another_project
360 370 @request.session[:user_id] = 1
361 371 post :move, :ids => [1, 2], :new_project_id => 2
362 372 assert_redirected_to 'projects/ecookbook/issues'
363 373 # Issues moved to project 2
364 374 assert_equal 2, Issue.find(1).project_id
365 375 assert_equal 2, Issue.find(2).project_id
366 376 # No tracker change
367 377 assert_equal 1, Issue.find(1).tracker_id
368 378 assert_equal 2, Issue.find(2).tracker_id
369 379 end
370 380
371 381 def test_bulk_move_to_another_tracker
372 382 @request.session[:user_id] = 1
373 383 post :move, :ids => [1, 2], :new_tracker_id => 2
374 384 assert_redirected_to 'projects/ecookbook/issues'
375 385 assert_equal 2, Issue.find(1).tracker_id
376 386 assert_equal 2, Issue.find(2).tracker_id
377 387 end
378 388
379 389 def test_context_menu_one_issue
380 390 @request.session[:user_id] = 2
381 391 get :context_menu, :ids => [1]
382 392 assert_response :success
383 393 assert_template 'context_menu'
384 394 assert_tag :tag => 'a', :content => 'Edit',
385 395 :attributes => { :href => '/issues/edit/1',
386 396 :class => 'icon-edit' }
387 397 assert_tag :tag => 'a', :content => 'Closed',
388 398 :attributes => { :href => '/issues/edit/1?issue%5Bstatus_id%5D=5',
389 399 :class => '' }
390 400 assert_tag :tag => 'a', :content => 'Immediate',
391 401 :attributes => { :href => '/issues/edit/1?issue%5Bpriority_id%5D=8',
392 402 :class => '' }
393 403 assert_tag :tag => 'a', :content => 'Dave Lopper',
394 404 :attributes => { :href => '/issues/edit/1?issue%5Bassigned_to_id%5D=3',
395 405 :class => '' }
396 406 assert_tag :tag => 'a', :content => 'Copy',
397 407 :attributes => { :href => '/projects/ecookbook/issues/new?copy_from=1',
398 408 :class => 'icon-copy' }
399 409 assert_tag :tag => 'a', :content => 'Move',
400 410 :attributes => { :href => '/issues/move?ids%5B%5D=1',
401 411 :class => 'icon-move' }
402 412 assert_tag :tag => 'a', :content => 'Delete',
403 413 :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
404 414 :class => 'icon-del' }
405 415 end
406 416
407 417 def test_context_menu_one_issue_by_anonymous
408 418 get :context_menu, :ids => [1]
409 419 assert_response :success
410 420 assert_template 'context_menu'
411 421 assert_tag :tag => 'a', :content => 'Delete',
412 422 :attributes => { :href => '#',
413 423 :class => 'icon-del disabled' }
414 424 end
415 425
416 426 def test_context_menu_multiple_issues_of_same_project
417 427 @request.session[:user_id] = 2
418 428 get :context_menu, :ids => [1, 2]
419 429 assert_response :success
420 430 assert_template 'context_menu'
421 431 assert_tag :tag => 'a', :content => 'Edit',
422 432 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
423 433 :class => 'icon-edit' }
424 434 assert_tag :tag => 'a', :content => 'Move',
425 435 :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
426 436 :class => 'icon-move' }
427 437 assert_tag :tag => 'a', :content => 'Delete',
428 438 :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
429 439 :class => 'icon-del' }
430 440 end
431 441
432 442 def test_context_menu_multiple_issues_of_different_project
433 443 @request.session[:user_id] = 2
434 444 get :context_menu, :ids => [1, 2, 4]
435 445 assert_response :success
436 446 assert_template 'context_menu'
437 447 assert_tag :tag => 'a', :content => 'Delete',
438 448 :attributes => { :href => '#',
439 449 :class => 'icon-del disabled' }
440 450 end
441 451
442 452 def test_destroy_issue_with_no_time_entries
443 453 @request.session[:user_id] = 2
444 454 post :destroy, :id => 3
445 455 assert_redirected_to 'projects/ecookbook/issues'
446 456 assert_nil Issue.find_by_id(3)
447 457 end
448 458
449 459 def test_destroy_issues_with_time_entries
450 460 @request.session[:user_id] = 2
451 461 post :destroy, :ids => [1, 3]
452 462 assert_response :success
453 463 assert_template 'destroy'
454 464 assert_not_nil assigns(:hours)
455 465 assert Issue.find_by_id(1) && Issue.find_by_id(3)
456 466 end
457 467
458 468 def test_destroy_issues_and_destroy_time_entries
459 469 @request.session[:user_id] = 2
460 470 post :destroy, :ids => [1, 3], :todo => 'destroy'
461 471 assert_redirected_to 'projects/ecookbook/issues'
462 472 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
463 473 assert_nil TimeEntry.find_by_id([1, 2])
464 474 end
465 475
466 476 def test_destroy_issues_and_assign_time_entries_to_project
467 477 @request.session[:user_id] = 2
468 478 post :destroy, :ids => [1, 3], :todo => 'nullify'
469 479 assert_redirected_to 'projects/ecookbook/issues'
470 480 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
471 481 assert_nil TimeEntry.find(1).issue_id
472 482 assert_nil TimeEntry.find(2).issue_id
473 483 end
474 484
475 485 def test_destroy_issues_and_reassign_time_entries_to_another_issue
476 486 @request.session[:user_id] = 2
477 487 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
478 488 assert_redirected_to 'projects/ecookbook/issues'
479 489 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
480 490 assert_equal 2, TimeEntry.find(1).issue_id
481 491 assert_equal 2, TimeEntry.find(2).issue_id
482 492 end
483 493
484 494 def test_destroy_attachment
485 495 issue = Issue.find(3)
486 496 a = issue.attachments.size
487 497 @request.session[:user_id] = 2
488 498 post :destroy_attachment, :id => 3, :attachment_id => 1
489 499 assert_redirected_to 'issues/show/3'
490 500 assert_nil Attachment.find_by_id(1)
491 501 issue.reload
492 502 assert_equal((a-1), issue.attachments.size)
493 503 j = issue.journals.find(:first, :order => 'created_on DESC')
494 504 assert_equal 'attachment', j.details.first.property
495 505 end
496 506 end
General Comments 0
You need to be logged in to leave comments. Login now