##// END OF EJS Templates
Only show statuses that can be applied to all issues on the bulk edit form (#10181)....
Jean-Philippe Lang -
r8724:2ec55c5337f9
parent child
Show More
@@ -1,428 +1,428
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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, :move, :perform_move, :destroy]
24 24 before_filter :check_project_uniqueness, :only => [:move, :perform_move]
25 25 before_filter :find_project, :only => [:new, :create]
26 26 before_filter :authorize, :except => [:index]
27 27 before_filter :find_optional_project, :only => [:index]
28 28 before_filter :check_for_default_issue_status, :only => [:new, :create]
29 29 before_filter :build_new_issue_from_params, :only => [:new, :create]
30 30 accept_rss_auth :index, :show
31 31 accept_api_auth :index, :show, :create, :update, :destroy
32 32
33 33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
34 34
35 35 helper :journals
36 36 helper :projects
37 37 include ProjectsHelper
38 38 helper :custom_fields
39 39 include CustomFieldsHelper
40 40 helper :issue_relations
41 41 include IssueRelationsHelper
42 42 helper :watchers
43 43 include WatchersHelper
44 44 helper :attachments
45 45 include AttachmentsHelper
46 46 helper :queries
47 47 include QueriesHelper
48 48 helper :repositories
49 49 include RepositoriesHelper
50 50 helper :sort
51 51 include SortHelper
52 52 include IssuesHelper
53 53 helper :timelog
54 54 helper :gantt
55 55 include Redmine::Export::PDF
56 56
57 57 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
58 58 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
59 59 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
60 60
61 61 def index
62 62 retrieve_query
63 63 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
64 64 sort_update(@query.sortable_columns)
65 65
66 66 if @query.valid?
67 67 case params[:format]
68 68 when 'csv', 'pdf'
69 69 @limit = Setting.issues_export_limit.to_i
70 70 when 'atom'
71 71 @limit = Setting.feeds_limit.to_i
72 72 when 'xml', 'json'
73 73 @offset, @limit = api_offset_and_limit
74 74 else
75 75 @limit = per_page_option
76 76 end
77 77
78 78 @issue_count = @query.issue_count
79 79 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
80 80 @offset ||= @issue_pages.current.offset
81 81 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
82 82 :order => sort_clause,
83 83 :offset => @offset,
84 84 :limit => @limit)
85 85 @issue_count_by_group = @query.issue_count_by_group
86 86
87 87 respond_to do |format|
88 88 format.html { render :template => 'issues/index', :layout => !request.xhr? }
89 89 format.api {
90 90 Issue.load_relations(@issues) if include_in_api_response?('relations')
91 91 }
92 92 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
93 93 format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
94 94 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
95 95 end
96 96 else
97 97 respond_to do |format|
98 98 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
99 99 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
100 100 format.api { render_validation_errors(@query) }
101 101 end
102 102 end
103 103 rescue ActiveRecord::RecordNotFound
104 104 render_404
105 105 end
106 106
107 107 def show
108 108 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
109 109 @journals.each_with_index {|j,i| j.indice = i+1}
110 110 @journals.reverse! if User.current.wants_comments_in_reverse_order?
111 111
112 112 @changesets = @issue.changesets.visible.all
113 113 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
114 114
115 115 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
116 116 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
117 117 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
118 118 @priorities = IssuePriority.active
119 119 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
120 120 respond_to do |format|
121 121 format.html {
122 122 retrieve_previous_and_next_issue_ids
123 123 render :template => 'issues/show'
124 124 }
125 125 format.api
126 126 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
127 127 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
128 128 end
129 129 end
130 130
131 131 # Add a new issue
132 132 # The new issue will be created from an existing one if copy_from parameter is given
133 133 def new
134 134 respond_to do |format|
135 135 format.html { render :action => 'new', :layout => !request.xhr? }
136 136 format.js {
137 137 render(:update) { |page|
138 138 if params[:project_change]
139 139 page.replace_html 'all_attributes', :partial => 'form'
140 140 else
141 141 page.replace_html 'attributes', :partial => 'attributes'
142 142 end
143 143 m = User.current.allowed_to?(:log_time, @issue.project) ? 'show' : 'hide'
144 144 page << "if ($('log_time')) {Element.#{m}('log_time');}"
145 145 }
146 146 }
147 147 end
148 148 end
149 149
150 150 def create
151 151 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
152 152 if @issue.save
153 153 attachments = Attachment.attach_files(@issue, params[:attachments])
154 154 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
155 155 respond_to do |format|
156 156 format.html {
157 157 render_attachment_warning_if_needed(@issue)
158 158 flash[:notice] = l(:notice_issue_successful_create, :id => "<a href='#{issue_path(@issue)}'>##{@issue.id}</a>")
159 159 redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
160 160 { :action => 'show', :id => @issue })
161 161 }
162 162 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
163 163 end
164 164 return
165 165 else
166 166 respond_to do |format|
167 167 format.html { render :action => 'new' }
168 168 format.api { render_validation_errors(@issue) }
169 169 end
170 170 end
171 171 end
172 172
173 173 def edit
174 174 return unless update_issue_from_params
175 175
176 176 respond_to do |format|
177 177 format.html { }
178 178 format.xml { }
179 179 end
180 180 end
181 181
182 182 def update
183 183 return unless update_issue_from_params
184 184 saved = false
185 185 begin
186 186 saved = @issue.save_issue_with_child_records(params, @time_entry)
187 187 rescue ActiveRecord::StaleObjectError
188 188 @conflict = true
189 189 if params[:last_journal_id]
190 190 if params[:last_journal_id].present?
191 191 last_journal_id = params[:last_journal_id].to_i
192 192 @conflict_journals = @issue.journals.all(:conditions => ["#{Journal.table_name}.id > ?", last_journal_id])
193 193 else
194 194 @conflict_journals = @issue.journals.all
195 195 end
196 196 end
197 197 end
198 198
199 199 if saved
200 200 render_attachment_warning_if_needed(@issue)
201 201 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
202 202
203 203 respond_to do |format|
204 204 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
205 205 format.api { head :ok }
206 206 end
207 207 else
208 208 respond_to do |format|
209 209 format.html { render :action => 'edit' }
210 210 format.api { render_validation_errors(@issue) }
211 211 end
212 212 end
213 213 end
214 214
215 215 # Bulk edit/copy a set of issues
216 216 def bulk_edit
217 217 @issues.sort!
218 218 @copy = params[:copy].present?
219 219 @notes = params[:notes]
220 220
221 221 if User.current.allowed_to?(:move_issues, @projects)
222 222 @allowed_projects = Issue.allowed_target_projects_on_move
223 223 if params[:issue]
224 224 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id]}
225 225 if @target_project
226 226 target_projects = [@target_project]
227 227 end
228 228 end
229 229 end
230 230 target_projects ||= @projects
231 231
232 @available_statuses = target_projects.map{|p|Workflow.available_statuses(p)}.reduce(:&)
232 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
233 233 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
234 234 @assignables = target_projects.map(&:assignable_users).reduce(:&)
235 235 @trackers = target_projects.map(&:trackers).reduce(:&)
236 236
237 237 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
238 238 render :layout => false if request.xhr?
239 239 end
240 240
241 241 def bulk_update
242 242 @issues.sort!
243 243 @copy = params[:copy].present?
244 244 attributes = parse_params_for_bulk_issue_attributes(params)
245 245
246 246 unsaved_issue_ids = []
247 247 moved_issues = []
248 248 @issues.each do |issue|
249 249 issue.reload
250 250 if @copy
251 251 issue = issue.copy
252 252 end
253 253 journal = issue.init_journal(User.current, params[:notes])
254 254 issue.safe_attributes = attributes
255 255 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
256 256 if issue.save
257 257 moved_issues << issue
258 258 else
259 259 # Keep unsaved issue ids to display them in flash error
260 260 unsaved_issue_ids << issue.id
261 261 end
262 262 end
263 263 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
264 264
265 265 if params[:follow]
266 266 if @issues.size == 1 && moved_issues.size == 1
267 267 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
268 268 elsif moved_issues.map(&:project).uniq.size == 1
269 269 redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first
270 270 end
271 271 else
272 272 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
273 273 end
274 274 end
275 275
276 276 verify :method => :delete, :only => :destroy, :render => { :nothing => true, :status => :method_not_allowed }
277 277 def destroy
278 278 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
279 279 if @hours > 0
280 280 case params[:todo]
281 281 when 'destroy'
282 282 # nothing to do
283 283 when 'nullify'
284 284 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
285 285 when 'reassign'
286 286 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
287 287 if reassign_to.nil?
288 288 flash.now[:error] = l(:error_issue_not_found_in_project)
289 289 return
290 290 else
291 291 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
292 292 end
293 293 else
294 294 # display the destroy form if it's a user request
295 295 return unless api_request?
296 296 end
297 297 end
298 298 @issues.each do |issue|
299 299 begin
300 300 issue.reload.destroy
301 301 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
302 302 # nothing to do, issue was already deleted (eg. by a parent)
303 303 end
304 304 end
305 305 respond_to do |format|
306 306 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
307 307 format.api { head :ok }
308 308 end
309 309 end
310 310
311 311 private
312 312 def find_issue
313 313 # Issue.visible.find(...) can not be used to redirect user to the login form
314 314 # if the issue actually exists but requires authentication
315 315 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
316 316 unless @issue.visible?
317 317 deny_access
318 318 return
319 319 end
320 320 @project = @issue.project
321 321 rescue ActiveRecord::RecordNotFound
322 322 render_404
323 323 end
324 324
325 325 def find_project
326 326 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
327 327 @project = Project.find(project_id)
328 328 rescue ActiveRecord::RecordNotFound
329 329 render_404
330 330 end
331 331
332 332 def retrieve_previous_and_next_issue_ids
333 333 retrieve_query_from_session
334 334 if @query
335 335 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
336 336 sort_update(@query.sortable_columns, 'issues_index_sort')
337 337 limit = 500
338 338 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
339 339 if (idx = issue_ids.index(@issue.id)) && idx < limit
340 340 if issue_ids.size < 500
341 341 @issue_position = idx + 1
342 342 @issue_count = issue_ids.size
343 343 end
344 344 @prev_issue_id = issue_ids[idx - 1] if idx > 0
345 345 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
346 346 end
347 347 end
348 348 end
349 349
350 350 # Used by #edit and #update to set some common instance variables
351 351 # from the params
352 352 # TODO: Refactor, not everything in here is needed by #edit
353 353 def update_issue_from_params
354 354 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
355 355 @priorities = IssuePriority.active
356 356 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
357 357 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
358 358 @time_entry.attributes = params[:time_entry]
359 359
360 360 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
361 361 @issue.init_journal(User.current, @notes)
362 362
363 363 issue_attributes = params[:issue]
364 364 if issue_attributes && params[:conflict_resolution]
365 365 case params[:conflict_resolution]
366 366 when 'overwrite'
367 367 issue_attributes = issue_attributes.dup
368 368 issue_attributes.delete(:lock_version)
369 369 when 'add_notes'
370 370 issue_attributes = {}
371 371 when 'cancel'
372 372 redirect_to issue_path(@issue)
373 373 return false
374 374 end
375 375 end
376 376 @issue.safe_attributes = issue_attributes
377 377 true
378 378 end
379 379
380 380 # TODO: Refactor, lots of extra code in here
381 381 # TODO: Changing tracker on an existing issue should not trigger this
382 382 def build_new_issue_from_params
383 383 if params[:id].blank?
384 384 @issue = Issue.new
385 385 if params[:copy_from]
386 386 begin
387 387 @copy_from = Issue.visible.find(params[:copy_from])
388 388 @copy_attachments = params[:copy_attachments].present? || request.get?
389 389 @issue.copy_from(@copy_from, :attachments => @copy_attachments)
390 390 rescue ActiveRecord::RecordNotFound
391 391 render_404
392 392 return
393 393 end
394 394 end
395 395 @issue.project = @project
396 396 else
397 397 @issue = @project.issues.visible.find(params[:id])
398 398 end
399 399
400 400 @issue.project = @project
401 401 @issue.author = User.current
402 402 # Tracker must be set before custom field values
403 403 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
404 404 if @issue.tracker.nil?
405 405 render_error l(:error_no_tracker_in_project)
406 406 return false
407 407 end
408 408 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
409 409 @issue.safe_attributes = params[:issue]
410 410
411 411 @priorities = IssuePriority.active
412 412 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
413 413 end
414 414
415 415 def check_for_default_issue_status
416 416 if IssueStatus.default.nil?
417 417 render_error l(:error_no_default_issue_status)
418 418 return false
419 419 end
420 420 end
421 421
422 422 def parse_params_for_bulk_issue_attributes(params)
423 423 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
424 424 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
425 425 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
426 426 attributes
427 427 end
428 428 end
@@ -1,2938 +1,2958
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 require 'issues_controller'
20 20
21 21 class IssuesControllerTest < ActionController::TestCase
22 22 fixtures :projects,
23 23 :users,
24 24 :roles,
25 25 :members,
26 26 :member_roles,
27 27 :issues,
28 28 :issue_statuses,
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 @controller = IssuesController.new
52 52 @request = ActionController::TestRequest.new
53 53 @response = ActionController::TestResponse.new
54 54 User.current = nil
55 55 end
56 56
57 57 def test_index
58 58 Setting.default_language = 'en'
59 59
60 60 get :index
61 61 assert_response :success
62 62 assert_template 'index'
63 63 assert_not_nil assigns(:issues)
64 64 assert_nil assigns(:project)
65 65 assert_tag :tag => 'a', :content => /Can't print recipes/
66 66 assert_tag :tag => 'a', :content => /Subproject issue/
67 67 # private projects hidden
68 68 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
69 69 assert_no_tag :tag => 'a', :content => /Issue on project 2/
70 70 # project column
71 71 assert_tag :tag => 'th', :content => /Project/
72 72 end
73 73
74 74 def test_index_should_not_list_issues_when_module_disabled
75 75 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
76 76 get :index
77 77 assert_response :success
78 78 assert_template 'index'
79 79 assert_not_nil assigns(:issues)
80 80 assert_nil assigns(:project)
81 81 assert_no_tag :tag => 'a', :content => /Can't print recipes/
82 82 assert_tag :tag => 'a', :content => /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 assert_tag :tag => 'a', :content => /Can't print recipes/
99 99 assert_no_tag :tag => 'a', :content => /Subproject issue/
100 100 end
101 101
102 102 def test_index_with_project_and_subprojects
103 103 Setting.display_subprojects_issues = 1
104 104 get :index, :project_id => 1
105 105 assert_response :success
106 106 assert_template 'index'
107 107 assert_not_nil assigns(:issues)
108 108 assert_tag :tag => 'a', :content => /Can't print recipes/
109 109 assert_tag :tag => 'a', :content => /Subproject issue/
110 110 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
111 111 end
112 112
113 113 def test_index_with_project_and_subprojects_should_show_private_subprojects
114 114 @request.session[:user_id] = 2
115 115 Setting.display_subprojects_issues = 1
116 116 get :index, :project_id => 1
117 117 assert_response :success
118 118 assert_template 'index'
119 119 assert_not_nil assigns(:issues)
120 120 assert_tag :tag => 'a', :content => /Can't print recipes/
121 121 assert_tag :tag => 'a', :content => /Subproject issue/
122 122 assert_tag :tag => 'a', :content => /Issue of a private subproject/
123 123 end
124 124
125 125 def test_index_with_project_and_default_filter
126 126 get :index, :project_id => 1, :set_filter => 1
127 127 assert_response :success
128 128 assert_template 'index'
129 129 assert_not_nil assigns(:issues)
130 130
131 131 query = assigns(:query)
132 132 assert_not_nil query
133 133 # default filter
134 134 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
135 135 end
136 136
137 137 def test_index_with_project_and_filter
138 138 get :index, :project_id => 1, :set_filter => 1,
139 139 :f => ['tracker_id'],
140 140 :op => {'tracker_id' => '='},
141 141 :v => {'tracker_id' => ['1']}
142 142 assert_response :success
143 143 assert_template 'index'
144 144 assert_not_nil assigns(:issues)
145 145
146 146 query = assigns(:query)
147 147 assert_not_nil query
148 148 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
149 149 end
150 150
151 151 def test_index_with_short_filters
152 152
153 153 to_test = {
154 154 'status_id' => {
155 155 'o' => { :op => 'o', :values => [''] },
156 156 'c' => { :op => 'c', :values => [''] },
157 157 '7' => { :op => '=', :values => ['7'] },
158 158 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
159 159 '=7' => { :op => '=', :values => ['7'] },
160 160 '!3' => { :op => '!', :values => ['3'] },
161 161 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
162 162 'subject' => {
163 163 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
164 164 'o' => { :op => '=', :values => ['o'] },
165 165 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
166 166 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
167 167 'tracker_id' => {
168 168 '3' => { :op => '=', :values => ['3'] },
169 169 '=3' => { :op => '=', :values => ['3'] }},
170 170 'start_date' => {
171 171 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
172 172 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
173 173 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
174 174 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
175 175 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
176 176 '<t+2' => { :op => '<t+', :values => ['2'] },
177 177 '>t+2' => { :op => '>t+', :values => ['2'] },
178 178 't+2' => { :op => 't+', :values => ['2'] },
179 179 't' => { :op => 't', :values => [''] },
180 180 'w' => { :op => 'w', :values => [''] },
181 181 '>t-2' => { :op => '>t-', :values => ['2'] },
182 182 '<t-2' => { :op => '<t-', :values => ['2'] },
183 183 't-2' => { :op => 't-', :values => ['2'] }},
184 184 'created_on' => {
185 185 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
186 186 '<t+2' => { :op => '=', :values => ['<t+2'] },
187 187 '>t+2' => { :op => '=', :values => ['>t+2'] },
188 188 't+2' => { :op => 't', :values => ['+2'] }},
189 189 'cf_1' => {
190 190 'c' => { :op => '=', :values => ['c'] },
191 191 '!c' => { :op => '!', :values => ['c'] },
192 192 '!*' => { :op => '!*', :values => [''] },
193 193 '*' => { :op => '*', :values => [''] }},
194 194 'estimated_hours' => {
195 195 '=13.4' => { :op => '=', :values => ['13.4'] },
196 196 '>=45' => { :op => '>=', :values => ['45'] },
197 197 '<=125' => { :op => '<=', :values => ['125'] },
198 198 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
199 199 '!*' => { :op => '!*', :values => [''] },
200 200 '*' => { :op => '*', :values => [''] }}
201 201 }
202 202
203 203 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
204 204
205 205 to_test.each do |field, expression_and_expected|
206 206 expression_and_expected.each do |filter_expression, expected|
207 207
208 208 get :index, :set_filter => 1, field => filter_expression
209 209
210 210 assert_response :success
211 211 assert_template 'index'
212 212 assert_not_nil assigns(:issues)
213 213
214 214 query = assigns(:query)
215 215 assert_not_nil query
216 216 assert query.has_filter?(field)
217 217 assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
218 218 end
219 219 end
220 220
221 221 end
222 222
223 223 def test_index_with_project_and_empty_filters
224 224 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
225 225 assert_response :success
226 226 assert_template 'index'
227 227 assert_not_nil assigns(:issues)
228 228
229 229 query = assigns(:query)
230 230 assert_not_nil query
231 231 # no filter
232 232 assert_equal({}, query.filters)
233 233 end
234 234
235 235 def test_index_with_query
236 236 get :index, :project_id => 1, :query_id => 5
237 237 assert_response :success
238 238 assert_template 'index'
239 239 assert_not_nil assigns(:issues)
240 240 assert_nil assigns(:issue_count_by_group)
241 241 end
242 242
243 243 def test_index_with_query_grouped_by_tracker
244 244 get :index, :project_id => 1, :query_id => 6
245 245 assert_response :success
246 246 assert_template 'index'
247 247 assert_not_nil assigns(:issues)
248 248 assert_not_nil assigns(:issue_count_by_group)
249 249 end
250 250
251 251 def test_index_with_query_grouped_by_list_custom_field
252 252 get :index, :project_id => 1, :query_id => 9
253 253 assert_response :success
254 254 assert_template 'index'
255 255 assert_not_nil assigns(:issues)
256 256 assert_not_nil assigns(:issue_count_by_group)
257 257 end
258 258
259 259 def test_index_with_query_id_and_project_id_should_set_session_query
260 260 get :index, :project_id => 1, :query_id => 4
261 261 assert_response :success
262 262 assert_kind_of Hash, session[:query]
263 263 assert_equal 4, session[:query][:id]
264 264 assert_equal 1, session[:query][:project_id]
265 265 end
266 266
267 267 def test_index_with_cross_project_query_in_session_should_show_project_issues
268 268 q = Query.create!(:name => "test", :user_id => 2, :is_public => false, :project => nil)
269 269 @request.session[:query] = {:id => q.id, :project_id => 1}
270 270
271 271 with_settings :display_subprojects_issues => '0' do
272 272 get :index, :project_id => 1
273 273 end
274 274 assert_response :success
275 275 assert_not_nil assigns(:query)
276 276 assert_equal q.id, assigns(:query).id
277 277 assert_equal 1, assigns(:query).project_id
278 278 assert_equal [1], assigns(:issues).map(&:project_id).uniq
279 279 end
280 280
281 281 def test_private_query_should_not_be_available_to_other_users
282 282 q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
283 283 @request.session[:user_id] = 3
284 284
285 285 get :index, :query_id => q.id
286 286 assert_response 403
287 287 end
288 288
289 289 def test_private_query_should_be_available_to_its_user
290 290 q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
291 291 @request.session[:user_id] = 2
292 292
293 293 get :index, :query_id => q.id
294 294 assert_response :success
295 295 end
296 296
297 297 def test_public_query_should_be_available_to_other_users
298 298 q = Query.create!(:name => "private", :user => User.find(2), :is_public => true, :project => nil)
299 299 @request.session[:user_id] = 3
300 300
301 301 get :index, :query_id => q.id
302 302 assert_response :success
303 303 end
304 304
305 305 def test_index_csv
306 306 get :index, :format => 'csv'
307 307 assert_response :success
308 308 assert_not_nil assigns(:issues)
309 309 assert_equal 'text/csv', @response.content_type
310 310 assert @response.body.starts_with?("#,")
311 311 lines = @response.body.chomp.split("\n")
312 312 assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
313 313 end
314 314
315 315 def test_index_csv_with_project
316 316 get :index, :project_id => 1, :format => 'csv'
317 317 assert_response :success
318 318 assert_not_nil assigns(:issues)
319 319 assert_equal 'text/csv', @response.content_type
320 320 end
321 321
322 322 def test_index_csv_with_description
323 323 get :index, :format => 'csv', :description => '1'
324 324 assert_response :success
325 325 assert_not_nil assigns(:issues)
326 326 assert_equal 'text/csv', @response.content_type
327 327 assert @response.body.starts_with?("#,")
328 328 lines = @response.body.chomp.split("\n")
329 329 assert_equal assigns(:query).columns.size + 2, lines[0].split(',').size
330 330 end
331 331
332 332 def test_index_csv_with_spent_time_column
333 333 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column')
334 334 TimeEntry.generate!(:project_id => issue.project_id, :issue_id => issue.id, :hours => 7.33)
335 335
336 336 get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
337 337 assert_response :success
338 338 assert_equal 'text/csv', @response.content_type
339 339 lines = @response.body.chomp.split("\n")
340 340 assert_include "#{issue.id},#{issue.subject},7.33", lines
341 341 end
342 342
343 343 def test_index_csv_with_all_columns
344 344 get :index, :format => 'csv', :columns => 'all'
345 345 assert_response :success
346 346 assert_not_nil assigns(:issues)
347 347 assert_equal 'text/csv', @response.content_type
348 348 assert @response.body.starts_with?("#,")
349 349 lines = @response.body.chomp.split("\n")
350 350 assert_equal assigns(:query).available_columns.size + 1, lines[0].split(',').size
351 351 end
352 352
353 353 def test_index_csv_with_multi_column_field
354 354 CustomField.find(1).update_attribute :multiple, true
355 355 issue = Issue.find(1)
356 356 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
357 357 issue.save!
358 358
359 359 get :index, :format => 'csv', :columns => 'all'
360 360 assert_response :success
361 361 lines = @response.body.chomp.split("\n")
362 362 assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
363 363 end
364 364
365 365 def test_index_csv_big_5
366 366 with_settings :default_language => "zh-TW" do
367 367 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
368 368 str_big5 = "\xa4@\xa4\xeb"
369 369 if str_utf8.respond_to?(:force_encoding)
370 370 str_utf8.force_encoding('UTF-8')
371 371 str_big5.force_encoding('Big5')
372 372 end
373 373 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
374 374 :status_id => 1, :priority => IssuePriority.all.first,
375 375 :subject => str_utf8)
376 376 assert issue.save
377 377
378 378 get :index, :project_id => 1,
379 379 :f => ['subject'],
380 380 :op => '=', :values => [str_utf8],
381 381 :format => 'csv'
382 382 assert_equal 'text/csv', @response.content_type
383 383 lines = @response.body.chomp.split("\n")
384 384 s1 = "\xaa\xac\xbaA"
385 385 if str_utf8.respond_to?(:force_encoding)
386 386 s1.force_encoding('Big5')
387 387 end
388 388 assert lines[0].include?(s1)
389 389 assert lines[1].include?(str_big5)
390 390 end
391 391 end
392 392
393 393 def test_index_csv_cannot_convert_should_be_replaced_big_5
394 394 with_settings :default_language => "zh-TW" do
395 395 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
396 396 if str_utf8.respond_to?(:force_encoding)
397 397 str_utf8.force_encoding('UTF-8')
398 398 end
399 399 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
400 400 :status_id => 1, :priority => IssuePriority.all.first,
401 401 :subject => str_utf8)
402 402 assert issue.save
403 403
404 404 get :index, :project_id => 1,
405 405 :f => ['subject'],
406 406 :op => '=', :values => [str_utf8],
407 407 :c => ['status', 'subject'],
408 408 :format => 'csv',
409 409 :set_filter => 1
410 410 assert_equal 'text/csv', @response.content_type
411 411 lines = @response.body.chomp.split("\n")
412 412 s1 = "\xaa\xac\xbaA" # status
413 413 if str_utf8.respond_to?(:force_encoding)
414 414 s1.force_encoding('Big5')
415 415 end
416 416 assert lines[0].include?(s1)
417 417 s2 = lines[1].split(",")[2]
418 418 if s1.respond_to?(:force_encoding)
419 419 s3 = "\xa5H?" # subject
420 420 s3.force_encoding('Big5')
421 421 assert_equal s3, s2
422 422 elsif RUBY_PLATFORM == 'java'
423 423 assert_equal "??", s2
424 424 else
425 425 assert_equal "\xa5H???", s2
426 426 end
427 427 end
428 428 end
429 429
430 430 def test_index_csv_tw
431 431 with_settings :default_language => "zh-TW" do
432 432 str1 = "test_index_csv_tw"
433 433 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
434 434 :status_id => 1, :priority => IssuePriority.all.first,
435 435 :subject => str1, :estimated_hours => '1234.5')
436 436 assert issue.save
437 437 assert_equal 1234.5, issue.estimated_hours
438 438
439 439 get :index, :project_id => 1,
440 440 :f => ['subject'],
441 441 :op => '=', :values => [str1],
442 442 :c => ['estimated_hours', 'subject'],
443 443 :format => 'csv',
444 444 :set_filter => 1
445 445 assert_equal 'text/csv', @response.content_type
446 446 lines = @response.body.chomp.split("\n")
447 447 assert_equal "#{issue.id},1234.50,#{str1}", lines[1]
448 448
449 449 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
450 450 if str_tw.respond_to?(:force_encoding)
451 451 str_tw.force_encoding('UTF-8')
452 452 end
453 453 assert_equal str_tw, l(:general_lang_name)
454 454 assert_equal ',', l(:general_csv_separator)
455 455 assert_equal '.', l(:general_csv_decimal_separator)
456 456 end
457 457 end
458 458
459 459 def test_index_csv_fr
460 460 with_settings :default_language => "fr" do
461 461 str1 = "test_index_csv_fr"
462 462 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
463 463 :status_id => 1, :priority => IssuePriority.all.first,
464 464 :subject => str1, :estimated_hours => '1234.5')
465 465 assert issue.save
466 466 assert_equal 1234.5, issue.estimated_hours
467 467
468 468 get :index, :project_id => 1,
469 469 :f => ['subject'],
470 470 :op => '=', :values => [str1],
471 471 :c => ['estimated_hours', 'subject'],
472 472 :format => 'csv',
473 473 :set_filter => 1
474 474 assert_equal 'text/csv', @response.content_type
475 475 lines = @response.body.chomp.split("\n")
476 476 assert_equal "#{issue.id};1234,50;#{str1}", lines[1]
477 477
478 478 str_fr = "Fran\xc3\xa7ais"
479 479 if str_fr.respond_to?(:force_encoding)
480 480 str_fr.force_encoding('UTF-8')
481 481 end
482 482 assert_equal str_fr, l(:general_lang_name)
483 483 assert_equal ';', l(:general_csv_separator)
484 484 assert_equal ',', l(:general_csv_decimal_separator)
485 485 end
486 486 end
487 487
488 488 def test_index_pdf
489 489 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
490 490 with_settings :default_language => lang do
491 491
492 492 get :index
493 493 assert_response :success
494 494 assert_template 'index'
495 495
496 496 if lang == "ja"
497 497 if RUBY_PLATFORM != 'java'
498 498 assert_equal "CP932", l(:general_pdf_encoding)
499 499 end
500 500 if RUBY_PLATFORM == 'java' && l(:general_pdf_encoding) == "CP932"
501 501 next
502 502 end
503 503 end
504 504
505 505 get :index, :format => 'pdf'
506 506 assert_response :success
507 507 assert_not_nil assigns(:issues)
508 508 assert_equal 'application/pdf', @response.content_type
509 509
510 510 get :index, :project_id => 1, :format => 'pdf'
511 511 assert_response :success
512 512 assert_not_nil assigns(:issues)
513 513 assert_equal 'application/pdf', @response.content_type
514 514
515 515 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
516 516 assert_response :success
517 517 assert_not_nil assigns(:issues)
518 518 assert_equal 'application/pdf', @response.content_type
519 519 end
520 520 end
521 521 end
522 522
523 523 def test_index_pdf_with_query_grouped_by_list_custom_field
524 524 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
525 525 assert_response :success
526 526 assert_not_nil assigns(:issues)
527 527 assert_not_nil assigns(:issue_count_by_group)
528 528 assert_equal 'application/pdf', @response.content_type
529 529 end
530 530
531 531 def test_index_sort
532 532 get :index, :sort => 'tracker,id:desc'
533 533 assert_response :success
534 534
535 535 sort_params = @request.session['issues_index_sort']
536 536 assert sort_params.is_a?(String)
537 537 assert_equal 'tracker,id:desc', sort_params
538 538
539 539 issues = assigns(:issues)
540 540 assert_not_nil issues
541 541 assert !issues.empty?
542 542 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
543 543 end
544 544
545 545 def test_index_sort_by_field_not_included_in_columns
546 546 Setting.issue_list_default_columns = %w(subject author)
547 547 get :index, :sort => 'tracker'
548 548 end
549 549
550 550 def test_index_sort_by_assigned_to
551 551 get :index, :sort => 'assigned_to'
552 552 assert_response :success
553 553 assignees = assigns(:issues).collect(&:assigned_to).compact
554 554 assert_equal assignees.sort, assignees
555 555 end
556 556
557 557 def test_index_sort_by_assigned_to_desc
558 558 get :index, :sort => 'assigned_to:desc'
559 559 assert_response :success
560 560 assignees = assigns(:issues).collect(&:assigned_to).compact
561 561 assert_equal assignees.sort.reverse, assignees
562 562 end
563 563
564 564 def test_index_group_by_assigned_to
565 565 get :index, :group_by => 'assigned_to', :sort => 'priority'
566 566 assert_response :success
567 567 end
568 568
569 569 def test_index_sort_by_author
570 570 get :index, :sort => 'author'
571 571 assert_response :success
572 572 authors = assigns(:issues).collect(&:author)
573 573 assert_equal authors.sort, authors
574 574 end
575 575
576 576 def test_index_sort_by_author_desc
577 577 get :index, :sort => 'author:desc'
578 578 assert_response :success
579 579 authors = assigns(:issues).collect(&:author)
580 580 assert_equal authors.sort.reverse, authors
581 581 end
582 582
583 583 def test_index_group_by_author
584 584 get :index, :group_by => 'author', :sort => 'priority'
585 585 assert_response :success
586 586 end
587 587
588 588 def test_index_sort_by_spent_hours
589 589 get :index, :sort => 'spent_hours:desc'
590 590 assert_response :success
591 591 hours = assigns(:issues).collect(&:spent_hours)
592 592 assert_equal hours.sort.reverse, hours
593 593 end
594 594
595 595 def test_index_with_columns
596 596 columns = ['tracker', 'subject', 'assigned_to']
597 597 get :index, :set_filter => 1, :c => columns
598 598 assert_response :success
599 599
600 600 # query should use specified columns
601 601 query = assigns(:query)
602 602 assert_kind_of Query, query
603 603 assert_equal columns, query.column_names.map(&:to_s)
604 604
605 605 # columns should be stored in session
606 606 assert_kind_of Hash, session[:query]
607 607 assert_kind_of Array, session[:query][:column_names]
608 608 assert_equal columns, session[:query][:column_names].map(&:to_s)
609 609
610 610 # ensure only these columns are kept in the selected columns list
611 611 assert_tag :tag => 'select', :attributes => { :id => 'selected_columns' },
612 612 :children => { :count => 3 }
613 613 assert_no_tag :tag => 'option', :attributes => { :value => 'project' },
614 614 :parent => { :tag => 'select', :attributes => { :id => "selected_columns" } }
615 615 end
616 616
617 617 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
618 618 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
619 619 get :index, :set_filter => 1
620 620
621 621 # query should use specified columns
622 622 query = assigns(:query)
623 623 assert_kind_of Query, query
624 624 assert_equal [:project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
625 625 end
626 626
627 627 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
628 628 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
629 629 columns = ['tracker', 'subject', 'assigned_to']
630 630 get :index, :set_filter => 1, :c => columns
631 631
632 632 # query should use specified columns
633 633 query = assigns(:query)
634 634 assert_kind_of Query, query
635 635 assert_equal columns.map(&:to_sym), query.columns.map(&:name)
636 636 end
637 637
638 638 def test_index_with_custom_field_column
639 639 columns = %w(tracker subject cf_2)
640 640 get :index, :set_filter => 1, :c => columns
641 641 assert_response :success
642 642
643 643 # query should use specified columns
644 644 query = assigns(:query)
645 645 assert_kind_of Query, query
646 646 assert_equal columns, query.column_names.map(&:to_s)
647 647
648 648 assert_tag :td,
649 649 :attributes => {:class => 'cf_2 string'},
650 650 :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
651 651 end
652 652
653 653 def test_index_with_multi_custom_field_column
654 654 field = CustomField.find(1)
655 655 field.update_attribute :multiple, true
656 656 issue = Issue.find(1)
657 657 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
658 658 issue.save!
659 659
660 660 get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
661 661 assert_response :success
662 662
663 663 assert_tag :td,
664 664 :attributes => {:class => /cf_1/},
665 665 :content => 'MySQL, Oracle'
666 666 end
667 667
668 668 def test_index_with_multi_user_custom_field_column
669 669 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
670 670 :tracker_ids => [1], :is_for_all => true)
671 671 issue = Issue.find(1)
672 672 issue.custom_field_values = {field.id => ['2', '3']}
673 673 issue.save!
674 674
675 675 get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
676 676 assert_response :success
677 677
678 678 assert_tag :td,
679 679 :attributes => {:class => /cf_#{field.id}/},
680 680 :child => {:tag => 'a', :content => 'John Smith'}
681 681 end
682 682
683 683 def test_index_with_date_column
684 684 Issue.find(1).update_attribute :start_date, '1987-08-24'
685 685
686 686 with_settings :date_format => '%d/%m/%Y' do
687 687 get :index, :set_filter => 1, :c => %w(start_date)
688 688 assert_tag 'td', :attributes => {:class => /start_date/}, :content => '24/08/1987'
689 689 end
690 690 end
691 691
692 692 def test_index_with_done_ratio
693 693 Issue.find(1).update_attribute :done_ratio, 40
694 694
695 695 get :index, :set_filter => 1, :c => %w(done_ratio)
696 696 assert_tag 'td', :attributes => {:class => /done_ratio/},
697 697 :child => {:tag => 'table', :attributes => {:class => 'progress'},
698 698 :descendant => {:tag => 'td', :attributes => {:class => 'closed', :style => 'width: 40%;'}}
699 699 }
700 700 end
701 701
702 702 def test_index_with_spent_hours_column
703 703 get :index, :set_filter => 1, :c => %w(subject spent_hours)
704 704
705 705 assert_tag 'tr', :attributes => {:id => 'issue-3'},
706 706 :child => {
707 707 :tag => 'td', :attributes => {:class => /spent_hours/}, :content => '1.00'
708 708 }
709 709 end
710 710
711 711 def test_index_should_not_show_spent_hours_column_without_permission
712 712 Role.anonymous.remove_permission! :view_time_entries
713 713 get :index, :set_filter => 1, :c => %w(subject spent_hours)
714 714
715 715 assert_no_tag 'td', :attributes => {:class => /spent_hours/}
716 716 end
717 717
718 718 def test_index_with_fixed_version
719 719 get :index, :set_filter => 1, :c => %w(fixed_version)
720 720 assert_tag 'td', :attributes => {:class => /fixed_version/},
721 721 :child => {:tag => 'a', :content => '1.0', :attributes => {:href => '/versions/2'}}
722 722 end
723 723
724 724 def test_index_send_html_if_query_is_invalid
725 725 get :index, :f => ['start_date'], :op => {:start_date => '='}
726 726 assert_equal 'text/html', @response.content_type
727 727 assert_template 'index'
728 728 end
729 729
730 730 def test_index_send_nothing_if_query_is_invalid
731 731 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
732 732 assert_equal 'text/csv', @response.content_type
733 733 assert @response.body.blank?
734 734 end
735 735
736 736 def test_show_by_anonymous
737 737 get :show, :id => 1
738 738 assert_response :success
739 739 assert_template 'show'
740 740 assert_not_nil assigns(:issue)
741 741 assert_equal Issue.find(1), assigns(:issue)
742 742
743 743 # anonymous role is allowed to add a note
744 744 assert_tag :tag => 'form',
745 745 :descendant => { :tag => 'fieldset',
746 746 :child => { :tag => 'legend',
747 747 :content => /Notes/ } }
748 748 assert_tag :tag => 'title',
749 749 :content => "Bug #1: Can't print recipes - eCookbook - Redmine"
750 750 end
751 751
752 752 def test_show_by_manager
753 753 @request.session[:user_id] = 2
754 754 get :show, :id => 1
755 755 assert_response :success
756 756
757 757 assert_tag :tag => 'a',
758 758 :content => /Quote/
759 759
760 760 assert_tag :tag => 'form',
761 761 :descendant => { :tag => 'fieldset',
762 762 :child => { :tag => 'legend',
763 763 :content => /Change properties/ } },
764 764 :descendant => { :tag => 'fieldset',
765 765 :child => { :tag => 'legend',
766 766 :content => /Log time/ } },
767 767 :descendant => { :tag => 'fieldset',
768 768 :child => { :tag => 'legend',
769 769 :content => /Notes/ } }
770 770 end
771 771
772 772 def test_show_should_display_update_form
773 773 @request.session[:user_id] = 2
774 774 get :show, :id => 1
775 775 assert_response :success
776 776
777 777 assert_tag 'form', :attributes => {:id => 'issue-form'}
778 778 assert_tag 'input', :attributes => {:name => 'issue[is_private]'}
779 779 assert_tag 'select', :attributes => {:name => 'issue[project_id]'}
780 780 assert_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
781 781 assert_tag 'input', :attributes => {:name => 'issue[subject]'}
782 782 assert_tag 'textarea', :attributes => {:name => 'issue[description]'}
783 783 assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
784 784 assert_tag 'select', :attributes => {:name => 'issue[priority_id]'}
785 785 assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
786 786 assert_tag 'select', :attributes => {:name => 'issue[category_id]'}
787 787 assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
788 788 assert_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
789 789 assert_tag 'input', :attributes => {:name => 'issue[start_date]'}
790 790 assert_tag 'input', :attributes => {:name => 'issue[due_date]'}
791 791 assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
792 792 assert_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]' }
793 793 assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
794 794 assert_tag 'textarea', :attributes => {:name => 'notes'}
795 795 end
796 796
797 797 def test_show_should_display_update_form_with_minimal_permissions
798 798 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
799 799 Workflow.delete_all :role_id => 1
800 800
801 801 @request.session[:user_id] = 2
802 802 get :show, :id => 1
803 803 assert_response :success
804 804
805 805 assert_tag 'form', :attributes => {:id => 'issue-form'}
806 806 assert_no_tag 'input', :attributes => {:name => 'issue[is_private]'}
807 807 assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
808 808 assert_no_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
809 809 assert_no_tag 'input', :attributes => {:name => 'issue[subject]'}
810 810 assert_no_tag 'textarea', :attributes => {:name => 'issue[description]'}
811 811 assert_no_tag 'select', :attributes => {:name => 'issue[status_id]'}
812 812 assert_no_tag 'select', :attributes => {:name => 'issue[priority_id]'}
813 813 assert_no_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
814 814 assert_no_tag 'select', :attributes => {:name => 'issue[category_id]'}
815 815 assert_no_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
816 816 assert_no_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
817 817 assert_no_tag 'input', :attributes => {:name => 'issue[start_date]'}
818 818 assert_no_tag 'input', :attributes => {:name => 'issue[due_date]'}
819 819 assert_no_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
820 820 assert_no_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]' }
821 821 assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
822 822 assert_tag 'textarea', :attributes => {:name => 'notes'}
823 823 end
824 824
825 825 def test_show_should_display_update_form_with_workflow_permissions
826 826 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
827 827
828 828 @request.session[:user_id] = 2
829 829 get :show, :id => 1
830 830 assert_response :success
831 831
832 832 assert_tag 'form', :attributes => {:id => 'issue-form'}
833 833 assert_no_tag 'input', :attributes => {:name => 'issue[is_private]'}
834 834 assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
835 835 assert_no_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
836 836 assert_no_tag 'input', :attributes => {:name => 'issue[subject]'}
837 837 assert_no_tag 'textarea', :attributes => {:name => 'issue[description]'}
838 838 assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
839 839 assert_no_tag 'select', :attributes => {:name => 'issue[priority_id]'}
840 840 assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
841 841 assert_no_tag 'select', :attributes => {:name => 'issue[category_id]'}
842 842 assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
843 843 assert_no_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
844 844 assert_no_tag 'input', :attributes => {:name => 'issue[start_date]'}
845 845 assert_no_tag 'input', :attributes => {:name => 'issue[due_date]'}
846 846 assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
847 847 assert_no_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]' }
848 848 assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
849 849 assert_tag 'textarea', :attributes => {:name => 'notes'}
850 850 end
851 851
852 852 def test_show_should_not_display_update_form_without_permissions
853 853 Role.find(1).update_attribute :permissions, [:view_issues]
854 854
855 855 @request.session[:user_id] = 2
856 856 get :show, :id => 1
857 857 assert_response :success
858 858
859 859 assert_no_tag 'form', :attributes => {:id => 'issue-form'}
860 860 end
861 861
862 862 def test_update_form_should_not_display_inactive_enumerations
863 863 @request.session[:user_id] = 2
864 864 get :show, :id => 1
865 865 assert_response :success
866 866
867 867 assert ! IssuePriority.find(15).active?
868 868 assert_no_tag :option, :attributes => {:value => '15'},
869 869 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
870 870 end
871 871
872 872 def test_update_form_should_allow_attachment_upload
873 873 @request.session[:user_id] = 2
874 874 get :show, :id => 1
875 875
876 876 assert_tag :tag => 'form',
877 877 :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
878 878 :descendant => {
879 879 :tag => 'input',
880 880 :attributes => {:type => 'file', :name => 'attachments[1][file]'}
881 881 }
882 882 end
883 883
884 884 def test_show_should_deny_anonymous_access_without_permission
885 885 Role.anonymous.remove_permission!(:view_issues)
886 886 get :show, :id => 1
887 887 assert_response :redirect
888 888 end
889 889
890 890 def test_show_should_deny_anonymous_access_to_private_issue
891 891 Issue.update_all(["is_private = ?", true], "id = 1")
892 892 get :show, :id => 1
893 893 assert_response :redirect
894 894 end
895 895
896 896 def test_show_should_deny_non_member_access_without_permission
897 897 Role.non_member.remove_permission!(:view_issues)
898 898 @request.session[:user_id] = 9
899 899 get :show, :id => 1
900 900 assert_response 403
901 901 end
902 902
903 903 def test_show_should_deny_non_member_access_to_private_issue
904 904 Issue.update_all(["is_private = ?", true], "id = 1")
905 905 @request.session[:user_id] = 9
906 906 get :show, :id => 1
907 907 assert_response 403
908 908 end
909 909
910 910 def test_show_should_deny_member_access_without_permission
911 911 Role.find(1).remove_permission!(:view_issues)
912 912 @request.session[:user_id] = 2
913 913 get :show, :id => 1
914 914 assert_response 403
915 915 end
916 916
917 917 def test_show_should_deny_member_access_to_private_issue_without_permission
918 918 Issue.update_all(["is_private = ?", true], "id = 1")
919 919 @request.session[:user_id] = 3
920 920 get :show, :id => 1
921 921 assert_response 403
922 922 end
923 923
924 924 def test_show_should_allow_author_access_to_private_issue
925 925 Issue.update_all(["is_private = ?, author_id = 3", true], "id = 1")
926 926 @request.session[:user_id] = 3
927 927 get :show, :id => 1
928 928 assert_response :success
929 929 end
930 930
931 931 def test_show_should_allow_assignee_access_to_private_issue
932 932 Issue.update_all(["is_private = ?, assigned_to_id = 3", true], "id = 1")
933 933 @request.session[:user_id] = 3
934 934 get :show, :id => 1
935 935 assert_response :success
936 936 end
937 937
938 938 def test_show_should_allow_member_access_to_private_issue_with_permission
939 939 Issue.update_all(["is_private = ?", true], "id = 1")
940 940 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
941 941 @request.session[:user_id] = 3
942 942 get :show, :id => 1
943 943 assert_response :success
944 944 end
945 945
946 946 def test_show_should_not_disclose_relations_to_invisible_issues
947 947 Setting.cross_project_issue_relations = '1'
948 948 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
949 949 # Relation to a private project issue
950 950 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
951 951
952 952 get :show, :id => 1
953 953 assert_response :success
954 954
955 955 assert_tag :div, :attributes => { :id => 'relations' },
956 956 :descendant => { :tag => 'a', :content => /#2$/ }
957 957 assert_no_tag :div, :attributes => { :id => 'relations' },
958 958 :descendant => { :tag => 'a', :content => /#4$/ }
959 959 end
960 960
961 961 def test_show_should_list_subtasks
962 962 Issue.generate!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
963 963
964 964 get :show, :id => 1
965 965 assert_response :success
966 966 assert_tag 'div', :attributes => {:id => 'issue_tree'},
967 967 :descendant => {:tag => 'td', :content => /Child Issue/, :attributes => {:class => /subject/}}
968 968 end
969 969
970 970 def test_show_should_list_parents
971 971 issue = Issue.generate!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
972 972
973 973 get :show, :id => issue.id
974 974 assert_response :success
975 975 assert_tag 'div', :attributes => {:class => 'subject'},
976 976 :descendant => {:tag => 'h3', :content => 'Child Issue'}
977 977 assert_tag 'div', :attributes => {:class => 'subject'},
978 978 :descendant => {:tag => 'a', :attributes => {:href => '/issues/1'}}
979 979 end
980 980
981 981 def test_show_should_not_display_prev_next_links_without_query_in_session
982 982 get :show, :id => 1
983 983 assert_response :success
984 984 assert_nil assigns(:prev_issue_id)
985 985 assert_nil assigns(:next_issue_id)
986 986
987 987 assert_no_tag 'div', :attributes => {:class => /next-prev-links/}
988 988 end
989 989
990 990 def test_show_should_display_prev_next_links_with_query_in_session
991 991 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
992 992 @request.session['issues_index_sort'] = 'id'
993 993
994 994 with_settings :display_subprojects_issues => '0' do
995 995 get :show, :id => 3
996 996 end
997 997
998 998 assert_response :success
999 999 # Previous and next issues for all projects
1000 1000 assert_equal 2, assigns(:prev_issue_id)
1001 1001 assert_equal 5, assigns(:next_issue_id)
1002 1002
1003 1003 assert_tag 'div', :attributes => {:class => /next-prev-links/}
1004 1004 assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Previous/
1005 1005 assert_tag 'a', :attributes => {:href => '/issues/5'}, :content => /Next/
1006 1006
1007 1007 count = Issue.open.visible.count
1008 1008 assert_tag 'span', :attributes => {:class => 'position'}, :content => "3 of #{count}"
1009 1009 end
1010 1010
1011 1011 def test_show_should_display_prev_next_links_with_saved_query_in_session
1012 1012 query = Query.create!(:name => 'test', :is_public => true, :user_id => 1,
1013 1013 :filters => {'status_id' => {:values => ['5'], :operator => '='}},
1014 1014 :sort_criteria => [['id', 'asc']])
1015 1015 @request.session[:query] = {:id => query.id, :project_id => nil}
1016 1016
1017 1017 get :show, :id => 11
1018 1018
1019 1019 assert_response :success
1020 1020 assert_equal query, assigns(:query)
1021 1021 # Previous and next issues for all projects
1022 1022 assert_equal 8, assigns(:prev_issue_id)
1023 1023 assert_equal 12, assigns(:next_issue_id)
1024 1024
1025 1025 assert_tag 'a', :attributes => {:href => '/issues/8'}, :content => /Previous/
1026 1026 assert_tag 'a', :attributes => {:href => '/issues/12'}, :content => /Next/
1027 1027 end
1028 1028
1029 1029 def test_show_should_display_prev_next_links_with_query_and_sort_on_association
1030 1030 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1031 1031
1032 1032 %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
1033 1033 @request.session['issues_index_sort'] = assoc_sort
1034 1034
1035 1035 get :show, :id => 3
1036 1036 assert_response :success, "Wrong response status for #{assoc_sort} sort"
1037 1037
1038 1038 assert_tag 'a', :content => /Previous/
1039 1039 assert_tag 'a', :content => /Next/
1040 1040 end
1041 1041 end
1042 1042
1043 1043 def test_show_should_display_prev_next_links_with_project_query_in_session
1044 1044 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1045 1045 @request.session['issues_index_sort'] = 'id'
1046 1046
1047 1047 with_settings :display_subprojects_issues => '0' do
1048 1048 get :show, :id => 3
1049 1049 end
1050 1050
1051 1051 assert_response :success
1052 1052 # Previous and next issues inside project
1053 1053 assert_equal 2, assigns(:prev_issue_id)
1054 1054 assert_equal 7, assigns(:next_issue_id)
1055 1055
1056 1056 assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Previous/
1057 1057 assert_tag 'a', :attributes => {:href => '/issues/7'}, :content => /Next/
1058 1058 end
1059 1059
1060 1060 def test_show_should_not_display_prev_link_for_first_issue
1061 1061 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1062 1062 @request.session['issues_index_sort'] = 'id'
1063 1063
1064 1064 with_settings :display_subprojects_issues => '0' do
1065 1065 get :show, :id => 1
1066 1066 end
1067 1067
1068 1068 assert_response :success
1069 1069 assert_nil assigns(:prev_issue_id)
1070 1070 assert_equal 2, assigns(:next_issue_id)
1071 1071
1072 1072 assert_no_tag 'a', :content => /Previous/
1073 1073 assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Next/
1074 1074 end
1075 1075
1076 1076 def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
1077 1077 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
1078 1078 @request.session['issues_index_sort'] = 'id'
1079 1079
1080 1080 get :show, :id => 1
1081 1081
1082 1082 assert_response :success
1083 1083 assert_nil assigns(:prev_issue_id)
1084 1084 assert_nil assigns(:next_issue_id)
1085 1085
1086 1086 assert_no_tag 'a', :content => /Previous/
1087 1087 assert_no_tag 'a', :content => /Next/
1088 1088 end
1089 1089
1090 1090 def test_show_should_display_visible_changesets_from_other_projects
1091 1091 project = Project.find(2)
1092 1092 issue = project.issues.first
1093 1093 issue.changeset_ids = [102]
1094 1094 issue.save!
1095 1095 project.disable_module! :repository
1096 1096
1097 1097 @request.session[:user_id] = 2
1098 1098 get :show, :id => issue.id
1099 1099 assert_tag 'a', :attributes => {:href => "/projects/ecookbook/repository/revisions/3"}
1100 1100 end
1101 1101
1102 1102 def test_show_with_multi_custom_field
1103 1103 field = CustomField.find(1)
1104 1104 field.update_attribute :multiple, true
1105 1105 issue = Issue.find(1)
1106 1106 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
1107 1107 issue.save!
1108 1108
1109 1109 get :show, :id => 1
1110 1110 assert_response :success
1111 1111
1112 1112 assert_tag :td, :content => 'MySQL, Oracle'
1113 1113 end
1114 1114
1115 1115 def test_show_with_multi_user_custom_field
1116 1116 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1117 1117 :tracker_ids => [1], :is_for_all => true)
1118 1118 issue = Issue.find(1)
1119 1119 issue.custom_field_values = {field.id => ['2', '3']}
1120 1120 issue.save!
1121 1121
1122 1122 get :show, :id => 1
1123 1123 assert_response :success
1124 1124
1125 1125 # TODO: should display links
1126 1126 assert_tag :td, :content => 'Dave Lopper, John Smith'
1127 1127 end
1128 1128
1129 1129 def test_show_atom
1130 1130 get :show, :id => 2, :format => 'atom'
1131 1131 assert_response :success
1132 1132 assert_template 'journals/index'
1133 1133 # Inline image
1134 1134 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1135 1135 end
1136 1136
1137 1137 def test_show_export_to_pdf
1138 1138 get :show, :id => 3, :format => 'pdf'
1139 1139 assert_response :success
1140 1140 assert_equal 'application/pdf', @response.content_type
1141 1141 assert @response.body.starts_with?('%PDF')
1142 1142 assert_not_nil assigns(:issue)
1143 1143 end
1144 1144
1145 1145 def test_get_new
1146 1146 @request.session[:user_id] = 2
1147 1147 get :new, :project_id => 1, :tracker_id => 1
1148 1148 assert_response :success
1149 1149 assert_template 'new'
1150 1150
1151 1151 assert_tag 'input', :attributes => {:name => 'issue[is_private]'}
1152 1152 assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
1153 1153 assert_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
1154 1154 assert_tag 'input', :attributes => {:name => 'issue[subject]'}
1155 1155 assert_tag 'textarea', :attributes => {:name => 'issue[description]'}
1156 1156 assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
1157 1157 assert_tag 'select', :attributes => {:name => 'issue[priority_id]'}
1158 1158 assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
1159 1159 assert_tag 'select', :attributes => {:name => 'issue[category_id]'}
1160 1160 assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
1161 1161 assert_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
1162 1162 assert_tag 'input', :attributes => {:name => 'issue[start_date]'}
1163 1163 assert_tag 'input', :attributes => {:name => 'issue[due_date]'}
1164 1164 assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
1165 1165 assert_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]', :value => 'Default string' }
1166 1166 assert_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
1167 1167
1168 1168 # Be sure we don't display inactive IssuePriorities
1169 1169 assert ! IssuePriority.find(15).active?
1170 1170 assert_no_tag :option, :attributes => {:value => '15'},
1171 1171 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
1172 1172 end
1173 1173
1174 1174 def test_get_new_with_minimal_permissions
1175 1175 Role.find(1).update_attribute :permissions, [:add_issues]
1176 1176 Workflow.delete_all :role_id => 1
1177 1177
1178 1178 @request.session[:user_id] = 2
1179 1179 get :new, :project_id => 1, :tracker_id => 1
1180 1180 assert_response :success
1181 1181 assert_template 'new'
1182 1182
1183 1183 assert_no_tag 'input', :attributes => {:name => 'issue[is_private]'}
1184 1184 assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
1185 1185 assert_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
1186 1186 assert_tag 'input', :attributes => {:name => 'issue[subject]'}
1187 1187 assert_tag 'textarea', :attributes => {:name => 'issue[description]'}
1188 1188 assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
1189 1189 assert_tag 'select', :attributes => {:name => 'issue[priority_id]'}
1190 1190 assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
1191 1191 assert_tag 'select', :attributes => {:name => 'issue[category_id]'}
1192 1192 assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
1193 1193 assert_no_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
1194 1194 assert_tag 'input', :attributes => {:name => 'issue[start_date]'}
1195 1195 assert_tag 'input', :attributes => {:name => 'issue[due_date]'}
1196 1196 assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
1197 1197 assert_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]', :value => 'Default string' }
1198 1198 assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
1199 1199 end
1200 1200
1201 1201 def test_get_new_with_multi_custom_field
1202 1202 field = IssueCustomField.find(1)
1203 1203 field.update_attribute :multiple, true
1204 1204
1205 1205 @request.session[:user_id] = 2
1206 1206 get :new, :project_id => 1, :tracker_id => 1
1207 1207 assert_response :success
1208 1208 assert_template 'new'
1209 1209
1210 1210 assert_tag 'select',
1211 1211 :attributes => {:name => 'issue[custom_field_values][1][]', :multiple => 'multiple'},
1212 1212 :children => {:count => 3},
1213 1213 :child => {:tag => 'option', :attributes => {:value => 'MySQL'}, :content => 'MySQL'}
1214 1214 assert_tag 'input',
1215 1215 :attributes => {:name => 'issue[custom_field_values][1][]', :value => ''}
1216 1216 end
1217 1217
1218 1218 def test_get_new_with_multi_user_custom_field
1219 1219 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1220 1220 :tracker_ids => [1], :is_for_all => true)
1221 1221
1222 1222 @request.session[:user_id] = 2
1223 1223 get :new, :project_id => 1, :tracker_id => 1
1224 1224 assert_response :success
1225 1225 assert_template 'new'
1226 1226
1227 1227 assert_tag 'select',
1228 1228 :attributes => {:name => "issue[custom_field_values][#{field.id}][]", :multiple => 'multiple'},
1229 1229 :children => {:count => Project.find(1).users.count},
1230 1230 :child => {:tag => 'option', :attributes => {:value => '2'}, :content => 'John Smith'}
1231 1231 assert_tag 'input',
1232 1232 :attributes => {:name => "issue[custom_field_values][#{field.id}][]", :value => ''}
1233 1233 end
1234 1234
1235 1235 def test_get_new_without_default_start_date_is_creation_date
1236 1236 Setting.default_issue_start_date_to_creation_date = 0
1237 1237
1238 1238 @request.session[:user_id] = 2
1239 1239 get :new, :project_id => 1, :tracker_id => 1
1240 1240 assert_response :success
1241 1241 assert_template 'new'
1242 1242
1243 1243 assert_tag :tag => 'input', :attributes => { :name => 'issue[start_date]',
1244 1244 :value => nil }
1245 1245 end
1246 1246
1247 1247 def test_get_new_with_default_start_date_is_creation_date
1248 1248 Setting.default_issue_start_date_to_creation_date = 1
1249 1249
1250 1250 @request.session[:user_id] = 2
1251 1251 get :new, :project_id => 1, :tracker_id => 1
1252 1252 assert_response :success
1253 1253 assert_template 'new'
1254 1254
1255 1255 assert_tag :tag => 'input', :attributes => { :name => 'issue[start_date]',
1256 1256 :value => Date.today.to_s }
1257 1257 end
1258 1258
1259 1259 def test_get_new_form_should_allow_attachment_upload
1260 1260 @request.session[:user_id] = 2
1261 1261 get :new, :project_id => 1, :tracker_id => 1
1262 1262
1263 1263 assert_tag :tag => 'form',
1264 1264 :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
1265 1265 :descendant => {
1266 1266 :tag => 'input',
1267 1267 :attributes => {:type => 'file', :name => 'attachments[1][file]'}
1268 1268 }
1269 1269 end
1270 1270
1271 1271 def test_get_new_without_tracker_id
1272 1272 @request.session[:user_id] = 2
1273 1273 get :new, :project_id => 1
1274 1274 assert_response :success
1275 1275 assert_template 'new'
1276 1276
1277 1277 issue = assigns(:issue)
1278 1278 assert_not_nil issue
1279 1279 assert_equal Project.find(1).trackers.first, issue.tracker
1280 1280 end
1281 1281
1282 1282 def test_get_new_with_no_default_status_should_display_an_error
1283 1283 @request.session[:user_id] = 2
1284 1284 IssueStatus.delete_all
1285 1285
1286 1286 get :new, :project_id => 1
1287 1287 assert_response 500
1288 1288 assert_error_tag :content => /No default issue/
1289 1289 end
1290 1290
1291 1291 def test_get_new_with_no_tracker_should_display_an_error
1292 1292 @request.session[:user_id] = 2
1293 1293 Tracker.delete_all
1294 1294
1295 1295 get :new, :project_id => 1
1296 1296 assert_response 500
1297 1297 assert_error_tag :content => /No tracker/
1298 1298 end
1299 1299
1300 1300 def test_update_new_form
1301 1301 @request.session[:user_id] = 2
1302 1302 xhr :post, :new, :project_id => 1,
1303 1303 :issue => {:tracker_id => 2,
1304 1304 :subject => 'This is the test_new issue',
1305 1305 :description => 'This is the description',
1306 1306 :priority_id => 5}
1307 1307 assert_response :success
1308 1308 assert_template 'attributes'
1309 1309
1310 1310 issue = assigns(:issue)
1311 1311 assert_kind_of Issue, issue
1312 1312 assert_equal 1, issue.project_id
1313 1313 assert_equal 2, issue.tracker_id
1314 1314 assert_equal 'This is the test_new issue', issue.subject
1315 1315 end
1316 1316
1317 1317 def test_post_create
1318 1318 @request.session[:user_id] = 2
1319 1319 assert_difference 'Issue.count' do
1320 1320 post :create, :project_id => 1,
1321 1321 :issue => {:tracker_id => 3,
1322 1322 :status_id => 2,
1323 1323 :subject => 'This is the test_new issue',
1324 1324 :description => 'This is the description',
1325 1325 :priority_id => 5,
1326 1326 :start_date => '2010-11-07',
1327 1327 :estimated_hours => '',
1328 1328 :custom_field_values => {'2' => 'Value for field 2'}}
1329 1329 end
1330 1330 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1331 1331
1332 1332 issue = Issue.find_by_subject('This is the test_new issue')
1333 1333 assert_not_nil issue
1334 1334 assert_equal 2, issue.author_id
1335 1335 assert_equal 3, issue.tracker_id
1336 1336 assert_equal 2, issue.status_id
1337 1337 assert_equal Date.parse('2010-11-07'), issue.start_date
1338 1338 assert_nil issue.estimated_hours
1339 1339 v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
1340 1340 assert_not_nil v
1341 1341 assert_equal 'Value for field 2', v.value
1342 1342 end
1343 1343
1344 1344 def test_post_new_with_group_assignment
1345 1345 group = Group.find(11)
1346 1346 project = Project.find(1)
1347 1347 project.members << Member.new(:principal => group, :roles => [Role.first])
1348 1348
1349 1349 with_settings :issue_group_assignment => '1' do
1350 1350 @request.session[:user_id] = 2
1351 1351 assert_difference 'Issue.count' do
1352 1352 post :create, :project_id => project.id,
1353 1353 :issue => {:tracker_id => 3,
1354 1354 :status_id => 1,
1355 1355 :subject => 'This is the test_new_with_group_assignment issue',
1356 1356 :assigned_to_id => group.id}
1357 1357 end
1358 1358 end
1359 1359 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1360 1360
1361 1361 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
1362 1362 assert_not_nil issue
1363 1363 assert_equal group, issue.assigned_to
1364 1364 end
1365 1365
1366 1366 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
1367 1367 Setting.default_issue_start_date_to_creation_date = 0
1368 1368
1369 1369 @request.session[:user_id] = 2
1370 1370 assert_difference 'Issue.count' do
1371 1371 post :create, :project_id => 1,
1372 1372 :issue => {:tracker_id => 3,
1373 1373 :status_id => 2,
1374 1374 :subject => 'This is the test_new issue',
1375 1375 :description => 'This is the description',
1376 1376 :priority_id => 5,
1377 1377 :estimated_hours => '',
1378 1378 :custom_field_values => {'2' => 'Value for field 2'}}
1379 1379 end
1380 1380 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1381 1381
1382 1382 issue = Issue.find_by_subject('This is the test_new issue')
1383 1383 assert_not_nil issue
1384 1384 assert_nil issue.start_date
1385 1385 end
1386 1386
1387 1387 def test_post_create_without_start_date_and_default_start_date_is_creation_date
1388 1388 Setting.default_issue_start_date_to_creation_date = 1
1389 1389
1390 1390 @request.session[:user_id] = 2
1391 1391 assert_difference 'Issue.count' do
1392 1392 post :create, :project_id => 1,
1393 1393 :issue => {:tracker_id => 3,
1394 1394 :status_id => 2,
1395 1395 :subject => 'This is the test_new issue',
1396 1396 :description => 'This is the description',
1397 1397 :priority_id => 5,
1398 1398 :estimated_hours => '',
1399 1399 :custom_field_values => {'2' => 'Value for field 2'}}
1400 1400 end
1401 1401 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1402 1402
1403 1403 issue = Issue.find_by_subject('This is the test_new issue')
1404 1404 assert_not_nil issue
1405 1405 assert_equal Date.today, issue.start_date
1406 1406 end
1407 1407
1408 1408 def test_post_create_and_continue
1409 1409 @request.session[:user_id] = 2
1410 1410 assert_difference 'Issue.count' do
1411 1411 post :create, :project_id => 1,
1412 1412 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
1413 1413 :continue => ''
1414 1414 end
1415 1415
1416 1416 issue = Issue.first(:order => 'id DESC')
1417 1417 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
1418 1418 assert_not_nil flash[:notice], "flash was not set"
1419 1419 assert flash[:notice].include?("<a href='/issues/#{issue.id}'>##{issue.id}</a>"), "issue link not found in flash: #{flash[:notice]}"
1420 1420 end
1421 1421
1422 1422 def test_post_create_without_custom_fields_param
1423 1423 @request.session[:user_id] = 2
1424 1424 assert_difference 'Issue.count' do
1425 1425 post :create, :project_id => 1,
1426 1426 :issue => {:tracker_id => 1,
1427 1427 :subject => 'This is the test_new issue',
1428 1428 :description => 'This is the description',
1429 1429 :priority_id => 5}
1430 1430 end
1431 1431 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1432 1432 end
1433 1433
1434 1434 def test_post_create_with_multi_custom_field
1435 1435 field = IssueCustomField.find_by_name('Database')
1436 1436 field.update_attribute(:multiple, true)
1437 1437
1438 1438 @request.session[:user_id] = 2
1439 1439 assert_difference 'Issue.count' do
1440 1440 post :create, :project_id => 1,
1441 1441 :issue => {:tracker_id => 1,
1442 1442 :subject => 'This is the test_new issue',
1443 1443 :description => 'This is the description',
1444 1444 :priority_id => 5,
1445 1445 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
1446 1446 end
1447 1447 assert_response 302
1448 1448 issue = Issue.first(:order => 'id DESC')
1449 1449 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
1450 1450 end
1451 1451
1452 1452 def test_post_create_with_empty_multi_custom_field
1453 1453 field = IssueCustomField.find_by_name('Database')
1454 1454 field.update_attribute(:multiple, true)
1455 1455
1456 1456 @request.session[:user_id] = 2
1457 1457 assert_difference 'Issue.count' do
1458 1458 post :create, :project_id => 1,
1459 1459 :issue => {:tracker_id => 1,
1460 1460 :subject => 'This is the test_new issue',
1461 1461 :description => 'This is the description',
1462 1462 :priority_id => 5,
1463 1463 :custom_field_values => {'1' => ['']}}
1464 1464 end
1465 1465 assert_response 302
1466 1466 issue = Issue.first(:order => 'id DESC')
1467 1467 assert_equal [''], issue.custom_field_value(1).sort
1468 1468 end
1469 1469
1470 1470 def test_post_create_with_multi_user_custom_field
1471 1471 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1472 1472 :tracker_ids => [1], :is_for_all => true)
1473 1473
1474 1474 @request.session[:user_id] = 2
1475 1475 assert_difference 'Issue.count' do
1476 1476 post :create, :project_id => 1,
1477 1477 :issue => {:tracker_id => 1,
1478 1478 :subject => 'This is the test_new issue',
1479 1479 :description => 'This is the description',
1480 1480 :priority_id => 5,
1481 1481 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
1482 1482 end
1483 1483 assert_response 302
1484 1484 issue = Issue.first(:order => 'id DESC')
1485 1485 assert_equal ['2', '3'], issue.custom_field_value(field).sort
1486 1486 end
1487 1487
1488 1488 def test_post_create_with_required_custom_field_and_without_custom_fields_param
1489 1489 field = IssueCustomField.find_by_name('Database')
1490 1490 field.update_attribute(:is_required, true)
1491 1491
1492 1492 @request.session[:user_id] = 2
1493 1493 assert_no_difference 'Issue.count' do
1494 1494 post :create, :project_id => 1,
1495 1495 :issue => {:tracker_id => 1,
1496 1496 :subject => 'This is the test_new issue',
1497 1497 :description => 'This is the description',
1498 1498 :priority_id => 5}
1499 1499 end
1500 1500 assert_response :success
1501 1501 assert_template 'new'
1502 1502 issue = assigns(:issue)
1503 1503 assert_not_nil issue
1504 1504 assert_error_tag :content => /Database can't be blank/
1505 1505 end
1506 1506
1507 1507 def test_post_create_with_watchers
1508 1508 @request.session[:user_id] = 2
1509 1509 ActionMailer::Base.deliveries.clear
1510 1510
1511 1511 assert_difference 'Watcher.count', 2 do
1512 1512 post :create, :project_id => 1,
1513 1513 :issue => {:tracker_id => 1,
1514 1514 :subject => 'This is a new issue with watchers',
1515 1515 :description => 'This is the description',
1516 1516 :priority_id => 5,
1517 1517 :watcher_user_ids => ['2', '3']}
1518 1518 end
1519 1519 issue = Issue.find_by_subject('This is a new issue with watchers')
1520 1520 assert_not_nil issue
1521 1521 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1522 1522
1523 1523 # Watchers added
1524 1524 assert_equal [2, 3], issue.watcher_user_ids.sort
1525 1525 assert issue.watched_by?(User.find(3))
1526 1526 # Watchers notified
1527 1527 mail = ActionMailer::Base.deliveries.last
1528 1528 assert_kind_of TMail::Mail, mail
1529 1529 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
1530 1530 end
1531 1531
1532 1532 def test_post_create_subissue
1533 1533 @request.session[:user_id] = 2
1534 1534
1535 1535 assert_difference 'Issue.count' do
1536 1536 post :create, :project_id => 1,
1537 1537 :issue => {:tracker_id => 1,
1538 1538 :subject => 'This is a child issue',
1539 1539 :parent_issue_id => 2}
1540 1540 end
1541 1541 issue = Issue.find_by_subject('This is a child issue')
1542 1542 assert_not_nil issue
1543 1543 assert_equal Issue.find(2), issue.parent
1544 1544 end
1545 1545
1546 1546 def test_post_create_subissue_with_non_numeric_parent_id
1547 1547 @request.session[:user_id] = 2
1548 1548
1549 1549 assert_difference 'Issue.count' do
1550 1550 post :create, :project_id => 1,
1551 1551 :issue => {:tracker_id => 1,
1552 1552 :subject => 'This is a child issue',
1553 1553 :parent_issue_id => 'ABC'}
1554 1554 end
1555 1555 issue = Issue.find_by_subject('This is a child issue')
1556 1556 assert_not_nil issue
1557 1557 assert_nil issue.parent
1558 1558 end
1559 1559
1560 1560 def test_post_create_private
1561 1561 @request.session[:user_id] = 2
1562 1562
1563 1563 assert_difference 'Issue.count' do
1564 1564 post :create, :project_id => 1,
1565 1565 :issue => {:tracker_id => 1,
1566 1566 :subject => 'This is a private issue',
1567 1567 :is_private => '1'}
1568 1568 end
1569 1569 issue = Issue.first(:order => 'id DESC')
1570 1570 assert issue.is_private?
1571 1571 end
1572 1572
1573 1573 def test_post_create_private_with_set_own_issues_private_permission
1574 1574 role = Role.find(1)
1575 1575 role.remove_permission! :set_issues_private
1576 1576 role.add_permission! :set_own_issues_private
1577 1577
1578 1578 @request.session[:user_id] = 2
1579 1579
1580 1580 assert_difference 'Issue.count' do
1581 1581 post :create, :project_id => 1,
1582 1582 :issue => {:tracker_id => 1,
1583 1583 :subject => 'This is a private issue',
1584 1584 :is_private => '1'}
1585 1585 end
1586 1586 issue = Issue.first(:order => 'id DESC')
1587 1587 assert issue.is_private?
1588 1588 end
1589 1589
1590 1590 def test_post_create_should_send_a_notification
1591 1591 ActionMailer::Base.deliveries.clear
1592 1592 @request.session[:user_id] = 2
1593 1593 assert_difference 'Issue.count' do
1594 1594 post :create, :project_id => 1,
1595 1595 :issue => {:tracker_id => 3,
1596 1596 :subject => 'This is the test_new issue',
1597 1597 :description => 'This is the description',
1598 1598 :priority_id => 5,
1599 1599 :estimated_hours => '',
1600 1600 :custom_field_values => {'2' => 'Value for field 2'}}
1601 1601 end
1602 1602 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1603 1603
1604 1604 assert_equal 1, ActionMailer::Base.deliveries.size
1605 1605 end
1606 1606
1607 1607 def test_post_create_should_preserve_fields_values_on_validation_failure
1608 1608 @request.session[:user_id] = 2
1609 1609 post :create, :project_id => 1,
1610 1610 :issue => {:tracker_id => 1,
1611 1611 # empty subject
1612 1612 :subject => '',
1613 1613 :description => 'This is a description',
1614 1614 :priority_id => 6,
1615 1615 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
1616 1616 assert_response :success
1617 1617 assert_template 'new'
1618 1618
1619 1619 assert_tag :textarea, :attributes => { :name => 'issue[description]' },
1620 1620 :content => 'This is a description'
1621 1621 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
1622 1622 :child => { :tag => 'option', :attributes => { :selected => 'selected',
1623 1623 :value => '6' },
1624 1624 :content => 'High' }
1625 1625 # Custom fields
1626 1626 assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
1627 1627 :child => { :tag => 'option', :attributes => { :selected => 'selected',
1628 1628 :value => 'Oracle' },
1629 1629 :content => 'Oracle' }
1630 1630 assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
1631 1631 :value => 'Value for field 2'}
1632 1632 end
1633 1633
1634 1634 def test_post_create_should_ignore_non_safe_attributes
1635 1635 @request.session[:user_id] = 2
1636 1636 assert_nothing_raised do
1637 1637 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
1638 1638 end
1639 1639 end
1640 1640
1641 1641 def test_post_create_with_attachment
1642 1642 set_tmp_attachments_directory
1643 1643 @request.session[:user_id] = 2
1644 1644
1645 1645 assert_difference 'Issue.count' do
1646 1646 assert_difference 'Attachment.count' do
1647 1647 post :create, :project_id => 1,
1648 1648 :issue => { :tracker_id => '1', :subject => 'With attachment' },
1649 1649 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
1650 1650 end
1651 1651 end
1652 1652
1653 1653 issue = Issue.first(:order => 'id DESC')
1654 1654 attachment = Attachment.first(:order => 'id DESC')
1655 1655
1656 1656 assert_equal issue, attachment.container
1657 1657 assert_equal 2, attachment.author_id
1658 1658 assert_equal 'testfile.txt', attachment.filename
1659 1659 assert_equal 'text/plain', attachment.content_type
1660 1660 assert_equal 'test file', attachment.description
1661 1661 assert_equal 59, attachment.filesize
1662 1662 assert File.exists?(attachment.diskfile)
1663 1663 assert_equal 59, File.size(attachment.diskfile)
1664 1664 end
1665 1665
1666 1666 context "without workflow privilege" do
1667 1667 setup do
1668 1668 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
1669 1669 Role.anonymous.add_permission! :add_issues, :add_issue_notes
1670 1670 end
1671 1671
1672 1672 context "#new" do
1673 1673 should "propose default status only" do
1674 1674 get :new, :project_id => 1
1675 1675 assert_response :success
1676 1676 assert_template 'new'
1677 1677 assert_tag :tag => 'select',
1678 1678 :attributes => {:name => 'issue[status_id]'},
1679 1679 :children => {:count => 1},
1680 1680 :child => {:tag => 'option', :attributes => {:value => IssueStatus.default.id.to_s}}
1681 1681 end
1682 1682
1683 1683 should "accept default status" do
1684 1684 assert_difference 'Issue.count' do
1685 1685 post :create, :project_id => 1,
1686 1686 :issue => {:tracker_id => 1,
1687 1687 :subject => 'This is an issue',
1688 1688 :status_id => 1}
1689 1689 end
1690 1690 issue = Issue.last(:order => 'id')
1691 1691 assert_equal IssueStatus.default, issue.status
1692 1692 end
1693 1693
1694 1694 should "ignore unauthorized status" do
1695 1695 assert_difference 'Issue.count' do
1696 1696 post :create, :project_id => 1,
1697 1697 :issue => {:tracker_id => 1,
1698 1698 :subject => 'This is an issue',
1699 1699 :status_id => 3}
1700 1700 end
1701 1701 issue = Issue.last(:order => 'id')
1702 1702 assert_equal IssueStatus.default, issue.status
1703 1703 end
1704 1704 end
1705 1705
1706 1706 context "#update" do
1707 1707 should "ignore status change" do
1708 1708 assert_difference 'Journal.count' do
1709 1709 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
1710 1710 end
1711 1711 assert_equal 1, Issue.find(1).status_id
1712 1712 end
1713 1713
1714 1714 should "ignore attributes changes" do
1715 1715 assert_difference 'Journal.count' do
1716 1716 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
1717 1717 end
1718 1718 issue = Issue.find(1)
1719 1719 assert_equal "Can't print recipes", issue.subject
1720 1720 assert_nil issue.assigned_to
1721 1721 end
1722 1722 end
1723 1723 end
1724 1724
1725 1725 context "with workflow privilege" do
1726 1726 setup do
1727 1727 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
1728 1728 Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
1729 1729 Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
1730 1730 Role.anonymous.add_permission! :add_issues, :add_issue_notes
1731 1731 end
1732 1732
1733 1733 context "#update" do
1734 1734 should "accept authorized status" do
1735 1735 assert_difference 'Journal.count' do
1736 1736 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
1737 1737 end
1738 1738 assert_equal 3, Issue.find(1).status_id
1739 1739 end
1740 1740
1741 1741 should "ignore unauthorized status" do
1742 1742 assert_difference 'Journal.count' do
1743 1743 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
1744 1744 end
1745 1745 assert_equal 1, Issue.find(1).status_id
1746 1746 end
1747 1747
1748 1748 should "accept authorized attributes changes" do
1749 1749 assert_difference 'Journal.count' do
1750 1750 put :update, :id => 1, :notes => 'just trying', :issue => {:assigned_to_id => 2}
1751 1751 end
1752 1752 issue = Issue.find(1)
1753 1753 assert_equal 2, issue.assigned_to_id
1754 1754 end
1755 1755
1756 1756 should "ignore unauthorized attributes changes" do
1757 1757 assert_difference 'Journal.count' do
1758 1758 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed'}
1759 1759 end
1760 1760 issue = Issue.find(1)
1761 1761 assert_equal "Can't print recipes", issue.subject
1762 1762 end
1763 1763 end
1764 1764
1765 1765 context "and :edit_issues permission" do
1766 1766 setup do
1767 1767 Role.anonymous.add_permission! :add_issues, :edit_issues
1768 1768 end
1769 1769
1770 1770 should "accept authorized status" do
1771 1771 assert_difference 'Journal.count' do
1772 1772 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
1773 1773 end
1774 1774 assert_equal 3, Issue.find(1).status_id
1775 1775 end
1776 1776
1777 1777 should "ignore unauthorized status" do
1778 1778 assert_difference 'Journal.count' do
1779 1779 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
1780 1780 end
1781 1781 assert_equal 1, Issue.find(1).status_id
1782 1782 end
1783 1783
1784 1784 should "accept authorized attributes changes" do
1785 1785 assert_difference 'Journal.count' do
1786 1786 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
1787 1787 end
1788 1788 issue = Issue.find(1)
1789 1789 assert_equal "changed", issue.subject
1790 1790 assert_equal 2, issue.assigned_to_id
1791 1791 end
1792 1792 end
1793 1793 end
1794 1794
1795 1795 def test_new_as_copy
1796 1796 @request.session[:user_id] = 2
1797 1797 get :new, :project_id => 1, :copy_from => 1
1798 1798
1799 1799 assert_response :success
1800 1800 assert_template 'new'
1801 1801
1802 1802 assert_not_nil assigns(:issue)
1803 1803 orig = Issue.find(1)
1804 1804 assert_equal 1, assigns(:issue).project_id
1805 1805 assert_equal orig.subject, assigns(:issue).subject
1806 1806 assert assigns(:issue).copy?
1807 1807
1808 1808 assert_tag 'form', :attributes => {:id => 'issue-form', :action => '/projects/ecookbook/issues'}
1809 1809 assert_tag 'select', :attributes => {:name => 'issue[project_id]'}
1810 1810 assert_tag 'select', :attributes => {:name => 'issue[project_id]'},
1811 1811 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}, :content => 'eCookbook'}
1812 1812 assert_tag 'select', :attributes => {:name => 'issue[project_id]'},
1813 1813 :child => {:tag => 'option', :attributes => {:value => '2', :selected => nil}, :content => 'OnlineStore'}
1814 1814 assert_tag 'input', :attributes => {:name => 'copy_from', :value => '1'}
1815 1815 end
1816 1816
1817 1817 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
1818 1818 @request.session[:user_id] = 2
1819 1819 issue = Issue.find(3)
1820 1820 assert issue.attachments.count > 0
1821 1821 get :new, :project_id => 1, :copy_from => 3
1822 1822
1823 1823 assert_tag 'input', :attributes => {:name => 'copy_attachments', :type => 'checkbox', :checked => 'checked', :value => '1'}
1824 1824 end
1825 1825
1826 1826 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
1827 1827 @request.session[:user_id] = 2
1828 1828 issue = Issue.find(3)
1829 1829 issue.attachments.delete_all
1830 1830 get :new, :project_id => 1, :copy_from => 3
1831 1831
1832 1832 assert_no_tag 'input', :attributes => {:name => 'copy_attachments', :type => 'checkbox', :checked => 'checked', :value => '1'}
1833 1833 end
1834 1834
1835 1835 def test_new_as_copy_with_invalid_issue_should_respond_with_404
1836 1836 @request.session[:user_id] = 2
1837 1837 get :new, :project_id => 1, :copy_from => 99999
1838 1838 assert_response 404
1839 1839 end
1840 1840
1841 1841 def test_create_as_copy_on_different_project
1842 1842 @request.session[:user_id] = 2
1843 1843 assert_difference 'Issue.count' do
1844 1844 post :create, :project_id => 1, :copy_from => 1,
1845 1845 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
1846 1846
1847 1847 assert_not_nil assigns(:issue)
1848 1848 assert assigns(:issue).copy?
1849 1849 end
1850 1850 issue = Issue.first(:order => 'id DESC')
1851 1851 assert_redirected_to "/issues/#{issue.id}"
1852 1852
1853 1853 assert_equal 2, issue.project_id
1854 1854 assert_equal 3, issue.tracker_id
1855 1855 assert_equal 'Copy', issue.subject
1856 1856 end
1857 1857
1858 1858 def test_create_as_copy_should_copy_attachments
1859 1859 @request.session[:user_id] = 2
1860 1860 issue = Issue.find(3)
1861 1861 count = issue.attachments.count
1862 1862 assert count > 0
1863 1863
1864 1864 assert_difference 'Issue.count' do
1865 1865 assert_difference 'Attachment.count', count do
1866 1866 assert_no_difference 'Journal.count' do
1867 1867 post :create, :project_id => 1, :copy_from => 3,
1868 1868 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'},
1869 1869 :copy_attachments => '1'
1870 1870 end
1871 1871 end
1872 1872 end
1873 1873 copy = Issue.first(:order => 'id DESC')
1874 1874 assert_equal count, copy.attachments.count
1875 1875 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
1876 1876 end
1877 1877
1878 1878 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
1879 1879 @request.session[:user_id] = 2
1880 1880 issue = Issue.find(3)
1881 1881 count = issue.attachments.count
1882 1882 assert count > 0
1883 1883
1884 1884 assert_difference 'Issue.count' do
1885 1885 assert_no_difference 'Attachment.count' do
1886 1886 assert_no_difference 'Journal.count' do
1887 1887 post :create, :project_id => 1, :copy_from => 3,
1888 1888 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'}
1889 1889 end
1890 1890 end
1891 1891 end
1892 1892 copy = Issue.first(:order => 'id DESC')
1893 1893 assert_equal 0, copy.attachments.count
1894 1894 end
1895 1895
1896 1896 def test_create_as_copy_with_attachments_should_add_new_files
1897 1897 @request.session[:user_id] = 2
1898 1898 issue = Issue.find(3)
1899 1899 count = issue.attachments.count
1900 1900 assert count > 0
1901 1901
1902 1902 assert_difference 'Issue.count' do
1903 1903 assert_difference 'Attachment.count', count + 1 do
1904 1904 assert_no_difference 'Journal.count' do
1905 1905 post :create, :project_id => 1, :copy_from => 3,
1906 1906 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'},
1907 1907 :copy_attachments => '1',
1908 1908 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
1909 1909 end
1910 1910 end
1911 1911 end
1912 1912 copy = Issue.first(:order => 'id DESC')
1913 1913 assert_equal count + 1, copy.attachments.count
1914 1914 end
1915 1915
1916 1916 def test_create_as_copy_with_failure
1917 1917 @request.session[:user_id] = 2
1918 1918 post :create, :project_id => 1, :copy_from => 1,
1919 1919 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
1920 1920
1921 1921 assert_response :success
1922 1922 assert_template 'new'
1923 1923
1924 1924 assert_not_nil assigns(:issue)
1925 1925 assert assigns(:issue).copy?
1926 1926
1927 1927 assert_tag 'form', :attributes => {:id => 'issue-form', :action => '/projects/ecookbook/issues'}
1928 1928 assert_tag 'select', :attributes => {:name => 'issue[project_id]'}
1929 1929 assert_tag 'select', :attributes => {:name => 'issue[project_id]'},
1930 1930 :child => {:tag => 'option', :attributes => {:value => '1', :selected => nil}, :content => 'eCookbook'}
1931 1931 assert_tag 'select', :attributes => {:name => 'issue[project_id]'},
1932 1932 :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}, :content => 'OnlineStore'}
1933 1933 assert_tag 'input', :attributes => {:name => 'copy_from', :value => '1'}
1934 1934 end
1935 1935
1936 1936 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
1937 1937 @request.session[:user_id] = 2
1938 1938 assert !User.find(2).member_of?(Project.find(4))
1939 1939
1940 1940 assert_difference 'Issue.count' do
1941 1941 post :create, :project_id => 1, :copy_from => 1,
1942 1942 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
1943 1943 end
1944 1944 issue = Issue.first(:order => 'id DESC')
1945 1945 assert_equal 1, issue.project_id
1946 1946 end
1947 1947
1948 1948 def test_get_edit
1949 1949 @request.session[:user_id] = 2
1950 1950 get :edit, :id => 1
1951 1951 assert_response :success
1952 1952 assert_template 'edit'
1953 1953 assert_not_nil assigns(:issue)
1954 1954 assert_equal Issue.find(1), assigns(:issue)
1955 1955
1956 1956 # Be sure we don't display inactive IssuePriorities
1957 1957 assert ! IssuePriority.find(15).active?
1958 1958 assert_no_tag :option, :attributes => {:value => '15'},
1959 1959 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
1960 1960 end
1961 1961
1962 1962 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
1963 1963 @request.session[:user_id] = 2
1964 1964 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
1965 1965
1966 1966 get :edit, :id => 1
1967 1967 assert_tag 'input', :attributes => {:name => 'time_entry[hours]'}
1968 1968 end
1969 1969
1970 1970 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
1971 1971 @request.session[:user_id] = 2
1972 1972 Role.find_by_name('Manager').remove_permission! :log_time
1973 1973
1974 1974 get :edit, :id => 1
1975 1975 assert_no_tag 'input', :attributes => {:name => 'time_entry[hours]'}
1976 1976 end
1977 1977
1978 1978 def test_get_edit_with_params
1979 1979 @request.session[:user_id] = 2
1980 1980 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
1981 1981 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => TimeEntryActivity.first.id }
1982 1982 assert_response :success
1983 1983 assert_template 'edit'
1984 1984
1985 1985 issue = assigns(:issue)
1986 1986 assert_not_nil issue
1987 1987
1988 1988 assert_equal 5, issue.status_id
1989 1989 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
1990 1990 :child => { :tag => 'option',
1991 1991 :content => 'Closed',
1992 1992 :attributes => { :selected => 'selected' } }
1993 1993
1994 1994 assert_equal 7, issue.priority_id
1995 1995 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
1996 1996 :child => { :tag => 'option',
1997 1997 :content => 'Urgent',
1998 1998 :attributes => { :selected => 'selected' } }
1999 1999
2000 2000 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => '2.5' }
2001 2001 assert_tag :select, :attributes => { :name => 'time_entry[activity_id]' },
2002 2002 :child => { :tag => 'option',
2003 2003 :attributes => { :selected => 'selected', :value => TimeEntryActivity.first.id } }
2004 2004 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => 'test_get_edit_with_params' }
2005 2005 end
2006 2006
2007 2007 def test_get_edit_with_multi_custom_field
2008 2008 field = CustomField.find(1)
2009 2009 field.update_attribute :multiple, true
2010 2010 issue = Issue.find(1)
2011 2011 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2012 2012 issue.save!
2013 2013
2014 2014 @request.session[:user_id] = 2
2015 2015 get :edit, :id => 1
2016 2016 assert_response :success
2017 2017 assert_template 'edit'
2018 2018
2019 2019 assert_tag 'select', :attributes => {:name => 'issue[custom_field_values][1][]', :multiple => 'multiple'}
2020 2020 assert_tag 'select', :attributes => {:name => 'issue[custom_field_values][1][]'},
2021 2021 :child => {:tag => 'option', :attributes => {:value => 'MySQL', :selected => 'selected'}}
2022 2022 assert_tag 'select', :attributes => {:name => 'issue[custom_field_values][1][]'},
2023 2023 :child => {:tag => 'option', :attributes => {:value => 'PostgreSQL', :selected => nil}}
2024 2024 assert_tag 'select', :attributes => {:name => 'issue[custom_field_values][1][]'},
2025 2025 :child => {:tag => 'option', :attributes => {:value => 'Oracle', :selected => 'selected'}}
2026 2026 end
2027 2027
2028 2028 def test_update_edit_form
2029 2029 @request.session[:user_id] = 2
2030 2030 xhr :put, :new, :project_id => 1,
2031 2031 :id => 1,
2032 2032 :issue => {:tracker_id => 2,
2033 2033 :subject => 'This is the test_new issue',
2034 2034 :description => 'This is the description',
2035 2035 :priority_id => 5}
2036 2036 assert_response :success
2037 2037 assert_template 'attributes'
2038 2038
2039 2039 issue = assigns(:issue)
2040 2040 assert_kind_of Issue, issue
2041 2041 assert_equal 1, issue.id
2042 2042 assert_equal 1, issue.project_id
2043 2043 assert_equal 2, issue.tracker_id
2044 2044 assert_equal 'This is the test_new issue', issue.subject
2045 2045 end
2046 2046
2047 2047 def test_update_edit_form_with_project_change
2048 2048 @request.session[:user_id] = 2
2049 2049 xhr :put, :new, :project_id => 1,
2050 2050 :id => 1,
2051 2051 :project_change => '1',
2052 2052 :issue => {:project_id => 2,
2053 2053 :tracker_id => 2,
2054 2054 :subject => 'This is the test_new issue',
2055 2055 :description => 'This is the description',
2056 2056 :priority_id => 5}
2057 2057 assert_response :success
2058 2058 assert_template 'form'
2059 2059
2060 2060 issue = assigns(:issue)
2061 2061 assert_kind_of Issue, issue
2062 2062 assert_equal 1, issue.id
2063 2063 assert_equal 2, issue.project_id
2064 2064 assert_equal 2, issue.tracker_id
2065 2065 assert_equal 'This is the test_new issue', issue.subject
2066 2066 end
2067 2067
2068 2068 def test_update_using_invalid_http_verbs
2069 2069 @request.session[:user_id] = 2
2070 2070 subject = 'Updated by an invalid http verb'
2071 2071
2072 2072 get :update, :id => 1, :issue => {:subject => subject}
2073 2073 assert_not_equal subject, Issue.find(1).subject
2074 2074
2075 2075 post :update, :id => 1, :issue => {:subject => subject}
2076 2076 assert_not_equal subject, Issue.find(1).subject
2077 2077
2078 2078 delete :update, :id => 1, :issue => {:subject => subject}
2079 2079 assert_not_equal subject, Issue.find(1).subject
2080 2080 end
2081 2081
2082 2082 def test_put_update_without_custom_fields_param
2083 2083 @request.session[:user_id] = 2
2084 2084 ActionMailer::Base.deliveries.clear
2085 2085
2086 2086 issue = Issue.find(1)
2087 2087 assert_equal '125', issue.custom_value_for(2).value
2088 2088 old_subject = issue.subject
2089 2089 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
2090 2090
2091 2091 assert_difference('Journal.count') do
2092 2092 assert_difference('JournalDetail.count', 2) do
2093 2093 put :update, :id => 1, :issue => {:subject => new_subject,
2094 2094 :priority_id => '6',
2095 2095 :category_id => '1' # no change
2096 2096 }
2097 2097 end
2098 2098 end
2099 2099 assert_redirected_to :action => 'show', :id => '1'
2100 2100 issue.reload
2101 2101 assert_equal new_subject, issue.subject
2102 2102 # Make sure custom fields were not cleared
2103 2103 assert_equal '125', issue.custom_value_for(2).value
2104 2104
2105 2105 mail = ActionMailer::Base.deliveries.last
2106 2106 assert_kind_of TMail::Mail, mail
2107 2107 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2108 2108 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
2109 2109 end
2110 2110
2111 2111 def test_put_update_with_project_change
2112 2112 @request.session[:user_id] = 2
2113 2113 ActionMailer::Base.deliveries.clear
2114 2114
2115 2115 assert_difference('Journal.count') do
2116 2116 assert_difference('JournalDetail.count', 3) do
2117 2117 put :update, :id => 1, :issue => {:project_id => '2',
2118 2118 :tracker_id => '1', # no change
2119 2119 :priority_id => '6',
2120 2120 :category_id => '3'
2121 2121 }
2122 2122 end
2123 2123 end
2124 2124 assert_redirected_to :action => 'show', :id => '1'
2125 2125 issue = Issue.find(1)
2126 2126 assert_equal 2, issue.project_id
2127 2127 assert_equal 1, issue.tracker_id
2128 2128 assert_equal 6, issue.priority_id
2129 2129 assert_equal 3, issue.category_id
2130 2130
2131 2131 mail = ActionMailer::Base.deliveries.last
2132 2132 assert_not_nil mail
2133 2133 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2134 2134 assert mail.body.include?("Project changed from eCookbook to OnlineStore")
2135 2135 end
2136 2136
2137 2137 def test_put_update_with_tracker_change
2138 2138 @request.session[:user_id] = 2
2139 2139 ActionMailer::Base.deliveries.clear
2140 2140
2141 2141 assert_difference('Journal.count') do
2142 2142 assert_difference('JournalDetail.count', 2) do
2143 2143 put :update, :id => 1, :issue => {:project_id => '1',
2144 2144 :tracker_id => '2',
2145 2145 :priority_id => '6'
2146 2146 }
2147 2147 end
2148 2148 end
2149 2149 assert_redirected_to :action => 'show', :id => '1'
2150 2150 issue = Issue.find(1)
2151 2151 assert_equal 1, issue.project_id
2152 2152 assert_equal 2, issue.tracker_id
2153 2153 assert_equal 6, issue.priority_id
2154 2154 assert_equal 1, issue.category_id
2155 2155
2156 2156 mail = ActionMailer::Base.deliveries.last
2157 2157 assert_not_nil mail
2158 2158 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2159 2159 assert mail.body.include?("Tracker changed from Bug to Feature request")
2160 2160 end
2161 2161
2162 2162 def test_put_update_with_custom_field_change
2163 2163 @request.session[:user_id] = 2
2164 2164 issue = Issue.find(1)
2165 2165 assert_equal '125', issue.custom_value_for(2).value
2166 2166
2167 2167 assert_difference('Journal.count') do
2168 2168 assert_difference('JournalDetail.count', 3) do
2169 2169 put :update, :id => 1, :issue => {:subject => 'Custom field change',
2170 2170 :priority_id => '6',
2171 2171 :category_id => '1', # no change
2172 2172 :custom_field_values => { '2' => 'New custom value' }
2173 2173 }
2174 2174 end
2175 2175 end
2176 2176 assert_redirected_to :action => 'show', :id => '1'
2177 2177 issue.reload
2178 2178 assert_equal 'New custom value', issue.custom_value_for(2).value
2179 2179
2180 2180 mail = ActionMailer::Base.deliveries.last
2181 2181 assert_kind_of TMail::Mail, mail
2182 2182 assert mail.body.include?("Searchable field changed from 125 to New custom value")
2183 2183 end
2184 2184
2185 2185 def test_put_update_with_multi_custom_field_change
2186 2186 field = CustomField.find(1)
2187 2187 field.update_attribute :multiple, true
2188 2188 issue = Issue.find(1)
2189 2189 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2190 2190 issue.save!
2191 2191
2192 2192 @request.session[:user_id] = 2
2193 2193 assert_difference('Journal.count') do
2194 2194 assert_difference('JournalDetail.count', 3) do
2195 2195 put :update, :id => 1,
2196 2196 :issue => {
2197 2197 :subject => 'Custom field change',
2198 2198 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
2199 2199 }
2200 2200 end
2201 2201 end
2202 2202 assert_redirected_to :action => 'show', :id => '1'
2203 2203 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
2204 2204 end
2205 2205
2206 2206 def test_put_update_with_status_and_assignee_change
2207 2207 issue = Issue.find(1)
2208 2208 assert_equal 1, issue.status_id
2209 2209 @request.session[:user_id] = 2
2210 2210 assert_difference('TimeEntry.count', 0) do
2211 2211 put :update,
2212 2212 :id => 1,
2213 2213 :issue => { :status_id => 2, :assigned_to_id => 3 },
2214 2214 :notes => 'Assigned to dlopper',
2215 2215 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
2216 2216 end
2217 2217 assert_redirected_to :action => 'show', :id => '1'
2218 2218 issue.reload
2219 2219 assert_equal 2, issue.status_id
2220 2220 j = Journal.find(:first, :order => 'id DESC')
2221 2221 assert_equal 'Assigned to dlopper', j.notes
2222 2222 assert_equal 2, j.details.size
2223 2223
2224 2224 mail = ActionMailer::Base.deliveries.last
2225 2225 assert mail.body.include?("Status changed from New to Assigned")
2226 2226 # subject should contain the new status
2227 2227 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
2228 2228 end
2229 2229
2230 2230 def test_put_update_with_note_only
2231 2231 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
2232 2232 # anonymous user
2233 2233 put :update,
2234 2234 :id => 1,
2235 2235 :notes => notes
2236 2236 assert_redirected_to :action => 'show', :id => '1'
2237 2237 j = Journal.find(:first, :order => 'id DESC')
2238 2238 assert_equal notes, j.notes
2239 2239 assert_equal 0, j.details.size
2240 2240 assert_equal User.anonymous, j.user
2241 2241
2242 2242 mail = ActionMailer::Base.deliveries.last
2243 2243 assert mail.body.include?(notes)
2244 2244 end
2245 2245
2246 2246 def test_put_update_with_note_and_spent_time
2247 2247 @request.session[:user_id] = 2
2248 2248 spent_hours_before = Issue.find(1).spent_hours
2249 2249 assert_difference('TimeEntry.count') do
2250 2250 put :update,
2251 2251 :id => 1,
2252 2252 :notes => '2.5 hours added',
2253 2253 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
2254 2254 end
2255 2255 assert_redirected_to :action => 'show', :id => '1'
2256 2256
2257 2257 issue = Issue.find(1)
2258 2258
2259 2259 j = Journal.find(:first, :order => 'id DESC')
2260 2260 assert_equal '2.5 hours added', j.notes
2261 2261 assert_equal 0, j.details.size
2262 2262
2263 2263 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
2264 2264 assert_not_nil t
2265 2265 assert_equal 2.5, t.hours
2266 2266 assert_equal spent_hours_before + 2.5, issue.spent_hours
2267 2267 end
2268 2268
2269 2269 def test_put_update_with_attachment_only
2270 2270 set_tmp_attachments_directory
2271 2271
2272 2272 # Delete all fixtured journals, a race condition can occur causing the wrong
2273 2273 # journal to get fetched in the next find.
2274 2274 Journal.delete_all
2275 2275
2276 2276 # anonymous user
2277 2277 assert_difference 'Attachment.count' do
2278 2278 put :update, :id => 1,
2279 2279 :notes => '',
2280 2280 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2281 2281 end
2282 2282
2283 2283 assert_redirected_to :action => 'show', :id => '1'
2284 2284 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
2285 2285 assert j.notes.blank?
2286 2286 assert_equal 1, j.details.size
2287 2287 assert_equal 'testfile.txt', j.details.first.value
2288 2288 assert_equal User.anonymous, j.user
2289 2289
2290 2290 attachment = Attachment.first(:order => 'id DESC')
2291 2291 assert_equal Issue.find(1), attachment.container
2292 2292 assert_equal User.anonymous, attachment.author
2293 2293 assert_equal 'testfile.txt', attachment.filename
2294 2294 assert_equal 'text/plain', attachment.content_type
2295 2295 assert_equal 'test file', attachment.description
2296 2296 assert_equal 59, attachment.filesize
2297 2297 assert File.exists?(attachment.diskfile)
2298 2298 assert_equal 59, File.size(attachment.diskfile)
2299 2299
2300 2300 mail = ActionMailer::Base.deliveries.last
2301 2301 assert mail.body.include?('testfile.txt')
2302 2302 end
2303 2303
2304 2304 def test_put_update_with_attachment_that_fails_to_save
2305 2305 set_tmp_attachments_directory
2306 2306
2307 2307 # Delete all fixtured journals, a race condition can occur causing the wrong
2308 2308 # journal to get fetched in the next find.
2309 2309 Journal.delete_all
2310 2310
2311 2311 # Mock out the unsaved attachment
2312 2312 Attachment.any_instance.stubs(:create).returns(Attachment.new)
2313 2313
2314 2314 # anonymous user
2315 2315 put :update,
2316 2316 :id => 1,
2317 2317 :notes => '',
2318 2318 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
2319 2319 assert_redirected_to :action => 'show', :id => '1'
2320 2320 assert_equal '1 file(s) could not be saved.', flash[:warning]
2321 2321 end
2322 2322
2323 2323 def test_put_update_with_no_change
2324 2324 issue = Issue.find(1)
2325 2325 issue.journals.clear
2326 2326 ActionMailer::Base.deliveries.clear
2327 2327
2328 2328 put :update,
2329 2329 :id => 1,
2330 2330 :notes => ''
2331 2331 assert_redirected_to :action => 'show', :id => '1'
2332 2332
2333 2333 issue.reload
2334 2334 assert issue.journals.empty?
2335 2335 # No email should be sent
2336 2336 assert ActionMailer::Base.deliveries.empty?
2337 2337 end
2338 2338
2339 2339 def test_put_update_should_send_a_notification
2340 2340 @request.session[:user_id] = 2
2341 2341 ActionMailer::Base.deliveries.clear
2342 2342 issue = Issue.find(1)
2343 2343 old_subject = issue.subject
2344 2344 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
2345 2345
2346 2346 put :update, :id => 1, :issue => {:subject => new_subject,
2347 2347 :priority_id => '6',
2348 2348 :category_id => '1' # no change
2349 2349 }
2350 2350 assert_equal 1, ActionMailer::Base.deliveries.size
2351 2351 end
2352 2352
2353 2353 def test_put_update_with_invalid_spent_time_hours_only
2354 2354 @request.session[:user_id] = 2
2355 2355 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
2356 2356
2357 2357 assert_no_difference('Journal.count') do
2358 2358 put :update,
2359 2359 :id => 1,
2360 2360 :notes => notes,
2361 2361 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
2362 2362 end
2363 2363 assert_response :success
2364 2364 assert_template 'edit'
2365 2365
2366 2366 assert_error_tag :descendant => {:content => /Activity can't be blank/}
2367 2367 assert_tag :textarea, :attributes => { :name => 'notes' }, :content => notes
2368 2368 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
2369 2369 end
2370 2370
2371 2371 def test_put_update_with_invalid_spent_time_comments_only
2372 2372 @request.session[:user_id] = 2
2373 2373 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
2374 2374
2375 2375 assert_no_difference('Journal.count') do
2376 2376 put :update,
2377 2377 :id => 1,
2378 2378 :notes => notes,
2379 2379 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
2380 2380 end
2381 2381 assert_response :success
2382 2382 assert_template 'edit'
2383 2383
2384 2384 assert_error_tag :descendant => {:content => /Activity can't be blank/}
2385 2385 assert_error_tag :descendant => {:content => /Hours can't be blank/}
2386 2386 assert_tag :textarea, :attributes => { :name => 'notes' }, :content => notes
2387 2387 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => "this is my comment" }
2388 2388 end
2389 2389
2390 2390 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
2391 2391 issue = Issue.find(2)
2392 2392 @request.session[:user_id] = 2
2393 2393
2394 2394 put :update,
2395 2395 :id => issue.id,
2396 2396 :issue => {
2397 2397 :fixed_version_id => 4
2398 2398 }
2399 2399
2400 2400 assert_response :redirect
2401 2401 issue.reload
2402 2402 assert_equal 4, issue.fixed_version_id
2403 2403 assert_not_equal issue.project_id, issue.fixed_version.project_id
2404 2404 end
2405 2405
2406 2406 def test_put_update_should_redirect_back_using_the_back_url_parameter
2407 2407 issue = Issue.find(2)
2408 2408 @request.session[:user_id] = 2
2409 2409
2410 2410 put :update,
2411 2411 :id => issue.id,
2412 2412 :issue => {
2413 2413 :fixed_version_id => 4
2414 2414 },
2415 2415 :back_url => '/issues'
2416 2416
2417 2417 assert_response :redirect
2418 2418 assert_redirected_to '/issues'
2419 2419 end
2420 2420
2421 2421 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
2422 2422 issue = Issue.find(2)
2423 2423 @request.session[:user_id] = 2
2424 2424
2425 2425 put :update,
2426 2426 :id => issue.id,
2427 2427 :issue => {
2428 2428 :fixed_version_id => 4
2429 2429 },
2430 2430 :back_url => 'http://google.com'
2431 2431
2432 2432 assert_response :redirect
2433 2433 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
2434 2434 end
2435 2435
2436 2436 def test_get_bulk_edit
2437 2437 @request.session[:user_id] = 2
2438 2438 get :bulk_edit, :ids => [1, 2]
2439 2439 assert_response :success
2440 2440 assert_template 'bulk_edit'
2441 2441
2442 2442 assert_tag :select, :attributes => {:name => 'issue[project_id]'}
2443 2443 assert_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
2444 2444
2445 2445 # Project specific custom field, date type
2446 2446 field = CustomField.find(9)
2447 2447 assert !field.is_for_all?
2448 2448 assert_equal 'date', field.field_format
2449 2449 assert_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
2450 2450
2451 2451 # System wide custom field
2452 2452 assert CustomField.find(1).is_for_all?
2453 2453 assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
2454 2454
2455 2455 # Be sure we don't display inactive IssuePriorities
2456 2456 assert ! IssuePriority.find(15).active?
2457 2457 assert_no_tag :option, :attributes => {:value => '15'},
2458 2458 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
2459 2459 end
2460 2460
2461 2461 def test_get_bulk_edit_on_different_projects
2462 2462 @request.session[:user_id] = 2
2463 2463 get :bulk_edit, :ids => [1, 2, 6]
2464 2464 assert_response :success
2465 2465 assert_template 'bulk_edit'
2466 2466
2467 2467 # Can not set issues from different projects as children of an issue
2468 2468 assert_no_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
2469 2469
2470 2470 # Project specific custom field, date type
2471 2471 field = CustomField.find(9)
2472 2472 assert !field.is_for_all?
2473 2473 assert !field.project_ids.include?(Issue.find(6).project_id)
2474 2474 assert_no_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
2475 2475 end
2476 2476
2477 2477 def test_get_bulk_edit_with_user_custom_field
2478 2478 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true)
2479 2479
2480 2480 @request.session[:user_id] = 2
2481 2481 get :bulk_edit, :ids => [1, 2]
2482 2482 assert_response :success
2483 2483 assert_template 'bulk_edit'
2484 2484
2485 2485 assert_tag :select,
2486 2486 :attributes => {:name => "issue[custom_field_values][#{field.id}]"},
2487 2487 :children => {
2488 2488 :only => {:tag => 'option'},
2489 2489 :count => Project.find(1).users.count + 1
2490 2490 }
2491 2491 end
2492 2492
2493 2493 def test_get_bulk_edit_with_version_custom_field
2494 2494 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true)
2495 2495
2496 2496 @request.session[:user_id] = 2
2497 2497 get :bulk_edit, :ids => [1, 2]
2498 2498 assert_response :success
2499 2499 assert_template 'bulk_edit'
2500 2500
2501 2501 assert_tag :select,
2502 2502 :attributes => {:name => "issue[custom_field_values][#{field.id}]"},
2503 2503 :children => {
2504 2504 :only => {:tag => 'option'},
2505 2505 :count => Project.find(1).shared_versions.count + 1
2506 2506 }
2507 2507 end
2508 2508
2509 2509 def test_get_bulk_edit_with_multi_custom_field
2510 2510 field = CustomField.find(1)
2511 2511 field.update_attribute :multiple, true
2512 2512
2513 2513 @request.session[:user_id] = 2
2514 2514 get :bulk_edit, :ids => [1, 2]
2515 2515 assert_response :success
2516 2516 assert_template 'bulk_edit'
2517 2517
2518 2518 assert_tag :select,
2519 2519 :attributes => {:name => "issue[custom_field_values][1][]"},
2520 2520 :children => {
2521 2521 :only => {:tag => 'option'},
2522 2522 :count => 3
2523 2523 }
2524 2524 end
2525 2525
2526 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
2527 Workflow.delete_all
2528 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 1)
2529 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
2530 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
2531 Workflow.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
2532 Workflow.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
2533 Workflow.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
2534 @request.session[:user_id] = 2
2535 get :bulk_edit, :ids => [1, 2]
2536
2537 assert_response :success
2538 statuses = assigns(:available_statuses)
2539 assert_not_nil statuses
2540 assert_equal [1, 3], statuses.map(&:id).sort
2541
2542 assert_tag 'select', :attributes => {:name => 'issue[status_id]'},
2543 :children => {:count => 3} # 2 statuses + "no change" option
2544 end
2545
2526 2546 def test_bulk_update
2527 2547 @request.session[:user_id] = 2
2528 2548 # update issues priority
2529 2549 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
2530 2550 :issue => {:priority_id => 7,
2531 2551 :assigned_to_id => '',
2532 2552 :custom_field_values => {'2' => ''}}
2533 2553
2534 2554 assert_response 302
2535 2555 # check that the issues were updated
2536 2556 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
2537 2557
2538 2558 issue = Issue.find(1)
2539 2559 journal = issue.journals.find(:first, :order => 'created_on DESC')
2540 2560 assert_equal '125', issue.custom_value_for(2).value
2541 2561 assert_equal 'Bulk editing', journal.notes
2542 2562 assert_equal 1, journal.details.size
2543 2563 end
2544 2564
2545 2565 def test_bulk_update_with_group_assignee
2546 2566 group = Group.find(11)
2547 2567 project = Project.find(1)
2548 2568 project.members << Member.new(:principal => group, :roles => [Role.first])
2549 2569
2550 2570 @request.session[:user_id] = 2
2551 2571 # update issues assignee
2552 2572 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
2553 2573 :issue => {:priority_id => '',
2554 2574 :assigned_to_id => group.id,
2555 2575 :custom_field_values => {'2' => ''}}
2556 2576
2557 2577 assert_response 302
2558 2578 assert_equal [group, group], Issue.find_all_by_id([1, 2]).collect {|i| i.assigned_to}
2559 2579 end
2560 2580
2561 2581 def test_bulk_update_on_different_projects
2562 2582 @request.session[:user_id] = 2
2563 2583 # update issues priority
2564 2584 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
2565 2585 :issue => {:priority_id => 7,
2566 2586 :assigned_to_id => '',
2567 2587 :custom_field_values => {'2' => ''}}
2568 2588
2569 2589 assert_response 302
2570 2590 # check that the issues were updated
2571 2591 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
2572 2592
2573 2593 issue = Issue.find(1)
2574 2594 journal = issue.journals.find(:first, :order => 'created_on DESC')
2575 2595 assert_equal '125', issue.custom_value_for(2).value
2576 2596 assert_equal 'Bulk editing', journal.notes
2577 2597 assert_equal 1, journal.details.size
2578 2598 end
2579 2599
2580 2600 def test_bulk_update_on_different_projects_without_rights
2581 2601 @request.session[:user_id] = 3
2582 2602 user = User.find(3)
2583 2603 action = { :controller => "issues", :action => "bulk_update" }
2584 2604 assert user.allowed_to?(action, Issue.find(1).project)
2585 2605 assert ! user.allowed_to?(action, Issue.find(6).project)
2586 2606 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
2587 2607 :issue => {:priority_id => 7,
2588 2608 :assigned_to_id => '',
2589 2609 :custom_field_values => {'2' => ''}}
2590 2610 assert_response 403
2591 2611 assert_not_equal "Bulk should fail", Journal.last.notes
2592 2612 end
2593 2613
2594 2614 def test_bullk_update_should_send_a_notification
2595 2615 @request.session[:user_id] = 2
2596 2616 ActionMailer::Base.deliveries.clear
2597 2617 post(:bulk_update,
2598 2618 {
2599 2619 :ids => [1, 2],
2600 2620 :notes => 'Bulk editing',
2601 2621 :issue => {
2602 2622 :priority_id => 7,
2603 2623 :assigned_to_id => '',
2604 2624 :custom_field_values => {'2' => ''}
2605 2625 }
2606 2626 })
2607 2627
2608 2628 assert_response 302
2609 2629 assert_equal 2, ActionMailer::Base.deliveries.size
2610 2630 end
2611 2631
2612 2632 def test_bulk_update_project
2613 2633 @request.session[:user_id] = 2
2614 2634 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
2615 2635 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
2616 2636 # Issues moved to project 2
2617 2637 assert_equal 2, Issue.find(1).project_id
2618 2638 assert_equal 2, Issue.find(2).project_id
2619 2639 # No tracker change
2620 2640 assert_equal 1, Issue.find(1).tracker_id
2621 2641 assert_equal 2, Issue.find(2).tracker_id
2622 2642 end
2623 2643
2624 2644 def test_bulk_update_project_on_single_issue_should_follow_when_needed
2625 2645 @request.session[:user_id] = 2
2626 2646 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
2627 2647 assert_redirected_to '/issues/1'
2628 2648 end
2629 2649
2630 2650 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
2631 2651 @request.session[:user_id] = 2
2632 2652 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
2633 2653 assert_redirected_to '/projects/onlinestore/issues'
2634 2654 end
2635 2655
2636 2656 def test_bulk_update_tracker
2637 2657 @request.session[:user_id] = 2
2638 2658 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
2639 2659 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
2640 2660 assert_equal 2, Issue.find(1).tracker_id
2641 2661 assert_equal 2, Issue.find(2).tracker_id
2642 2662 end
2643 2663
2644 2664 def test_bulk_update_status
2645 2665 @request.session[:user_id] = 2
2646 2666 # update issues priority
2647 2667 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
2648 2668 :issue => {:priority_id => '',
2649 2669 :assigned_to_id => '',
2650 2670 :status_id => '5'}
2651 2671
2652 2672 assert_response 302
2653 2673 issue = Issue.find(1)
2654 2674 assert issue.closed?
2655 2675 end
2656 2676
2657 2677 def test_bulk_update_priority
2658 2678 @request.session[:user_id] = 2
2659 2679 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
2660 2680
2661 2681 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
2662 2682 assert_equal 6, Issue.find(1).priority_id
2663 2683 assert_equal 6, Issue.find(2).priority_id
2664 2684 end
2665 2685
2666 2686 def test_bulk_update_with_notes
2667 2687 @request.session[:user_id] = 2
2668 2688 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
2669 2689
2670 2690 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
2671 2691 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
2672 2692 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
2673 2693 end
2674 2694
2675 2695 def test_bulk_update_parent_id
2676 2696 @request.session[:user_id] = 2
2677 2697 post :bulk_update, :ids => [1, 3],
2678 2698 :notes => 'Bulk editing parent',
2679 2699 :issue => {:priority_id => '', :assigned_to_id => '', :status_id => '', :parent_issue_id => '2'}
2680 2700
2681 2701 assert_response 302
2682 2702 parent = Issue.find(2)
2683 2703 assert_equal parent.id, Issue.find(1).parent_id
2684 2704 assert_equal parent.id, Issue.find(3).parent_id
2685 2705 assert_equal [1, 3], parent.children.collect(&:id).sort
2686 2706 end
2687 2707
2688 2708 def test_bulk_update_custom_field
2689 2709 @request.session[:user_id] = 2
2690 2710 # update issues priority
2691 2711 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
2692 2712 :issue => {:priority_id => '',
2693 2713 :assigned_to_id => '',
2694 2714 :custom_field_values => {'2' => '777'}}
2695 2715
2696 2716 assert_response 302
2697 2717
2698 2718 issue = Issue.find(1)
2699 2719 journal = issue.journals.find(:first, :order => 'created_on DESC')
2700 2720 assert_equal '777', issue.custom_value_for(2).value
2701 2721 assert_equal 1, journal.details.size
2702 2722 assert_equal '125', journal.details.first.old_value
2703 2723 assert_equal '777', journal.details.first.value
2704 2724 end
2705 2725
2706 2726 def test_bulk_update_multi_custom_field
2707 2727 field = CustomField.find(1)
2708 2728 field.update_attribute :multiple, true
2709 2729
2710 2730 @request.session[:user_id] = 2
2711 2731 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
2712 2732 :issue => {:priority_id => '',
2713 2733 :assigned_to_id => '',
2714 2734 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
2715 2735
2716 2736 assert_response 302
2717 2737
2718 2738 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
2719 2739 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
2720 2740 # the custom field is not associated with the issue tracker
2721 2741 assert_nil Issue.find(2).custom_field_value(1)
2722 2742 end
2723 2743
2724 2744 def test_bulk_update_unassign
2725 2745 assert_not_nil Issue.find(2).assigned_to
2726 2746 @request.session[:user_id] = 2
2727 2747 # unassign issues
2728 2748 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
2729 2749 assert_response 302
2730 2750 # check that the issues were updated
2731 2751 assert_nil Issue.find(2).assigned_to
2732 2752 end
2733 2753
2734 2754 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
2735 2755 @request.session[:user_id] = 2
2736 2756
2737 2757 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
2738 2758
2739 2759 assert_response :redirect
2740 2760 issues = Issue.find([1,2])
2741 2761 issues.each do |issue|
2742 2762 assert_equal 4, issue.fixed_version_id
2743 2763 assert_not_equal issue.project_id, issue.fixed_version.project_id
2744 2764 end
2745 2765 end
2746 2766
2747 2767 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
2748 2768 @request.session[:user_id] = 2
2749 2769 post :bulk_update, :ids => [1,2], :back_url => '/issues'
2750 2770
2751 2771 assert_response :redirect
2752 2772 assert_redirected_to '/issues'
2753 2773 end
2754 2774
2755 2775 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
2756 2776 @request.session[:user_id] = 2
2757 2777 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
2758 2778
2759 2779 assert_response :redirect
2760 2780 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
2761 2781 end
2762 2782
2763 2783 def test_bulk_copy_to_another_project
2764 2784 @request.session[:user_id] = 2
2765 2785 assert_difference 'Issue.count', 2 do
2766 2786 assert_no_difference 'Project.find(1).issues.count' do
2767 2787 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
2768 2788 end
2769 2789 end
2770 2790 assert_redirected_to '/projects/ecookbook/issues'
2771 2791 end
2772 2792
2773 2793 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
2774 2794 @request.session[:user_id] = 2
2775 2795 issue_before_move = Issue.find(1)
2776 2796 assert_difference 'Issue.count', 1 do
2777 2797 assert_no_difference 'Project.find(1).issues.count' do
2778 2798 post :bulk_update, :ids => [1], :copy => '1',
2779 2799 :issue => {
2780 2800 :project_id => '2', :tracker_id => '', :assigned_to_id => '',
2781 2801 :status_id => '', :start_date => '', :due_date => ''
2782 2802 }
2783 2803 end
2784 2804 end
2785 2805 issue_after_move = Issue.first(:order => 'id desc', :conditions => {:project_id => 2})
2786 2806 assert_equal issue_before_move.tracker_id, issue_after_move.tracker_id
2787 2807 assert_equal issue_before_move.status_id, issue_after_move.status_id
2788 2808 assert_equal issue_before_move.assigned_to_id, issue_after_move.assigned_to_id
2789 2809 end
2790 2810
2791 2811 def test_bulk_copy_should_allow_changing_the_issue_attributes
2792 2812 # Fixes random test failure with Mysql
2793 2813 # where Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
2794 2814 # doesn't return the expected results
2795 2815 Issue.delete_all("project_id=2")
2796 2816
2797 2817 @request.session[:user_id] = 2
2798 2818 assert_difference 'Issue.count', 2 do
2799 2819 assert_no_difference 'Project.find(1).issues.count' do
2800 2820 post :bulk_update, :ids => [1, 2], :copy => '1',
2801 2821 :issue => {
2802 2822 :project_id => '2', :tracker_id => '', :assigned_to_id => '4',
2803 2823 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
2804 2824 }
2805 2825 end
2806 2826 end
2807 2827
2808 2828 copied_issues = Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
2809 2829 assert_equal 2, copied_issues.size
2810 2830 copied_issues.each do |issue|
2811 2831 assert_equal 2, issue.project_id, "Project is incorrect"
2812 2832 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
2813 2833 assert_equal 3, issue.status_id, "Status is incorrect"
2814 2834 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
2815 2835 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
2816 2836 end
2817 2837 end
2818 2838
2819 2839 def test_bulk_copy_should_allow_adding_a_note
2820 2840 @request.session[:user_id] = 2
2821 2841 assert_difference 'Issue.count', 1 do
2822 2842 post :bulk_update, :ids => [1], :copy => '1',
2823 2843 :notes => 'Copying one issue',
2824 2844 :issue => {
2825 2845 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
2826 2846 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
2827 2847 }
2828 2848 end
2829 2849
2830 2850 issue = Issue.first(:order => 'id DESC')
2831 2851 assert_equal 1, issue.journals.size
2832 2852 journal = issue.journals.first
2833 2853 assert_equal 0, journal.details.size
2834 2854 assert_equal 'Copying one issue', journal.notes
2835 2855 end
2836 2856
2837 2857 def test_bulk_copy_to_another_project_should_follow_when_needed
2838 2858 @request.session[:user_id] = 2
2839 2859 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
2840 2860 issue = Issue.first(:order => 'id DESC')
2841 2861 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
2842 2862 end
2843 2863
2844 2864 def test_destroy_issue_with_no_time_entries
2845 2865 assert_nil TimeEntry.find_by_issue_id(2)
2846 2866 @request.session[:user_id] = 2
2847 2867
2848 2868 assert_difference 'Issue.count', -1 do
2849 2869 delete :destroy, :id => 2
2850 2870 end
2851 2871 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
2852 2872 assert_nil Issue.find_by_id(2)
2853 2873 end
2854 2874
2855 2875 def test_destroy_issues_with_time_entries
2856 2876 @request.session[:user_id] = 2
2857 2877
2858 2878 assert_no_difference 'Issue.count' do
2859 2879 delete :destroy, :ids => [1, 3]
2860 2880 end
2861 2881 assert_response :success
2862 2882 assert_template 'destroy'
2863 2883 assert_not_nil assigns(:hours)
2864 2884 assert Issue.find_by_id(1) && Issue.find_by_id(3)
2865 2885 assert_tag 'form',
2866 2886 :descendant => {:tag => 'input', :attributes => {:name => '_method', :value => 'delete'}}
2867 2887 end
2868 2888
2869 2889 def test_destroy_issues_and_destroy_time_entries
2870 2890 @request.session[:user_id] = 2
2871 2891
2872 2892 assert_difference 'Issue.count', -2 do
2873 2893 assert_difference 'TimeEntry.count', -3 do
2874 2894 delete :destroy, :ids => [1, 3], :todo => 'destroy'
2875 2895 end
2876 2896 end
2877 2897 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
2878 2898 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
2879 2899 assert_nil TimeEntry.find_by_id([1, 2])
2880 2900 end
2881 2901
2882 2902 def test_destroy_issues_and_assign_time_entries_to_project
2883 2903 @request.session[:user_id] = 2
2884 2904
2885 2905 assert_difference 'Issue.count', -2 do
2886 2906 assert_no_difference 'TimeEntry.count' do
2887 2907 delete :destroy, :ids => [1, 3], :todo => 'nullify'
2888 2908 end
2889 2909 end
2890 2910 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
2891 2911 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
2892 2912 assert_nil TimeEntry.find(1).issue_id
2893 2913 assert_nil TimeEntry.find(2).issue_id
2894 2914 end
2895 2915
2896 2916 def test_destroy_issues_and_reassign_time_entries_to_another_issue
2897 2917 @request.session[:user_id] = 2
2898 2918
2899 2919 assert_difference 'Issue.count', -2 do
2900 2920 assert_no_difference 'TimeEntry.count' do
2901 2921 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
2902 2922 end
2903 2923 end
2904 2924 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
2905 2925 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
2906 2926 assert_equal 2, TimeEntry.find(1).issue_id
2907 2927 assert_equal 2, TimeEntry.find(2).issue_id
2908 2928 end
2909 2929
2910 2930 def test_destroy_issues_from_different_projects
2911 2931 @request.session[:user_id] = 2
2912 2932
2913 2933 assert_difference 'Issue.count', -3 do
2914 2934 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
2915 2935 end
2916 2936 assert_redirected_to :controller => 'issues', :action => 'index'
2917 2937 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
2918 2938 end
2919 2939
2920 2940 def test_destroy_parent_and_child_issues
2921 2941 parent = Issue.generate!(:project_id => 1, :tracker_id => 1)
2922 2942 child = Issue.generate!(:project_id => 1, :tracker_id => 1, :parent_issue_id => parent.id)
2923 2943 assert child.is_descendant_of?(parent.reload)
2924 2944
2925 2945 @request.session[:user_id] = 2
2926 2946 assert_difference 'Issue.count', -2 do
2927 2947 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
2928 2948 end
2929 2949 assert_response 302
2930 2950 end
2931 2951
2932 2952 def test_default_search_scope
2933 2953 get :index
2934 2954 assert_tag :div, :attributes => {:id => 'quick-search'},
2935 2955 :child => {:tag => 'form',
2936 2956 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
2937 2957 end
2938 2958 end
General Comments 0
You need to be logged in to leave comments. Login now