##// END OF EJS Templates
Fixed: bulk destroying parent and child issues raises a stale object error (#7920)....
Jean-Philippe Lang -
r5163:4e7835c68c7d
parent child
Show More
@@ -1,319 +1,325
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 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_key_auth :index, :show, :create, :update, :destroy
31 31
32 32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 33
34 34 helper :journals
35 35 helper :projects
36 36 include ProjectsHelper
37 37 helper :custom_fields
38 38 include CustomFieldsHelper
39 39 helper :issue_relations
40 40 include IssueRelationsHelper
41 41 helper :watchers
42 42 include WatchersHelper
43 43 helper :attachments
44 44 include AttachmentsHelper
45 45 helper :queries
46 46 include QueriesHelper
47 47 helper :repositories
48 48 include RepositoriesHelper
49 49 helper :sort
50 50 include SortHelper
51 51 include IssuesHelper
52 52 helper :timelog
53 53 helper :gantt
54 54 include Redmine::Export::PDF
55 55
56 56 verify :method => [:post, :delete],
57 57 :only => :destroy,
58 58 :render => { :nothing => true, :status => :method_not_allowed }
59 59
60 60 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
61 61 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
62 62 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
63 63
64 64 def index
65 65 retrieve_query
66 66 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
67 67 sort_update(@query.sortable_columns)
68 68
69 69 if @query.valid?
70 70 case params[:format]
71 71 when 'csv', 'pdf'
72 72 @limit = Setting.issues_export_limit.to_i
73 73 when 'atom'
74 74 @limit = Setting.feeds_limit.to_i
75 75 when 'xml', 'json'
76 76 @offset, @limit = api_offset_and_limit
77 77 else
78 78 @limit = per_page_option
79 79 end
80 80
81 81 @issue_count = @query.issue_count
82 82 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
83 83 @offset ||= @issue_pages.current.offset
84 84 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
85 85 :order => sort_clause,
86 86 :offset => @offset,
87 87 :limit => @limit)
88 88 @issue_count_by_group = @query.issue_count_by_group
89 89
90 90 respond_to do |format|
91 91 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
92 92 format.api
93 93 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
94 94 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
95 95 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
96 96 end
97 97 else
98 98 # Send html if the query is not valid
99 99 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
100 100 end
101 101 rescue ActiveRecord::RecordNotFound
102 102 render_404
103 103 end
104 104
105 105 def show
106 106 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
107 107 @journals.each_with_index {|j,i| j.indice = i+1}
108 108 @journals.reverse! if User.current.wants_comments_in_reverse_order?
109 109 @changesets = @issue.changesets.visible.all
110 110 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
111 111 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
112 112 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
113 113 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
114 114 @priorities = IssuePriority.all
115 115 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
116 116 respond_to do |format|
117 117 format.html { render :template => 'issues/show.rhtml' }
118 118 format.api
119 119 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
120 120 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
121 121 end
122 122 end
123 123
124 124 # Add a new issue
125 125 # The new issue will be created from an existing one if copy_from parameter is given
126 126 def new
127 127 respond_to do |format|
128 128 format.html { render :action => 'new', :layout => !request.xhr? }
129 129 format.js { render :partial => 'attributes' }
130 130 end
131 131 end
132 132
133 133 def create
134 134 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
135 135 if @issue.save
136 136 attachments = Attachment.attach_files(@issue, params[:attachments])
137 137 render_attachment_warning_if_needed(@issue)
138 138 flash[:notice] = l(:notice_successful_create)
139 139 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
140 140 respond_to do |format|
141 141 format.html {
142 142 redirect_to(params[:continue] ? { :action => 'new', :project_id => @project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
143 143 { :action => 'show', :id => @issue })
144 144 }
145 145 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
146 146 end
147 147 return
148 148 else
149 149 respond_to do |format|
150 150 format.html { render :action => 'new' }
151 151 format.api { render_validation_errors(@issue) }
152 152 end
153 153 end
154 154 end
155 155
156 156 def edit
157 157 update_issue_from_params
158 158
159 159 @journal = @issue.current_journal
160 160
161 161 respond_to do |format|
162 162 format.html { }
163 163 format.xml { }
164 164 end
165 165 end
166 166
167 167 def update
168 168 update_issue_from_params
169 169
170 170 if @issue.save_issue_with_child_records(params, @time_entry)
171 171 render_attachment_warning_if_needed(@issue)
172 172 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
173 173
174 174 respond_to do |format|
175 175 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
176 176 format.api { head :ok }
177 177 end
178 178 else
179 179 render_attachment_warning_if_needed(@issue)
180 180 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
181 181 @journal = @issue.current_journal
182 182
183 183 respond_to do |format|
184 184 format.html { render :action => 'edit' }
185 185 format.api { render_validation_errors(@issue) }
186 186 end
187 187 end
188 188 end
189 189
190 190 # Bulk edit a set of issues
191 191 def bulk_edit
192 192 @issues.sort!
193 193 @available_statuses = @projects.map{|p|Workflow.available_statuses(p)}.inject{|memo,w|memo & w}
194 194 @custom_fields = @projects.map{|p|p.all_issue_custom_fields}.inject{|memo,c|memo & c}
195 195 @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a}
196 196 @trackers = @projects.map(&:trackers).inject{|memo,t| memo & t}
197 197 end
198 198
199 199 def bulk_update
200 200 @issues.sort!
201 201 attributes = parse_params_for_bulk_issue_attributes(params)
202 202
203 203 unsaved_issue_ids = []
204 204 @issues.each do |issue|
205 205 issue.reload
206 206 journal = issue.init_journal(User.current, params[:notes])
207 207 issue.safe_attributes = attributes
208 208 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
209 209 unless issue.save
210 210 # Keep unsaved issue ids to display them in flash error
211 211 unsaved_issue_ids << issue.id
212 212 end
213 213 end
214 214 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
215 215 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
216 216 end
217 217
218 218 def destroy
219 219 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
220 220 if @hours > 0
221 221 case params[:todo]
222 222 when 'destroy'
223 223 # nothing to do
224 224 when 'nullify'
225 225 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
226 226 when 'reassign'
227 227 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
228 228 if reassign_to.nil?
229 229 flash.now[:error] = l(:error_issue_not_found_in_project)
230 230 return
231 231 else
232 232 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
233 233 end
234 234 else
235 235 # display the destroy form if it's a user request
236 236 return unless api_request?
237 237 end
238 238 end
239 @issues.each(&:destroy)
239 @issues.each do |issue|
240 begin
241 issue.reload.destroy
242 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
243 # nothing to do, issue was already deleted (eg. by a parent)
244 end
245 end
240 246 respond_to do |format|
241 247 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
242 248 format.api { head :ok }
243 249 end
244 250 end
245 251
246 252 private
247 253 def find_issue
248 254 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
249 255 @project = @issue.project
250 256 rescue ActiveRecord::RecordNotFound
251 257 render_404
252 258 end
253 259
254 260 def find_project
255 261 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
256 262 @project = Project.find(project_id)
257 263 rescue ActiveRecord::RecordNotFound
258 264 render_404
259 265 end
260 266
261 267 # Used by #edit and #update to set some common instance variables
262 268 # from the params
263 269 # TODO: Refactor, not everything in here is needed by #edit
264 270 def update_issue_from_params
265 271 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
266 272 @priorities = IssuePriority.all
267 273 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
268 274 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
269 275 @time_entry.attributes = params[:time_entry]
270 276
271 277 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
272 278 @issue.init_journal(User.current, @notes)
273 279 @issue.safe_attributes = params[:issue]
274 280 end
275 281
276 282 # TODO: Refactor, lots of extra code in here
277 283 # TODO: Changing tracker on an existing issue should not trigger this
278 284 def build_new_issue_from_params
279 285 if params[:id].blank?
280 286 @issue = Issue.new
281 287 @issue.copy_from(params[:copy_from]) if params[:copy_from]
282 288 @issue.project = @project
283 289 else
284 290 @issue = @project.issues.visible.find(params[:id])
285 291 end
286 292
287 293 @issue.project = @project
288 294 # Tracker must be set before custom field values
289 295 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
290 296 if @issue.tracker.nil?
291 297 render_error l(:error_no_tracker_in_project)
292 298 return false
293 299 end
294 300 @issue.start_date ||= Date.today
295 301 if params[:issue].is_a?(Hash)
296 302 @issue.safe_attributes = params[:issue]
297 303 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
298 304 @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
299 305 end
300 306 end
301 307 @issue.author = User.current
302 308 @priorities = IssuePriority.all
303 309 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
304 310 end
305 311
306 312 def check_for_default_issue_status
307 313 if IssueStatus.default.nil?
308 314 render_error l(:error_no_default_issue_status)
309 315 return false
310 316 end
311 317 end
312 318
313 319 def parse_params_for_bulk_issue_attributes(params)
314 320 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
315 321 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
316 322 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
317 323 attributes
318 324 end
319 325 end
@@ -1,1332 +1,1344
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 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 # Re-raise errors caught by the controller.
22 22 class IssuesController; def rescue_action(e) raise e end; end
23 23
24 24 class IssuesControllerTest < ActionController::TestCase
25 25 fixtures :projects,
26 26 :users,
27 27 :roles,
28 28 :members,
29 29 :member_roles,
30 30 :issues,
31 31 :issue_statuses,
32 32 :versions,
33 33 :trackers,
34 34 :projects_trackers,
35 35 :issue_categories,
36 36 :enabled_modules,
37 37 :enumerations,
38 38 :attachments,
39 39 :workflows,
40 40 :custom_fields,
41 41 :custom_values,
42 42 :custom_fields_projects,
43 43 :custom_fields_trackers,
44 44 :time_entries,
45 45 :journals,
46 46 :journal_details,
47 47 :queries
48 48
49 49 def setup
50 50 @controller = IssuesController.new
51 51 @request = ActionController::TestRequest.new
52 52 @response = ActionController::TestResponse.new
53 53 User.current = nil
54 54 end
55 55
56 56 def test_index
57 57 Setting.default_language = 'en'
58 58
59 59 get :index
60 60 assert_response :success
61 61 assert_template 'index.rhtml'
62 62 assert_not_nil assigns(:issues)
63 63 assert_nil assigns(:project)
64 64 assert_tag :tag => 'a', :content => /Can't print recipes/
65 65 assert_tag :tag => 'a', :content => /Subproject issue/
66 66 # private projects hidden
67 67 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
68 68 assert_no_tag :tag => 'a', :content => /Issue on project 2/
69 69 # project column
70 70 assert_tag :tag => 'th', :content => /Project/
71 71 end
72 72
73 73 def test_index_should_not_list_issues_when_module_disabled
74 74 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
75 75 get :index
76 76 assert_response :success
77 77 assert_template 'index.rhtml'
78 78 assert_not_nil assigns(:issues)
79 79 assert_nil assigns(:project)
80 80 assert_no_tag :tag => 'a', :content => /Can't print recipes/
81 81 assert_tag :tag => 'a', :content => /Subproject issue/
82 82 end
83 83
84 84 def test_index_should_not_list_issues_when_module_disabled
85 85 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
86 86 get :index
87 87 assert_response :success
88 88 assert_template 'index.rhtml'
89 89 assert_not_nil assigns(:issues)
90 90 assert_nil assigns(:project)
91 91 assert_no_tag :tag => 'a', :content => /Can't print recipes/
92 92 assert_tag :tag => 'a', :content => /Subproject issue/
93 93 end
94 94
95 95 def test_index_with_project
96 96 Setting.display_subprojects_issues = 0
97 97 get :index, :project_id => 1
98 98 assert_response :success
99 99 assert_template 'index.rhtml'
100 100 assert_not_nil assigns(:issues)
101 101 assert_tag :tag => 'a', :content => /Can't print recipes/
102 102 assert_no_tag :tag => 'a', :content => /Subproject issue/
103 103 end
104 104
105 105 def test_index_with_project_and_subprojects
106 106 Setting.display_subprojects_issues = 1
107 107 get :index, :project_id => 1
108 108 assert_response :success
109 109 assert_template 'index.rhtml'
110 110 assert_not_nil assigns(:issues)
111 111 assert_tag :tag => 'a', :content => /Can't print recipes/
112 112 assert_tag :tag => 'a', :content => /Subproject issue/
113 113 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
114 114 end
115 115
116 116 def test_index_with_project_and_subprojects_should_show_private_subprojects
117 117 @request.session[:user_id] = 2
118 118 Setting.display_subprojects_issues = 1
119 119 get :index, :project_id => 1
120 120 assert_response :success
121 121 assert_template 'index.rhtml'
122 122 assert_not_nil assigns(:issues)
123 123 assert_tag :tag => 'a', :content => /Can't print recipes/
124 124 assert_tag :tag => 'a', :content => /Subproject issue/
125 125 assert_tag :tag => 'a', :content => /Issue of a private subproject/
126 126 end
127 127
128 128 def test_index_with_project_and_default_filter
129 129 get :index, :project_id => 1, :set_filter => 1
130 130 assert_response :success
131 131 assert_template 'index.rhtml'
132 132 assert_not_nil assigns(:issues)
133 133
134 134 query = assigns(:query)
135 135 assert_not_nil query
136 136 # default filter
137 137 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
138 138 end
139 139
140 140 def test_index_with_project_and_filter
141 141 get :index, :project_id => 1, :set_filter => 1,
142 142 :f => ['tracker_id'],
143 143 :op => {'tracker_id' => '='},
144 144 :v => {'tracker_id' => ['1']}
145 145 assert_response :success
146 146 assert_template 'index.rhtml'
147 147 assert_not_nil assigns(:issues)
148 148
149 149 query = assigns(:query)
150 150 assert_not_nil query
151 151 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
152 152 end
153 153
154 154 def test_index_with_project_and_empty_filters
155 155 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
156 156 assert_response :success
157 157 assert_template 'index.rhtml'
158 158 assert_not_nil assigns(:issues)
159 159
160 160 query = assigns(:query)
161 161 assert_not_nil query
162 162 # no filter
163 163 assert_equal({}, query.filters)
164 164 end
165 165
166 166 def test_index_with_query
167 167 get :index, :project_id => 1, :query_id => 5
168 168 assert_response :success
169 169 assert_template 'index.rhtml'
170 170 assert_not_nil assigns(:issues)
171 171 assert_nil assigns(:issue_count_by_group)
172 172 end
173 173
174 174 def test_index_with_query_grouped_by_tracker
175 175 get :index, :project_id => 1, :query_id => 6
176 176 assert_response :success
177 177 assert_template 'index.rhtml'
178 178 assert_not_nil assigns(:issues)
179 179 assert_not_nil assigns(:issue_count_by_group)
180 180 end
181 181
182 182 def test_index_with_query_grouped_by_list_custom_field
183 183 get :index, :project_id => 1, :query_id => 9
184 184 assert_response :success
185 185 assert_template 'index.rhtml'
186 186 assert_not_nil assigns(:issues)
187 187 assert_not_nil assigns(:issue_count_by_group)
188 188 end
189 189
190 190 def test_index_sort_by_field_not_included_in_columns
191 191 Setting.issue_list_default_columns = %w(subject author)
192 192 get :index, :sort => 'tracker'
193 193 end
194 194
195 195 def test_index_csv_with_project
196 196 Setting.default_language = 'en'
197 197
198 198 get :index, :format => 'csv'
199 199 assert_response :success
200 200 assert_not_nil assigns(:issues)
201 201 assert_equal 'text/csv', @response.content_type
202 202 assert @response.body.starts_with?("#,")
203 203
204 204 get :index, :project_id => 1, :format => 'csv'
205 205 assert_response :success
206 206 assert_not_nil assigns(:issues)
207 207 assert_equal 'text/csv', @response.content_type
208 208 end
209 209
210 210 def test_index_pdf
211 211 get :index, :format => 'pdf'
212 212 assert_response :success
213 213 assert_not_nil assigns(:issues)
214 214 assert_equal 'application/pdf', @response.content_type
215 215
216 216 get :index, :project_id => 1, :format => 'pdf'
217 217 assert_response :success
218 218 assert_not_nil assigns(:issues)
219 219 assert_equal 'application/pdf', @response.content_type
220 220
221 221 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
222 222 assert_response :success
223 223 assert_not_nil assigns(:issues)
224 224 assert_equal 'application/pdf', @response.content_type
225 225 end
226 226
227 227 def test_index_pdf_with_query_grouped_by_list_custom_field
228 228 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
229 229 assert_response :success
230 230 assert_not_nil assigns(:issues)
231 231 assert_not_nil assigns(:issue_count_by_group)
232 232 assert_equal 'application/pdf', @response.content_type
233 233 end
234 234
235 235 def test_index_sort
236 236 get :index, :sort => 'tracker,id:desc'
237 237 assert_response :success
238 238
239 239 sort_params = @request.session['issues_index_sort']
240 240 assert sort_params.is_a?(String)
241 241 assert_equal 'tracker,id:desc', sort_params
242 242
243 243 issues = assigns(:issues)
244 244 assert_not_nil issues
245 245 assert !issues.empty?
246 246 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
247 247 end
248 248
249 249 def test_index_with_columns
250 250 columns = ['tracker', 'subject', 'assigned_to']
251 251 get :index, :set_filter => 1, :query => { 'column_names' => columns}
252 252 assert_response :success
253 253
254 254 # query should use specified columns
255 255 query = assigns(:query)
256 256 assert_kind_of Query, query
257 257 assert_equal columns, query.column_names.map(&:to_s)
258 258
259 259 # columns should be stored in session
260 260 assert_kind_of Hash, session[:query]
261 261 assert_kind_of Array, session[:query][:column_names]
262 262 assert_equal columns, session[:query][:column_names].map(&:to_s)
263 263 end
264 264
265 265 def test_show_by_anonymous
266 266 get :show, :id => 1
267 267 assert_response :success
268 268 assert_template 'show.rhtml'
269 269 assert_not_nil assigns(:issue)
270 270 assert_equal Issue.find(1), assigns(:issue)
271 271
272 272 # anonymous role is allowed to add a note
273 273 assert_tag :tag => 'form',
274 274 :descendant => { :tag => 'fieldset',
275 275 :child => { :tag => 'legend',
276 276 :content => /Notes/ } }
277 277 end
278 278
279 279 def test_show_by_manager
280 280 @request.session[:user_id] = 2
281 281 get :show, :id => 1
282 282 assert_response :success
283 283
284 284 assert_tag :tag => 'a',
285 285 :content => /Quote/
286 286
287 287 assert_tag :tag => 'form',
288 288 :descendant => { :tag => 'fieldset',
289 289 :child => { :tag => 'legend',
290 290 :content => /Change properties/ } },
291 291 :descendant => { :tag => 'fieldset',
292 292 :child => { :tag => 'legend',
293 293 :content => /Log time/ } },
294 294 :descendant => { :tag => 'fieldset',
295 295 :child => { :tag => 'legend',
296 296 :content => /Notes/ } }
297 297 end
298 298
299 299 def test_show_should_deny_anonymous_access_without_permission
300 300 Role.anonymous.remove_permission!(:view_issues)
301 301 get :show, :id => 1
302 302 assert_response :redirect
303 303 end
304 304
305 305 def test_show_should_deny_non_member_access_without_permission
306 306 Role.non_member.remove_permission!(:view_issues)
307 307 @request.session[:user_id] = 9
308 308 get :show, :id => 1
309 309 assert_response 403
310 310 end
311 311
312 312 def test_show_should_deny_member_access_without_permission
313 313 Role.find(1).remove_permission!(:view_issues)
314 314 @request.session[:user_id] = 2
315 315 get :show, :id => 1
316 316 assert_response 403
317 317 end
318 318
319 319 def test_show_should_not_disclose_relations_to_invisible_issues
320 320 Setting.cross_project_issue_relations = '1'
321 321 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
322 322 # Relation to a private project issue
323 323 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
324 324
325 325 get :show, :id => 1
326 326 assert_response :success
327 327
328 328 assert_tag :div, :attributes => { :id => 'relations' },
329 329 :descendant => { :tag => 'a', :content => /#2$/ }
330 330 assert_no_tag :div, :attributes => { :id => 'relations' },
331 331 :descendant => { :tag => 'a', :content => /#4$/ }
332 332 end
333 333
334 334 def test_show_atom
335 335 get :show, :id => 2, :format => 'atom'
336 336 assert_response :success
337 337 assert_template 'journals/index.rxml'
338 338 # Inline image
339 339 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
340 340 end
341 341
342 342 def test_show_export_to_pdf
343 343 get :show, :id => 3, :format => 'pdf'
344 344 assert_response :success
345 345 assert_equal 'application/pdf', @response.content_type
346 346 assert @response.body.starts_with?('%PDF')
347 347 assert_not_nil assigns(:issue)
348 348 end
349 349
350 350 def test_get_new
351 351 @request.session[:user_id] = 2
352 352 get :new, :project_id => 1, :tracker_id => 1
353 353 assert_response :success
354 354 assert_template 'new'
355 355
356 356 assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
357 357 :value => 'Default string' }
358 358 end
359 359
360 360 def test_get_new_without_tracker_id
361 361 @request.session[:user_id] = 2
362 362 get :new, :project_id => 1
363 363 assert_response :success
364 364 assert_template 'new'
365 365
366 366 issue = assigns(:issue)
367 367 assert_not_nil issue
368 368 assert_equal Project.find(1).trackers.first, issue.tracker
369 369 end
370 370
371 371 def test_get_new_with_no_default_status_should_display_an_error
372 372 @request.session[:user_id] = 2
373 373 IssueStatus.delete_all
374 374
375 375 get :new, :project_id => 1
376 376 assert_response 500
377 377 assert_error_tag :content => /No default issue/
378 378 end
379 379
380 380 def test_get_new_with_no_tracker_should_display_an_error
381 381 @request.session[:user_id] = 2
382 382 Tracker.delete_all
383 383
384 384 get :new, :project_id => 1
385 385 assert_response 500
386 386 assert_error_tag :content => /No tracker/
387 387 end
388 388
389 389 def test_update_new_form
390 390 @request.session[:user_id] = 2
391 391 xhr :post, :new, :project_id => 1,
392 392 :issue => {:tracker_id => 2,
393 393 :subject => 'This is the test_new issue',
394 394 :description => 'This is the description',
395 395 :priority_id => 5}
396 396 assert_response :success
397 397 assert_template 'attributes'
398 398
399 399 issue = assigns(:issue)
400 400 assert_kind_of Issue, issue
401 401 assert_equal 1, issue.project_id
402 402 assert_equal 2, issue.tracker_id
403 403 assert_equal 'This is the test_new issue', issue.subject
404 404 end
405 405
406 406 def test_post_create
407 407 @request.session[:user_id] = 2
408 408 assert_difference 'Issue.count' do
409 409 post :create, :project_id => 1,
410 410 :issue => {:tracker_id => 3,
411 411 :status_id => 2,
412 412 :subject => 'This is the test_new issue',
413 413 :description => 'This is the description',
414 414 :priority_id => 5,
415 415 :start_date => '2010-11-07',
416 416 :estimated_hours => '',
417 417 :custom_field_values => {'2' => 'Value for field 2'}}
418 418 end
419 419 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
420 420
421 421 issue = Issue.find_by_subject('This is the test_new issue')
422 422 assert_not_nil issue
423 423 assert_equal 2, issue.author_id
424 424 assert_equal 3, issue.tracker_id
425 425 assert_equal 2, issue.status_id
426 426 assert_equal Date.parse('2010-11-07'), issue.start_date
427 427 assert_nil issue.estimated_hours
428 428 v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
429 429 assert_not_nil v
430 430 assert_equal 'Value for field 2', v.value
431 431 end
432 432
433 433 def test_post_create_without_start_date
434 434 @request.session[:user_id] = 2
435 435 assert_difference 'Issue.count' do
436 436 post :create, :project_id => 1,
437 437 :issue => {:tracker_id => 3,
438 438 :status_id => 2,
439 439 :subject => 'This is the test_new issue',
440 440 :description => 'This is the description',
441 441 :priority_id => 5,
442 442 :start_date => '',
443 443 :estimated_hours => '',
444 444 :custom_field_values => {'2' => 'Value for field 2'}}
445 445 end
446 446 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
447 447
448 448 issue = Issue.find_by_subject('This is the test_new issue')
449 449 assert_not_nil issue
450 450 assert_nil issue.start_date
451 451 end
452 452
453 453 def test_post_create_and_continue
454 454 @request.session[:user_id] = 2
455 455 post :create, :project_id => 1,
456 456 :issue => {:tracker_id => 3,
457 457 :subject => 'This is first issue',
458 458 :priority_id => 5},
459 459 :continue => ''
460 460 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook',
461 461 :issue => {:tracker_id => 3}
462 462 end
463 463
464 464 def test_post_create_without_custom_fields_param
465 465 @request.session[:user_id] = 2
466 466 assert_difference 'Issue.count' do
467 467 post :create, :project_id => 1,
468 468 :issue => {:tracker_id => 1,
469 469 :subject => 'This is the test_new issue',
470 470 :description => 'This is the description',
471 471 :priority_id => 5}
472 472 end
473 473 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
474 474 end
475 475
476 476 def test_post_create_with_required_custom_field_and_without_custom_fields_param
477 477 field = IssueCustomField.find_by_name('Database')
478 478 field.update_attribute(:is_required, true)
479 479
480 480 @request.session[:user_id] = 2
481 481 post :create, :project_id => 1,
482 482 :issue => {:tracker_id => 1,
483 483 :subject => 'This is the test_new issue',
484 484 :description => 'This is the description',
485 485 :priority_id => 5}
486 486 assert_response :success
487 487 assert_template 'new'
488 488 issue = assigns(:issue)
489 489 assert_not_nil issue
490 490 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
491 491 end
492 492
493 493 def test_post_create_with_watchers
494 494 @request.session[:user_id] = 2
495 495 ActionMailer::Base.deliveries.clear
496 496
497 497 assert_difference 'Watcher.count', 2 do
498 498 post :create, :project_id => 1,
499 499 :issue => {:tracker_id => 1,
500 500 :subject => 'This is a new issue with watchers',
501 501 :description => 'This is the description',
502 502 :priority_id => 5,
503 503 :watcher_user_ids => ['2', '3']}
504 504 end
505 505 issue = Issue.find_by_subject('This is a new issue with watchers')
506 506 assert_not_nil issue
507 507 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
508 508
509 509 # Watchers added
510 510 assert_equal [2, 3], issue.watcher_user_ids.sort
511 511 assert issue.watched_by?(User.find(3))
512 512 # Watchers notified
513 513 mail = ActionMailer::Base.deliveries.last
514 514 assert_kind_of TMail::Mail, mail
515 515 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
516 516 end
517 517
518 518 def test_post_create_subissue
519 519 @request.session[:user_id] = 2
520 520
521 521 assert_difference 'Issue.count' do
522 522 post :create, :project_id => 1,
523 523 :issue => {:tracker_id => 1,
524 524 :subject => 'This is a child issue',
525 525 :parent_issue_id => 2}
526 526 end
527 527 issue = Issue.find_by_subject('This is a child issue')
528 528 assert_not_nil issue
529 529 assert_equal Issue.find(2), issue.parent
530 530 end
531 531
532 532 def test_post_create_subissue_with_non_numeric_parent_id
533 533 @request.session[:user_id] = 2
534 534
535 535 assert_difference 'Issue.count' do
536 536 post :create, :project_id => 1,
537 537 :issue => {:tracker_id => 1,
538 538 :subject => 'This is a child issue',
539 539 :parent_issue_id => 'ABC'}
540 540 end
541 541 issue = Issue.find_by_subject('This is a child issue')
542 542 assert_not_nil issue
543 543 assert_nil issue.parent
544 544 end
545 545
546 546 def test_post_create_should_send_a_notification
547 547 ActionMailer::Base.deliveries.clear
548 548 @request.session[:user_id] = 2
549 549 assert_difference 'Issue.count' do
550 550 post :create, :project_id => 1,
551 551 :issue => {:tracker_id => 3,
552 552 :subject => 'This is the test_new issue',
553 553 :description => 'This is the description',
554 554 :priority_id => 5,
555 555 :estimated_hours => '',
556 556 :custom_field_values => {'2' => 'Value for field 2'}}
557 557 end
558 558 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
559 559
560 560 assert_equal 1, ActionMailer::Base.deliveries.size
561 561 end
562 562
563 563 def test_post_create_should_preserve_fields_values_on_validation_failure
564 564 @request.session[:user_id] = 2
565 565 post :create, :project_id => 1,
566 566 :issue => {:tracker_id => 1,
567 567 # empty subject
568 568 :subject => '',
569 569 :description => 'This is a description',
570 570 :priority_id => 6,
571 571 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
572 572 assert_response :success
573 573 assert_template 'new'
574 574
575 575 assert_tag :textarea, :attributes => { :name => 'issue[description]' },
576 576 :content => 'This is a description'
577 577 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
578 578 :child => { :tag => 'option', :attributes => { :selected => 'selected',
579 579 :value => '6' },
580 580 :content => 'High' }
581 581 # Custom fields
582 582 assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
583 583 :child => { :tag => 'option', :attributes => { :selected => 'selected',
584 584 :value => 'Oracle' },
585 585 :content => 'Oracle' }
586 586 assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
587 587 :value => 'Value for field 2'}
588 588 end
589 589
590 590 def test_post_create_should_ignore_non_safe_attributes
591 591 @request.session[:user_id] = 2
592 592 assert_nothing_raised do
593 593 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
594 594 end
595 595 end
596 596
597 597 context "without workflow privilege" do
598 598 setup do
599 599 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
600 600 Role.anonymous.add_permission! :add_issues, :add_issue_notes
601 601 end
602 602
603 603 context "#new" do
604 604 should "propose default status only" do
605 605 get :new, :project_id => 1
606 606 assert_response :success
607 607 assert_template 'new'
608 608 assert_tag :tag => 'select',
609 609 :attributes => {:name => 'issue[status_id]'},
610 610 :children => {:count => 1},
611 611 :child => {:tag => 'option', :attributes => {:value => IssueStatus.default.id.to_s}}
612 612 end
613 613
614 614 should "accept default status" do
615 615 assert_difference 'Issue.count' do
616 616 post :create, :project_id => 1,
617 617 :issue => {:tracker_id => 1,
618 618 :subject => 'This is an issue',
619 619 :status_id => 1}
620 620 end
621 621 issue = Issue.last(:order => 'id')
622 622 assert_equal IssueStatus.default, issue.status
623 623 end
624 624
625 625 should "accept default status" do
626 626 assert_difference 'Issue.count' do
627 627 post :create, :project_id => 1,
628 628 :issue => {:tracker_id => 1,
629 629 :subject => 'This is an issue',
630 630 :status_id => 1}
631 631 end
632 632 issue = Issue.last(:order => 'id')
633 633 assert_equal IssueStatus.default, issue.status
634 634 end
635 635
636 636 should "ignore unauthorized status" do
637 637 assert_difference 'Issue.count' do
638 638 post :create, :project_id => 1,
639 639 :issue => {:tracker_id => 1,
640 640 :subject => 'This is an issue',
641 641 :status_id => 3}
642 642 end
643 643 issue = Issue.last(:order => 'id')
644 644 assert_equal IssueStatus.default, issue.status
645 645 end
646 646 end
647 647
648 648 context "#update" do
649 649 should "ignore status change" do
650 650 assert_difference 'Journal.count' do
651 651 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
652 652 end
653 653 assert_equal 1, Issue.find(1).status_id
654 654 end
655 655
656 656 should "ignore attributes changes" do
657 657 assert_difference 'Journal.count' do
658 658 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
659 659 end
660 660 issue = Issue.find(1)
661 661 assert_equal "Can't print recipes", issue.subject
662 662 assert_nil issue.assigned_to
663 663 end
664 664 end
665 665 end
666 666
667 667 context "with workflow privilege" do
668 668 setup do
669 669 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
670 670 Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
671 671 Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
672 672 Role.anonymous.add_permission! :add_issues, :add_issue_notes
673 673 end
674 674
675 675 context "#update" do
676 676 should "accept authorized status" do
677 677 assert_difference 'Journal.count' do
678 678 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
679 679 end
680 680 assert_equal 3, Issue.find(1).status_id
681 681 end
682 682
683 683 should "ignore unauthorized status" do
684 684 assert_difference 'Journal.count' do
685 685 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
686 686 end
687 687 assert_equal 1, Issue.find(1).status_id
688 688 end
689 689
690 690 should "accept authorized attributes changes" do
691 691 assert_difference 'Journal.count' do
692 692 put :update, :id => 1, :notes => 'just trying', :issue => {:assigned_to_id => 2}
693 693 end
694 694 issue = Issue.find(1)
695 695 assert_equal 2, issue.assigned_to_id
696 696 end
697 697
698 698 should "ignore unauthorized attributes changes" do
699 699 assert_difference 'Journal.count' do
700 700 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed'}
701 701 end
702 702 issue = Issue.find(1)
703 703 assert_equal "Can't print recipes", issue.subject
704 704 end
705 705 end
706 706
707 707 context "and :edit_issues permission" do
708 708 setup do
709 709 Role.anonymous.add_permission! :add_issues, :edit_issues
710 710 end
711 711
712 712 should "accept authorized status" do
713 713 assert_difference 'Journal.count' do
714 714 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
715 715 end
716 716 assert_equal 3, Issue.find(1).status_id
717 717 end
718 718
719 719 should "ignore unauthorized status" do
720 720 assert_difference 'Journal.count' do
721 721 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
722 722 end
723 723 assert_equal 1, Issue.find(1).status_id
724 724 end
725 725
726 726 should "accept authorized attributes changes" do
727 727 assert_difference 'Journal.count' do
728 728 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
729 729 end
730 730 issue = Issue.find(1)
731 731 assert_equal "changed", issue.subject
732 732 assert_equal 2, issue.assigned_to_id
733 733 end
734 734 end
735 735 end
736 736
737 737 def test_copy_issue
738 738 @request.session[:user_id] = 2
739 739 get :new, :project_id => 1, :copy_from => 1
740 740 assert_template 'new'
741 741 assert_not_nil assigns(:issue)
742 742 orig = Issue.find(1)
743 743 assert_equal orig.subject, assigns(:issue).subject
744 744 end
745 745
746 746 def test_get_edit
747 747 @request.session[:user_id] = 2
748 748 get :edit, :id => 1
749 749 assert_response :success
750 750 assert_template 'edit'
751 751 assert_not_nil assigns(:issue)
752 752 assert_equal Issue.find(1), assigns(:issue)
753 753 end
754 754
755 755 def test_get_edit_with_params
756 756 @request.session[:user_id] = 2
757 757 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
758 758 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => TimeEntryActivity.first.id }
759 759 assert_response :success
760 760 assert_template 'edit'
761 761
762 762 issue = assigns(:issue)
763 763 assert_not_nil issue
764 764
765 765 assert_equal 5, issue.status_id
766 766 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
767 767 :child => { :tag => 'option',
768 768 :content => 'Closed',
769 769 :attributes => { :selected => 'selected' } }
770 770
771 771 assert_equal 7, issue.priority_id
772 772 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
773 773 :child => { :tag => 'option',
774 774 :content => 'Urgent',
775 775 :attributes => { :selected => 'selected' } }
776 776
777 777 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => '2.5' }
778 778 assert_tag :select, :attributes => { :name => 'time_entry[activity_id]' },
779 779 :child => { :tag => 'option',
780 780 :attributes => { :selected => 'selected', :value => TimeEntryActivity.first.id } }
781 781 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => 'test_get_edit_with_params' }
782 782 end
783 783
784 784 def test_update_edit_form
785 785 @request.session[:user_id] = 2
786 786 xhr :post, :new, :project_id => 1,
787 787 :id => 1,
788 788 :issue => {:tracker_id => 2,
789 789 :subject => 'This is the test_new issue',
790 790 :description => 'This is the description',
791 791 :priority_id => 5}
792 792 assert_response :success
793 793 assert_template 'attributes'
794 794
795 795 issue = assigns(:issue)
796 796 assert_kind_of Issue, issue
797 797 assert_equal 1, issue.id
798 798 assert_equal 1, issue.project_id
799 799 assert_equal 2, issue.tracker_id
800 800 assert_equal 'This is the test_new issue', issue.subject
801 801 end
802 802
803 803 def test_update_using_invalid_http_verbs
804 804 @request.session[:user_id] = 2
805 805 subject = 'Updated by an invalid http verb'
806 806
807 807 get :update, :id => 1, :issue => {:subject => subject}
808 808 assert_not_equal subject, Issue.find(1).subject
809 809
810 810 post :update, :id => 1, :issue => {:subject => subject}
811 811 assert_not_equal subject, Issue.find(1).subject
812 812
813 813 delete :update, :id => 1, :issue => {:subject => subject}
814 814 assert_not_equal subject, Issue.find(1).subject
815 815 end
816 816
817 817 def test_put_update_without_custom_fields_param
818 818 @request.session[:user_id] = 2
819 819 ActionMailer::Base.deliveries.clear
820 820
821 821 issue = Issue.find(1)
822 822 assert_equal '125', issue.custom_value_for(2).value
823 823 old_subject = issue.subject
824 824 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
825 825
826 826 assert_difference('Journal.count') do
827 827 assert_difference('JournalDetail.count', 2) do
828 828 put :update, :id => 1, :issue => {:subject => new_subject,
829 829 :priority_id => '6',
830 830 :category_id => '1' # no change
831 831 }
832 832 end
833 833 end
834 834 assert_redirected_to :action => 'show', :id => '1'
835 835 issue.reload
836 836 assert_equal new_subject, issue.subject
837 837 # Make sure custom fields were not cleared
838 838 assert_equal '125', issue.custom_value_for(2).value
839 839
840 840 mail = ActionMailer::Base.deliveries.last
841 841 assert_kind_of TMail::Mail, mail
842 842 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
843 843 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
844 844 end
845 845
846 846 def test_put_update_with_custom_field_change
847 847 @request.session[:user_id] = 2
848 848 issue = Issue.find(1)
849 849 assert_equal '125', issue.custom_value_for(2).value
850 850
851 851 assert_difference('Journal.count') do
852 852 assert_difference('JournalDetail.count', 3) do
853 853 put :update, :id => 1, :issue => {:subject => 'Custom field change',
854 854 :priority_id => '6',
855 855 :category_id => '1', # no change
856 856 :custom_field_values => { '2' => 'New custom value' }
857 857 }
858 858 end
859 859 end
860 860 assert_redirected_to :action => 'show', :id => '1'
861 861 issue.reload
862 862 assert_equal 'New custom value', issue.custom_value_for(2).value
863 863
864 864 mail = ActionMailer::Base.deliveries.last
865 865 assert_kind_of TMail::Mail, mail
866 866 assert mail.body.include?("Searchable field changed from 125 to New custom value")
867 867 end
868 868
869 869 def test_put_update_with_status_and_assignee_change
870 870 issue = Issue.find(1)
871 871 assert_equal 1, issue.status_id
872 872 @request.session[:user_id] = 2
873 873 assert_difference('TimeEntry.count', 0) do
874 874 put :update,
875 875 :id => 1,
876 876 :issue => { :status_id => 2, :assigned_to_id => 3 },
877 877 :notes => 'Assigned to dlopper',
878 878 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
879 879 end
880 880 assert_redirected_to :action => 'show', :id => '1'
881 881 issue.reload
882 882 assert_equal 2, issue.status_id
883 883 j = Journal.find(:first, :order => 'id DESC')
884 884 assert_equal 'Assigned to dlopper', j.notes
885 885 assert_equal 2, j.details.size
886 886
887 887 mail = ActionMailer::Base.deliveries.last
888 888 assert mail.body.include?("Status changed from New to Assigned")
889 889 # subject should contain the new status
890 890 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
891 891 end
892 892
893 893 def test_put_update_with_note_only
894 894 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
895 895 # anonymous user
896 896 put :update,
897 897 :id => 1,
898 898 :notes => notes
899 899 assert_redirected_to :action => 'show', :id => '1'
900 900 j = Journal.find(:first, :order => 'id DESC')
901 901 assert_equal notes, j.notes
902 902 assert_equal 0, j.details.size
903 903 assert_equal User.anonymous, j.user
904 904
905 905 mail = ActionMailer::Base.deliveries.last
906 906 assert mail.body.include?(notes)
907 907 end
908 908
909 909 def test_put_update_with_note_and_spent_time
910 910 @request.session[:user_id] = 2
911 911 spent_hours_before = Issue.find(1).spent_hours
912 912 assert_difference('TimeEntry.count') do
913 913 put :update,
914 914 :id => 1,
915 915 :notes => '2.5 hours added',
916 916 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
917 917 end
918 918 assert_redirected_to :action => 'show', :id => '1'
919 919
920 920 issue = Issue.find(1)
921 921
922 922 j = Journal.find(:first, :order => 'id DESC')
923 923 assert_equal '2.5 hours added', j.notes
924 924 assert_equal 0, j.details.size
925 925
926 926 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
927 927 assert_not_nil t
928 928 assert_equal 2.5, t.hours
929 929 assert_equal spent_hours_before + 2.5, issue.spent_hours
930 930 end
931 931
932 932 def test_put_update_with_attachment_only
933 933 set_tmp_attachments_directory
934 934
935 935 # Delete all fixtured journals, a race condition can occur causing the wrong
936 936 # journal to get fetched in the next find.
937 937 Journal.delete_all
938 938
939 939 # anonymous user
940 940 put :update,
941 941 :id => 1,
942 942 :notes => '',
943 943 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
944 944 assert_redirected_to :action => 'show', :id => '1'
945 945 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
946 946 assert j.notes.blank?
947 947 assert_equal 1, j.details.size
948 948 assert_equal 'testfile.txt', j.details.first.value
949 949 assert_equal User.anonymous, j.user
950 950
951 951 mail = ActionMailer::Base.deliveries.last
952 952 assert mail.body.include?('testfile.txt')
953 953 end
954 954
955 955 def test_put_update_with_attachment_that_fails_to_save
956 956 set_tmp_attachments_directory
957 957
958 958 # Delete all fixtured journals, a race condition can occur causing the wrong
959 959 # journal to get fetched in the next find.
960 960 Journal.delete_all
961 961
962 962 # Mock out the unsaved attachment
963 963 Attachment.any_instance.stubs(:create).returns(Attachment.new)
964 964
965 965 # anonymous user
966 966 put :update,
967 967 :id => 1,
968 968 :notes => '',
969 969 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
970 970 assert_redirected_to :action => 'show', :id => '1'
971 971 assert_equal '1 file(s) could not be saved.', flash[:warning]
972 972
973 973 end if Object.const_defined?(:Mocha)
974 974
975 975 def test_put_update_with_no_change
976 976 issue = Issue.find(1)
977 977 issue.journals.clear
978 978 ActionMailer::Base.deliveries.clear
979 979
980 980 put :update,
981 981 :id => 1,
982 982 :notes => ''
983 983 assert_redirected_to :action => 'show', :id => '1'
984 984
985 985 issue.reload
986 986 assert issue.journals.empty?
987 987 # No email should be sent
988 988 assert ActionMailer::Base.deliveries.empty?
989 989 end
990 990
991 991 def test_put_update_should_send_a_notification
992 992 @request.session[:user_id] = 2
993 993 ActionMailer::Base.deliveries.clear
994 994 issue = Issue.find(1)
995 995 old_subject = issue.subject
996 996 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
997 997
998 998 put :update, :id => 1, :issue => {:subject => new_subject,
999 999 :priority_id => '6',
1000 1000 :category_id => '1' # no change
1001 1001 }
1002 1002 assert_equal 1, ActionMailer::Base.deliveries.size
1003 1003 end
1004 1004
1005 1005 def test_put_update_with_invalid_spent_time_hours_only
1006 1006 @request.session[:user_id] = 2
1007 1007 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
1008 1008
1009 1009 assert_no_difference('Journal.count') do
1010 1010 put :update,
1011 1011 :id => 1,
1012 1012 :notes => notes,
1013 1013 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
1014 1014 end
1015 1015 assert_response :success
1016 1016 assert_template 'edit'
1017 1017
1018 1018 assert_error_tag :descendant => {:content => /Activity can't be blank/}
1019 1019 assert_tag :textarea, :attributes => { :name => 'notes' }, :content => notes
1020 1020 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
1021 1021 end
1022 1022
1023 1023 def test_put_update_with_invalid_spent_time_comments_only
1024 1024 @request.session[:user_id] = 2
1025 1025 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
1026 1026
1027 1027 assert_no_difference('Journal.count') do
1028 1028 put :update,
1029 1029 :id => 1,
1030 1030 :notes => notes,
1031 1031 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
1032 1032 end
1033 1033 assert_response :success
1034 1034 assert_template 'edit'
1035 1035
1036 1036 assert_error_tag :descendant => {:content => /Activity can't be blank/}
1037 1037 assert_error_tag :descendant => {:content => /Hours can't be blank/}
1038 1038 assert_tag :textarea, :attributes => { :name => 'notes' }, :content => notes
1039 1039 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => "this is my comment" }
1040 1040 end
1041 1041
1042 1042 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
1043 1043 issue = Issue.find(2)
1044 1044 @request.session[:user_id] = 2
1045 1045
1046 1046 put :update,
1047 1047 :id => issue.id,
1048 1048 :issue => {
1049 1049 :fixed_version_id => 4
1050 1050 }
1051 1051
1052 1052 assert_response :redirect
1053 1053 issue.reload
1054 1054 assert_equal 4, issue.fixed_version_id
1055 1055 assert_not_equal issue.project_id, issue.fixed_version.project_id
1056 1056 end
1057 1057
1058 1058 def test_put_update_should_redirect_back_using_the_back_url_parameter
1059 1059 issue = Issue.find(2)
1060 1060 @request.session[:user_id] = 2
1061 1061
1062 1062 put :update,
1063 1063 :id => issue.id,
1064 1064 :issue => {
1065 1065 :fixed_version_id => 4
1066 1066 },
1067 1067 :back_url => '/issues'
1068 1068
1069 1069 assert_response :redirect
1070 1070 assert_redirected_to '/issues'
1071 1071 end
1072 1072
1073 1073 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1074 1074 issue = Issue.find(2)
1075 1075 @request.session[:user_id] = 2
1076 1076
1077 1077 put :update,
1078 1078 :id => issue.id,
1079 1079 :issue => {
1080 1080 :fixed_version_id => 4
1081 1081 },
1082 1082 :back_url => 'http://google.com'
1083 1083
1084 1084 assert_response :redirect
1085 1085 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
1086 1086 end
1087 1087
1088 1088 def test_get_bulk_edit
1089 1089 @request.session[:user_id] = 2
1090 1090 get :bulk_edit, :ids => [1, 2]
1091 1091 assert_response :success
1092 1092 assert_template 'bulk_edit'
1093 1093
1094 1094 assert_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
1095 1095
1096 1096 # Project specific custom field, date type
1097 1097 field = CustomField.find(9)
1098 1098 assert !field.is_for_all?
1099 1099 assert_equal 'date', field.field_format
1100 1100 assert_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
1101 1101
1102 1102 # System wide custom field
1103 1103 assert CustomField.find(1).is_for_all?
1104 1104 assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
1105 1105 end
1106 1106
1107 1107 def test_get_bulk_edit_on_different_projects
1108 1108 @request.session[:user_id] = 2
1109 1109 get :bulk_edit, :ids => [1, 2, 6]
1110 1110 assert_response :success
1111 1111 assert_template 'bulk_edit'
1112 1112
1113 1113 # Can not set issues from different projects as children of an issue
1114 1114 assert_no_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
1115 1115
1116 1116 # Project specific custom field, date type
1117 1117 field = CustomField.find(9)
1118 1118 assert !field.is_for_all?
1119 1119 assert !field.project_ids.include?(Issue.find(6).project_id)
1120 1120 assert_no_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
1121 1121 end
1122 1122
1123 1123 def test_bulk_update
1124 1124 @request.session[:user_id] = 2
1125 1125 # update issues priority
1126 1126 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
1127 1127 :issue => {:priority_id => 7,
1128 1128 :assigned_to_id => '',
1129 1129 :custom_field_values => {'2' => ''}}
1130 1130
1131 1131 assert_response 302
1132 1132 # check that the issues were updated
1133 1133 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
1134 1134
1135 1135 issue = Issue.find(1)
1136 1136 journal = issue.journals.find(:first, :order => 'created_on DESC')
1137 1137 assert_equal '125', issue.custom_value_for(2).value
1138 1138 assert_equal 'Bulk editing', journal.notes
1139 1139 assert_equal 1, journal.details.size
1140 1140 end
1141 1141
1142 1142 def test_bulk_update_on_different_projects
1143 1143 @request.session[:user_id] = 2
1144 1144 # update issues priority
1145 1145 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
1146 1146 :issue => {:priority_id => 7,
1147 1147 :assigned_to_id => '',
1148 1148 :custom_field_values => {'2' => ''}}
1149 1149
1150 1150 assert_response 302
1151 1151 # check that the issues were updated
1152 1152 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
1153 1153
1154 1154 issue = Issue.find(1)
1155 1155 journal = issue.journals.find(:first, :order => 'created_on DESC')
1156 1156 assert_equal '125', issue.custom_value_for(2).value
1157 1157 assert_equal 'Bulk editing', journal.notes
1158 1158 assert_equal 1, journal.details.size
1159 1159 end
1160 1160
1161 1161 def test_bulk_update_on_different_projects_without_rights
1162 1162 @request.session[:user_id] = 3
1163 1163 user = User.find(3)
1164 1164 action = { :controller => "issues", :action => "bulk_update" }
1165 1165 assert user.allowed_to?(action, Issue.find(1).project)
1166 1166 assert ! user.allowed_to?(action, Issue.find(6).project)
1167 1167 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
1168 1168 :issue => {:priority_id => 7,
1169 1169 :assigned_to_id => '',
1170 1170 :custom_field_values => {'2' => ''}}
1171 1171 assert_response 403
1172 1172 assert_not_equal "Bulk should fail", Journal.last.notes
1173 1173 end
1174 1174
1175 1175 def test_bullk_update_should_send_a_notification
1176 1176 @request.session[:user_id] = 2
1177 1177 ActionMailer::Base.deliveries.clear
1178 1178 post(:bulk_update,
1179 1179 {
1180 1180 :ids => [1, 2],
1181 1181 :notes => 'Bulk editing',
1182 1182 :issue => {
1183 1183 :priority_id => 7,
1184 1184 :assigned_to_id => '',
1185 1185 :custom_field_values => {'2' => ''}
1186 1186 }
1187 1187 })
1188 1188
1189 1189 assert_response 302
1190 1190 assert_equal 2, ActionMailer::Base.deliveries.size
1191 1191 end
1192 1192
1193 1193 def test_bulk_update_status
1194 1194 @request.session[:user_id] = 2
1195 1195 # update issues priority
1196 1196 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
1197 1197 :issue => {:priority_id => '',
1198 1198 :assigned_to_id => '',
1199 1199 :status_id => '5'}
1200 1200
1201 1201 assert_response 302
1202 1202 issue = Issue.find(1)
1203 1203 assert issue.closed?
1204 1204 end
1205 1205
1206 1206 def test_bulk_update_parent_id
1207 1207 @request.session[:user_id] = 2
1208 1208 post :bulk_update, :ids => [1, 3],
1209 1209 :notes => 'Bulk editing parent',
1210 1210 :issue => {:priority_id => '', :assigned_to_id => '', :status_id => '', :parent_issue_id => '2'}
1211 1211
1212 1212 assert_response 302
1213 1213 parent = Issue.find(2)
1214 1214 assert_equal parent.id, Issue.find(1).parent_id
1215 1215 assert_equal parent.id, Issue.find(3).parent_id
1216 1216 assert_equal [1, 3], parent.children.collect(&:id).sort
1217 1217 end
1218 1218
1219 1219 def test_bulk_update_custom_field
1220 1220 @request.session[:user_id] = 2
1221 1221 # update issues priority
1222 1222 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
1223 1223 :issue => {:priority_id => '',
1224 1224 :assigned_to_id => '',
1225 1225 :custom_field_values => {'2' => '777'}}
1226 1226
1227 1227 assert_response 302
1228 1228
1229 1229 issue = Issue.find(1)
1230 1230 journal = issue.journals.find(:first, :order => 'created_on DESC')
1231 1231 assert_equal '777', issue.custom_value_for(2).value
1232 1232 assert_equal 1, journal.details.size
1233 1233 assert_equal '125', journal.details.first.old_value
1234 1234 assert_equal '777', journal.details.first.value
1235 1235 end
1236 1236
1237 1237 def test_bulk_update_unassign
1238 1238 assert_not_nil Issue.find(2).assigned_to
1239 1239 @request.session[:user_id] = 2
1240 1240 # unassign issues
1241 1241 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
1242 1242 assert_response 302
1243 1243 # check that the issues were updated
1244 1244 assert_nil Issue.find(2).assigned_to
1245 1245 end
1246 1246
1247 1247 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
1248 1248 @request.session[:user_id] = 2
1249 1249
1250 1250 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
1251 1251
1252 1252 assert_response :redirect
1253 1253 issues = Issue.find([1,2])
1254 1254 issues.each do |issue|
1255 1255 assert_equal 4, issue.fixed_version_id
1256 1256 assert_not_equal issue.project_id, issue.fixed_version.project_id
1257 1257 end
1258 1258 end
1259 1259
1260 1260 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
1261 1261 @request.session[:user_id] = 2
1262 1262 post :bulk_update, :ids => [1,2], :back_url => '/issues'
1263 1263
1264 1264 assert_response :redirect
1265 1265 assert_redirected_to '/issues'
1266 1266 end
1267 1267
1268 1268 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1269 1269 @request.session[:user_id] = 2
1270 1270 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
1271 1271
1272 1272 assert_response :redirect
1273 1273 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
1274 1274 end
1275 1275
1276 1276 def test_destroy_issue_with_no_time_entries
1277 1277 assert_nil TimeEntry.find_by_issue_id(2)
1278 1278 @request.session[:user_id] = 2
1279 1279 post :destroy, :id => 2
1280 1280 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1281 1281 assert_nil Issue.find_by_id(2)
1282 1282 end
1283 1283
1284 1284 def test_destroy_issues_with_time_entries
1285 1285 @request.session[:user_id] = 2
1286 1286 post :destroy, :ids => [1, 3]
1287 1287 assert_response :success
1288 1288 assert_template 'destroy'
1289 1289 assert_not_nil assigns(:hours)
1290 1290 assert Issue.find_by_id(1) && Issue.find_by_id(3)
1291 1291 end
1292 1292
1293 1293 def test_destroy_issues_and_destroy_time_entries
1294 1294 @request.session[:user_id] = 2
1295 1295 post :destroy, :ids => [1, 3], :todo => 'destroy'
1296 1296 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1297 1297 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1298 1298 assert_nil TimeEntry.find_by_id([1, 2])
1299 1299 end
1300 1300
1301 1301 def test_destroy_issues_and_assign_time_entries_to_project
1302 1302 @request.session[:user_id] = 2
1303 1303 post :destroy, :ids => [1, 3], :todo => 'nullify'
1304 1304 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1305 1305 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1306 1306 assert_nil TimeEntry.find(1).issue_id
1307 1307 assert_nil TimeEntry.find(2).issue_id
1308 1308 end
1309 1309
1310 1310 def test_destroy_issues_and_reassign_time_entries_to_another_issue
1311 1311 @request.session[:user_id] = 2
1312 1312 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1313 1313 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1314 1314 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1315 1315 assert_equal 2, TimeEntry.find(1).issue_id
1316 1316 assert_equal 2, TimeEntry.find(2).issue_id
1317 1317 end
1318 1318
1319 1319 def test_destroy_issues_from_different_projects
1320 1320 @request.session[:user_id] = 2
1321 1321 post :destroy, :ids => [1, 2, 6], :todo => 'destroy'
1322 1322 assert_redirected_to :controller => 'issues', :action => 'index'
1323 1323 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
1324 1324 end
1325 1325
1326 def test_destroy_parent_and_child_issues
1327 parent = Issue.generate!(:project_id => 1, :tracker_id => 1)
1328 child = Issue.generate!(:project_id => 1, :tracker_id => 1, :parent_issue_id => parent.id)
1329 assert child.is_descendant_of?(parent.reload)
1330
1331 @request.session[:user_id] = 2
1332 assert_difference 'Issue.count', -2 do
1333 post :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
1334 end
1335 assert_response 302
1336 end
1337
1326 1338 def test_default_search_scope
1327 1339 get :index
1328 1340 assert_tag :div, :attributes => {:id => 'quick-search'},
1329 1341 :child => {:tag => 'form',
1330 1342 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1331 1343 end
1332 1344 end
General Comments 0
You need to be logged in to leave comments. Login now