##// END OF EJS Templates
Merged r14141 and r14146 (#19276)....
Jean-Philippe Lang -
r13768:20eea840f52a
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,508 +1,514
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssuesController < ApplicationController
19 19 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 24 before_filter :authorize, :except => [:index, :new, :create]
25 25 before_filter :find_optional_project, :only => [:index, :new, :create]
26 26 before_filter :build_new_issue_from_params, :only => [:new, :create]
27 27 accept_rss_auth :index, :show
28 28 accept_api_auth :index, :show, :create, :update, :destroy
29 29
30 30 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
31 31
32 32 helper :journals
33 33 helper :projects
34 34 helper :custom_fields
35 35 helper :issue_relations
36 36 helper :watchers
37 37 helper :attachments
38 38 helper :queries
39 39 include QueriesHelper
40 40 helper :repositories
41 41 helper :sort
42 42 include SortHelper
43 43 helper :timelog
44 44
45 45 def index
46 46 retrieve_query
47 47 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
48 48 sort_update(@query.sortable_columns)
49 49 @query.sort_criteria = sort_criteria.to_a
50 50
51 51 if @query.valid?
52 52 case params[:format]
53 53 when 'csv', 'pdf'
54 54 @limit = Setting.issues_export_limit.to_i
55 55 if params[:columns] == 'all'
56 56 @query.column_names = @query.available_inline_columns.map(&:name)
57 57 end
58 58 when 'atom'
59 59 @limit = Setting.feeds_limit.to_i
60 60 when 'xml', 'json'
61 61 @offset, @limit = api_offset_and_limit
62 62 @query.column_names = %w(author)
63 63 else
64 64 @limit = per_page_option
65 65 end
66 66
67 67 @issue_count = @query.issue_count
68 68 @issue_pages = Paginator.new @issue_count, @limit, params['page']
69 69 @offset ||= @issue_pages.offset
70 70 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
71 71 :order => sort_clause,
72 72 :offset => @offset,
73 73 :limit => @limit)
74 74 @issue_count_by_group = @query.issue_count_by_group
75 75
76 76 respond_to do |format|
77 77 format.html { render :template => 'issues/index', :layout => !request.xhr? }
78 78 format.api {
79 79 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
80 80 }
81 81 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
82 82 format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
83 83 format.pdf { send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf' }
84 84 end
85 85 else
86 86 respond_to do |format|
87 87 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
88 88 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
89 89 format.api { render_validation_errors(@query) }
90 90 end
91 91 end
92 92 rescue ActiveRecord::RecordNotFound
93 93 render_404
94 94 end
95 95
96 96 def show
97 97 @journals = @issue.journals.includes(:user, :details).
98 98 references(:user, :details).
99 99 reorder("#{Journal.table_name}.id ASC").to_a
100 100 @journals.each_with_index {|j,i| j.indice = i+1}
101 101 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
102 102 Journal.preload_journals_details_custom_fields(@journals)
103 103 @journals.select! {|journal| journal.notes? || journal.visible_details.any?}
104 104 @journals.reverse! if User.current.wants_comments_in_reverse_order?
105 105
106 106 @changesets = @issue.changesets.visible.to_a
107 107 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
108 108
109 109 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
110 110 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
111 111 @priorities = IssuePriority.active
112 112 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
113 113 @relation = IssueRelation.new
114 114
115 115 respond_to do |format|
116 116 format.html {
117 117 retrieve_previous_and_next_issue_ids
118 118 render :template => 'issues/show'
119 119 }
120 120 format.api
121 121 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
122 122 format.pdf {
123 123 send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf"
124 124 }
125 125 end
126 126 end
127 127
128 128 def new
129 129 respond_to do |format|
130 130 format.html { render :action => 'new', :layout => !request.xhr? }
131 131 format.js
132 132 end
133 133 end
134 134
135 135 def create
136 unless User.current.allowed_to?(:add_issues, @issue.project)
136 unless User.current.allowed_to?(:add_issues, @issue.project, :global => true)
137 137 raise ::Unauthorized
138 138 end
139 139 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
140 140 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
141 141 if @issue.save
142 142 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
143 143 respond_to do |format|
144 144 format.html {
145 145 render_attachment_warning_if_needed(@issue)
146 146 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
147 147 redirect_after_create
148 148 }
149 149 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
150 150 end
151 151 return
152 152 else
153 153 respond_to do |format|
154 format.html { render :action => 'new' }
154 format.html {
155 if @issue.project.nil?
156 render_error :status => 422
157 else
158 render :action => 'new'
159 end
160 }
155 161 format.api { render_validation_errors(@issue) }
156 162 end
157 163 end
158 164 end
159 165
160 166 def edit
161 167 return unless update_issue_from_params
162 168
163 169 respond_to do |format|
164 170 format.html { }
165 171 format.js
166 172 end
167 173 end
168 174
169 175 def update
170 176 return unless update_issue_from_params
171 177 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
172 178 saved = false
173 179 begin
174 180 saved = save_issue_with_child_records
175 181 rescue ActiveRecord::StaleObjectError
176 182 @conflict = true
177 183 if params[:last_journal_id]
178 184 @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
179 185 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
180 186 end
181 187 end
182 188
183 189 if saved
184 190 render_attachment_warning_if_needed(@issue)
185 191 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
186 192
187 193 respond_to do |format|
188 194 format.html { redirect_back_or_default issue_path(@issue) }
189 195 format.api { render_api_ok }
190 196 end
191 197 else
192 198 respond_to do |format|
193 199 format.html { render :action => 'edit' }
194 200 format.api { render_validation_errors(@issue) }
195 201 end
196 202 end
197 203 end
198 204
199 205 # Bulk edit/copy a set of issues
200 206 def bulk_edit
201 207 @issues.sort!
202 208 @copy = params[:copy].present?
203 209 @notes = params[:notes]
204 210
205 211 if @copy
206 212 unless User.current.allowed_to?(:copy_issues, @projects)
207 213 raise ::Unauthorized
208 214 end
209 215 end
210 216
211 217 @allowed_projects = Issue.allowed_target_projects
212 218 if params[:issue]
213 219 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
214 220 if @target_project
215 221 target_projects = [@target_project]
216 222 end
217 223 end
218 224 target_projects ||= @projects
219 225
220 226 if @copy
221 227 # Copied issues will get their default statuses
222 228 @available_statuses = []
223 229 else
224 230 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
225 231 end
226 232 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields.visible}.reduce(:&)
227 233 @assignables = target_projects.map(&:assignable_users).reduce(:&)
228 234 @trackers = target_projects.map(&:trackers).reduce(:&)
229 235 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
230 236 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
231 237 if @copy
232 238 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
233 239 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
234 240 end
235 241
236 242 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
237 243
238 244 @issue_params = params[:issue] || {}
239 245 @issue_params[:custom_field_values] ||= {}
240 246 end
241 247
242 248 def bulk_update
243 249 @issues.sort!
244 250 @copy = params[:copy].present?
245 251 attributes = parse_params_for_bulk_issue_attributes(params)
246 252
247 253 if @copy
248 254 unless User.current.allowed_to?(:copy_issues, @projects)
249 255 raise ::Unauthorized
250 256 end
251 257 target_projects = @projects
252 258 if attributes['project_id'].present?
253 259 target_projects = Project.where(:id => attributes['project_id']).to_a
254 260 end
255 261 unless User.current.allowed_to?(:add_issues, target_projects)
256 262 raise ::Unauthorized
257 263 end
258 264 end
259 265
260 266 unsaved_issues = []
261 267 saved_issues = []
262 268
263 269 if @copy && params[:copy_subtasks].present?
264 270 # Descendant issues will be copied with the parent task
265 271 # Don't copy them twice
266 272 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
267 273 end
268 274
269 275 @issues.each do |orig_issue|
270 276 orig_issue.reload
271 277 if @copy
272 278 issue = orig_issue.copy({},
273 279 :attachments => params[:copy_attachments].present?,
274 280 :subtasks => params[:copy_subtasks].present?,
275 281 :link => link_copy?(params[:link_copy])
276 282 )
277 283 else
278 284 issue = orig_issue
279 285 end
280 286 journal = issue.init_journal(User.current, params[:notes])
281 287 issue.safe_attributes = attributes
282 288 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
283 289 if issue.save
284 290 saved_issues << issue
285 291 else
286 292 unsaved_issues << orig_issue
287 293 end
288 294 end
289 295
290 296 if unsaved_issues.empty?
291 297 flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
292 298 if params[:follow]
293 299 if @issues.size == 1 && saved_issues.size == 1
294 300 redirect_to issue_path(saved_issues.first)
295 301 elsif saved_issues.map(&:project).uniq.size == 1
296 302 redirect_to project_issues_path(saved_issues.map(&:project).first)
297 303 end
298 304 else
299 305 redirect_back_or_default _project_issues_path(@project)
300 306 end
301 307 else
302 308 @saved_issues = @issues
303 309 @unsaved_issues = unsaved_issues
304 310 @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
305 311 bulk_edit
306 312 render :action => 'bulk_edit'
307 313 end
308 314 end
309 315
310 316 def destroy
311 317 @hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f
312 318 if @hours > 0
313 319 case params[:todo]
314 320 when 'destroy'
315 321 # nothing to do
316 322 when 'nullify'
317 323 TimeEntry.where(['issue_id IN (?)', @issues]).update_all('issue_id = NULL')
318 324 when 'reassign'
319 325 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
320 326 if reassign_to.nil?
321 327 flash.now[:error] = l(:error_issue_not_found_in_project)
322 328 return
323 329 else
324 330 TimeEntry.where(['issue_id IN (?)', @issues]).
325 331 update_all("issue_id = #{reassign_to.id}")
326 332 end
327 333 else
328 334 # display the destroy form if it's a user request
329 335 return unless api_request?
330 336 end
331 337 end
332 338 @issues.each do |issue|
333 339 begin
334 340 issue.reload.destroy
335 341 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
336 342 # nothing to do, issue was already deleted (eg. by a parent)
337 343 end
338 344 end
339 345 respond_to do |format|
340 346 format.html { redirect_back_or_default _project_issues_path(@project) }
341 347 format.api { render_api_ok }
342 348 end
343 349 end
344 350
345 351 private
346 352
347 353 def retrieve_previous_and_next_issue_ids
348 354 retrieve_query_from_session
349 355 if @query
350 356 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
351 357 sort_update(@query.sortable_columns, 'issues_index_sort')
352 358 limit = 500
353 359 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
354 360 if (idx = issue_ids.index(@issue.id)) && idx < limit
355 361 if issue_ids.size < 500
356 362 @issue_position = idx + 1
357 363 @issue_count = issue_ids.size
358 364 end
359 365 @prev_issue_id = issue_ids[idx - 1] if idx > 0
360 366 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
361 367 end
362 368 end
363 369 end
364 370
365 371 # Used by #edit and #update to set some common instance variables
366 372 # from the params
367 373 def update_issue_from_params
368 374 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
369 375 if params[:time_entry]
370 376 @time_entry.attributes = params[:time_entry]
371 377 end
372 378
373 379 @issue.init_journal(User.current)
374 380
375 381 issue_attributes = params[:issue]
376 382 if issue_attributes && params[:conflict_resolution]
377 383 case params[:conflict_resolution]
378 384 when 'overwrite'
379 385 issue_attributes = issue_attributes.dup
380 386 issue_attributes.delete(:lock_version)
381 387 when 'add_notes'
382 388 issue_attributes = issue_attributes.slice(:notes)
383 389 when 'cancel'
384 390 redirect_to issue_path(@issue)
385 391 return false
386 392 end
387 393 end
388 394 @issue.safe_attributes = issue_attributes
389 395 @priorities = IssuePriority.active
390 396 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
391 397 true
392 398 end
393 399
394 400 # Used by #new and #create to build a new issue from the params
395 401 # The new issue will be copied from an existing one if copy_from parameter is given
396 402 def build_new_issue_from_params
397 403 @issue = Issue.new
398 404 if params[:copy_from]
399 405 begin
400 406 @issue.init_journal(User.current)
401 407 @copy_from = Issue.visible.find(params[:copy_from])
402 408 unless User.current.allowed_to?(:copy_issues, @copy_from.project)
403 409 raise ::Unauthorized
404 410 end
405 411 @link_copy = link_copy?(params[:link_copy]) || request.get?
406 412 @copy_attachments = params[:copy_attachments].present? || request.get?
407 413 @copy_subtasks = params[:copy_subtasks].present? || request.get?
408 414 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks, :link => @link_copy)
409 415 rescue ActiveRecord::RecordNotFound
410 416 render_404
411 417 return
412 418 end
413 419 end
414 420 @issue.project = @project
415 421 if request.get?
416 422 @issue.project ||= @issue.allowed_target_projects.first
417 423 end
418 424 @issue.author ||= User.current
419 425 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
420 426
421 427 if attrs = params[:issue].deep_dup
422 428 if params[:was_default_status] == attrs[:status_id]
423 429 attrs.delete(:status_id)
424 430 end
425 431 @issue.safe_attributes = attrs
426 432 end
427 433 if @issue.project
428 434 @issue.tracker ||= @issue.project.trackers.first
429 435 if @issue.tracker.nil?
430 436 render_error l(:error_no_tracker_in_project)
431 437 return false
432 438 end
433 439 if @issue.status.nil?
434 440 render_error l(:error_no_default_issue_status)
435 441 return false
436 442 end
437 443 end
438 444
439 445 @priorities = IssuePriority.active
440 446 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, @issue.new_record?)
441 447 end
442 448
443 449 def parse_params_for_bulk_issue_attributes(params)
444 450 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
445 451 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
446 452 if custom = attributes[:custom_field_values]
447 453 custom.reject! {|k,v| v.blank?}
448 454 custom.keys.each do |k|
449 455 if custom[k].is_a?(Array)
450 456 custom[k] << '' if custom[k].delete('__none__')
451 457 else
452 458 custom[k] = '' if custom[k] == '__none__'
453 459 end
454 460 end
455 461 end
456 462 attributes
457 463 end
458 464
459 465 # Saves @issue and a time_entry from the parameters
460 466 def save_issue_with_child_records
461 467 Issue.transaction do
462 468 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
463 469 time_entry = @time_entry || TimeEntry.new
464 470 time_entry.project = @issue.project
465 471 time_entry.issue = @issue
466 472 time_entry.user = User.current
467 473 time_entry.spent_on = User.current.today
468 474 time_entry.attributes = params[:time_entry]
469 475 @issue.time_entries << time_entry
470 476 end
471 477
472 478 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
473 479 if @issue.save
474 480 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
475 481 else
476 482 raise ActiveRecord::Rollback
477 483 end
478 484 end
479 485 end
480 486
481 487 # Returns true if the issue copy should be linked
482 488 # to the original issue
483 489 def link_copy?(param)
484 490 case Setting.link_copied_issue
485 491 when 'yes'
486 492 true
487 493 when 'no'
488 494 false
489 495 when 'ask'
490 496 param == '1'
491 497 end
492 498 end
493 499
494 500 # Redirects user after a successful issue creation
495 501 def redirect_after_create
496 502 if params[:continue]
497 503 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
498 504 if params[:project_id]
499 505 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
500 506 else
501 507 attrs.merge! :project_id => @issue.project_id
502 508 redirect_to new_issue_path(:issue => attrs)
503 509 end
504 510 else
505 511 redirect_to issue_path(@issue)
506 512 end
507 513 end
508 514 end
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,688 +1,693
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 20 class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
21 21 fixtures :projects,
22 22 :users,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :issues,
27 27 :issue_statuses,
28 28 :issue_relations,
29 29 :versions,
30 30 :trackers,
31 31 :projects_trackers,
32 32 :issue_categories,
33 33 :enabled_modules,
34 34 :enumerations,
35 35 :attachments,
36 36 :workflows,
37 37 :custom_fields,
38 38 :custom_values,
39 39 :custom_fields_projects,
40 40 :custom_fields_trackers,
41 41 :time_entries,
42 42 :journals,
43 43 :journal_details,
44 44 :queries,
45 45 :attachments
46 46
47 47 test "GET /issues.xml should contain metadata" do
48 48 get '/issues.xml'
49 49 assert_select 'issues[type=array][total_count=?][limit="25"][offset="0"]',
50 50 assigns(:issue_count).to_s
51 51 end
52 52
53 53 test "GET /issues.xml with nometa param should not contain metadata" do
54 54 get '/issues.xml?nometa=1'
55 55 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
56 56 end
57 57
58 58 test "GET /issues.xml with nometa header should not contain metadata" do
59 59 get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
60 60 assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
61 61 end
62 62
63 63 test "GET /issues.xml with offset and limit" do
64 64 get '/issues.xml?offset=2&limit=3'
65 65
66 66 assert_equal 3, assigns(:limit)
67 67 assert_equal 2, assigns(:offset)
68 68 assert_select 'issues issue', 3
69 69 end
70 70
71 71 test "GET /issues.xml with relations" do
72 72 get '/issues.xml?include=relations'
73 73
74 74 assert_response :success
75 75 assert_equal 'application/xml', @response.content_type
76 76
77 77 assert_select 'issue id', :text => '3' do
78 78 assert_select '~ relations relation', 1
79 79 assert_select '~ relations relation[id="2"][issue_id="2"][issue_to_id="3"][relation_type=relates]'
80 80 end
81 81
82 82 assert_select 'issue id', :text => '1' do
83 83 assert_select '~ relations'
84 84 assert_select '~ relations relation', 0
85 85 end
86 86 end
87 87
88 88 test "GET /issues.xml with invalid query params" do
89 89 get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
90 90
91 91 assert_response :unprocessable_entity
92 92 assert_equal 'application/xml', @response.content_type
93 93 assert_select 'errors error', :text => "Start date cannot be blank"
94 94 end
95 95
96 96 test "GET /issues.xml with custom field filter" do
97 97 get '/issues.xml',
98 98 {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='}, :v => {:cf_1 => ['MySQL']}}
99 99
100 100 expected_ids = Issue.visible.
101 101 joins(:custom_values).
102 102 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
103 103 assert expected_ids.any?
104 104
105 105 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
106 106 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
107 107 end
108 108 end
109 109
110 110 test "GET /issues.xml with custom field filter (shorthand method)" do
111 111 get '/issues.xml', {:cf_1 => 'MySQL'}
112 112
113 113 expected_ids = Issue.visible.
114 114 joins(:custom_values).
115 115 where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
116 116 assert expected_ids.any?
117 117
118 118 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
119 119 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
120 120 end
121 121 end
122 122
123 123 def test_index_should_include_issue_attributes
124 124 get '/issues.xml'
125 125 assert_select 'issues>issue>is_private', :text => 'false'
126 126 end
127 127
128 128 def test_index_should_allow_timestamp_filtering
129 129 Issue.delete_all
130 130 Issue.generate!(:subject => '1').update_column(:updated_on, Time.parse("2014-01-02T10:25:00Z"))
131 131 Issue.generate!(:subject => '2').update_column(:updated_on, Time.parse("2014-01-02T12:13:00Z"))
132 132
133 133 get '/issues.xml',
134 134 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '<='},
135 135 :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
136 136 assert_select 'issues>issue', :count => 1
137 137 assert_select 'issues>issue>subject', :text => '1'
138 138
139 139 get '/issues.xml',
140 140 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
141 141 :v => {:updated_on => ['2014-01-02T12:00:00Z']}}
142 142 assert_select 'issues>issue', :count => 1
143 143 assert_select 'issues>issue>subject', :text => '2'
144 144
145 145 get '/issues.xml',
146 146 {:set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
147 147 :v => {:updated_on => ['2014-01-02T08:00:00Z']}}
148 148 assert_select 'issues>issue', :count => 2
149 149 end
150 150
151 151 test "GET /issues.xml with filter" do
152 152 get '/issues.xml?status_id=5'
153 153
154 154 expected_ids = Issue.visible.where(:status_id => 5).map(&:id)
155 155 assert expected_ids.any?
156 156
157 157 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
158 158 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
159 159 end
160 160 end
161 161
162 162 test "GET /issues.json with filter" do
163 163 get '/issues.json?status_id=5'
164 164
165 165 json = ActiveSupport::JSON.decode(response.body)
166 166 status_ids_used = json['issues'].collect {|j| j['status']['id'] }
167 167 assert_equal 3, status_ids_used.length
168 168 assert status_ids_used.all? {|id| id == 5 }
169 169 end
170 170
171 171 test "GET /issues/:id.xml with journals" do
172 172 get '/issues/1.xml?include=journals'
173 173
174 174 assert_select 'issue journals[type=array]' do
175 175 assert_select 'journal[id="1"]' do
176 176 assert_select 'details[type=array]' do
177 177 assert_select 'detail[name=status_id]' do
178 178 assert_select 'old_value', :text => '1'
179 179 assert_select 'new_value', :text => '2'
180 180 end
181 181 end
182 182 end
183 183 end
184 184 end
185 185
186 186 test "GET /issues/:id.xml with journals should format timestamps in ISO 8601" do
187 187 get '/issues/1.xml?include=journals'
188 188
189 189 iso_date = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
190 190 assert_select 'issue>created_on', :text => iso_date
191 191 assert_select 'issue>updated_on', :text => iso_date
192 192 assert_select 'issue journal>created_on', :text => iso_date
193 193 end
194 194
195 195 test "GET /issues/:id.xml with custom fields" do
196 196 get '/issues/3.xml'
197 197
198 198 assert_select 'issue custom_fields[type=array]' do
199 199 assert_select 'custom_field[id="1"]' do
200 200 assert_select 'value', :text => 'MySQL'
201 201 end
202 202 end
203 203 assert_nothing_raised do
204 204 Hash.from_xml(response.body).to_xml
205 205 end
206 206 end
207 207
208 208 test "GET /issues/:id.xml with multi custom fields" do
209 209 field = CustomField.find(1)
210 210 field.update_attribute :multiple, true
211 211 issue = Issue.find(3)
212 212 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
213 213 issue.save!
214 214
215 215 get '/issues/3.xml'
216 216 assert_response :success
217 217
218 218 assert_select 'issue custom_fields[type=array]' do
219 219 assert_select 'custom_field[id="1"]' do
220 220 assert_select 'value[type=array] value', 2
221 221 end
222 222 end
223 223 xml = Hash.from_xml(response.body)
224 224 custom_fields = xml['issue']['custom_fields']
225 225 assert_kind_of Array, custom_fields
226 226 field = custom_fields.detect {|f| f['id'] == '1'}
227 227 assert_kind_of Hash, field
228 228 assert_equal ['MySQL', 'Oracle'], field['value'].sort
229 229 end
230 230
231 231 test "GET /issues/:id.json with multi custom fields" do
232 232 field = CustomField.find(1)
233 233 field.update_attribute :multiple, true
234 234 issue = Issue.find(3)
235 235 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
236 236 issue.save!
237 237
238 238 get '/issues/3.json'
239 239 assert_response :success
240 240
241 241 json = ActiveSupport::JSON.decode(response.body)
242 242 custom_fields = json['issue']['custom_fields']
243 243 assert_kind_of Array, custom_fields
244 244 field = custom_fields.detect {|f| f['id'] == 1}
245 245 assert_kind_of Hash, field
246 246 assert_equal ['MySQL', 'Oracle'], field['value'].sort
247 247 end
248 248
249 249 test "GET /issues/:id.xml with empty value for multi custom field" do
250 250 field = CustomField.find(1)
251 251 field.update_attribute :multiple, true
252 252 issue = Issue.find(3)
253 253 issue.custom_field_values = {1 => ['']}
254 254 issue.save!
255 255
256 256 get '/issues/3.xml'
257 257
258 258 assert_select 'issue custom_fields[type=array]' do
259 259 assert_select 'custom_field[id="1"]' do
260 260 assert_select 'value[type=array]:empty'
261 261 end
262 262 end
263 263 xml = Hash.from_xml(response.body)
264 264 custom_fields = xml['issue']['custom_fields']
265 265 assert_kind_of Array, custom_fields
266 266 field = custom_fields.detect {|f| f['id'] == '1'}
267 267 assert_kind_of Hash, field
268 268 assert_equal [], field['value']
269 269 end
270 270
271 271 test "GET /issues/:id.json with empty value for multi custom field" do
272 272 field = CustomField.find(1)
273 273 field.update_attribute :multiple, true
274 274 issue = Issue.find(3)
275 275 issue.custom_field_values = {1 => ['']}
276 276 issue.save!
277 277
278 278 get '/issues/3.json'
279 279 assert_response :success
280 280 json = ActiveSupport::JSON.decode(response.body)
281 281 custom_fields = json['issue']['custom_fields']
282 282 assert_kind_of Array, custom_fields
283 283 field = custom_fields.detect {|f| f['id'] == 1}
284 284 assert_kind_of Hash, field
285 285 assert_equal [], field['value'].sort
286 286 end
287 287
288 288 test "GET /issues/:id.xml with attachments" do
289 289 get '/issues/3.xml?include=attachments'
290 290
291 291 assert_select 'issue attachments[type=array]' do
292 292 assert_select 'attachment', 4
293 293 assert_select 'attachment id', :text => '1' do
294 294 assert_select '~ filename', :text => 'error281.txt'
295 295 assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/1/error281.txt'
296 296 end
297 297 end
298 298 end
299 299
300 300 test "GET /issues/:id.xml with subtasks" do
301 301 issue = Issue.generate_with_descendants!(:project_id => 1)
302 302 get "/issues/#{issue.id}.xml?include=children"
303 303
304 304 assert_select 'issue id', :text => issue.id.to_s do
305 305 assert_select '~ children[type=array] > issue', 2
306 306 assert_select '~ children[type=array] > issue > children', 1
307 307 end
308 308 end
309 309
310 310 test "GET /issues/:id.json with subtasks" do
311 311 issue = Issue.generate_with_descendants!(:project_id => 1)
312 312 get "/issues/#{issue.id}.json?include=children"
313 313
314 314 json = ActiveSupport::JSON.decode(response.body)
315 315 assert_equal 2, json['issue']['children'].size
316 316 assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
317 317 end
318 318
319 319 def test_show_should_include_issue_attributes
320 320 get '/issues/1.xml'
321 321 assert_select 'issue>is_private', :text => 'false'
322 322 end
323 323
324 324 test "GET /issues/:id.xml?include=watchers should include watchers" do
325 325 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
326 326
327 327 get '/issues/1.xml?include=watchers', {}, credentials('jsmith')
328 328
329 329 assert_response :ok
330 330 assert_equal 'application/xml', response.content_type
331 331 assert_select 'issue' do
332 332 assert_select 'watchers', Issue.find(1).watchers.count
333 333 assert_select 'watchers' do
334 334 assert_select 'user[id="3"]'
335 335 end
336 336 end
337 337 end
338 338
339 339 test "POST /issues.xml should create an issue with the attributes" do
340 340
341 341 payload = <<-XML
342 342 <?xml version="1.0" encoding="UTF-8" ?>
343 343 <issue>
344 344 <project_id>1</project_id>
345 345 <tracker_id>2</tracker_id>
346 346 <status_id>3</status_id>
347 347 <subject>API test</subject>
348 348 </issue>
349 349 XML
350 350
351 351 assert_difference('Issue.count') do
352 352 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
353 353 end
354 354 issue = Issue.order('id DESC').first
355 355 assert_equal 1, issue.project_id
356 356 assert_equal 2, issue.tracker_id
357 357 assert_equal 3, issue.status_id
358 358 assert_equal 'API test', issue.subject
359 359
360 360 assert_response :created
361 361 assert_equal 'application/xml', @response.content_type
362 362 assert_select 'issue > id', :text => issue.id.to_s
363 363 end
364 364
365 365 test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
366 366 assert_difference('Issue.count') do
367 367 post '/issues.xml',
368 368 {:issue => {:project_id => 1, :subject => 'Watchers',
369 369 :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
370 370 assert_response :created
371 371 end
372 372 issue = Issue.order('id desc').first
373 373 assert_equal 2, issue.watchers.size
374 374 assert_equal [1, 3], issue.watcher_user_ids.sort
375 375 end
376 376
377 377 test "POST /issues.xml with failure should return errors" do
378 378 assert_no_difference('Issue.count') do
379 379 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
380 380 end
381 381
382 382 assert_select 'errors error', :text => "Subject cannot be blank"
383 383 end
384 384
385 385 test "POST /issues.json should create an issue with the attributes" do
386 386
387 387 payload = <<-JSON
388 388 {
389 389 "issue": {
390 390 "project_id": "1",
391 391 "tracker_id": "2",
392 392 "status_id": "3",
393 393 "subject": "API test"
394 394 }
395 395 }
396 396 JSON
397 397
398 398 assert_difference('Issue.count') do
399 399 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
400 400 end
401 401
402 402 issue = Issue.order('id DESC').first
403 403 assert_equal 1, issue.project_id
404 404 assert_equal 2, issue.tracker_id
405 405 assert_equal 3, issue.status_id
406 406 assert_equal 'API test', issue.subject
407 407 end
408 408
409 409 test "POST /issues.json without tracker_id should accept custom fields" do
410 410 field = IssueCustomField.generate!(
411 411 :field_format => 'list',
412 412 :multiple => true,
413 413 :possible_values => ["V1", "V2", "V3"],
414 414 :default_value => "V2",
415 415 :is_for_all => true,
416 416 :trackers => Tracker.all.to_a
417 417 )
418 418
419 419 payload = <<-JSON
420 420 {
421 421 "issue": {
422 422 "project_id": "1",
423 423 "subject": "Multivalued custom field",
424 424 "custom_field_values":{"#{field.id}":["V1","V3"]}
425 425 }
426 426 }
427 427 JSON
428 428
429 429 assert_difference('Issue.count') do
430 430 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
431 431 end
432 432
433 433 assert_response :created
434 434 issue = Issue.order('id DESC').first
435 435 assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
436 436 end
437 437
438 438 test "POST /issues.json with failure should return errors" do
439 439 assert_no_difference('Issue.count') do
440 440 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
441 441 end
442 442
443 443 json = ActiveSupport::JSON.decode(response.body)
444 444 assert json['errors'].include?("Subject cannot be blank")
445 445 end
446 446
447 test "POST /issues.json with invalid project_id should respond with 422" do
448 post '/issues.json', {:issue => {:project_id => 999, :subject => "API"}}, credentials('jsmith')
449 assert_response 422
450 end
451
447 452 test "PUT /issues/:id.xml" do
448 453 assert_difference('Journal.count') do
449 454 put '/issues/6.xml',
450 455 {:issue => {:subject => 'API update', :notes => 'A new note'}},
451 456 credentials('jsmith')
452 457 end
453 458
454 459 issue = Issue.find(6)
455 460 assert_equal "API update", issue.subject
456 461 journal = Journal.last
457 462 assert_equal "A new note", journal.notes
458 463 end
459 464
460 465 test "PUT /issues/:id.xml with custom fields" do
461 466 put '/issues/3.xml',
462 467 {:issue => {:custom_fields => [
463 468 {'id' => '1', 'value' => 'PostgreSQL' },
464 469 {'id' => '2', 'value' => '150'}
465 470 ]}},
466 471 credentials('jsmith')
467 472
468 473 issue = Issue.find(3)
469 474 assert_equal '150', issue.custom_value_for(2).value
470 475 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
471 476 end
472 477
473 478 test "PUT /issues/:id.xml with multi custom fields" do
474 479 field = CustomField.find(1)
475 480 field.update_attribute :multiple, true
476 481
477 482 put '/issues/3.xml',
478 483 {:issue => {:custom_fields => [
479 484 {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
480 485 {'id' => '2', 'value' => '150'}
481 486 ]}},
482 487 credentials('jsmith')
483 488
484 489 issue = Issue.find(3)
485 490 assert_equal '150', issue.custom_value_for(2).value
486 491 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
487 492 end
488 493
489 494 test "PUT /issues/:id.xml with project change" do
490 495 put '/issues/3.xml',
491 496 {:issue => {:project_id => 2, :subject => 'Project changed'}},
492 497 credentials('jsmith')
493 498
494 499 issue = Issue.find(3)
495 500 assert_equal 2, issue.project_id
496 501 assert_equal 'Project changed', issue.subject
497 502 end
498 503
499 504 test "PUT /issues/:id.xml with failed update" do
500 505 put '/issues/6.xml', {:issue => {:subject => ''}}, credentials('jsmith')
501 506
502 507 assert_response :unprocessable_entity
503 508 assert_select 'errors error', :text => "Subject cannot be blank"
504 509 end
505 510
506 511 test "PUT /issues/:id.json" do
507 512 assert_difference('Journal.count') do
508 513 put '/issues/6.json',
509 514 {:issue => {:subject => 'API update', :notes => 'A new note'}},
510 515 credentials('jsmith')
511 516
512 517 assert_response :ok
513 518 assert_equal '', response.body
514 519 end
515 520
516 521 issue = Issue.find(6)
517 522 assert_equal "API update", issue.subject
518 523 journal = Journal.last
519 524 assert_equal "A new note", journal.notes
520 525 end
521 526
522 527 test "PUT /issues/:id.json with failed update" do
523 528 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
524 529
525 530 assert_response :unprocessable_entity
526 531 json = ActiveSupport::JSON.decode(response.body)
527 532 assert json['errors'].include?("Subject cannot be blank")
528 533 end
529 534
530 535 test "DELETE /issues/:id.xml" do
531 536 assert_difference('Issue.count', -1) do
532 537 delete '/issues/6.xml', {}, credentials('jsmith')
533 538
534 539 assert_response :ok
535 540 assert_equal '', response.body
536 541 end
537 542 assert_nil Issue.find_by_id(6)
538 543 end
539 544
540 545 test "DELETE /issues/:id.json" do
541 546 assert_difference('Issue.count', -1) do
542 547 delete '/issues/6.json', {}, credentials('jsmith')
543 548
544 549 assert_response :ok
545 550 assert_equal '', response.body
546 551 end
547 552 assert_nil Issue.find_by_id(6)
548 553 end
549 554
550 555 test "POST /issues/:id/watchers.xml should add watcher" do
551 556 assert_difference 'Watcher.count' do
552 557 post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')
553 558
554 559 assert_response :ok
555 560 assert_equal '', response.body
556 561 end
557 562 watcher = Watcher.order('id desc').first
558 563 assert_equal Issue.find(1), watcher.watchable
559 564 assert_equal User.find(3), watcher.user
560 565 end
561 566
562 567 test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
563 568 Watcher.create!(:user_id => 3, :watchable => Issue.find(1))
564 569
565 570 assert_difference 'Watcher.count', -1 do
566 571 delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')
567 572
568 573 assert_response :ok
569 574 assert_equal '', response.body
570 575 end
571 576 assert_equal false, Issue.find(1).watched_by?(User.find(3))
572 577 end
573 578
574 579 def test_create_issue_with_uploaded_file
575 580 token = xml_upload('test_create_with_upload', credentials('jsmith'))
576 581 attachment = Attachment.find_by_token(token)
577 582
578 583 # create the issue with the upload's token
579 584 assert_difference 'Issue.count' do
580 585 post '/issues.xml',
581 586 {:issue => {:project_id => 1, :subject => 'Uploaded file',
582 587 :uploads => [{:token => token, :filename => 'test.txt',
583 588 :content_type => 'text/plain'}]}},
584 589 credentials('jsmith')
585 590 assert_response :created
586 591 end
587 592 issue = Issue.order('id DESC').first
588 593 assert_equal 1, issue.attachments.count
589 594 assert_equal attachment, issue.attachments.first
590 595
591 596 attachment.reload
592 597 assert_equal 'test.txt', attachment.filename
593 598 assert_equal 'text/plain', attachment.content_type
594 599 assert_equal 'test_create_with_upload'.size, attachment.filesize
595 600 assert_equal 2, attachment.author_id
596 601
597 602 # get the issue with its attachments
598 603 get "/issues/#{issue.id}.xml", :include => 'attachments'
599 604 assert_response :success
600 605 xml = Hash.from_xml(response.body)
601 606 attachments = xml['issue']['attachments']
602 607 assert_kind_of Array, attachments
603 608 assert_equal 1, attachments.size
604 609 url = attachments.first['content_url']
605 610 assert_not_nil url
606 611
607 612 # download the attachment
608 613 get url
609 614 assert_response :success
610 615 assert_equal 'test_create_with_upload', response.body
611 616 end
612 617
613 618 def test_create_issue_with_multiple_uploaded_files_as_xml
614 619 token1 = xml_upload('File content 1', credentials('jsmith'))
615 620 token2 = xml_upload('File content 2', credentials('jsmith'))
616 621
617 622 payload = <<-XML
618 623 <?xml version="1.0" encoding="UTF-8" ?>
619 624 <issue>
620 625 <project_id>1</project_id>
621 626 <tracker_id>1</tracker_id>
622 627 <subject>Issue with multiple attachments</subject>
623 628 <uploads type="array">
624 629 <upload>
625 630 <token>#{token1}</token>
626 631 <filename>test1.txt</filename>
627 632 </upload>
628 633 <upload>
629 634 <token>#{token2}</token>
630 635 <filename>test1.txt</filename>
631 636 </upload>
632 637 </uploads>
633 638 </issue>
634 639 XML
635 640
636 641 assert_difference 'Issue.count' do
637 642 post '/issues.xml', payload, {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
638 643 assert_response :created
639 644 end
640 645 issue = Issue.order('id DESC').first
641 646 assert_equal 2, issue.attachments.count
642 647 end
643 648
644 649 def test_create_issue_with_multiple_uploaded_files_as_json
645 650 token1 = json_upload('File content 1', credentials('jsmith'))
646 651 token2 = json_upload('File content 2', credentials('jsmith'))
647 652
648 653 payload = <<-JSON
649 654 {
650 655 "issue": {
651 656 "project_id": "1",
652 657 "tracker_id": "1",
653 658 "subject": "Issue with multiple attachments",
654 659 "uploads": [
655 660 {"token": "#{token1}", "filename": "test1.txt"},
656 661 {"token": "#{token2}", "filename": "test2.txt"}
657 662 ]
658 663 }
659 664 }
660 665 JSON
661 666
662 667 assert_difference 'Issue.count' do
663 668 post '/issues.json', payload, {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
664 669 assert_response :created
665 670 end
666 671 issue = Issue.order('id DESC').first
667 672 assert_equal 2, issue.attachments.count
668 673 end
669 674
670 675 def test_update_issue_with_uploaded_file
671 676 token = xml_upload('test_upload_with_upload', credentials('jsmith'))
672 677 attachment = Attachment.find_by_token(token)
673 678
674 679 # update the issue with the upload's token
675 680 assert_difference 'Journal.count' do
676 681 put '/issues/1.xml',
677 682 {:issue => {:notes => 'Attachment added',
678 683 :uploads => [{:token => token, :filename => 'test.txt',
679 684 :content_type => 'text/plain'}]}},
680 685 credentials('jsmith')
681 686 assert_response :ok
682 687 assert_equal '', @response.body
683 688 end
684 689
685 690 issue = Issue.find(1)
686 691 assert_include attachment, issue.attachments
687 692 end
688 693 end
General Comments 0
You need to be logged in to leave comments. Login now