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