##// END OF EJS Templates
Previous/next links may be lost after editing the issue (#14462)....
Jean-Philippe Lang -
r14871:17ea1539ef03
parent child
Show More
@@ -1,523 +1,539
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 24 before_filter :authorize, :except => [:index, :new, :create]
25 25 before_filter :find_optional_project, :only => [:index, :new, :create]
26 26 before_filter :build_new_issue_from_params, :only => [:new, :create]
27 27 accept_rss_auth :index, :show
28 28 accept_api_auth :index, :show, :create, :update, :destroy
29 29
30 30 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
31 31
32 32 helper :journals
33 33 helper :projects
34 34 helper :custom_fields
35 35 helper :issue_relations
36 36 helper :watchers
37 37 helper :attachments
38 38 helper :queries
39 39 include QueriesHelper
40 40 helper :repositories
41 41 helper :sort
42 42 include SortHelper
43 43 helper :timelog
44 44
45 45 def index
46 46 retrieve_query
47 47 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
48 48 sort_update(@query.sortable_columns)
49 49 @query.sort_criteria = sort_criteria.to_a
50 50
51 51 if @query.valid?
52 52 case params[:format]
53 53 when 'csv', 'pdf'
54 54 @limit = Setting.issues_export_limit.to_i
55 55 if params[:columns] == 'all'
56 56 @query.column_names = @query.available_inline_columns.map(&:name)
57 57 end
58 58 when 'atom'
59 59 @limit = Setting.feeds_limit.to_i
60 60 when 'xml', 'json'
61 61 @offset, @limit = api_offset_and_limit
62 62 @query.column_names = %w(author)
63 63 else
64 64 @limit = per_page_option
65 65 end
66 66
67 67 @issue_count = @query.issue_count
68 68 @issue_pages = Paginator.new @issue_count, @limit, params['page']
69 69 @offset ||= @issue_pages.offset
70 70 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
71 71 :order => sort_clause,
72 72 :offset => @offset,
73 73 :limit => @limit)
74 74 @issue_count_by_group = @query.issue_count_by_group
75 75
76 76 respond_to do |format|
77 77 format.html { render :template => 'issues/index', :layout => !request.xhr? }
78 78 format.api {
79 79 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
80 80 }
81 81 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
82 82 format.csv { send_data(query_to_csv(@issues, @query, params[:csv]), :type => 'text/csv; header=present', :filename => 'issues.csv') }
83 83 format.pdf { send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf' }
84 84 end
85 85 else
86 86 respond_to do |format|
87 87 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
88 88 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
89 89 format.api { render_validation_errors(@query) }
90 90 end
91 91 end
92 92 rescue ActiveRecord::RecordNotFound
93 93 render_404
94 94 end
95 95
96 96 def show
97 97 @journals = @issue.journals.includes(:user, :details).
98 98 references(:user, :details).
99 99 reorder(:created_on, :id).to_a
100 100 @journals.each_with_index {|j,i| j.indice = i+1}
101 101 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
102 102 Journal.preload_journals_details_custom_fields(@journals)
103 103 @journals.select! {|journal| journal.notes? || journal.visible_details.any?}
104 104 @journals.reverse! if User.current.wants_comments_in_reverse_order?
105 105
106 106 @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
107 107 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
108 108
109 109 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
110 110 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
111 111 @priorities = IssuePriority.active
112 112 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
113 113 @relation = IssueRelation.new
114 114
115 115 respond_to do |format|
116 116 format.html {
117 117 retrieve_previous_and_next_issue_ids
118 118 render :template => 'issues/show'
119 119 }
120 120 format.api
121 121 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
122 122 format.pdf {
123 123 send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf"
124 124 }
125 125 end
126 126 end
127 127
128 128 def new
129 129 respond_to do |format|
130 130 format.html { render :action => 'new', :layout => !request.xhr? }
131 131 format.js
132 132 end
133 133 end
134 134
135 135 def create
136 136 unless User.current.allowed_to?(:add_issues, @issue.project, :global => true)
137 137 raise ::Unauthorized
138 138 end
139 139 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
140 140 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
141 141 if @issue.save
142 142 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
143 143 respond_to do |format|
144 144 format.html {
145 145 render_attachment_warning_if_needed(@issue)
146 146 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
147 147 redirect_after_create
148 148 }
149 149 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
150 150 end
151 151 return
152 152 else
153 153 respond_to do |format|
154 154 format.html {
155 155 if @issue.project.nil?
156 156 render_error :status => 422
157 157 else
158 158 render :action => 'new'
159 159 end
160 160 }
161 161 format.api { render_validation_errors(@issue) }
162 162 end
163 163 end
164 164 end
165 165
166 166 def edit
167 167 return unless update_issue_from_params
168 168
169 169 respond_to do |format|
170 170 format.html { }
171 171 format.js
172 172 end
173 173 end
174 174
175 175 def update
176 176 return unless update_issue_from_params
177 177 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
178 178 saved = false
179 179 begin
180 180 saved = save_issue_with_child_records
181 181 rescue ActiveRecord::StaleObjectError
182 182 @conflict = true
183 183 if params[:last_journal_id]
184 184 @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
185 185 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
186 186 end
187 187 end
188 188
189 189 if saved
190 190 render_attachment_warning_if_needed(@issue)
191 191 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
192 192
193 193 respond_to do |format|
194 format.html { redirect_back_or_default issue_path(@issue) }
194 format.html { redirect_back_or_default issue_path(@issue, previous_and_next_issue_ids_params) }
195 195 format.api { render_api_ok }
196 196 end
197 197 else
198 198 respond_to do |format|
199 199 format.html { render :action => 'edit' }
200 200 format.api { render_validation_errors(@issue) }
201 201 end
202 202 end
203 203 end
204 204
205 205 # Bulk edit/copy a set of issues
206 206 def bulk_edit
207 207 @issues.sort!
208 208 @copy = params[:copy].present?
209 209 @notes = params[:notes]
210 210
211 211 if @copy
212 212 unless User.current.allowed_to?(:copy_issues, @projects)
213 213 raise ::Unauthorized
214 214 end
215 215 end
216 216
217 217 @allowed_projects = Issue.allowed_target_projects
218 218 if params[:issue]
219 219 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
220 220 if @target_project
221 221 target_projects = [@target_project]
222 222 end
223 223 end
224 224 target_projects ||= @projects
225 225
226 226 if @copy
227 227 # Copied issues will get their default statuses
228 228 @available_statuses = []
229 229 else
230 230 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
231 231 end
232 232 @custom_fields = @issues.map{|i|i.editable_custom_fields}.reduce(:&)
233 233 @assignables = target_projects.map(&:assignable_users).reduce(:&)
234 234 @trackers = target_projects.map(&:trackers).reduce(:&)
235 235 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
236 236 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
237 237 if @copy
238 238 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
239 239 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
240 240 end
241 241
242 242 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
243 243
244 244 @issue_params = params[:issue] || {}
245 245 @issue_params[:custom_field_values] ||= {}
246 246 end
247 247
248 248 def bulk_update
249 249 @issues.sort!
250 250 @copy = params[:copy].present?
251 251
252 252 attributes = parse_params_for_bulk_issue_attributes(params)
253 253 copy_subtasks = (params[:copy_subtasks] == '1')
254 254 copy_attachments = (params[:copy_attachments] == '1')
255 255
256 256 if @copy
257 257 unless User.current.allowed_to?(:copy_issues, @projects)
258 258 raise ::Unauthorized
259 259 end
260 260 target_projects = @projects
261 261 if attributes['project_id'].present?
262 262 target_projects = Project.where(:id => attributes['project_id']).to_a
263 263 end
264 264 unless User.current.allowed_to?(:add_issues, target_projects)
265 265 raise ::Unauthorized
266 266 end
267 267 end
268 268
269 269 unsaved_issues = []
270 270 saved_issues = []
271 271
272 272 if @copy && copy_subtasks
273 273 # Descendant issues will be copied with the parent task
274 274 # Don't copy them twice
275 275 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
276 276 end
277 277
278 278 @issues.each do |orig_issue|
279 279 orig_issue.reload
280 280 if @copy
281 281 issue = orig_issue.copy({},
282 282 :attachments => copy_attachments,
283 283 :subtasks => copy_subtasks,
284 284 :link => link_copy?(params[:link_copy])
285 285 )
286 286 else
287 287 issue = orig_issue
288 288 end
289 289 journal = issue.init_journal(User.current, params[:notes])
290 290 issue.safe_attributes = attributes
291 291 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
292 292 if issue.save
293 293 saved_issues << issue
294 294 else
295 295 unsaved_issues << orig_issue
296 296 end
297 297 end
298 298
299 299 if unsaved_issues.empty?
300 300 flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
301 301 if params[:follow]
302 302 if @issues.size == 1 && saved_issues.size == 1
303 303 redirect_to issue_path(saved_issues.first)
304 304 elsif saved_issues.map(&:project).uniq.size == 1
305 305 redirect_to project_issues_path(saved_issues.map(&:project).first)
306 306 end
307 307 else
308 308 redirect_back_or_default _project_issues_path(@project)
309 309 end
310 310 else
311 311 @saved_issues = @issues
312 312 @unsaved_issues = unsaved_issues
313 313 @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
314 314 bulk_edit
315 315 render :action => 'bulk_edit'
316 316 end
317 317 end
318 318
319 319 def destroy
320 320 @hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f
321 321 if @hours > 0
322 322 case params[:todo]
323 323 when 'destroy'
324 324 # nothing to do
325 325 when 'nullify'
326 326 TimeEntry.where(['issue_id IN (?)', @issues]).update_all('issue_id = NULL')
327 327 when 'reassign'
328 328 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
329 329 if reassign_to.nil?
330 330 flash.now[:error] = l(:error_issue_not_found_in_project)
331 331 return
332 332 else
333 333 TimeEntry.where(['issue_id IN (?)', @issues]).
334 334 update_all("issue_id = #{reassign_to.id}")
335 335 end
336 336 else
337 337 # display the destroy form if it's a user request
338 338 return unless api_request?
339 339 end
340 340 end
341 341 @issues.each do |issue|
342 342 begin
343 343 issue.reload.destroy
344 344 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
345 345 # nothing to do, issue was already deleted (eg. by a parent)
346 346 end
347 347 end
348 348 respond_to do |format|
349 349 format.html { redirect_back_or_default _project_issues_path(@project) }
350 350 format.api { render_api_ok }
351 351 end
352 352 end
353 353
354 354 private
355 355
356 356 def retrieve_previous_and_next_issue_ids
357 retrieve_query_from_session
358 if @query
359 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
360 sort_update(@query.sortable_columns, 'issues_index_sort')
361 limit = 500
362 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
363 if (idx = issue_ids.index(@issue.id)) && idx < limit
364 if issue_ids.size < 500
365 @issue_position = idx + 1
366 @issue_count = issue_ids.size
357 if params[:prev_issue_id].present? || params[:next_issue_id].present?
358 @prev_issue_id = params[:prev_issue_id].presence.try(:to_i)
359 @next_issue_id = params[:next_issue_id].presence.try(:to_i)
360 @issue_position = params[:issue_position].presence.try(:to_i)
361 @issue_count = params[:issue_count].presence.try(:to_i)
362 else
363 retrieve_query_from_session
364 if @query
365 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
366 sort_update(@query.sortable_columns, 'issues_index_sort')
367 limit = 500
368 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
369 if (idx = issue_ids.index(@issue.id)) && idx < limit
370 if issue_ids.size < 500
371 @issue_position = idx + 1
372 @issue_count = issue_ids.size
373 end
374 @prev_issue_id = issue_ids[idx - 1] if idx > 0
375 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
367 376 end
368 @prev_issue_id = issue_ids[idx - 1] if idx > 0
369 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
370 377 end
371 378 end
372 379 end
373 380
381 def previous_and_next_issue_ids_params
382 {
383 :prev_issue_id => params[:prev_issue_id],
384 :next_issue_id => params[:next_issue_id],
385 :issue_position => params[:issue_position],
386 :issue_count => params[:issue_count]
387 }.reject {|k,v| k.blank?}
388 end
389
374 390 # Used by #edit and #update to set some common instance variables
375 391 # from the params
376 392 def update_issue_from_params
377 393 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
378 394 if params[:time_entry]
379 395 @time_entry.safe_attributes = params[:time_entry]
380 396 end
381 397
382 398 @issue.init_journal(User.current)
383 399
384 400 issue_attributes = params[:issue]
385 401 if issue_attributes && params[:conflict_resolution]
386 402 case params[:conflict_resolution]
387 403 when 'overwrite'
388 404 issue_attributes = issue_attributes.dup
389 405 issue_attributes.delete(:lock_version)
390 406 when 'add_notes'
391 407 issue_attributes = issue_attributes.slice(:notes, :private_notes)
392 408 when 'cancel'
393 409 redirect_to issue_path(@issue)
394 410 return false
395 411 end
396 412 end
397 413 @issue.safe_attributes = issue_attributes
398 414 @priorities = IssuePriority.active
399 415 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
400 416 true
401 417 end
402 418
403 419 # Used by #new and #create to build a new issue from the params
404 420 # The new issue will be copied from an existing one if copy_from parameter is given
405 421 def build_new_issue_from_params
406 422 @issue = Issue.new
407 423 if params[:copy_from]
408 424 begin
409 425 @issue.init_journal(User.current)
410 426 @copy_from = Issue.visible.find(params[:copy_from])
411 427 unless User.current.allowed_to?(:copy_issues, @copy_from.project)
412 428 raise ::Unauthorized
413 429 end
414 430 @link_copy = link_copy?(params[:link_copy]) || request.get?
415 431 @copy_attachments = params[:copy_attachments].present? || request.get?
416 432 @copy_subtasks = params[:copy_subtasks].present? || request.get?
417 433 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks, :link => @link_copy)
418 434 @issue.parent_issue_id = @copy_from.parent_id
419 435 rescue ActiveRecord::RecordNotFound
420 436 render_404
421 437 return
422 438 end
423 439 end
424 440 @issue.project = @project
425 441 if request.get?
426 442 @issue.project ||= @issue.allowed_target_projects.first
427 443 end
428 444 @issue.author ||= User.current
429 445 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
430 446
431 447 attrs = (params[:issue] || {}).deep_dup
432 448 if action_name == 'new' && params[:was_default_status] == attrs[:status_id]
433 449 attrs.delete(:status_id)
434 450 end
435 451 if action_name == 'new' && params[:form_update_triggered_by] == 'issue_project_id'
436 452 # Discard submitted version when changing the project on the issue form
437 453 # so we can use the default version for the new project
438 454 attrs.delete(:fixed_version_id)
439 455 end
440 456 @issue.safe_attributes = attrs
441 457
442 458 if @issue.project
443 459 @issue.tracker ||= @issue.project.trackers.first
444 460 if @issue.tracker.nil?
445 461 render_error l(:error_no_tracker_in_project)
446 462 return false
447 463 end
448 464 if @issue.status.nil?
449 465 render_error l(:error_no_default_issue_status)
450 466 return false
451 467 end
452 468 end
453 469
454 470 @priorities = IssuePriority.active
455 471 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
456 472 end
457 473
458 474 def parse_params_for_bulk_issue_attributes(params)
459 475 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
460 476 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
461 477 if custom = attributes[:custom_field_values]
462 478 custom.reject! {|k,v| v.blank?}
463 479 custom.keys.each do |k|
464 480 if custom[k].is_a?(Array)
465 481 custom[k] << '' if custom[k].delete('__none__')
466 482 else
467 483 custom[k] = '' if custom[k] == '__none__'
468 484 end
469 485 end
470 486 end
471 487 attributes
472 488 end
473 489
474 490 # Saves @issue and a time_entry from the parameters
475 491 def save_issue_with_child_records
476 492 Issue.transaction do
477 493 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
478 494 time_entry = @time_entry || TimeEntry.new
479 495 time_entry.project = @issue.project
480 496 time_entry.issue = @issue
481 497 time_entry.user = User.current
482 498 time_entry.spent_on = User.current.today
483 499 time_entry.attributes = params[:time_entry]
484 500 @issue.time_entries << time_entry
485 501 end
486 502
487 503 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
488 504 if @issue.save
489 505 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
490 506 else
491 507 raise ActiveRecord::Rollback
492 508 end
493 509 end
494 510 end
495 511
496 512 # Returns true if the issue copy should be linked
497 513 # to the original issue
498 514 def link_copy?(param)
499 515 case Setting.link_copied_issue
500 516 when 'yes'
501 517 true
502 518 when 'no'
503 519 false
504 520 when 'ask'
505 521 param == '1'
506 522 end
507 523 end
508 524
509 525 # Redirects user after a successful issue creation
510 526 def redirect_after_create
511 527 if params[:continue]
512 528 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
513 529 if params[:project_id]
514 530 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
515 531 else
516 532 attrs.merge! :project_id => @issue.project_id
517 533 redirect_to new_issue_path(:issue => attrs)
518 534 end
519 535 else
520 536 redirect_to issue_path(@issue)
521 537 end
522 538 end
523 539 end
@@ -1,54 +1,59
1 1 <%= labelled_form_for @issue, :html => {:id => 'issue-form', :multipart => true} do |f| %>
2 2 <%= error_messages_for 'issue', 'time_entry' %>
3 3 <%= render :partial => 'conflict' if @conflict %>
4 4 <div class="box">
5 5 <% if @issue.attributes_editable? %>
6 6 <fieldset class="tabular"><legend><%= l(:label_change_properties) %></legend>
7 7 <div id="all_attributes">
8 8 <%= render :partial => 'form', :locals => {:f => f} %>
9 9 </div>
10 10 </fieldset>
11 11 <% end %>
12 12 <% if User.current.allowed_to?(:log_time, @project) %>
13 13 <fieldset class="tabular"><legend><%= l(:button_log_time) %></legend>
14 14 <%= labelled_fields_for :time_entry, @time_entry do |time_entry| %>
15 15 <div class="splitcontent">
16 16 <div class="splitcontentleft">
17 17 <p><%= time_entry.text_field :hours, :size => 6, :label => :label_spent_time %> <%= l(:field_hours) %></p>
18 18 </div>
19 19 <div class="splitcontentright">
20 20 <p><%= time_entry.select :activity_id, activity_collection_for_select_options %></p>
21 21 </div>
22 22 </div>
23 23 <p><%= time_entry.text_field :comments, :size => 60 %></p>
24 24 <% @time_entry.custom_field_values.each do |value| %>
25 25 <p><%= custom_field_tag_with_label :time_entry, value %></p>
26 26 <% end %>
27 27 <% end %>
28 28 </fieldset>
29 29 <% end %>
30 30
31 31 <fieldset><legend><%= l(:field_notes) %></legend>
32 32 <%= f.text_area :notes, :cols => 60, :rows => 10, :class => 'wiki-edit', :no_label => true %>
33 33 <%= wikitoolbar_for 'issue_notes' %>
34 34
35 35 <% if @issue.safe_attribute? 'private_notes' %>
36 36 <%= f.check_box :private_notes, :no_label => true %> <label for="issue_private_notes"><%= l(:field_private_notes) %></label>
37 37 <% end %>
38 38
39 39 <%= call_hook(:view_issues_edit_notes_bottom, { :issue => @issue, :notes => @notes, :form => f }) %>
40 40 </fieldset>
41 41
42 42 <fieldset><legend><%= l(:label_attachment_plural) %></legend>
43 43 <p><%= render :partial => 'attachments/form', :locals => {:container => @issue} %></p>
44 44 </fieldset>
45 45 </div>
46 46
47 47 <%= f.hidden_field :lock_version %>
48 48 <%= hidden_field_tag 'last_journal_id', params[:last_journal_id] || @issue.last_journal_id %>
49 49 <%= submit_tag l(:button_submit) %>
50 50 <%= preview_link preview_edit_issue_path(:project_id => @project, :id => @issue), 'issue-form' %>
51 51 | <%= link_to l(:button_cancel), {}, :onclick => "$('#update').hide(); return false;" %>
52
53 <%= hidden_field_tag 'prev_issue_id', @prev_issue_id if @prev_issue_id %>
54 <%= hidden_field_tag 'next_issue_id', @next_issue_id if @next_issue_id %>
55 <%= hidden_field_tag 'issue_position', @issue_position if @issue_position %>
56 <%= hidden_field_tag 'issue_count', @issue_count if @issue_count %>
52 57 <% end %>
53 58
54 59 <div id="preview" class="wiki"></div>
@@ -1,4522 +1,4546
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssuesControllerTest < ActionController::TestCase
21 21 fixtures :projects,
22 22 :users, :email_addresses,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :issues,
27 27 :issue_statuses,
28 28 :issue_relations,
29 29 :versions,
30 30 :trackers,
31 31 :projects_trackers,
32 32 :issue_categories,
33 33 :enabled_modules,
34 34 :enumerations,
35 35 :attachments,
36 36 :workflows,
37 37 :custom_fields,
38 38 :custom_values,
39 39 :custom_fields_projects,
40 40 :custom_fields_trackers,
41 41 :time_entries,
42 42 :journals,
43 43 :journal_details,
44 44 :queries,
45 45 :repositories,
46 46 :changesets
47 47
48 48 include Redmine::I18n
49 49
50 50 def setup
51 51 User.current = nil
52 52 end
53 53
54 54 def test_index
55 55 with_settings :default_language => "en" do
56 56 get :index
57 57 assert_response :success
58 58 assert_template 'index'
59 59 assert_not_nil assigns(:issues)
60 60 assert_nil assigns(:project)
61 61
62 62 # links to visible issues
63 63 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
64 64 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
65 65 # private projects hidden
66 66 assert_select 'a[href="/issues/6"]', 0
67 67 assert_select 'a[href="/issues/4"]', 0
68 68 # project column
69 69 assert_select 'th', :text => /Project/
70 70 end
71 71 end
72 72
73 73 def test_index_should_not_list_issues_when_module_disabled
74 74 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
75 75 get :index
76 76 assert_response :success
77 77 assert_template 'index'
78 78 assert_not_nil assigns(:issues)
79 79 assert_nil assigns(:project)
80 80
81 81 assert_select 'a[href="/issues/1"]', 0
82 82 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
83 83 end
84 84
85 85 def test_index_should_list_visible_issues_only
86 86 get :index, :per_page => 100
87 87 assert_response :success
88 88 assert_not_nil assigns(:issues)
89 89 assert_nil assigns(:issues).detect {|issue| !issue.visible?}
90 90 end
91 91
92 92 def test_index_with_project
93 93 Setting.display_subprojects_issues = 0
94 94 get :index, :project_id => 1
95 95 assert_response :success
96 96 assert_template 'index'
97 97 assert_not_nil assigns(:issues)
98 98
99 99 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
100 100 assert_select 'a[href="/issues/5"]', 0
101 101 end
102 102
103 103 def test_index_with_project_and_subprojects
104 104 Setting.display_subprojects_issues = 1
105 105 get :index, :project_id => 1
106 106 assert_response :success
107 107 assert_template 'index'
108 108 assert_not_nil assigns(:issues)
109 109
110 110 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
111 111 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
112 112 assert_select 'a[href="/issues/6"]', 0
113 113 end
114 114
115 115 def test_index_with_project_and_subprojects_should_show_private_subprojects_with_permission
116 116 @request.session[:user_id] = 2
117 117 Setting.display_subprojects_issues = 1
118 118 get :index, :project_id => 1
119 119 assert_response :success
120 120 assert_template 'index'
121 121 assert_not_nil assigns(:issues)
122 122
123 123 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
124 124 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
125 125 assert_select 'a[href="/issues/6"]', :text => /Issue of a private subproject/
126 126 end
127 127
128 128 def test_index_with_project_and_default_filter
129 129 get :index, :project_id => 1, :set_filter => 1
130 130 assert_response :success
131 131 assert_template 'index'
132 132 assert_not_nil assigns(:issues)
133 133
134 134 query = assigns(:query)
135 135 assert_not_nil query
136 136 # default filter
137 137 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
138 138 end
139 139
140 140 def test_index_with_project_and_filter
141 141 get :index, :project_id => 1, :set_filter => 1,
142 142 :f => ['tracker_id'],
143 143 :op => {'tracker_id' => '='},
144 144 :v => {'tracker_id' => ['1']}
145 145 assert_response :success
146 146 assert_template 'index'
147 147 assert_not_nil assigns(:issues)
148 148
149 149 query = assigns(:query)
150 150 assert_not_nil query
151 151 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
152 152 end
153 153
154 154 def test_index_with_short_filters
155 155 to_test = {
156 156 'status_id' => {
157 157 'o' => { :op => 'o', :values => [''] },
158 158 'c' => { :op => 'c', :values => [''] },
159 159 '7' => { :op => '=', :values => ['7'] },
160 160 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
161 161 '=7' => { :op => '=', :values => ['7'] },
162 162 '!3' => { :op => '!', :values => ['3'] },
163 163 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
164 164 'subject' => {
165 165 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
166 166 'o' => { :op => '=', :values => ['o'] },
167 167 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
168 168 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
169 169 'tracker_id' => {
170 170 '3' => { :op => '=', :values => ['3'] },
171 171 '=3' => { :op => '=', :values => ['3'] }},
172 172 'start_date' => {
173 173 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
174 174 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
175 175 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
176 176 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
177 177 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
178 178 '<t+2' => { :op => '<t+', :values => ['2'] },
179 179 '>t+2' => { :op => '>t+', :values => ['2'] },
180 180 't+2' => { :op => 't+', :values => ['2'] },
181 181 't' => { :op => 't', :values => [''] },
182 182 'w' => { :op => 'w', :values => [''] },
183 183 '>t-2' => { :op => '>t-', :values => ['2'] },
184 184 '<t-2' => { :op => '<t-', :values => ['2'] },
185 185 't-2' => { :op => 't-', :values => ['2'] }},
186 186 'created_on' => {
187 187 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
188 188 '<t-2' => { :op => '<t-', :values => ['2'] },
189 189 '>t-2' => { :op => '>t-', :values => ['2'] },
190 190 't-2' => { :op => 't-', :values => ['2'] }},
191 191 'cf_1' => {
192 192 'c' => { :op => '=', :values => ['c'] },
193 193 '!c' => { :op => '!', :values => ['c'] },
194 194 '!*' => { :op => '!*', :values => [''] },
195 195 '*' => { :op => '*', :values => [''] }},
196 196 'estimated_hours' => {
197 197 '=13.4' => { :op => '=', :values => ['13.4'] },
198 198 '>=45' => { :op => '>=', :values => ['45'] },
199 199 '<=125' => { :op => '<=', :values => ['125'] },
200 200 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
201 201 '!*' => { :op => '!*', :values => [''] },
202 202 '*' => { :op => '*', :values => [''] }}
203 203 }
204 204
205 205 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
206 206
207 207 to_test.each do |field, expression_and_expected|
208 208 expression_and_expected.each do |filter_expression, expected|
209 209
210 210 get :index, :set_filter => 1, field => filter_expression
211 211
212 212 assert_response :success
213 213 assert_template 'index'
214 214 assert_not_nil assigns(:issues)
215 215
216 216 query = assigns(:query)
217 217 assert_not_nil query
218 218 assert query.has_filter?(field)
219 219 assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
220 220 end
221 221 end
222 222 end
223 223
224 224 def test_index_with_project_and_empty_filters
225 225 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
226 226 assert_response :success
227 227 assert_template 'index'
228 228 assert_not_nil assigns(:issues)
229 229
230 230 query = assigns(:query)
231 231 assert_not_nil query
232 232 # no filter
233 233 assert_equal({}, query.filters)
234 234 end
235 235
236 236 def test_index_with_project_custom_field_filter
237 237 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
238 238 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
239 239 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
240 240 filter_name = "project.cf_#{field.id}"
241 241 @request.session[:user_id] = 1
242 242
243 243 get :index, :set_filter => 1,
244 244 :f => [filter_name],
245 245 :op => {filter_name => '='},
246 246 :v => {filter_name => ['Foo']}
247 247 assert_response :success
248 248 assert_template 'index'
249 249 assert_equal [3, 5], assigns(:issues).map(&:project_id).uniq.sort
250 250 end
251 251
252 252 def test_index_with_query
253 253 get :index, :project_id => 1, :query_id => 5
254 254 assert_response :success
255 255 assert_template 'index'
256 256 assert_not_nil assigns(:issues)
257 257 assert_nil assigns(:issue_count_by_group)
258 258 end
259 259
260 260 def test_index_with_query_grouped_by_tracker
261 261 get :index, :project_id => 1, :query_id => 6
262 262 assert_response :success
263 263 assert_template 'index'
264 264 assert_not_nil assigns(:issues)
265 265 assert_not_nil assigns(:issue_count_by_group)
266 266 end
267 267
268 268 def test_index_with_query_grouped_and_sorted_by_category
269 269 get :index, :project_id => 1, :set_filter => 1, :group_by => "category", :sort => "category"
270 270 assert_response :success
271 271 assert_template 'index'
272 272 assert_not_nil assigns(:issues)
273 273 assert_not_nil assigns(:issue_count_by_group)
274 274 end
275 275
276 276 def test_index_with_query_grouped_by_list_custom_field
277 277 get :index, :project_id => 1, :query_id => 9
278 278 assert_response :success
279 279 assert_template 'index'
280 280 assert_not_nil assigns(:issues)
281 281 assert_not_nil assigns(:issue_count_by_group)
282 282 end
283 283
284 284 def test_index_with_query_grouped_by_key_value_custom_field
285 285 cf = IssueCustomField.create!(:name => 'Key', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'enumeration')
286 286 cf.enumerations << valueb = CustomFieldEnumeration.new(:name => 'Value B', :position => 1)
287 287 cf.enumerations << valuea = CustomFieldEnumeration.new(:name => 'Value A', :position => 2)
288 288 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => valueb.id)
289 289 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => valueb.id)
290 290 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => valuea.id)
291 291 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
292 292
293 293 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
294 294 assert_response :success
295 295 assert_template 'index'
296 296 assert_not_nil assigns(:issues)
297 297 assert_not_nil assigns(:issue_count_by_group)
298 298
299 299 assert_select 'tr.group', 3
300 300 assert_select 'tr.group' do
301 301 assert_select 'span.name', :text => 'Value B'
302 302 assert_select 'span.count', :text => '2'
303 303 end
304 304 assert_select 'tr.group' do
305 305 assert_select 'span.name', :text => 'Value A'
306 306 assert_select 'span.count', :text => '1'
307 307 end
308 308 end
309 309
310 310 def test_index_with_query_grouped_by_user_custom_field
311 311 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
312 312 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
313 313 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
314 314 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
315 315 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
316 316
317 317 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
318 318 assert_response :success
319 319
320 320 assert_select 'tr.group', 3
321 321 assert_select 'tr.group' do
322 322 assert_select 'a', :text => 'John Smith'
323 323 assert_select 'span.count', :text => '1'
324 324 end
325 325 assert_select 'tr.group' do
326 326 assert_select 'a', :text => 'Dave Lopper'
327 327 assert_select 'span.count', :text => '2'
328 328 end
329 329 end
330 330
331 331 def test_index_grouped_by_boolean_custom_field_should_distinguish_blank_and_false_values
332 332 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool')
333 333 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '1')
334 334 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
335 335 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '')
336 336
337 337 with_settings :default_language => 'en' do
338 338 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
339 339 assert_response :success
340 340 end
341 341
342 342 assert_select 'tr.group', 3
343 343 assert_select 'tr.group', :text => /Yes/
344 344 assert_select 'tr.group', :text => /No/
345 345 assert_select 'tr.group', :text => /blank/
346 346 end
347 347
348 348 def test_index_grouped_by_boolean_custom_field_with_false_group_in_first_position_should_show_the_group
349 349 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool', :is_filter => true)
350 350 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '0')
351 351 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
352 352
353 353 with_settings :default_language => 'en' do
354 354 get :index, :project_id => 1, :set_filter => 1, "cf_#{cf.id}" => "*", :group_by => "cf_#{cf.id}"
355 355 assert_response :success
356 356 assert_equal [1, 2], assigns(:issues).map(&:id).sort
357 357 end
358 358
359 359 assert_select 'tr.group', 1
360 360 assert_select 'tr.group', :text => /No/
361 361 end
362 362
363 363 def test_index_with_query_grouped_by_tracker_in_normal_order
364 364 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
365 365
366 366 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc'
367 367 assert_response :success
368 368
369 369 trackers = assigns(:issues).map(&:tracker).uniq
370 370 assert_equal [1, 2, 3], trackers.map(&:id)
371 371 end
372 372
373 373 def test_index_with_query_grouped_by_tracker_in_reverse_order
374 374 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
375 375
376 376 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc,tracker:desc'
377 377 assert_response :success
378 378
379 379 trackers = assigns(:issues).map(&:tracker).uniq
380 380 assert_equal [3, 2, 1], trackers.map(&:id)
381 381 end
382 382
383 383 def test_index_with_query_id_and_project_id_should_set_session_query
384 384 get :index, :project_id => 1, :query_id => 4
385 385 assert_response :success
386 386 assert_kind_of Hash, session[:query]
387 387 assert_equal 4, session[:query][:id]
388 388 assert_equal 1, session[:query][:project_id]
389 389 end
390 390
391 391 def test_index_with_invalid_query_id_should_respond_404
392 392 get :index, :project_id => 1, :query_id => 999
393 393 assert_response 404
394 394 end
395 395
396 396 def test_index_with_cross_project_query_in_session_should_show_project_issues
397 397 q = IssueQuery.create!(:name => "test", :user_id => 2, :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
398 398 @request.session[:query] = {:id => q.id, :project_id => 1}
399 399
400 400 with_settings :display_subprojects_issues => '0' do
401 401 get :index, :project_id => 1
402 402 end
403 403 assert_response :success
404 404 assert_not_nil assigns(:query)
405 405 assert_equal q.id, assigns(:query).id
406 406 assert_equal 1, assigns(:query).project_id
407 407 assert_equal [1], assigns(:issues).map(&:project_id).uniq
408 408 end
409 409
410 410 def test_private_query_should_not_be_available_to_other_users
411 411 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
412 412 @request.session[:user_id] = 3
413 413
414 414 get :index, :query_id => q.id
415 415 assert_response 403
416 416 end
417 417
418 418 def test_private_query_should_be_available_to_its_user
419 419 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
420 420 @request.session[:user_id] = 2
421 421
422 422 get :index, :query_id => q.id
423 423 assert_response :success
424 424 end
425 425
426 426 def test_public_query_should_be_available_to_other_users
427 427 q = IssueQuery.create!(:name => "public", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PUBLIC, :project => nil)
428 428 @request.session[:user_id] = 3
429 429
430 430 get :index, :query_id => q.id
431 431 assert_response :success
432 432 end
433 433
434 434 def test_index_should_omit_page_param_in_export_links
435 435 get :index, :page => 2
436 436 assert_response :success
437 437 assert_select 'a.atom[href="/issues.atom"]'
438 438 assert_select 'a.csv[href="/issues.csv"]'
439 439 assert_select 'a.pdf[href="/issues.pdf"]'
440 440 assert_select 'form#csv-export-form[action="/issues.csv"]'
441 441 end
442 442
443 443 def test_index_should_not_warn_when_not_exceeding_export_limit
444 444 with_settings :issues_export_limit => 200 do
445 445 get :index
446 446 assert_select '#csv-export-options p.icon-warning', 0
447 447 end
448 448 end
449 449
450 450 def test_index_should_warn_when_exceeding_export_limit
451 451 with_settings :issues_export_limit => 2 do
452 452 get :index
453 453 assert_select '#csv-export-options p.icon-warning', :text => %r{limit: 2}
454 454 end
455 455 end
456 456
457 457 def test_index_should_include_query_params_as_hidden_fields_in_csv_export_form
458 458 get :index, :project_id => 1, :set_filter => "1", :tracker_id => "2", :sort => 'status', :c => ["status", "priority"]
459 459
460 460 assert_select '#csv-export-form[action=?]', '/projects/ecookbook/issues.csv'
461 461 assert_select '#csv-export-form[method=?]', 'get'
462 462
463 463 assert_select '#csv-export-form' do
464 464 assert_select 'input[name=?][value=?]', 'set_filter', '1'
465 465
466 466 assert_select 'input[name=?][value=?]', 'f[]', 'tracker_id'
467 467 assert_select 'input[name=?][value=?]', 'op[tracker_id]', '='
468 468 assert_select 'input[name=?][value=?]', 'v[tracker_id][]', '2'
469 469
470 470 assert_select 'input[name=?][value=?]', 'c[]', 'status'
471 471 assert_select 'input[name=?][value=?]', 'c[]', 'priority'
472 472
473 473 assert_select 'input[name=?][value=?]', 'sort', 'status'
474 474 end
475 475 end
476 476
477 477 def test_index_csv
478 478 get :index, :format => 'csv'
479 479 assert_response :success
480 480 assert_not_nil assigns(:issues)
481 481 assert_equal 'text/csv; header=present', @response.content_type
482 482 assert @response.body.starts_with?("#,")
483 483 lines = @response.body.chomp.split("\n")
484 484 assert_equal assigns(:query).columns.size, lines[0].split(',').size
485 485 end
486 486
487 487 def test_index_csv_with_project
488 488 get :index, :project_id => 1, :format => 'csv'
489 489 assert_response :success
490 490 assert_not_nil assigns(:issues)
491 491 assert_equal 'text/csv; header=present', @response.content_type
492 492 end
493 493
494 494 def test_index_csv_with_description
495 495 Issue.generate!(:description => 'test_index_csv_with_description')
496 496
497 497 with_settings :default_language => 'en' do
498 498 get :index, :format => 'csv', :csv => {:description => '1'}
499 499 assert_response :success
500 500 assert_not_nil assigns(:issues)
501 501 end
502 502
503 503 assert_equal 'text/csv; header=present', response.content_type
504 504 headers = response.body.chomp.split("\n").first.split(',')
505 505 assert_include 'Description', headers
506 506 assert_include 'test_index_csv_with_description', response.body
507 507 end
508 508
509 509 def test_index_csv_with_spent_time_column
510 510 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column', :author_id => 2)
511 511 TimeEntry.create!(:project => issue.project, :issue => issue, :hours => 7.33, :user => User.find(2), :spent_on => Date.today)
512 512
513 513 get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
514 514 assert_response :success
515 515 assert_equal 'text/csv; header=present', @response.content_type
516 516 lines = @response.body.chomp.split("\n")
517 517 assert_include "#{issue.id},#{issue.subject},7.33", lines
518 518 end
519 519
520 520 def test_index_csv_with_all_columns
521 521 get :index, :format => 'csv', :csv => {:columns => 'all'}
522 522 assert_response :success
523 523 assert_not_nil assigns(:issues)
524 524 assert_equal 'text/csv; header=present', @response.content_type
525 525 assert_match /\A#,/, response.body
526 526 lines = response.body.chomp.split("\n")
527 527 assert_equal assigns(:query).available_inline_columns.size, lines[0].split(',').size
528 528 end
529 529
530 530 def test_index_csv_with_multi_column_field
531 531 CustomField.find(1).update_attribute :multiple, true
532 532 issue = Issue.find(1)
533 533 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
534 534 issue.save!
535 535
536 536 get :index, :format => 'csv', :csv => {:columns => 'all'}
537 537 assert_response :success
538 538 lines = @response.body.chomp.split("\n")
539 539 assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
540 540 end
541 541
542 542 def test_index_csv_should_format_float_custom_fields_with_csv_decimal_separator
543 543 field = IssueCustomField.create!(:name => 'Float', :is_for_all => true, :tracker_ids => [1], :field_format => 'float')
544 544 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id => '185.6'})
545 545
546 546 with_settings :default_language => 'fr' do
547 547 get :index, :format => 'csv', :csv => {:columns => 'all'}
548 548 assert_response :success
549 549 issue_line = response.body.chomp.split("\n").map {|line| line.split(';')}.detect {|line| line[0]==issue.id.to_s}
550 550 assert_include '185,60', issue_line
551 551 end
552 552
553 553 with_settings :default_language => 'en' do
554 554 get :index, :format => 'csv', :csv => {:columns => 'all'}
555 555 assert_response :success
556 556 issue_line = response.body.chomp.split("\n").map {|line| line.split(',')}.detect {|line| line[0]==issue.id.to_s}
557 557 assert_include '185.60', issue_line
558 558 end
559 559 end
560 560
561 561 def test_index_csv_should_fill_parent_column_with_parent_id
562 562 Issue.delete_all
563 563 parent = Issue.generate!
564 564 child = Issue.generate!(:parent_issue_id => parent.id)
565 565
566 566 with_settings :default_language => 'en' do
567 567 get :index, :format => 'csv', :c => %w(parent)
568 568 end
569 569 lines = response.body.split("\n")
570 570 assert_include "#{child.id},#{parent.id}", lines
571 571 end
572 572
573 573 def test_index_csv_big_5
574 574 with_settings :default_language => "zh-TW" do
575 575 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88".force_encoding('UTF-8')
576 576 str_big5 = "\xa4@\xa4\xeb".force_encoding('Big5')
577 577 issue = Issue.generate!(:subject => str_utf8)
578 578
579 579 get :index, :project_id => 1,
580 580 :f => ['subject'],
581 581 :op => '=', :values => [str_utf8],
582 582 :format => 'csv'
583 583 assert_equal 'text/csv; header=present', @response.content_type
584 584 lines = @response.body.chomp.split("\n")
585 585 header = lines[0]
586 586 status = "\xaa\xac\xbaA".force_encoding('Big5')
587 587 assert_include status, header
588 588 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
589 589 assert_include str_big5, issue_line
590 590 end
591 591 end
592 592
593 593 def test_index_csv_cannot_convert_should_be_replaced_big_5
594 594 with_settings :default_language => "zh-TW" do
595 595 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85".force_encoding('UTF-8')
596 596 issue = Issue.generate!(:subject => str_utf8)
597 597
598 598 get :index, :project_id => 1,
599 599 :f => ['subject'],
600 600 :op => '=', :values => [str_utf8],
601 601 :c => ['status', 'subject'],
602 602 :format => 'csv',
603 603 :set_filter => 1
604 604 assert_equal 'text/csv; header=present', @response.content_type
605 605 lines = @response.body.chomp.split("\n")
606 606 header = lines[0]
607 607 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
608 608 s1 = "\xaa\xac\xbaA".force_encoding('Big5') # status
609 609 assert header.include?(s1)
610 610 s2 = issue_line.split(",")[2]
611 611 s3 = "\xa5H?".force_encoding('Big5') # subject
612 612 assert_equal s3, s2
613 613 end
614 614 end
615 615
616 616 def test_index_csv_tw
617 617 with_settings :default_language => "zh-TW" do
618 618 str1 = "test_index_csv_tw"
619 619 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
620 620
621 621 get :index, :project_id => 1,
622 622 :f => ['subject'],
623 623 :op => '=', :values => [str1],
624 624 :c => ['estimated_hours', 'subject'],
625 625 :format => 'csv',
626 626 :set_filter => 1
627 627 assert_equal 'text/csv; header=present', @response.content_type
628 628 lines = @response.body.chomp.split("\n")
629 629 assert_include "#{issue.id},1234.50,#{str1}", lines
630 630 end
631 631 end
632 632
633 633 def test_index_csv_fr
634 634 with_settings :default_language => "fr" do
635 635 str1 = "test_index_csv_fr"
636 636 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
637 637
638 638 get :index, :project_id => 1,
639 639 :f => ['subject'],
640 640 :op => '=', :values => [str1],
641 641 :c => ['estimated_hours', 'subject'],
642 642 :format => 'csv',
643 643 :set_filter => 1
644 644 assert_equal 'text/csv; header=present', @response.content_type
645 645 lines = @response.body.chomp.split("\n")
646 646 assert_include "#{issue.id};1234,50;#{str1}", lines
647 647 end
648 648 end
649 649
650 650 def test_index_pdf
651 651 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
652 652 with_settings :default_language => lang do
653 653
654 654 get :index
655 655 assert_response :success
656 656 assert_template 'index'
657 657
658 658 get :index, :format => 'pdf'
659 659 assert_response :success
660 660 assert_not_nil assigns(:issues)
661 661 assert_equal 'application/pdf', @response.content_type
662 662
663 663 get :index, :project_id => 1, :format => 'pdf'
664 664 assert_response :success
665 665 assert_not_nil assigns(:issues)
666 666 assert_equal 'application/pdf', @response.content_type
667 667
668 668 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
669 669 assert_response :success
670 670 assert_not_nil assigns(:issues)
671 671 assert_equal 'application/pdf', @response.content_type
672 672 end
673 673 end
674 674 end
675 675
676 676 def test_index_pdf_with_query_grouped_by_list_custom_field
677 677 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
678 678 assert_response :success
679 679 assert_not_nil assigns(:issues)
680 680 assert_not_nil assigns(:issue_count_by_group)
681 681 assert_equal 'application/pdf', @response.content_type
682 682 end
683 683
684 684 def test_index_atom
685 685 get :index, :project_id => 'ecookbook', :format => 'atom'
686 686 assert_response :success
687 687 assert_template 'common/feed'
688 688 assert_equal 'application/atom+xml', response.content_type
689 689
690 690 assert_select 'feed' do
691 691 assert_select 'link[rel=self][href=?]', 'http://test.host/projects/ecookbook/issues.atom'
692 692 assert_select 'link[rel=alternate][href=?]', 'http://test.host/projects/ecookbook/issues'
693 693 assert_select 'entry link[href=?]', 'http://test.host/issues/1'
694 694 end
695 695 end
696 696
697 697 def test_index_sort
698 698 get :index, :sort => 'tracker,id:desc'
699 699 assert_response :success
700 700
701 701 sort_params = @request.session['issues_index_sort']
702 702 assert sort_params.is_a?(String)
703 703 assert_equal 'tracker,id:desc', sort_params
704 704
705 705 issues = assigns(:issues)
706 706 assert_not_nil issues
707 707 assert !issues.empty?
708 708 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
709 709 assert_select 'table.issues.sort-by-tracker.sort-asc'
710 710 end
711 711
712 712 def test_index_sort_by_field_not_included_in_columns
713 713 Setting.issue_list_default_columns = %w(subject author)
714 714 get :index, :sort => 'tracker'
715 715 end
716 716
717 717 def test_index_sort_by_assigned_to
718 718 get :index, :sort => 'assigned_to'
719 719 assert_response :success
720 720 assignees = assigns(:issues).collect(&:assigned_to).compact
721 721 assert_equal assignees.sort, assignees
722 722 assert_select 'table.issues.sort-by-assigned-to.sort-asc'
723 723 end
724 724
725 725 def test_index_sort_by_assigned_to_desc
726 726 get :index, :sort => 'assigned_to:desc'
727 727 assert_response :success
728 728 assignees = assigns(:issues).collect(&:assigned_to).compact
729 729 assert_equal assignees.sort.reverse, assignees
730 730 assert_select 'table.issues.sort-by-assigned-to.sort-desc'
731 731 end
732 732
733 733 def test_index_group_by_assigned_to
734 734 get :index, :group_by => 'assigned_to', :sort => 'priority'
735 735 assert_response :success
736 736 end
737 737
738 738 def test_index_sort_by_author
739 739 get :index, :sort => 'author'
740 740 assert_response :success
741 741 authors = assigns(:issues).collect(&:author)
742 742 assert_equal authors.sort, authors
743 743 end
744 744
745 745 def test_index_sort_by_author_desc
746 746 get :index, :sort => 'author:desc'
747 747 assert_response :success
748 748 authors = assigns(:issues).collect(&:author)
749 749 assert_equal authors.sort.reverse, authors
750 750 end
751 751
752 752 def test_index_group_by_author
753 753 get :index, :group_by => 'author', :sort => 'priority'
754 754 assert_response :success
755 755 end
756 756
757 757 def test_index_sort_by_spent_hours
758 758 get :index, :sort => 'spent_hours:desc'
759 759 assert_response :success
760 760 hours = assigns(:issues).collect(&:spent_hours)
761 761 assert_equal hours.sort.reverse, hours
762 762 end
763 763
764 764 def test_index_sort_by_total_spent_hours
765 765 get :index, :sort => 'total_spent_hours:desc'
766 766 assert_response :success
767 767 hours = assigns(:issues).collect(&:total_spent_hours)
768 768 assert_equal hours.sort.reverse, hours
769 769 end
770 770
771 771 def test_index_sort_by_total_estimated_hours
772 772 get :index, :sort => 'total_estimated_hours:desc'
773 773 assert_response :success
774 774 hours = assigns(:issues).collect(&:total_estimated_hours)
775 775 assert_equal hours.sort.reverse, hours
776 776 end
777 777
778 778 def test_index_sort_by_user_custom_field
779 779 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
780 780 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
781 781 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
782 782 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
783 783 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
784 784
785 785 get :index, :project_id => 1, :set_filter => 1, :sort => "cf_#{cf.id},id"
786 786 assert_response :success
787 787
788 788 assert_equal [2, 3, 1], assigns(:issues).select {|issue| issue.custom_field_value(cf).present?}.map(&:id)
789 789 end
790 790
791 791 def test_index_with_columns
792 792 columns = ['tracker', 'subject', 'assigned_to']
793 793 get :index, :set_filter => 1, :c => columns
794 794 assert_response :success
795 795
796 796 # query should use specified columns
797 797 query = assigns(:query)
798 798 assert_kind_of IssueQuery, query
799 799 assert_equal columns, query.column_names.map(&:to_s)
800 800
801 801 # columns should be stored in session
802 802 assert_kind_of Hash, session[:query]
803 803 assert_kind_of Array, session[:query][:column_names]
804 804 assert_equal columns, session[:query][:column_names].map(&:to_s)
805 805
806 806 # ensure only these columns are kept in the selected columns list
807 807 assert_select 'select#selected_columns option' do
808 808 assert_select 'option', 3
809 809 assert_select 'option[value=tracker]'
810 810 assert_select 'option[value=project]', 0
811 811 end
812 812 end
813 813
814 814 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
815 815 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
816 816 get :index, :set_filter => 1
817 817
818 818 # query should use specified columns
819 819 query = assigns(:query)
820 820 assert_kind_of IssueQuery, query
821 821 assert_equal [:id, :project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
822 822 end
823 823
824 824 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
825 825 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
826 826 columns = ['id', 'tracker', 'subject', 'assigned_to']
827 827 get :index, :set_filter => 1, :c => columns
828 828
829 829 # query should use specified columns
830 830 query = assigns(:query)
831 831 assert_kind_of IssueQuery, query
832 832 assert_equal columns.map(&:to_sym), query.columns.map(&:name)
833 833 end
834 834
835 835 def test_index_with_default_columns_should_respect_default_columns_order
836 836 columns = ['assigned_to', 'subject', 'status', 'tracker']
837 837 with_settings :issue_list_default_columns => columns do
838 838 get :index, :project_id => 1, :set_filter => 1
839 839
840 840 query = assigns(:query)
841 841 assert_equal (['id'] + columns).map(&:to_sym), query.columns.map(&:name)
842 842 end
843 843 end
844 844
845 845 def test_index_with_custom_field_column
846 846 columns = %w(tracker subject cf_2)
847 847 get :index, :set_filter => 1, :c => columns
848 848 assert_response :success
849 849
850 850 # query should use specified columns
851 851 query = assigns(:query)
852 852 assert_kind_of IssueQuery, query
853 853 assert_equal columns, query.column_names.map(&:to_s)
854 854
855 855 assert_select 'table.issues td.cf_2.string'
856 856 end
857 857
858 858 def test_index_with_multi_custom_field_column
859 859 field = CustomField.find(1)
860 860 field.update_attribute :multiple, true
861 861 issue = Issue.find(1)
862 862 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
863 863 issue.save!
864 864
865 865 get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
866 866 assert_response :success
867 867
868 868 assert_select 'table.issues td.cf_1', :text => 'MySQL, Oracle'
869 869 end
870 870
871 871 def test_index_with_multi_user_custom_field_column
872 872 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
873 873 :tracker_ids => [1], :is_for_all => true)
874 874 issue = Issue.find(1)
875 875 issue.custom_field_values = {field.id => ['2', '3']}
876 876 issue.save!
877 877
878 878 get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
879 879 assert_response :success
880 880
881 881 assert_select "table.issues td.cf_#{field.id}" do
882 882 assert_select 'a', 2
883 883 assert_select 'a[href=?]', '/users/2', :text => 'John Smith'
884 884 assert_select 'a[href=?]', '/users/3', :text => 'Dave Lopper'
885 885 end
886 886 end
887 887
888 888 def test_index_with_date_column
889 889 with_settings :date_format => '%d/%m/%Y' do
890 890 Issue.find(1).update_attribute :start_date, '1987-08-24'
891 891 get :index, :set_filter => 1, :c => %w(start_date)
892 892 assert_select "table.issues td.start_date", :text => '24/08/1987'
893 893 end
894 894 end
895 895
896 896 def test_index_with_done_ratio_column
897 897 Issue.find(1).update_attribute :done_ratio, 40
898 898 get :index, :set_filter => 1, :c => %w(done_ratio)
899 899 assert_select 'table.issues td.done_ratio' do
900 900 assert_select 'table.progress' do
901 901 assert_select 'td.closed[style=?]', 'width: 40%;'
902 902 end
903 903 end
904 904 end
905 905
906 906 def test_index_with_spent_hours_column
907 907 Issue.expects(:load_visible_spent_hours).once
908 908 get :index, :set_filter => 1, :c => %w(subject spent_hours)
909 909 assert_select 'table.issues tr#issue-3 td.spent_hours', :text => '1.00'
910 910 end
911 911
912 912 def test_index_with_total_spent_hours_column
913 913 Issue.expects(:load_visible_total_spent_hours).once
914 914 get :index, :set_filter => 1, :c => %w(subject total_spent_hours)
915 915 assert_select 'table.issues tr#issue-3 td.total_spent_hours', :text => '1.00'
916 916 end
917 917
918 918 def test_index_with_total_estimated_hours_column
919 919 get :index, :set_filter => 1, :c => %w(subject total_estimated_hours)
920 920 assert_select 'table.issues td.total_estimated_hours'
921 921 end
922 922
923 923 def test_index_should_not_show_spent_hours_column_without_permission
924 924 Role.anonymous.remove_permission! :view_time_entries
925 925 get :index, :set_filter => 1, :c => %w(subject spent_hours)
926 926 assert_select 'td.spent_hours', 0
927 927 end
928 928
929 929 def test_index_with_fixed_version_column
930 930 get :index, :set_filter => 1, :c => %w(fixed_version)
931 931 assert_select 'table.issues td.fixed_version' do
932 932 assert_select 'a[href=?]', '/versions/2', :text => 'eCookbook - 1.0'
933 933 end
934 934 end
935 935
936 936 def test_index_with_relations_column
937 937 IssueRelation.delete_all
938 938 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(7))
939 939 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(8), :issue_to => Issue.find(1))
940 940 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(1), :issue_to => Issue.find(11))
941 941 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(12), :issue_to => Issue.find(2))
942 942
943 943 get :index, :set_filter => 1, :c => %w(subject relations)
944 944 assert_response :success
945 945 assert_select "tr#issue-1 td.relations" do
946 946 assert_select "span", 3
947 947 assert_select "span", :text => "Related to #7"
948 948 assert_select "span", :text => "Related to #8"
949 949 assert_select "span", :text => "Blocks #11"
950 950 end
951 951 assert_select "tr#issue-2 td.relations" do
952 952 assert_select "span", 1
953 953 assert_select "span", :text => "Blocked by #12"
954 954 end
955 955 assert_select "tr#issue-3 td.relations" do
956 956 assert_select "span", 0
957 957 end
958 958
959 959 get :index, :set_filter => 1, :c => %w(relations), :format => 'csv'
960 960 assert_response :success
961 961 assert_equal 'text/csv; header=present', response.content_type
962 962 lines = response.body.chomp.split("\n")
963 963 assert_include '1,"Related to #7, Related to #8, Blocks #11"', lines
964 964 assert_include '2,Blocked by #12', lines
965 965 assert_include '3,""', lines
966 966
967 967 get :index, :set_filter => 1, :c => %w(subject relations), :format => 'pdf'
968 968 assert_response :success
969 969 assert_equal 'application/pdf', response.content_type
970 970 end
971 971
972 972 def test_index_with_description_column
973 973 get :index, :set_filter => 1, :c => %w(subject description)
974 974
975 975 assert_select 'table.issues thead th', 3 # columns: chekbox + id + subject
976 976 assert_select 'td.description[colspan="3"]', :text => 'Unable to print recipes'
977 977
978 978 get :index, :set_filter => 1, :c => %w(subject description), :format => 'pdf'
979 979 assert_response :success
980 980 assert_equal 'application/pdf', response.content_type
981 981 end
982 982
983 983 def test_index_with_parent_column
984 984 Issue.delete_all
985 985 parent = Issue.generate!
986 986 child = Issue.generate!(:parent_issue_id => parent.id)
987 987
988 988 get :index, :c => %w(parent)
989 989
990 990 assert_select 'td.parent', :text => "#{parent.tracker} ##{parent.id}"
991 991 assert_select 'td.parent a[title=?]', parent.subject
992 992 end
993 993
994 994 def test_index_with_estimated_hours_total
995 995 Issue.delete_all
996 996 Issue.generate!(:estimated_hours => 5.5)
997 997 Issue.generate!(:estimated_hours => 1.1)
998 998
999 999 get :index, :t => %w(estimated_hours)
1000 1000 assert_response :success
1001 1001 assert_select '.query-totals'
1002 1002 assert_select '.total-for-estimated-hours span.value', :text => '6.60'
1003 1003 assert_select 'input[type=checkbox][name=?][value=estimated_hours][checked=checked]', 't[]'
1004 1004 end
1005 1005
1006 1006 def test_index_with_grouped_query_and_estimated_hours_total
1007 1007 Issue.delete_all
1008 1008 Issue.generate!(:estimated_hours => 5.5, :category_id => 1)
1009 1009 Issue.generate!(:estimated_hours => 2.3, :category_id => 1)
1010 1010 Issue.generate!(:estimated_hours => 1.1, :category_id => 2)
1011 1011 Issue.generate!(:estimated_hours => 4.6)
1012 1012
1013 1013 get :index, :t => %w(estimated_hours), :group_by => 'category'
1014 1014 assert_response :success
1015 1015 assert_select '.query-totals'
1016 1016 assert_select '.query-totals .total-for-estimated-hours span.value', :text => '13.50'
1017 1017 assert_select 'tr.group', :text => /Printing/ do
1018 1018 assert_select '.total-for-estimated-hours span.value', :text => '7.80'
1019 1019 end
1020 1020 assert_select 'tr.group', :text => /Recipes/ do
1021 1021 assert_select '.total-for-estimated-hours span.value', :text => '1.10'
1022 1022 end
1023 1023 assert_select 'tr.group', :text => /blank/ do
1024 1024 assert_select '.total-for-estimated-hours span.value', :text => '4.60'
1025 1025 end
1026 1026 end
1027 1027
1028 1028 def test_index_with_int_custom_field_total
1029 1029 field = IssueCustomField.generate!(:field_format => 'int', :is_for_all => true)
1030 1030 CustomValue.create!(:customized => Issue.find(1), :custom_field => field, :value => '2')
1031 1031 CustomValue.create!(:customized => Issue.find(2), :custom_field => field, :value => '7')
1032 1032
1033 1033 get :index, :t => ["cf_#{field.id}"]
1034 1034 assert_response :success
1035 1035 assert_select '.query-totals'
1036 1036 assert_select ".total-for-cf-#{field.id} span.value", :text => '9'
1037 1037 end
1038 1038
1039 1039 def test_index_totals_should_default_to_settings
1040 1040 with_settings :issue_list_default_totals => ['estimated_hours'] do
1041 1041 get :index
1042 1042 assert_response :success
1043 1043 assert_select '.total-for-estimated-hours span.value'
1044 1044 assert_select '.query-totals>span', 1
1045 1045 end
1046 1046 end
1047 1047
1048 1048 def test_index_send_html_if_query_is_invalid
1049 1049 get :index, :f => ['start_date'], :op => {:start_date => '='}
1050 1050 assert_equal 'text/html', @response.content_type
1051 1051 assert_template 'index'
1052 1052 end
1053 1053
1054 1054 def test_index_send_nothing_if_query_is_invalid
1055 1055 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
1056 1056 assert_equal 'text/csv', @response.content_type
1057 1057 assert @response.body.blank?
1058 1058 end
1059 1059
1060 1060 def test_show_by_anonymous
1061 1061 get :show, :id => 1
1062 1062 assert_response :success
1063 1063 assert_template 'show'
1064 1064 assert_equal Issue.find(1), assigns(:issue)
1065 1065 assert_select 'div.issue div.description', :text => /Unable to print recipes/
1066 1066 # anonymous role is allowed to add a note
1067 1067 assert_select 'form#issue-form' do
1068 1068 assert_select 'fieldset' do
1069 1069 assert_select 'legend', :text => 'Notes'
1070 1070 assert_select 'textarea[name=?]', 'issue[notes]'
1071 1071 end
1072 1072 end
1073 1073 assert_select 'title', :text => "Bug #1: Cannot print recipes - eCookbook - Redmine"
1074 1074 end
1075 1075
1076 1076 def test_show_by_manager
1077 1077 @request.session[:user_id] = 2
1078 1078 get :show, :id => 1
1079 1079 assert_response :success
1080 1080 assert_select 'a', :text => /Quote/
1081 1081 assert_select 'form#issue-form' do
1082 1082 assert_select 'fieldset' do
1083 1083 assert_select 'legend', :text => 'Change properties'
1084 1084 assert_select 'input[name=?]', 'issue[subject]'
1085 1085 end
1086 1086 assert_select 'fieldset' do
1087 1087 assert_select 'legend', :text => 'Log time'
1088 1088 assert_select 'input[name=?]', 'time_entry[hours]'
1089 1089 end
1090 1090 assert_select 'fieldset' do
1091 1091 assert_select 'legend', :text => 'Notes'
1092 1092 assert_select 'textarea[name=?]', 'issue[notes]'
1093 1093 end
1094 1094 end
1095 1095 end
1096 1096
1097 1097 def test_show_should_display_update_form
1098 1098 @request.session[:user_id] = 2
1099 1099 get :show, :id => 1
1100 1100 assert_response :success
1101 1101
1102 1102 assert_select 'form#issue-form' do
1103 1103 assert_select 'input[name=?]', 'issue[is_private]'
1104 1104 assert_select 'select[name=?]', 'issue[project_id]'
1105 1105 assert_select 'select[name=?]', 'issue[tracker_id]'
1106 1106 assert_select 'input[name=?]', 'issue[subject]'
1107 1107 assert_select 'textarea[name=?]', 'issue[description]'
1108 1108 assert_select 'select[name=?]', 'issue[status_id]'
1109 1109 assert_select 'select[name=?]', 'issue[priority_id]'
1110 1110 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1111 1111 assert_select 'select[name=?]', 'issue[category_id]'
1112 1112 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1113 1113 assert_select 'input[name=?]', 'issue[parent_issue_id]'
1114 1114 assert_select 'input[name=?]', 'issue[start_date]'
1115 1115 assert_select 'input[name=?]', 'issue[due_date]'
1116 1116 assert_select 'select[name=?]', 'issue[done_ratio]'
1117 1117 assert_select 'input[name=?]', 'issue[custom_field_values][2]'
1118 1118 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1119 1119 assert_select 'textarea[name=?]', 'issue[notes]'
1120 1120 end
1121 1121 end
1122 1122
1123 1123 def test_show_should_display_update_form_with_minimal_permissions
1124 1124 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
1125 1125 WorkflowTransition.delete_all :role_id => 1
1126 1126
1127 1127 @request.session[:user_id] = 2
1128 1128 get :show, :id => 1
1129 1129 assert_response :success
1130 1130
1131 1131 assert_select 'form#issue-form' do
1132 1132 assert_select 'input[name=?]', 'issue[is_private]', 0
1133 1133 assert_select 'select[name=?]', 'issue[project_id]', 0
1134 1134 assert_select 'select[name=?]', 'issue[tracker_id]', 0
1135 1135 assert_select 'input[name=?]', 'issue[subject]', 0
1136 1136 assert_select 'textarea[name=?]', 'issue[description]', 0
1137 1137 assert_select 'select[name=?]', 'issue[status_id]', 0
1138 1138 assert_select 'select[name=?]', 'issue[priority_id]', 0
1139 1139 assert_select 'select[name=?]', 'issue[assigned_to_id]', 0
1140 1140 assert_select 'select[name=?]', 'issue[category_id]', 0
1141 1141 assert_select 'select[name=?]', 'issue[fixed_version_id]', 0
1142 1142 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1143 1143 assert_select 'input[name=?]', 'issue[start_date]', 0
1144 1144 assert_select 'input[name=?]', 'issue[due_date]', 0
1145 1145 assert_select 'select[name=?]', 'issue[done_ratio]', 0
1146 1146 assert_select 'input[name=?]', 'issue[custom_field_values][2]', 0
1147 1147 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1148 1148 assert_select 'textarea[name=?]', 'issue[notes]'
1149 1149 end
1150 1150 end
1151 1151
1152 1152 def test_show_should_not_display_update_form_without_permissions
1153 1153 Role.find(1).update_attribute :permissions, [:view_issues]
1154 1154
1155 1155 @request.session[:user_id] = 2
1156 1156 get :show, :id => 1
1157 1157 assert_response :success
1158 1158
1159 1159 assert_select 'form#issue-form', 0
1160 1160 end
1161 1161
1162 1162 def test_update_form_should_not_display_inactive_enumerations
1163 1163 assert !IssuePriority.find(15).active?
1164 1164
1165 1165 @request.session[:user_id] = 2
1166 1166 get :show, :id => 1
1167 1167 assert_response :success
1168 1168
1169 1169 assert_select 'form#issue-form' do
1170 1170 assert_select 'select[name=?]', 'issue[priority_id]' do
1171 1171 assert_select 'option[value="4"]'
1172 1172 assert_select 'option[value="15"]', 0
1173 1173 end
1174 1174 end
1175 1175 end
1176 1176
1177 1177 def test_update_form_should_allow_attachment_upload
1178 1178 @request.session[:user_id] = 2
1179 1179 get :show, :id => 1
1180 1180
1181 1181 assert_select 'form#issue-form[method=post][enctype="multipart/form-data"]' do
1182 1182 assert_select 'input[type=file][name=?]', 'attachments[dummy][file]'
1183 1183 end
1184 1184 end
1185 1185
1186 1186 def test_show_should_deny_anonymous_access_without_permission
1187 1187 Role.anonymous.remove_permission!(:view_issues)
1188 1188 get :show, :id => 1
1189 1189 assert_response :redirect
1190 1190 end
1191 1191
1192 1192 def test_show_should_deny_anonymous_access_to_private_issue
1193 1193 Issue.where(:id => 1).update_all(["is_private = ?", true])
1194 1194 get :show, :id => 1
1195 1195 assert_response :redirect
1196 1196 end
1197 1197
1198 1198 def test_show_should_deny_non_member_access_without_permission
1199 1199 Role.non_member.remove_permission!(:view_issues)
1200 1200 @request.session[:user_id] = 9
1201 1201 get :show, :id => 1
1202 1202 assert_response 403
1203 1203 end
1204 1204
1205 1205 def test_show_should_deny_non_member_access_to_private_issue
1206 1206 Issue.where(:id => 1).update_all(["is_private = ?", true])
1207 1207 @request.session[:user_id] = 9
1208 1208 get :show, :id => 1
1209 1209 assert_response 403
1210 1210 end
1211 1211
1212 1212 def test_show_should_deny_member_access_without_permission
1213 1213 Role.find(1).remove_permission!(:view_issues)
1214 1214 @request.session[:user_id] = 2
1215 1215 get :show, :id => 1
1216 1216 assert_response 403
1217 1217 end
1218 1218
1219 1219 def test_show_should_deny_member_access_to_private_issue_without_permission
1220 1220 Issue.where(:id => 1).update_all(["is_private = ?", true])
1221 1221 @request.session[:user_id] = 3
1222 1222 get :show, :id => 1
1223 1223 assert_response 403
1224 1224 end
1225 1225
1226 1226 def test_show_should_allow_author_access_to_private_issue
1227 1227 Issue.where(:id => 1).update_all(["is_private = ?, author_id = 3", true])
1228 1228 @request.session[:user_id] = 3
1229 1229 get :show, :id => 1
1230 1230 assert_response :success
1231 1231 end
1232 1232
1233 1233 def test_show_should_allow_assignee_access_to_private_issue
1234 1234 Issue.where(:id => 1).update_all(["is_private = ?, assigned_to_id = 3", true])
1235 1235 @request.session[:user_id] = 3
1236 1236 get :show, :id => 1
1237 1237 assert_response :success
1238 1238 end
1239 1239
1240 1240 def test_show_should_allow_member_access_to_private_issue_with_permission
1241 1241 Issue.where(:id => 1).update_all(["is_private = ?", true])
1242 1242 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
1243 1243 @request.session[:user_id] = 3
1244 1244 get :show, :id => 1
1245 1245 assert_response :success
1246 1246 end
1247 1247
1248 1248 def test_show_should_not_disclose_relations_to_invisible_issues
1249 1249 Setting.cross_project_issue_relations = '1'
1250 1250 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
1251 1251 # Relation to a private project issue
1252 1252 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
1253 1253
1254 1254 get :show, :id => 1
1255 1255 assert_response :success
1256 1256
1257 1257 assert_select 'div#relations' do
1258 1258 assert_select 'a', :text => /#2$/
1259 1259 assert_select 'a', :text => /#4$/, :count => 0
1260 1260 end
1261 1261 end
1262 1262
1263 1263 def test_show_should_list_subtasks
1264 1264 Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1265 1265
1266 1266 get :show, :id => 1
1267 1267 assert_response :success
1268 1268
1269 1269 assert_select 'div#issue_tree' do
1270 1270 assert_select 'td.subject', :text => /Child Issue/
1271 1271 end
1272 1272 end
1273 1273
1274 1274 def test_show_should_list_parents
1275 1275 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1276 1276
1277 1277 get :show, :id => issue.id
1278 1278 assert_response :success
1279 1279
1280 1280 assert_select 'div.subject' do
1281 1281 assert_select 'h3', 'Child Issue'
1282 1282 assert_select 'a[href="/issues/1"]'
1283 1283 end
1284 1284 end
1285 1285
1286 1286 def test_show_should_not_display_prev_next_links_without_query_in_session
1287 1287 get :show, :id => 1
1288 1288 assert_response :success
1289 1289 assert_nil assigns(:prev_issue_id)
1290 1290 assert_nil assigns(:next_issue_id)
1291 1291
1292 1292 assert_select 'div.next-prev-links', 0
1293 1293 end
1294 1294
1295 1295 def test_show_should_display_prev_next_links_with_query_in_session
1296 1296 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1297 1297 @request.session['issues_index_sort'] = 'id'
1298 1298
1299 1299 with_settings :display_subprojects_issues => '0' do
1300 1300 get :show, :id => 3
1301 1301 end
1302 1302
1303 1303 assert_response :success
1304 1304 # Previous and next issues for all projects
1305 1305 assert_equal 2, assigns(:prev_issue_id)
1306 1306 assert_equal 5, assigns(:next_issue_id)
1307 1307
1308 1308 count = Issue.open.visible.count
1309 1309
1310 1310 assert_select 'div.next-prev-links' do
1311 1311 assert_select 'a[href="/issues/2"]', :text => /Previous/
1312 1312 assert_select 'a[href="/issues/5"]', :text => /Next/
1313 1313 assert_select 'span.position', :text => "3 of #{count}"
1314 1314 end
1315 1315 end
1316 1316
1317 1317 def test_show_should_display_prev_next_links_with_saved_query_in_session
1318 1318 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1,
1319 1319 :filters => {'status_id' => {:values => ['5'], :operator => '='}},
1320 1320 :sort_criteria => [['id', 'asc']])
1321 1321 @request.session[:query] = {:id => query.id, :project_id => nil}
1322 1322
1323 1323 get :show, :id => 11
1324 1324
1325 1325 assert_response :success
1326 1326 assert_equal query, assigns(:query)
1327 1327 # Previous and next issues for all projects
1328 1328 assert_equal 8, assigns(:prev_issue_id)
1329 1329 assert_equal 12, assigns(:next_issue_id)
1330 1330
1331 1331 assert_select 'div.next-prev-links' do
1332 1332 assert_select 'a[href="/issues/8"]', :text => /Previous/
1333 1333 assert_select 'a[href="/issues/12"]', :text => /Next/
1334 1334 end
1335 1335 end
1336 1336
1337 1337 def test_show_should_display_prev_next_links_with_query_and_sort_on_association
1338 1338 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1339 1339
1340 1340 %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
1341 1341 @request.session['issues_index_sort'] = assoc_sort
1342 1342
1343 1343 get :show, :id => 3
1344 1344 assert_response :success, "Wrong response status for #{assoc_sort} sort"
1345 1345
1346 1346 assert_select 'div.next-prev-links' do
1347 1347 assert_select 'a', :text => /(Previous|Next)/
1348 1348 end
1349 1349 end
1350 1350 end
1351 1351
1352 1352 def test_show_should_display_prev_next_links_with_project_query_in_session
1353 1353 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1354 1354 @request.session['issues_index_sort'] = 'id'
1355 1355
1356 1356 with_settings :display_subprojects_issues => '0' do
1357 1357 get :show, :id => 3
1358 1358 end
1359 1359
1360 1360 assert_response :success
1361 1361 # Previous and next issues inside project
1362 1362 assert_equal 2, assigns(:prev_issue_id)
1363 1363 assert_equal 7, assigns(:next_issue_id)
1364 1364
1365 1365 assert_select 'div.next-prev-links' do
1366 1366 assert_select 'a[href="/issues/2"]', :text => /Previous/
1367 1367 assert_select 'a[href="/issues/7"]', :text => /Next/
1368 1368 end
1369 1369 end
1370 1370
1371 1371 def test_show_should_not_display_prev_link_for_first_issue
1372 1372 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1373 1373 @request.session['issues_index_sort'] = 'id'
1374 1374
1375 1375 with_settings :display_subprojects_issues => '0' do
1376 1376 get :show, :id => 1
1377 1377 end
1378 1378
1379 1379 assert_response :success
1380 1380 assert_nil assigns(:prev_issue_id)
1381 1381 assert_equal 2, assigns(:next_issue_id)
1382 1382
1383 1383 assert_select 'div.next-prev-links' do
1384 1384 assert_select 'a', :text => /Previous/, :count => 0
1385 1385 assert_select 'a[href="/issues/2"]', :text => /Next/
1386 1386 end
1387 1387 end
1388 1388
1389 1389 def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
1390 1390 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
1391 1391 @request.session['issues_index_sort'] = 'id'
1392 1392
1393 1393 get :show, :id => 1
1394 1394
1395 1395 assert_response :success
1396 1396 assert_nil assigns(:prev_issue_id)
1397 1397 assert_nil assigns(:next_issue_id)
1398 1398
1399 1399 assert_select 'a', :text => /Previous/, :count => 0
1400 1400 assert_select 'a', :text => /Next/, :count => 0
1401 1401 end
1402 1402
1403 1403 def test_show_show_should_display_prev_next_links_with_query_sort_by_user_custom_field
1404 1404 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
1405 1405 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
1406 1406 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
1407 1407 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
1408 1408 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
1409 1409
1410 1410 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1, :filters => {},
1411 1411 :sort_criteria => [["cf_#{cf.id}", 'asc'], ['id', 'asc']])
1412 1412 @request.session[:query] = {:id => query.id, :project_id => nil}
1413 1413
1414 1414 get :show, :id => 3
1415 1415 assert_response :success
1416 1416
1417 1417 assert_equal 2, assigns(:prev_issue_id)
1418 1418 assert_equal 1, assigns(:next_issue_id)
1419 1419
1420 1420 assert_select 'div.next-prev-links' do
1421 1421 assert_select 'a[href="/issues/2"]', :text => /Previous/
1422 1422 assert_select 'a[href="/issues/1"]', :text => /Next/
1423 1423 end
1424 1424 end
1425 1425
1426 def test_show_should_display_prev_next_links_when_request_has_previous_and_next_issue_ids_params
1427 get :show, :id => 1, :prev_issue_id => 1, :next_issue_id => 3, :issue_position => 2, :issue_count => 4
1428 assert_response :success
1429
1430 assert_select 'div.next-prev-links' do
1431 assert_select 'a[href="/issues/1"]', :text => /Previous/
1432 assert_select 'a[href="/issues/3"]', :text => /Next/
1433 assert_select 'span.position', :text => "2 of 4"
1434 end
1435 end
1436
1426 1437 def test_show_should_display_category_field_if_categories_are_defined
1427 1438 Issue.update_all :category_id => nil
1428 1439
1429 1440 get :show, :id => 1
1430 1441 assert_response :success
1431 1442 assert_select '.attributes .category'
1432 1443 end
1433 1444
1434 1445 def test_show_should_not_display_category_field_if_no_categories_are_defined
1435 1446 Project.find(1).issue_categories.delete_all
1436 1447
1437 1448 get :show, :id => 1
1438 1449 assert_response :success
1439 1450 assert_select 'table.attributes .category', 0
1440 1451 end
1441 1452
1442 1453 def test_show_should_display_link_to_the_assignee
1443 1454 get :show, :id => 2
1444 1455 assert_response :success
1445 1456 assert_select '.assigned-to' do
1446 1457 assert_select 'a[href="/users/3"]'
1447 1458 end
1448 1459 end
1449 1460
1450 1461 def test_show_should_display_visible_changesets_from_other_projects
1451 1462 project = Project.find(2)
1452 1463 issue = project.issues.first
1453 1464 issue.changeset_ids = [102]
1454 1465 issue.save!
1455 1466 # changesets from other projects should be displayed even if repository
1456 1467 # is disabled on issue's project
1457 1468 project.disable_module! :repository
1458 1469
1459 1470 @request.session[:user_id] = 2
1460 1471 get :show, :id => issue.id
1461 1472
1462 1473 assert_select 'a[href=?]', '/projects/ecookbook/repository/revisions/3'
1463 1474 end
1464 1475
1465 1476 def test_show_should_display_watchers
1466 1477 @request.session[:user_id] = 2
1467 1478 Issue.find(1).add_watcher User.find(2)
1468 1479
1469 1480 get :show, :id => 1
1470 1481 assert_select 'div#watchers ul' do
1471 1482 assert_select 'li' do
1472 1483 assert_select 'a[href="/users/2"]'
1473 1484 assert_select 'a[class*=delete]'
1474 1485 end
1475 1486 end
1476 1487 end
1477 1488
1478 1489 def test_show_should_display_watchers_with_gravatars
1479 1490 @request.session[:user_id] = 2
1480 1491 Issue.find(1).add_watcher User.find(2)
1481 1492
1482 1493 with_settings :gravatar_enabled => '1' do
1483 1494 get :show, :id => 1
1484 1495 end
1485 1496
1486 1497 assert_select 'div#watchers ul' do
1487 1498 assert_select 'li' do
1488 1499 assert_select 'img.gravatar'
1489 1500 assert_select 'a[href="/users/2"]'
1490 1501 assert_select 'a[class*=delete]'
1491 1502 end
1492 1503 end
1493 1504 end
1494 1505
1495 1506 def test_show_with_thumbnails_enabled_should_display_thumbnails
1496 1507 @request.session[:user_id] = 2
1497 1508
1498 1509 with_settings :thumbnails_enabled => '1' do
1499 1510 get :show, :id => 14
1500 1511 assert_response :success
1501 1512 end
1502 1513
1503 1514 assert_select 'div.thumbnails' do
1504 1515 assert_select 'a[href="/attachments/16/testfile.png"]' do
1505 1516 assert_select 'img[src="/attachments/thumbnail/16"]'
1506 1517 end
1507 1518 end
1508 1519 end
1509 1520
1510 1521 def test_show_with_thumbnails_disabled_should_not_display_thumbnails
1511 1522 @request.session[:user_id] = 2
1512 1523
1513 1524 with_settings :thumbnails_enabled => '0' do
1514 1525 get :show, :id => 14
1515 1526 assert_response :success
1516 1527 end
1517 1528
1518 1529 assert_select 'div.thumbnails', 0
1519 1530 end
1520 1531
1521 1532 def test_show_with_multi_custom_field
1522 1533 field = CustomField.find(1)
1523 1534 field.update_attribute :multiple, true
1524 1535 issue = Issue.find(1)
1525 1536 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
1526 1537 issue.save!
1527 1538
1528 1539 get :show, :id => 1
1529 1540 assert_response :success
1530 1541
1531 1542 assert_select ".cf_1 .value", :text => 'MySQL, Oracle'
1532 1543 end
1533 1544
1534 1545 def test_show_with_multi_user_custom_field
1535 1546 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1536 1547 :tracker_ids => [1], :is_for_all => true)
1537 1548 issue = Issue.find(1)
1538 1549 issue.custom_field_values = {field.id => ['2', '3']}
1539 1550 issue.save!
1540 1551
1541 1552 get :show, :id => 1
1542 1553 assert_response :success
1543 1554
1544 1555 assert_select ".cf_#{field.id} .value", :text => 'Dave Lopper, John Smith' do
1545 1556 assert_select 'a', :text => 'Dave Lopper'
1546 1557 assert_select 'a', :text => 'John Smith'
1547 1558 end
1548 1559 end
1549 1560
1550 1561 def test_show_should_display_private_notes_with_permission_only
1551 1562 journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
1552 1563 @request.session[:user_id] = 2
1553 1564
1554 1565 get :show, :id => 2
1555 1566 assert_response :success
1556 1567 assert_include journal, assigns(:journals)
1557 1568
1558 1569 Role.find(1).remove_permission! :view_private_notes
1559 1570 get :show, :id => 2
1560 1571 assert_response :success
1561 1572 assert_not_include journal, assigns(:journals)
1562 1573 end
1563 1574
1564 1575 def test_show_atom
1565 1576 get :show, :id => 2, :format => 'atom'
1566 1577 assert_response :success
1567 1578 assert_template 'journals/index'
1568 1579 # Inline image
1569 1580 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1570 1581 end
1571 1582
1572 1583 def test_show_export_to_pdf
1573 1584 issue = Issue.find(3)
1574 1585 assert issue.relations.select{|r| r.other_issue(issue).visible?}.present?
1575 1586 get :show, :id => 3, :format => 'pdf'
1576 1587 assert_response :success
1577 1588 assert_equal 'application/pdf', @response.content_type
1578 1589 assert @response.body.starts_with?('%PDF')
1579 1590 assert_not_nil assigns(:issue)
1580 1591 end
1581 1592
1582 1593 def test_export_to_pdf_with_utf8_u_fffd
1583 1594 # U+FFFD
1584 1595 s = "\xef\xbf\xbd"
1585 1596 s.force_encoding('UTF-8') if s.respond_to?(:force_encoding)
1586 1597 issue = Issue.generate!(:subject => s)
1587 1598 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
1588 1599 with_settings :default_language => lang do
1589 1600 get :show, :id => issue.id, :format => 'pdf'
1590 1601 assert_response :success
1591 1602 assert_equal 'application/pdf', @response.content_type
1592 1603 assert @response.body.starts_with?('%PDF')
1593 1604 assert_not_nil assigns(:issue)
1594 1605 end
1595 1606 end
1596 1607 end
1597 1608
1598 1609 def test_show_export_to_pdf_with_ancestors
1599 1610 issue = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1600 1611
1601 1612 get :show, :id => issue.id, :format => 'pdf'
1602 1613 assert_response :success
1603 1614 assert_equal 'application/pdf', @response.content_type
1604 1615 assert @response.body.starts_with?('%PDF')
1605 1616 end
1606 1617
1607 1618 def test_show_export_to_pdf_with_descendants
1608 1619 c1 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1609 1620 c2 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1610 1621 c3 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => c1.id)
1611 1622
1612 1623 get :show, :id => 1, :format => 'pdf'
1613 1624 assert_response :success
1614 1625 assert_equal 'application/pdf', @response.content_type
1615 1626 assert @response.body.starts_with?('%PDF')
1616 1627 end
1617 1628
1618 1629 def test_show_export_to_pdf_with_journals
1619 1630 get :show, :id => 1, :format => 'pdf'
1620 1631 assert_response :success
1621 1632 assert_equal 'application/pdf', @response.content_type
1622 1633 assert @response.body.starts_with?('%PDF')
1623 1634 end
1624 1635
1625 1636 def test_show_export_to_pdf_with_changesets
1626 1637 [[100], [100, 101], [100, 101, 102]].each do |cs|
1627 1638 issue1 = Issue.find(3)
1628 1639 issue1.changesets = Changeset.find(cs)
1629 1640 issue1.save!
1630 1641 issue = Issue.find(3)
1631 1642 assert_equal issue.changesets.count, cs.size
1632 1643 get :show, :id => 3, :format => 'pdf'
1633 1644 assert_response :success
1634 1645 assert_equal 'application/pdf', @response.content_type
1635 1646 assert @response.body.starts_with?('%PDF')
1636 1647 end
1637 1648 end
1638 1649
1639 1650 def test_show_invalid_should_respond_with_404
1640 1651 get :show, :id => 999
1641 1652 assert_response 404
1642 1653 end
1643 1654
1644 1655 def test_get_new
1645 1656 @request.session[:user_id] = 2
1646 1657 get :new, :project_id => 1, :tracker_id => 1
1647 1658 assert_response :success
1648 1659 assert_template 'new'
1649 1660
1650 1661 assert_select 'form#issue-form[action=?]', '/projects/ecookbook/issues'
1651 1662 assert_select 'form#issue-form' do
1652 1663 assert_select 'input[name=?]', 'issue[is_private]'
1653 1664 assert_select 'select[name=?]', 'issue[project_id]', 0
1654 1665 assert_select 'select[name=?]', 'issue[tracker_id]'
1655 1666 assert_select 'input[name=?]', 'issue[subject]'
1656 1667 assert_select 'textarea[name=?]', 'issue[description]'
1657 1668 assert_select 'select[name=?]', 'issue[status_id]'
1658 1669 assert_select 'select[name=?]', 'issue[priority_id]'
1659 1670 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1660 1671 assert_select 'select[name=?]', 'issue[category_id]'
1661 1672 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1662 1673 assert_select 'input[name=?]', 'issue[parent_issue_id]'
1663 1674 assert_select 'input[name=?]', 'issue[start_date]'
1664 1675 assert_select 'input[name=?]', 'issue[due_date]'
1665 1676 assert_select 'select[name=?]', 'issue[done_ratio]'
1666 1677 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1667 1678 assert_select 'input[name=?]', 'issue[watcher_user_ids][]'
1668 1679 end
1669 1680
1670 1681 # Be sure we don't display inactive IssuePriorities
1671 1682 assert ! IssuePriority.find(15).active?
1672 1683 assert_select 'select[name=?]', 'issue[priority_id]' do
1673 1684 assert_select 'option[value="15"]', 0
1674 1685 end
1675 1686 end
1676 1687
1677 1688 def test_get_new_with_minimal_permissions
1678 1689 Role.find(1).update_attribute :permissions, [:add_issues]
1679 1690 WorkflowTransition.delete_all :role_id => 1
1680 1691
1681 1692 @request.session[:user_id] = 2
1682 1693 get :new, :project_id => 1, :tracker_id => 1
1683 1694 assert_response :success
1684 1695 assert_template 'new'
1685 1696
1686 1697 assert_select 'form#issue-form' do
1687 1698 assert_select 'input[name=?]', 'issue[is_private]', 0
1688 1699 assert_select 'select[name=?]', 'issue[project_id]', 0
1689 1700 assert_select 'select[name=?]', 'issue[tracker_id]'
1690 1701 assert_select 'input[name=?]', 'issue[subject]'
1691 1702 assert_select 'textarea[name=?]', 'issue[description]'
1692 1703 assert_select 'select[name=?]', 'issue[status_id]'
1693 1704 assert_select 'select[name=?]', 'issue[priority_id]'
1694 1705 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1695 1706 assert_select 'select[name=?]', 'issue[category_id]'
1696 1707 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1697 1708 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1698 1709 assert_select 'input[name=?]', 'issue[start_date]'
1699 1710 assert_select 'input[name=?]', 'issue[due_date]'
1700 1711 assert_select 'select[name=?]', 'issue[done_ratio]'
1701 1712 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1702 1713 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1703 1714 end
1704 1715 end
1705 1716
1706 1717 def test_new_without_project_id
1707 1718 @request.session[:user_id] = 2
1708 1719 get :new
1709 1720 assert_response :success
1710 1721 assert_template 'new'
1711 1722
1712 1723 assert_select 'form#issue-form[action=?]', '/issues'
1713 1724 assert_select 'form#issue-form' do
1714 1725 assert_select 'select[name=?]', 'issue[project_id]'
1715 1726 end
1716 1727
1717 1728 assert_nil assigns(:project)
1718 1729 assert_not_nil assigns(:issue)
1719 1730 end
1720 1731
1721 1732 def test_new_should_select_default_status
1722 1733 @request.session[:user_id] = 2
1723 1734
1724 1735 get :new, :project_id => 1
1725 1736 assert_response :success
1726 1737 assert_template 'new'
1727 1738 assert_select 'select[name=?]', 'issue[status_id]' do
1728 1739 assert_select 'option[value="1"][selected=selected]'
1729 1740 end
1730 1741 assert_select 'input[name=was_default_status][value="1"]'
1731 1742 end
1732 1743
1733 1744 def test_new_should_propose_allowed_statuses
1734 1745 WorkflowTransition.delete_all
1735 1746 WorkflowTransition.create!(:tracker_id => 1, :role_id => 1, :old_status_id => 0, :new_status_id => 1)
1736 1747 WorkflowTransition.create!(:tracker_id => 1, :role_id => 1, :old_status_id => 0, :new_status_id => 3)
1737 1748 @request.session[:user_id] = 2
1738 1749
1739 1750 get :new, :project_id => 1
1740 1751 assert_response :success
1741 1752 assert_select 'select[name=?]', 'issue[status_id]' do
1742 1753 assert_select 'option[value="1"]'
1743 1754 assert_select 'option[value="3"]'
1744 1755 assert_select 'option', 2
1745 1756 assert_select 'option[value="1"][selected=selected]'
1746 1757 end
1747 1758 end
1748 1759
1749 1760 def test_new_should_propose_allowed_statuses_without_default_status_allowed
1750 1761 WorkflowTransition.delete_all
1751 1762 WorkflowTransition.create!(:tracker_id => 1, :role_id => 1, :old_status_id => 0, :new_status_id => 2)
1752 1763 assert_equal 1, Tracker.find(1).default_status_id
1753 1764 @request.session[:user_id] = 2
1754 1765
1755 1766 get :new, :project_id => 1
1756 1767 assert_response :success
1757 1768 assert_select 'select[name=?]', 'issue[status_id]' do
1758 1769 assert_select 'option[value="2"]'
1759 1770 assert_select 'option', 1
1760 1771 assert_select 'option[value="2"][selected=selected]'
1761 1772 end
1762 1773 end
1763 1774
1764 1775 def test_new_should_preselect_default_version
1765 1776 version = Version.generate!(:project_id => 1)
1766 1777 Project.find(1).update_attribute :default_version_id, version.id
1767 1778 @request.session[:user_id] = 2
1768 1779
1769 1780 get :new, :project_id => 1
1770 1781 assert_response :success
1771 1782 assert_equal version, assigns(:issue).fixed_version
1772 1783 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
1773 1784 assert_select 'option[value=?][selected=selected]', version.id.to_s
1774 1785 end
1775 1786 end
1776 1787
1777 1788 def test_get_new_with_list_custom_field
1778 1789 @request.session[:user_id] = 2
1779 1790 get :new, :project_id => 1, :tracker_id => 1
1780 1791 assert_response :success
1781 1792 assert_template 'new'
1782 1793
1783 1794 assert_select 'select.list_cf[name=?]', 'issue[custom_field_values][1]' do
1784 1795 assert_select 'option', 4
1785 1796 assert_select 'option[value=MySQL]', :text => 'MySQL'
1786 1797 end
1787 1798 end
1788 1799
1789 1800 def test_get_new_with_multi_custom_field
1790 1801 field = IssueCustomField.find(1)
1791 1802 field.update_attribute :multiple, true
1792 1803
1793 1804 @request.session[:user_id] = 2
1794 1805 get :new, :project_id => 1, :tracker_id => 1
1795 1806 assert_response :success
1796 1807 assert_template 'new'
1797 1808
1798 1809 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
1799 1810 assert_select 'option', 3
1800 1811 assert_select 'option[value=MySQL]', :text => 'MySQL'
1801 1812 end
1802 1813 assert_select 'input[name=?][type=hidden][value=?]', 'issue[custom_field_values][1][]', ''
1803 1814 end
1804 1815
1805 1816 def test_get_new_with_multi_user_custom_field
1806 1817 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1807 1818 :tracker_ids => [1], :is_for_all => true)
1808 1819
1809 1820 @request.session[:user_id] = 2
1810 1821 get :new, :project_id => 1, :tracker_id => 1
1811 1822 assert_response :success
1812 1823 assert_template 'new'
1813 1824
1814 1825 assert_select 'select[name=?][multiple=multiple]', "issue[custom_field_values][#{field.id}][]" do
1815 1826 assert_select 'option', Project.find(1).users.count
1816 1827 assert_select 'option[value="2"]', :text => 'John Smith'
1817 1828 end
1818 1829 assert_select 'input[name=?][type=hidden][value=?]', "issue[custom_field_values][#{field.id}][]", ''
1819 1830 end
1820 1831
1821 1832 def test_get_new_with_date_custom_field
1822 1833 field = IssueCustomField.create!(:name => 'Date', :field_format => 'date', :tracker_ids => [1], :is_for_all => true)
1823 1834
1824 1835 @request.session[:user_id] = 2
1825 1836 get :new, :project_id => 1, :tracker_id => 1
1826 1837 assert_response :success
1827 1838
1828 1839 assert_select 'input[name=?]', "issue[custom_field_values][#{field.id}]"
1829 1840 end
1830 1841
1831 1842 def test_get_new_with_text_custom_field
1832 1843 field = IssueCustomField.create!(:name => 'Text', :field_format => 'text', :tracker_ids => [1], :is_for_all => true)
1833 1844
1834 1845 @request.session[:user_id] = 2
1835 1846 get :new, :project_id => 1, :tracker_id => 1
1836 1847 assert_response :success
1837 1848
1838 1849 assert_select 'textarea[name=?]', "issue[custom_field_values][#{field.id}]"
1839 1850 end
1840 1851
1841 1852 def test_get_new_without_default_start_date_is_creation_date
1842 1853 with_settings :default_issue_start_date_to_creation_date => 0 do
1843 1854 @request.session[:user_id] = 2
1844 1855 get :new, :project_id => 1, :tracker_id => 1
1845 1856 assert_response :success
1846 1857 assert_template 'new'
1847 1858 assert_select 'input[name=?]', 'issue[start_date]'
1848 1859 assert_select 'input[name=?][value]', 'issue[start_date]', 0
1849 1860 end
1850 1861 end
1851 1862
1852 1863 def test_get_new_with_default_start_date_is_creation_date
1853 1864 with_settings :default_issue_start_date_to_creation_date => 1 do
1854 1865 @request.session[:user_id] = 2
1855 1866 get :new, :project_id => 1, :tracker_id => 1
1856 1867 assert_response :success
1857 1868 assert_template 'new'
1858 1869 assert_select 'input[name=?][value=?]', 'issue[start_date]',
1859 1870 Date.today.to_s
1860 1871 end
1861 1872 end
1862 1873
1863 1874 def test_get_new_form_should_allow_attachment_upload
1864 1875 @request.session[:user_id] = 2
1865 1876 get :new, :project_id => 1, :tracker_id => 1
1866 1877
1867 1878 assert_select 'form[id=issue-form][method=post][enctype="multipart/form-data"]' do
1868 1879 assert_select 'input[name=?][type=file]', 'attachments[dummy][file]'
1869 1880 end
1870 1881 end
1871 1882
1872 1883 def test_get_new_should_prefill_the_form_from_params
1873 1884 @request.session[:user_id] = 2
1874 1885 get :new, :project_id => 1,
1875 1886 :issue => {:tracker_id => 3, :description => 'Prefilled', :custom_field_values => {'2' => 'Custom field value'}}
1876 1887
1877 1888 issue = assigns(:issue)
1878 1889 assert_equal 3, issue.tracker_id
1879 1890 assert_equal 'Prefilled', issue.description
1880 1891 assert_equal 'Custom field value', issue.custom_field_value(2)
1881 1892
1882 1893 assert_select 'select[name=?]', 'issue[tracker_id]' do
1883 1894 assert_select 'option[value="3"][selected=selected]'
1884 1895 end
1885 1896 assert_select 'textarea[name=?]', 'issue[description]', :text => /Prefilled/
1886 1897 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Custom field value'
1887 1898 end
1888 1899
1889 1900 def test_get_new_should_mark_required_fields
1890 1901 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1891 1902 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1892 1903 WorkflowPermission.delete_all
1893 1904 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1894 1905 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1895 1906 @request.session[:user_id] = 2
1896 1907
1897 1908 get :new, :project_id => 1
1898 1909 assert_response :success
1899 1910 assert_template 'new'
1900 1911
1901 1912 assert_select 'label[for=issue_start_date]' do
1902 1913 assert_select 'span[class=required]', 0
1903 1914 end
1904 1915 assert_select 'label[for=issue_due_date]' do
1905 1916 assert_select 'span[class=required]'
1906 1917 end
1907 1918 assert_select 'label[for=?]', "issue_custom_field_values_#{cf1.id}" do
1908 1919 assert_select 'span[class=required]', 0
1909 1920 end
1910 1921 assert_select 'label[for=?]', "issue_custom_field_values_#{cf2.id}" do
1911 1922 assert_select 'span[class=required]'
1912 1923 end
1913 1924 end
1914 1925
1915 1926 def test_get_new_should_not_display_readonly_fields
1916 1927 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1917 1928 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1918 1929 WorkflowPermission.delete_all
1919 1930 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
1920 1931 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
1921 1932 @request.session[:user_id] = 2
1922 1933
1923 1934 get :new, :project_id => 1
1924 1935 assert_response :success
1925 1936 assert_template 'new'
1926 1937
1927 1938 assert_select 'input[name=?]', 'issue[start_date]'
1928 1939 assert_select 'input[name=?]', 'issue[due_date]', 0
1929 1940 assert_select 'input[name=?]', "issue[custom_field_values][#{cf1.id}]"
1930 1941 assert_select 'input[name=?]', "issue[custom_field_values][#{cf2.id}]", 0
1931 1942 end
1932 1943
1933 1944 def test_new_with_tracker_set_as_readonly_should_accept_status
1934 1945 WorkflowPermission.delete_all
1935 1946 [1, 2].each do |status_id|
1936 1947 WorkflowPermission.create!(:tracker_id => 1, :old_status_id => status_id, :role_id => 1, :field_name => 'tracker_id', :rule => 'readonly')
1937 1948 end
1938 1949 @request.session[:user_id] = 2
1939 1950
1940 1951 get :new, :project_id => 1, :issue => {:status_id => 2}
1941 1952 assert_select 'select[name=?]', 'issue[tracker_id]', 0
1942 1953 assert_equal 2, assigns(:issue).status_id
1943 1954 end
1944 1955
1945 1956 def test_get_new_without_tracker_id
1946 1957 @request.session[:user_id] = 2
1947 1958 get :new, :project_id => 1
1948 1959 assert_response :success
1949 1960 assert_template 'new'
1950 1961
1951 1962 issue = assigns(:issue)
1952 1963 assert_not_nil issue
1953 1964 assert_equal Project.find(1).trackers.first, issue.tracker
1954 1965 end
1955 1966
1956 1967 def test_get_new_with_no_default_status_should_display_an_error
1957 1968 @request.session[:user_id] = 2
1958 1969 IssueStatus.delete_all
1959 1970
1960 1971 get :new, :project_id => 1
1961 1972 assert_response 500
1962 1973 assert_select_error /No default issue/
1963 1974 end
1964 1975
1965 1976 def test_get_new_with_no_tracker_should_display_an_error
1966 1977 @request.session[:user_id] = 2
1967 1978 Tracker.delete_all
1968 1979
1969 1980 get :new, :project_id => 1
1970 1981 assert_response 500
1971 1982 assert_select_error /No tracker/
1972 1983 end
1973 1984
1974 1985 def test_new_with_invalid_project_id
1975 1986 @request.session[:user_id] = 1
1976 1987 get :new, :project_id => 'invalid'
1977 1988 assert_response 404
1978 1989 end
1979 1990
1980 1991 def test_update_form_for_new_issue
1981 1992 @request.session[:user_id] = 2
1982 1993 xhr :post, :new, :project_id => 1,
1983 1994 :issue => {:tracker_id => 2,
1984 1995 :subject => 'This is the test_new issue',
1985 1996 :description => 'This is the description',
1986 1997 :priority_id => 5}
1987 1998 assert_response :success
1988 1999 assert_template 'new'
1989 2000 assert_template :partial => '_form'
1990 2001 assert_equal 'text/javascript', response.content_type
1991 2002
1992 2003 issue = assigns(:issue)
1993 2004 assert_kind_of Issue, issue
1994 2005 assert_equal 1, issue.project_id
1995 2006 assert_equal 2, issue.tracker_id
1996 2007 assert_equal 'This is the test_new issue', issue.subject
1997 2008 end
1998 2009
1999 2010 def test_update_form_for_new_issue_should_propose_transitions_based_on_initial_status
2000 2011 @request.session[:user_id] = 2
2001 2012 WorkflowTransition.delete_all
2002 2013 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 0, :new_status_id => 2)
2003 2014 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 0, :new_status_id => 5)
2004 2015 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4)
2005 2016
2006 2017 xhr :post, :new, :project_id => 1,
2007 2018 :issue => {:tracker_id => 1,
2008 2019 :status_id => 5,
2009 2020 :subject => 'This is an issue'}
2010 2021
2011 2022 assert_equal 5, assigns(:issue).status_id
2012 2023 assert_equal [2,5], assigns(:allowed_statuses).map(&:id).sort
2013 2024 end
2014 2025
2015 2026 def test_update_form_with_default_status_should_ignore_submitted_status_id_if_equals
2016 2027 @request.session[:user_id] = 2
2017 2028 tracker = Tracker.find(2)
2018 2029 tracker.update! :default_status_id => 2
2019 2030 tracker.generate_transitions! 2, 1, :clear => true
2020 2031
2021 2032 xhr :post, :new, :project_id => 1,
2022 2033 :issue => {:tracker_id => 2,
2023 2034 :status_id => 1},
2024 2035 :was_default_status => 1
2025 2036
2026 2037 assert_equal 2, assigns(:issue).status_id
2027 2038 end
2028 2039
2029 2040 def test_update_form_for_new_issue_should_ignore_version_when_changing_project
2030 2041 version = Version.generate!(:project_id => 1)
2031 2042 Project.find(1).update_attribute :default_version_id, version.id
2032 2043 @request.session[:user_id] = 2
2033 2044
2034 2045 xhr :post, :new, :issue => {:project_id => 1,
2035 2046 :fixed_version_id => ''},
2036 2047 :form_update_triggered_by => 'issue_project_id'
2037 2048 assert_response :success
2038 2049 assert_template 'new'
2039 2050
2040 2051 issue = assigns(:issue)
2041 2052 assert_equal 1, issue.project_id
2042 2053 assert_equal version, issue.fixed_version
2043 2054 end
2044 2055
2045 2056 def test_post_create
2046 2057 @request.session[:user_id] = 2
2047 2058 assert_difference 'Issue.count' do
2048 2059 assert_no_difference 'Journal.count' do
2049 2060 post :create, :project_id => 1,
2050 2061 :issue => {:tracker_id => 3,
2051 2062 :status_id => 2,
2052 2063 :subject => 'This is the test_new issue',
2053 2064 :description => 'This is the description',
2054 2065 :priority_id => 5,
2055 2066 :start_date => '2010-11-07',
2056 2067 :estimated_hours => '',
2057 2068 :custom_field_values => {'2' => 'Value for field 2'}}
2058 2069 end
2059 2070 end
2060 2071 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2061 2072
2062 2073 issue = Issue.find_by_subject('This is the test_new issue')
2063 2074 assert_not_nil issue
2064 2075 assert_equal 2, issue.author_id
2065 2076 assert_equal 3, issue.tracker_id
2066 2077 assert_equal 2, issue.status_id
2067 2078 assert_equal Date.parse('2010-11-07'), issue.start_date
2068 2079 assert_nil issue.estimated_hours
2069 2080 v = issue.custom_values.where(:custom_field_id => 2).first
2070 2081 assert_not_nil v
2071 2082 assert_equal 'Value for field 2', v.value
2072 2083 end
2073 2084
2074 2085 def test_post_new_with_group_assignment
2075 2086 group = Group.find(11)
2076 2087 project = Project.find(1)
2077 2088 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
2078 2089
2079 2090 with_settings :issue_group_assignment => '1' do
2080 2091 @request.session[:user_id] = 2
2081 2092 assert_difference 'Issue.count' do
2082 2093 post :create, :project_id => project.id,
2083 2094 :issue => {:tracker_id => 3,
2084 2095 :status_id => 1,
2085 2096 :subject => 'This is the test_new_with_group_assignment issue',
2086 2097 :assigned_to_id => group.id}
2087 2098 end
2088 2099 end
2089 2100 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2090 2101
2091 2102 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
2092 2103 assert_not_nil issue
2093 2104 assert_equal group, issue.assigned_to
2094 2105 end
2095 2106
2096 2107 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
2097 2108 with_settings :default_issue_start_date_to_creation_date => 0 do
2098 2109 @request.session[:user_id] = 2
2099 2110 assert_difference 'Issue.count' do
2100 2111 post :create, :project_id => 1,
2101 2112 :issue => {:tracker_id => 3,
2102 2113 :status_id => 2,
2103 2114 :subject => 'This is the test_new issue',
2104 2115 :description => 'This is the description',
2105 2116 :priority_id => 5,
2106 2117 :estimated_hours => '',
2107 2118 :custom_field_values => {'2' => 'Value for field 2'}}
2108 2119 end
2109 2120 assert_redirected_to :controller => 'issues', :action => 'show',
2110 2121 :id => Issue.last.id
2111 2122 issue = Issue.find_by_subject('This is the test_new issue')
2112 2123 assert_not_nil issue
2113 2124 assert_nil issue.start_date
2114 2125 end
2115 2126 end
2116 2127
2117 2128 def test_post_create_without_start_date_and_default_start_date_is_creation_date
2118 2129 with_settings :default_issue_start_date_to_creation_date => 1 do
2119 2130 @request.session[:user_id] = 2
2120 2131 assert_difference 'Issue.count' do
2121 2132 post :create, :project_id => 1,
2122 2133 :issue => {:tracker_id => 3,
2123 2134 :status_id => 2,
2124 2135 :subject => 'This is the test_new issue',
2125 2136 :description => 'This is the description',
2126 2137 :priority_id => 5,
2127 2138 :estimated_hours => '',
2128 2139 :custom_field_values => {'2' => 'Value for field 2'}}
2129 2140 end
2130 2141 assert_redirected_to :controller => 'issues', :action => 'show',
2131 2142 :id => Issue.last.id
2132 2143 issue = Issue.find_by_subject('This is the test_new issue')
2133 2144 assert_not_nil issue
2134 2145 assert_equal Date.today, issue.start_date
2135 2146 end
2136 2147 end
2137 2148
2138 2149 def test_post_create_and_continue
2139 2150 @request.session[:user_id] = 2
2140 2151 assert_difference 'Issue.count' do
2141 2152 post :create, :project_id => 1,
2142 2153 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
2143 2154 :continue => ''
2144 2155 end
2145 2156
2146 2157 issue = Issue.order('id DESC').first
2147 2158 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
2148 2159 assert_not_nil flash[:notice], "flash was not set"
2149 2160 assert_select_in flash[:notice],
2150 2161 'a[href=?][title=?]', "/issues/#{issue.id}", "This is first issue", :text => "##{issue.id}"
2151 2162 end
2152 2163
2153 2164 def test_post_create_without_custom_fields_param
2154 2165 @request.session[:user_id] = 2
2155 2166 assert_difference 'Issue.count' do
2156 2167 post :create, :project_id => 1,
2157 2168 :issue => {:tracker_id => 1,
2158 2169 :subject => 'This is the test_new issue',
2159 2170 :description => 'This is the description',
2160 2171 :priority_id => 5}
2161 2172 end
2162 2173 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2163 2174 end
2164 2175
2165 2176 def test_post_create_with_multi_custom_field
2166 2177 field = IssueCustomField.find_by_name('Database')
2167 2178 field.update_attribute(:multiple, true)
2168 2179
2169 2180 @request.session[:user_id] = 2
2170 2181 assert_difference 'Issue.count' do
2171 2182 post :create, :project_id => 1,
2172 2183 :issue => {:tracker_id => 1,
2173 2184 :subject => 'This is the test_new issue',
2174 2185 :description => 'This is the description',
2175 2186 :priority_id => 5,
2176 2187 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
2177 2188 end
2178 2189 assert_response 302
2179 2190 issue = Issue.order('id DESC').first
2180 2191 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
2181 2192 end
2182 2193
2183 2194 def test_post_create_with_empty_multi_custom_field
2184 2195 field = IssueCustomField.find_by_name('Database')
2185 2196 field.update_attribute(:multiple, true)
2186 2197
2187 2198 @request.session[:user_id] = 2
2188 2199 assert_difference 'Issue.count' do
2189 2200 post :create, :project_id => 1,
2190 2201 :issue => {:tracker_id => 1,
2191 2202 :subject => 'This is the test_new issue',
2192 2203 :description => 'This is the description',
2193 2204 :priority_id => 5,
2194 2205 :custom_field_values => {'1' => ['']}}
2195 2206 end
2196 2207 assert_response 302
2197 2208 issue = Issue.order('id DESC').first
2198 2209 assert_equal [''], issue.custom_field_value(1).sort
2199 2210 end
2200 2211
2201 2212 def test_post_create_with_multi_user_custom_field
2202 2213 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
2203 2214 :tracker_ids => [1], :is_for_all => true)
2204 2215
2205 2216 @request.session[:user_id] = 2
2206 2217 assert_difference 'Issue.count' do
2207 2218 post :create, :project_id => 1,
2208 2219 :issue => {:tracker_id => 1,
2209 2220 :subject => 'This is the test_new issue',
2210 2221 :description => 'This is the description',
2211 2222 :priority_id => 5,
2212 2223 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
2213 2224 end
2214 2225 assert_response 302
2215 2226 issue = Issue.order('id DESC').first
2216 2227 assert_equal ['2', '3'], issue.custom_field_value(field).sort
2217 2228 end
2218 2229
2219 2230 def test_post_create_with_required_custom_field_and_without_custom_fields_param
2220 2231 field = IssueCustomField.find_by_name('Database')
2221 2232 field.update_attribute(:is_required, true)
2222 2233
2223 2234 @request.session[:user_id] = 2
2224 2235 assert_no_difference 'Issue.count' do
2225 2236 post :create, :project_id => 1,
2226 2237 :issue => {:tracker_id => 1,
2227 2238 :subject => 'This is the test_new issue',
2228 2239 :description => 'This is the description',
2229 2240 :priority_id => 5}
2230 2241 end
2231 2242 assert_response :success
2232 2243 assert_template 'new'
2233 2244 issue = assigns(:issue)
2234 2245 assert_not_nil issue
2235 2246 assert_select_error /Database cannot be blank/
2236 2247 end
2237 2248
2238 2249 def test_create_should_validate_required_fields
2239 2250 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2240 2251 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2241 2252 WorkflowPermission.delete_all
2242 2253 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'required')
2243 2254 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2244 2255 @request.session[:user_id] = 2
2245 2256
2246 2257 assert_no_difference 'Issue.count' do
2247 2258 post :create, :project_id => 1, :issue => {
2248 2259 :tracker_id => 2,
2249 2260 :status_id => 1,
2250 2261 :subject => 'Test',
2251 2262 :start_date => '',
2252 2263 :due_date => '',
2253 2264 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ''}
2254 2265 }
2255 2266 assert_response :success
2256 2267 assert_template 'new'
2257 2268 end
2258 2269
2259 2270 assert_select_error /Due date cannot be blank/i
2260 2271 assert_select_error /Bar cannot be blank/i
2261 2272 end
2262 2273
2263 2274 def test_create_should_validate_required_list_fields
2264 2275 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'list', :is_for_all => true, :tracker_ids => [1, 2], :multiple => false, :possible_values => ['a', 'b'])
2265 2276 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'list', :is_for_all => true, :tracker_ids => [1, 2], :multiple => true, :possible_values => ['a', 'b'])
2266 2277 WorkflowPermission.delete_all
2267 2278 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf1.id.to_s, :rule => 'required')
2268 2279 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2269 2280 @request.session[:user_id] = 2
2270 2281
2271 2282 assert_no_difference 'Issue.count' do
2272 2283 post :create, :project_id => 1, :issue => {
2273 2284 :tracker_id => 2,
2274 2285 :status_id => 1,
2275 2286 :subject => 'Test',
2276 2287 :start_date => '',
2277 2288 :due_date => '',
2278 2289 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ['']}
2279 2290 }
2280 2291 assert_response :success
2281 2292 assert_template 'new'
2282 2293 end
2283 2294
2284 2295 assert_select_error /Foo cannot be blank/i
2285 2296 assert_select_error /Bar cannot be blank/i
2286 2297 end
2287 2298
2288 2299 def test_create_should_ignore_readonly_fields
2289 2300 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2290 2301 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2291 2302 WorkflowPermission.delete_all
2292 2303 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
2293 2304 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
2294 2305 @request.session[:user_id] = 2
2295 2306
2296 2307 assert_difference 'Issue.count' do
2297 2308 post :create, :project_id => 1, :issue => {
2298 2309 :tracker_id => 2,
2299 2310 :status_id => 1,
2300 2311 :subject => 'Test',
2301 2312 :start_date => '2012-07-14',
2302 2313 :due_date => '2012-07-16',
2303 2314 :custom_field_values => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
2304 2315 }
2305 2316 assert_response 302
2306 2317 end
2307 2318
2308 2319 issue = Issue.order('id DESC').first
2309 2320 assert_equal Date.parse('2012-07-14'), issue.start_date
2310 2321 assert_nil issue.due_date
2311 2322 assert_equal 'value1', issue.custom_field_value(cf1)
2312 2323 assert_nil issue.custom_field_value(cf2)
2313 2324 end
2314 2325
2315 2326 def test_post_create_with_watchers
2316 2327 @request.session[:user_id] = 2
2317 2328 ActionMailer::Base.deliveries.clear
2318 2329
2319 2330 with_settings :notified_events => %w(issue_added) do
2320 2331 assert_difference 'Watcher.count', 2 do
2321 2332 post :create, :project_id => 1,
2322 2333 :issue => {:tracker_id => 1,
2323 2334 :subject => 'This is a new issue with watchers',
2324 2335 :description => 'This is the description',
2325 2336 :priority_id => 5,
2326 2337 :watcher_user_ids => ['2', '3']}
2327 2338 end
2328 2339 end
2329 2340 issue = Issue.find_by_subject('This is a new issue with watchers')
2330 2341 assert_not_nil issue
2331 2342 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
2332 2343
2333 2344 # Watchers added
2334 2345 assert_equal [2, 3], issue.watcher_user_ids.sort
2335 2346 assert issue.watched_by?(User.find(3))
2336 2347 # Watchers notified
2337 2348 mail = ActionMailer::Base.deliveries.last
2338 2349 assert_not_nil mail
2339 2350 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
2340 2351 end
2341 2352
2342 2353 def test_post_create_subissue
2343 2354 @request.session[:user_id] = 2
2344 2355
2345 2356 assert_difference 'Issue.count' do
2346 2357 post :create, :project_id => 1,
2347 2358 :issue => {:tracker_id => 1,
2348 2359 :subject => 'This is a child issue',
2349 2360 :parent_issue_id => '2'}
2350 2361 assert_response 302
2351 2362 end
2352 2363 issue = Issue.order('id DESC').first
2353 2364 assert_equal Issue.find(2), issue.parent
2354 2365 end
2355 2366
2356 2367 def test_post_create_subissue_with_sharp_parent_id
2357 2368 @request.session[:user_id] = 2
2358 2369
2359 2370 assert_difference 'Issue.count' do
2360 2371 post :create, :project_id => 1,
2361 2372 :issue => {:tracker_id => 1,
2362 2373 :subject => 'This is a child issue',
2363 2374 :parent_issue_id => '#2'}
2364 2375 assert_response 302
2365 2376 end
2366 2377 issue = Issue.order('id DESC').first
2367 2378 assert_equal Issue.find(2), issue.parent
2368 2379 end
2369 2380
2370 2381 def test_post_create_subissue_with_non_visible_parent_id_should_not_validate
2371 2382 @request.session[:user_id] = 2
2372 2383
2373 2384 assert_no_difference 'Issue.count' do
2374 2385 post :create, :project_id => 1,
2375 2386 :issue => {:tracker_id => 1,
2376 2387 :subject => 'This is a child issue',
2377 2388 :parent_issue_id => '4'}
2378 2389
2379 2390 assert_response :success
2380 2391 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '4'
2381 2392 assert_select_error /Parent task is invalid/i
2382 2393 end
2383 2394 end
2384 2395
2385 2396 def test_post_create_subissue_with_non_numeric_parent_id_should_not_validate
2386 2397 @request.session[:user_id] = 2
2387 2398
2388 2399 assert_no_difference 'Issue.count' do
2389 2400 post :create, :project_id => 1,
2390 2401 :issue => {:tracker_id => 1,
2391 2402 :subject => 'This is a child issue',
2392 2403 :parent_issue_id => '01ABC'}
2393 2404
2394 2405 assert_response :success
2395 2406 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '01ABC'
2396 2407 assert_select_error /Parent task is invalid/i
2397 2408 end
2398 2409 end
2399 2410
2400 2411 def test_post_create_private
2401 2412 @request.session[:user_id] = 2
2402 2413
2403 2414 assert_difference 'Issue.count' do
2404 2415 post :create, :project_id => 1,
2405 2416 :issue => {:tracker_id => 1,
2406 2417 :subject => 'This is a private issue',
2407 2418 :is_private => '1'}
2408 2419 end
2409 2420 issue = Issue.order('id DESC').first
2410 2421 assert issue.is_private?
2411 2422 end
2412 2423
2413 2424 def test_post_create_private_with_set_own_issues_private_permission
2414 2425 role = Role.find(1)
2415 2426 role.remove_permission! :set_issues_private
2416 2427 role.add_permission! :set_own_issues_private
2417 2428
2418 2429 @request.session[:user_id] = 2
2419 2430
2420 2431 assert_difference 'Issue.count' do
2421 2432 post :create, :project_id => 1,
2422 2433 :issue => {:tracker_id => 1,
2423 2434 :subject => 'This is a private issue',
2424 2435 :is_private => '1'}
2425 2436 end
2426 2437 issue = Issue.order('id DESC').first
2427 2438 assert issue.is_private?
2428 2439 end
2429 2440
2430 2441 def test_create_without_project_id
2431 2442 @request.session[:user_id] = 2
2432 2443
2433 2444 assert_difference 'Issue.count' do
2434 2445 post :create,
2435 2446 :issue => {:project_id => 3,
2436 2447 :tracker_id => 2,
2437 2448 :subject => 'Foo'}
2438 2449 assert_response 302
2439 2450 end
2440 2451 issue = Issue.order('id DESC').first
2441 2452 assert_equal 3, issue.project_id
2442 2453 assert_equal 2, issue.tracker_id
2443 2454 end
2444 2455
2445 2456 def test_create_without_project_id_and_continue_should_redirect_without_project_id
2446 2457 @request.session[:user_id] = 2
2447 2458
2448 2459 assert_difference 'Issue.count' do
2449 2460 post :create,
2450 2461 :issue => {:project_id => 3,
2451 2462 :tracker_id => 2,
2452 2463 :subject => 'Foo'},
2453 2464 :continue => '1'
2454 2465 assert_redirected_to '/issues/new?issue%5Bproject_id%5D=3&issue%5Btracker_id%5D=2'
2455 2466 end
2456 2467 end
2457 2468
2458 2469 def test_create_without_project_id_should_be_denied_without_permission
2459 2470 Role.non_member.remove_permission! :add_issues
2460 2471 Role.anonymous.remove_permission! :add_issues
2461 2472 @request.session[:user_id] = 2
2462 2473
2463 2474 assert_no_difference 'Issue.count' do
2464 2475 post :create,
2465 2476 :issue => {:project_id => 3,
2466 2477 :tracker_id => 2,
2467 2478 :subject => 'Foo'}
2468 2479 assert_response 422
2469 2480 end
2470 2481 end
2471 2482
2472 2483 def test_create_without_project_id_with_failure
2473 2484 @request.session[:user_id] = 2
2474 2485
2475 2486 post :create,
2476 2487 :issue => {:project_id => 3,
2477 2488 :tracker_id => 2,
2478 2489 :subject => ''}
2479 2490 assert_response :success
2480 2491 assert_nil assigns(:project)
2481 2492 end
2482 2493
2483 2494 def test_post_create_should_send_a_notification
2484 2495 ActionMailer::Base.deliveries.clear
2485 2496 @request.session[:user_id] = 2
2486 2497 with_settings :notified_events => %w(issue_added) do
2487 2498 assert_difference 'Issue.count' do
2488 2499 post :create, :project_id => 1,
2489 2500 :issue => {:tracker_id => 3,
2490 2501 :subject => 'This is the test_new issue',
2491 2502 :description => 'This is the description',
2492 2503 :priority_id => 5,
2493 2504 :estimated_hours => '',
2494 2505 :custom_field_values => {'2' => 'Value for field 2'}}
2495 2506 end
2496 2507 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2497 2508
2498 2509 assert_equal 1, ActionMailer::Base.deliveries.size
2499 2510 end
2500 2511 end
2501 2512
2502 2513 def test_post_create_should_preserve_fields_values_on_validation_failure
2503 2514 @request.session[:user_id] = 2
2504 2515 post :create, :project_id => 1,
2505 2516 :issue => {:tracker_id => 1,
2506 2517 # empty subject
2507 2518 :subject => '',
2508 2519 :description => 'This is a description',
2509 2520 :priority_id => 6,
2510 2521 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
2511 2522 assert_response :success
2512 2523 assert_template 'new'
2513 2524
2514 2525 assert_select 'textarea[name=?]', 'issue[description]', :text => 'This is a description'
2515 2526 assert_select 'select[name=?]', 'issue[priority_id]' do
2516 2527 assert_select 'option[value="6"][selected=selected]', :text => 'High'
2517 2528 end
2518 2529 # Custom fields
2519 2530 assert_select 'select[name=?]', 'issue[custom_field_values][1]' do
2520 2531 assert_select 'option[value=Oracle][selected=selected]', :text => 'Oracle'
2521 2532 end
2522 2533 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Value for field 2'
2523 2534 end
2524 2535
2525 2536 def test_post_create_with_failure_should_preserve_watchers
2526 2537 assert !User.find(8).member_of?(Project.find(1))
2527 2538
2528 2539 @request.session[:user_id] = 2
2529 2540 post :create, :project_id => 1,
2530 2541 :issue => {:tracker_id => 1,
2531 2542 :watcher_user_ids => ['3', '8']}
2532 2543 assert_response :success
2533 2544 assert_template 'new'
2534 2545
2535 2546 assert_select 'input[name=?][value="2"]:not(checked)', 'issue[watcher_user_ids][]'
2536 2547 assert_select 'input[name=?][value="3"][checked=checked]', 'issue[watcher_user_ids][]'
2537 2548 assert_select 'input[name=?][value="8"][checked=checked]', 'issue[watcher_user_ids][]'
2538 2549 end
2539 2550
2540 2551 def test_post_create_should_ignore_non_safe_attributes
2541 2552 @request.session[:user_id] = 2
2542 2553 assert_nothing_raised do
2543 2554 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
2544 2555 end
2545 2556 end
2546 2557
2547 2558 def test_post_create_with_attachment
2548 2559 set_tmp_attachments_directory
2549 2560 @request.session[:user_id] = 2
2550 2561
2551 2562 assert_difference 'Issue.count' do
2552 2563 assert_difference 'Attachment.count' do
2553 2564 assert_no_difference 'Journal.count' do
2554 2565 post :create, :project_id => 1,
2555 2566 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2556 2567 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2557 2568 end
2558 2569 end
2559 2570 end
2560 2571
2561 2572 issue = Issue.order('id DESC').first
2562 2573 attachment = Attachment.order('id DESC').first
2563 2574
2564 2575 assert_equal issue, attachment.container
2565 2576 assert_equal 2, attachment.author_id
2566 2577 assert_equal 'testfile.txt', attachment.filename
2567 2578 assert_equal 'text/plain', attachment.content_type
2568 2579 assert_equal 'test file', attachment.description
2569 2580 assert_equal 59, attachment.filesize
2570 2581 assert File.exists?(attachment.diskfile)
2571 2582 assert_equal 59, File.size(attachment.diskfile)
2572 2583 end
2573 2584
2574 2585 def test_post_create_with_attachment_should_notify_with_attachments
2575 2586 ActionMailer::Base.deliveries.clear
2576 2587 set_tmp_attachments_directory
2577 2588 @request.session[:user_id] = 2
2578 2589
2579 2590 with_settings :host_name => 'mydomain.foo', :protocol => 'http', :notified_events => %w(issue_added) do
2580 2591 assert_difference 'Issue.count' do
2581 2592 post :create, :project_id => 1,
2582 2593 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2583 2594 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2584 2595 end
2585 2596 end
2586 2597
2587 2598 assert_not_nil ActionMailer::Base.deliveries.last
2588 2599 assert_select_email do
2589 2600 assert_select 'a[href^=?]', 'http://mydomain.foo/attachments/download', 'testfile.txt'
2590 2601 end
2591 2602 end
2592 2603
2593 2604 def test_post_create_with_failure_should_save_attachments
2594 2605 set_tmp_attachments_directory
2595 2606 @request.session[:user_id] = 2
2596 2607
2597 2608 assert_no_difference 'Issue.count' do
2598 2609 assert_difference 'Attachment.count' do
2599 2610 post :create, :project_id => 1,
2600 2611 :issue => { :tracker_id => '1', :subject => '' },
2601 2612 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2602 2613 assert_response :success
2603 2614 assert_template 'new'
2604 2615 end
2605 2616 end
2606 2617
2607 2618 attachment = Attachment.order('id DESC').first
2608 2619 assert_equal 'testfile.txt', attachment.filename
2609 2620 assert File.exists?(attachment.diskfile)
2610 2621 assert_nil attachment.container
2611 2622
2612 2623 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2613 2624 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2614 2625 end
2615 2626
2616 2627 def test_post_create_with_failure_should_keep_saved_attachments
2617 2628 set_tmp_attachments_directory
2618 2629 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2619 2630 @request.session[:user_id] = 2
2620 2631
2621 2632 assert_no_difference 'Issue.count' do
2622 2633 assert_no_difference 'Attachment.count' do
2623 2634 post :create, :project_id => 1,
2624 2635 :issue => { :tracker_id => '1', :subject => '' },
2625 2636 :attachments => {'p0' => {'token' => attachment.token}}
2626 2637 assert_response :success
2627 2638 assert_template 'new'
2628 2639 end
2629 2640 end
2630 2641
2631 2642 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2632 2643 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2633 2644 end
2634 2645
2635 2646 def test_post_create_should_attach_saved_attachments
2636 2647 set_tmp_attachments_directory
2637 2648 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2638 2649 @request.session[:user_id] = 2
2639 2650
2640 2651 assert_difference 'Issue.count' do
2641 2652 assert_no_difference 'Attachment.count' do
2642 2653 post :create, :project_id => 1,
2643 2654 :issue => { :tracker_id => '1', :subject => 'Saved attachments' },
2644 2655 :attachments => {'p0' => {'token' => attachment.token}}
2645 2656 assert_response 302
2646 2657 end
2647 2658 end
2648 2659
2649 2660 issue = Issue.order('id DESC').first
2650 2661 assert_equal 1, issue.attachments.count
2651 2662
2652 2663 attachment.reload
2653 2664 assert_equal issue, attachment.container
2654 2665 end
2655 2666
2656 2667 def setup_without_workflow_privilege
2657 2668 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2658 2669 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2659 2670 end
2660 2671 private :setup_without_workflow_privilege
2661 2672
2662 2673 test "without workflow privilege #new should propose default status only" do
2663 2674 setup_without_workflow_privilege
2664 2675 get :new, :project_id => 1
2665 2676 assert_response :success
2666 2677 assert_template 'new'
2667 2678
2668 2679 issue = assigns(:issue)
2669 2680 assert_not_nil issue.default_status
2670 2681
2671 2682 assert_select 'select[name=?]', 'issue[status_id]' do
2672 2683 assert_select 'option', 1
2673 2684 assert_select 'option[value=?]', issue.default_status.id.to_s
2674 2685 end
2675 2686 end
2676 2687
2677 2688 test "without workflow privilege #create should accept default status" do
2678 2689 setup_without_workflow_privilege
2679 2690 assert_difference 'Issue.count' do
2680 2691 post :create, :project_id => 1,
2681 2692 :issue => {:tracker_id => 1,
2682 2693 :subject => 'This is an issue',
2683 2694 :status_id => 1}
2684 2695 end
2685 2696 issue = Issue.order('id').last
2686 2697 assert_not_nil issue.default_status
2687 2698 assert_equal issue.default_status, issue.status
2688 2699 end
2689 2700
2690 2701 test "without workflow privilege #create should ignore unauthorized status" do
2691 2702 setup_without_workflow_privilege
2692 2703 assert_difference 'Issue.count' do
2693 2704 post :create, :project_id => 1,
2694 2705 :issue => {:tracker_id => 1,
2695 2706 :subject => 'This is an issue',
2696 2707 :status_id => 3}
2697 2708 end
2698 2709 issue = Issue.order('id').last
2699 2710 assert_not_nil issue.default_status
2700 2711 assert_equal issue.default_status, issue.status
2701 2712 end
2702 2713
2703 2714 test "without workflow privilege #update should ignore status change" do
2704 2715 setup_without_workflow_privilege
2705 2716 assert_difference 'Journal.count' do
2706 2717 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2707 2718 end
2708 2719 assert_equal 1, Issue.find(1).status_id
2709 2720 end
2710 2721
2711 2722 test "without workflow privilege #update ignore attributes changes" do
2712 2723 setup_without_workflow_privilege
2713 2724 assert_difference 'Journal.count' do
2714 2725 put :update, :id => 1,
2715 2726 :issue => {:subject => 'changed', :assigned_to_id => 2,
2716 2727 :notes => 'just trying'}
2717 2728 end
2718 2729 issue = Issue.find(1)
2719 2730 assert_equal "Cannot print recipes", issue.subject
2720 2731 assert_nil issue.assigned_to
2721 2732 end
2722 2733
2723 2734 def setup_with_workflow_privilege
2724 2735 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2725 2736 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2726 2737 :old_status_id => 1, :new_status_id => 3)
2727 2738 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2728 2739 :old_status_id => 1, :new_status_id => 4)
2729 2740 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2730 2741 end
2731 2742 private :setup_with_workflow_privilege
2732 2743
2733 2744 def setup_with_workflow_privilege_and_edit_issues_permission
2734 2745 setup_with_workflow_privilege
2735 2746 Role.anonymous.add_permission! :add_issues, :edit_issues
2736 2747 end
2737 2748 private :setup_with_workflow_privilege_and_edit_issues_permission
2738 2749
2739 2750 test "with workflow privilege and :edit_issues permission should accept authorized status" do
2740 2751 setup_with_workflow_privilege_and_edit_issues_permission
2741 2752 assert_difference 'Journal.count' do
2742 2753 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2743 2754 end
2744 2755 assert_equal 3, Issue.find(1).status_id
2745 2756 end
2746 2757
2747 2758 test "with workflow privilege and :edit_issues permission should ignore unauthorized status" do
2748 2759 setup_with_workflow_privilege_and_edit_issues_permission
2749 2760 assert_difference 'Journal.count' do
2750 2761 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2751 2762 end
2752 2763 assert_equal 1, Issue.find(1).status_id
2753 2764 end
2754 2765
2755 2766 test "with workflow privilege and :edit_issues permission should accept authorized attributes changes" do
2756 2767 setup_with_workflow_privilege_and_edit_issues_permission
2757 2768 assert_difference 'Journal.count' do
2758 2769 put :update, :id => 1,
2759 2770 :issue => {:subject => 'changed', :assigned_to_id => 2,
2760 2771 :notes => 'just trying'}
2761 2772 end
2762 2773 issue = Issue.find(1)
2763 2774 assert_equal "changed", issue.subject
2764 2775 assert_equal 2, issue.assigned_to_id
2765 2776 end
2766 2777
2767 2778 def test_new_as_copy
2768 2779 @request.session[:user_id] = 2
2769 2780 get :new, :project_id => 1, :copy_from => 1
2770 2781
2771 2782 assert_response :success
2772 2783 assert_template 'new'
2773 2784
2774 2785 assert_not_nil assigns(:issue)
2775 2786 orig = Issue.find(1)
2776 2787 assert_equal 1, assigns(:issue).project_id
2777 2788 assert_equal orig.subject, assigns(:issue).subject
2778 2789 assert assigns(:issue).copy?
2779 2790
2780 2791 assert_select 'form[id=issue-form][action="/projects/ecookbook/issues"]' do
2781 2792 assert_select 'select[name=?]', 'issue[project_id]' do
2782 2793 assert_select 'option[value="1"][selected=selected]', :text => 'eCookbook'
2783 2794 assert_select 'option[value="2"]:not([selected])', :text => 'OnlineStore'
2784 2795 end
2785 2796 assert_select 'input[name=copy_from][value="1"]'
2786 2797 end
2787 2798
2788 2799 # "New issue" menu item should not link to copy
2789 2800 assert_select '#main-menu a.new-issue[href="/projects/ecookbook/issues/new"]'
2790 2801 end
2791 2802
2792 2803 def test_new_as_copy_without_add_issues_permission_should_not_propose_current_project_as_target
2793 2804 user = setup_user_with_copy_but_not_add_permission
2794 2805
2795 2806 @request.session[:user_id] = user.id
2796 2807 get :new, :project_id => 1, :copy_from => 1
2797 2808
2798 2809 assert_response :success
2799 2810 assert_template 'new'
2800 2811 assert_select 'select[name=?]', 'issue[project_id]' do
2801 2812 assert_select 'option[value="1"]', 0
2802 2813 assert_select 'option[value="2"]', :text => 'OnlineStore'
2803 2814 end
2804 2815 end
2805 2816
2806 2817 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
2807 2818 @request.session[:user_id] = 2
2808 2819 issue = Issue.find(3)
2809 2820 assert issue.attachments.count > 0
2810 2821 get :new, :project_id => 1, :copy_from => 3
2811 2822
2812 2823 assert_select 'input[name=copy_attachments][type=checkbox][checked=checked][value="1"]'
2813 2824 end
2814 2825
2815 2826 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
2816 2827 @request.session[:user_id] = 2
2817 2828 issue = Issue.find(3)
2818 2829 issue.attachments.delete_all
2819 2830 get :new, :project_id => 1, :copy_from => 3
2820 2831
2821 2832 assert_select 'input[name=copy_attachments]', 0
2822 2833 end
2823 2834
2824 2835 def test_new_as_copy_should_preserve_parent_id
2825 2836 @request.session[:user_id] = 2
2826 2837 issue = Issue.generate!(:parent_issue_id => 2)
2827 2838 get :new, :project_id => 1, :copy_from => issue.id
2828 2839
2829 2840 assert_select 'input[name=?][value="2"]', 'issue[parent_issue_id]'
2830 2841 end
2831 2842
2832 2843 def test_new_as_copy_with_subtasks_should_show_copy_subtasks_checkbox
2833 2844 @request.session[:user_id] = 2
2834 2845 issue = Issue.generate_with_descendants!
2835 2846 get :new, :project_id => 1, :copy_from => issue.id
2836 2847
2837 2848 assert_select 'input[type=checkbox][name=copy_subtasks][checked=checked][value="1"]'
2838 2849 end
2839 2850
2840 2851 def test_new_as_copy_with_invalid_issue_should_respond_with_404
2841 2852 @request.session[:user_id] = 2
2842 2853 get :new, :project_id => 1, :copy_from => 99999
2843 2854 assert_response 404
2844 2855 end
2845 2856
2846 2857 def test_create_as_copy_on_different_project
2847 2858 @request.session[:user_id] = 2
2848 2859 assert_difference 'Issue.count' do
2849 2860 post :create, :project_id => 1, :copy_from => 1,
2850 2861 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2851 2862
2852 2863 assert_not_nil assigns(:issue)
2853 2864 assert assigns(:issue).copy?
2854 2865 end
2855 2866 issue = Issue.order('id DESC').first
2856 2867 assert_redirected_to "/issues/#{issue.id}"
2857 2868
2858 2869 assert_equal 2, issue.project_id
2859 2870 assert_equal 3, issue.tracker_id
2860 2871 assert_equal 'Copy', issue.subject
2861 2872 end
2862 2873
2863 2874 def test_create_as_copy_should_allow_status_to_be_set_to_default
2864 2875 copied = Issue.generate! :status_id => 2
2865 2876 assert_equal 2, copied.reload.status_id
2866 2877
2867 2878 @request.session[:user_id] = 2
2868 2879 assert_difference 'Issue.count' do
2869 2880 post :create, :project_id => 1, :copy_from => copied.id,
2870 2881 :issue => {:project_id => '1', :tracker_id => '1', :status_id => '1'},
2871 2882 :was_default_status => '1'
2872 2883 end
2873 2884 issue = Issue.order('id DESC').first
2874 2885 assert_equal 1, issue.status_id
2875 2886 end
2876 2887
2877 2888 def test_create_as_copy_should_copy_attachments
2878 2889 @request.session[:user_id] = 2
2879 2890 issue = Issue.find(3)
2880 2891 count = issue.attachments.count
2881 2892 assert count > 0
2882 2893 assert_difference 'Issue.count' do
2883 2894 assert_difference 'Attachment.count', count do
2884 2895 post :create, :project_id => 1, :copy_from => 3,
2885 2896 :issue => {:project_id => '1', :tracker_id => '3',
2886 2897 :status_id => '1', :subject => 'Copy with attachments'},
2887 2898 :copy_attachments => '1'
2888 2899 end
2889 2900 end
2890 2901 copy = Issue.order('id DESC').first
2891 2902 assert_equal count, copy.attachments.count
2892 2903 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
2893 2904 end
2894 2905
2895 2906 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
2896 2907 @request.session[:user_id] = 2
2897 2908 issue = Issue.find(3)
2898 2909 count = issue.attachments.count
2899 2910 assert count > 0
2900 2911 assert_difference 'Issue.count' do
2901 2912 assert_no_difference 'Attachment.count' do
2902 2913 post :create, :project_id => 1, :copy_from => 3,
2903 2914 :issue => {:project_id => '1', :tracker_id => '3',
2904 2915 :status_id => '1', :subject => 'Copy with attachments'}
2905 2916 end
2906 2917 end
2907 2918 copy = Issue.order('id DESC').first
2908 2919 assert_equal 0, copy.attachments.count
2909 2920 end
2910 2921
2911 2922 def test_create_as_copy_with_attachments_should_also_add_new_files
2912 2923 @request.session[:user_id] = 2
2913 2924 issue = Issue.find(3)
2914 2925 count = issue.attachments.count
2915 2926 assert count > 0
2916 2927 assert_difference 'Issue.count' do
2917 2928 assert_difference 'Attachment.count', count + 1 do
2918 2929 post :create, :project_id => 1, :copy_from => 3,
2919 2930 :issue => {:project_id => '1', :tracker_id => '3',
2920 2931 :status_id => '1', :subject => 'Copy with attachments'},
2921 2932 :copy_attachments => '1',
2922 2933 :attachments => {'1' =>
2923 2934 {'file' => uploaded_test_file('testfile.txt', 'text/plain'),
2924 2935 'description' => 'test file'}}
2925 2936 end
2926 2937 end
2927 2938 copy = Issue.order('id DESC').first
2928 2939 assert_equal count + 1, copy.attachments.count
2929 2940 end
2930 2941
2931 2942 def test_create_as_copy_should_add_relation_with_copied_issue
2932 2943 @request.session[:user_id] = 2
2933 2944 assert_difference 'Issue.count' do
2934 2945 assert_difference 'IssueRelation.count' do
2935 2946 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2936 2947 :issue => {:project_id => '1', :tracker_id => '3',
2937 2948 :status_id => '1', :subject => 'Copy'}
2938 2949 end
2939 2950 end
2940 2951 copy = Issue.order('id DESC').first
2941 2952 assert_equal 1, copy.relations.size
2942 2953 end
2943 2954
2944 2955 def test_create_as_copy_should_allow_not_to_add_relation_with_copied_issue
2945 2956 @request.session[:user_id] = 2
2946 2957 assert_difference 'Issue.count' do
2947 2958 assert_no_difference 'IssueRelation.count' do
2948 2959 post :create, :project_id => 1, :copy_from => 1,
2949 2960 :issue => {:subject => 'Copy'}
2950 2961 end
2951 2962 end
2952 2963 end
2953 2964
2954 2965 def test_create_as_copy_should_always_add_relation_with_copied_issue_by_setting
2955 2966 with_settings :link_copied_issue => 'yes' do
2956 2967 @request.session[:user_id] = 2
2957 2968 assert_difference 'Issue.count' do
2958 2969 assert_difference 'IssueRelation.count' do
2959 2970 post :create, :project_id => 1, :copy_from => 1,
2960 2971 :issue => {:subject => 'Copy'}
2961 2972 end
2962 2973 end
2963 2974 end
2964 2975 end
2965 2976
2966 2977 def test_create_as_copy_should_never_add_relation_with_copied_issue_by_setting
2967 2978 with_settings :link_copied_issue => 'no' do
2968 2979 @request.session[:user_id] = 2
2969 2980 assert_difference 'Issue.count' do
2970 2981 assert_no_difference 'IssueRelation.count' do
2971 2982 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2972 2983 :issue => {:subject => 'Copy'}
2973 2984 end
2974 2985 end
2975 2986 end
2976 2987 end
2977 2988
2978 2989 def test_create_as_copy_should_copy_subtasks
2979 2990 @request.session[:user_id] = 2
2980 2991 issue = Issue.generate_with_descendants!
2981 2992 count = issue.descendants.count
2982 2993 assert_difference 'Issue.count', count + 1 do
2983 2994 post :create, :project_id => 1, :copy_from => issue.id,
2984 2995 :issue => {:project_id => '1', :tracker_id => '3',
2985 2996 :status_id => '1', :subject => 'Copy with subtasks'},
2986 2997 :copy_subtasks => '1'
2987 2998 end
2988 2999 copy = Issue.where(:parent_id => nil).order('id DESC').first
2989 3000 assert_equal count, copy.descendants.count
2990 3001 assert_equal issue.descendants.map(&:subject).sort, copy.descendants.map(&:subject).sort
2991 3002 end
2992 3003
2993 3004 def test_create_as_copy_without_copy_subtasks_option_should_not_copy_subtasks
2994 3005 @request.session[:user_id] = 2
2995 3006 issue = Issue.generate_with_descendants!
2996 3007 assert_difference 'Issue.count', 1 do
2997 3008 post :create, :project_id => 1, :copy_from => 3,
2998 3009 :issue => {:project_id => '1', :tracker_id => '3',
2999 3010 :status_id => '1', :subject => 'Copy with subtasks'}
3000 3011 end
3001 3012 copy = Issue.where(:parent_id => nil).order('id DESC').first
3002 3013 assert_equal 0, copy.descendants.count
3003 3014 end
3004 3015
3005 3016 def test_create_as_copy_with_failure
3006 3017 @request.session[:user_id] = 2
3007 3018 post :create, :project_id => 1, :copy_from => 1,
3008 3019 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
3009 3020
3010 3021 assert_response :success
3011 3022 assert_template 'new'
3012 3023
3013 3024 assert_not_nil assigns(:issue)
3014 3025 assert assigns(:issue).copy?
3015 3026
3016 3027 assert_select 'form#issue-form[action="/projects/ecookbook/issues"]' do
3017 3028 assert_select 'select[name=?]', 'issue[project_id]' do
3018 3029 assert_select 'option[value="1"]:not([selected])', :text => 'eCookbook'
3019 3030 assert_select 'option[value="2"][selected=selected]', :text => 'OnlineStore'
3020 3031 end
3021 3032 assert_select 'input[name=copy_from][value="1"]'
3022 3033 end
3023 3034 end
3024 3035
3025 3036 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
3026 3037 @request.session[:user_id] = 2
3027 3038 assert !User.find(2).member_of?(Project.find(4))
3028 3039
3029 3040 assert_difference 'Issue.count' do
3030 3041 post :create, :project_id => 1, :copy_from => 1,
3031 3042 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
3032 3043 end
3033 3044 issue = Issue.order('id DESC').first
3034 3045 assert_equal 1, issue.project_id
3035 3046 end
3036 3047
3037 3048 def test_get_edit
3038 3049 @request.session[:user_id] = 2
3039 3050 get :edit, :id => 1
3040 3051 assert_response :success
3041 3052 assert_template 'edit'
3042 3053 assert_not_nil assigns(:issue)
3043 3054 assert_equal Issue.find(1), assigns(:issue)
3044 3055
3045 3056 # Be sure we don't display inactive IssuePriorities
3046 3057 assert ! IssuePriority.find(15).active?
3047 3058 assert_select 'select[name=?]', 'issue[priority_id]' do
3048 3059 assert_select 'option[value="15"]', 0
3049 3060 end
3050 3061 end
3051 3062
3052 3063 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
3053 3064 @request.session[:user_id] = 2
3054 3065 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
3055 3066
3056 3067 get :edit, :id => 1
3057 3068 assert_select 'input[name=?]', 'time_entry[hours]'
3058 3069 end
3059 3070
3060 3071 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
3061 3072 @request.session[:user_id] = 2
3062 3073 Role.find_by_name('Manager').remove_permission! :log_time
3063 3074
3064 3075 get :edit, :id => 1
3065 3076 assert_select 'input[name=?]', 'time_entry[hours]', 0
3066 3077 end
3067 3078
3068 3079 def test_get_edit_with_params
3069 3080 @request.session[:user_id] = 2
3070 3081 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
3071 3082 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => 10 }
3072 3083 assert_response :success
3073 3084 assert_template 'edit'
3074 3085
3075 3086 issue = assigns(:issue)
3076 3087 assert_not_nil issue
3077 3088
3078 3089 assert_equal 5, issue.status_id
3079 3090 assert_select 'select[name=?]', 'issue[status_id]' do
3080 3091 assert_select 'option[value="5"][selected=selected]', :text => 'Closed'
3081 3092 end
3082 3093
3083 3094 assert_equal 7, issue.priority_id
3084 3095 assert_select 'select[name=?]', 'issue[priority_id]' do
3085 3096 assert_select 'option[value="7"][selected=selected]', :text => 'Urgent'
3086 3097 end
3087 3098
3088 3099 assert_select 'input[name=?][value="2.5"]', 'time_entry[hours]'
3089 3100 assert_select 'select[name=?]', 'time_entry[activity_id]' do
3090 3101 assert_select 'option[value="10"][selected=selected]', :text => 'Development'
3091 3102 end
3092 3103 assert_select 'input[name=?][value=test_get_edit_with_params]', 'time_entry[comments]'
3093 3104 end
3094 3105
3095 3106 def test_get_edit_with_multi_custom_field
3096 3107 field = CustomField.find(1)
3097 3108 field.update_attribute :multiple, true
3098 3109 issue = Issue.find(1)
3099 3110 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3100 3111 issue.save!
3101 3112
3102 3113 @request.session[:user_id] = 2
3103 3114 get :edit, :id => 1
3104 3115 assert_response :success
3105 3116 assert_template 'edit'
3106 3117
3107 3118 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
3108 3119 assert_select 'option', 3
3109 3120 assert_select 'option[value=MySQL][selected=selected]'
3110 3121 assert_select 'option[value=Oracle][selected=selected]'
3111 3122 assert_select 'option[value=PostgreSQL]:not([selected])'
3112 3123 end
3113 3124 end
3114 3125
3115 3126 def test_update_form_for_existing_issue
3116 3127 @request.session[:user_id] = 2
3117 3128 xhr :patch, :edit, :id => 1,
3118 3129 :issue => {:tracker_id => 2,
3119 3130 :subject => 'This is the test_new issue',
3120 3131 :description => 'This is the description',
3121 3132 :priority_id => 5}
3122 3133 assert_response :success
3123 3134 assert_equal 'text/javascript', response.content_type
3124 3135 assert_template 'edit'
3125 3136 assert_template :partial => '_form'
3126 3137
3127 3138 issue = assigns(:issue)
3128 3139 assert_kind_of Issue, issue
3129 3140 assert_equal 1, issue.id
3130 3141 assert_equal 1, issue.project_id
3131 3142 assert_equal 2, issue.tracker_id
3132 3143 assert_equal 'This is the test_new issue', issue.subject
3133 3144 end
3134 3145
3135 3146 def test_update_form_for_existing_issue_should_keep_issue_author
3136 3147 @request.session[:user_id] = 3
3137 3148 xhr :patch, :edit, :id => 1, :issue => {:subject => 'Changed'}
3138 3149 assert_response :success
3139 3150 assert_equal 'text/javascript', response.content_type
3140 3151
3141 3152 issue = assigns(:issue)
3142 3153 assert_equal User.find(2), issue.author
3143 3154 assert_equal 2, issue.author_id
3144 3155 assert_not_equal User.current, issue.author
3145 3156 end
3146 3157
3147 3158 def test_update_form_for_existing_issue_should_propose_transitions_based_on_initial_status
3148 3159 @request.session[:user_id] = 2
3149 3160 WorkflowTransition.delete_all
3150 3161 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
3151 3162 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
3152 3163 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 5, :new_status_id => 4)
3153 3164
3154 3165 xhr :patch, :edit, :id => 2,
3155 3166 :issue => {:tracker_id => 2,
3156 3167 :status_id => 5,
3157 3168 :subject => 'This is an issue'}
3158 3169
3159 3170 assert_equal 5, assigns(:issue).status_id
3160 3171 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
3161 3172 end
3162 3173
3163 3174 def test_update_form_for_existing_issue_with_project_change
3164 3175 @request.session[:user_id] = 2
3165 3176 xhr :patch, :edit, :id => 1,
3166 3177 :issue => {:project_id => 2,
3167 3178 :tracker_id => 2,
3168 3179 :subject => 'This is the test_new issue',
3169 3180 :description => 'This is the description',
3170 3181 :priority_id => 5}
3171 3182 assert_response :success
3172 3183 assert_template :partial => '_form'
3173 3184
3174 3185 issue = assigns(:issue)
3175 3186 assert_kind_of Issue, issue
3176 3187 assert_equal 1, issue.id
3177 3188 assert_equal 2, issue.project_id
3178 3189 assert_equal 2, issue.tracker_id
3179 3190 assert_equal 'This is the test_new issue', issue.subject
3180 3191 end
3181 3192
3182 3193 def test_update_form_should_keep_category_with_same_when_changing_project
3183 3194 source = Project.generate!
3184 3195 target = Project.generate!
3185 3196 source_category = IssueCategory.create!(:name => 'Foo', :project => source)
3186 3197 target_category = IssueCategory.create!(:name => 'Foo', :project => target)
3187 3198 issue = Issue.generate!(:project => source, :category => source_category)
3188 3199
3189 3200 @request.session[:user_id] = 1
3190 3201 patch :edit, :id => issue.id,
3191 3202 :issue => {:project_id => target.id, :category_id => source_category.id}
3192 3203 assert_response :success
3193 3204
3194 3205 issue = assigns(:issue)
3195 3206 assert_equal target_category, issue.category
3196 3207 end
3197 3208
3198 3209 def test_update_form_should_propose_default_status_for_existing_issue
3199 3210 @request.session[:user_id] = 2
3200 3211 WorkflowTransition.delete_all
3201 3212 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
3202 3213
3203 3214 xhr :patch, :edit, :id => 2
3204 3215 assert_response :success
3205 3216 assert_equal [2,3], assigns(:allowed_statuses).map(&:id).sort
3206 3217 end
3207 3218
3208 3219 def test_put_update_without_custom_fields_param
3209 3220 @request.session[:user_id] = 2
3210 3221
3211 3222 issue = Issue.find(1)
3212 3223 assert_equal '125', issue.custom_value_for(2).value
3213 3224
3214 3225 assert_difference('Journal.count') do
3215 3226 assert_difference('JournalDetail.count') do
3216 3227 put :update, :id => 1, :issue => {:subject => 'New subject'}
3217 3228 end
3218 3229 end
3219 3230 assert_redirected_to :action => 'show', :id => '1'
3220 3231 issue.reload
3221 3232 assert_equal 'New subject', issue.subject
3222 3233 # Make sure custom fields were not cleared
3223 3234 assert_equal '125', issue.custom_value_for(2).value
3224 3235 end
3225 3236
3226 3237 def test_put_update_with_project_change
3227 3238 @request.session[:user_id] = 2
3228 3239 ActionMailer::Base.deliveries.clear
3229 3240
3230 3241 with_settings :notified_events => %w(issue_updated) do
3231 3242 assert_difference('Journal.count') do
3232 3243 assert_difference('JournalDetail.count', 3) do
3233 3244 put :update, :id => 1, :issue => {:project_id => '2',
3234 3245 :tracker_id => '1', # no change
3235 3246 :priority_id => '6',
3236 3247 :category_id => '3'
3237 3248 }
3238 3249 end
3239 3250 end
3240 3251 end
3241 3252 assert_redirected_to :action => 'show', :id => '1'
3242 3253 issue = Issue.find(1)
3243 3254 assert_equal 2, issue.project_id
3244 3255 assert_equal 1, issue.tracker_id
3245 3256 assert_equal 6, issue.priority_id
3246 3257 assert_equal 3, issue.category_id
3247 3258
3248 3259 mail = ActionMailer::Base.deliveries.last
3249 3260 assert_not_nil mail
3250 3261 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
3251 3262 assert_mail_body_match "Project changed from eCookbook to OnlineStore", mail
3252 3263 end
3253 3264
3254 3265 def test_put_update_trying_to_move_issue_to_project_without_tracker_should_not_error
3255 3266 target = Project.generate!(:tracker_ids => [])
3256 3267 assert target.trackers.empty?
3257 3268 issue = Issue.generate!
3258 3269 @request.session[:user_id] = 1
3259 3270
3260 3271 put :update, :id => issue.id, :issue => {:project_id => target.id}
3261 3272 assert_response 302
3262 3273 end
3263 3274
3264 3275 def test_put_update_with_tracker_change
3265 3276 @request.session[:user_id] = 2
3266 3277 ActionMailer::Base.deliveries.clear
3267 3278
3268 3279 with_settings :notified_events => %w(issue_updated) do
3269 3280 assert_difference('Journal.count') do
3270 3281 assert_difference('JournalDetail.count', 2) do
3271 3282 put :update, :id => 1, :issue => {:project_id => '1',
3272 3283 :tracker_id => '2',
3273 3284 :priority_id => '6'
3274 3285 }
3275 3286 end
3276 3287 end
3277 3288 end
3278 3289 assert_redirected_to :action => 'show', :id => '1'
3279 3290 issue = Issue.find(1)
3280 3291 assert_equal 1, issue.project_id
3281 3292 assert_equal 2, issue.tracker_id
3282 3293 assert_equal 6, issue.priority_id
3283 3294 assert_equal 1, issue.category_id
3284 3295
3285 3296 mail = ActionMailer::Base.deliveries.last
3286 3297 assert_not_nil mail
3287 3298 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
3288 3299 assert_mail_body_match "Tracker changed from Bug to Feature request", mail
3289 3300 end
3290 3301
3291 3302 def test_put_update_with_custom_field_change
3292 3303 @request.session[:user_id] = 2
3293 3304 issue = Issue.find(1)
3294 3305 assert_equal '125', issue.custom_value_for(2).value
3295 3306
3296 3307 with_settings :notified_events => %w(issue_updated) do
3297 3308 assert_difference('Journal.count') do
3298 3309 assert_difference('JournalDetail.count', 3) do
3299 3310 put :update, :id => 1, :issue => {:subject => 'Custom field change',
3300 3311 :priority_id => '6',
3301 3312 :category_id => '1', # no change
3302 3313 :custom_field_values => { '2' => 'New custom value' }
3303 3314 }
3304 3315 end
3305 3316 end
3306 3317 end
3307 3318 assert_redirected_to :action => 'show', :id => '1'
3308 3319 issue.reload
3309 3320 assert_equal 'New custom value', issue.custom_value_for(2).value
3310 3321
3311 3322 mail = ActionMailer::Base.deliveries.last
3312 3323 assert_not_nil mail
3313 3324 assert_mail_body_match "Searchable field changed from 125 to New custom value", mail
3314 3325 end
3315 3326
3316 3327 def test_put_update_with_multi_custom_field_change
3317 3328 field = CustomField.find(1)
3318 3329 field.update_attribute :multiple, true
3319 3330 issue = Issue.find(1)
3320 3331 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3321 3332 issue.save!
3322 3333
3323 3334 @request.session[:user_id] = 2
3324 3335 assert_difference('Journal.count') do
3325 3336 assert_difference('JournalDetail.count', 3) do
3326 3337 put :update, :id => 1,
3327 3338 :issue => {
3328 3339 :subject => 'Custom field change',
3329 3340 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
3330 3341 }
3331 3342 end
3332 3343 end
3333 3344 assert_redirected_to :action => 'show', :id => '1'
3334 3345 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
3335 3346 end
3336 3347
3337 3348 def test_put_update_with_status_and_assignee_change
3338 3349 issue = Issue.find(1)
3339 3350 assert_equal 1, issue.status_id
3340 3351 @request.session[:user_id] = 2
3341 3352
3342 3353 with_settings :notified_events => %w(issue_updated) do
3343 3354 assert_difference('TimeEntry.count', 0) do
3344 3355 put :update,
3345 3356 :id => 1,
3346 3357 :issue => { :status_id => 2, :assigned_to_id => 3, :notes => 'Assigned to dlopper' },
3347 3358 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
3348 3359 end
3349 3360 end
3350 3361 assert_redirected_to :action => 'show', :id => '1'
3351 3362 issue.reload
3352 3363 assert_equal 2, issue.status_id
3353 3364 j = Journal.order('id DESC').first
3354 3365 assert_equal 'Assigned to dlopper', j.notes
3355 3366 assert_equal 2, j.details.size
3356 3367
3357 3368 mail = ActionMailer::Base.deliveries.last
3358 3369 assert_mail_body_match "Status changed from New to Assigned", mail
3359 3370 # subject should contain the new status
3360 3371 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
3361 3372 end
3362 3373
3363 3374 def test_put_update_with_note_only
3364 3375 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
3365 3376
3366 3377 with_settings :notified_events => %w(issue_updated) do
3367 3378 # anonymous user
3368 3379 put :update,
3369 3380 :id => 1,
3370 3381 :issue => { :notes => notes }
3371 3382 end
3372 3383 assert_redirected_to :action => 'show', :id => '1'
3373 3384 j = Journal.order('id DESC').first
3374 3385 assert_equal notes, j.notes
3375 3386 assert_equal 0, j.details.size
3376 3387 assert_equal User.anonymous, j.user
3377 3388
3378 3389 mail = ActionMailer::Base.deliveries.last
3379 3390 assert_mail_body_match notes, mail
3380 3391 end
3381 3392
3382 3393 def test_put_update_with_private_note_only
3383 3394 notes = 'Private note'
3384 3395 @request.session[:user_id] = 2
3385 3396
3386 3397 assert_difference 'Journal.count' do
3387 3398 put :update, :id => 1, :issue => {:notes => notes, :private_notes => '1'}
3388 3399 assert_redirected_to :action => 'show', :id => '1'
3389 3400 end
3390 3401
3391 3402 j = Journal.order('id DESC').first
3392 3403 assert_equal notes, j.notes
3393 3404 assert_equal true, j.private_notes
3394 3405 end
3395 3406
3396 3407 def test_put_update_with_private_note_and_changes
3397 3408 notes = 'Private note'
3398 3409 @request.session[:user_id] = 2
3399 3410
3400 3411 assert_difference 'Journal.count', 2 do
3401 3412 put :update, :id => 1, :issue => {:subject => 'New subject', :notes => notes, :private_notes => '1'}
3402 3413 assert_redirected_to :action => 'show', :id => '1'
3403 3414 end
3404 3415
3405 3416 j = Journal.order('id DESC').first
3406 3417 assert_equal notes, j.notes
3407 3418 assert_equal true, j.private_notes
3408 3419 assert_equal 0, j.details.count
3409 3420
3410 3421 j = Journal.order('id DESC').offset(1).first
3411 3422 assert_nil j.notes
3412 3423 assert_equal false, j.private_notes
3413 3424 assert_equal 1, j.details.count
3414 3425 end
3415 3426
3416 3427 def test_put_update_with_note_and_spent_time
3417 3428 @request.session[:user_id] = 2
3418 3429 spent_hours_before = Issue.find(1).spent_hours
3419 3430 assert_difference('TimeEntry.count') do
3420 3431 put :update,
3421 3432 :id => 1,
3422 3433 :issue => { :notes => '2.5 hours added' },
3423 3434 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
3424 3435 end
3425 3436 assert_redirected_to :action => 'show', :id => '1'
3426 3437
3427 3438 issue = Issue.find(1)
3428 3439
3429 3440 j = Journal.order('id DESC').first
3430 3441 assert_equal '2.5 hours added', j.notes
3431 3442 assert_equal 0, j.details.size
3432 3443
3433 3444 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
3434 3445 assert_not_nil t
3435 3446 assert_equal 2.5, t.hours
3436 3447 assert_equal spent_hours_before + 2.5, issue.spent_hours
3437 3448 end
3438 3449
3439 3450 def test_put_update_should_preserve_parent_issue_even_if_not_visible
3440 3451 parent = Issue.generate!(:project_id => 1, :is_private => true)
3441 3452 issue = Issue.generate!(:parent_issue_id => parent.id)
3442 3453 assert !parent.visible?(User.find(3))
3443 3454 @request.session[:user_id] = 3
3444 3455
3445 3456 get :edit, :id => issue.id
3446 3457 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', parent.id.to_s
3447 3458
3448 3459 put :update, :id => issue.id, :issue => {:subject => 'New subject', :parent_issue_id => parent.id.to_s}
3449 3460 assert_response 302
3450 3461 assert_equal parent, issue.parent
3451 3462 end
3452 3463
3453 3464 def test_put_update_with_attachment_only
3454 3465 set_tmp_attachments_directory
3455 3466
3456 3467 # Delete all fixtured journals, a race condition can occur causing the wrong
3457 3468 # journal to get fetched in the next find.
3458 3469 Journal.delete_all
3459 3470
3460 3471 with_settings :notified_events => %w(issue_updated) do
3461 3472 # anonymous user
3462 3473 assert_difference 'Attachment.count' do
3463 3474 put :update, :id => 1,
3464 3475 :issue => {:notes => ''},
3465 3476 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3466 3477 end
3467 3478 end
3468 3479
3469 3480 assert_redirected_to :action => 'show', :id => '1'
3470 3481 j = Issue.find(1).journals.reorder('id DESC').first
3471 3482 assert j.notes.blank?
3472 3483 assert_equal 1, j.details.size
3473 3484 assert_equal 'testfile.txt', j.details.first.value
3474 3485 assert_equal User.anonymous, j.user
3475 3486
3476 3487 attachment = Attachment.order('id DESC').first
3477 3488 assert_equal Issue.find(1), attachment.container
3478 3489 assert_equal User.anonymous, attachment.author
3479 3490 assert_equal 'testfile.txt', attachment.filename
3480 3491 assert_equal 'text/plain', attachment.content_type
3481 3492 assert_equal 'test file', attachment.description
3482 3493 assert_equal 59, attachment.filesize
3483 3494 assert File.exists?(attachment.diskfile)
3484 3495 assert_equal 59, File.size(attachment.diskfile)
3485 3496
3486 3497 mail = ActionMailer::Base.deliveries.last
3487 3498 assert_mail_body_match 'testfile.txt', mail
3488 3499 end
3489 3500
3490 3501 def test_put_update_with_failure_should_save_attachments
3491 3502 set_tmp_attachments_directory
3492 3503 @request.session[:user_id] = 2
3493 3504
3494 3505 assert_no_difference 'Journal.count' do
3495 3506 assert_difference 'Attachment.count' do
3496 3507 put :update, :id => 1,
3497 3508 :issue => { :subject => '' },
3498 3509 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3499 3510 assert_response :success
3500 3511 assert_template 'edit'
3501 3512 end
3502 3513 end
3503 3514
3504 3515 attachment = Attachment.order('id DESC').first
3505 3516 assert_equal 'testfile.txt', attachment.filename
3506 3517 assert File.exists?(attachment.diskfile)
3507 3518 assert_nil attachment.container
3508 3519
3509 3520 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3510 3521 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3511 3522 end
3512 3523
3513 3524 def test_put_update_with_failure_should_keep_saved_attachments
3514 3525 set_tmp_attachments_directory
3515 3526 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3516 3527 @request.session[:user_id] = 2
3517 3528
3518 3529 assert_no_difference 'Journal.count' do
3519 3530 assert_no_difference 'Attachment.count' do
3520 3531 put :update, :id => 1,
3521 3532 :issue => { :subject => '' },
3522 3533 :attachments => {'p0' => {'token' => attachment.token}}
3523 3534 assert_response :success
3524 3535 assert_template 'edit'
3525 3536 end
3526 3537 end
3527 3538
3528 3539 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3529 3540 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3530 3541 end
3531 3542
3532 3543 def test_put_update_should_attach_saved_attachments
3533 3544 set_tmp_attachments_directory
3534 3545 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3535 3546 @request.session[:user_id] = 2
3536 3547
3537 3548 assert_difference 'Journal.count' do
3538 3549 assert_difference 'JournalDetail.count' do
3539 3550 assert_no_difference 'Attachment.count' do
3540 3551 put :update, :id => 1,
3541 3552 :issue => {:notes => 'Attachment added'},
3542 3553 :attachments => {'p0' => {'token' => attachment.token}}
3543 3554 assert_redirected_to '/issues/1'
3544 3555 end
3545 3556 end
3546 3557 end
3547 3558
3548 3559 attachment.reload
3549 3560 assert_equal Issue.find(1), attachment.container
3550 3561
3551 3562 journal = Journal.order('id DESC').first
3552 3563 assert_equal 1, journal.details.size
3553 3564 assert_equal 'testfile.txt', journal.details.first.value
3554 3565 end
3555 3566
3556 3567 def test_put_update_with_attachment_that_fails_to_save
3557 3568 set_tmp_attachments_directory
3558 3569
3559 3570 # anonymous user
3560 3571 with_settings :attachment_max_size => 0 do
3561 3572 put :update,
3562 3573 :id => 1,
3563 3574 :issue => {:notes => ''},
3564 3575 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
3565 3576 assert_redirected_to :action => 'show', :id => '1'
3566 3577 assert_equal '1 file(s) could not be saved.', flash[:warning]
3567 3578 end
3568 3579 end
3569 3580
3570 3581 def test_put_update_with_no_change
3571 3582 issue = Issue.find(1)
3572 3583 issue.journals.clear
3573 3584 ActionMailer::Base.deliveries.clear
3574 3585
3575 3586 put :update,
3576 3587 :id => 1,
3577 3588 :issue => {:notes => ''}
3578 3589 assert_redirected_to :action => 'show', :id => '1'
3579 3590
3580 3591 issue.reload
3581 3592 assert issue.journals.empty?
3582 3593 # No email should be sent
3583 3594 assert ActionMailer::Base.deliveries.empty?
3584 3595 end
3585 3596
3586 3597 def test_put_update_should_send_a_notification
3587 3598 @request.session[:user_id] = 2
3588 3599 ActionMailer::Base.deliveries.clear
3589 3600 issue = Issue.find(1)
3590 3601 old_subject = issue.subject
3591 3602 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
3592 3603
3593 3604 with_settings :notified_events => %w(issue_updated) do
3594 3605 put :update, :id => 1, :issue => {:subject => new_subject,
3595 3606 :priority_id => '6',
3596 3607 :category_id => '1' # no change
3597 3608 }
3598 3609 assert_equal 1, ActionMailer::Base.deliveries.size
3599 3610 end
3600 3611 end
3601 3612
3602 3613 def test_put_update_with_invalid_spent_time_hours_only
3603 3614 @request.session[:user_id] = 2
3604 3615 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3605 3616
3606 3617 assert_no_difference('Journal.count') do
3607 3618 put :update,
3608 3619 :id => 1,
3609 3620 :issue => {:notes => notes},
3610 3621 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
3611 3622 end
3612 3623 assert_response :success
3613 3624 assert_template 'edit'
3614 3625
3615 3626 assert_select_error /Activity cannot be blank/
3616 3627 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3617 3628 assert_select 'input[name=?][value=?]', 'time_entry[hours]', '2z'
3618 3629 end
3619 3630
3620 3631 def test_put_update_with_invalid_spent_time_comments_only
3621 3632 @request.session[:user_id] = 2
3622 3633 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3623 3634
3624 3635 assert_no_difference('Journal.count') do
3625 3636 put :update,
3626 3637 :id => 1,
3627 3638 :issue => {:notes => notes},
3628 3639 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
3629 3640 end
3630 3641 assert_response :success
3631 3642 assert_template 'edit'
3632 3643
3633 3644 assert_select_error /Activity cannot be blank/
3634 3645 assert_select_error /Hours cannot be blank/
3635 3646 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3636 3647 assert_select 'input[name=?][value=?]', 'time_entry[comments]', 'this is my comment'
3637 3648 end
3638 3649
3639 3650 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
3640 3651 issue = Issue.find(2)
3641 3652 @request.session[:user_id] = 2
3642 3653
3643 3654 put :update,
3644 3655 :id => issue.id,
3645 3656 :issue => {
3646 3657 :fixed_version_id => 4
3647 3658 }
3648 3659
3649 3660 assert_response :redirect
3650 3661 issue.reload
3651 3662 assert_equal 4, issue.fixed_version_id
3652 3663 assert_not_equal issue.project_id, issue.fixed_version.project_id
3653 3664 end
3654 3665
3655 3666 def test_put_update_should_redirect_back_using_the_back_url_parameter
3656 3667 issue = Issue.find(2)
3657 3668 @request.session[:user_id] = 2
3658 3669
3659 3670 put :update,
3660 3671 :id => issue.id,
3661 3672 :issue => {
3662 3673 :fixed_version_id => 4
3663 3674 },
3664 3675 :back_url => '/issues'
3665 3676
3666 3677 assert_response :redirect
3667 3678 assert_redirected_to '/issues'
3668 3679 end
3669 3680
3670 3681 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3671 3682 issue = Issue.find(2)
3672 3683 @request.session[:user_id] = 2
3673 3684
3674 3685 put :update,
3675 3686 :id => issue.id,
3676 3687 :issue => {
3677 3688 :fixed_version_id => 4
3678 3689 },
3679 3690 :back_url => 'http://google.com'
3680 3691
3681 3692 assert_response :redirect
3682 3693 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
3683 3694 end
3695
3696 def test_put_update_should_redirect_with_previous_and_next_issue_ids_params
3697 @request.session[:user_id] = 2
3698
3699 put :update, :id => 11,
3700 :issue => {:status_id => 6, :notes => 'Notes'},
3701 :prev_issue_id => 8,
3702 :next_issue_id => 12,
3703 :issue_position => 2,
3704 :issue_count => 3
3705
3706 assert_redirected_to '/issues/11?issue_count=3&issue_position=2&next_issue_id=12&prev_issue_id=8'
3707 end
3684 3708
3685 3709 def test_get_bulk_edit
3686 3710 @request.session[:user_id] = 2
3687 3711 get :bulk_edit, :ids => [1, 3]
3688 3712 assert_response :success
3689 3713 assert_template 'bulk_edit'
3690 3714
3691 3715 assert_select 'ul#bulk-selection' do
3692 3716 assert_select 'li', 2
3693 3717 assert_select 'li a', :text => 'Bug #1'
3694 3718 end
3695 3719
3696 3720 assert_select 'form#bulk_edit_form[action=?]', '/issues/bulk_update' do
3697 3721 assert_select 'input[name=?]', 'ids[]', 2
3698 3722 assert_select 'input[name=?][value="1"][type=hidden]', 'ids[]'
3699 3723
3700 3724 assert_select 'select[name=?]', 'issue[project_id]'
3701 3725 assert_select 'input[name=?]', 'issue[parent_issue_id]'
3702 3726
3703 3727 # Project specific custom field, date type
3704 3728 field = CustomField.find(9)
3705 3729 assert !field.is_for_all?
3706 3730 assert_equal 'date', field.field_format
3707 3731 assert_select 'input[name=?]', 'issue[custom_field_values][9]'
3708 3732
3709 3733 # System wide custom field
3710 3734 assert CustomField.find(1).is_for_all?
3711 3735 assert_select 'select[name=?]', 'issue[custom_field_values][1]'
3712 3736
3713 3737 # Be sure we don't display inactive IssuePriorities
3714 3738 assert ! IssuePriority.find(15).active?
3715 3739 assert_select 'select[name=?]', 'issue[priority_id]' do
3716 3740 assert_select 'option[value="15"]', 0
3717 3741 end
3718 3742 end
3719 3743 end
3720 3744
3721 3745 def test_get_bulk_edit_on_different_projects
3722 3746 @request.session[:user_id] = 2
3723 3747 get :bulk_edit, :ids => [1, 2, 6]
3724 3748 assert_response :success
3725 3749 assert_template 'bulk_edit'
3726 3750
3727 3751 # Can not set issues from different projects as children of an issue
3728 3752 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
3729 3753
3730 3754 # Project specific custom field, date type
3731 3755 field = CustomField.find(9)
3732 3756 assert !field.is_for_all?
3733 3757 assert !field.project_ids.include?(Issue.find(6).project_id)
3734 3758 assert_select 'input[name=?]', 'issue[custom_field_values][9]', 0
3735 3759 end
3736 3760
3737 3761 def test_get_bulk_edit_with_user_custom_field
3738 3762 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :tracker_ids => [1,2,3])
3739 3763
3740 3764 @request.session[:user_id] = 2
3741 3765 get :bulk_edit, :ids => [1, 2]
3742 3766 assert_response :success
3743 3767 assert_template 'bulk_edit'
3744 3768
3745 3769 assert_select 'select.user_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3746 3770 assert_select 'option', Project.find(1).users.count + 2 # "no change" + "none" options
3747 3771 end
3748 3772 end
3749 3773
3750 3774 def test_get_bulk_edit_with_version_custom_field
3751 3775 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1,2,3])
3752 3776
3753 3777 @request.session[:user_id] = 2
3754 3778 get :bulk_edit, :ids => [1, 2]
3755 3779 assert_response :success
3756 3780 assert_template 'bulk_edit'
3757 3781
3758 3782 assert_select 'select.version_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3759 3783 assert_select 'option', Project.find(1).shared_versions.count + 2 # "no change" + "none" options
3760 3784 end
3761 3785 end
3762 3786
3763 3787 def test_get_bulk_edit_with_multi_custom_field
3764 3788 field = CustomField.find(1)
3765 3789 field.update_attribute :multiple, true
3766 3790
3767 3791 @request.session[:user_id] = 2
3768 3792 get :bulk_edit, :ids => [1, 3]
3769 3793 assert_response :success
3770 3794 assert_template 'bulk_edit'
3771 3795
3772 3796 assert_select 'select[name=?]', 'issue[custom_field_values][1][]' do
3773 3797 assert_select 'option', field.possible_values.size + 1 # "none" options
3774 3798 end
3775 3799 end
3776 3800
3777 3801 def test_bulk_edit_should_propose_to_clear_text_custom_fields
3778 3802 @request.session[:user_id] = 2
3779 3803 get :bulk_edit, :ids => [1, 3]
3780 3804 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', '__none__'
3781 3805 end
3782 3806
3783 3807 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
3784 3808 WorkflowTransition.delete_all
3785 3809 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3786 3810 :old_status_id => 1, :new_status_id => 1)
3787 3811 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3788 3812 :old_status_id => 1, :new_status_id => 3)
3789 3813 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3790 3814 :old_status_id => 1, :new_status_id => 4)
3791 3815 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3792 3816 :old_status_id => 2, :new_status_id => 1)
3793 3817 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3794 3818 :old_status_id => 2, :new_status_id => 3)
3795 3819 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3796 3820 :old_status_id => 2, :new_status_id => 5)
3797 3821 @request.session[:user_id] = 2
3798 3822 get :bulk_edit, :ids => [1, 2]
3799 3823
3800 3824 assert_response :success
3801 3825 statuses = assigns(:available_statuses)
3802 3826 assert_not_nil statuses
3803 3827 assert_equal [1, 3], statuses.map(&:id).sort
3804 3828
3805 3829 assert_select 'select[name=?]', 'issue[status_id]' do
3806 3830 assert_select 'option', 3 # 2 statuses + "no change" option
3807 3831 end
3808 3832 end
3809 3833
3810 3834 def test_bulk_edit_should_propose_target_project_open_shared_versions
3811 3835 @request.session[:user_id] = 2
3812 3836 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3813 3837 assert_response :success
3814 3838 assert_template 'bulk_edit'
3815 3839 assert_equal Project.find(1).shared_versions.open.to_a.sort, assigns(:versions).sort
3816 3840
3817 3841 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
3818 3842 assert_select 'option', :text => '2.0'
3819 3843 end
3820 3844 end
3821 3845
3822 3846 def test_bulk_edit_should_propose_target_project_categories
3823 3847 @request.session[:user_id] = 2
3824 3848 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3825 3849 assert_response :success
3826 3850 assert_template 'bulk_edit'
3827 3851 assert_equal Project.find(1).issue_categories.sort, assigns(:categories).sort
3828 3852
3829 3853 assert_select 'select[name=?]', 'issue[category_id]' do
3830 3854 assert_select 'option', :text => 'Recipes'
3831 3855 end
3832 3856 end
3833 3857
3834 3858 def test_bulk_edit_should_only_propose_issues_trackers_custom_fields
3835 3859 IssueCustomField.delete_all
3836 3860 field = IssueCustomField.generate!(:tracker_ids => [1], :is_for_all => true)
3837 3861 IssueCustomField.generate!(:tracker_ids => [2], :is_for_all => true)
3838 3862 @request.session[:user_id] = 2
3839 3863
3840 3864 issue_ids = Issue.where(:project_id => 1, :tracker_id => 1).limit(2).ids
3841 3865 get :bulk_edit, :ids => issue_ids
3842 3866 assert_equal [field], assigns(:custom_fields)
3843 3867 end
3844 3868
3845 3869 def test_bulk_update
3846 3870 @request.session[:user_id] = 2
3847 3871 # update issues priority
3848 3872 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3849 3873 :issue => {:priority_id => 7,
3850 3874 :assigned_to_id => '',
3851 3875 :custom_field_values => {'2' => ''}}
3852 3876
3853 3877 assert_response 302
3854 3878 # check that the issues were updated
3855 3879 assert_equal [7, 7], Issue.where(:id =>[1, 2]).collect {|i| i.priority.id}
3856 3880
3857 3881 issue = Issue.find(1)
3858 3882 journal = issue.journals.reorder('created_on DESC').first
3859 3883 assert_equal '125', issue.custom_value_for(2).value
3860 3884 assert_equal 'Bulk editing', journal.notes
3861 3885 assert_equal 1, journal.details.size
3862 3886 end
3863 3887
3864 3888 def test_bulk_update_with_group_assignee
3865 3889 group = Group.find(11)
3866 3890 project = Project.find(1)
3867 3891 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
3868 3892
3869 3893 @request.session[:user_id] = 2
3870 3894 # update issues assignee
3871 3895 with_settings :issue_group_assignment => '1' do
3872 3896 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3873 3897 :issue => {:priority_id => '',
3874 3898 :assigned_to_id => group.id,
3875 3899 :custom_field_values => {'2' => ''}}
3876 3900
3877 3901 assert_response 302
3878 3902 assert_equal [group, group], Issue.where(:id => [1, 2]).collect {|i| i.assigned_to}
3879 3903 end
3880 3904 end
3881 3905
3882 3906 def test_bulk_update_on_different_projects
3883 3907 @request.session[:user_id] = 2
3884 3908 # update issues priority
3885 3909 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
3886 3910 :issue => {:priority_id => 7,
3887 3911 :assigned_to_id => '',
3888 3912 :custom_field_values => {'2' => ''}}
3889 3913
3890 3914 assert_response 302
3891 3915 # check that the issues were updated
3892 3916 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
3893 3917
3894 3918 issue = Issue.find(1)
3895 3919 journal = issue.journals.reorder('created_on DESC').first
3896 3920 assert_equal '125', issue.custom_value_for(2).value
3897 3921 assert_equal 'Bulk editing', journal.notes
3898 3922 assert_equal 1, journal.details.size
3899 3923 end
3900 3924
3901 3925 def test_bulk_update_on_different_projects_without_rights
3902 3926 @request.session[:user_id] = 3
3903 3927 user = User.find(3)
3904 3928 action = { :controller => "issues", :action => "bulk_update" }
3905 3929 assert user.allowed_to?(action, Issue.find(1).project)
3906 3930 assert ! user.allowed_to?(action, Issue.find(6).project)
3907 3931 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
3908 3932 :issue => {:priority_id => 7,
3909 3933 :assigned_to_id => '',
3910 3934 :custom_field_values => {'2' => ''}}
3911 3935 assert_response 403
3912 3936 assert_not_equal "Bulk should fail", Journal.last.notes
3913 3937 end
3914 3938
3915 3939 def test_bullk_update_should_send_a_notification
3916 3940 @request.session[:user_id] = 2
3917 3941 ActionMailer::Base.deliveries.clear
3918 3942 with_settings :notified_events => %w(issue_updated) do
3919 3943 post(:bulk_update,
3920 3944 {
3921 3945 :ids => [1, 2],
3922 3946 :notes => 'Bulk editing',
3923 3947 :issue => {
3924 3948 :priority_id => 7,
3925 3949 :assigned_to_id => '',
3926 3950 :custom_field_values => {'2' => ''}
3927 3951 }
3928 3952 })
3929 3953 assert_response 302
3930 3954 assert_equal 2, ActionMailer::Base.deliveries.size
3931 3955 end
3932 3956 end
3933 3957
3934 3958 def test_bulk_update_project
3935 3959 @request.session[:user_id] = 2
3936 3960 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
3937 3961 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3938 3962 # Issues moved to project 2
3939 3963 assert_equal 2, Issue.find(1).project_id
3940 3964 assert_equal 2, Issue.find(2).project_id
3941 3965 # No tracker change
3942 3966 assert_equal 1, Issue.find(1).tracker_id
3943 3967 assert_equal 2, Issue.find(2).tracker_id
3944 3968 end
3945 3969
3946 3970 def test_bulk_update_project_on_single_issue_should_follow_when_needed
3947 3971 @request.session[:user_id] = 2
3948 3972 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
3949 3973 assert_redirected_to '/issues/1'
3950 3974 end
3951 3975
3952 3976 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
3953 3977 @request.session[:user_id] = 2
3954 3978 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
3955 3979 assert_redirected_to '/projects/onlinestore/issues'
3956 3980 end
3957 3981
3958 3982 def test_bulk_update_tracker
3959 3983 @request.session[:user_id] = 2
3960 3984 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
3961 3985 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3962 3986 assert_equal 2, Issue.find(1).tracker_id
3963 3987 assert_equal 2, Issue.find(2).tracker_id
3964 3988 end
3965 3989
3966 3990 def test_bulk_update_status
3967 3991 @request.session[:user_id] = 2
3968 3992 # update issues priority
3969 3993 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
3970 3994 :issue => {:priority_id => '',
3971 3995 :assigned_to_id => '',
3972 3996 :status_id => '5'}
3973 3997
3974 3998 assert_response 302
3975 3999 issue = Issue.find(1)
3976 4000 assert issue.closed?
3977 4001 end
3978 4002
3979 4003 def test_bulk_update_priority
3980 4004 @request.session[:user_id] = 2
3981 4005 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3982 4006
3983 4007 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3984 4008 assert_equal 6, Issue.find(1).priority_id
3985 4009 assert_equal 6, Issue.find(2).priority_id
3986 4010 end
3987 4011
3988 4012 def test_bulk_update_with_notes
3989 4013 @request.session[:user_id] = 2
3990 4014 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
3991 4015
3992 4016 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3993 4017 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
3994 4018 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
3995 4019 end
3996 4020
3997 4021 def test_bulk_update_parent_id
3998 4022 IssueRelation.delete_all
3999 4023 @request.session[:user_id] = 2
4000 4024 post :bulk_update, :ids => [1, 3],
4001 4025 :notes => 'Bulk editing parent',
4002 4026 :issue => {:priority_id => '', :assigned_to_id => '',
4003 4027 :status_id => '', :parent_issue_id => '2'}
4004 4028 assert_response 302
4005 4029 parent = Issue.find(2)
4006 4030 assert_equal parent.id, Issue.find(1).parent_id
4007 4031 assert_equal parent.id, Issue.find(3).parent_id
4008 4032 assert_equal [1, 3], parent.children.collect(&:id).sort
4009 4033 end
4010 4034
4011 4035 def test_bulk_update_custom_field
4012 4036 @request.session[:user_id] = 2
4013 4037 # update issues priority
4014 4038 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
4015 4039 :issue => {:priority_id => '',
4016 4040 :assigned_to_id => '',
4017 4041 :custom_field_values => {'2' => '777'}}
4018 4042
4019 4043 assert_response 302
4020 4044
4021 4045 issue = Issue.find(1)
4022 4046 journal = issue.journals.reorder('created_on DESC').first
4023 4047 assert_equal '777', issue.custom_value_for(2).value
4024 4048 assert_equal 1, journal.details.size
4025 4049 assert_equal '125', journal.details.first.old_value
4026 4050 assert_equal '777', journal.details.first.value
4027 4051 end
4028 4052
4029 4053 def test_bulk_update_custom_field_to_blank
4030 4054 @request.session[:user_id] = 2
4031 4055 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing custom field',
4032 4056 :issue => {:priority_id => '',
4033 4057 :assigned_to_id => '',
4034 4058 :custom_field_values => {'1' => '__none__'}}
4035 4059 assert_response 302
4036 4060 assert_equal '', Issue.find(1).custom_field_value(1)
4037 4061 assert_equal '', Issue.find(3).custom_field_value(1)
4038 4062 end
4039 4063
4040 4064 def test_bulk_update_multi_custom_field
4041 4065 field = CustomField.find(1)
4042 4066 field.update_attribute :multiple, true
4043 4067
4044 4068 @request.session[:user_id] = 2
4045 4069 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
4046 4070 :issue => {:priority_id => '',
4047 4071 :assigned_to_id => '',
4048 4072 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
4049 4073
4050 4074 assert_response 302
4051 4075
4052 4076 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
4053 4077 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
4054 4078 # the custom field is not associated with the issue tracker
4055 4079 assert_nil Issue.find(2).custom_field_value(1)
4056 4080 end
4057 4081
4058 4082 def test_bulk_update_multi_custom_field_to_blank
4059 4083 field = CustomField.find(1)
4060 4084 field.update_attribute :multiple, true
4061 4085
4062 4086 @request.session[:user_id] = 2
4063 4087 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing multi custom field',
4064 4088 :issue => {:priority_id => '',
4065 4089 :assigned_to_id => '',
4066 4090 :custom_field_values => {'1' => ['__none__']}}
4067 4091 assert_response 302
4068 4092 assert_equal [''], Issue.find(1).custom_field_value(1)
4069 4093 assert_equal [''], Issue.find(3).custom_field_value(1)
4070 4094 end
4071 4095
4072 4096 def test_bulk_update_unassign
4073 4097 assert_not_nil Issue.find(2).assigned_to
4074 4098 @request.session[:user_id] = 2
4075 4099 # unassign issues
4076 4100 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
4077 4101 assert_response 302
4078 4102 # check that the issues were updated
4079 4103 assert_nil Issue.find(2).assigned_to
4080 4104 end
4081 4105
4082 4106 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
4083 4107 @request.session[:user_id] = 2
4084 4108
4085 4109 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
4086 4110
4087 4111 assert_response :redirect
4088 4112 issues = Issue.find([1,2])
4089 4113 issues.each do |issue|
4090 4114 assert_equal 4, issue.fixed_version_id
4091 4115 assert_not_equal issue.project_id, issue.fixed_version.project_id
4092 4116 end
4093 4117 end
4094 4118
4095 4119 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
4096 4120 @request.session[:user_id] = 2
4097 4121 post :bulk_update, :ids => [1,2], :back_url => '/issues'
4098 4122
4099 4123 assert_response :redirect
4100 4124 assert_redirected_to '/issues'
4101 4125 end
4102 4126
4103 4127 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
4104 4128 @request.session[:user_id] = 2
4105 4129 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
4106 4130
4107 4131 assert_response :redirect
4108 4132 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
4109 4133 end
4110 4134
4111 4135 def test_bulk_update_with_all_failures_should_show_errors
4112 4136 @request.session[:user_id] = 2
4113 4137 post :bulk_update, :ids => [1, 2], :issue => {:start_date => 'foo'}
4114 4138
4115 4139 assert_response :success
4116 4140 assert_template 'bulk_edit'
4117 4141 assert_select '#errorExplanation span', :text => 'Failed to save 2 issue(s) on 2 selected: #1, #2.'
4118 4142 assert_select '#errorExplanation ul li', :text => 'Start date is not a valid date: #1, #2'
4119 4143
4120 4144 assert_equal [1, 2], assigns[:issues].map(&:id)
4121 4145 end
4122 4146
4123 4147 def test_bulk_update_with_some_failures_should_show_errors
4124 4148 issue1 = Issue.generate!(:start_date => '2013-05-12')
4125 4149 issue2 = Issue.generate!(:start_date => '2013-05-15')
4126 4150 issue3 = Issue.generate!
4127 4151 @request.session[:user_id] = 2
4128 4152 post :bulk_update, :ids => [issue1.id, issue2.id, issue3.id],
4129 4153 :issue => {:due_date => '2013-05-01'}
4130 4154 assert_response :success
4131 4155 assert_template 'bulk_edit'
4132 4156 assert_select '#errorExplanation span',
4133 4157 :text => "Failed to save 2 issue(s) on 3 selected: ##{issue1.id}, ##{issue2.id}."
4134 4158 assert_select '#errorExplanation ul li',
4135 4159 :text => "Due date must be greater than start date: ##{issue1.id}, ##{issue2.id}"
4136 4160 assert_equal [issue1.id, issue2.id], assigns[:issues].map(&:id)
4137 4161 end
4138 4162
4139 4163 def test_bulk_update_with_failure_should_preserved_form_values
4140 4164 @request.session[:user_id] = 2
4141 4165 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2', :start_date => 'foo'}
4142 4166
4143 4167 assert_response :success
4144 4168 assert_template 'bulk_edit'
4145 4169 assert_select 'select[name=?]', 'issue[tracker_id]' do
4146 4170 assert_select 'option[value="2"][selected=selected]'
4147 4171 end
4148 4172 assert_select 'input[name=?][value=?]', 'issue[start_date]', 'foo'
4149 4173 end
4150 4174
4151 4175 def test_get_bulk_copy
4152 4176 @request.session[:user_id] = 2
4153 4177 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
4154 4178 assert_response :success
4155 4179 assert_template 'bulk_edit'
4156 4180
4157 4181 issues = assigns(:issues)
4158 4182 assert_not_nil issues
4159 4183 assert_equal [1, 2, 3], issues.map(&:id).sort
4160 4184
4161 4185 assert_select 'select[name=?]', 'issue[project_id]' do
4162 4186 assert_select 'option[value=""]'
4163 4187 end
4164 4188 assert_select 'input[name=copy_attachments]'
4165 4189 end
4166 4190
4167 4191 def test_get_bulk_copy_without_add_issues_permission_should_not_propose_current_project_as_target
4168 4192 user = setup_user_with_copy_but_not_add_permission
4169 4193 @request.session[:user_id] = user.id
4170 4194
4171 4195 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
4172 4196 assert_response :success
4173 4197 assert_template 'bulk_edit'
4174 4198
4175 4199 assert_select 'select[name=?]', 'issue[project_id]' do
4176 4200 assert_select 'option[value=""]', 0
4177 4201 assert_select 'option[value="2"]'
4178 4202 end
4179 4203 end
4180 4204
4181 4205 def test_bulk_copy_to_another_project
4182 4206 @request.session[:user_id] = 2
4183 4207 assert_difference 'Issue.count', 2 do
4184 4208 assert_no_difference 'Project.find(1).issues.count' do
4185 4209 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
4186 4210 end
4187 4211 end
4188 4212 assert_redirected_to '/projects/ecookbook/issues'
4189 4213
4190 4214 copies = Issue.order('id DESC').limit(issues.size)
4191 4215 copies.each do |copy|
4192 4216 assert_equal 2, copy.project_id
4193 4217 end
4194 4218 end
4195 4219
4196 4220 def test_bulk_copy_without_add_issues_permission_should_be_allowed_on_project_with_permission
4197 4221 user = setup_user_with_copy_but_not_add_permission
4198 4222 @request.session[:user_id] = user.id
4199 4223
4200 4224 assert_difference 'Issue.count', 3 do
4201 4225 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '2'}, :copy => '1'
4202 4226 assert_response 302
4203 4227 end
4204 4228 end
4205 4229
4206 4230 def test_bulk_copy_on_same_project_without_add_issues_permission_should_be_denied
4207 4231 user = setup_user_with_copy_but_not_add_permission
4208 4232 @request.session[:user_id] = user.id
4209 4233
4210 4234 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => ''}, :copy => '1'
4211 4235 assert_response 403
4212 4236 end
4213 4237
4214 4238 def test_bulk_copy_on_different_project_without_add_issues_permission_should_be_denied
4215 4239 user = setup_user_with_copy_but_not_add_permission
4216 4240 @request.session[:user_id] = user.id
4217 4241
4218 4242 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '1'}, :copy => '1'
4219 4243 assert_response 403
4220 4244 end
4221 4245
4222 4246 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
4223 4247 @request.session[:user_id] = 2
4224 4248 issues = [
4225 4249 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
4226 4250 :priority_id => 2, :subject => 'issue 1', :author_id => 1,
4227 4251 :assigned_to_id => nil),
4228 4252 Issue.create!(:project_id => 2, :tracker_id => 3, :status_id => 2,
4229 4253 :priority_id => 1, :subject => 'issue 2', :author_id => 2,
4230 4254 :assigned_to_id => 3)
4231 4255 ]
4232 4256 assert_difference 'Issue.count', issues.size do
4233 4257 post :bulk_update, :ids => issues.map(&:id), :copy => '1',
4234 4258 :issue => {
4235 4259 :project_id => '', :tracker_id => '', :assigned_to_id => '',
4236 4260 :status_id => '', :start_date => '', :due_date => ''
4237 4261 }
4238 4262 end
4239 4263
4240 4264 copies = Issue.order('id DESC').limit(issues.size)
4241 4265 issues.each do |orig|
4242 4266 copy = copies.detect {|c| c.subject == orig.subject}
4243 4267 assert_not_nil copy
4244 4268 assert_equal orig.project_id, copy.project_id
4245 4269 assert_equal orig.tracker_id, copy.tracker_id
4246 4270 assert_equal orig.status_id, copy.status_id
4247 4271 assert_equal orig.assigned_to_id, copy.assigned_to_id
4248 4272 assert_equal orig.priority_id, copy.priority_id
4249 4273 end
4250 4274 end
4251 4275
4252 4276 def test_bulk_copy_should_allow_changing_the_issue_attributes
4253 4277 # Fixes random test failure with Mysql
4254 4278 # where Issue.where(:project_id => 2).limit(2).order('id desc')
4255 4279 # doesn't return the expected results
4256 4280 Issue.delete_all("project_id=2")
4257 4281
4258 4282 @request.session[:user_id] = 2
4259 4283 assert_difference 'Issue.count', 2 do
4260 4284 assert_no_difference 'Project.find(1).issues.count' do
4261 4285 post :bulk_update, :ids => [1, 2], :copy => '1',
4262 4286 :issue => {
4263 4287 :project_id => '2', :tracker_id => '', :assigned_to_id => '2',
4264 4288 :status_id => '1', :start_date => '2009-12-01', :due_date => '2009-12-31'
4265 4289 }
4266 4290 end
4267 4291 end
4268 4292
4269 4293 copied_issues = Issue.where(:project_id => 2).limit(2).order('id desc').to_a
4270 4294 assert_equal 2, copied_issues.size
4271 4295 copied_issues.each do |issue|
4272 4296 assert_equal 2, issue.project_id, "Project is incorrect"
4273 4297 assert_equal 2, issue.assigned_to_id, "Assigned to is incorrect"
4274 4298 assert_equal 1, issue.status_id, "Status is incorrect"
4275 4299 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
4276 4300 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
4277 4301 end
4278 4302 end
4279 4303
4280 4304 def test_bulk_copy_should_allow_adding_a_note
4281 4305 @request.session[:user_id] = 2
4282 4306 assert_difference 'Issue.count', 1 do
4283 4307 post :bulk_update, :ids => [1], :copy => '1',
4284 4308 :notes => 'Copying one issue',
4285 4309 :issue => {
4286 4310 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
4287 4311 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
4288 4312 }
4289 4313 end
4290 4314 issue = Issue.order('id DESC').first
4291 4315 assert_equal 1, issue.journals.size
4292 4316 journal = issue.journals.first
4293 4317 assert_equal 'Copying one issue', journal.notes
4294 4318 end
4295 4319
4296 4320 def test_bulk_copy_should_allow_not_copying_the_attachments
4297 4321 attachment_count = Issue.find(3).attachments.size
4298 4322 assert attachment_count > 0
4299 4323 @request.session[:user_id] = 2
4300 4324
4301 4325 assert_difference 'Issue.count', 1 do
4302 4326 assert_no_difference 'Attachment.count' do
4303 4327 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '0',
4304 4328 :issue => {
4305 4329 :project_id => ''
4306 4330 }
4307 4331 end
4308 4332 end
4309 4333 end
4310 4334
4311 4335 def test_bulk_copy_should_allow_copying_the_attachments
4312 4336 attachment_count = Issue.find(3).attachments.size
4313 4337 assert attachment_count > 0
4314 4338 @request.session[:user_id] = 2
4315 4339
4316 4340 assert_difference 'Issue.count', 1 do
4317 4341 assert_difference 'Attachment.count', attachment_count do
4318 4342 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '1',
4319 4343 :issue => {
4320 4344 :project_id => ''
4321 4345 }
4322 4346 end
4323 4347 end
4324 4348 end
4325 4349
4326 4350 def test_bulk_copy_should_add_relations_with_copied_issues
4327 4351 @request.session[:user_id] = 2
4328 4352
4329 4353 assert_difference 'Issue.count', 2 do
4330 4354 assert_difference 'IssueRelation.count', 2 do
4331 4355 post :bulk_update, :ids => [1, 3], :copy => '1', :link_copy => '1',
4332 4356 :issue => {
4333 4357 :project_id => '1'
4334 4358 }
4335 4359 end
4336 4360 end
4337 4361 end
4338 4362
4339 4363 def test_bulk_copy_should_allow_not_copying_the_subtasks
4340 4364 issue = Issue.generate_with_descendants!
4341 4365 @request.session[:user_id] = 2
4342 4366
4343 4367 assert_difference 'Issue.count', 1 do
4344 4368 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '0',
4345 4369 :issue => {
4346 4370 :project_id => ''
4347 4371 }
4348 4372 end
4349 4373 end
4350 4374
4351 4375 def test_bulk_copy_should_allow_copying_the_subtasks
4352 4376 issue = Issue.generate_with_descendants!
4353 4377 count = issue.descendants.count
4354 4378 @request.session[:user_id] = 2
4355 4379
4356 4380 assert_difference 'Issue.count', count+1 do
4357 4381 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '1',
4358 4382 :issue => {
4359 4383 :project_id => ''
4360 4384 }
4361 4385 end
4362 4386 copy = Issue.where(:parent_id => nil).order("id DESC").first
4363 4387 assert_equal count, copy.descendants.count
4364 4388 end
4365 4389
4366 4390 def test_bulk_copy_should_not_copy_selected_subtasks_twice
4367 4391 issue = Issue.generate_with_descendants!
4368 4392 count = issue.descendants.count
4369 4393 @request.session[:user_id] = 2
4370 4394
4371 4395 assert_difference 'Issue.count', count+1 do
4372 4396 post :bulk_update, :ids => issue.self_and_descendants.map(&:id), :copy => '1', :copy_subtasks => '1',
4373 4397 :issue => {
4374 4398 :project_id => ''
4375 4399 }
4376 4400 end
4377 4401 copy = Issue.where(:parent_id => nil).order("id DESC").first
4378 4402 assert_equal count, copy.descendants.count
4379 4403 end
4380 4404
4381 4405 def test_bulk_copy_to_another_project_should_follow_when_needed
4382 4406 @request.session[:user_id] = 2
4383 4407 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
4384 4408 issue = Issue.order('id DESC').first
4385 4409 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
4386 4410 end
4387 4411
4388 4412 def test_bulk_copy_with_all_failures_should_display_errors
4389 4413 @request.session[:user_id] = 2
4390 4414 post :bulk_update, :ids => [1, 2], :copy => '1', :issue => {:start_date => 'foo'}
4391 4415
4392 4416 assert_response :success
4393 4417 end
4394 4418
4395 4419 def test_destroy_issue_with_no_time_entries
4396 4420 assert_nil TimeEntry.find_by_issue_id(2)
4397 4421 @request.session[:user_id] = 2
4398 4422
4399 4423 assert_difference 'Issue.count', -1 do
4400 4424 delete :destroy, :id => 2
4401 4425 end
4402 4426 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4403 4427 assert_nil Issue.find_by_id(2)
4404 4428 end
4405 4429
4406 4430 def test_destroy_issues_with_time_entries
4407 4431 @request.session[:user_id] = 2
4408 4432
4409 4433 assert_no_difference 'Issue.count' do
4410 4434 delete :destroy, :ids => [1, 3]
4411 4435 end
4412 4436 assert_response :success
4413 4437 assert_template 'destroy'
4414 4438 assert_not_nil assigns(:hours)
4415 4439 assert Issue.find_by_id(1) && Issue.find_by_id(3)
4416 4440
4417 4441 assert_select 'form' do
4418 4442 assert_select 'input[name=_method][value=delete]'
4419 4443 end
4420 4444 end
4421 4445
4422 4446 def test_destroy_issues_and_destroy_time_entries
4423 4447 @request.session[:user_id] = 2
4424 4448
4425 4449 assert_difference 'Issue.count', -2 do
4426 4450 assert_difference 'TimeEntry.count', -3 do
4427 4451 delete :destroy, :ids => [1, 3], :todo => 'destroy'
4428 4452 end
4429 4453 end
4430 4454 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4431 4455 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4432 4456 assert_nil TimeEntry.find_by_id([1, 2])
4433 4457 end
4434 4458
4435 4459 def test_destroy_issues_and_assign_time_entries_to_project
4436 4460 @request.session[:user_id] = 2
4437 4461
4438 4462 assert_difference 'Issue.count', -2 do
4439 4463 assert_no_difference 'TimeEntry.count' do
4440 4464 delete :destroy, :ids => [1, 3], :todo => 'nullify'
4441 4465 end
4442 4466 end
4443 4467 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4444 4468 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4445 4469 assert_nil TimeEntry.find(1).issue_id
4446 4470 assert_nil TimeEntry.find(2).issue_id
4447 4471 end
4448 4472
4449 4473 def test_destroy_issues_and_reassign_time_entries_to_another_issue
4450 4474 @request.session[:user_id] = 2
4451 4475
4452 4476 assert_difference 'Issue.count', -2 do
4453 4477 assert_no_difference 'TimeEntry.count' do
4454 4478 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
4455 4479 end
4456 4480 end
4457 4481 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4458 4482 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4459 4483 assert_equal 2, TimeEntry.find(1).issue_id
4460 4484 assert_equal 2, TimeEntry.find(2).issue_id
4461 4485 end
4462 4486
4463 4487 def test_destroy_issues_and_reassign_time_entries_to_an_invalid_issue_should_fail
4464 4488 @request.session[:user_id] = 2
4465 4489
4466 4490 assert_no_difference 'Issue.count' do
4467 4491 assert_no_difference 'TimeEntry.count' do
4468 4492 # try to reassign time to an issue of another project
4469 4493 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 4
4470 4494 end
4471 4495 end
4472 4496 assert_response :success
4473 4497 assert_template 'destroy'
4474 4498 end
4475 4499
4476 4500 def test_destroy_issues_from_different_projects
4477 4501 @request.session[:user_id] = 2
4478 4502
4479 4503 assert_difference 'Issue.count', -3 do
4480 4504 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
4481 4505 end
4482 4506 assert_redirected_to :controller => 'issues', :action => 'index'
4483 4507 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
4484 4508 end
4485 4509
4486 4510 def test_destroy_parent_and_child_issues
4487 4511 parent = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Parent Issue')
4488 4512 child = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Child Issue', :parent_issue_id => parent.id)
4489 4513 assert child.is_descendant_of?(parent.reload)
4490 4514
4491 4515 @request.session[:user_id] = 2
4492 4516 assert_difference 'Issue.count', -2 do
4493 4517 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
4494 4518 end
4495 4519 assert_response 302
4496 4520 end
4497 4521
4498 4522 def test_destroy_invalid_should_respond_with_404
4499 4523 @request.session[:user_id] = 2
4500 4524 assert_no_difference 'Issue.count' do
4501 4525 delete :destroy, :id => 999
4502 4526 end
4503 4527 assert_response 404
4504 4528 end
4505 4529
4506 4530 def test_default_search_scope
4507 4531 get :index
4508 4532
4509 4533 assert_select 'div#quick-search form' do
4510 4534 assert_select 'input[name=issues][value="1"][type=hidden]'
4511 4535 end
4512 4536 end
4513 4537
4514 4538 def setup_user_with_copy_but_not_add_permission
4515 4539 Role.all.each {|r| r.remove_permission! :add_issues}
4516 4540 Role.find_by_name('Manager').add_permission! :add_issues
4517 4541 user = User.generate!
4518 4542 User.add_to_project(user, Project.find(1), Role.find_by_name('Developer'))
4519 4543 User.add_to_project(user, Project.find(2), Role.find_by_name('Manager'))
4520 4544 user
4521 4545 end
4522 4546 end
General Comments 0
You need to be logged in to leave comments. Login now