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