##// END OF EJS Templates
Set default project version after selecting a different project on the new issue form (#1828)....
Jean-Philippe Lang -
r14406:fb8e348254a2
parent child
Show More
@@ -1,517 +1,522
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)
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 418 rescue ActiveRecord::RecordNotFound
419 419 render_404
420 420 return
421 421 end
422 422 end
423 423 @issue.project = @project
424 424 if request.get?
425 425 @issue.project ||= @issue.allowed_target_projects.first
426 426 end
427 427 @issue.author ||= User.current
428 428 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
429 429
430 430 attrs = (params[:issue] || {}).deep_dup
431 431 if action_name == 'new' && params[:was_default_status] == attrs[:status_id]
432 432 attrs.delete(:status_id)
433 433 end
434 if action_name == 'new' && params[:form_update_triggered_by] == 'issue_project_id'
435 # Discard submitted version when changing the project on the issue form
436 # so we can use the default version for the new project
437 attrs.delete(:fixed_version_id)
438 end
434 439 @issue.safe_attributes = attrs
435 440
436 441 if @issue.project
437 442 @issue.tracker ||= @issue.project.trackers.first
438 443 if @issue.tracker.nil?
439 444 render_error l(:error_no_tracker_in_project)
440 445 return false
441 446 end
442 447 if @issue.status.nil?
443 448 render_error l(:error_no_default_issue_status)
444 449 return false
445 450 end
446 451 end
447 452
448 453 @priorities = IssuePriority.active
449 454 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
450 455 end
451 456
452 457 def parse_params_for_bulk_issue_attributes(params)
453 458 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
454 459 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
455 460 if custom = attributes[:custom_field_values]
456 461 custom.reject! {|k,v| v.blank?}
457 462 custom.keys.each do |k|
458 463 if custom[k].is_a?(Array)
459 464 custom[k] << '' if custom[k].delete('__none__')
460 465 else
461 466 custom[k] = '' if custom[k] == '__none__'
462 467 end
463 468 end
464 469 end
465 470 attributes
466 471 end
467 472
468 473 # Saves @issue and a time_entry from the parameters
469 474 def save_issue_with_child_records
470 475 Issue.transaction do
471 476 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
472 477 time_entry = @time_entry || TimeEntry.new
473 478 time_entry.project = @issue.project
474 479 time_entry.issue = @issue
475 480 time_entry.user = User.current
476 481 time_entry.spent_on = User.current.today
477 482 time_entry.attributes = params[:time_entry]
478 483 @issue.time_entries << time_entry
479 484 end
480 485
481 486 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
482 487 if @issue.save
483 488 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
484 489 else
485 490 raise ActiveRecord::Rollback
486 491 end
487 492 end
488 493 end
489 494
490 495 # Returns true if the issue copy should be linked
491 496 # to the original issue
492 497 def link_copy?(param)
493 498 case Setting.link_copied_issue
494 499 when 'yes'
495 500 true
496 501 when 'no'
497 502 false
498 503 when 'ask'
499 504 param == '1'
500 505 end
501 506 end
502 507
503 508 # Redirects user after a successful issue creation
504 509 def redirect_after_create
505 510 if params[:continue]
506 511 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
507 512 if params[:project_id]
508 513 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
509 514 else
510 515 attrs.merge! :project_id => @issue.project_id
511 516 redirect_to new_issue_path(:issue => attrs)
512 517 end
513 518 else
514 519 redirect_to issue_path(@issue)
515 520 end
516 521 end
517 522 end
@@ -1,79 +1,79
1 1 <%= labelled_fields_for :issue, @issue do |f| %>
2 2
3 3 <div class="splitcontent">
4 4 <div class="splitcontentleft">
5 5 <% if @issue.safe_attribute?('status_id') && @allowed_statuses.present? %>
6 6 <p><%= f.select :status_id, (@allowed_statuses.collect {|p| [p.name, p.id]}), {:required => true},
7 :onchange => "updateIssueFrom('#{escape_javascript update_issue_form_path(@project, @issue)}')" %></p>
7 :onchange => "updateIssueFrom('#{escape_javascript update_issue_form_path(@project, @issue)}', this)" %></p>
8 8 <%= hidden_field_tag 'was_default_status', @issue.status_id, :id => nil if @issue.status == @issue.default_status %>
9 9 <% else %>
10 10 <p><label><%= l(:field_status) %></label> <%= @issue.status %></p>
11 11 <% end %>
12 12
13 13 <% if @issue.safe_attribute? 'priority_id' %>
14 14 <p><%= f.select :priority_id, (@priorities.collect {|p| [p.name, p.id]}), {:required => true} %></p>
15 15 <% end %>
16 16
17 17 <% if @issue.safe_attribute? 'assigned_to_id' %>
18 18 <p><%= f.select :assigned_to_id, principals_options_for_select(@issue.assignable_users, @issue.assigned_to), :include_blank => true, :required => @issue.required_attribute?('assigned_to_id') %></p>
19 19 <% end %>
20 20
21 21 <% if @issue.safe_attribute?('category_id') && @issue.project.issue_categories.any? %>
22 22 <p><%= f.select :category_id, (@issue.project.issue_categories.collect {|c| [c.name, c.id]}), :include_blank => true, :required => @issue.required_attribute?('category_id') %>
23 23 <%= link_to(image_tag('add.png', :style => 'vertical-align: middle;'),
24 24 new_project_issue_category_path(@issue.project),
25 25 :remote => true,
26 26 :method => 'get',
27 27 :title => l(:label_issue_category_new),
28 28 :tabindex => 200) if User.current.allowed_to?(:manage_categories, @issue.project) %></p>
29 29 <% end %>
30 30
31 31 <% if @issue.safe_attribute?('fixed_version_id') && @issue.assignable_versions.any? %>
32 32 <p><%= f.select :fixed_version_id, version_options_for_select(@issue.assignable_versions, @issue.fixed_version), :include_blank => true, :required => @issue.required_attribute?('fixed_version_id') %>
33 33 <%= link_to(image_tag('add.png', :style => 'vertical-align: middle;'),
34 34 new_project_version_path(@issue.project),
35 35 :remote => true,
36 36 :method => 'get',
37 37 :title => l(:label_version_new),
38 38 :tabindex => 200) if User.current.allowed_to?(:manage_versions, @issue.project) %>
39 39 </p>
40 40 <% end %>
41 41 </div>
42 42
43 43 <div class="splitcontentright">
44 44 <% if @issue.safe_attribute? 'parent_issue_id' %>
45 45 <p id="parent_issue"><%= f.text_field :parent_issue_id, :size => 10, :required => @issue.required_attribute?('parent_issue_id') %></p>
46 46 <%= javascript_tag "observeAutocompleteField('issue_parent_issue_id', '#{escape_javascript auto_complete_issues_path(:project_id => @issue.project, :scope => Setting.cross_project_subtasks)}')" %>
47 47 <% end %>
48 48
49 49 <% if @issue.safe_attribute? 'start_date' %>
50 50 <p id="start_date_area">
51 51 <%= f.text_field(:start_date, :size => 10, :required => @issue.required_attribute?('start_date')) %>
52 52 <%= calendar_for('issue_start_date') if @issue.leaf? %>
53 53 </p>
54 54 <% end %>
55 55
56 56 <% if @issue.safe_attribute? 'due_date' %>
57 57 <p id="due_date_area">
58 58 <%= f.text_field(:due_date, :size => 10, :required => @issue.required_attribute?('due_date')) %>
59 59 <%= calendar_for('issue_due_date') if @issue.leaf? %>
60 60 </p>
61 61 <% end %>
62 62
63 63 <% if @issue.safe_attribute? 'estimated_hours' %>
64 64 <p><%= f.text_field :estimated_hours, :size => 3, :required => @issue.required_attribute?('estimated_hours') %> <%= l(:field_hours) %></p>
65 65 <% end %>
66 66
67 67 <% if @issue.safe_attribute?('done_ratio') && Issue.use_field_for_done_ratio? %>
68 68 <p><%= f.select :done_ratio, ((0..10).to_a.collect {|r| ["#{r*10} %", r*10] }), :required => @issue.required_attribute?('done_ratio') %></p>
69 69 <% end %>
70 70 </div>
71 71 </div>
72 72
73 73 <% if @issue.safe_attribute? 'custom_field_values' %>
74 74 <%= render :partial => 'issues/form_custom_fields' %>
75 75 <% end %>
76 76
77 77 <% end %>
78 78
79 79 <% include_calendar_headers_tags %>
@@ -1,55 +1,56
1 1 <%= labelled_fields_for :issue, @issue do |f| %>
2 2 <%= call_hook(:view_issues_form_details_top, { :issue => @issue, :form => f }) %>
3 <%= hidden_field_tag 'form_update_triggered_by', '' %>
3 4
4 5 <% if @issue.safe_attribute? 'is_private' %>
5 6 <p id="issue_is_private_wrap">
6 7 <%= f.check_box :is_private, :no_label => true %><label class="inline" for="issue_is_private" id="issue_is_private_label"><%= l(:field_is_private) %></label>
7 8 </p>
8 9 <% end %>
9 10
10 11 <% if @issue.safe_attribute?('project_id') && (!@issue.new_record? || @project.nil? || @issue.copy?) %>
11 12 <p><%= f.select :project_id, project_tree_options_for_select(@issue.allowed_target_projects, :selected => @issue.project), {:required => true},
12 :onchange => "updateIssueFrom('#{escape_javascript update_issue_form_path(@project, @issue)}')" %></p>
13 :onchange => "updateIssueFrom('#{escape_javascript update_issue_form_path(@project, @issue)}', this)" %></p>
13 14 <% end %>
14 15
15 16 <% if @issue.safe_attribute? 'tracker_id' %>
16 17 <p><%= f.select :tracker_id, @issue.project.trackers.collect {|t| [t.name, t.id]}, {:required => true},
17 :onchange => "updateIssueFrom('#{escape_javascript update_issue_form_path(@project, @issue)}')" %></p>
18 :onchange => "updateIssueFrom('#{escape_javascript update_issue_form_path(@project, @issue)}', this)" %></p>
18 19 <% end %>
19 20
20 21 <% if @issue.safe_attribute? 'subject' %>
21 22 <p><%= f.text_field :subject, :size => 80, :maxlength => 255, :required => true %></p>
22 23 <% end %>
23 24
24 25 <% if @issue.safe_attribute? 'description' %>
25 26 <p>
26 27 <%= f.label_for_field :description, :required => @issue.required_attribute?('description') %>
27 28 <%= link_to_function content_tag(:span, l(:button_edit), :class => 'icon icon-edit'), '$(this).hide(); $("#issue_description_and_toolbar").show()' unless @issue.new_record? %>
28 29 <%= content_tag 'span', :id => "issue_description_and_toolbar", :style => (@issue.new_record? ? nil : 'display:none') do %>
29 30 <%= f.text_area :description,
30 31 :cols => 60,
31 32 :rows => (@issue.description.blank? ? 10 : [[10, @issue.description.length / 50].max, 100].min),
32 33 :accesskey => accesskey(:edit),
33 34 :class => 'wiki-edit',
34 35 :no_label => true %>
35 36 <% end %>
36 37 </p>
37 38 <%= wikitoolbar_for 'issue_description' %>
38 39 <% end %>
39 40
40 41 <div id="attributes" class="attributes">
41 42 <%= render :partial => 'issues/attributes' %>
42 43 </div>
43 44
44 45 <%= call_hook(:view_issues_form_details_bottom, { :issue => @issue, :form => f }) %>
45 46 <% end %>
46 47
47 48 <% heads_for_wiki_formatter %>
48 49
49 50 <%= javascript_tag do %>
50 51 $(document).ready(function(){
51 52 $("#issue_tracker_id, #issue_status_id").each(function(){
52 53 $(this).val($(this).find("option[selected=selected]").val());
53 54 });
54 55 });
55 56 <% end %>
@@ -1,668 +1,671
1 1 /* Redmine - project management software
2 2 Copyright (C) 2006-2015 Jean-Philippe Lang */
3 3
4 4 function checkAll(id, checked) {
5 5 $('#'+id).find('input[type=checkbox]:enabled').prop('checked', checked);
6 6 }
7 7
8 8 function toggleCheckboxesBySelector(selector) {
9 9 var all_checked = true;
10 10 $(selector).each(function(index) {
11 11 if (!$(this).is(':checked')) { all_checked = false; }
12 12 });
13 13 $(selector).prop('checked', !all_checked);
14 14 }
15 15
16 16 function showAndScrollTo(id, focus) {
17 17 $('#'+id).show();
18 18 if (focus !== null) {
19 19 $('#'+focus).focus();
20 20 }
21 21 $('html, body').animate({scrollTop: $('#'+id).offset().top}, 100);
22 22 }
23 23
24 24 function toggleRowGroup(el) {
25 25 var tr = $(el).parents('tr').first();
26 26 var n = tr.next();
27 27 tr.toggleClass('open');
28 28 while (n.length && !n.hasClass('group')) {
29 29 n.toggle();
30 30 n = n.next('tr');
31 31 }
32 32 }
33 33
34 34 function collapseAllRowGroups(el) {
35 35 var tbody = $(el).parents('tbody').first();
36 36 tbody.children('tr').each(function(index) {
37 37 if ($(this).hasClass('group')) {
38 38 $(this).removeClass('open');
39 39 } else {
40 40 $(this).hide();
41 41 }
42 42 });
43 43 }
44 44
45 45 function expandAllRowGroups(el) {
46 46 var tbody = $(el).parents('tbody').first();
47 47 tbody.children('tr').each(function(index) {
48 48 if ($(this).hasClass('group')) {
49 49 $(this).addClass('open');
50 50 } else {
51 51 $(this).show();
52 52 }
53 53 });
54 54 }
55 55
56 56 function toggleAllRowGroups(el) {
57 57 var tr = $(el).parents('tr').first();
58 58 if (tr.hasClass('open')) {
59 59 collapseAllRowGroups(el);
60 60 } else {
61 61 expandAllRowGroups(el);
62 62 }
63 63 }
64 64
65 65 function toggleFieldset(el) {
66 66 var fieldset = $(el).parents('fieldset').first();
67 67 fieldset.toggleClass('collapsed');
68 68 fieldset.children('div').toggle();
69 69 }
70 70
71 71 function hideFieldset(el) {
72 72 var fieldset = $(el).parents('fieldset').first();
73 73 fieldset.toggleClass('collapsed');
74 74 fieldset.children('div').hide();
75 75 }
76 76
77 77 // columns selection
78 78 function moveOptions(theSelFrom, theSelTo) {
79 79 $(theSelFrom).find('option:selected').detach().prop("selected", false).appendTo($(theSelTo));
80 80 }
81 81
82 82 function moveOptionUp(theSel) {
83 83 $(theSel).find('option:selected').each(function(){
84 84 $(this).prev(':not(:selected)').detach().insertAfter($(this));
85 85 });
86 86 }
87 87
88 88 function moveOptionTop(theSel) {
89 89 $(theSel).find('option:selected').detach().prependTo($(theSel));
90 90 }
91 91
92 92 function moveOptionDown(theSel) {
93 93 $($(theSel).find('option:selected').get().reverse()).each(function(){
94 94 $(this).next(':not(:selected)').detach().insertBefore($(this));
95 95 });
96 96 }
97 97
98 98 function moveOptionBottom(theSel) {
99 99 $(theSel).find('option:selected').detach().appendTo($(theSel));
100 100 }
101 101
102 102 function initFilters() {
103 103 $('#add_filter_select').change(function() {
104 104 addFilter($(this).val(), '', []);
105 105 });
106 106 $('#filters-table td.field input[type=checkbox]').each(function() {
107 107 toggleFilter($(this).val());
108 108 });
109 109 $('#filters-table').on('click', 'td.field input[type=checkbox]', function() {
110 110 toggleFilter($(this).val());
111 111 });
112 112 $('#filters-table').on('click', '.toggle-multiselect', function() {
113 113 toggleMultiSelect($(this).siblings('select'));
114 114 });
115 115 $('#filters-table').on('keypress', 'input[type=text]', function(e) {
116 116 if (e.keyCode == 13) $(this).closest('form').submit();
117 117 });
118 118 }
119 119
120 120 function addFilter(field, operator, values) {
121 121 var fieldId = field.replace('.', '_');
122 122 var tr = $('#tr_'+fieldId);
123 123 if (tr.length > 0) {
124 124 tr.show();
125 125 } else {
126 126 buildFilterRow(field, operator, values);
127 127 }
128 128 $('#cb_'+fieldId).prop('checked', true);
129 129 toggleFilter(field);
130 130 $('#add_filter_select').val('').find('option').each(function() {
131 131 if ($(this).attr('value') == field) {
132 132 $(this).attr('disabled', true);
133 133 }
134 134 });
135 135 }
136 136
137 137 function buildFilterRow(field, operator, values) {
138 138 var fieldId = field.replace('.', '_');
139 139 var filterTable = $("#filters-table");
140 140 var filterOptions = availableFilters[field];
141 141 if (!filterOptions) return;
142 142 var operators = operatorByType[filterOptions['type']];
143 143 var filterValues = filterOptions['values'];
144 144 var i, select;
145 145
146 146 var tr = $('<tr class="filter">').attr('id', 'tr_'+fieldId).html(
147 147 '<td class="field"><input checked="checked" id="cb_'+fieldId+'" name="f[]" value="'+field+'" type="checkbox"><label for="cb_'+fieldId+'"> '+filterOptions['name']+'</label></td>' +
148 148 '<td class="operator"><select id="operators_'+fieldId+'" name="op['+field+']"></td>' +
149 149 '<td class="values"></td>'
150 150 );
151 151 filterTable.append(tr);
152 152
153 153 select = tr.find('td.operator select');
154 154 for (i = 0; i < operators.length; i++) {
155 155 var option = $('<option>').val(operators[i]).text(operatorLabels[operators[i]]);
156 156 if (operators[i] == operator) { option.attr('selected', true); }
157 157 select.append(option);
158 158 }
159 159 select.change(function(){ toggleOperator(field); });
160 160
161 161 switch (filterOptions['type']) {
162 162 case "list":
163 163 case "list_optional":
164 164 case "list_status":
165 165 case "list_subprojects":
166 166 tr.find('td.values').append(
167 167 '<span style="display:none;"><select class="value" id="values_'+fieldId+'_1" name="v['+field+'][]"></select>' +
168 168 ' <span class="toggle-multiselect">&nbsp;</span></span>'
169 169 );
170 170 select = tr.find('td.values select');
171 171 if (values.length > 1) { select.attr('multiple', true); }
172 172 for (i = 0; i < filterValues.length; i++) {
173 173 var filterValue = filterValues[i];
174 174 var option = $('<option>');
175 175 if ($.isArray(filterValue)) {
176 176 option.val(filterValue[1]).text(filterValue[0]);
177 177 if ($.inArray(filterValue[1], values) > -1) {option.attr('selected', true);}
178 178 } else {
179 179 option.val(filterValue).text(filterValue);
180 180 if ($.inArray(filterValue, values) > -1) {option.attr('selected', true);}
181 181 }
182 182 select.append(option);
183 183 }
184 184 break;
185 185 case "date":
186 186 case "date_past":
187 187 tr.find('td.values').append(
188 188 '<span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'_1" size="10" class="value date_value" /></span>' +
189 189 ' <span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'_2" size="10" class="value date_value" /></span>' +
190 190 ' <span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'" size="3" class="value" /> '+labelDayPlural+'</span>'
191 191 );
192 192 $('#values_'+fieldId+'_1').val(values[0]).datepicker(datepickerOptions);
193 193 $('#values_'+fieldId+'_2').val(values[1]).datepicker(datepickerOptions);
194 194 $('#values_'+fieldId).val(values[0]);
195 195 break;
196 196 case "string":
197 197 case "text":
198 198 tr.find('td.values').append(
199 199 '<span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'" size="30" class="value" /></span>'
200 200 );
201 201 $('#values_'+fieldId).val(values[0]);
202 202 break;
203 203 case "relation":
204 204 tr.find('td.values').append(
205 205 '<span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'" size="6" class="value" /></span>' +
206 206 '<span style="display:none;"><select class="value" name="v['+field+'][]" id="values_'+fieldId+'_1"></select></span>'
207 207 );
208 208 $('#values_'+fieldId).val(values[0]);
209 209 select = tr.find('td.values select');
210 210 for (i = 0; i < allProjects.length; i++) {
211 211 var filterValue = allProjects[i];
212 212 var option = $('<option>');
213 213 option.val(filterValue[1]).text(filterValue[0]);
214 214 if (values[0] == filterValue[1]) { option.attr('selected', true); }
215 215 select.append(option);
216 216 }
217 217 break;
218 218 case "integer":
219 219 case "float":
220 220 case "tree":
221 221 tr.find('td.values').append(
222 222 '<span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'_1" size="6" class="value" /></span>' +
223 223 ' <span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'_2" size="6" class="value" /></span>'
224 224 );
225 225 $('#values_'+fieldId+'_1').val(values[0]);
226 226 $('#values_'+fieldId+'_2').val(values[1]);
227 227 break;
228 228 }
229 229 }
230 230
231 231 function toggleFilter(field) {
232 232 var fieldId = field.replace('.', '_');
233 233 if ($('#cb_' + fieldId).is(':checked')) {
234 234 $("#operators_" + fieldId).show().removeAttr('disabled');
235 235 toggleOperator(field);
236 236 } else {
237 237 $("#operators_" + fieldId).hide().attr('disabled', true);
238 238 enableValues(field, []);
239 239 }
240 240 }
241 241
242 242 function enableValues(field, indexes) {
243 243 var fieldId = field.replace('.', '_');
244 244 $('#tr_'+fieldId+' td.values .value').each(function(index) {
245 245 if ($.inArray(index, indexes) >= 0) {
246 246 $(this).removeAttr('disabled');
247 247 $(this).parents('span').first().show();
248 248 } else {
249 249 $(this).val('');
250 250 $(this).attr('disabled', true);
251 251 $(this).parents('span').first().hide();
252 252 }
253 253
254 254 if ($(this).hasClass('group')) {
255 255 $(this).addClass('open');
256 256 } else {
257 257 $(this).show();
258 258 }
259 259 });
260 260 }
261 261
262 262 function toggleOperator(field) {
263 263 var fieldId = field.replace('.', '_');
264 264 var operator = $("#operators_" + fieldId);
265 265 switch (operator.val()) {
266 266 case "!*":
267 267 case "*":
268 268 case "t":
269 269 case "ld":
270 270 case "w":
271 271 case "lw":
272 272 case "l2w":
273 273 case "m":
274 274 case "lm":
275 275 case "y":
276 276 case "o":
277 277 case "c":
278 278 enableValues(field, []);
279 279 break;
280 280 case "><":
281 281 enableValues(field, [0,1]);
282 282 break;
283 283 case "<t+":
284 284 case ">t+":
285 285 case "><t+":
286 286 case "t+":
287 287 case ">t-":
288 288 case "<t-":
289 289 case "><t-":
290 290 case "t-":
291 291 enableValues(field, [2]);
292 292 break;
293 293 case "=p":
294 294 case "=!p":
295 295 case "!p":
296 296 enableValues(field, [1]);
297 297 break;
298 298 default:
299 299 enableValues(field, [0]);
300 300 break;
301 301 }
302 302 }
303 303
304 304 function toggleMultiSelect(el) {
305 305 if (el.attr('multiple')) {
306 306 el.removeAttr('multiple');
307 307 el.attr('size', 1);
308 308 } else {
309 309 el.attr('multiple', true);
310 310 if (el.children().length > 10)
311 311 el.attr('size', 10);
312 312 else
313 313 el.attr('size', 4);
314 314 }
315 315 }
316 316
317 317 function showTab(name, url) {
318 318 $('#tab-content-' + name).parent().find('.tab-content').hide();
319 319 $('#tab-content-' + name).parent().find('div.tabs a').removeClass('selected');
320 320 $('#tab-content-' + name).show();
321 321 $('#tab-' + name).addClass('selected');
322 322 //replaces current URL with the "href" attribute of the current link
323 323 //(only triggered if supported by browser)
324 324 if ("replaceState" in window.history) {
325 325 window.history.replaceState(null, document.title, url);
326 326 }
327 327 return false;
328 328 }
329 329
330 330 function moveTabRight(el) {
331 331 var lis = $(el).parents('div.tabs').first().find('ul').children();
332 332 var tabsWidth = 0;
333 333 var i = 0;
334 334 lis.each(function() {
335 335 if ($(this).is(':visible')) {
336 336 tabsWidth += $(this).width() + 6;
337 337 }
338 338 });
339 339 if (tabsWidth < $(el).parents('div.tabs').first().width() - 60) { return; }
340 340 while (i<lis.length && !lis.eq(i).is(':visible')) { i++; }
341 341 lis.eq(i).hide();
342 342 }
343 343
344 344 function moveTabLeft(el) {
345 345 var lis = $(el).parents('div.tabs').first().find('ul').children();
346 346 var i = 0;
347 347 while (i < lis.length && !lis.eq(i).is(':visible')) { i++; }
348 348 if (i > 0) {
349 349 lis.eq(i-1).show();
350 350 }
351 351 }
352 352
353 353 function displayTabsButtons() {
354 354 var lis;
355 355 var tabsWidth;
356 356 var el;
357 357 $('div.tabs').each(function() {
358 358 el = $(this);
359 359 lis = el.find('ul').children();
360 360 tabsWidth = 0;
361 361 lis.each(function(){
362 362 if ($(this).is(':visible')) {
363 363 tabsWidth += $(this).width() + 6;
364 364 }
365 365 });
366 366 if ((tabsWidth < el.width() - 60) && (lis.first().is(':visible'))) {
367 367 el.find('div.tabs-buttons').hide();
368 368 } else {
369 369 el.find('div.tabs-buttons').show();
370 370 }
371 371 });
372 372 }
373 373
374 374 function setPredecessorFieldsVisibility() {
375 375 var relationType = $('#relation_relation_type');
376 376 if (relationType.val() == "precedes" || relationType.val() == "follows") {
377 377 $('#predecessor_fields').show();
378 378 } else {
379 379 $('#predecessor_fields').hide();
380 380 }
381 381 }
382 382
383 383 function showModal(id, width, title) {
384 384 var el = $('#'+id).first();
385 385 if (el.length === 0 || el.is(':visible')) {return;}
386 386 if (!title) title = el.find('h3.title').text();
387 387 // moves existing modals behind the transparent background
388 388 $(".modal").zIndex(99);
389 389 el.dialog({
390 390 width: width,
391 391 modal: true,
392 392 resizable: false,
393 393 dialogClass: 'modal',
394 394 title: title
395 395 }).on('dialogclose', function(){
396 396 $(".modal").zIndex(101);
397 397 });
398 398 el.find("input[type=text], input[type=submit]").first().focus();
399 399 }
400 400
401 401 function hideModal(el) {
402 402 var modal;
403 403 if (el) {
404 404 modal = $(el).parents('.ui-dialog-content');
405 405 } else {
406 406 modal = $('#ajax-modal');
407 407 }
408 408 modal.dialog("close");
409 409 }
410 410
411 411 function submitPreview(url, form, target) {
412 412 $.ajax({
413 413 url: url,
414 414 type: 'post',
415 415 data: $('#'+form).serialize(),
416 416 success: function(data){
417 417 $('#'+target).html(data);
418 418 }
419 419 });
420 420 }
421 421
422 422 function collapseScmEntry(id) {
423 423 $('.'+id).each(function() {
424 424 if ($(this).hasClass('open')) {
425 425 collapseScmEntry($(this).attr('id'));
426 426 }
427 427 $(this).hide();
428 428 });
429 429 $('#'+id).removeClass('open');
430 430 }
431 431
432 432 function expandScmEntry(id) {
433 433 $('.'+id).each(function() {
434 434 $(this).show();
435 435 if ($(this).hasClass('loaded') && !$(this).hasClass('collapsed')) {
436 436 expandScmEntry($(this).attr('id'));
437 437 }
438 438 });
439 439 $('#'+id).addClass('open');
440 440 }
441 441
442 442 function scmEntryClick(id, url) {
443 443 var el = $('#'+id);
444 444 if (el.hasClass('open')) {
445 445 collapseScmEntry(id);
446 446 el.addClass('collapsed');
447 447 return false;
448 448 } else if (el.hasClass('loaded')) {
449 449 expandScmEntry(id);
450 450 el.removeClass('collapsed');
451 451 return false;
452 452 }
453 453 if (el.hasClass('loading')) {
454 454 return false;
455 455 }
456 456 el.addClass('loading');
457 457 $.ajax({
458 458 url: url,
459 459 success: function(data) {
460 460 el.after(data);
461 461 el.addClass('open').addClass('loaded').removeClass('loading');
462 462 }
463 463 });
464 464 return true;
465 465 }
466 466
467 467 function randomKey(size) {
468 468 var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
469 469 var key = '';
470 470 for (var i = 0; i < size; i++) {
471 471 key += chars.charAt(Math.floor(Math.random() * chars.length));
472 472 }
473 473 return key;
474 474 }
475 475
476 function updateIssueFrom(url) {
476 function updateIssueFrom(url, el) {
477 477 $('#all_attributes input, #all_attributes textarea, #all_attributes select').each(function(){
478 478 $(this).data('valuebeforeupdate', $(this).val());
479 479 });
480 if (el) {
481 $("#form_update_triggered_by").val($(el).attr('id'));
482 }
480 483 return $.ajax({
481 484 url: url,
482 485 type: 'post',
483 486 data: $('#issue-form').serialize()
484 487 });
485 488 }
486 489
487 490 function replaceIssueFormWith(html){
488 491 var replacement = $(html);
489 492 $('#all_attributes input, #all_attributes textarea, #all_attributes select').each(function(){
490 493 var object_id = $(this).attr('id');
491 494 if (object_id && $(this).data('valuebeforeupdate')!=$(this).val()) {
492 495 replacement.find('#'+object_id).val($(this).val());
493 496 }
494 497 });
495 498 $('#all_attributes').empty();
496 499 $('#all_attributes').prepend(replacement);
497 500 }
498 501
499 502 function updateBulkEditFrom(url) {
500 503 $.ajax({
501 504 url: url,
502 505 type: 'post',
503 506 data: $('#bulk_edit_form').serialize()
504 507 });
505 508 }
506 509
507 510 function observeAutocompleteField(fieldId, url, options) {
508 511 $(document).ready(function() {
509 512 $('#'+fieldId).autocomplete($.extend({
510 513 source: url,
511 514 minLength: 2,
512 515 search: function(){$('#'+fieldId).addClass('ajax-loading');},
513 516 response: function(){$('#'+fieldId).removeClass('ajax-loading');}
514 517 }, options));
515 518 $('#'+fieldId).addClass('autocomplete');
516 519 });
517 520 }
518 521
519 522 function observeSearchfield(fieldId, targetId, url) {
520 523 $('#'+fieldId).each(function() {
521 524 var $this = $(this);
522 525 $this.addClass('autocomplete');
523 526 $this.attr('data-value-was', $this.val());
524 527 var check = function() {
525 528 var val = $this.val();
526 529 if ($this.attr('data-value-was') != val){
527 530 $this.attr('data-value-was', val);
528 531 $.ajax({
529 532 url: url,
530 533 type: 'get',
531 534 data: {q: $this.val()},
532 535 success: function(data){ if(targetId) $('#'+targetId).html(data); },
533 536 beforeSend: function(){ $this.addClass('ajax-loading'); },
534 537 complete: function(){ $this.removeClass('ajax-loading'); }
535 538 });
536 539 }
537 540 };
538 541 var reset = function() {
539 542 if (timer) {
540 543 clearInterval(timer);
541 544 timer = setInterval(check, 300);
542 545 }
543 546 };
544 547 var timer = setInterval(check, 300);
545 548 $this.bind('keyup click mousemove', reset);
546 549 });
547 550 }
548 551
549 552 function beforeShowDatePicker(input, inst) {
550 553 var default_date = null;
551 554 switch ($(input).attr("id")) {
552 555 case "issue_start_date" :
553 556 if ($("#issue_due_date").size() > 0) {
554 557 default_date = $("#issue_due_date").val();
555 558 }
556 559 break;
557 560 case "issue_due_date" :
558 561 if ($("#issue_start_date").size() > 0) {
559 562 default_date = $("#issue_start_date").val();
560 563 }
561 564 break;
562 565 }
563 566 $(input).datepicker("option", "defaultDate", default_date);
564 567 }
565 568
566 569 function initMyPageSortable(list, url) {
567 570 $('#list-'+list).sortable({
568 571 connectWith: '.block-receiver',
569 572 tolerance: 'pointer',
570 573 update: function(){
571 574 $.ajax({
572 575 url: url,
573 576 type: 'post',
574 577 data: {'blocks': $.map($('#list-'+list).children(), function(el){return $(el).attr('id');})}
575 578 });
576 579 }
577 580 });
578 581 $("#list-top, #list-left, #list-right").disableSelection();
579 582 }
580 583
581 584 var warnLeavingUnsavedMessage;
582 585 function warnLeavingUnsaved(message) {
583 586 warnLeavingUnsavedMessage = message;
584 587 $(document).on('submit', 'form', function(){
585 588 $('textarea').removeData('changed');
586 589 });
587 590 $(document).on('change', 'textarea', function(){
588 591 $(this).data('changed', 'changed');
589 592 });
590 593 window.onbeforeunload = function(){
591 594 var warn = false;
592 595 $('textarea').blur().each(function(){
593 596 if ($(this).data('changed')) {
594 597 warn = true;
595 598 }
596 599 });
597 600 if (warn) {return warnLeavingUnsavedMessage;}
598 601 };
599 602 }
600 603
601 604 function setupAjaxIndicator() {
602 605 $(document).bind('ajaxSend', function(event, xhr, settings) {
603 606 if ($('.ajax-loading').length === 0 && settings.contentType != 'application/octet-stream') {
604 607 $('#ajax-indicator').show();
605 608 }
606 609 });
607 610 $(document).bind('ajaxStop', function() {
608 611 $('#ajax-indicator').hide();
609 612 });
610 613 }
611 614
612 615 function hideOnLoad() {
613 616 $('.hol').hide();
614 617 }
615 618
616 619 function addFormObserversForDoubleSubmit() {
617 620 $('form[method=post]').each(function() {
618 621 if (!$(this).hasClass('multiple-submit')) {
619 622 $(this).submit(function(form_submission) {
620 623 if ($(form_submission.target).attr('data-submitted')) {
621 624 form_submission.preventDefault();
622 625 } else {
623 626 $(form_submission.target).attr('data-submitted', true);
624 627 }
625 628 });
626 629 }
627 630 });
628 631 }
629 632
630 633 function defaultFocus(){
631 634 if (($('#content :focus').length == 0) && (window.location.hash == '')) {
632 635 $('#content input[type=text], #content textarea').first().focus();
633 636 }
634 637 }
635 638
636 639 function blockEventPropagation(event) {
637 640 event.stopPropagation();
638 641 event.preventDefault();
639 642 }
640 643
641 644 function toggleDisabledOnChange() {
642 645 var checked = $(this).is(':checked');
643 646 $($(this).data('disables')).attr('disabled', checked);
644 647 $($(this).data('enables')).attr('disabled', !checked);
645 648 }
646 649 function toggleDisabledInit() {
647 650 $('input[data-disables], input[data-enables]').each(toggleDisabledOnChange);
648 651 }
649 652 $(document).ready(function(){
650 653 $('#content').on('change', 'input[data-disables], input[data-enables]', toggleDisabledOnChange);
651 654 toggleDisabledInit();
652 655 });
653 656
654 657 function keepAnchorOnSignIn(form){
655 658 var hash = decodeURIComponent(self.document.location.hash);
656 659 if (hash) {
657 660 if (hash.indexOf("#") === -1) {
658 661 hash = "#" + hash;
659 662 }
660 663 form.action = form.action + hash;
661 664 }
662 665 return true;
663 666 }
664 667
665 668 $(document).ready(setupAjaxIndicator);
666 669 $(document).ready(hideOnLoad);
667 670 $(document).ready(addFormObserversForDoubleSubmit);
668 671 $(document).ready(defaultFocus);
@@ -1,4450 +1,4466
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 header.include?(status)
542 542 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
543 543 assert issue_line.include?(str_big5)
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 'table.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 'td', :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 "td.cf_#{field.id}", :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 def test_update_form_for_new_issue_should_ignore_version_when_changing_project
1984 version = Version.generate!(:project_id => 1)
1985 Project.find(1).update_attribute :default_version_id, version.id
1986 @request.session[:user_id] = 2
1987
1988 xhr :post, :new, :issue => {:project_id => 1,
1989 :fixed_version_id => ''},
1990 :form_update_triggered_by => 'issue_project_id'
1991 assert_response :success
1992 assert_template 'new'
1993
1994 issue = assigns(:issue)
1995 assert_equal 1, issue.project_id
1996 assert_equal version, issue.fixed_version
1997 end
1998
1983 1999 def test_post_create
1984 2000 @request.session[:user_id] = 2
1985 2001 assert_difference 'Issue.count' do
1986 2002 assert_no_difference 'Journal.count' do
1987 2003 post :create, :project_id => 1,
1988 2004 :issue => {:tracker_id => 3,
1989 2005 :status_id => 2,
1990 2006 :subject => 'This is the test_new issue',
1991 2007 :description => 'This is the description',
1992 2008 :priority_id => 5,
1993 2009 :start_date => '2010-11-07',
1994 2010 :estimated_hours => '',
1995 2011 :custom_field_values => {'2' => 'Value for field 2'}}
1996 2012 end
1997 2013 end
1998 2014 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1999 2015
2000 2016 issue = Issue.find_by_subject('This is the test_new issue')
2001 2017 assert_not_nil issue
2002 2018 assert_equal 2, issue.author_id
2003 2019 assert_equal 3, issue.tracker_id
2004 2020 assert_equal 2, issue.status_id
2005 2021 assert_equal Date.parse('2010-11-07'), issue.start_date
2006 2022 assert_nil issue.estimated_hours
2007 2023 v = issue.custom_values.where(:custom_field_id => 2).first
2008 2024 assert_not_nil v
2009 2025 assert_equal 'Value for field 2', v.value
2010 2026 end
2011 2027
2012 2028 def test_post_new_with_group_assignment
2013 2029 group = Group.find(11)
2014 2030 project = Project.find(1)
2015 2031 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
2016 2032
2017 2033 with_settings :issue_group_assignment => '1' do
2018 2034 @request.session[:user_id] = 2
2019 2035 assert_difference 'Issue.count' do
2020 2036 post :create, :project_id => project.id,
2021 2037 :issue => {:tracker_id => 3,
2022 2038 :status_id => 1,
2023 2039 :subject => 'This is the test_new_with_group_assignment issue',
2024 2040 :assigned_to_id => group.id}
2025 2041 end
2026 2042 end
2027 2043 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2028 2044
2029 2045 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
2030 2046 assert_not_nil issue
2031 2047 assert_equal group, issue.assigned_to
2032 2048 end
2033 2049
2034 2050 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
2035 2051 with_settings :default_issue_start_date_to_creation_date => 0 do
2036 2052 @request.session[:user_id] = 2
2037 2053 assert_difference 'Issue.count' do
2038 2054 post :create, :project_id => 1,
2039 2055 :issue => {:tracker_id => 3,
2040 2056 :status_id => 2,
2041 2057 :subject => 'This is the test_new issue',
2042 2058 :description => 'This is the description',
2043 2059 :priority_id => 5,
2044 2060 :estimated_hours => '',
2045 2061 :custom_field_values => {'2' => 'Value for field 2'}}
2046 2062 end
2047 2063 assert_redirected_to :controller => 'issues', :action => 'show',
2048 2064 :id => Issue.last.id
2049 2065 issue = Issue.find_by_subject('This is the test_new issue')
2050 2066 assert_not_nil issue
2051 2067 assert_nil issue.start_date
2052 2068 end
2053 2069 end
2054 2070
2055 2071 def test_post_create_without_start_date_and_default_start_date_is_creation_date
2056 2072 with_settings :default_issue_start_date_to_creation_date => 1 do
2057 2073 @request.session[:user_id] = 2
2058 2074 assert_difference 'Issue.count' do
2059 2075 post :create, :project_id => 1,
2060 2076 :issue => {:tracker_id => 3,
2061 2077 :status_id => 2,
2062 2078 :subject => 'This is the test_new issue',
2063 2079 :description => 'This is the description',
2064 2080 :priority_id => 5,
2065 2081 :estimated_hours => '',
2066 2082 :custom_field_values => {'2' => 'Value for field 2'}}
2067 2083 end
2068 2084 assert_redirected_to :controller => 'issues', :action => 'show',
2069 2085 :id => Issue.last.id
2070 2086 issue = Issue.find_by_subject('This is the test_new issue')
2071 2087 assert_not_nil issue
2072 2088 assert_equal Date.today, issue.start_date
2073 2089 end
2074 2090 end
2075 2091
2076 2092 def test_post_create_and_continue
2077 2093 @request.session[:user_id] = 2
2078 2094 assert_difference 'Issue.count' do
2079 2095 post :create, :project_id => 1,
2080 2096 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
2081 2097 :continue => ''
2082 2098 end
2083 2099
2084 2100 issue = Issue.order('id DESC').first
2085 2101 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
2086 2102 assert_not_nil flash[:notice], "flash was not set"
2087 2103 assert_select_in flash[:notice],
2088 2104 'a[href=?][title=?]', "/issues/#{issue.id}", "This is first issue", :text => "##{issue.id}"
2089 2105 end
2090 2106
2091 2107 def test_post_create_without_custom_fields_param
2092 2108 @request.session[:user_id] = 2
2093 2109 assert_difference 'Issue.count' do
2094 2110 post :create, :project_id => 1,
2095 2111 :issue => {:tracker_id => 1,
2096 2112 :subject => 'This is the test_new issue',
2097 2113 :description => 'This is the description',
2098 2114 :priority_id => 5}
2099 2115 end
2100 2116 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2101 2117 end
2102 2118
2103 2119 def test_post_create_with_multi_custom_field
2104 2120 field = IssueCustomField.find_by_name('Database')
2105 2121 field.update_attribute(:multiple, true)
2106 2122
2107 2123 @request.session[:user_id] = 2
2108 2124 assert_difference 'Issue.count' do
2109 2125 post :create, :project_id => 1,
2110 2126 :issue => {:tracker_id => 1,
2111 2127 :subject => 'This is the test_new issue',
2112 2128 :description => 'This is the description',
2113 2129 :priority_id => 5,
2114 2130 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
2115 2131 end
2116 2132 assert_response 302
2117 2133 issue = Issue.order('id DESC').first
2118 2134 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
2119 2135 end
2120 2136
2121 2137 def test_post_create_with_empty_multi_custom_field
2122 2138 field = IssueCustomField.find_by_name('Database')
2123 2139 field.update_attribute(:multiple, true)
2124 2140
2125 2141 @request.session[:user_id] = 2
2126 2142 assert_difference 'Issue.count' do
2127 2143 post :create, :project_id => 1,
2128 2144 :issue => {:tracker_id => 1,
2129 2145 :subject => 'This is the test_new issue',
2130 2146 :description => 'This is the description',
2131 2147 :priority_id => 5,
2132 2148 :custom_field_values => {'1' => ['']}}
2133 2149 end
2134 2150 assert_response 302
2135 2151 issue = Issue.order('id DESC').first
2136 2152 assert_equal [''], issue.custom_field_value(1).sort
2137 2153 end
2138 2154
2139 2155 def test_post_create_with_multi_user_custom_field
2140 2156 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
2141 2157 :tracker_ids => [1], :is_for_all => true)
2142 2158
2143 2159 @request.session[:user_id] = 2
2144 2160 assert_difference 'Issue.count' do
2145 2161 post :create, :project_id => 1,
2146 2162 :issue => {:tracker_id => 1,
2147 2163 :subject => 'This is the test_new issue',
2148 2164 :description => 'This is the description',
2149 2165 :priority_id => 5,
2150 2166 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
2151 2167 end
2152 2168 assert_response 302
2153 2169 issue = Issue.order('id DESC').first
2154 2170 assert_equal ['2', '3'], issue.custom_field_value(field).sort
2155 2171 end
2156 2172
2157 2173 def test_post_create_with_required_custom_field_and_without_custom_fields_param
2158 2174 field = IssueCustomField.find_by_name('Database')
2159 2175 field.update_attribute(:is_required, true)
2160 2176
2161 2177 @request.session[:user_id] = 2
2162 2178 assert_no_difference 'Issue.count' do
2163 2179 post :create, :project_id => 1,
2164 2180 :issue => {:tracker_id => 1,
2165 2181 :subject => 'This is the test_new issue',
2166 2182 :description => 'This is the description',
2167 2183 :priority_id => 5}
2168 2184 end
2169 2185 assert_response :success
2170 2186 assert_template 'new'
2171 2187 issue = assigns(:issue)
2172 2188 assert_not_nil issue
2173 2189 assert_select_error /Database cannot be blank/
2174 2190 end
2175 2191
2176 2192 def test_create_should_validate_required_fields
2177 2193 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2178 2194 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2179 2195 WorkflowPermission.delete_all
2180 2196 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'required')
2181 2197 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2182 2198 @request.session[:user_id] = 2
2183 2199
2184 2200 assert_no_difference 'Issue.count' do
2185 2201 post :create, :project_id => 1, :issue => {
2186 2202 :tracker_id => 2,
2187 2203 :status_id => 1,
2188 2204 :subject => 'Test',
2189 2205 :start_date => '',
2190 2206 :due_date => '',
2191 2207 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ''}
2192 2208 }
2193 2209 assert_response :success
2194 2210 assert_template 'new'
2195 2211 end
2196 2212
2197 2213 assert_select_error /Due date cannot be blank/i
2198 2214 assert_select_error /Bar cannot be blank/i
2199 2215 end
2200 2216
2201 2217 def test_create_should_validate_required_list_fields
2202 2218 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'list', :is_for_all => true, :tracker_ids => [1, 2], :multiple => false, :possible_values => ['a', 'b'])
2203 2219 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'list', :is_for_all => true, :tracker_ids => [1, 2], :multiple => true, :possible_values => ['a', 'b'])
2204 2220 WorkflowPermission.delete_all
2205 2221 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf1.id.to_s, :rule => 'required')
2206 2222 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2207 2223 @request.session[:user_id] = 2
2208 2224
2209 2225 assert_no_difference 'Issue.count' do
2210 2226 post :create, :project_id => 1, :issue => {
2211 2227 :tracker_id => 2,
2212 2228 :status_id => 1,
2213 2229 :subject => 'Test',
2214 2230 :start_date => '',
2215 2231 :due_date => '',
2216 2232 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ['']}
2217 2233 }
2218 2234 assert_response :success
2219 2235 assert_template 'new'
2220 2236 end
2221 2237
2222 2238 assert_select_error /Foo cannot be blank/i
2223 2239 assert_select_error /Bar cannot be blank/i
2224 2240 end
2225 2241
2226 2242 def test_create_should_ignore_readonly_fields
2227 2243 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2228 2244 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2229 2245 WorkflowPermission.delete_all
2230 2246 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
2231 2247 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
2232 2248 @request.session[:user_id] = 2
2233 2249
2234 2250 assert_difference 'Issue.count' do
2235 2251 post :create, :project_id => 1, :issue => {
2236 2252 :tracker_id => 2,
2237 2253 :status_id => 1,
2238 2254 :subject => 'Test',
2239 2255 :start_date => '2012-07-14',
2240 2256 :due_date => '2012-07-16',
2241 2257 :custom_field_values => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
2242 2258 }
2243 2259 assert_response 302
2244 2260 end
2245 2261
2246 2262 issue = Issue.order('id DESC').first
2247 2263 assert_equal Date.parse('2012-07-14'), issue.start_date
2248 2264 assert_nil issue.due_date
2249 2265 assert_equal 'value1', issue.custom_field_value(cf1)
2250 2266 assert_nil issue.custom_field_value(cf2)
2251 2267 end
2252 2268
2253 2269 def test_post_create_with_watchers
2254 2270 @request.session[:user_id] = 2
2255 2271 ActionMailer::Base.deliveries.clear
2256 2272
2257 2273 with_settings :notified_events => %w(issue_added) do
2258 2274 assert_difference 'Watcher.count', 2 do
2259 2275 post :create, :project_id => 1,
2260 2276 :issue => {:tracker_id => 1,
2261 2277 :subject => 'This is a new issue with watchers',
2262 2278 :description => 'This is the description',
2263 2279 :priority_id => 5,
2264 2280 :watcher_user_ids => ['2', '3']}
2265 2281 end
2266 2282 end
2267 2283 issue = Issue.find_by_subject('This is a new issue with watchers')
2268 2284 assert_not_nil issue
2269 2285 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
2270 2286
2271 2287 # Watchers added
2272 2288 assert_equal [2, 3], issue.watcher_user_ids.sort
2273 2289 assert issue.watched_by?(User.find(3))
2274 2290 # Watchers notified
2275 2291 mail = ActionMailer::Base.deliveries.last
2276 2292 assert_not_nil mail
2277 2293 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
2278 2294 end
2279 2295
2280 2296 def test_post_create_subissue
2281 2297 @request.session[:user_id] = 2
2282 2298
2283 2299 assert_difference 'Issue.count' do
2284 2300 post :create, :project_id => 1,
2285 2301 :issue => {:tracker_id => 1,
2286 2302 :subject => 'This is a child issue',
2287 2303 :parent_issue_id => '2'}
2288 2304 assert_response 302
2289 2305 end
2290 2306 issue = Issue.order('id DESC').first
2291 2307 assert_equal Issue.find(2), issue.parent
2292 2308 end
2293 2309
2294 2310 def test_post_create_subissue_with_sharp_parent_id
2295 2311 @request.session[:user_id] = 2
2296 2312
2297 2313 assert_difference 'Issue.count' do
2298 2314 post :create, :project_id => 1,
2299 2315 :issue => {:tracker_id => 1,
2300 2316 :subject => 'This is a child issue',
2301 2317 :parent_issue_id => '#2'}
2302 2318 assert_response 302
2303 2319 end
2304 2320 issue = Issue.order('id DESC').first
2305 2321 assert_equal Issue.find(2), issue.parent
2306 2322 end
2307 2323
2308 2324 def test_post_create_subissue_with_non_visible_parent_id_should_not_validate
2309 2325 @request.session[:user_id] = 2
2310 2326
2311 2327 assert_no_difference 'Issue.count' do
2312 2328 post :create, :project_id => 1,
2313 2329 :issue => {:tracker_id => 1,
2314 2330 :subject => 'This is a child issue',
2315 2331 :parent_issue_id => '4'}
2316 2332
2317 2333 assert_response :success
2318 2334 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '4'
2319 2335 assert_select_error /Parent task is invalid/i
2320 2336 end
2321 2337 end
2322 2338
2323 2339 def test_post_create_subissue_with_non_numeric_parent_id_should_not_validate
2324 2340 @request.session[:user_id] = 2
2325 2341
2326 2342 assert_no_difference 'Issue.count' do
2327 2343 post :create, :project_id => 1,
2328 2344 :issue => {:tracker_id => 1,
2329 2345 :subject => 'This is a child issue',
2330 2346 :parent_issue_id => '01ABC'}
2331 2347
2332 2348 assert_response :success
2333 2349 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '01ABC'
2334 2350 assert_select_error /Parent task is invalid/i
2335 2351 end
2336 2352 end
2337 2353
2338 2354 def test_post_create_private
2339 2355 @request.session[:user_id] = 2
2340 2356
2341 2357 assert_difference 'Issue.count' do
2342 2358 post :create, :project_id => 1,
2343 2359 :issue => {:tracker_id => 1,
2344 2360 :subject => 'This is a private issue',
2345 2361 :is_private => '1'}
2346 2362 end
2347 2363 issue = Issue.order('id DESC').first
2348 2364 assert issue.is_private?
2349 2365 end
2350 2366
2351 2367 def test_post_create_private_with_set_own_issues_private_permission
2352 2368 role = Role.find(1)
2353 2369 role.remove_permission! :set_issues_private
2354 2370 role.add_permission! :set_own_issues_private
2355 2371
2356 2372 @request.session[:user_id] = 2
2357 2373
2358 2374 assert_difference 'Issue.count' do
2359 2375 post :create, :project_id => 1,
2360 2376 :issue => {:tracker_id => 1,
2361 2377 :subject => 'This is a private issue',
2362 2378 :is_private => '1'}
2363 2379 end
2364 2380 issue = Issue.order('id DESC').first
2365 2381 assert issue.is_private?
2366 2382 end
2367 2383
2368 2384 def test_create_without_project_id
2369 2385 @request.session[:user_id] = 2
2370 2386
2371 2387 assert_difference 'Issue.count' do
2372 2388 post :create,
2373 2389 :issue => {:project_id => 3,
2374 2390 :tracker_id => 2,
2375 2391 :subject => 'Foo'}
2376 2392 assert_response 302
2377 2393 end
2378 2394 issue = Issue.order('id DESC').first
2379 2395 assert_equal 3, issue.project_id
2380 2396 assert_equal 2, issue.tracker_id
2381 2397 end
2382 2398
2383 2399 def test_create_without_project_id_and_continue_should_redirect_without_project_id
2384 2400 @request.session[:user_id] = 2
2385 2401
2386 2402 assert_difference 'Issue.count' do
2387 2403 post :create,
2388 2404 :issue => {:project_id => 3,
2389 2405 :tracker_id => 2,
2390 2406 :subject => 'Foo'},
2391 2407 :continue => '1'
2392 2408 assert_redirected_to '/issues/new?issue%5Bproject_id%5D=3&issue%5Btracker_id%5D=2'
2393 2409 end
2394 2410 end
2395 2411
2396 2412 def test_create_without_project_id_should_be_denied_without_permission
2397 2413 Role.non_member.remove_permission! :add_issues
2398 2414 Role.anonymous.remove_permission! :add_issues
2399 2415 @request.session[:user_id] = 2
2400 2416
2401 2417 assert_no_difference 'Issue.count' do
2402 2418 post :create,
2403 2419 :issue => {:project_id => 3,
2404 2420 :tracker_id => 2,
2405 2421 :subject => 'Foo'}
2406 2422 assert_response 422
2407 2423 end
2408 2424 end
2409 2425
2410 2426 def test_create_without_project_id_with_failure
2411 2427 @request.session[:user_id] = 2
2412 2428
2413 2429 post :create,
2414 2430 :issue => {:project_id => 3,
2415 2431 :tracker_id => 2,
2416 2432 :subject => ''}
2417 2433 assert_response :success
2418 2434 assert_nil assigns(:project)
2419 2435 end
2420 2436
2421 2437 def test_post_create_should_send_a_notification
2422 2438 ActionMailer::Base.deliveries.clear
2423 2439 @request.session[:user_id] = 2
2424 2440 with_settings :notified_events => %w(issue_added) do
2425 2441 assert_difference 'Issue.count' do
2426 2442 post :create, :project_id => 1,
2427 2443 :issue => {:tracker_id => 3,
2428 2444 :subject => 'This is the test_new issue',
2429 2445 :description => 'This is the description',
2430 2446 :priority_id => 5,
2431 2447 :estimated_hours => '',
2432 2448 :custom_field_values => {'2' => 'Value for field 2'}}
2433 2449 end
2434 2450 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2435 2451
2436 2452 assert_equal 1, ActionMailer::Base.deliveries.size
2437 2453 end
2438 2454 end
2439 2455
2440 2456 def test_post_create_should_preserve_fields_values_on_validation_failure
2441 2457 @request.session[:user_id] = 2
2442 2458 post :create, :project_id => 1,
2443 2459 :issue => {:tracker_id => 1,
2444 2460 # empty subject
2445 2461 :subject => '',
2446 2462 :description => 'This is a description',
2447 2463 :priority_id => 6,
2448 2464 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
2449 2465 assert_response :success
2450 2466 assert_template 'new'
2451 2467
2452 2468 assert_select 'textarea[name=?]', 'issue[description]', :text => 'This is a description'
2453 2469 assert_select 'select[name=?]', 'issue[priority_id]' do
2454 2470 assert_select 'option[value="6"][selected=selected]', :text => 'High'
2455 2471 end
2456 2472 # Custom fields
2457 2473 assert_select 'select[name=?]', 'issue[custom_field_values][1]' do
2458 2474 assert_select 'option[value=Oracle][selected=selected]', :text => 'Oracle'
2459 2475 end
2460 2476 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Value for field 2'
2461 2477 end
2462 2478
2463 2479 def test_post_create_with_failure_should_preserve_watchers
2464 2480 assert !User.find(8).member_of?(Project.find(1))
2465 2481
2466 2482 @request.session[:user_id] = 2
2467 2483 post :create, :project_id => 1,
2468 2484 :issue => {:tracker_id => 1,
2469 2485 :watcher_user_ids => ['3', '8']}
2470 2486 assert_response :success
2471 2487 assert_template 'new'
2472 2488
2473 2489 assert_select 'input[name=?][value="2"]:not(checked)', 'issue[watcher_user_ids][]'
2474 2490 assert_select 'input[name=?][value="3"][checked=checked]', 'issue[watcher_user_ids][]'
2475 2491 assert_select 'input[name=?][value="8"][checked=checked]', 'issue[watcher_user_ids][]'
2476 2492 end
2477 2493
2478 2494 def test_post_create_should_ignore_non_safe_attributes
2479 2495 @request.session[:user_id] = 2
2480 2496 assert_nothing_raised do
2481 2497 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
2482 2498 end
2483 2499 end
2484 2500
2485 2501 def test_post_create_with_attachment
2486 2502 set_tmp_attachments_directory
2487 2503 @request.session[:user_id] = 2
2488 2504
2489 2505 assert_difference 'Issue.count' do
2490 2506 assert_difference 'Attachment.count' do
2491 2507 assert_no_difference 'Journal.count' do
2492 2508 post :create, :project_id => 1,
2493 2509 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2494 2510 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2495 2511 end
2496 2512 end
2497 2513 end
2498 2514
2499 2515 issue = Issue.order('id DESC').first
2500 2516 attachment = Attachment.order('id DESC').first
2501 2517
2502 2518 assert_equal issue, attachment.container
2503 2519 assert_equal 2, attachment.author_id
2504 2520 assert_equal 'testfile.txt', attachment.filename
2505 2521 assert_equal 'text/plain', attachment.content_type
2506 2522 assert_equal 'test file', attachment.description
2507 2523 assert_equal 59, attachment.filesize
2508 2524 assert File.exists?(attachment.diskfile)
2509 2525 assert_equal 59, File.size(attachment.diskfile)
2510 2526 end
2511 2527
2512 2528 def test_post_create_with_attachment_should_notify_with_attachments
2513 2529 ActionMailer::Base.deliveries.clear
2514 2530 set_tmp_attachments_directory
2515 2531 @request.session[:user_id] = 2
2516 2532
2517 2533 with_settings :host_name => 'mydomain.foo', :protocol => 'http', :notified_events => %w(issue_added) do
2518 2534 assert_difference 'Issue.count' do
2519 2535 post :create, :project_id => 1,
2520 2536 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2521 2537 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2522 2538 end
2523 2539 end
2524 2540
2525 2541 assert_not_nil ActionMailer::Base.deliveries.last
2526 2542 assert_select_email do
2527 2543 assert_select 'a[href^=?]', 'http://mydomain.foo/attachments/download', 'testfile.txt'
2528 2544 end
2529 2545 end
2530 2546
2531 2547 def test_post_create_with_failure_should_save_attachments
2532 2548 set_tmp_attachments_directory
2533 2549 @request.session[:user_id] = 2
2534 2550
2535 2551 assert_no_difference 'Issue.count' do
2536 2552 assert_difference 'Attachment.count' do
2537 2553 post :create, :project_id => 1,
2538 2554 :issue => { :tracker_id => '1', :subject => '' },
2539 2555 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2540 2556 assert_response :success
2541 2557 assert_template 'new'
2542 2558 end
2543 2559 end
2544 2560
2545 2561 attachment = Attachment.order('id DESC').first
2546 2562 assert_equal 'testfile.txt', attachment.filename
2547 2563 assert File.exists?(attachment.diskfile)
2548 2564 assert_nil attachment.container
2549 2565
2550 2566 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2551 2567 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2552 2568 end
2553 2569
2554 2570 def test_post_create_with_failure_should_keep_saved_attachments
2555 2571 set_tmp_attachments_directory
2556 2572 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2557 2573 @request.session[:user_id] = 2
2558 2574
2559 2575 assert_no_difference 'Issue.count' do
2560 2576 assert_no_difference 'Attachment.count' do
2561 2577 post :create, :project_id => 1,
2562 2578 :issue => { :tracker_id => '1', :subject => '' },
2563 2579 :attachments => {'p0' => {'token' => attachment.token}}
2564 2580 assert_response :success
2565 2581 assert_template 'new'
2566 2582 end
2567 2583 end
2568 2584
2569 2585 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2570 2586 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2571 2587 end
2572 2588
2573 2589 def test_post_create_should_attach_saved_attachments
2574 2590 set_tmp_attachments_directory
2575 2591 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2576 2592 @request.session[:user_id] = 2
2577 2593
2578 2594 assert_difference 'Issue.count' do
2579 2595 assert_no_difference 'Attachment.count' do
2580 2596 post :create, :project_id => 1,
2581 2597 :issue => { :tracker_id => '1', :subject => 'Saved attachments' },
2582 2598 :attachments => {'p0' => {'token' => attachment.token}}
2583 2599 assert_response 302
2584 2600 end
2585 2601 end
2586 2602
2587 2603 issue = Issue.order('id DESC').first
2588 2604 assert_equal 1, issue.attachments.count
2589 2605
2590 2606 attachment.reload
2591 2607 assert_equal issue, attachment.container
2592 2608 end
2593 2609
2594 2610 def setup_without_workflow_privilege
2595 2611 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2596 2612 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2597 2613 end
2598 2614 private :setup_without_workflow_privilege
2599 2615
2600 2616 test "without workflow privilege #new should propose default status only" do
2601 2617 setup_without_workflow_privilege
2602 2618 get :new, :project_id => 1
2603 2619 assert_response :success
2604 2620 assert_template 'new'
2605 2621
2606 2622 issue = assigns(:issue)
2607 2623 assert_not_nil issue.default_status
2608 2624
2609 2625 assert_select 'select[name=?]', 'issue[status_id]' do
2610 2626 assert_select 'option', 1
2611 2627 assert_select 'option[value=?]', issue.default_status.id.to_s
2612 2628 end
2613 2629 end
2614 2630
2615 2631 test "without workflow privilege #create should accept default status" do
2616 2632 setup_without_workflow_privilege
2617 2633 assert_difference 'Issue.count' do
2618 2634 post :create, :project_id => 1,
2619 2635 :issue => {:tracker_id => 1,
2620 2636 :subject => 'This is an issue',
2621 2637 :status_id => 1}
2622 2638 end
2623 2639 issue = Issue.order('id').last
2624 2640 assert_not_nil issue.default_status
2625 2641 assert_equal issue.default_status, issue.status
2626 2642 end
2627 2643
2628 2644 test "without workflow privilege #create should ignore unauthorized status" do
2629 2645 setup_without_workflow_privilege
2630 2646 assert_difference 'Issue.count' do
2631 2647 post :create, :project_id => 1,
2632 2648 :issue => {:tracker_id => 1,
2633 2649 :subject => 'This is an issue',
2634 2650 :status_id => 3}
2635 2651 end
2636 2652 issue = Issue.order('id').last
2637 2653 assert_not_nil issue.default_status
2638 2654 assert_equal issue.default_status, issue.status
2639 2655 end
2640 2656
2641 2657 test "without workflow privilege #update should ignore status change" do
2642 2658 setup_without_workflow_privilege
2643 2659 assert_difference 'Journal.count' do
2644 2660 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2645 2661 end
2646 2662 assert_equal 1, Issue.find(1).status_id
2647 2663 end
2648 2664
2649 2665 test "without workflow privilege #update ignore attributes changes" do
2650 2666 setup_without_workflow_privilege
2651 2667 assert_difference 'Journal.count' do
2652 2668 put :update, :id => 1,
2653 2669 :issue => {:subject => 'changed', :assigned_to_id => 2,
2654 2670 :notes => 'just trying'}
2655 2671 end
2656 2672 issue = Issue.find(1)
2657 2673 assert_equal "Cannot print recipes", issue.subject
2658 2674 assert_nil issue.assigned_to
2659 2675 end
2660 2676
2661 2677 def setup_with_workflow_privilege
2662 2678 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2663 2679 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2664 2680 :old_status_id => 1, :new_status_id => 3)
2665 2681 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2666 2682 :old_status_id => 1, :new_status_id => 4)
2667 2683 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2668 2684 end
2669 2685 private :setup_with_workflow_privilege
2670 2686
2671 2687 def setup_with_workflow_privilege_and_edit_issues_permission
2672 2688 setup_with_workflow_privilege
2673 2689 Role.anonymous.add_permission! :add_issues, :edit_issues
2674 2690 end
2675 2691 private :setup_with_workflow_privilege_and_edit_issues_permission
2676 2692
2677 2693 test "with workflow privilege and :edit_issues permission should accept authorized status" do
2678 2694 setup_with_workflow_privilege_and_edit_issues_permission
2679 2695 assert_difference 'Journal.count' do
2680 2696 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2681 2697 end
2682 2698 assert_equal 3, Issue.find(1).status_id
2683 2699 end
2684 2700
2685 2701 test "with workflow privilege and :edit_issues permission should ignore unauthorized status" do
2686 2702 setup_with_workflow_privilege_and_edit_issues_permission
2687 2703 assert_difference 'Journal.count' do
2688 2704 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2689 2705 end
2690 2706 assert_equal 1, Issue.find(1).status_id
2691 2707 end
2692 2708
2693 2709 test "with workflow privilege and :edit_issues permission should accept authorized attributes changes" do
2694 2710 setup_with_workflow_privilege_and_edit_issues_permission
2695 2711 assert_difference 'Journal.count' do
2696 2712 put :update, :id => 1,
2697 2713 :issue => {:subject => 'changed', :assigned_to_id => 2,
2698 2714 :notes => 'just trying'}
2699 2715 end
2700 2716 issue = Issue.find(1)
2701 2717 assert_equal "changed", issue.subject
2702 2718 assert_equal 2, issue.assigned_to_id
2703 2719 end
2704 2720
2705 2721 def test_new_as_copy
2706 2722 @request.session[:user_id] = 2
2707 2723 get :new, :project_id => 1, :copy_from => 1
2708 2724
2709 2725 assert_response :success
2710 2726 assert_template 'new'
2711 2727
2712 2728 assert_not_nil assigns(:issue)
2713 2729 orig = Issue.find(1)
2714 2730 assert_equal 1, assigns(:issue).project_id
2715 2731 assert_equal orig.subject, assigns(:issue).subject
2716 2732 assert assigns(:issue).copy?
2717 2733
2718 2734 assert_select 'form[id=issue-form][action="/projects/ecookbook/issues"]' do
2719 2735 assert_select 'select[name=?]', 'issue[project_id]' do
2720 2736 assert_select 'option[value="1"][selected=selected]', :text => 'eCookbook'
2721 2737 assert_select 'option[value="2"]:not([selected])', :text => 'OnlineStore'
2722 2738 end
2723 2739 assert_select 'input[name=copy_from][value="1"]'
2724 2740 end
2725 2741
2726 2742 # "New issue" menu item should not link to copy
2727 2743 assert_select '#main-menu a.new-issue[href="/projects/ecookbook/issues/new"]'
2728 2744 end
2729 2745
2730 2746 def test_new_as_copy_without_add_issues_permission_should_not_propose_current_project_as_target
2731 2747 user = setup_user_with_copy_but_not_add_permission
2732 2748
2733 2749 @request.session[:user_id] = user.id
2734 2750 get :new, :project_id => 1, :copy_from => 1
2735 2751
2736 2752 assert_response :success
2737 2753 assert_template 'new'
2738 2754 assert_select 'select[name=?]', 'issue[project_id]' do
2739 2755 assert_select 'option[value="1"]', 0
2740 2756 assert_select 'option[value="2"]', :text => 'OnlineStore'
2741 2757 end
2742 2758 end
2743 2759
2744 2760 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
2745 2761 @request.session[:user_id] = 2
2746 2762 issue = Issue.find(3)
2747 2763 assert issue.attachments.count > 0
2748 2764 get :new, :project_id => 1, :copy_from => 3
2749 2765
2750 2766 assert_select 'input[name=copy_attachments][type=checkbox][checked=checked][value="1"]'
2751 2767 end
2752 2768
2753 2769 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
2754 2770 @request.session[:user_id] = 2
2755 2771 issue = Issue.find(3)
2756 2772 issue.attachments.delete_all
2757 2773 get :new, :project_id => 1, :copy_from => 3
2758 2774
2759 2775 assert_select 'input[name=copy_attachments]', 0
2760 2776 end
2761 2777
2762 2778 def test_new_as_copy_with_subtasks_should_show_copy_subtasks_checkbox
2763 2779 @request.session[:user_id] = 2
2764 2780 issue = Issue.generate_with_descendants!
2765 2781 get :new, :project_id => 1, :copy_from => issue.id
2766 2782
2767 2783 assert_select 'input[type=checkbox][name=copy_subtasks][checked=checked][value="1"]'
2768 2784 end
2769 2785
2770 2786 def test_new_as_copy_with_invalid_issue_should_respond_with_404
2771 2787 @request.session[:user_id] = 2
2772 2788 get :new, :project_id => 1, :copy_from => 99999
2773 2789 assert_response 404
2774 2790 end
2775 2791
2776 2792 def test_create_as_copy_on_different_project
2777 2793 @request.session[:user_id] = 2
2778 2794 assert_difference 'Issue.count' do
2779 2795 post :create, :project_id => 1, :copy_from => 1,
2780 2796 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2781 2797
2782 2798 assert_not_nil assigns(:issue)
2783 2799 assert assigns(:issue).copy?
2784 2800 end
2785 2801 issue = Issue.order('id DESC').first
2786 2802 assert_redirected_to "/issues/#{issue.id}"
2787 2803
2788 2804 assert_equal 2, issue.project_id
2789 2805 assert_equal 3, issue.tracker_id
2790 2806 assert_equal 'Copy', issue.subject
2791 2807 end
2792 2808
2793 2809 def test_create_as_copy_should_allow_status_to_be_set_to_default
2794 2810 copied = Issue.generate! :status_id => 2
2795 2811 assert_equal 2, copied.reload.status_id
2796 2812
2797 2813 @request.session[:user_id] = 2
2798 2814 assert_difference 'Issue.count' do
2799 2815 post :create, :project_id => 1, :copy_from => copied.id,
2800 2816 :issue => {:project_id => '1', :tracker_id => '1', :status_id => '1'},
2801 2817 :was_default_status => '1'
2802 2818 end
2803 2819 issue = Issue.order('id DESC').first
2804 2820 assert_equal 1, issue.status_id
2805 2821 end
2806 2822
2807 2823 def test_create_as_copy_should_copy_attachments
2808 2824 @request.session[:user_id] = 2
2809 2825 issue = Issue.find(3)
2810 2826 count = issue.attachments.count
2811 2827 assert count > 0
2812 2828 assert_difference 'Issue.count' do
2813 2829 assert_difference 'Attachment.count', count do
2814 2830 post :create, :project_id => 1, :copy_from => 3,
2815 2831 :issue => {:project_id => '1', :tracker_id => '3',
2816 2832 :status_id => '1', :subject => 'Copy with attachments'},
2817 2833 :copy_attachments => '1'
2818 2834 end
2819 2835 end
2820 2836 copy = Issue.order('id DESC').first
2821 2837 assert_equal count, copy.attachments.count
2822 2838 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
2823 2839 end
2824 2840
2825 2841 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
2826 2842 @request.session[:user_id] = 2
2827 2843 issue = Issue.find(3)
2828 2844 count = issue.attachments.count
2829 2845 assert count > 0
2830 2846 assert_difference 'Issue.count' do
2831 2847 assert_no_difference 'Attachment.count' do
2832 2848 post :create, :project_id => 1, :copy_from => 3,
2833 2849 :issue => {:project_id => '1', :tracker_id => '3',
2834 2850 :status_id => '1', :subject => 'Copy with attachments'}
2835 2851 end
2836 2852 end
2837 2853 copy = Issue.order('id DESC').first
2838 2854 assert_equal 0, copy.attachments.count
2839 2855 end
2840 2856
2841 2857 def test_create_as_copy_with_attachments_should_also_add_new_files
2842 2858 @request.session[:user_id] = 2
2843 2859 issue = Issue.find(3)
2844 2860 count = issue.attachments.count
2845 2861 assert count > 0
2846 2862 assert_difference 'Issue.count' do
2847 2863 assert_difference 'Attachment.count', count + 1 do
2848 2864 post :create, :project_id => 1, :copy_from => 3,
2849 2865 :issue => {:project_id => '1', :tracker_id => '3',
2850 2866 :status_id => '1', :subject => 'Copy with attachments'},
2851 2867 :copy_attachments => '1',
2852 2868 :attachments => {'1' =>
2853 2869 {'file' => uploaded_test_file('testfile.txt', 'text/plain'),
2854 2870 'description' => 'test file'}}
2855 2871 end
2856 2872 end
2857 2873 copy = Issue.order('id DESC').first
2858 2874 assert_equal count + 1, copy.attachments.count
2859 2875 end
2860 2876
2861 2877 def test_create_as_copy_should_add_relation_with_copied_issue
2862 2878 @request.session[:user_id] = 2
2863 2879 assert_difference 'Issue.count' do
2864 2880 assert_difference 'IssueRelation.count' do
2865 2881 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2866 2882 :issue => {:project_id => '1', :tracker_id => '3',
2867 2883 :status_id => '1', :subject => 'Copy'}
2868 2884 end
2869 2885 end
2870 2886 copy = Issue.order('id DESC').first
2871 2887 assert_equal 1, copy.relations.size
2872 2888 end
2873 2889
2874 2890 def test_create_as_copy_should_allow_not_to_add_relation_with_copied_issue
2875 2891 @request.session[:user_id] = 2
2876 2892 assert_difference 'Issue.count' do
2877 2893 assert_no_difference 'IssueRelation.count' do
2878 2894 post :create, :project_id => 1, :copy_from => 1,
2879 2895 :issue => {:subject => 'Copy'}
2880 2896 end
2881 2897 end
2882 2898 end
2883 2899
2884 2900 def test_create_as_copy_should_always_add_relation_with_copied_issue_by_setting
2885 2901 with_settings :link_copied_issue => 'yes' do
2886 2902 @request.session[:user_id] = 2
2887 2903 assert_difference 'Issue.count' do
2888 2904 assert_difference 'IssueRelation.count' do
2889 2905 post :create, :project_id => 1, :copy_from => 1,
2890 2906 :issue => {:subject => 'Copy'}
2891 2907 end
2892 2908 end
2893 2909 end
2894 2910 end
2895 2911
2896 2912 def test_create_as_copy_should_never_add_relation_with_copied_issue_by_setting
2897 2913 with_settings :link_copied_issue => 'no' do
2898 2914 @request.session[:user_id] = 2
2899 2915 assert_difference 'Issue.count' do
2900 2916 assert_no_difference 'IssueRelation.count' do
2901 2917 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2902 2918 :issue => {:subject => 'Copy'}
2903 2919 end
2904 2920 end
2905 2921 end
2906 2922 end
2907 2923
2908 2924 def test_create_as_copy_should_copy_subtasks
2909 2925 @request.session[:user_id] = 2
2910 2926 issue = Issue.generate_with_descendants!
2911 2927 count = issue.descendants.count
2912 2928 assert_difference 'Issue.count', count + 1 do
2913 2929 post :create, :project_id => 1, :copy_from => issue.id,
2914 2930 :issue => {:project_id => '1', :tracker_id => '3',
2915 2931 :status_id => '1', :subject => 'Copy with subtasks'},
2916 2932 :copy_subtasks => '1'
2917 2933 end
2918 2934 copy = Issue.where(:parent_id => nil).order('id DESC').first
2919 2935 assert_equal count, copy.descendants.count
2920 2936 assert_equal issue.descendants.map(&:subject).sort, copy.descendants.map(&:subject).sort
2921 2937 end
2922 2938
2923 2939 def test_create_as_copy_without_copy_subtasks_option_should_not_copy_subtasks
2924 2940 @request.session[:user_id] = 2
2925 2941 issue = Issue.generate_with_descendants!
2926 2942 assert_difference 'Issue.count', 1 do
2927 2943 post :create, :project_id => 1, :copy_from => 3,
2928 2944 :issue => {:project_id => '1', :tracker_id => '3',
2929 2945 :status_id => '1', :subject => 'Copy with subtasks'}
2930 2946 end
2931 2947 copy = Issue.where(:parent_id => nil).order('id DESC').first
2932 2948 assert_equal 0, copy.descendants.count
2933 2949 end
2934 2950
2935 2951 def test_create_as_copy_with_failure
2936 2952 @request.session[:user_id] = 2
2937 2953 post :create, :project_id => 1, :copy_from => 1,
2938 2954 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
2939 2955
2940 2956 assert_response :success
2941 2957 assert_template 'new'
2942 2958
2943 2959 assert_not_nil assigns(:issue)
2944 2960 assert assigns(:issue).copy?
2945 2961
2946 2962 assert_select 'form#issue-form[action="/projects/ecookbook/issues"]' do
2947 2963 assert_select 'select[name=?]', 'issue[project_id]' do
2948 2964 assert_select 'option[value="1"]:not([selected])', :text => 'eCookbook'
2949 2965 assert_select 'option[value="2"][selected=selected]', :text => 'OnlineStore'
2950 2966 end
2951 2967 assert_select 'input[name=copy_from][value="1"]'
2952 2968 end
2953 2969 end
2954 2970
2955 2971 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
2956 2972 @request.session[:user_id] = 2
2957 2973 assert !User.find(2).member_of?(Project.find(4))
2958 2974
2959 2975 assert_difference 'Issue.count' do
2960 2976 post :create, :project_id => 1, :copy_from => 1,
2961 2977 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2962 2978 end
2963 2979 issue = Issue.order('id DESC').first
2964 2980 assert_equal 1, issue.project_id
2965 2981 end
2966 2982
2967 2983 def test_get_edit
2968 2984 @request.session[:user_id] = 2
2969 2985 get :edit, :id => 1
2970 2986 assert_response :success
2971 2987 assert_template 'edit'
2972 2988 assert_not_nil assigns(:issue)
2973 2989 assert_equal Issue.find(1), assigns(:issue)
2974 2990
2975 2991 # Be sure we don't display inactive IssuePriorities
2976 2992 assert ! IssuePriority.find(15).active?
2977 2993 assert_select 'select[name=?]', 'issue[priority_id]' do
2978 2994 assert_select 'option[value="15"]', 0
2979 2995 end
2980 2996 end
2981 2997
2982 2998 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
2983 2999 @request.session[:user_id] = 2
2984 3000 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
2985 3001
2986 3002 get :edit, :id => 1
2987 3003 assert_select 'input[name=?]', 'time_entry[hours]'
2988 3004 end
2989 3005
2990 3006 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
2991 3007 @request.session[:user_id] = 2
2992 3008 Role.find_by_name('Manager').remove_permission! :log_time
2993 3009
2994 3010 get :edit, :id => 1
2995 3011 assert_select 'input[name=?]', 'time_entry[hours]', 0
2996 3012 end
2997 3013
2998 3014 def test_get_edit_with_params
2999 3015 @request.session[:user_id] = 2
3000 3016 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
3001 3017 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => 10 }
3002 3018 assert_response :success
3003 3019 assert_template 'edit'
3004 3020
3005 3021 issue = assigns(:issue)
3006 3022 assert_not_nil issue
3007 3023
3008 3024 assert_equal 5, issue.status_id
3009 3025 assert_select 'select[name=?]', 'issue[status_id]' do
3010 3026 assert_select 'option[value="5"][selected=selected]', :text => 'Closed'
3011 3027 end
3012 3028
3013 3029 assert_equal 7, issue.priority_id
3014 3030 assert_select 'select[name=?]', 'issue[priority_id]' do
3015 3031 assert_select 'option[value="7"][selected=selected]', :text => 'Urgent'
3016 3032 end
3017 3033
3018 3034 assert_select 'input[name=?][value="2.5"]', 'time_entry[hours]'
3019 3035 assert_select 'select[name=?]', 'time_entry[activity_id]' do
3020 3036 assert_select 'option[value="10"][selected=selected]', :text => 'Development'
3021 3037 end
3022 3038 assert_select 'input[name=?][value=test_get_edit_with_params]', 'time_entry[comments]'
3023 3039 end
3024 3040
3025 3041 def test_get_edit_with_multi_custom_field
3026 3042 field = CustomField.find(1)
3027 3043 field.update_attribute :multiple, true
3028 3044 issue = Issue.find(1)
3029 3045 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3030 3046 issue.save!
3031 3047
3032 3048 @request.session[:user_id] = 2
3033 3049 get :edit, :id => 1
3034 3050 assert_response :success
3035 3051 assert_template 'edit'
3036 3052
3037 3053 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
3038 3054 assert_select 'option', 3
3039 3055 assert_select 'option[value=MySQL][selected=selected]'
3040 3056 assert_select 'option[value=Oracle][selected=selected]'
3041 3057 assert_select 'option[value=PostgreSQL]:not([selected])'
3042 3058 end
3043 3059 end
3044 3060
3045 3061 def test_update_form_for_existing_issue
3046 3062 @request.session[:user_id] = 2
3047 3063 xhr :patch, :edit, :id => 1,
3048 3064 :issue => {:tracker_id => 2,
3049 3065 :subject => 'This is the test_new issue',
3050 3066 :description => 'This is the description',
3051 3067 :priority_id => 5}
3052 3068 assert_response :success
3053 3069 assert_equal 'text/javascript', response.content_type
3054 3070 assert_template 'edit'
3055 3071 assert_template :partial => '_form'
3056 3072
3057 3073 issue = assigns(:issue)
3058 3074 assert_kind_of Issue, issue
3059 3075 assert_equal 1, issue.id
3060 3076 assert_equal 1, issue.project_id
3061 3077 assert_equal 2, issue.tracker_id
3062 3078 assert_equal 'This is the test_new issue', issue.subject
3063 3079 end
3064 3080
3065 3081 def test_update_form_for_existing_issue_should_keep_issue_author
3066 3082 @request.session[:user_id] = 3
3067 3083 xhr :patch, :edit, :id => 1, :issue => {:subject => 'Changed'}
3068 3084 assert_response :success
3069 3085 assert_equal 'text/javascript', response.content_type
3070 3086
3071 3087 issue = assigns(:issue)
3072 3088 assert_equal User.find(2), issue.author
3073 3089 assert_equal 2, issue.author_id
3074 3090 assert_not_equal User.current, issue.author
3075 3091 end
3076 3092
3077 3093 def test_update_form_for_existing_issue_should_propose_transitions_based_on_initial_status
3078 3094 @request.session[:user_id] = 2
3079 3095 WorkflowTransition.delete_all
3080 3096 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
3081 3097 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
3082 3098 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 5, :new_status_id => 4)
3083 3099
3084 3100 xhr :patch, :edit, :id => 2,
3085 3101 :issue => {:tracker_id => 2,
3086 3102 :status_id => 5,
3087 3103 :subject => 'This is an issue'}
3088 3104
3089 3105 assert_equal 5, assigns(:issue).status_id
3090 3106 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
3091 3107 end
3092 3108
3093 3109 def test_update_form_for_existing_issue_with_project_change
3094 3110 @request.session[:user_id] = 2
3095 3111 xhr :patch, :edit, :id => 1,
3096 3112 :issue => {:project_id => 2,
3097 3113 :tracker_id => 2,
3098 3114 :subject => 'This is the test_new issue',
3099 3115 :description => 'This is the description',
3100 3116 :priority_id => 5}
3101 3117 assert_response :success
3102 3118 assert_template :partial => '_form'
3103 3119
3104 3120 issue = assigns(:issue)
3105 3121 assert_kind_of Issue, issue
3106 3122 assert_equal 1, issue.id
3107 3123 assert_equal 2, issue.project_id
3108 3124 assert_equal 2, issue.tracker_id
3109 3125 assert_equal 'This is the test_new issue', issue.subject
3110 3126 end
3111 3127
3112 3128 def test_update_form_should_keep_category_with_same_when_changing_project
3113 3129 source = Project.generate!
3114 3130 target = Project.generate!
3115 3131 source_category = IssueCategory.create!(:name => 'Foo', :project => source)
3116 3132 target_category = IssueCategory.create!(:name => 'Foo', :project => target)
3117 3133 issue = Issue.generate!(:project => source, :category => source_category)
3118 3134
3119 3135 @request.session[:user_id] = 1
3120 3136 patch :edit, :id => issue.id,
3121 3137 :issue => {:project_id => target.id, :category_id => source_category.id}
3122 3138 assert_response :success
3123 3139
3124 3140 issue = assigns(:issue)
3125 3141 assert_equal target_category, issue.category
3126 3142 end
3127 3143
3128 3144 def test_update_form_should_propose_default_status_for_existing_issue
3129 3145 @request.session[:user_id] = 2
3130 3146 WorkflowTransition.delete_all
3131 3147 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
3132 3148
3133 3149 xhr :patch, :edit, :id => 2
3134 3150 assert_response :success
3135 3151 assert_equal [2,3], assigns(:allowed_statuses).map(&:id).sort
3136 3152 end
3137 3153
3138 3154 def test_put_update_without_custom_fields_param
3139 3155 @request.session[:user_id] = 2
3140 3156
3141 3157 issue = Issue.find(1)
3142 3158 assert_equal '125', issue.custom_value_for(2).value
3143 3159
3144 3160 assert_difference('Journal.count') do
3145 3161 assert_difference('JournalDetail.count') do
3146 3162 put :update, :id => 1, :issue => {:subject => 'New subject'}
3147 3163 end
3148 3164 end
3149 3165 assert_redirected_to :action => 'show', :id => '1'
3150 3166 issue.reload
3151 3167 assert_equal 'New subject', issue.subject
3152 3168 # Make sure custom fields were not cleared
3153 3169 assert_equal '125', issue.custom_value_for(2).value
3154 3170 end
3155 3171
3156 3172 def test_put_update_with_project_change
3157 3173 @request.session[:user_id] = 2
3158 3174 ActionMailer::Base.deliveries.clear
3159 3175
3160 3176 with_settings :notified_events => %w(issue_updated) do
3161 3177 assert_difference('Journal.count') do
3162 3178 assert_difference('JournalDetail.count', 3) do
3163 3179 put :update, :id => 1, :issue => {:project_id => '2',
3164 3180 :tracker_id => '1', # no change
3165 3181 :priority_id => '6',
3166 3182 :category_id => '3'
3167 3183 }
3168 3184 end
3169 3185 end
3170 3186 end
3171 3187 assert_redirected_to :action => 'show', :id => '1'
3172 3188 issue = Issue.find(1)
3173 3189 assert_equal 2, issue.project_id
3174 3190 assert_equal 1, issue.tracker_id
3175 3191 assert_equal 6, issue.priority_id
3176 3192 assert_equal 3, issue.category_id
3177 3193
3178 3194 mail = ActionMailer::Base.deliveries.last
3179 3195 assert_not_nil mail
3180 3196 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
3181 3197 assert_mail_body_match "Project changed from eCookbook to OnlineStore", mail
3182 3198 end
3183 3199
3184 3200 def test_put_update_trying_to_move_issue_to_project_without_tracker_should_not_error
3185 3201 target = Project.generate!(:tracker_ids => [])
3186 3202 assert target.trackers.empty?
3187 3203 issue = Issue.generate!
3188 3204 @request.session[:user_id] = 1
3189 3205
3190 3206 put :update, :id => issue.id, :issue => {:project_id => target.id}
3191 3207 assert_response 302
3192 3208 end
3193 3209
3194 3210 def test_put_update_with_tracker_change
3195 3211 @request.session[:user_id] = 2
3196 3212 ActionMailer::Base.deliveries.clear
3197 3213
3198 3214 with_settings :notified_events => %w(issue_updated) do
3199 3215 assert_difference('Journal.count') do
3200 3216 assert_difference('JournalDetail.count', 2) do
3201 3217 put :update, :id => 1, :issue => {:project_id => '1',
3202 3218 :tracker_id => '2',
3203 3219 :priority_id => '6'
3204 3220 }
3205 3221 end
3206 3222 end
3207 3223 end
3208 3224 assert_redirected_to :action => 'show', :id => '1'
3209 3225 issue = Issue.find(1)
3210 3226 assert_equal 1, issue.project_id
3211 3227 assert_equal 2, issue.tracker_id
3212 3228 assert_equal 6, issue.priority_id
3213 3229 assert_equal 1, issue.category_id
3214 3230
3215 3231 mail = ActionMailer::Base.deliveries.last
3216 3232 assert_not_nil mail
3217 3233 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
3218 3234 assert_mail_body_match "Tracker changed from Bug to Feature request", mail
3219 3235 end
3220 3236
3221 3237 def test_put_update_with_custom_field_change
3222 3238 @request.session[:user_id] = 2
3223 3239 issue = Issue.find(1)
3224 3240 assert_equal '125', issue.custom_value_for(2).value
3225 3241
3226 3242 with_settings :notified_events => %w(issue_updated) do
3227 3243 assert_difference('Journal.count') do
3228 3244 assert_difference('JournalDetail.count', 3) do
3229 3245 put :update, :id => 1, :issue => {:subject => 'Custom field change',
3230 3246 :priority_id => '6',
3231 3247 :category_id => '1', # no change
3232 3248 :custom_field_values => { '2' => 'New custom value' }
3233 3249 }
3234 3250 end
3235 3251 end
3236 3252 end
3237 3253 assert_redirected_to :action => 'show', :id => '1'
3238 3254 issue.reload
3239 3255 assert_equal 'New custom value', issue.custom_value_for(2).value
3240 3256
3241 3257 mail = ActionMailer::Base.deliveries.last
3242 3258 assert_not_nil mail
3243 3259 assert_mail_body_match "Searchable field changed from 125 to New custom value", mail
3244 3260 end
3245 3261
3246 3262 def test_put_update_with_multi_custom_field_change
3247 3263 field = CustomField.find(1)
3248 3264 field.update_attribute :multiple, true
3249 3265 issue = Issue.find(1)
3250 3266 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3251 3267 issue.save!
3252 3268
3253 3269 @request.session[:user_id] = 2
3254 3270 assert_difference('Journal.count') do
3255 3271 assert_difference('JournalDetail.count', 3) do
3256 3272 put :update, :id => 1,
3257 3273 :issue => {
3258 3274 :subject => 'Custom field change',
3259 3275 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
3260 3276 }
3261 3277 end
3262 3278 end
3263 3279 assert_redirected_to :action => 'show', :id => '1'
3264 3280 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
3265 3281 end
3266 3282
3267 3283 def test_put_update_with_status_and_assignee_change
3268 3284 issue = Issue.find(1)
3269 3285 assert_equal 1, issue.status_id
3270 3286 @request.session[:user_id] = 2
3271 3287
3272 3288 with_settings :notified_events => %w(issue_updated) do
3273 3289 assert_difference('TimeEntry.count', 0) do
3274 3290 put :update,
3275 3291 :id => 1,
3276 3292 :issue => { :status_id => 2, :assigned_to_id => 3, :notes => 'Assigned to dlopper' },
3277 3293 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
3278 3294 end
3279 3295 end
3280 3296 assert_redirected_to :action => 'show', :id => '1'
3281 3297 issue.reload
3282 3298 assert_equal 2, issue.status_id
3283 3299 j = Journal.order('id DESC').first
3284 3300 assert_equal 'Assigned to dlopper', j.notes
3285 3301 assert_equal 2, j.details.size
3286 3302
3287 3303 mail = ActionMailer::Base.deliveries.last
3288 3304 assert_mail_body_match "Status changed from New to Assigned", mail
3289 3305 # subject should contain the new status
3290 3306 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
3291 3307 end
3292 3308
3293 3309 def test_put_update_with_note_only
3294 3310 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
3295 3311
3296 3312 with_settings :notified_events => %w(issue_updated) do
3297 3313 # anonymous user
3298 3314 put :update,
3299 3315 :id => 1,
3300 3316 :issue => { :notes => notes }
3301 3317 end
3302 3318 assert_redirected_to :action => 'show', :id => '1'
3303 3319 j = Journal.order('id DESC').first
3304 3320 assert_equal notes, j.notes
3305 3321 assert_equal 0, j.details.size
3306 3322 assert_equal User.anonymous, j.user
3307 3323
3308 3324 mail = ActionMailer::Base.deliveries.last
3309 3325 assert_mail_body_match notes, mail
3310 3326 end
3311 3327
3312 3328 def test_put_update_with_private_note_only
3313 3329 notes = 'Private note'
3314 3330 @request.session[:user_id] = 2
3315 3331
3316 3332 assert_difference 'Journal.count' do
3317 3333 put :update, :id => 1, :issue => {:notes => notes, :private_notes => '1'}
3318 3334 assert_redirected_to :action => 'show', :id => '1'
3319 3335 end
3320 3336
3321 3337 j = Journal.order('id DESC').first
3322 3338 assert_equal notes, j.notes
3323 3339 assert_equal true, j.private_notes
3324 3340 end
3325 3341
3326 3342 def test_put_update_with_private_note_and_changes
3327 3343 notes = 'Private note'
3328 3344 @request.session[:user_id] = 2
3329 3345
3330 3346 assert_difference 'Journal.count', 2 do
3331 3347 put :update, :id => 1, :issue => {:subject => 'New subject', :notes => notes, :private_notes => '1'}
3332 3348 assert_redirected_to :action => 'show', :id => '1'
3333 3349 end
3334 3350
3335 3351 j = Journal.order('id DESC').first
3336 3352 assert_equal notes, j.notes
3337 3353 assert_equal true, j.private_notes
3338 3354 assert_equal 0, j.details.count
3339 3355
3340 3356 j = Journal.order('id DESC').offset(1).first
3341 3357 assert_nil j.notes
3342 3358 assert_equal false, j.private_notes
3343 3359 assert_equal 1, j.details.count
3344 3360 end
3345 3361
3346 3362 def test_put_update_with_note_and_spent_time
3347 3363 @request.session[:user_id] = 2
3348 3364 spent_hours_before = Issue.find(1).spent_hours
3349 3365 assert_difference('TimeEntry.count') do
3350 3366 put :update,
3351 3367 :id => 1,
3352 3368 :issue => { :notes => '2.5 hours added' },
3353 3369 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
3354 3370 end
3355 3371 assert_redirected_to :action => 'show', :id => '1'
3356 3372
3357 3373 issue = Issue.find(1)
3358 3374
3359 3375 j = Journal.order('id DESC').first
3360 3376 assert_equal '2.5 hours added', j.notes
3361 3377 assert_equal 0, j.details.size
3362 3378
3363 3379 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
3364 3380 assert_not_nil t
3365 3381 assert_equal 2.5, t.hours
3366 3382 assert_equal spent_hours_before + 2.5, issue.spent_hours
3367 3383 end
3368 3384
3369 3385 def test_put_update_should_preserve_parent_issue_even_if_not_visible
3370 3386 parent = Issue.generate!(:project_id => 1, :is_private => true)
3371 3387 issue = Issue.generate!(:parent_issue_id => parent.id)
3372 3388 assert !parent.visible?(User.find(3))
3373 3389 @request.session[:user_id] = 3
3374 3390
3375 3391 get :edit, :id => issue.id
3376 3392 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', parent.id.to_s
3377 3393
3378 3394 put :update, :id => issue.id, :issue => {:subject => 'New subject', :parent_issue_id => parent.id.to_s}
3379 3395 assert_response 302
3380 3396 assert_equal parent, issue.parent
3381 3397 end
3382 3398
3383 3399 def test_put_update_with_attachment_only
3384 3400 set_tmp_attachments_directory
3385 3401
3386 3402 # Delete all fixtured journals, a race condition can occur causing the wrong
3387 3403 # journal to get fetched in the next find.
3388 3404 Journal.delete_all
3389 3405
3390 3406 with_settings :notified_events => %w(issue_updated) do
3391 3407 # anonymous user
3392 3408 assert_difference 'Attachment.count' do
3393 3409 put :update, :id => 1,
3394 3410 :issue => {:notes => ''},
3395 3411 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3396 3412 end
3397 3413 end
3398 3414
3399 3415 assert_redirected_to :action => 'show', :id => '1'
3400 3416 j = Issue.find(1).journals.reorder('id DESC').first
3401 3417 assert j.notes.blank?
3402 3418 assert_equal 1, j.details.size
3403 3419 assert_equal 'testfile.txt', j.details.first.value
3404 3420 assert_equal User.anonymous, j.user
3405 3421
3406 3422 attachment = Attachment.order('id DESC').first
3407 3423 assert_equal Issue.find(1), attachment.container
3408 3424 assert_equal User.anonymous, attachment.author
3409 3425 assert_equal 'testfile.txt', attachment.filename
3410 3426 assert_equal 'text/plain', attachment.content_type
3411 3427 assert_equal 'test file', attachment.description
3412 3428 assert_equal 59, attachment.filesize
3413 3429 assert File.exists?(attachment.diskfile)
3414 3430 assert_equal 59, File.size(attachment.diskfile)
3415 3431
3416 3432 mail = ActionMailer::Base.deliveries.last
3417 3433 assert_mail_body_match 'testfile.txt', mail
3418 3434 end
3419 3435
3420 3436 def test_put_update_with_failure_should_save_attachments
3421 3437 set_tmp_attachments_directory
3422 3438 @request.session[:user_id] = 2
3423 3439
3424 3440 assert_no_difference 'Journal.count' do
3425 3441 assert_difference 'Attachment.count' do
3426 3442 put :update, :id => 1,
3427 3443 :issue => { :subject => '' },
3428 3444 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3429 3445 assert_response :success
3430 3446 assert_template 'edit'
3431 3447 end
3432 3448 end
3433 3449
3434 3450 attachment = Attachment.order('id DESC').first
3435 3451 assert_equal 'testfile.txt', attachment.filename
3436 3452 assert File.exists?(attachment.diskfile)
3437 3453 assert_nil attachment.container
3438 3454
3439 3455 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3440 3456 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3441 3457 end
3442 3458
3443 3459 def test_put_update_with_failure_should_keep_saved_attachments
3444 3460 set_tmp_attachments_directory
3445 3461 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3446 3462 @request.session[:user_id] = 2
3447 3463
3448 3464 assert_no_difference 'Journal.count' do
3449 3465 assert_no_difference 'Attachment.count' do
3450 3466 put :update, :id => 1,
3451 3467 :issue => { :subject => '' },
3452 3468 :attachments => {'p0' => {'token' => attachment.token}}
3453 3469 assert_response :success
3454 3470 assert_template 'edit'
3455 3471 end
3456 3472 end
3457 3473
3458 3474 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3459 3475 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3460 3476 end
3461 3477
3462 3478 def test_put_update_should_attach_saved_attachments
3463 3479 set_tmp_attachments_directory
3464 3480 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3465 3481 @request.session[:user_id] = 2
3466 3482
3467 3483 assert_difference 'Journal.count' do
3468 3484 assert_difference 'JournalDetail.count' do
3469 3485 assert_no_difference 'Attachment.count' do
3470 3486 put :update, :id => 1,
3471 3487 :issue => {:notes => 'Attachment added'},
3472 3488 :attachments => {'p0' => {'token' => attachment.token}}
3473 3489 assert_redirected_to '/issues/1'
3474 3490 end
3475 3491 end
3476 3492 end
3477 3493
3478 3494 attachment.reload
3479 3495 assert_equal Issue.find(1), attachment.container
3480 3496
3481 3497 journal = Journal.order('id DESC').first
3482 3498 assert_equal 1, journal.details.size
3483 3499 assert_equal 'testfile.txt', journal.details.first.value
3484 3500 end
3485 3501
3486 3502 def test_put_update_with_attachment_that_fails_to_save
3487 3503 set_tmp_attachments_directory
3488 3504
3489 3505 # anonymous user
3490 3506 with_settings :attachment_max_size => 0 do
3491 3507 put :update,
3492 3508 :id => 1,
3493 3509 :issue => {:notes => ''},
3494 3510 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
3495 3511 assert_redirected_to :action => 'show', :id => '1'
3496 3512 assert_equal '1 file(s) could not be saved.', flash[:warning]
3497 3513 end
3498 3514 end
3499 3515
3500 3516 def test_put_update_with_no_change
3501 3517 issue = Issue.find(1)
3502 3518 issue.journals.clear
3503 3519 ActionMailer::Base.deliveries.clear
3504 3520
3505 3521 put :update,
3506 3522 :id => 1,
3507 3523 :issue => {:notes => ''}
3508 3524 assert_redirected_to :action => 'show', :id => '1'
3509 3525
3510 3526 issue.reload
3511 3527 assert issue.journals.empty?
3512 3528 # No email should be sent
3513 3529 assert ActionMailer::Base.deliveries.empty?
3514 3530 end
3515 3531
3516 3532 def test_put_update_should_send_a_notification
3517 3533 @request.session[:user_id] = 2
3518 3534 ActionMailer::Base.deliveries.clear
3519 3535 issue = Issue.find(1)
3520 3536 old_subject = issue.subject
3521 3537 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
3522 3538
3523 3539 with_settings :notified_events => %w(issue_updated) do
3524 3540 put :update, :id => 1, :issue => {:subject => new_subject,
3525 3541 :priority_id => '6',
3526 3542 :category_id => '1' # no change
3527 3543 }
3528 3544 assert_equal 1, ActionMailer::Base.deliveries.size
3529 3545 end
3530 3546 end
3531 3547
3532 3548 def test_put_update_with_invalid_spent_time_hours_only
3533 3549 @request.session[:user_id] = 2
3534 3550 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3535 3551
3536 3552 assert_no_difference('Journal.count') do
3537 3553 put :update,
3538 3554 :id => 1,
3539 3555 :issue => {:notes => notes},
3540 3556 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
3541 3557 end
3542 3558 assert_response :success
3543 3559 assert_template 'edit'
3544 3560
3545 3561 assert_select_error /Activity cannot be blank/
3546 3562 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3547 3563 assert_select 'input[name=?][value=?]', 'time_entry[hours]', '2z'
3548 3564 end
3549 3565
3550 3566 def test_put_update_with_invalid_spent_time_comments_only
3551 3567 @request.session[:user_id] = 2
3552 3568 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3553 3569
3554 3570 assert_no_difference('Journal.count') do
3555 3571 put :update,
3556 3572 :id => 1,
3557 3573 :issue => {:notes => notes},
3558 3574 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
3559 3575 end
3560 3576 assert_response :success
3561 3577 assert_template 'edit'
3562 3578
3563 3579 assert_select_error /Activity cannot be blank/
3564 3580 assert_select_error /Hours cannot be blank/
3565 3581 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3566 3582 assert_select 'input[name=?][value=?]', 'time_entry[comments]', 'this is my comment'
3567 3583 end
3568 3584
3569 3585 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
3570 3586 issue = Issue.find(2)
3571 3587 @request.session[:user_id] = 2
3572 3588
3573 3589 put :update,
3574 3590 :id => issue.id,
3575 3591 :issue => {
3576 3592 :fixed_version_id => 4
3577 3593 }
3578 3594
3579 3595 assert_response :redirect
3580 3596 issue.reload
3581 3597 assert_equal 4, issue.fixed_version_id
3582 3598 assert_not_equal issue.project_id, issue.fixed_version.project_id
3583 3599 end
3584 3600
3585 3601 def test_put_update_should_redirect_back_using_the_back_url_parameter
3586 3602 issue = Issue.find(2)
3587 3603 @request.session[:user_id] = 2
3588 3604
3589 3605 put :update,
3590 3606 :id => issue.id,
3591 3607 :issue => {
3592 3608 :fixed_version_id => 4
3593 3609 },
3594 3610 :back_url => '/issues'
3595 3611
3596 3612 assert_response :redirect
3597 3613 assert_redirected_to '/issues'
3598 3614 end
3599 3615
3600 3616 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3601 3617 issue = Issue.find(2)
3602 3618 @request.session[:user_id] = 2
3603 3619
3604 3620 put :update,
3605 3621 :id => issue.id,
3606 3622 :issue => {
3607 3623 :fixed_version_id => 4
3608 3624 },
3609 3625 :back_url => 'http://google.com'
3610 3626
3611 3627 assert_response :redirect
3612 3628 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
3613 3629 end
3614 3630
3615 3631 def test_get_bulk_edit
3616 3632 @request.session[:user_id] = 2
3617 3633 get :bulk_edit, :ids => [1, 3]
3618 3634 assert_response :success
3619 3635 assert_template 'bulk_edit'
3620 3636
3621 3637 assert_select 'ul#bulk-selection' do
3622 3638 assert_select 'li', 2
3623 3639 assert_select 'li a', :text => 'Bug #1'
3624 3640 end
3625 3641
3626 3642 assert_select 'form#bulk_edit_form[action=?]', '/issues/bulk_update' do
3627 3643 assert_select 'input[name=?]', 'ids[]', 2
3628 3644 assert_select 'input[name=?][value="1"][type=hidden]', 'ids[]'
3629 3645
3630 3646 assert_select 'select[name=?]', 'issue[project_id]'
3631 3647 assert_select 'input[name=?]', 'issue[parent_issue_id]'
3632 3648
3633 3649 # Project specific custom field, date type
3634 3650 field = CustomField.find(9)
3635 3651 assert !field.is_for_all?
3636 3652 assert_equal 'date', field.field_format
3637 3653 assert_select 'input[name=?]', 'issue[custom_field_values][9]'
3638 3654
3639 3655 # System wide custom field
3640 3656 assert CustomField.find(1).is_for_all?
3641 3657 assert_select 'select[name=?]', 'issue[custom_field_values][1]'
3642 3658
3643 3659 # Be sure we don't display inactive IssuePriorities
3644 3660 assert ! IssuePriority.find(15).active?
3645 3661 assert_select 'select[name=?]', 'issue[priority_id]' do
3646 3662 assert_select 'option[value="15"]', 0
3647 3663 end
3648 3664 end
3649 3665 end
3650 3666
3651 3667 def test_get_bulk_edit_on_different_projects
3652 3668 @request.session[:user_id] = 2
3653 3669 get :bulk_edit, :ids => [1, 2, 6]
3654 3670 assert_response :success
3655 3671 assert_template 'bulk_edit'
3656 3672
3657 3673 # Can not set issues from different projects as children of an issue
3658 3674 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
3659 3675
3660 3676 # Project specific custom field, date type
3661 3677 field = CustomField.find(9)
3662 3678 assert !field.is_for_all?
3663 3679 assert !field.project_ids.include?(Issue.find(6).project_id)
3664 3680 assert_select 'input[name=?]', 'issue[custom_field_values][9]', 0
3665 3681 end
3666 3682
3667 3683 def test_get_bulk_edit_with_user_custom_field
3668 3684 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :tracker_ids => [1,2,3])
3669 3685
3670 3686 @request.session[:user_id] = 2
3671 3687 get :bulk_edit, :ids => [1, 2]
3672 3688 assert_response :success
3673 3689 assert_template 'bulk_edit'
3674 3690
3675 3691 assert_select 'select.user_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3676 3692 assert_select 'option', Project.find(1).users.count + 2 # "no change" + "none" options
3677 3693 end
3678 3694 end
3679 3695
3680 3696 def test_get_bulk_edit_with_version_custom_field
3681 3697 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1,2,3])
3682 3698
3683 3699 @request.session[:user_id] = 2
3684 3700 get :bulk_edit, :ids => [1, 2]
3685 3701 assert_response :success
3686 3702 assert_template 'bulk_edit'
3687 3703
3688 3704 assert_select 'select.version_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3689 3705 assert_select 'option', Project.find(1).shared_versions.count + 2 # "no change" + "none" options
3690 3706 end
3691 3707 end
3692 3708
3693 3709 def test_get_bulk_edit_with_multi_custom_field
3694 3710 field = CustomField.find(1)
3695 3711 field.update_attribute :multiple, true
3696 3712
3697 3713 @request.session[:user_id] = 2
3698 3714 get :bulk_edit, :ids => [1, 3]
3699 3715 assert_response :success
3700 3716 assert_template 'bulk_edit'
3701 3717
3702 3718 assert_select 'select[name=?]', 'issue[custom_field_values][1][]' do
3703 3719 assert_select 'option', field.possible_values.size + 1 # "none" options
3704 3720 end
3705 3721 end
3706 3722
3707 3723 def test_bulk_edit_should_propose_to_clear_text_custom_fields
3708 3724 @request.session[:user_id] = 2
3709 3725 get :bulk_edit, :ids => [1, 3]
3710 3726 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', '__none__'
3711 3727 end
3712 3728
3713 3729 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
3714 3730 WorkflowTransition.delete_all
3715 3731 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3716 3732 :old_status_id => 1, :new_status_id => 1)
3717 3733 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3718 3734 :old_status_id => 1, :new_status_id => 3)
3719 3735 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3720 3736 :old_status_id => 1, :new_status_id => 4)
3721 3737 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3722 3738 :old_status_id => 2, :new_status_id => 1)
3723 3739 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3724 3740 :old_status_id => 2, :new_status_id => 3)
3725 3741 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3726 3742 :old_status_id => 2, :new_status_id => 5)
3727 3743 @request.session[:user_id] = 2
3728 3744 get :bulk_edit, :ids => [1, 2]
3729 3745
3730 3746 assert_response :success
3731 3747 statuses = assigns(:available_statuses)
3732 3748 assert_not_nil statuses
3733 3749 assert_equal [1, 3], statuses.map(&:id).sort
3734 3750
3735 3751 assert_select 'select[name=?]', 'issue[status_id]' do
3736 3752 assert_select 'option', 3 # 2 statuses + "no change" option
3737 3753 end
3738 3754 end
3739 3755
3740 3756 def test_bulk_edit_should_propose_target_project_open_shared_versions
3741 3757 @request.session[:user_id] = 2
3742 3758 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3743 3759 assert_response :success
3744 3760 assert_template 'bulk_edit'
3745 3761 assert_equal Project.find(1).shared_versions.open.to_a.sort, assigns(:versions).sort
3746 3762
3747 3763 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
3748 3764 assert_select 'option', :text => '2.0'
3749 3765 end
3750 3766 end
3751 3767
3752 3768 def test_bulk_edit_should_propose_target_project_categories
3753 3769 @request.session[:user_id] = 2
3754 3770 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3755 3771 assert_response :success
3756 3772 assert_template 'bulk_edit'
3757 3773 assert_equal Project.find(1).issue_categories.sort, assigns(:categories).sort
3758 3774
3759 3775 assert_select 'select[name=?]', 'issue[category_id]' do
3760 3776 assert_select 'option', :text => 'Recipes'
3761 3777 end
3762 3778 end
3763 3779
3764 3780 def test_bulk_edit_should_only_propose_issues_trackers_custom_fields
3765 3781 IssueCustomField.delete_all
3766 3782 field = IssueCustomField.generate!(:tracker_ids => [1], :is_for_all => true)
3767 3783 IssueCustomField.generate!(:tracker_ids => [2], :is_for_all => true)
3768 3784 @request.session[:user_id] = 2
3769 3785
3770 3786 issue_ids = Issue.where(:project_id => 1, :tracker_id => 1).limit(2).ids
3771 3787 get :bulk_edit, :ids => issue_ids
3772 3788 assert_equal [field], assigns(:custom_fields)
3773 3789 end
3774 3790
3775 3791 def test_bulk_update
3776 3792 @request.session[:user_id] = 2
3777 3793 # update issues priority
3778 3794 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3779 3795 :issue => {:priority_id => 7,
3780 3796 :assigned_to_id => '',
3781 3797 :custom_field_values => {'2' => ''}}
3782 3798
3783 3799 assert_response 302
3784 3800 # check that the issues were updated
3785 3801 assert_equal [7, 7], Issue.where(:id =>[1, 2]).collect {|i| i.priority.id}
3786 3802
3787 3803 issue = Issue.find(1)
3788 3804 journal = issue.journals.reorder('created_on DESC').first
3789 3805 assert_equal '125', issue.custom_value_for(2).value
3790 3806 assert_equal 'Bulk editing', journal.notes
3791 3807 assert_equal 1, journal.details.size
3792 3808 end
3793 3809
3794 3810 def test_bulk_update_with_group_assignee
3795 3811 group = Group.find(11)
3796 3812 project = Project.find(1)
3797 3813 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
3798 3814
3799 3815 @request.session[:user_id] = 2
3800 3816 # update issues assignee
3801 3817 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3802 3818 :issue => {:priority_id => '',
3803 3819 :assigned_to_id => group.id,
3804 3820 :custom_field_values => {'2' => ''}}
3805 3821
3806 3822 assert_response 302
3807 3823 assert_equal [group, group], Issue.where(:id => [1, 2]).collect {|i| i.assigned_to}
3808 3824 end
3809 3825
3810 3826 def test_bulk_update_on_different_projects
3811 3827 @request.session[:user_id] = 2
3812 3828 # update issues priority
3813 3829 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
3814 3830 :issue => {:priority_id => 7,
3815 3831 :assigned_to_id => '',
3816 3832 :custom_field_values => {'2' => ''}}
3817 3833
3818 3834 assert_response 302
3819 3835 # check that the issues were updated
3820 3836 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
3821 3837
3822 3838 issue = Issue.find(1)
3823 3839 journal = issue.journals.reorder('created_on DESC').first
3824 3840 assert_equal '125', issue.custom_value_for(2).value
3825 3841 assert_equal 'Bulk editing', journal.notes
3826 3842 assert_equal 1, journal.details.size
3827 3843 end
3828 3844
3829 3845 def test_bulk_update_on_different_projects_without_rights
3830 3846 @request.session[:user_id] = 3
3831 3847 user = User.find(3)
3832 3848 action = { :controller => "issues", :action => "bulk_update" }
3833 3849 assert user.allowed_to?(action, Issue.find(1).project)
3834 3850 assert ! user.allowed_to?(action, Issue.find(6).project)
3835 3851 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
3836 3852 :issue => {:priority_id => 7,
3837 3853 :assigned_to_id => '',
3838 3854 :custom_field_values => {'2' => ''}}
3839 3855 assert_response 403
3840 3856 assert_not_equal "Bulk should fail", Journal.last.notes
3841 3857 end
3842 3858
3843 3859 def test_bullk_update_should_send_a_notification
3844 3860 @request.session[:user_id] = 2
3845 3861 ActionMailer::Base.deliveries.clear
3846 3862 with_settings :notified_events => %w(issue_updated) do
3847 3863 post(:bulk_update,
3848 3864 {
3849 3865 :ids => [1, 2],
3850 3866 :notes => 'Bulk editing',
3851 3867 :issue => {
3852 3868 :priority_id => 7,
3853 3869 :assigned_to_id => '',
3854 3870 :custom_field_values => {'2' => ''}
3855 3871 }
3856 3872 })
3857 3873 assert_response 302
3858 3874 assert_equal 2, ActionMailer::Base.deliveries.size
3859 3875 end
3860 3876 end
3861 3877
3862 3878 def test_bulk_update_project
3863 3879 @request.session[:user_id] = 2
3864 3880 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
3865 3881 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3866 3882 # Issues moved to project 2
3867 3883 assert_equal 2, Issue.find(1).project_id
3868 3884 assert_equal 2, Issue.find(2).project_id
3869 3885 # No tracker change
3870 3886 assert_equal 1, Issue.find(1).tracker_id
3871 3887 assert_equal 2, Issue.find(2).tracker_id
3872 3888 end
3873 3889
3874 3890 def test_bulk_update_project_on_single_issue_should_follow_when_needed
3875 3891 @request.session[:user_id] = 2
3876 3892 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
3877 3893 assert_redirected_to '/issues/1'
3878 3894 end
3879 3895
3880 3896 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
3881 3897 @request.session[:user_id] = 2
3882 3898 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
3883 3899 assert_redirected_to '/projects/onlinestore/issues'
3884 3900 end
3885 3901
3886 3902 def test_bulk_update_tracker
3887 3903 @request.session[:user_id] = 2
3888 3904 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
3889 3905 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3890 3906 assert_equal 2, Issue.find(1).tracker_id
3891 3907 assert_equal 2, Issue.find(2).tracker_id
3892 3908 end
3893 3909
3894 3910 def test_bulk_update_status
3895 3911 @request.session[:user_id] = 2
3896 3912 # update issues priority
3897 3913 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
3898 3914 :issue => {:priority_id => '',
3899 3915 :assigned_to_id => '',
3900 3916 :status_id => '5'}
3901 3917
3902 3918 assert_response 302
3903 3919 issue = Issue.find(1)
3904 3920 assert issue.closed?
3905 3921 end
3906 3922
3907 3923 def test_bulk_update_priority
3908 3924 @request.session[:user_id] = 2
3909 3925 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3910 3926
3911 3927 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3912 3928 assert_equal 6, Issue.find(1).priority_id
3913 3929 assert_equal 6, Issue.find(2).priority_id
3914 3930 end
3915 3931
3916 3932 def test_bulk_update_with_notes
3917 3933 @request.session[:user_id] = 2
3918 3934 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
3919 3935
3920 3936 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3921 3937 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
3922 3938 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
3923 3939 end
3924 3940
3925 3941 def test_bulk_update_parent_id
3926 3942 IssueRelation.delete_all
3927 3943 @request.session[:user_id] = 2
3928 3944 post :bulk_update, :ids => [1, 3],
3929 3945 :notes => 'Bulk editing parent',
3930 3946 :issue => {:priority_id => '', :assigned_to_id => '',
3931 3947 :status_id => '', :parent_issue_id => '2'}
3932 3948 assert_response 302
3933 3949 parent = Issue.find(2)
3934 3950 assert_equal parent.id, Issue.find(1).parent_id
3935 3951 assert_equal parent.id, Issue.find(3).parent_id
3936 3952 assert_equal [1, 3], parent.children.collect(&:id).sort
3937 3953 end
3938 3954
3939 3955 def test_bulk_update_custom_field
3940 3956 @request.session[:user_id] = 2
3941 3957 # update issues priority
3942 3958 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
3943 3959 :issue => {:priority_id => '',
3944 3960 :assigned_to_id => '',
3945 3961 :custom_field_values => {'2' => '777'}}
3946 3962
3947 3963 assert_response 302
3948 3964
3949 3965 issue = Issue.find(1)
3950 3966 journal = issue.journals.reorder('created_on DESC').first
3951 3967 assert_equal '777', issue.custom_value_for(2).value
3952 3968 assert_equal 1, journal.details.size
3953 3969 assert_equal '125', journal.details.first.old_value
3954 3970 assert_equal '777', journal.details.first.value
3955 3971 end
3956 3972
3957 3973 def test_bulk_update_custom_field_to_blank
3958 3974 @request.session[:user_id] = 2
3959 3975 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing custom field',
3960 3976 :issue => {:priority_id => '',
3961 3977 :assigned_to_id => '',
3962 3978 :custom_field_values => {'1' => '__none__'}}
3963 3979 assert_response 302
3964 3980 assert_equal '', Issue.find(1).custom_field_value(1)
3965 3981 assert_equal '', Issue.find(3).custom_field_value(1)
3966 3982 end
3967 3983
3968 3984 def test_bulk_update_multi_custom_field
3969 3985 field = CustomField.find(1)
3970 3986 field.update_attribute :multiple, true
3971 3987
3972 3988 @request.session[:user_id] = 2
3973 3989 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
3974 3990 :issue => {:priority_id => '',
3975 3991 :assigned_to_id => '',
3976 3992 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
3977 3993
3978 3994 assert_response 302
3979 3995
3980 3996 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
3981 3997 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
3982 3998 # the custom field is not associated with the issue tracker
3983 3999 assert_nil Issue.find(2).custom_field_value(1)
3984 4000 end
3985 4001
3986 4002 def test_bulk_update_multi_custom_field_to_blank
3987 4003 field = CustomField.find(1)
3988 4004 field.update_attribute :multiple, true
3989 4005
3990 4006 @request.session[:user_id] = 2
3991 4007 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing multi custom field',
3992 4008 :issue => {:priority_id => '',
3993 4009 :assigned_to_id => '',
3994 4010 :custom_field_values => {'1' => ['__none__']}}
3995 4011 assert_response 302
3996 4012 assert_equal [''], Issue.find(1).custom_field_value(1)
3997 4013 assert_equal [''], Issue.find(3).custom_field_value(1)
3998 4014 end
3999 4015
4000 4016 def test_bulk_update_unassign
4001 4017 assert_not_nil Issue.find(2).assigned_to
4002 4018 @request.session[:user_id] = 2
4003 4019 # unassign issues
4004 4020 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
4005 4021 assert_response 302
4006 4022 # check that the issues were updated
4007 4023 assert_nil Issue.find(2).assigned_to
4008 4024 end
4009 4025
4010 4026 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
4011 4027 @request.session[:user_id] = 2
4012 4028
4013 4029 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
4014 4030
4015 4031 assert_response :redirect
4016 4032 issues = Issue.find([1,2])
4017 4033 issues.each do |issue|
4018 4034 assert_equal 4, issue.fixed_version_id
4019 4035 assert_not_equal issue.project_id, issue.fixed_version.project_id
4020 4036 end
4021 4037 end
4022 4038
4023 4039 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
4024 4040 @request.session[:user_id] = 2
4025 4041 post :bulk_update, :ids => [1,2], :back_url => '/issues'
4026 4042
4027 4043 assert_response :redirect
4028 4044 assert_redirected_to '/issues'
4029 4045 end
4030 4046
4031 4047 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
4032 4048 @request.session[:user_id] = 2
4033 4049 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
4034 4050
4035 4051 assert_response :redirect
4036 4052 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
4037 4053 end
4038 4054
4039 4055 def test_bulk_update_with_all_failures_should_show_errors
4040 4056 @request.session[:user_id] = 2
4041 4057 post :bulk_update, :ids => [1, 2], :issue => {:start_date => 'foo'}
4042 4058
4043 4059 assert_response :success
4044 4060 assert_template 'bulk_edit'
4045 4061 assert_select '#errorExplanation span', :text => 'Failed to save 2 issue(s) on 2 selected: #1, #2.'
4046 4062 assert_select '#errorExplanation ul li', :text => 'Start date is not a valid date: #1, #2'
4047 4063
4048 4064 assert_equal [1, 2], assigns[:issues].map(&:id)
4049 4065 end
4050 4066
4051 4067 def test_bulk_update_with_some_failures_should_show_errors
4052 4068 issue1 = Issue.generate!(:start_date => '2013-05-12')
4053 4069 issue2 = Issue.generate!(:start_date => '2013-05-15')
4054 4070 issue3 = Issue.generate!
4055 4071 @request.session[:user_id] = 2
4056 4072 post :bulk_update, :ids => [issue1.id, issue2.id, issue3.id],
4057 4073 :issue => {:due_date => '2013-05-01'}
4058 4074 assert_response :success
4059 4075 assert_template 'bulk_edit'
4060 4076 assert_select '#errorExplanation span',
4061 4077 :text => "Failed to save 2 issue(s) on 3 selected: ##{issue1.id}, ##{issue2.id}."
4062 4078 assert_select '#errorExplanation ul li',
4063 4079 :text => "Due date must be greater than start date: ##{issue1.id}, ##{issue2.id}"
4064 4080 assert_equal [issue1.id, issue2.id], assigns[:issues].map(&:id)
4065 4081 end
4066 4082
4067 4083 def test_bulk_update_with_failure_should_preserved_form_values
4068 4084 @request.session[:user_id] = 2
4069 4085 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2', :start_date => 'foo'}
4070 4086
4071 4087 assert_response :success
4072 4088 assert_template 'bulk_edit'
4073 4089 assert_select 'select[name=?]', 'issue[tracker_id]' do
4074 4090 assert_select 'option[value="2"][selected=selected]'
4075 4091 end
4076 4092 assert_select 'input[name=?][value=?]', 'issue[start_date]', 'foo'
4077 4093 end
4078 4094
4079 4095 def test_get_bulk_copy
4080 4096 @request.session[:user_id] = 2
4081 4097 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
4082 4098 assert_response :success
4083 4099 assert_template 'bulk_edit'
4084 4100
4085 4101 issues = assigns(:issues)
4086 4102 assert_not_nil issues
4087 4103 assert_equal [1, 2, 3], issues.map(&:id).sort
4088 4104
4089 4105 assert_select 'select[name=?]', 'issue[project_id]' do
4090 4106 assert_select 'option[value=""]'
4091 4107 end
4092 4108 assert_select 'input[name=copy_attachments]'
4093 4109 end
4094 4110
4095 4111 def test_get_bulk_copy_without_add_issues_permission_should_not_propose_current_project_as_target
4096 4112 user = setup_user_with_copy_but_not_add_permission
4097 4113 @request.session[:user_id] = user.id
4098 4114
4099 4115 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
4100 4116 assert_response :success
4101 4117 assert_template 'bulk_edit'
4102 4118
4103 4119 assert_select 'select[name=?]', 'issue[project_id]' do
4104 4120 assert_select 'option[value=""]', 0
4105 4121 assert_select 'option[value="2"]'
4106 4122 end
4107 4123 end
4108 4124
4109 4125 def test_bulk_copy_to_another_project
4110 4126 @request.session[:user_id] = 2
4111 4127 assert_difference 'Issue.count', 2 do
4112 4128 assert_no_difference 'Project.find(1).issues.count' do
4113 4129 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
4114 4130 end
4115 4131 end
4116 4132 assert_redirected_to '/projects/ecookbook/issues'
4117 4133
4118 4134 copies = Issue.order('id DESC').limit(issues.size)
4119 4135 copies.each do |copy|
4120 4136 assert_equal 2, copy.project_id
4121 4137 end
4122 4138 end
4123 4139
4124 4140 def test_bulk_copy_without_add_issues_permission_should_be_allowed_on_project_with_permission
4125 4141 user = setup_user_with_copy_but_not_add_permission
4126 4142 @request.session[:user_id] = user.id
4127 4143
4128 4144 assert_difference 'Issue.count', 3 do
4129 4145 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '2'}, :copy => '1'
4130 4146 assert_response 302
4131 4147 end
4132 4148 end
4133 4149
4134 4150 def test_bulk_copy_on_same_project_without_add_issues_permission_should_be_denied
4135 4151 user = setup_user_with_copy_but_not_add_permission
4136 4152 @request.session[:user_id] = user.id
4137 4153
4138 4154 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => ''}, :copy => '1'
4139 4155 assert_response 403
4140 4156 end
4141 4157
4142 4158 def test_bulk_copy_on_different_project_without_add_issues_permission_should_be_denied
4143 4159 user = setup_user_with_copy_but_not_add_permission
4144 4160 @request.session[:user_id] = user.id
4145 4161
4146 4162 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '1'}, :copy => '1'
4147 4163 assert_response 403
4148 4164 end
4149 4165
4150 4166 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
4151 4167 @request.session[:user_id] = 2
4152 4168 issues = [
4153 4169 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
4154 4170 :priority_id => 2, :subject => 'issue 1', :author_id => 1,
4155 4171 :assigned_to_id => nil),
4156 4172 Issue.create!(:project_id => 2, :tracker_id => 3, :status_id => 2,
4157 4173 :priority_id => 1, :subject => 'issue 2', :author_id => 2,
4158 4174 :assigned_to_id => 3)
4159 4175 ]
4160 4176 assert_difference 'Issue.count', issues.size do
4161 4177 post :bulk_update, :ids => issues.map(&:id), :copy => '1',
4162 4178 :issue => {
4163 4179 :project_id => '', :tracker_id => '', :assigned_to_id => '',
4164 4180 :status_id => '', :start_date => '', :due_date => ''
4165 4181 }
4166 4182 end
4167 4183
4168 4184 copies = Issue.order('id DESC').limit(issues.size)
4169 4185 issues.each do |orig|
4170 4186 copy = copies.detect {|c| c.subject == orig.subject}
4171 4187 assert_not_nil copy
4172 4188 assert_equal orig.project_id, copy.project_id
4173 4189 assert_equal orig.tracker_id, copy.tracker_id
4174 4190 assert_equal orig.status_id, copy.status_id
4175 4191 assert_equal orig.assigned_to_id, copy.assigned_to_id
4176 4192 assert_equal orig.priority_id, copy.priority_id
4177 4193 end
4178 4194 end
4179 4195
4180 4196 def test_bulk_copy_should_allow_changing_the_issue_attributes
4181 4197 # Fixes random test failure with Mysql
4182 4198 # where Issue.where(:project_id => 2).limit(2).order('id desc')
4183 4199 # doesn't return the expected results
4184 4200 Issue.delete_all("project_id=2")
4185 4201
4186 4202 @request.session[:user_id] = 2
4187 4203 assert_difference 'Issue.count', 2 do
4188 4204 assert_no_difference 'Project.find(1).issues.count' do
4189 4205 post :bulk_update, :ids => [1, 2], :copy => '1',
4190 4206 :issue => {
4191 4207 :project_id => '2', :tracker_id => '', :assigned_to_id => '4',
4192 4208 :status_id => '1', :start_date => '2009-12-01', :due_date => '2009-12-31'
4193 4209 }
4194 4210 end
4195 4211 end
4196 4212
4197 4213 copied_issues = Issue.where(:project_id => 2).limit(2).order('id desc').to_a
4198 4214 assert_equal 2, copied_issues.size
4199 4215 copied_issues.each do |issue|
4200 4216 assert_equal 2, issue.project_id, "Project is incorrect"
4201 4217 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
4202 4218 assert_equal 1, issue.status_id, "Status is incorrect"
4203 4219 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
4204 4220 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
4205 4221 end
4206 4222 end
4207 4223
4208 4224 def test_bulk_copy_should_allow_adding_a_note
4209 4225 @request.session[:user_id] = 2
4210 4226 assert_difference 'Issue.count', 1 do
4211 4227 post :bulk_update, :ids => [1], :copy => '1',
4212 4228 :notes => 'Copying one issue',
4213 4229 :issue => {
4214 4230 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
4215 4231 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
4216 4232 }
4217 4233 end
4218 4234 issue = Issue.order('id DESC').first
4219 4235 assert_equal 1, issue.journals.size
4220 4236 journal = issue.journals.first
4221 4237 assert_equal 'Copying one issue', journal.notes
4222 4238 end
4223 4239
4224 4240 def test_bulk_copy_should_allow_not_copying_the_attachments
4225 4241 attachment_count = Issue.find(3).attachments.size
4226 4242 assert attachment_count > 0
4227 4243 @request.session[:user_id] = 2
4228 4244
4229 4245 assert_difference 'Issue.count', 1 do
4230 4246 assert_no_difference 'Attachment.count' do
4231 4247 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '0',
4232 4248 :issue => {
4233 4249 :project_id => ''
4234 4250 }
4235 4251 end
4236 4252 end
4237 4253 end
4238 4254
4239 4255 def test_bulk_copy_should_allow_copying_the_attachments
4240 4256 attachment_count = Issue.find(3).attachments.size
4241 4257 assert attachment_count > 0
4242 4258 @request.session[:user_id] = 2
4243 4259
4244 4260 assert_difference 'Issue.count', 1 do
4245 4261 assert_difference 'Attachment.count', attachment_count do
4246 4262 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '1',
4247 4263 :issue => {
4248 4264 :project_id => ''
4249 4265 }
4250 4266 end
4251 4267 end
4252 4268 end
4253 4269
4254 4270 def test_bulk_copy_should_add_relations_with_copied_issues
4255 4271 @request.session[:user_id] = 2
4256 4272
4257 4273 assert_difference 'Issue.count', 2 do
4258 4274 assert_difference 'IssueRelation.count', 2 do
4259 4275 post :bulk_update, :ids => [1, 3], :copy => '1', :link_copy => '1',
4260 4276 :issue => {
4261 4277 :project_id => '1'
4262 4278 }
4263 4279 end
4264 4280 end
4265 4281 end
4266 4282
4267 4283 def test_bulk_copy_should_allow_not_copying_the_subtasks
4268 4284 issue = Issue.generate_with_descendants!
4269 4285 @request.session[:user_id] = 2
4270 4286
4271 4287 assert_difference 'Issue.count', 1 do
4272 4288 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '0',
4273 4289 :issue => {
4274 4290 :project_id => ''
4275 4291 }
4276 4292 end
4277 4293 end
4278 4294
4279 4295 def test_bulk_copy_should_allow_copying_the_subtasks
4280 4296 issue = Issue.generate_with_descendants!
4281 4297 count = issue.descendants.count
4282 4298 @request.session[:user_id] = 2
4283 4299
4284 4300 assert_difference 'Issue.count', count+1 do
4285 4301 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '1',
4286 4302 :issue => {
4287 4303 :project_id => ''
4288 4304 }
4289 4305 end
4290 4306 copy = Issue.where(:parent_id => nil).order("id DESC").first
4291 4307 assert_equal count, copy.descendants.count
4292 4308 end
4293 4309
4294 4310 def test_bulk_copy_should_not_copy_selected_subtasks_twice
4295 4311 issue = Issue.generate_with_descendants!
4296 4312 count = issue.descendants.count
4297 4313 @request.session[:user_id] = 2
4298 4314
4299 4315 assert_difference 'Issue.count', count+1 do
4300 4316 post :bulk_update, :ids => issue.self_and_descendants.map(&:id), :copy => '1', :copy_subtasks => '1',
4301 4317 :issue => {
4302 4318 :project_id => ''
4303 4319 }
4304 4320 end
4305 4321 copy = Issue.where(:parent_id => nil).order("id DESC").first
4306 4322 assert_equal count, copy.descendants.count
4307 4323 end
4308 4324
4309 4325 def test_bulk_copy_to_another_project_should_follow_when_needed
4310 4326 @request.session[:user_id] = 2
4311 4327 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
4312 4328 issue = Issue.order('id DESC').first
4313 4329 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
4314 4330 end
4315 4331
4316 4332 def test_bulk_copy_with_all_failures_should_display_errors
4317 4333 @request.session[:user_id] = 2
4318 4334 post :bulk_update, :ids => [1, 2], :copy => '1', :issue => {:start_date => 'foo'}
4319 4335
4320 4336 assert_response :success
4321 4337 end
4322 4338
4323 4339 def test_destroy_issue_with_no_time_entries
4324 4340 assert_nil TimeEntry.find_by_issue_id(2)
4325 4341 @request.session[:user_id] = 2
4326 4342
4327 4343 assert_difference 'Issue.count', -1 do
4328 4344 delete :destroy, :id => 2
4329 4345 end
4330 4346 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4331 4347 assert_nil Issue.find_by_id(2)
4332 4348 end
4333 4349
4334 4350 def test_destroy_issues_with_time_entries
4335 4351 @request.session[:user_id] = 2
4336 4352
4337 4353 assert_no_difference 'Issue.count' do
4338 4354 delete :destroy, :ids => [1, 3]
4339 4355 end
4340 4356 assert_response :success
4341 4357 assert_template 'destroy'
4342 4358 assert_not_nil assigns(:hours)
4343 4359 assert Issue.find_by_id(1) && Issue.find_by_id(3)
4344 4360
4345 4361 assert_select 'form' do
4346 4362 assert_select 'input[name=_method][value=delete]'
4347 4363 end
4348 4364 end
4349 4365
4350 4366 def test_destroy_issues_and_destroy_time_entries
4351 4367 @request.session[:user_id] = 2
4352 4368
4353 4369 assert_difference 'Issue.count', -2 do
4354 4370 assert_difference 'TimeEntry.count', -3 do
4355 4371 delete :destroy, :ids => [1, 3], :todo => 'destroy'
4356 4372 end
4357 4373 end
4358 4374 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4359 4375 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4360 4376 assert_nil TimeEntry.find_by_id([1, 2])
4361 4377 end
4362 4378
4363 4379 def test_destroy_issues_and_assign_time_entries_to_project
4364 4380 @request.session[:user_id] = 2
4365 4381
4366 4382 assert_difference 'Issue.count', -2 do
4367 4383 assert_no_difference 'TimeEntry.count' do
4368 4384 delete :destroy, :ids => [1, 3], :todo => 'nullify'
4369 4385 end
4370 4386 end
4371 4387 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4372 4388 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4373 4389 assert_nil TimeEntry.find(1).issue_id
4374 4390 assert_nil TimeEntry.find(2).issue_id
4375 4391 end
4376 4392
4377 4393 def test_destroy_issues_and_reassign_time_entries_to_another_issue
4378 4394 @request.session[:user_id] = 2
4379 4395
4380 4396 assert_difference 'Issue.count', -2 do
4381 4397 assert_no_difference 'TimeEntry.count' do
4382 4398 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
4383 4399 end
4384 4400 end
4385 4401 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4386 4402 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4387 4403 assert_equal 2, TimeEntry.find(1).issue_id
4388 4404 assert_equal 2, TimeEntry.find(2).issue_id
4389 4405 end
4390 4406
4391 4407 def test_destroy_issues_and_reassign_time_entries_to_an_invalid_issue_should_fail
4392 4408 @request.session[:user_id] = 2
4393 4409
4394 4410 assert_no_difference 'Issue.count' do
4395 4411 assert_no_difference 'TimeEntry.count' do
4396 4412 # try to reassign time to an issue of another project
4397 4413 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 4
4398 4414 end
4399 4415 end
4400 4416 assert_response :success
4401 4417 assert_template 'destroy'
4402 4418 end
4403 4419
4404 4420 def test_destroy_issues_from_different_projects
4405 4421 @request.session[:user_id] = 2
4406 4422
4407 4423 assert_difference 'Issue.count', -3 do
4408 4424 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
4409 4425 end
4410 4426 assert_redirected_to :controller => 'issues', :action => 'index'
4411 4427 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
4412 4428 end
4413 4429
4414 4430 def test_destroy_parent_and_child_issues
4415 4431 parent = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Parent Issue')
4416 4432 child = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Child Issue', :parent_issue_id => parent.id)
4417 4433 assert child.is_descendant_of?(parent.reload)
4418 4434
4419 4435 @request.session[:user_id] = 2
4420 4436 assert_difference 'Issue.count', -2 do
4421 4437 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
4422 4438 end
4423 4439 assert_response 302
4424 4440 end
4425 4441
4426 4442 def test_destroy_invalid_should_respond_with_404
4427 4443 @request.session[:user_id] = 2
4428 4444 assert_no_difference 'Issue.count' do
4429 4445 delete :destroy, :id => 999
4430 4446 end
4431 4447 assert_response 404
4432 4448 end
4433 4449
4434 4450 def test_default_search_scope
4435 4451 get :index
4436 4452
4437 4453 assert_select 'div#quick-search form' do
4438 4454 assert_select 'input[name=issues][value="1"][type=hidden]'
4439 4455 end
4440 4456 end
4441 4457
4442 4458 def setup_user_with_copy_but_not_add_permission
4443 4459 Role.all.each {|r| r.remove_permission! :add_issues}
4444 4460 Role.find_by_name('Manager').add_permission! :add_issues
4445 4461 user = User.generate!
4446 4462 User.add_to_project(user, Project.find(1), Role.find_by_name('Developer'))
4447 4463 User.add_to_project(user, Project.find(2), Role.find_by_name('Manager'))
4448 4464 user
4449 4465 end
4450 4466 end
General Comments 0
You need to be logged in to leave comments. Login now