##// END OF EJS Templates
Additional tests for TimelogController....
Jean-Philippe Lang -
r9306:f79961f1c533
parent child
Show More
@@ -1,730 +1,742
1 1 # -*- coding: utf-8 -*-
2 2 # Redmine - project management software
3 3 # Copyright (C) 2006-2011 Jean-Philippe Lang
4 4 #
5 5 # This program is free software; you can redistribute it and/or
6 6 # modify it under the terms of the GNU General Public License
7 7 # as published by the Free Software Foundation; either version 2
8 8 # of the License, or (at your option) any later version.
9 9 #
10 10 # This program is distributed in the hope that it will be useful,
11 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 # GNU General Public License for more details.
14 14 #
15 15 # You should have received a copy of the GNU General Public License
16 16 # along with this program; if not, write to the Free Software
17 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 18
19 19 require File.expand_path('../../test_helper', __FILE__)
20 20 require 'timelog_controller'
21 21
22 22 # Re-raise errors caught by the controller.
23 23 class TimelogController; def rescue_action(e) raise e end; end
24 24
25 25 class TimelogControllerTest < ActionController::TestCase
26 26 fixtures :projects, :enabled_modules, :roles, :members,
27 27 :member_roles, :issues, :time_entries, :users,
28 28 :trackers, :enumerations, :issue_statuses,
29 29 :custom_fields, :custom_values
30 30
31 31 include Redmine::I18n
32 32
33 33 def setup
34 34 @controller = TimelogController.new
35 35 @request = ActionController::TestRequest.new
36 36 @response = ActionController::TestResponse.new
37 37 end
38 38
39 39 def test_get_new
40 40 @request.session[:user_id] = 3
41 41 get :new, :project_id => 1
42 42 assert_response :success
43 43 assert_template 'new'
44 44 # Default activity selected
45 45 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
46 46 :content => 'Development'
47 47 end
48 48
49 49 def test_get_new_should_only_show_active_time_entry_activities
50 50 @request.session[:user_id] = 3
51 51 get :new, :project_id => 1
52 52 assert_response :success
53 53 assert_template 'new'
54 54 assert_no_tag 'select', :attributes => {:name => 'time_entry[project_id]'}
55 55 assert_no_tag 'option', :content => 'Inactive Activity'
56 56 end
57 57
58 58 def test_new_without_project
59 59 @request.session[:user_id] = 3
60 60 get :new
61 61 assert_response :success
62 62 assert_template 'new'
63 63 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'}
64 64 end
65 65
66 66 def test_new_without_project_should_deny_without_permission
67 67 Role.all.each {|role| role.remove_permission! :log_time}
68 68 @request.session[:user_id] = 3
69 69
70 70 get :new
71 71 assert_response 403
72 72 end
73 73
74 74 def test_get_edit_existing_time
75 75 @request.session[:user_id] = 2
76 76 get :edit, :id => 2, :project_id => nil
77 77 assert_response :success
78 78 assert_template 'edit'
79 79 # Default activity selected
80 80 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
81 81 end
82 82
83 83 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
84 84 te = TimeEntry.find(1)
85 85 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
86 86 te.save!
87 87
88 88 @request.session[:user_id] = 1
89 89 get :edit, :project_id => 1, :id => 1
90 90 assert_response :success
91 91 assert_template 'edit'
92 92 # Blank option since nothing is pre-selected
93 93 assert_tag :tag => 'option', :content => '--- Please select ---'
94 94 end
95 95
96 96 def test_post_create
97 97 # TODO: should POST to issues’ time log instead of project. change form
98 98 # and routing
99 99 @request.session[:user_id] = 3
100 100 post :create, :project_id => 1,
101 101 :time_entry => {:comments => 'Some work on TimelogControllerTest',
102 102 # Not the default activity
103 103 :activity_id => '11',
104 104 :spent_on => '2008-03-14',
105 105 :issue_id => '1',
106 106 :hours => '7.3'}
107 107 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
108 108
109 109 i = Issue.find(1)
110 110 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
111 111 assert_not_nil t
112 112 assert_equal 11, t.activity_id
113 113 assert_equal 7.3, t.hours
114 114 assert_equal 3, t.user_id
115 115 assert_equal i, t.issue
116 116 assert_equal i.project, t.project
117 117 end
118 118
119 119 def test_post_create_with_blank_issue
120 120 # TODO: should POST to issues’ time log instead of project. change form
121 121 # and routing
122 122 @request.session[:user_id] = 3
123 123 post :create, :project_id => 1,
124 124 :time_entry => {:comments => 'Some work on TimelogControllerTest',
125 125 # Not the default activity
126 126 :activity_id => '11',
127 127 :issue_id => '',
128 128 :spent_on => '2008-03-14',
129 129 :hours => '7.3'}
130 130 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
131 131
132 132 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
133 133 assert_not_nil t
134 134 assert_equal 11, t.activity_id
135 135 assert_equal 7.3, t.hours
136 136 assert_equal 3, t.user_id
137 137 end
138 138
139 139 def test_create_and_continue
140 140 @request.session[:user_id] = 2
141 141 post :create, :project_id => 1,
142 142 :time_entry => {:activity_id => '11',
143 143 :issue_id => '',
144 144 :spent_on => '2008-03-14',
145 145 :hours => '7.3'},
146 146 :continue => '1'
147 147 assert_redirected_to '/projects/ecookbook/time_entries/new'
148 148 end
149 149
150 150 def test_create_and_continue_with_issue_id
151 151 @request.session[:user_id] = 2
152 152 post :create, :project_id => 1,
153 153 :time_entry => {:activity_id => '11',
154 154 :issue_id => '1',
155 155 :spent_on => '2008-03-14',
156 156 :hours => '7.3'},
157 157 :continue => '1'
158 158 assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new'
159 159 end
160 160
161 161 def test_create_and_continue_without_project
162 162 @request.session[:user_id] = 2
163 163 post :create, :time_entry => {:project_id => '1',
164 164 :activity_id => '11',
165 165 :issue_id => '',
166 166 :spent_on => '2008-03-14',
167 167 :hours => '7.3'},
168 168 :continue => '1'
169 169
170 170 assert_redirected_to '/time_entries/new'
171 171 end
172 172
173 173 def test_create_without_log_time_permission_should_be_denied
174 174 @request.session[:user_id] = 2
175 175 Role.find_by_name('Manager').remove_permission! :log_time
176 176 post :create, :project_id => 1,
177 177 :time_entry => {:activity_id => '11',
178 178 :issue_id => '',
179 179 :spent_on => '2008-03-14',
180 180 :hours => '7.3'}
181 181
182 182 assert_response 403
183 183 end
184 184
185 185 def test_create_with_failure
186 186 @request.session[:user_id] = 2
187 187 post :create, :project_id => 1,
188 188 :time_entry => {:activity_id => '',
189 189 :issue_id => '',
190 190 :spent_on => '2008-03-14',
191 191 :hours => '7.3'}
192 192
193 193 assert_response :success
194 194 assert_template 'new'
195 195 end
196 196
197 197 def test_create_without_project
198 198 @request.session[:user_id] = 2
199 199 assert_difference 'TimeEntry.count' do
200 200 post :create, :time_entry => {:project_id => '1',
201 201 :activity_id => '11',
202 202 :issue_id => '',
203 203 :spent_on => '2008-03-14',
204 204 :hours => '7.3'}
205 205 end
206 206
207 207 assert_redirected_to '/projects/ecookbook/time_entries'
208 208 time_entry = TimeEntry.first(:order => 'id DESC')
209 209 assert_equal 1, time_entry.project_id
210 210 end
211 211
212 212 def test_create_without_project_should_fail_with_issue_not_inside_project
213 213 @request.session[:user_id] = 2
214 214 assert_no_difference 'TimeEntry.count' do
215 215 post :create, :time_entry => {:project_id => '1',
216 216 :activity_id => '11',
217 217 :issue_id => '5',
218 218 :spent_on => '2008-03-14',
219 219 :hours => '7.3'}
220 220 end
221 221
222 222 assert_response :success
223 223 assert assigns(:time_entry).errors[:issue_id].present?
224 224 end
225 225
226 226 def test_create_without_project_should_deny_without_permission
227 227 @request.session[:user_id] = 2
228 228 Project.find(3).disable_module!(:time_tracking)
229 229
230 230 assert_no_difference 'TimeEntry.count' do
231 231 post :create, :time_entry => {:project_id => '3',
232 232 :activity_id => '11',
233 233 :issue_id => '',
234 234 :spent_on => '2008-03-14',
235 235 :hours => '7.3'}
236 236 end
237 237
238 238 assert_response 403
239 239 end
240 240
241 241 def test_create_without_project_with_failure
242 242 @request.session[:user_id] = 2
243 243 assert_no_difference 'TimeEntry.count' do
244 244 post :create, :time_entry => {:project_id => '1',
245 245 :activity_id => '11',
246 246 :issue_id => '',
247 247 :spent_on => '2008-03-14',
248 248 :hours => ''}
249 249 end
250 250
251 251 assert_response :success
252 252 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'},
253 253 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
254 254 end
255 255
256 256 def test_update
257 257 entry = TimeEntry.find(1)
258 258 assert_equal 1, entry.issue_id
259 259 assert_equal 2, entry.user_id
260 260
261 261 @request.session[:user_id] = 1
262 262 put :update, :id => 1,
263 263 :time_entry => {:issue_id => '2',
264 264 :hours => '8'}
265 265 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
266 266 entry.reload
267 267
268 268 assert_equal 8, entry.hours
269 269 assert_equal 2, entry.issue_id
270 270 assert_equal 2, entry.user_id
271 271 end
272 272
273 273 def test_get_bulk_edit
274 274 @request.session[:user_id] = 2
275 275 get :bulk_edit, :ids => [1, 2]
276 276 assert_response :success
277 277 assert_template 'bulk_edit'
278 278
279 279 # System wide custom field
280 280 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
281 281 end
282 282
283 283 def test_get_bulk_edit_on_different_projects
284 284 @request.session[:user_id] = 2
285 285 get :bulk_edit, :ids => [1, 2, 6]
286 286 assert_response :success
287 287 assert_template 'bulk_edit'
288 288 end
289 289
290 290 def test_bulk_update
291 291 @request.session[:user_id] = 2
292 292 # update time entry activity
293 293 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
294 294
295 295 assert_response 302
296 296 # check that the issues were updated
297 297 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
298 298 end
299 299
300 300 def test_bulk_update_with_failure
301 301 @request.session[:user_id] = 2
302 302 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
303 303
304 304 assert_response 302
305 305 assert_match /Failed to save 2 time entrie/, flash[:error]
306 306 end
307 307
308 308 def test_bulk_update_on_different_projects
309 309 @request.session[:user_id] = 2
310 310 # makes user a manager on the other project
311 311 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
312 312
313 313 # update time entry activity
314 314 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
315 315
316 316 assert_response 302
317 317 # check that the issues were updated
318 318 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
319 319 end
320 320
321 321 def test_bulk_update_on_different_projects_without_rights
322 322 @request.session[:user_id] = 3
323 323 user = User.find(3)
324 324 action = { :controller => "timelog", :action => "bulk_update" }
325 325 assert user.allowed_to?(action, TimeEntry.find(1).project)
326 326 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
327 327 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
328 328 assert_response 403
329 329 end
330 330
331 331 def test_bulk_update_custom_field
332 332 @request.session[:user_id] = 2
333 333 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
334 334
335 335 assert_response 302
336 336 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
337 337 end
338 338
339 339 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
340 340 @request.session[:user_id] = 2
341 341 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
342 342
343 343 assert_response :redirect
344 344 assert_redirected_to '/time_entries'
345 345 end
346 346
347 347 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
348 348 @request.session[:user_id] = 2
349 349 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
350 350
351 351 assert_response :redirect
352 352 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
353 353 end
354 354
355 355 def test_post_bulk_update_without_edit_permission_should_be_denied
356 356 @request.session[:user_id] = 2
357 357 Role.find_by_name('Manager').remove_permission! :edit_time_entries
358 358 post :bulk_update, :ids => [1,2]
359 359
360 360 assert_response 403
361 361 end
362 362
363 363 def test_destroy
364 364 @request.session[:user_id] = 2
365 365 delete :destroy, :id => 1
366 366 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
367 367 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
368 368 assert_nil TimeEntry.find_by_id(1)
369 369 end
370 370
371 371 def test_destroy_should_fail
372 372 # simulate that this fails (e.g. due to a plugin), see #5700
373 373 TimeEntry.any_instance.expects(:destroy).returns(false)
374 374
375 375 @request.session[:user_id] = 2
376 376 delete :destroy, :id => 1
377 377 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
378 378 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
379 379 assert_not_nil TimeEntry.find_by_id(1)
380 380 end
381 381
382 382 def test_index_all_projects
383 383 get :index
384 384 assert_response :success
385 385 assert_template 'index'
386 386 assert_not_nil assigns(:total_hours)
387 387 assert_equal "162.90", "%.2f" % assigns(:total_hours)
388 388 assert_tag :form,
389 389 :attributes => {:action => "/time_entries", :id => 'query_form'}
390 390 end
391 391
392 392 def test_index_all_projects_should_show_log_time_link
393 393 @request.session[:user_id] = 2
394 394 get :index
395 395 assert_response :success
396 396 assert_template 'index'
397 397 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/
398 398 end
399 399
400 400 def test_index_at_project_level
401 401 get :index, :project_id => 'ecookbook'
402 402 assert_response :success
403 403 assert_template 'index'
404 404 assert_not_nil assigns(:entries)
405 405 assert_equal 4, assigns(:entries).size
406 406 # project and subproject
407 407 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
408 408 assert_not_nil assigns(:total_hours)
409 409 assert_equal "162.90", "%.2f" % assigns(:total_hours)
410 410 # display all time by default
411 411 assert_nil assigns(:from)
412 412 assert_nil assigns(:to)
413 413 assert_tag :form,
414 414 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
415 415 end
416 416
417 417 def test_index_at_project_level_with_date_range
418 418 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
419 419 assert_response :success
420 420 assert_template 'index'
421 421 assert_not_nil assigns(:entries)
422 422 assert_equal 3, assigns(:entries).size
423 423 assert_not_nil assigns(:total_hours)
424 424 assert_equal "12.90", "%.2f" % assigns(:total_hours)
425 425 assert_equal '2007-03-20'.to_date, assigns(:from)
426 426 assert_equal '2007-04-30'.to_date, assigns(:to)
427 427 assert_tag :form,
428 428 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
429 429 end
430 430
431 431 def test_index_at_project_level_with_period
432 432 get :index, :project_id => 'ecookbook', :period => '7_days'
433 433 assert_response :success
434 434 assert_template 'index'
435 435 assert_not_nil assigns(:entries)
436 436 assert_not_nil assigns(:total_hours)
437 437 assert_equal Date.today - 7, assigns(:from)
438 438 assert_equal Date.today, assigns(:to)
439 439 assert_tag :form,
440 440 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
441 441 end
442 442
443 443 def test_index_one_day
444 444 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
445 445 assert_response :success
446 446 assert_template 'index'
447 447 assert_not_nil assigns(:total_hours)
448 448 assert_equal "4.25", "%.2f" % assigns(:total_hours)
449 449 assert_tag :form,
450 450 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
451 451 end
452 452
453 def test_index_from_a_date
454 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => ""
455 assert_equal '2007-03-23'.to_date, assigns(:from)
456 assert_nil assigns(:to)
457 end
458
459 def test_index_to_a_date
460 get :index, :project_id => 'ecookbook', :from => "", :to => "2007-03-23"
461 assert_nil assigns(:from)
462 assert_equal '2007-03-23'.to_date, assigns(:to)
463 end
464
453 465 def test_index_today
454 466 Date.stubs(:today).returns('2011-12-15'.to_date)
455 467 get :index, :period => 'today'
456 468 assert_equal '2011-12-15'.to_date, assigns(:from)
457 469 assert_equal '2011-12-15'.to_date, assigns(:to)
458 470 end
459 471
460 472 def test_index_yesterday
461 473 Date.stubs(:today).returns('2011-12-15'.to_date)
462 474 get :index, :period => 'yesterday'
463 475 assert_equal '2011-12-14'.to_date, assigns(:from)
464 476 assert_equal '2011-12-14'.to_date, assigns(:to)
465 477 end
466 478
467 479 def test_index_current_week
468 480 Date.stubs(:today).returns('2011-12-15'.to_date)
469 481 get :index, :period => 'current_week'
470 482 assert_equal '2011-12-12'.to_date, assigns(:from)
471 483 assert_equal '2011-12-18'.to_date, assigns(:to)
472 484 end
473 485
474 486 def test_index_last_week
475 487 Date.stubs(:today).returns('2011-12-15'.to_date)
476 488 get :index, :period => 'current_week'
477 489 assert_equal '2011-12-05'.to_date, assigns(:from)
478 490 assert_equal '2011-12-11'.to_date, assigns(:to)
479 491 end
480 492
481 493 def test_index_last_week
482 494 Date.stubs(:today).returns('2011-12-15'.to_date)
483 495 get :index, :period => 'last_week'
484 496 assert_equal '2011-12-05'.to_date, assigns(:from)
485 497 assert_equal '2011-12-11'.to_date, assigns(:to)
486 498 end
487 499
488 500 def test_index_7_days
489 501 Date.stubs(:today).returns('2011-12-15'.to_date)
490 502 get :index, :period => '7_days'
491 503 assert_equal '2011-12-08'.to_date, assigns(:from)
492 504 assert_equal '2011-12-15'.to_date, assigns(:to)
493 505 end
494 506
495 507 def test_index_current_month
496 508 Date.stubs(:today).returns('2011-12-15'.to_date)
497 509 get :index, :period => 'current_month'
498 510 assert_equal '2011-12-01'.to_date, assigns(:from)
499 511 assert_equal '2011-12-31'.to_date, assigns(:to)
500 512 end
501 513
502 514 def test_index_last_month
503 515 Date.stubs(:today).returns('2011-12-15'.to_date)
504 516 get :index, :period => 'last_month'
505 517 assert_equal '2011-11-01'.to_date, assigns(:from)
506 518 assert_equal '2011-11-30'.to_date, assigns(:to)
507 519 end
508 520
509 521 def test_index_30_days
510 522 Date.stubs(:today).returns('2011-12-15'.to_date)
511 523 get :index, :period => '30_days'
512 524 assert_equal '2011-11-15'.to_date, assigns(:from)
513 525 assert_equal '2011-12-15'.to_date, assigns(:to)
514 526 end
515 527
516 528 def test_index_current_year
517 529 Date.stubs(:today).returns('2011-12-15'.to_date)
518 530 get :index, :period => 'current_year'
519 531 assert_equal '2011-01-01'.to_date, assigns(:from)
520 532 assert_equal '2011-12-31'.to_date, assigns(:to)
521 533 end
522 534
523 535 def test_index_at_issue_level
524 536 get :index, :issue_id => 1
525 537 assert_response :success
526 538 assert_template 'index'
527 539 assert_not_nil assigns(:entries)
528 540 assert_equal 2, assigns(:entries).size
529 541 assert_not_nil assigns(:total_hours)
530 542 assert_equal 154.25, assigns(:total_hours)
531 543 # display all time
532 544 assert_nil assigns(:from)
533 545 assert_nil assigns(:to)
534 546 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
535 547 # to use /issues/:issue_id/time_entries
536 548 assert_tag :form,
537 549 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
538 550 end
539 551
540 552 def test_index_atom_feed
541 553 get :index, :project_id => 1, :format => 'atom'
542 554 assert_response :success
543 555 assert_equal 'application/atom+xml', @response.content_type
544 556 assert_not_nil assigns(:items)
545 557 assert assigns(:items).first.is_a?(TimeEntry)
546 558 end
547 559
548 560 def test_index_all_projects_csv_export
549 561 Setting.date_format = '%m/%d/%Y'
550 562 get :index, :format => 'csv'
551 563 assert_response :success
552 564 assert_equal 'text/csv', @response.content_type
553 565 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
554 566 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
555 567 end
556 568
557 569 def test_index_csv_export
558 570 Setting.date_format = '%m/%d/%Y'
559 571 get :index, :project_id => 1, :format => 'csv'
560 572 assert_response :success
561 573 assert_equal 'text/csv', @response.content_type
562 574 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
563 575 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
564 576 end
565 577
566 578 def test_index_csv_export_with_multi_custom_field
567 579 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'list',
568 580 :multiple => true, :possible_values => ['value1', 'value2'])
569 581 entry = TimeEntry.find(1)
570 582 entry.custom_field_values = {field.id => ['value1', 'value2']}
571 583 entry.save!
572 584
573 585 get :index, :project_id => 1, :format => 'csv'
574 586 assert_response :success
575 587 assert_include '"value1, value2"', @response.body
576 588 end
577 589
578 590 def test_csv_big_5
579 591 user = User.find_by_id(3)
580 592 user.language = "zh-TW"
581 593 assert user.save
582 594 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
583 595 str_big5 = "\xa4@\xa4\xeb"
584 596 if str_utf8.respond_to?(:force_encoding)
585 597 str_utf8.force_encoding('UTF-8')
586 598 str_big5.force_encoding('Big5')
587 599 end
588 600 @request.session[:user_id] = 3
589 601 post :create, :project_id => 1,
590 602 :time_entry => {:comments => str_utf8,
591 603 # Not the default activity
592 604 :activity_id => '11',
593 605 :issue_id => '',
594 606 :spent_on => '2011-11-10',
595 607 :hours => '7.3'}
596 608 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
597 609
598 610 t = TimeEntry.find_by_comments(str_utf8)
599 611 assert_not_nil t
600 612 assert_equal 11, t.activity_id
601 613 assert_equal 7.3, t.hours
602 614 assert_equal 3, t.user_id
603 615
604 616 get :index, :project_id => 1, :format => 'csv',
605 617 :from => '2011-11-10', :to => '2011-11-10'
606 618 assert_response :success
607 619 assert_equal 'text/csv', @response.content_type
608 620 ar = @response.body.chomp.split("\n")
609 621 s1 = "\xa4\xe9\xb4\xc1"
610 622 if str_utf8.respond_to?(:force_encoding)
611 623 s1.force_encoding('Big5')
612 624 end
613 625 assert ar[0].include?(s1)
614 626 assert ar[1].include?(str_big5)
615 627 end
616 628
617 629 def test_csv_cannot_convert_should_be_replaced_big_5
618 630 user = User.find_by_id(3)
619 631 user.language = "zh-TW"
620 632 assert user.save
621 633 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
622 634 if str_utf8.respond_to?(:force_encoding)
623 635 str_utf8.force_encoding('UTF-8')
624 636 end
625 637 @request.session[:user_id] = 3
626 638 post :create, :project_id => 1,
627 639 :time_entry => {:comments => str_utf8,
628 640 # Not the default activity
629 641 :activity_id => '11',
630 642 :issue_id => '',
631 643 :spent_on => '2011-11-10',
632 644 :hours => '7.3'}
633 645 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
634 646
635 647 t = TimeEntry.find_by_comments(str_utf8)
636 648 assert_not_nil t
637 649 assert_equal 11, t.activity_id
638 650 assert_equal 7.3, t.hours
639 651 assert_equal 3, t.user_id
640 652
641 653 get :index, :project_id => 1, :format => 'csv',
642 654 :from => '2011-11-10', :to => '2011-11-10'
643 655 assert_response :success
644 656 assert_equal 'text/csv', @response.content_type
645 657 ar = @response.body.chomp.split("\n")
646 658 s1 = "\xa4\xe9\xb4\xc1"
647 659 if str_utf8.respond_to?(:force_encoding)
648 660 s1.force_encoding('Big5')
649 661 end
650 662 assert ar[0].include?(s1)
651 663 s2 = ar[1].split(",")[8]
652 664 if s2.respond_to?(:force_encoding)
653 665 s3 = "\xa5H?"
654 666 s3.force_encoding('Big5')
655 667 assert_equal s3, s2
656 668 elsif RUBY_PLATFORM == 'java'
657 669 assert_equal "??", s2
658 670 else
659 671 assert_equal "\xa5H???", s2
660 672 end
661 673 end
662 674
663 675 def test_csv_tw
664 676 with_settings :default_language => "zh-TW" do
665 677 str1 = "test_csv_tw"
666 678 user = User.find_by_id(3)
667 679 te1 = TimeEntry.create(:spent_on => '2011-11-10',
668 680 :hours => 999.9,
669 681 :project => Project.find(1),
670 682 :user => user,
671 683 :activity => TimeEntryActivity.find_by_name('Design'),
672 684 :comments => str1)
673 685 te2 = TimeEntry.find_by_comments(str1)
674 686 assert_not_nil te2
675 687 assert_equal 999.9, te2.hours
676 688 assert_equal 3, te2.user_id
677 689
678 690 get :index, :project_id => 1, :format => 'csv',
679 691 :from => '2011-11-10', :to => '2011-11-10'
680 692 assert_response :success
681 693 assert_equal 'text/csv', @response.content_type
682 694
683 695 ar = @response.body.chomp.split("\n")
684 696 s2 = ar[1].split(",")[7]
685 697 assert_equal '999.9', s2
686 698
687 699 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
688 700 if str_tw.respond_to?(:force_encoding)
689 701 str_tw.force_encoding('UTF-8')
690 702 end
691 703 assert_equal str_tw, l(:general_lang_name)
692 704 assert_equal ',', l(:general_csv_separator)
693 705 assert_equal '.', l(:general_csv_decimal_separator)
694 706 end
695 707 end
696 708
697 709 def test_csv_fr
698 710 with_settings :default_language => "fr" do
699 711 str1 = "test_csv_fr"
700 712 user = User.find_by_id(3)
701 713 te1 = TimeEntry.create(:spent_on => '2011-11-10',
702 714 :hours => 999.9,
703 715 :project => Project.find(1),
704 716 :user => user,
705 717 :activity => TimeEntryActivity.find_by_name('Design'),
706 718 :comments => str1)
707 719 te2 = TimeEntry.find_by_comments(str1)
708 720 assert_not_nil te2
709 721 assert_equal 999.9, te2.hours
710 722 assert_equal 3, te2.user_id
711 723
712 724 get :index, :project_id => 1, :format => 'csv',
713 725 :from => '2011-11-10', :to => '2011-11-10'
714 726 assert_response :success
715 727 assert_equal 'text/csv', @response.content_type
716 728
717 729 ar = @response.body.chomp.split("\n")
718 730 s2 = ar[1].split(";")[7]
719 731 assert_equal '999,9', s2
720 732
721 733 str_fr = "Fran\xc3\xa7ais"
722 734 if str_fr.respond_to?(:force_encoding)
723 735 str_fr.force_encoding('UTF-8')
724 736 end
725 737 assert_equal str_fr, l(:general_lang_name)
726 738 assert_equal ';', l(:general_csv_separator)
727 739 assert_equal ',', l(:general_csv_decimal_separator)
728 740 end
729 741 end
730 742 end
General Comments 0
You need to be logged in to leave comments. Login now