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