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