##// END OF EJS Templates
Merged r8113 from trunk (#9738)....
Jean-Philippe Lang -
r8039:65a7c7e96c9b
parent child
Show More
@@ -1,102 +1,103
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module QueriesHelper
19 19
20 20 def operators_for_select(filter_type)
21 21 Query.operators_by_filter_type[filter_type].collect {|o| [l(Query.operators[o]), o]}
22 22 end
23 23
24 24 def column_header(column)
25 25 column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption,
26 26 :default_order => column.default_order) :
27 27 content_tag('th', h(column.caption))
28 28 end
29 29
30 30 def column_content(column, issue)
31 31 value = column.value(issue)
32 32
33 33 case value.class.name
34 34 when 'String'
35 35 if column.name == :subject
36 36 link_to(h(value), :controller => 'issues', :action => 'show', :id => issue)
37 37 else
38 38 h(value)
39 39 end
40 40 when 'Time'
41 41 format_time(value)
42 42 when 'Date'
43 43 format_date(value)
44 44 when 'Fixnum', 'Float'
45 45 if column.name == :done_ratio
46 46 progress_bar(value, :width => '80px')
47 47 else
48 48 h(value.to_s)
49 49 end
50 50 when 'User'
51 51 link_to_user value
52 52 when 'Project'
53 53 link_to_project value
54 54 when 'Version'
55 55 link_to(h(value), :controller => 'versions', :action => 'show', :id => value)
56 56 when 'TrueClass'
57 57 l(:general_text_Yes)
58 58 when 'FalseClass'
59 59 l(:general_text_No)
60 60 when 'Issue'
61 61 link_to_issue(value, :subject => false)
62 62 else
63 63 h(value)
64 64 end
65 65 end
66 66
67 67 # Retrieve query from session or build a new query
68 68 def retrieve_query
69 69 if !params[:query_id].blank?
70 70 cond = "project_id IS NULL"
71 71 cond << " OR project_id = #{@project.id}" if @project
72 72 @query = Query.find(params[:query_id], :conditions => cond)
73 73 raise ::Unauthorized unless @query.visible?
74 74 @query.project = @project
75 75 session[:query] = {:id => @query.id, :project_id => @query.project_id}
76 76 sort_clear
77 77 elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
78 78 # Give it a name, required to be valid
79 79 @query = Query.new(:name => "_")
80 80 @query.project = @project
81 81 build_query_from_params
82 82 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
83 83 else
84 84 # retrieve from session
85 85 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
86 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
86 @query ||= Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
87 @query.project = @project
87 88 end
88 89 end
89 90
90 91 def build_query_from_params
91 92 if params[:fields] || params[:f]
92 93 @query.filters = {}
93 94 @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v])
94 95 else
95 96 @query.available_filters.keys.each do |field|
96 97 @query.add_short_filter(field, params[field]) if params[field]
97 98 end
98 99 end
99 100 @query.group_by = params[:group_by] || (params[:query] && params[:query][:group_by])
100 101 @query.column_names = params[:c] || (params[:query] && params[:query][:column_names])
101 102 end
102 103 end
@@ -1,1999 +1,2021
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19 require 'issues_controller'
20 20
21 21 class IssuesControllerTest < ActionController::TestCase
22 22 fixtures :projects,
23 23 :users,
24 24 :roles,
25 25 :members,
26 26 :member_roles,
27 27 :issues,
28 28 :issue_statuses,
29 29 :versions,
30 30 :trackers,
31 31 :projects_trackers,
32 32 :issue_categories,
33 33 :enabled_modules,
34 34 :enumerations,
35 35 :attachments,
36 36 :workflows,
37 37 :custom_fields,
38 38 :custom_values,
39 39 :custom_fields_projects,
40 40 :custom_fields_trackers,
41 41 :time_entries,
42 42 :journals,
43 43 :journal_details,
44 44 :queries
45 45
46 46 include Redmine::I18n
47 47
48 48 def setup
49 49 @controller = IssuesController.new
50 50 @request = ActionController::TestRequest.new
51 51 @response = ActionController::TestResponse.new
52 52 User.current = nil
53 53 end
54 54
55 55 def test_index
56 56 Setting.default_language = 'en'
57 57
58 58 get :index
59 59 assert_response :success
60 60 assert_template 'index'
61 61 assert_not_nil assigns(:issues)
62 62 assert_nil assigns(:project)
63 63 assert_tag :tag => 'a', :content => /Can't print recipes/
64 64 assert_tag :tag => 'a', :content => /Subproject issue/
65 65 # private projects hidden
66 66 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
67 67 assert_no_tag :tag => 'a', :content => /Issue on project 2/
68 68 # project column
69 69 assert_tag :tag => 'th', :content => /Project/
70 70 end
71 71
72 72 def test_index_should_not_list_issues_when_module_disabled
73 73 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
74 74 get :index
75 75 assert_response :success
76 76 assert_template 'index'
77 77 assert_not_nil assigns(:issues)
78 78 assert_nil assigns(:project)
79 79 assert_no_tag :tag => 'a', :content => /Can't print recipes/
80 80 assert_tag :tag => 'a', :content => /Subproject issue/
81 81 end
82 82
83 83 def test_index_should_list_visible_issues_only
84 84 get :index, :per_page => 100
85 85 assert_response :success
86 86 assert_not_nil assigns(:issues)
87 87 assert_nil assigns(:issues).detect {|issue| !issue.visible?}
88 88 end
89 89
90 90 def test_index_with_project
91 91 Setting.display_subprojects_issues = 0
92 92 get :index, :project_id => 1
93 93 assert_response :success
94 94 assert_template 'index'
95 95 assert_not_nil assigns(:issues)
96 96 assert_tag :tag => 'a', :content => /Can't print recipes/
97 97 assert_no_tag :tag => 'a', :content => /Subproject issue/
98 98 end
99 99
100 100 def test_index_with_project_and_subprojects
101 101 Setting.display_subprojects_issues = 1
102 102 get :index, :project_id => 1
103 103 assert_response :success
104 104 assert_template 'index'
105 105 assert_not_nil assigns(:issues)
106 106 assert_tag :tag => 'a', :content => /Can't print recipes/
107 107 assert_tag :tag => 'a', :content => /Subproject issue/
108 108 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
109 109 end
110 110
111 111 def test_index_with_project_and_subprojects_should_show_private_subprojects
112 112 @request.session[:user_id] = 2
113 113 Setting.display_subprojects_issues = 1
114 114 get :index, :project_id => 1
115 115 assert_response :success
116 116 assert_template 'index'
117 117 assert_not_nil assigns(:issues)
118 118 assert_tag :tag => 'a', :content => /Can't print recipes/
119 119 assert_tag :tag => 'a', :content => /Subproject issue/
120 120 assert_tag :tag => 'a', :content => /Issue of a private subproject/
121 121 end
122 122
123 123 def test_index_with_project_and_default_filter
124 124 get :index, :project_id => 1, :set_filter => 1
125 125 assert_response :success
126 126 assert_template 'index'
127 127 assert_not_nil assigns(:issues)
128 128
129 129 query = assigns(:query)
130 130 assert_not_nil query
131 131 # default filter
132 132 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
133 133 end
134 134
135 135 def test_index_with_project_and_filter
136 136 get :index, :project_id => 1, :set_filter => 1,
137 137 :f => ['tracker_id'],
138 138 :op => {'tracker_id' => '='},
139 139 :v => {'tracker_id' => ['1']}
140 140 assert_response :success
141 141 assert_template 'index'
142 142 assert_not_nil assigns(:issues)
143 143
144 144 query = assigns(:query)
145 145 assert_not_nil query
146 146 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
147 147 end
148 148
149 149 def test_index_with_short_filters
150 150
151 151 to_test = {
152 152 'status_id' => {
153 153 'o' => { :op => 'o', :values => [''] },
154 154 'c' => { :op => 'c', :values => [''] },
155 155 '7' => { :op => '=', :values => ['7'] },
156 156 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
157 157 '=7' => { :op => '=', :values => ['7'] },
158 158 '!3' => { :op => '!', :values => ['3'] },
159 159 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
160 160 'subject' => {
161 161 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
162 162 'o' => { :op => '=', :values => ['o'] },
163 163 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
164 164 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
165 165 'tracker_id' => {
166 166 '3' => { :op => '=', :values => ['3'] },
167 167 '=3' => { :op => '=', :values => ['3'] }},
168 168 'start_date' => {
169 169 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
170 170 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
171 171 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
172 172 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
173 173 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
174 174 '<t+2' => { :op => '<t+', :values => ['2'] },
175 175 '>t+2' => { :op => '>t+', :values => ['2'] },
176 176 't+2' => { :op => 't+', :values => ['2'] },
177 177 't' => { :op => 't', :values => [''] },
178 178 'w' => { :op => 'w', :values => [''] },
179 179 '>t-2' => { :op => '>t-', :values => ['2'] },
180 180 '<t-2' => { :op => '<t-', :values => ['2'] },
181 181 't-2' => { :op => 't-', :values => ['2'] }},
182 182 'created_on' => {
183 183 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
184 184 '<t+2' => { :op => '=', :values => ['<t+2'] },
185 185 '>t+2' => { :op => '=', :values => ['>t+2'] },
186 186 't+2' => { :op => 't', :values => ['+2'] }},
187 187 'cf_1' => {
188 188 'c' => { :op => '=', :values => ['c'] },
189 189 '!c' => { :op => '!', :values => ['c'] },
190 190 '!*' => { :op => '!*', :values => [''] },
191 191 '*' => { :op => '*', :values => [''] }},
192 192 'estimated_hours' => {
193 193 '=13.4' => { :op => '=', :values => ['13.4'] },
194 194 '>=45' => { :op => '>=', :values => ['45'] },
195 195 '<=125' => { :op => '<=', :values => ['125'] },
196 196 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
197 197 '!*' => { :op => '!*', :values => [''] },
198 198 '*' => { :op => '*', :values => [''] }}
199 199 }
200 200
201 201 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
202 202
203 203 to_test.each do |field, expression_and_expected|
204 204 expression_and_expected.each do |filter_expression, expected|
205 205
206 206 get :index, :set_filter => 1, field => filter_expression
207 207
208 208 assert_response :success
209 209 assert_template 'index'
210 210 assert_not_nil assigns(:issues)
211 211
212 212 query = assigns(:query)
213 213 assert_not_nil query
214 214 assert query.has_filter?(field)
215 215 assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
216 216 end
217 217 end
218 218
219 219 end
220 220
221 221 def test_index_with_project_and_empty_filters
222 222 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
223 223 assert_response :success
224 224 assert_template 'index'
225 225 assert_not_nil assigns(:issues)
226 226
227 227 query = assigns(:query)
228 228 assert_not_nil query
229 229 # no filter
230 230 assert_equal({}, query.filters)
231 231 end
232 232
233 233 def test_index_with_query
234 234 get :index, :project_id => 1, :query_id => 5
235 235 assert_response :success
236 236 assert_template 'index'
237 237 assert_not_nil assigns(:issues)
238 238 assert_nil assigns(:issue_count_by_group)
239 239 end
240 240
241 241 def test_index_with_query_grouped_by_tracker
242 242 get :index, :project_id => 1, :query_id => 6
243 243 assert_response :success
244 244 assert_template 'index'
245 245 assert_not_nil assigns(:issues)
246 246 assert_not_nil assigns(:issue_count_by_group)
247 247 end
248 248
249 249 def test_index_with_query_grouped_by_list_custom_field
250 250 get :index, :project_id => 1, :query_id => 9
251 251 assert_response :success
252 252 assert_template 'index'
253 253 assert_not_nil assigns(:issues)
254 254 assert_not_nil assigns(:issue_count_by_group)
255 255 end
256 256
257 def test_index_with_query_id_and_project_id_should_set_session_query
258 get :index, :project_id => 1, :query_id => 4
259 assert_response :success
260 assert_kind_of Hash, session[:query]
261 assert_equal 4, session[:query][:id]
262 assert_equal 1, session[:query][:project_id]
263 end
264
265 def test_index_with_cross_project_query_in_session_should_show_project_issues
266 q = Query.create!(:name => "test", :user_id => 2, :is_public => false, :project => nil)
267 @request.session[:query] = {:id => q.id, :project_id => 1}
268
269 with_settings :display_subprojects_issues => '0' do
270 get :index, :project_id => 1
271 end
272 assert_response :success
273 assert_not_nil assigns(:query)
274 assert_equal q.id, assigns(:query).id
275 assert_equal 1, assigns(:query).project_id
276 assert_equal [1], assigns(:issues).map(&:project_id).uniq
277 end
278
257 279 def test_private_query_should_not_be_available_to_other_users
258 280 q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
259 281 @request.session[:user_id] = 3
260 282
261 283 get :index, :query_id => q.id
262 284 assert_response 403
263 285 end
264 286
265 287 def test_private_query_should_be_available_to_its_user
266 288 q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
267 289 @request.session[:user_id] = 2
268 290
269 291 get :index, :query_id => q.id
270 292 assert_response :success
271 293 end
272 294
273 295 def test_public_query_should_be_available_to_other_users
274 296 q = Query.create!(:name => "private", :user => User.find(2), :is_public => true, :project => nil)
275 297 @request.session[:user_id] = 3
276 298
277 299 get :index, :query_id => q.id
278 300 assert_response :success
279 301 end
280 302
281 303 def test_index_csv
282 304 get :index, :format => 'csv'
283 305 assert_response :success
284 306 assert_not_nil assigns(:issues)
285 307 assert_equal 'text/csv', @response.content_type
286 308 assert @response.body.starts_with?("#,")
287 309 lines = @response.body.chomp.split("\n")
288 310 assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
289 311 end
290 312
291 313 def test_index_csv_with_project
292 314 get :index, :project_id => 1, :format => 'csv'
293 315 assert_response :success
294 316 assert_not_nil assigns(:issues)
295 317 assert_equal 'text/csv', @response.content_type
296 318 end
297 319
298 320 def test_index_csv_with_description
299 321 get :index, :format => 'csv', :description => '1'
300 322 assert_response :success
301 323 assert_not_nil assigns(:issues)
302 324 assert_equal 'text/csv', @response.content_type
303 325 assert @response.body.starts_with?("#,")
304 326 lines = @response.body.chomp.split("\n")
305 327 assert_equal assigns(:query).columns.size + 2, lines[0].split(',').size
306 328 end
307 329
308 330 def test_index_csv_with_all_columns
309 331 get :index, :format => 'csv', :columns => 'all'
310 332 assert_response :success
311 333 assert_not_nil assigns(:issues)
312 334 assert_equal 'text/csv', @response.content_type
313 335 assert @response.body.starts_with?("#,")
314 336 lines = @response.body.chomp.split("\n")
315 337 assert_equal assigns(:query).available_columns.size + 1, lines[0].split(',').size
316 338 end
317 339
318 340 def test_index_csv_big_5
319 341 with_settings :default_language => "zh-TW" do
320 342 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
321 343 str_big5 = "\xa4@\xa4\xeb"
322 344 if str_utf8.respond_to?(:force_encoding)
323 345 str_utf8.force_encoding('UTF-8')
324 346 str_big5.force_encoding('Big5')
325 347 end
326 348 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
327 349 :status_id => 1, :priority => IssuePriority.all.first,
328 350 :subject => str_utf8)
329 351 assert issue.save
330 352
331 353 get :index, :project_id => 1,
332 354 :f => ['subject'],
333 355 :op => '=', :values => [str_utf8],
334 356 :format => 'csv'
335 357 assert_equal 'text/csv', @response.content_type
336 358 lines = @response.body.chomp.split("\n")
337 359 s1 = "\xaa\xac\xbaA"
338 360 if str_utf8.respond_to?(:force_encoding)
339 361 s1.force_encoding('Big5')
340 362 end
341 363 assert lines[0].include?(s1)
342 364 assert lines[1].include?(str_big5)
343 365 end
344 366 end
345 367
346 368 def test_index_csv_cannot_convert_should_be_replaced_big_5
347 369 with_settings :default_language => "zh-TW" do
348 370 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
349 371 if str_utf8.respond_to?(:force_encoding)
350 372 str_utf8.force_encoding('UTF-8')
351 373 end
352 374 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
353 375 :status_id => 1, :priority => IssuePriority.all.first,
354 376 :subject => str_utf8)
355 377 assert issue.save
356 378
357 379 get :index, :project_id => 1,
358 380 :f => ['subject'],
359 381 :op => '=', :values => [str_utf8],
360 382 :c => ['status', 'subject'],
361 383 :format => 'csv',
362 384 :set_filter => 1
363 385 assert_equal 'text/csv', @response.content_type
364 386 lines = @response.body.chomp.split("\n")
365 387 s1 = "\xaa\xac\xbaA" # status
366 388 if str_utf8.respond_to?(:force_encoding)
367 389 s1.force_encoding('Big5')
368 390 end
369 391 assert lines[0].include?(s1)
370 392 s2 = lines[1].split(",")[2]
371 393 if s1.respond_to?(:force_encoding)
372 394 s3 = "\xa5H?" # subject
373 395 s3.force_encoding('Big5')
374 396 assert_equal s3, s2
375 397 elsif RUBY_PLATFORM == 'java'
376 398 assert_equal "??", s2
377 399 else
378 400 assert_equal "\xa5H???", s2
379 401 end
380 402 end
381 403 end
382 404
383 405 def test_index_csv_tw
384 406 with_settings :default_language => "zh-TW" do
385 407 str1 = "test_index_csv_tw"
386 408 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
387 409 :status_id => 1, :priority => IssuePriority.all.first,
388 410 :subject => str1, :estimated_hours => '1234.5')
389 411 assert issue.save
390 412 assert_equal 1234.5, issue.estimated_hours
391 413
392 414 get :index, :project_id => 1,
393 415 :f => ['subject'],
394 416 :op => '=', :values => [str1],
395 417 :c => ['estimated_hours', 'subject'],
396 418 :format => 'csv',
397 419 :set_filter => 1
398 420 assert_equal 'text/csv', @response.content_type
399 421 lines = @response.body.chomp.split("\n")
400 422 assert_equal "#{issue.id},1234.5,#{str1}", lines[1]
401 423
402 424 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
403 425 if str_tw.respond_to?(:force_encoding)
404 426 str_tw.force_encoding('UTF-8')
405 427 end
406 428 assert_equal str_tw, l(:general_lang_name)
407 429 assert_equal ',', l(:general_csv_separator)
408 430 assert_equal '.', l(:general_csv_decimal_separator)
409 431 end
410 432 end
411 433
412 434 def test_index_csv_fr
413 435 with_settings :default_language => "fr" do
414 436 str1 = "test_index_csv_fr"
415 437 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
416 438 :status_id => 1, :priority => IssuePriority.all.first,
417 439 :subject => str1, :estimated_hours => '1234.5')
418 440 assert issue.save
419 441 assert_equal 1234.5, issue.estimated_hours
420 442
421 443 get :index, :project_id => 1,
422 444 :f => ['subject'],
423 445 :op => '=', :values => [str1],
424 446 :c => ['estimated_hours', 'subject'],
425 447 :format => 'csv',
426 448 :set_filter => 1
427 449 assert_equal 'text/csv', @response.content_type
428 450 lines = @response.body.chomp.split("\n")
429 451 assert_equal "#{issue.id};1234,5;#{str1}", lines[1]
430 452
431 453 str_fr = "Fran\xc3\xa7ais"
432 454 if str_fr.respond_to?(:force_encoding)
433 455 str_fr.force_encoding('UTF-8')
434 456 end
435 457 assert_equal str_fr, l(:general_lang_name)
436 458 assert_equal ';', l(:general_csv_separator)
437 459 assert_equal ',', l(:general_csv_decimal_separator)
438 460 end
439 461 end
440 462
441 463 def test_index_pdf
442 464 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
443 465 with_settings :default_language => lang do
444 466
445 467 get :index
446 468 assert_response :success
447 469 assert_template 'index'
448 470
449 471 if lang == "ja"
450 472 if RUBY_PLATFORM != 'java'
451 473 assert_equal "CP932", l(:general_pdf_encoding)
452 474 end
453 475 if RUBY_PLATFORM == 'java' && l(:general_pdf_encoding) == "CP932"
454 476 next
455 477 end
456 478 end
457 479
458 480 get :index, :format => 'pdf'
459 481 assert_response :success
460 482 assert_not_nil assigns(:issues)
461 483 assert_equal 'application/pdf', @response.content_type
462 484
463 485 get :index, :project_id => 1, :format => 'pdf'
464 486 assert_response :success
465 487 assert_not_nil assigns(:issues)
466 488 assert_equal 'application/pdf', @response.content_type
467 489
468 490 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
469 491 assert_response :success
470 492 assert_not_nil assigns(:issues)
471 493 assert_equal 'application/pdf', @response.content_type
472 494 end
473 495 end
474 496 end
475 497
476 498 def test_index_pdf_with_query_grouped_by_list_custom_field
477 499 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
478 500 assert_response :success
479 501 assert_not_nil assigns(:issues)
480 502 assert_not_nil assigns(:issue_count_by_group)
481 503 assert_equal 'application/pdf', @response.content_type
482 504 end
483 505
484 506 def test_index_sort
485 507 get :index, :sort => 'tracker,id:desc'
486 508 assert_response :success
487 509
488 510 sort_params = @request.session['issues_index_sort']
489 511 assert sort_params.is_a?(String)
490 512 assert_equal 'tracker,id:desc', sort_params
491 513
492 514 issues = assigns(:issues)
493 515 assert_not_nil issues
494 516 assert !issues.empty?
495 517 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
496 518 end
497 519
498 520 def test_index_sort_by_field_not_included_in_columns
499 521 Setting.issue_list_default_columns = %w(subject author)
500 522 get :index, :sort => 'tracker'
501 523 end
502 524
503 525 def test_index_sort_by_assigned_to
504 526 get :index, :sort => 'assigned_to'
505 527 assert_response :success
506 528 assignees = assigns(:issues).collect(&:assigned_to).compact
507 529 assert_equal assignees.sort, assignees
508 530 end
509 531
510 532 def test_index_sort_by_assigned_to_desc
511 533 get :index, :sort => 'assigned_to:desc'
512 534 assert_response :success
513 535 assignees = assigns(:issues).collect(&:assigned_to).compact
514 536 assert_equal assignees.sort.reverse, assignees
515 537 end
516 538
517 539 def test_index_group_by_assigned_to
518 540 get :index, :group_by => 'assigned_to', :sort => 'priority'
519 541 assert_response :success
520 542 end
521 543
522 544 def test_index_sort_by_author
523 545 get :index, :sort => 'author'
524 546 assert_response :success
525 547 authors = assigns(:issues).collect(&:author)
526 548 assert_equal authors.sort, authors
527 549 end
528 550
529 551 def test_index_sort_by_author_desc
530 552 get :index, :sort => 'author:desc'
531 553 assert_response :success
532 554 authors = assigns(:issues).collect(&:author)
533 555 assert_equal authors.sort.reverse, authors
534 556 end
535 557
536 558 def test_index_group_by_author
537 559 get :index, :group_by => 'author', :sort => 'priority'
538 560 assert_response :success
539 561 end
540 562
541 563 def test_index_with_columns
542 564 columns = ['tracker', 'subject', 'assigned_to']
543 565 get :index, :set_filter => 1, :c => columns
544 566 assert_response :success
545 567
546 568 # query should use specified columns
547 569 query = assigns(:query)
548 570 assert_kind_of Query, query
549 571 assert_equal columns, query.column_names.map(&:to_s)
550 572
551 573 # columns should be stored in session
552 574 assert_kind_of Hash, session[:query]
553 575 assert_kind_of Array, session[:query][:column_names]
554 576 assert_equal columns, session[:query][:column_names].map(&:to_s)
555 577
556 578 # ensure only these columns are kept in the selected columns list
557 579 assert_tag :tag => 'select', :attributes => { :id => 'selected_columns' },
558 580 :children => { :count => 3 }
559 581 assert_no_tag :tag => 'option', :attributes => { :value => 'project' },
560 582 :parent => { :tag => 'select', :attributes => { :id => "selected_columns" } }
561 583 end
562 584
563 585 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
564 586 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
565 587 get :index, :set_filter => 1
566 588
567 589 # query should use specified columns
568 590 query = assigns(:query)
569 591 assert_kind_of Query, query
570 592 assert_equal [:project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
571 593 end
572 594
573 595 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
574 596 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
575 597 columns = ['tracker', 'subject', 'assigned_to']
576 598 get :index, :set_filter => 1, :c => columns
577 599
578 600 # query should use specified columns
579 601 query = assigns(:query)
580 602 assert_kind_of Query, query
581 603 assert_equal columns.map(&:to_sym), query.columns.map(&:name)
582 604 end
583 605
584 606 def test_index_with_custom_field_column
585 607 columns = %w(tracker subject cf_2)
586 608 get :index, :set_filter => 1, :c => columns
587 609 assert_response :success
588 610
589 611 # query should use specified columns
590 612 query = assigns(:query)
591 613 assert_kind_of Query, query
592 614 assert_equal columns, query.column_names.map(&:to_s)
593 615
594 616 assert_tag :td,
595 617 :attributes => {:class => 'cf_2 string'},
596 618 :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
597 619 end
598 620
599 621 def test_index_with_date_column
600 622 Issue.find(1).update_attribute :start_date, '1987-08-24'
601 623
602 624 with_settings :date_format => '%d/%m/%Y' do
603 625 get :index, :set_filter => 1, :c => %w(start_date)
604 626 assert_tag 'td', :attributes => {:class => /start_date/}, :content => '24/08/1987'
605 627 end
606 628 end
607 629
608 630 def test_index_with_done_ratio
609 631 Issue.find(1).update_attribute :done_ratio, 40
610 632
611 633 get :index, :set_filter => 1, :c => %w(done_ratio)
612 634 assert_tag 'td', :attributes => {:class => /done_ratio/},
613 635 :child => {:tag => 'table', :attributes => {:class => 'progress'},
614 636 :descendant => {:tag => 'td', :attributes => {:class => 'closed', :style => 'width: 40%;'}}
615 637 }
616 638 end
617 639
618 640 def test_index_with_fixed_version
619 641 get :index, :set_filter => 1, :c => %w(fixed_version)
620 642 assert_tag 'td', :attributes => {:class => /fixed_version/},
621 643 :child => {:tag => 'a', :content => '1.0', :attributes => {:href => '/versions/2'}}
622 644 end
623 645
624 646 def test_index_send_html_if_query_is_invalid
625 647 get :index, :f => ['start_date'], :op => {:start_date => '='}
626 648 assert_equal 'text/html', @response.content_type
627 649 assert_template 'index'
628 650 end
629 651
630 652 def test_index_send_nothing_if_query_is_invalid
631 653 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
632 654 assert_equal 'text/csv', @response.content_type
633 655 assert @response.body.blank?
634 656 end
635 657
636 658 def test_show_by_anonymous
637 659 get :show, :id => 1
638 660 assert_response :success
639 661 assert_template 'show'
640 662 assert_not_nil assigns(:issue)
641 663 assert_equal Issue.find(1), assigns(:issue)
642 664
643 665 # anonymous role is allowed to add a note
644 666 assert_tag :tag => 'form',
645 667 :descendant => { :tag => 'fieldset',
646 668 :child => { :tag => 'legend',
647 669 :content => /Notes/ } }
648 670 assert_tag :tag => 'title',
649 671 :content => "Bug #1: Can't print recipes - eCookbook - Redmine"
650 672 end
651 673
652 674 def test_show_by_manager
653 675 @request.session[:user_id] = 2
654 676 get :show, :id => 1
655 677 assert_response :success
656 678
657 679 assert_tag :tag => 'a',
658 680 :content => /Quote/
659 681
660 682 assert_tag :tag => 'form',
661 683 :descendant => { :tag => 'fieldset',
662 684 :child => { :tag => 'legend',
663 685 :content => /Change properties/ } },
664 686 :descendant => { :tag => 'fieldset',
665 687 :child => { :tag => 'legend',
666 688 :content => /Log time/ } },
667 689 :descendant => { :tag => 'fieldset',
668 690 :child => { :tag => 'legend',
669 691 :content => /Notes/ } }
670 692 end
671 693
672 694 def test_update_form_should_not_display_inactive_enumerations
673 695 @request.session[:user_id] = 2
674 696 get :show, :id => 1
675 697 assert_response :success
676 698
677 699 assert ! IssuePriority.find(15).active?
678 700 assert_no_tag :option, :attributes => {:value => '15'},
679 701 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
680 702 end
681 703
682 704 def test_update_form_should_allow_attachment_upload
683 705 @request.session[:user_id] = 2
684 706 get :show, :id => 1
685 707
686 708 assert_tag :tag => 'form',
687 709 :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
688 710 :descendant => {
689 711 :tag => 'input',
690 712 :attributes => {:type => 'file', :name => 'attachments[1][file]'}
691 713 }
692 714 end
693 715
694 716 def test_show_should_deny_anonymous_access_without_permission
695 717 Role.anonymous.remove_permission!(:view_issues)
696 718 get :show, :id => 1
697 719 assert_response :redirect
698 720 end
699 721
700 722 def test_show_should_deny_anonymous_access_to_private_issue
701 723 Issue.update_all(["is_private = ?", true], "id = 1")
702 724 get :show, :id => 1
703 725 assert_response :redirect
704 726 end
705 727
706 728 def test_show_should_deny_non_member_access_without_permission
707 729 Role.non_member.remove_permission!(:view_issues)
708 730 @request.session[:user_id] = 9
709 731 get :show, :id => 1
710 732 assert_response 403
711 733 end
712 734
713 735 def test_show_should_deny_non_member_access_to_private_issue
714 736 Issue.update_all(["is_private = ?", true], "id = 1")
715 737 @request.session[:user_id] = 9
716 738 get :show, :id => 1
717 739 assert_response 403
718 740 end
719 741
720 742 def test_show_should_deny_member_access_without_permission
721 743 Role.find(1).remove_permission!(:view_issues)
722 744 @request.session[:user_id] = 2
723 745 get :show, :id => 1
724 746 assert_response 403
725 747 end
726 748
727 749 def test_show_should_deny_member_access_to_private_issue_without_permission
728 750 Issue.update_all(["is_private = ?", true], "id = 1")
729 751 @request.session[:user_id] = 3
730 752 get :show, :id => 1
731 753 assert_response 403
732 754 end
733 755
734 756 def test_show_should_allow_author_access_to_private_issue
735 757 Issue.update_all(["is_private = ?, author_id = 3", true], "id = 1")
736 758 @request.session[:user_id] = 3
737 759 get :show, :id => 1
738 760 assert_response :success
739 761 end
740 762
741 763 def test_show_should_allow_assignee_access_to_private_issue
742 764 Issue.update_all(["is_private = ?, assigned_to_id = 3", true], "id = 1")
743 765 @request.session[:user_id] = 3
744 766 get :show, :id => 1
745 767 assert_response :success
746 768 end
747 769
748 770 def test_show_should_allow_member_access_to_private_issue_with_permission
749 771 Issue.update_all(["is_private = ?", true], "id = 1")
750 772 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
751 773 @request.session[:user_id] = 3
752 774 get :show, :id => 1
753 775 assert_response :success
754 776 end
755 777
756 778 def test_show_should_not_disclose_relations_to_invisible_issues
757 779 Setting.cross_project_issue_relations = '1'
758 780 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
759 781 # Relation to a private project issue
760 782 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
761 783
762 784 get :show, :id => 1
763 785 assert_response :success
764 786
765 787 assert_tag :div, :attributes => { :id => 'relations' },
766 788 :descendant => { :tag => 'a', :content => /#2$/ }
767 789 assert_no_tag :div, :attributes => { :id => 'relations' },
768 790 :descendant => { :tag => 'a', :content => /#4$/ }
769 791 end
770 792
771 793 def test_show_atom
772 794 get :show, :id => 2, :format => 'atom'
773 795 assert_response :success
774 796 assert_template 'journals/index'
775 797 # Inline image
776 798 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
777 799 end
778 800
779 801 def test_show_export_to_pdf
780 802 get :show, :id => 3, :format => 'pdf'
781 803 assert_response :success
782 804 assert_equal 'application/pdf', @response.content_type
783 805 assert @response.body.starts_with?('%PDF')
784 806 assert_not_nil assigns(:issue)
785 807 end
786 808
787 809 def test_get_new
788 810 @request.session[:user_id] = 2
789 811 get :new, :project_id => 1, :tracker_id => 1
790 812 assert_response :success
791 813 assert_template 'new'
792 814
793 815 assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
794 816 :value => 'Default string' }
795 817
796 818 # Be sure we don't display inactive IssuePriorities
797 819 assert ! IssuePriority.find(15).active?
798 820 assert_no_tag :option, :attributes => {:value => '15'},
799 821 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
800 822 end
801 823
802 824 def test_get_new_without_default_start_date_is_creation_date
803 825 Setting.default_issue_start_date_to_creation_date = 0
804 826
805 827 @request.session[:user_id] = 2
806 828 get :new, :project_id => 1, :tracker_id => 1
807 829 assert_response :success
808 830 assert_template 'new'
809 831
810 832 assert_tag :tag => 'input', :attributes => { :name => 'issue[start_date]',
811 833 :value => nil }
812 834 end
813 835
814 836 def test_get_new_with_default_start_date_is_creation_date
815 837 Setting.default_issue_start_date_to_creation_date = 1
816 838
817 839 @request.session[:user_id] = 2
818 840 get :new, :project_id => 1, :tracker_id => 1
819 841 assert_response :success
820 842 assert_template 'new'
821 843
822 844 assert_tag :tag => 'input', :attributes => { :name => 'issue[start_date]',
823 845 :value => Date.today.to_s }
824 846 end
825 847
826 848 def test_get_new_form_should_allow_attachment_upload
827 849 @request.session[:user_id] = 2
828 850 get :new, :project_id => 1, :tracker_id => 1
829 851
830 852 assert_tag :tag => 'form',
831 853 :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
832 854 :descendant => {
833 855 :tag => 'input',
834 856 :attributes => {:type => 'file', :name => 'attachments[1][file]'}
835 857 }
836 858 end
837 859
838 860 def test_get_new_without_tracker_id
839 861 @request.session[:user_id] = 2
840 862 get :new, :project_id => 1
841 863 assert_response :success
842 864 assert_template 'new'
843 865
844 866 issue = assigns(:issue)
845 867 assert_not_nil issue
846 868 assert_equal Project.find(1).trackers.first, issue.tracker
847 869 end
848 870
849 871 def test_get_new_with_no_default_status_should_display_an_error
850 872 @request.session[:user_id] = 2
851 873 IssueStatus.delete_all
852 874
853 875 get :new, :project_id => 1
854 876 assert_response 500
855 877 assert_error_tag :content => /No default issue/
856 878 end
857 879
858 880 def test_get_new_with_no_tracker_should_display_an_error
859 881 @request.session[:user_id] = 2
860 882 Tracker.delete_all
861 883
862 884 get :new, :project_id => 1
863 885 assert_response 500
864 886 assert_error_tag :content => /No tracker/
865 887 end
866 888
867 889 def test_update_new_form
868 890 @request.session[:user_id] = 2
869 891 xhr :post, :new, :project_id => 1,
870 892 :issue => {:tracker_id => 2,
871 893 :subject => 'This is the test_new issue',
872 894 :description => 'This is the description',
873 895 :priority_id => 5}
874 896 assert_response :success
875 897 assert_template 'attributes'
876 898
877 899 issue = assigns(:issue)
878 900 assert_kind_of Issue, issue
879 901 assert_equal 1, issue.project_id
880 902 assert_equal 2, issue.tracker_id
881 903 assert_equal 'This is the test_new issue', issue.subject
882 904 end
883 905
884 906 def test_post_create
885 907 @request.session[:user_id] = 2
886 908 assert_difference 'Issue.count' do
887 909 post :create, :project_id => 1,
888 910 :issue => {:tracker_id => 3,
889 911 :status_id => 2,
890 912 :subject => 'This is the test_new issue',
891 913 :description => 'This is the description',
892 914 :priority_id => 5,
893 915 :start_date => '2010-11-07',
894 916 :estimated_hours => '',
895 917 :custom_field_values => {'2' => 'Value for field 2'}}
896 918 end
897 919 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
898 920
899 921 issue = Issue.find_by_subject('This is the test_new issue')
900 922 assert_not_nil issue
901 923 assert_equal 2, issue.author_id
902 924 assert_equal 3, issue.tracker_id
903 925 assert_equal 2, issue.status_id
904 926 assert_equal Date.parse('2010-11-07'), issue.start_date
905 927 assert_nil issue.estimated_hours
906 928 v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
907 929 assert_not_nil v
908 930 assert_equal 'Value for field 2', v.value
909 931 end
910 932
911 933 def test_post_new_with_group_assignment
912 934 group = Group.find(11)
913 935 project = Project.find(1)
914 936 project.members << Member.new(:principal => group, :roles => [Role.first])
915 937
916 938 with_settings :issue_group_assignment => '1' do
917 939 @request.session[:user_id] = 2
918 940 assert_difference 'Issue.count' do
919 941 post :create, :project_id => project.id,
920 942 :issue => {:tracker_id => 3,
921 943 :status_id => 1,
922 944 :subject => 'This is the test_new_with_group_assignment issue',
923 945 :assigned_to_id => group.id}
924 946 end
925 947 end
926 948 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
927 949
928 950 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
929 951 assert_not_nil issue
930 952 assert_equal group, issue.assigned_to
931 953 end
932 954
933 955 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
934 956 Setting.default_issue_start_date_to_creation_date = 0
935 957
936 958 @request.session[:user_id] = 2
937 959 assert_difference 'Issue.count' do
938 960 post :create, :project_id => 1,
939 961 :issue => {:tracker_id => 3,
940 962 :status_id => 2,
941 963 :subject => 'This is the test_new issue',
942 964 :description => 'This is the description',
943 965 :priority_id => 5,
944 966 :estimated_hours => '',
945 967 :custom_field_values => {'2' => 'Value for field 2'}}
946 968 end
947 969 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
948 970
949 971 issue = Issue.find_by_subject('This is the test_new issue')
950 972 assert_not_nil issue
951 973 assert_nil issue.start_date
952 974 end
953 975
954 976 def test_post_create_without_start_date_and_default_start_date_is_creation_date
955 977 Setting.default_issue_start_date_to_creation_date = 1
956 978
957 979 @request.session[:user_id] = 2
958 980 assert_difference 'Issue.count' do
959 981 post :create, :project_id => 1,
960 982 :issue => {:tracker_id => 3,
961 983 :status_id => 2,
962 984 :subject => 'This is the test_new issue',
963 985 :description => 'This is the description',
964 986 :priority_id => 5,
965 987 :estimated_hours => '',
966 988 :custom_field_values => {'2' => 'Value for field 2'}}
967 989 end
968 990 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
969 991
970 992 issue = Issue.find_by_subject('This is the test_new issue')
971 993 assert_not_nil issue
972 994 assert_equal Date.today, issue.start_date
973 995 end
974 996
975 997 def test_post_create_and_continue
976 998 @request.session[:user_id] = 2
977 999 assert_difference 'Issue.count' do
978 1000 post :create, :project_id => 1,
979 1001 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
980 1002 :continue => ''
981 1003 end
982 1004
983 1005 issue = Issue.first(:order => 'id DESC')
984 1006 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
985 1007 assert_not_nil flash[:notice], "flash was not set"
986 1008 assert flash[:notice].include?("<a href='/issues/#{issue.id}'>##{issue.id}</a>"), "issue link not found in flash: #{flash[:notice]}"
987 1009 end
988 1010
989 1011 def test_post_create_without_custom_fields_param
990 1012 @request.session[:user_id] = 2
991 1013 assert_difference 'Issue.count' do
992 1014 post :create, :project_id => 1,
993 1015 :issue => {:tracker_id => 1,
994 1016 :subject => 'This is the test_new issue',
995 1017 :description => 'This is the description',
996 1018 :priority_id => 5}
997 1019 end
998 1020 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
999 1021 end
1000 1022
1001 1023 def test_post_create_with_required_custom_field_and_without_custom_fields_param
1002 1024 field = IssueCustomField.find_by_name('Database')
1003 1025 field.update_attribute(:is_required, true)
1004 1026
1005 1027 @request.session[:user_id] = 2
1006 1028 post :create, :project_id => 1,
1007 1029 :issue => {:tracker_id => 1,
1008 1030 :subject => 'This is the test_new issue',
1009 1031 :description => 'This is the description',
1010 1032 :priority_id => 5}
1011 1033 assert_response :success
1012 1034 assert_template 'new'
1013 1035 issue = assigns(:issue)
1014 1036 assert_not_nil issue
1015 1037 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
1016 1038 end
1017 1039
1018 1040 def test_post_create_with_watchers
1019 1041 @request.session[:user_id] = 2
1020 1042 ActionMailer::Base.deliveries.clear
1021 1043
1022 1044 assert_difference 'Watcher.count', 2 do
1023 1045 post :create, :project_id => 1,
1024 1046 :issue => {:tracker_id => 1,
1025 1047 :subject => 'This is a new issue with watchers',
1026 1048 :description => 'This is the description',
1027 1049 :priority_id => 5,
1028 1050 :watcher_user_ids => ['2', '3']}
1029 1051 end
1030 1052 issue = Issue.find_by_subject('This is a new issue with watchers')
1031 1053 assert_not_nil issue
1032 1054 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1033 1055
1034 1056 # Watchers added
1035 1057 assert_equal [2, 3], issue.watcher_user_ids.sort
1036 1058 assert issue.watched_by?(User.find(3))
1037 1059 # Watchers notified
1038 1060 mail = ActionMailer::Base.deliveries.last
1039 1061 assert_kind_of TMail::Mail, mail
1040 1062 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
1041 1063 end
1042 1064
1043 1065 def test_post_create_subissue
1044 1066 @request.session[:user_id] = 2
1045 1067
1046 1068 assert_difference 'Issue.count' do
1047 1069 post :create, :project_id => 1,
1048 1070 :issue => {:tracker_id => 1,
1049 1071 :subject => 'This is a child issue',
1050 1072 :parent_issue_id => 2}
1051 1073 end
1052 1074 issue = Issue.find_by_subject('This is a child issue')
1053 1075 assert_not_nil issue
1054 1076 assert_equal Issue.find(2), issue.parent
1055 1077 end
1056 1078
1057 1079 def test_post_create_subissue_with_non_numeric_parent_id
1058 1080 @request.session[:user_id] = 2
1059 1081
1060 1082 assert_difference 'Issue.count' do
1061 1083 post :create, :project_id => 1,
1062 1084 :issue => {:tracker_id => 1,
1063 1085 :subject => 'This is a child issue',
1064 1086 :parent_issue_id => 'ABC'}
1065 1087 end
1066 1088 issue = Issue.find_by_subject('This is a child issue')
1067 1089 assert_not_nil issue
1068 1090 assert_nil issue.parent
1069 1091 end
1070 1092
1071 1093 def test_post_create_private
1072 1094 @request.session[:user_id] = 2
1073 1095
1074 1096 assert_difference 'Issue.count' do
1075 1097 post :create, :project_id => 1,
1076 1098 :issue => {:tracker_id => 1,
1077 1099 :subject => 'This is a private issue',
1078 1100 :is_private => '1'}
1079 1101 end
1080 1102 issue = Issue.first(:order => 'id DESC')
1081 1103 assert issue.is_private?
1082 1104 end
1083 1105
1084 1106 def test_post_create_private_with_set_own_issues_private_permission
1085 1107 role = Role.find(1)
1086 1108 role.remove_permission! :set_issues_private
1087 1109 role.add_permission! :set_own_issues_private
1088 1110
1089 1111 @request.session[:user_id] = 2
1090 1112
1091 1113 assert_difference 'Issue.count' do
1092 1114 post :create, :project_id => 1,
1093 1115 :issue => {:tracker_id => 1,
1094 1116 :subject => 'This is a private issue',
1095 1117 :is_private => '1'}
1096 1118 end
1097 1119 issue = Issue.first(:order => 'id DESC')
1098 1120 assert issue.is_private?
1099 1121 end
1100 1122
1101 1123 def test_post_create_should_send_a_notification
1102 1124 ActionMailer::Base.deliveries.clear
1103 1125 @request.session[:user_id] = 2
1104 1126 assert_difference 'Issue.count' do
1105 1127 post :create, :project_id => 1,
1106 1128 :issue => {:tracker_id => 3,
1107 1129 :subject => 'This is the test_new issue',
1108 1130 :description => 'This is the description',
1109 1131 :priority_id => 5,
1110 1132 :estimated_hours => '',
1111 1133 :custom_field_values => {'2' => 'Value for field 2'}}
1112 1134 end
1113 1135 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1114 1136
1115 1137 assert_equal 1, ActionMailer::Base.deliveries.size
1116 1138 end
1117 1139
1118 1140 def test_post_create_should_preserve_fields_values_on_validation_failure
1119 1141 @request.session[:user_id] = 2
1120 1142 post :create, :project_id => 1,
1121 1143 :issue => {:tracker_id => 1,
1122 1144 # empty subject
1123 1145 :subject => '',
1124 1146 :description => 'This is a description',
1125 1147 :priority_id => 6,
1126 1148 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
1127 1149 assert_response :success
1128 1150 assert_template 'new'
1129 1151
1130 1152 assert_tag :textarea, :attributes => { :name => 'issue[description]' },
1131 1153 :content => 'This is a description'
1132 1154 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
1133 1155 :child => { :tag => 'option', :attributes => { :selected => 'selected',
1134 1156 :value => '6' },
1135 1157 :content => 'High' }
1136 1158 # Custom fields
1137 1159 assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
1138 1160 :child => { :tag => 'option', :attributes => { :selected => 'selected',
1139 1161 :value => 'Oracle' },
1140 1162 :content => 'Oracle' }
1141 1163 assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
1142 1164 :value => 'Value for field 2'}
1143 1165 end
1144 1166
1145 1167 def test_post_create_should_ignore_non_safe_attributes
1146 1168 @request.session[:user_id] = 2
1147 1169 assert_nothing_raised do
1148 1170 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
1149 1171 end
1150 1172 end
1151 1173
1152 1174 def test_post_create_with_attachment
1153 1175 set_tmp_attachments_directory
1154 1176 @request.session[:user_id] = 2
1155 1177
1156 1178 assert_difference 'Issue.count' do
1157 1179 assert_difference 'Attachment.count' do
1158 1180 post :create, :project_id => 1,
1159 1181 :issue => { :tracker_id => '1', :subject => 'With attachment' },
1160 1182 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
1161 1183 end
1162 1184 end
1163 1185
1164 1186 issue = Issue.first(:order => 'id DESC')
1165 1187 attachment = Attachment.first(:order => 'id DESC')
1166 1188
1167 1189 assert_equal issue, attachment.container
1168 1190 assert_equal 2, attachment.author_id
1169 1191 assert_equal 'testfile.txt', attachment.filename
1170 1192 assert_equal 'text/plain', attachment.content_type
1171 1193 assert_equal 'test file', attachment.description
1172 1194 assert_equal 59, attachment.filesize
1173 1195 assert File.exists?(attachment.diskfile)
1174 1196 assert_equal 59, File.size(attachment.diskfile)
1175 1197 end
1176 1198
1177 1199 context "without workflow privilege" do
1178 1200 setup do
1179 1201 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
1180 1202 Role.anonymous.add_permission! :add_issues, :add_issue_notes
1181 1203 end
1182 1204
1183 1205 context "#new" do
1184 1206 should "propose default status only" do
1185 1207 get :new, :project_id => 1
1186 1208 assert_response :success
1187 1209 assert_template 'new'
1188 1210 assert_tag :tag => 'select',
1189 1211 :attributes => {:name => 'issue[status_id]'},
1190 1212 :children => {:count => 1},
1191 1213 :child => {:tag => 'option', :attributes => {:value => IssueStatus.default.id.to_s}}
1192 1214 end
1193 1215
1194 1216 should "accept default status" do
1195 1217 assert_difference 'Issue.count' do
1196 1218 post :create, :project_id => 1,
1197 1219 :issue => {:tracker_id => 1,
1198 1220 :subject => 'This is an issue',
1199 1221 :status_id => 1}
1200 1222 end
1201 1223 issue = Issue.last(:order => 'id')
1202 1224 assert_equal IssueStatus.default, issue.status
1203 1225 end
1204 1226
1205 1227 should "ignore unauthorized status" do
1206 1228 assert_difference 'Issue.count' do
1207 1229 post :create, :project_id => 1,
1208 1230 :issue => {:tracker_id => 1,
1209 1231 :subject => 'This is an issue',
1210 1232 :status_id => 3}
1211 1233 end
1212 1234 issue = Issue.last(:order => 'id')
1213 1235 assert_equal IssueStatus.default, issue.status
1214 1236 end
1215 1237 end
1216 1238
1217 1239 context "#update" do
1218 1240 should "ignore status change" do
1219 1241 assert_difference 'Journal.count' do
1220 1242 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
1221 1243 end
1222 1244 assert_equal 1, Issue.find(1).status_id
1223 1245 end
1224 1246
1225 1247 should "ignore attributes changes" do
1226 1248 assert_difference 'Journal.count' do
1227 1249 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
1228 1250 end
1229 1251 issue = Issue.find(1)
1230 1252 assert_equal "Can't print recipes", issue.subject
1231 1253 assert_nil issue.assigned_to
1232 1254 end
1233 1255 end
1234 1256 end
1235 1257
1236 1258 context "with workflow privilege" do
1237 1259 setup do
1238 1260 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
1239 1261 Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
1240 1262 Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
1241 1263 Role.anonymous.add_permission! :add_issues, :add_issue_notes
1242 1264 end
1243 1265
1244 1266 context "#update" do
1245 1267 should "accept authorized status" do
1246 1268 assert_difference 'Journal.count' do
1247 1269 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
1248 1270 end
1249 1271 assert_equal 3, Issue.find(1).status_id
1250 1272 end
1251 1273
1252 1274 should "ignore unauthorized status" do
1253 1275 assert_difference 'Journal.count' do
1254 1276 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
1255 1277 end
1256 1278 assert_equal 1, Issue.find(1).status_id
1257 1279 end
1258 1280
1259 1281 should "accept authorized attributes changes" do
1260 1282 assert_difference 'Journal.count' do
1261 1283 put :update, :id => 1, :notes => 'just trying', :issue => {:assigned_to_id => 2}
1262 1284 end
1263 1285 issue = Issue.find(1)
1264 1286 assert_equal 2, issue.assigned_to_id
1265 1287 end
1266 1288
1267 1289 should "ignore unauthorized attributes changes" do
1268 1290 assert_difference 'Journal.count' do
1269 1291 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed'}
1270 1292 end
1271 1293 issue = Issue.find(1)
1272 1294 assert_equal "Can't print recipes", issue.subject
1273 1295 end
1274 1296 end
1275 1297
1276 1298 context "and :edit_issues permission" do
1277 1299 setup do
1278 1300 Role.anonymous.add_permission! :add_issues, :edit_issues
1279 1301 end
1280 1302
1281 1303 should "accept authorized status" do
1282 1304 assert_difference 'Journal.count' do
1283 1305 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
1284 1306 end
1285 1307 assert_equal 3, Issue.find(1).status_id
1286 1308 end
1287 1309
1288 1310 should "ignore unauthorized status" do
1289 1311 assert_difference 'Journal.count' do
1290 1312 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
1291 1313 end
1292 1314 assert_equal 1, Issue.find(1).status_id
1293 1315 end
1294 1316
1295 1317 should "accept authorized attributes changes" do
1296 1318 assert_difference 'Journal.count' do
1297 1319 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
1298 1320 end
1299 1321 issue = Issue.find(1)
1300 1322 assert_equal "changed", issue.subject
1301 1323 assert_equal 2, issue.assigned_to_id
1302 1324 end
1303 1325 end
1304 1326 end
1305 1327
1306 1328 def test_copy_issue
1307 1329 @request.session[:user_id] = 2
1308 1330 get :new, :project_id => 1, :copy_from => 1
1309 1331 assert_template 'new'
1310 1332 assert_not_nil assigns(:issue)
1311 1333 orig = Issue.find(1)
1312 1334 assert_equal orig.subject, assigns(:issue).subject
1313 1335 end
1314 1336
1315 1337 def test_get_edit
1316 1338 @request.session[:user_id] = 2
1317 1339 get :edit, :id => 1
1318 1340 assert_response :success
1319 1341 assert_template 'edit'
1320 1342 assert_not_nil assigns(:issue)
1321 1343 assert_equal Issue.find(1), assigns(:issue)
1322 1344
1323 1345 # Be sure we don't display inactive IssuePriorities
1324 1346 assert ! IssuePriority.find(15).active?
1325 1347 assert_no_tag :option, :attributes => {:value => '15'},
1326 1348 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
1327 1349 end
1328 1350
1329 1351 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
1330 1352 @request.session[:user_id] = 2
1331 1353 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
1332 1354
1333 1355 get :edit, :id => 1
1334 1356 assert_tag 'input', :attributes => {:name => 'time_entry[hours]'}
1335 1357 end
1336 1358
1337 1359 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
1338 1360 @request.session[:user_id] = 2
1339 1361 Role.find_by_name('Manager').remove_permission! :log_time
1340 1362
1341 1363 get :edit, :id => 1
1342 1364 assert_no_tag 'input', :attributes => {:name => 'time_entry[hours]'}
1343 1365 end
1344 1366
1345 1367 def test_get_edit_with_params
1346 1368 @request.session[:user_id] = 2
1347 1369 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
1348 1370 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => TimeEntryActivity.first.id }
1349 1371 assert_response :success
1350 1372 assert_template 'edit'
1351 1373
1352 1374 issue = assigns(:issue)
1353 1375 assert_not_nil issue
1354 1376
1355 1377 assert_equal 5, issue.status_id
1356 1378 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
1357 1379 :child => { :tag => 'option',
1358 1380 :content => 'Closed',
1359 1381 :attributes => { :selected => 'selected' } }
1360 1382
1361 1383 assert_equal 7, issue.priority_id
1362 1384 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
1363 1385 :child => { :tag => 'option',
1364 1386 :content => 'Urgent',
1365 1387 :attributes => { :selected => 'selected' } }
1366 1388
1367 1389 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => '2.5' }
1368 1390 assert_tag :select, :attributes => { :name => 'time_entry[activity_id]' },
1369 1391 :child => { :tag => 'option',
1370 1392 :attributes => { :selected => 'selected', :value => TimeEntryActivity.first.id } }
1371 1393 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => 'test_get_edit_with_params' }
1372 1394 end
1373 1395
1374 1396 def test_update_edit_form
1375 1397 @request.session[:user_id] = 2
1376 1398 xhr :post, :new, :project_id => 1,
1377 1399 :id => 1,
1378 1400 :issue => {:tracker_id => 2,
1379 1401 :subject => 'This is the test_new issue',
1380 1402 :description => 'This is the description',
1381 1403 :priority_id => 5}
1382 1404 assert_response :success
1383 1405 assert_template 'attributes'
1384 1406
1385 1407 issue = assigns(:issue)
1386 1408 assert_kind_of Issue, issue
1387 1409 assert_equal 1, issue.id
1388 1410 assert_equal 1, issue.project_id
1389 1411 assert_equal 2, issue.tracker_id
1390 1412 assert_equal 'This is the test_new issue', issue.subject
1391 1413 end
1392 1414
1393 1415 def test_update_using_invalid_http_verbs
1394 1416 @request.session[:user_id] = 2
1395 1417 subject = 'Updated by an invalid http verb'
1396 1418
1397 1419 get :update, :id => 1, :issue => {:subject => subject}
1398 1420 assert_not_equal subject, Issue.find(1).subject
1399 1421
1400 1422 post :update, :id => 1, :issue => {:subject => subject}
1401 1423 assert_not_equal subject, Issue.find(1).subject
1402 1424
1403 1425 delete :update, :id => 1, :issue => {:subject => subject}
1404 1426 assert_not_equal subject, Issue.find(1).subject
1405 1427 end
1406 1428
1407 1429 def test_put_update_without_custom_fields_param
1408 1430 @request.session[:user_id] = 2
1409 1431 ActionMailer::Base.deliveries.clear
1410 1432
1411 1433 issue = Issue.find(1)
1412 1434 assert_equal '125', issue.custom_value_for(2).value
1413 1435 old_subject = issue.subject
1414 1436 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
1415 1437
1416 1438 assert_difference('Journal.count') do
1417 1439 assert_difference('JournalDetail.count', 2) do
1418 1440 put :update, :id => 1, :issue => {:subject => new_subject,
1419 1441 :priority_id => '6',
1420 1442 :category_id => '1' # no change
1421 1443 }
1422 1444 end
1423 1445 end
1424 1446 assert_redirected_to :action => 'show', :id => '1'
1425 1447 issue.reload
1426 1448 assert_equal new_subject, issue.subject
1427 1449 # Make sure custom fields were not cleared
1428 1450 assert_equal '125', issue.custom_value_for(2).value
1429 1451
1430 1452 mail = ActionMailer::Base.deliveries.last
1431 1453 assert_kind_of TMail::Mail, mail
1432 1454 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
1433 1455 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
1434 1456 end
1435 1457
1436 1458 def test_put_update_with_custom_field_change
1437 1459 @request.session[:user_id] = 2
1438 1460 issue = Issue.find(1)
1439 1461 assert_equal '125', issue.custom_value_for(2).value
1440 1462
1441 1463 assert_difference('Journal.count') do
1442 1464 assert_difference('JournalDetail.count', 3) do
1443 1465 put :update, :id => 1, :issue => {:subject => 'Custom field change',
1444 1466 :priority_id => '6',
1445 1467 :category_id => '1', # no change
1446 1468 :custom_field_values => { '2' => 'New custom value' }
1447 1469 }
1448 1470 end
1449 1471 end
1450 1472 assert_redirected_to :action => 'show', :id => '1'
1451 1473 issue.reload
1452 1474 assert_equal 'New custom value', issue.custom_value_for(2).value
1453 1475
1454 1476 mail = ActionMailer::Base.deliveries.last
1455 1477 assert_kind_of TMail::Mail, mail
1456 1478 assert mail.body.include?("Searchable field changed from 125 to New custom value")
1457 1479 end
1458 1480
1459 1481 def test_put_update_with_status_and_assignee_change
1460 1482 issue = Issue.find(1)
1461 1483 assert_equal 1, issue.status_id
1462 1484 @request.session[:user_id] = 2
1463 1485 assert_difference('TimeEntry.count', 0) do
1464 1486 put :update,
1465 1487 :id => 1,
1466 1488 :issue => { :status_id => 2, :assigned_to_id => 3 },
1467 1489 :notes => 'Assigned to dlopper',
1468 1490 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
1469 1491 end
1470 1492 assert_redirected_to :action => 'show', :id => '1'
1471 1493 issue.reload
1472 1494 assert_equal 2, issue.status_id
1473 1495 j = Journal.find(:first, :order => 'id DESC')
1474 1496 assert_equal 'Assigned to dlopper', j.notes
1475 1497 assert_equal 2, j.details.size
1476 1498
1477 1499 mail = ActionMailer::Base.deliveries.last
1478 1500 assert mail.body.include?("Status changed from New to Assigned")
1479 1501 # subject should contain the new status
1480 1502 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
1481 1503 end
1482 1504
1483 1505 def test_put_update_with_note_only
1484 1506 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
1485 1507 # anonymous user
1486 1508 put :update,
1487 1509 :id => 1,
1488 1510 :notes => notes
1489 1511 assert_redirected_to :action => 'show', :id => '1'
1490 1512 j = Journal.find(:first, :order => 'id DESC')
1491 1513 assert_equal notes, j.notes
1492 1514 assert_equal 0, j.details.size
1493 1515 assert_equal User.anonymous, j.user
1494 1516
1495 1517 mail = ActionMailer::Base.deliveries.last
1496 1518 assert mail.body.include?(notes)
1497 1519 end
1498 1520
1499 1521 def test_put_update_with_note_and_spent_time
1500 1522 @request.session[:user_id] = 2
1501 1523 spent_hours_before = Issue.find(1).spent_hours
1502 1524 assert_difference('TimeEntry.count') do
1503 1525 put :update,
1504 1526 :id => 1,
1505 1527 :notes => '2.5 hours added',
1506 1528 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
1507 1529 end
1508 1530 assert_redirected_to :action => 'show', :id => '1'
1509 1531
1510 1532 issue = Issue.find(1)
1511 1533
1512 1534 j = Journal.find(:first, :order => 'id DESC')
1513 1535 assert_equal '2.5 hours added', j.notes
1514 1536 assert_equal 0, j.details.size
1515 1537
1516 1538 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
1517 1539 assert_not_nil t
1518 1540 assert_equal 2.5, t.hours
1519 1541 assert_equal spent_hours_before + 2.5, issue.spent_hours
1520 1542 end
1521 1543
1522 1544 def test_put_update_with_attachment_only
1523 1545 set_tmp_attachments_directory
1524 1546
1525 1547 # Delete all fixtured journals, a race condition can occur causing the wrong
1526 1548 # journal to get fetched in the next find.
1527 1549 Journal.delete_all
1528 1550
1529 1551 # anonymous user
1530 1552 assert_difference 'Attachment.count' do
1531 1553 put :update, :id => 1,
1532 1554 :notes => '',
1533 1555 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
1534 1556 end
1535 1557
1536 1558 assert_redirected_to :action => 'show', :id => '1'
1537 1559 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
1538 1560 assert j.notes.blank?
1539 1561 assert_equal 1, j.details.size
1540 1562 assert_equal 'testfile.txt', j.details.first.value
1541 1563 assert_equal User.anonymous, j.user
1542 1564
1543 1565 attachment = Attachment.first(:order => 'id DESC')
1544 1566 assert_equal Issue.find(1), attachment.container
1545 1567 assert_equal User.anonymous, attachment.author
1546 1568 assert_equal 'testfile.txt', attachment.filename
1547 1569 assert_equal 'text/plain', attachment.content_type
1548 1570 assert_equal 'test file', attachment.description
1549 1571 assert_equal 59, attachment.filesize
1550 1572 assert File.exists?(attachment.diskfile)
1551 1573 assert_equal 59, File.size(attachment.diskfile)
1552 1574
1553 1575 mail = ActionMailer::Base.deliveries.last
1554 1576 assert mail.body.include?('testfile.txt')
1555 1577 end
1556 1578
1557 1579 def test_put_update_with_attachment_that_fails_to_save
1558 1580 set_tmp_attachments_directory
1559 1581
1560 1582 # Delete all fixtured journals, a race condition can occur causing the wrong
1561 1583 # journal to get fetched in the next find.
1562 1584 Journal.delete_all
1563 1585
1564 1586 # Mock out the unsaved attachment
1565 1587 Attachment.any_instance.stubs(:create).returns(Attachment.new)
1566 1588
1567 1589 # anonymous user
1568 1590 put :update,
1569 1591 :id => 1,
1570 1592 :notes => '',
1571 1593 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
1572 1594 assert_redirected_to :action => 'show', :id => '1'
1573 1595 assert_equal '1 file(s) could not be saved.', flash[:warning]
1574 1596
1575 1597 end if Object.const_defined?(:Mocha)
1576 1598
1577 1599 def test_put_update_with_no_change
1578 1600 issue = Issue.find(1)
1579 1601 issue.journals.clear
1580 1602 ActionMailer::Base.deliveries.clear
1581 1603
1582 1604 put :update,
1583 1605 :id => 1,
1584 1606 :notes => ''
1585 1607 assert_redirected_to :action => 'show', :id => '1'
1586 1608
1587 1609 issue.reload
1588 1610 assert issue.journals.empty?
1589 1611 # No email should be sent
1590 1612 assert ActionMailer::Base.deliveries.empty?
1591 1613 end
1592 1614
1593 1615 def test_put_update_should_send_a_notification
1594 1616 @request.session[:user_id] = 2
1595 1617 ActionMailer::Base.deliveries.clear
1596 1618 issue = Issue.find(1)
1597 1619 old_subject = issue.subject
1598 1620 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
1599 1621
1600 1622 put :update, :id => 1, :issue => {:subject => new_subject,
1601 1623 :priority_id => '6',
1602 1624 :category_id => '1' # no change
1603 1625 }
1604 1626 assert_equal 1, ActionMailer::Base.deliveries.size
1605 1627 end
1606 1628
1607 1629 def test_put_update_with_invalid_spent_time_hours_only
1608 1630 @request.session[:user_id] = 2
1609 1631 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
1610 1632
1611 1633 assert_no_difference('Journal.count') do
1612 1634 put :update,
1613 1635 :id => 1,
1614 1636 :notes => notes,
1615 1637 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
1616 1638 end
1617 1639 assert_response :success
1618 1640 assert_template 'edit'
1619 1641
1620 1642 assert_error_tag :descendant => {:content => /Activity can't be blank/}
1621 1643 assert_tag :textarea, :attributes => { :name => 'notes' }, :content => notes
1622 1644 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
1623 1645 end
1624 1646
1625 1647 def test_put_update_with_invalid_spent_time_comments_only
1626 1648 @request.session[:user_id] = 2
1627 1649 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
1628 1650
1629 1651 assert_no_difference('Journal.count') do
1630 1652 put :update,
1631 1653 :id => 1,
1632 1654 :notes => notes,
1633 1655 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
1634 1656 end
1635 1657 assert_response :success
1636 1658 assert_template 'edit'
1637 1659
1638 1660 assert_error_tag :descendant => {:content => /Activity can't be blank/}
1639 1661 assert_error_tag :descendant => {:content => /Hours can't be blank/}
1640 1662 assert_tag :textarea, :attributes => { :name => 'notes' }, :content => notes
1641 1663 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => "this is my comment" }
1642 1664 end
1643 1665
1644 1666 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
1645 1667 issue = Issue.find(2)
1646 1668 @request.session[:user_id] = 2
1647 1669
1648 1670 put :update,
1649 1671 :id => issue.id,
1650 1672 :issue => {
1651 1673 :fixed_version_id => 4
1652 1674 }
1653 1675
1654 1676 assert_response :redirect
1655 1677 issue.reload
1656 1678 assert_equal 4, issue.fixed_version_id
1657 1679 assert_not_equal issue.project_id, issue.fixed_version.project_id
1658 1680 end
1659 1681
1660 1682 def test_put_update_should_redirect_back_using_the_back_url_parameter
1661 1683 issue = Issue.find(2)
1662 1684 @request.session[:user_id] = 2
1663 1685
1664 1686 put :update,
1665 1687 :id => issue.id,
1666 1688 :issue => {
1667 1689 :fixed_version_id => 4
1668 1690 },
1669 1691 :back_url => '/issues'
1670 1692
1671 1693 assert_response :redirect
1672 1694 assert_redirected_to '/issues'
1673 1695 end
1674 1696
1675 1697 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1676 1698 issue = Issue.find(2)
1677 1699 @request.session[:user_id] = 2
1678 1700
1679 1701 put :update,
1680 1702 :id => issue.id,
1681 1703 :issue => {
1682 1704 :fixed_version_id => 4
1683 1705 },
1684 1706 :back_url => 'http://google.com'
1685 1707
1686 1708 assert_response :redirect
1687 1709 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
1688 1710 end
1689 1711
1690 1712 def test_get_bulk_edit
1691 1713 @request.session[:user_id] = 2
1692 1714 get :bulk_edit, :ids => [1, 2]
1693 1715 assert_response :success
1694 1716 assert_template 'bulk_edit'
1695 1717
1696 1718 assert_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
1697 1719
1698 1720 # Project specific custom field, date type
1699 1721 field = CustomField.find(9)
1700 1722 assert !field.is_for_all?
1701 1723 assert_equal 'date', field.field_format
1702 1724 assert_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
1703 1725
1704 1726 # System wide custom field
1705 1727 assert CustomField.find(1).is_for_all?
1706 1728 assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
1707 1729
1708 1730 # Be sure we don't display inactive IssuePriorities
1709 1731 assert ! IssuePriority.find(15).active?
1710 1732 assert_no_tag :option, :attributes => {:value => '15'},
1711 1733 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
1712 1734 end
1713 1735
1714 1736 def test_get_bulk_edit_on_different_projects
1715 1737 @request.session[:user_id] = 2
1716 1738 get :bulk_edit, :ids => [1, 2, 6]
1717 1739 assert_response :success
1718 1740 assert_template 'bulk_edit'
1719 1741
1720 1742 # Can not set issues from different projects as children of an issue
1721 1743 assert_no_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
1722 1744
1723 1745 # Project specific custom field, date type
1724 1746 field = CustomField.find(9)
1725 1747 assert !field.is_for_all?
1726 1748 assert !field.project_ids.include?(Issue.find(6).project_id)
1727 1749 assert_no_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
1728 1750 end
1729 1751
1730 1752 def test_get_bulk_edit_with_user_custom_field
1731 1753 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true)
1732 1754
1733 1755 @request.session[:user_id] = 2
1734 1756 get :bulk_edit, :ids => [1, 2]
1735 1757 assert_response :success
1736 1758 assert_template 'bulk_edit'
1737 1759
1738 1760 assert_tag :select,
1739 1761 :attributes => {:name => "issue[custom_field_values][#{field.id}]"},
1740 1762 :children => {
1741 1763 :only => {:tag => 'option'},
1742 1764 :count => Project.find(1).users.count + 1
1743 1765 }
1744 1766 end
1745 1767
1746 1768 def test_get_bulk_edit_with_version_custom_field
1747 1769 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true)
1748 1770
1749 1771 @request.session[:user_id] = 2
1750 1772 get :bulk_edit, :ids => [1, 2]
1751 1773 assert_response :success
1752 1774 assert_template 'bulk_edit'
1753 1775
1754 1776 assert_tag :select,
1755 1777 :attributes => {:name => "issue[custom_field_values][#{field.id}]"},
1756 1778 :children => {
1757 1779 :only => {:tag => 'option'},
1758 1780 :count => Project.find(1).shared_versions.count + 1
1759 1781 }
1760 1782 end
1761 1783
1762 1784 def test_bulk_update
1763 1785 @request.session[:user_id] = 2
1764 1786 # update issues priority
1765 1787 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
1766 1788 :issue => {:priority_id => 7,
1767 1789 :assigned_to_id => '',
1768 1790 :custom_field_values => {'2' => ''}}
1769 1791
1770 1792 assert_response 302
1771 1793 # check that the issues were updated
1772 1794 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
1773 1795
1774 1796 issue = Issue.find(1)
1775 1797 journal = issue.journals.find(:first, :order => 'created_on DESC')
1776 1798 assert_equal '125', issue.custom_value_for(2).value
1777 1799 assert_equal 'Bulk editing', journal.notes
1778 1800 assert_equal 1, journal.details.size
1779 1801 end
1780 1802
1781 1803 def test_bulk_update_with_group_assignee
1782 1804 group = Group.find(11)
1783 1805 project = Project.find(1)
1784 1806 project.members << Member.new(:principal => group, :roles => [Role.first])
1785 1807
1786 1808 @request.session[:user_id] = 2
1787 1809 # update issues assignee
1788 1810 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
1789 1811 :issue => {:priority_id => '',
1790 1812 :assigned_to_id => group.id,
1791 1813 :custom_field_values => {'2' => ''}}
1792 1814
1793 1815 assert_response 302
1794 1816 assert_equal [group, group], Issue.find_all_by_id([1, 2]).collect {|i| i.assigned_to}
1795 1817 end
1796 1818
1797 1819 def test_bulk_update_on_different_projects
1798 1820 @request.session[:user_id] = 2
1799 1821 # update issues priority
1800 1822 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
1801 1823 :issue => {:priority_id => 7,
1802 1824 :assigned_to_id => '',
1803 1825 :custom_field_values => {'2' => ''}}
1804 1826
1805 1827 assert_response 302
1806 1828 # check that the issues were updated
1807 1829 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
1808 1830
1809 1831 issue = Issue.find(1)
1810 1832 journal = issue.journals.find(:first, :order => 'created_on DESC')
1811 1833 assert_equal '125', issue.custom_value_for(2).value
1812 1834 assert_equal 'Bulk editing', journal.notes
1813 1835 assert_equal 1, journal.details.size
1814 1836 end
1815 1837
1816 1838 def test_bulk_update_on_different_projects_without_rights
1817 1839 @request.session[:user_id] = 3
1818 1840 user = User.find(3)
1819 1841 action = { :controller => "issues", :action => "bulk_update" }
1820 1842 assert user.allowed_to?(action, Issue.find(1).project)
1821 1843 assert ! user.allowed_to?(action, Issue.find(6).project)
1822 1844 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
1823 1845 :issue => {:priority_id => 7,
1824 1846 :assigned_to_id => '',
1825 1847 :custom_field_values => {'2' => ''}}
1826 1848 assert_response 403
1827 1849 assert_not_equal "Bulk should fail", Journal.last.notes
1828 1850 end
1829 1851
1830 1852 def test_bullk_update_should_send_a_notification
1831 1853 @request.session[:user_id] = 2
1832 1854 ActionMailer::Base.deliveries.clear
1833 1855 post(:bulk_update,
1834 1856 {
1835 1857 :ids => [1, 2],
1836 1858 :notes => 'Bulk editing',
1837 1859 :issue => {
1838 1860 :priority_id => 7,
1839 1861 :assigned_to_id => '',
1840 1862 :custom_field_values => {'2' => ''}
1841 1863 }
1842 1864 })
1843 1865
1844 1866 assert_response 302
1845 1867 assert_equal 2, ActionMailer::Base.deliveries.size
1846 1868 end
1847 1869
1848 1870 def test_bulk_update_status
1849 1871 @request.session[:user_id] = 2
1850 1872 # update issues priority
1851 1873 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
1852 1874 :issue => {:priority_id => '',
1853 1875 :assigned_to_id => '',
1854 1876 :status_id => '5'}
1855 1877
1856 1878 assert_response 302
1857 1879 issue = Issue.find(1)
1858 1880 assert issue.closed?
1859 1881 end
1860 1882
1861 1883 def test_bulk_update_parent_id
1862 1884 @request.session[:user_id] = 2
1863 1885 post :bulk_update, :ids => [1, 3],
1864 1886 :notes => 'Bulk editing parent',
1865 1887 :issue => {:priority_id => '', :assigned_to_id => '', :status_id => '', :parent_issue_id => '2'}
1866 1888
1867 1889 assert_response 302
1868 1890 parent = Issue.find(2)
1869 1891 assert_equal parent.id, Issue.find(1).parent_id
1870 1892 assert_equal parent.id, Issue.find(3).parent_id
1871 1893 assert_equal [1, 3], parent.children.collect(&:id).sort
1872 1894 end
1873 1895
1874 1896 def test_bulk_update_custom_field
1875 1897 @request.session[:user_id] = 2
1876 1898 # update issues priority
1877 1899 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
1878 1900 :issue => {:priority_id => '',
1879 1901 :assigned_to_id => '',
1880 1902 :custom_field_values => {'2' => '777'}}
1881 1903
1882 1904 assert_response 302
1883 1905
1884 1906 issue = Issue.find(1)
1885 1907 journal = issue.journals.find(:first, :order => 'created_on DESC')
1886 1908 assert_equal '777', issue.custom_value_for(2).value
1887 1909 assert_equal 1, journal.details.size
1888 1910 assert_equal '125', journal.details.first.old_value
1889 1911 assert_equal '777', journal.details.first.value
1890 1912 end
1891 1913
1892 1914 def test_bulk_update_unassign
1893 1915 assert_not_nil Issue.find(2).assigned_to
1894 1916 @request.session[:user_id] = 2
1895 1917 # unassign issues
1896 1918 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
1897 1919 assert_response 302
1898 1920 # check that the issues were updated
1899 1921 assert_nil Issue.find(2).assigned_to
1900 1922 end
1901 1923
1902 1924 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
1903 1925 @request.session[:user_id] = 2
1904 1926
1905 1927 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
1906 1928
1907 1929 assert_response :redirect
1908 1930 issues = Issue.find([1,2])
1909 1931 issues.each do |issue|
1910 1932 assert_equal 4, issue.fixed_version_id
1911 1933 assert_not_equal issue.project_id, issue.fixed_version.project_id
1912 1934 end
1913 1935 end
1914 1936
1915 1937 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
1916 1938 @request.session[:user_id] = 2
1917 1939 post :bulk_update, :ids => [1,2], :back_url => '/issues'
1918 1940
1919 1941 assert_response :redirect
1920 1942 assert_redirected_to '/issues'
1921 1943 end
1922 1944
1923 1945 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1924 1946 @request.session[:user_id] = 2
1925 1947 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
1926 1948
1927 1949 assert_response :redirect
1928 1950 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
1929 1951 end
1930 1952
1931 1953 def test_destroy_issue_with_no_time_entries
1932 1954 assert_nil TimeEntry.find_by_issue_id(2)
1933 1955 @request.session[:user_id] = 2
1934 1956 post :destroy, :id => 2
1935 1957 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1936 1958 assert_nil Issue.find_by_id(2)
1937 1959 end
1938 1960
1939 1961 def test_destroy_issues_with_time_entries
1940 1962 @request.session[:user_id] = 2
1941 1963 post :destroy, :ids => [1, 3]
1942 1964 assert_response :success
1943 1965 assert_template 'destroy'
1944 1966 assert_not_nil assigns(:hours)
1945 1967 assert Issue.find_by_id(1) && Issue.find_by_id(3)
1946 1968 end
1947 1969
1948 1970 def test_destroy_issues_and_destroy_time_entries
1949 1971 @request.session[:user_id] = 2
1950 1972 post :destroy, :ids => [1, 3], :todo => 'destroy'
1951 1973 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1952 1974 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1953 1975 assert_nil TimeEntry.find_by_id([1, 2])
1954 1976 end
1955 1977
1956 1978 def test_destroy_issues_and_assign_time_entries_to_project
1957 1979 @request.session[:user_id] = 2
1958 1980 post :destroy, :ids => [1, 3], :todo => 'nullify'
1959 1981 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1960 1982 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1961 1983 assert_nil TimeEntry.find(1).issue_id
1962 1984 assert_nil TimeEntry.find(2).issue_id
1963 1985 end
1964 1986
1965 1987 def test_destroy_issues_and_reassign_time_entries_to_another_issue
1966 1988 @request.session[:user_id] = 2
1967 1989 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1968 1990 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1969 1991 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1970 1992 assert_equal 2, TimeEntry.find(1).issue_id
1971 1993 assert_equal 2, TimeEntry.find(2).issue_id
1972 1994 end
1973 1995
1974 1996 def test_destroy_issues_from_different_projects
1975 1997 @request.session[:user_id] = 2
1976 1998 post :destroy, :ids => [1, 2, 6], :todo => 'destroy'
1977 1999 assert_redirected_to :controller => 'issues', :action => 'index'
1978 2000 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
1979 2001 end
1980 2002
1981 2003 def test_destroy_parent_and_child_issues
1982 2004 parent = Issue.generate!(:project_id => 1, :tracker_id => 1)
1983 2005 child = Issue.generate!(:project_id => 1, :tracker_id => 1, :parent_issue_id => parent.id)
1984 2006 assert child.is_descendant_of?(parent.reload)
1985 2007
1986 2008 @request.session[:user_id] = 2
1987 2009 assert_difference 'Issue.count', -2 do
1988 2010 post :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
1989 2011 end
1990 2012 assert_response 302
1991 2013 end
1992 2014
1993 2015 def test_default_search_scope
1994 2016 get :index
1995 2017 assert_tag :div, :attributes => {:id => 'quick-search'},
1996 2018 :child => {:tag => 'form',
1997 2019 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1998 2020 end
1999 2021 end
General Comments 0
You need to be logged in to leave comments. Login now