##// END OF EJS Templates
add tests to export time log csv in Traditional Chinese and French for csv separator (#8368)...
Toshi MARUYAMA -
r7824:caefb912b537
parent child
Show More
@@ -1,440 +1,510
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 include Redmine::I18n
32
31 33 def setup
32 34 @controller = TimelogController.new
33 35 @request = ActionController::TestRequest.new
34 36 @response = ActionController::TestResponse.new
35 37 end
36 38
37 39 def test_get_new
38 40 @request.session[:user_id] = 3
39 41 get :new, :project_id => 1
40 42 assert_response :success
41 43 assert_template 'edit'
42 44 # Default activity selected
43 45 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
44 46 :content => 'Development'
45 47 end
46 48
47 49 def test_get_new_should_only_show_active_time_entry_activities
48 50 @request.session[:user_id] = 3
49 51 get :new, :project_id => 1
50 52 assert_response :success
51 53 assert_template 'edit'
52 54 assert_no_tag :tag => 'option', :content => 'Inactive Activity'
53 55 end
54 56
55 57 def test_get_edit_existing_time
56 58 @request.session[:user_id] = 2
57 59 get :edit, :id => 2, :project_id => nil
58 60 assert_response :success
59 61 assert_template 'edit'
60 62 # Default activity selected
61 63 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
62 64 end
63 65
64 66 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
65 67 te = TimeEntry.find(1)
66 68 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
67 69 te.save!
68 70
69 71 @request.session[:user_id] = 1
70 72 get :edit, :project_id => 1, :id => 1
71 73 assert_response :success
72 74 assert_template 'edit'
73 75 # Blank option since nothing is pre-selected
74 76 assert_tag :tag => 'option', :content => '--- Please select ---'
75 77 end
76 78
77 79 def test_post_create
78 80 # TODO: should POST to issues’ time log instead of project. change form
79 81 # and routing
80 82 @request.session[:user_id] = 3
81 83 post :create, :project_id => 1,
82 84 :time_entry => {:comments => 'Some work on TimelogControllerTest',
83 85 # Not the default activity
84 86 :activity_id => '11',
85 87 :spent_on => '2008-03-14',
86 88 :issue_id => '1',
87 89 :hours => '7.3'}
88 90 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
89 91
90 92 i = Issue.find(1)
91 93 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
92 94 assert_not_nil t
93 95 assert_equal 11, t.activity_id
94 96 assert_equal 7.3, t.hours
95 97 assert_equal 3, t.user_id
96 98 assert_equal i, t.issue
97 99 assert_equal i.project, t.project
98 100 end
99 101
100 102 def test_post_create_with_blank_issue
101 103 # TODO: should POST to issues’ time log instead of project. change form
102 104 # and routing
103 105 @request.session[:user_id] = 3
104 106 post :create, :project_id => 1,
105 107 :time_entry => {:comments => 'Some work on TimelogControllerTest',
106 108 # Not the default activity
107 109 :activity_id => '11',
108 110 :issue_id => '',
109 111 :spent_on => '2008-03-14',
110 112 :hours => '7.3'}
111 113 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
112 114
113 115 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
114 116 assert_not_nil t
115 117 assert_equal 11, t.activity_id
116 118 assert_equal 7.3, t.hours
117 119 assert_equal 3, t.user_id
118 120 end
119 121
120 122 def test_create_without_log_time_permission_should_be_denied
121 123 @request.session[:user_id] = 2
122 124 Role.find_by_name('Manager').remove_permission! :log_time
123 125 post :create, :project_id => 1,
124 126 :time_entry => {:activity_id => '11',
125 127 :issue_id => '',
126 128 :spent_on => '2008-03-14',
127 129 :hours => '7.3'}
128 130
129 131 assert_response 403
130 132 end
131 133
132 134 def test_update
133 135 entry = TimeEntry.find(1)
134 136 assert_equal 1, entry.issue_id
135 137 assert_equal 2, entry.user_id
136 138
137 139 @request.session[:user_id] = 1
138 140 put :update, :id => 1,
139 141 :time_entry => {:issue_id => '2',
140 142 :hours => '8'}
141 143 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
142 144 entry.reload
143 145
144 146 assert_equal 8, entry.hours
145 147 assert_equal 2, entry.issue_id
146 148 assert_equal 2, entry.user_id
147 149 end
148 150
149 151 def test_get_bulk_edit
150 152 @request.session[:user_id] = 2
151 153 get :bulk_edit, :ids => [1, 2]
152 154 assert_response :success
153 155 assert_template 'bulk_edit'
154 156
155 157 # System wide custom field
156 158 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
157 159 end
158 160
159 161 def test_get_bulk_edit_on_different_projects
160 162 @request.session[:user_id] = 2
161 163 get :bulk_edit, :ids => [1, 2, 6]
162 164 assert_response :success
163 165 assert_template 'bulk_edit'
164 166 end
165 167
166 168 def test_bulk_update
167 169 @request.session[:user_id] = 2
168 170 # update time entry activity
169 171 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
170 172
171 173 assert_response 302
172 174 # check that the issues were updated
173 175 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
174 176 end
175 177
176 178 def test_bulk_update_on_different_projects
177 179 @request.session[:user_id] = 2
178 180 # makes user a manager on the other project
179 181 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
180 182
181 183 # update time entry activity
182 184 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
183 185
184 186 assert_response 302
185 187 # check that the issues were updated
186 188 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
187 189 end
188 190
189 191 def test_bulk_update_on_different_projects_without_rights
190 192 @request.session[:user_id] = 3
191 193 user = User.find(3)
192 194 action = { :controller => "timelog", :action => "bulk_update" }
193 195 assert user.allowed_to?(action, TimeEntry.find(1).project)
194 196 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
195 197 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
196 198 assert_response 403
197 199 end
198 200
199 201 def test_bulk_update_custom_field
200 202 @request.session[:user_id] = 2
201 203 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
202 204
203 205 assert_response 302
204 206 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
205 207 end
206 208
207 209 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
208 210 @request.session[:user_id] = 2
209 211 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
210 212
211 213 assert_response :redirect
212 214 assert_redirected_to '/time_entries'
213 215 end
214 216
215 217 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
216 218 @request.session[:user_id] = 2
217 219 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
218 220
219 221 assert_response :redirect
220 222 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
221 223 end
222 224
223 225 def test_post_bulk_update_without_edit_permission_should_be_denied
224 226 @request.session[:user_id] = 2
225 227 Role.find_by_name('Manager').remove_permission! :edit_time_entries
226 228 post :bulk_update, :ids => [1,2]
227 229
228 230 assert_response 403
229 231 end
230 232
231 233 def test_destroy
232 234 @request.session[:user_id] = 2
233 235 delete :destroy, :id => 1
234 236 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
235 237 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
236 238 assert_nil TimeEntry.find_by_id(1)
237 239 end
238 240
239 241 def test_destroy_should_fail
240 242 # simulate that this fails (e.g. due to a plugin), see #5700
241 243 TimeEntry.any_instance.expects(:destroy).returns(false)
242 244
243 245 @request.session[:user_id] = 2
244 246 delete :destroy, :id => 1
245 247 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
246 248 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
247 249 assert_not_nil TimeEntry.find_by_id(1)
248 250 end
249 251
250 252 def test_index_all_projects
251 253 get :index
252 254 assert_response :success
253 255 assert_template 'index'
254 256 assert_not_nil assigns(:total_hours)
255 257 assert_equal "162.90", "%.2f" % assigns(:total_hours)
256 258 assert_tag :form,
257 259 :attributes => {:action => "/time_entries", :id => 'query_form'}
258 260 end
259 261
260 262 def test_index_at_project_level
261 263 get :index, :project_id => 'ecookbook'
262 264 assert_response :success
263 265 assert_template 'index'
264 266 assert_not_nil assigns(:entries)
265 267 assert_equal 4, assigns(:entries).size
266 268 # project and subproject
267 269 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
268 270 assert_not_nil assigns(:total_hours)
269 271 assert_equal "162.90", "%.2f" % assigns(:total_hours)
270 272 # display all time by default
271 273 assert_equal '2007-03-12'.to_date, assigns(:from)
272 274 assert_equal '2007-04-22'.to_date, assigns(:to)
273 275 assert_tag :form,
274 276 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
275 277 end
276 278
277 279 def test_index_at_project_level_with_date_range
278 280 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
279 281 assert_response :success
280 282 assert_template 'index'
281 283 assert_not_nil assigns(:entries)
282 284 assert_equal 3, assigns(:entries).size
283 285 assert_not_nil assigns(:total_hours)
284 286 assert_equal "12.90", "%.2f" % assigns(:total_hours)
285 287 assert_equal '2007-03-20'.to_date, assigns(:from)
286 288 assert_equal '2007-04-30'.to_date, assigns(:to)
287 289 assert_tag :form,
288 290 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
289 291 end
290 292
291 293 def test_index_at_project_level_with_period
292 294 get :index, :project_id => 'ecookbook', :period => '7_days'
293 295 assert_response :success
294 296 assert_template 'index'
295 297 assert_not_nil assigns(:entries)
296 298 assert_not_nil assigns(:total_hours)
297 299 assert_equal Date.today - 7, assigns(:from)
298 300 assert_equal Date.today, assigns(:to)
299 301 assert_tag :form,
300 302 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
301 303 end
302 304
303 305 def test_index_one_day
304 306 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
305 307 assert_response :success
306 308 assert_template 'index'
307 309 assert_not_nil assigns(:total_hours)
308 310 assert_equal "4.25", "%.2f" % assigns(:total_hours)
309 311 assert_tag :form,
310 312 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
311 313 end
312 314
313 315 def test_index_at_issue_level
314 316 get :index, :issue_id => 1
315 317 assert_response :success
316 318 assert_template 'index'
317 319 assert_not_nil assigns(:entries)
318 320 assert_equal 2, assigns(:entries).size
319 321 assert_not_nil assigns(:total_hours)
320 322 assert_equal 154.25, assigns(:total_hours)
321 323 # display all time based on what's been logged
322 324 assert_equal '2007-03-12'.to_date, assigns(:from)
323 325 assert_equal '2007-04-22'.to_date, assigns(:to)
324 326 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
325 327 # to use /issues/:issue_id/time_entries
326 328 assert_tag :form,
327 329 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
328 330 end
329 331
330 332 def test_index_atom_feed
331 333 get :index, :project_id => 1, :format => 'atom'
332 334 assert_response :success
333 335 assert_equal 'application/atom+xml', @response.content_type
334 336 assert_not_nil assigns(:items)
335 337 assert assigns(:items).first.is_a?(TimeEntry)
336 338 end
337 339
338 340 def test_index_all_projects_csv_export
339 341 Setting.date_format = '%m/%d/%Y'
340 342 get :index, :format => 'csv'
341 343 assert_response :success
342 344 assert_equal 'text/csv', @response.content_type
343 345 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
344 346 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
345 347 end
346 348
347 349 def test_index_csv_export
348 350 Setting.date_format = '%m/%d/%Y'
349 351 get :index, :project_id => 1, :format => 'csv'
350 352 assert_response :success
351 353 assert_equal 'text/csv', @response.content_type
352 354 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
353 355 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
354 356 end
355 357
356 358 def test_csv_big_5
357 359 user = User.find_by_id(3)
358 360 user.language = "zh-TW"
359 361 assert user.save
360 362 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
361 363 str_big5 = "\xa4@\xa4\xeb"
362 364 if str_utf8.respond_to?(:force_encoding)
363 365 str_utf8.force_encoding('UTF-8')
364 366 str_big5.force_encoding('Big5')
365 367 end
366 368 @request.session[:user_id] = 3
367 369 post :create, :project_id => 1,
368 370 :time_entry => {:comments => str_utf8,
369 371 # Not the default activity
370 372 :activity_id => '11',
371 373 :issue_id => '',
372 374 :spent_on => '2011-11-10',
373 375 :hours => '7.3'}
374 376 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
375 377
376 378 t = TimeEntry.find_by_comments(str_utf8)
377 379 assert_not_nil t
378 380 assert_equal 11, t.activity_id
379 381 assert_equal 7.3, t.hours
380 382 assert_equal 3, t.user_id
381 383
382 384 get :index, :project_id => 1, :format => 'csv',
383 385 :from => '2011-11-10', :to => '2011-11-10'
384 386 assert_response :success
385 387 assert_equal 'text/csv', @response.content_type
386 388 ar = @response.body.chomp.split("\n")
387 389 s1 = "\xa4\xe9\xb4\xc1"
388 390 if str_utf8.respond_to?(:force_encoding)
389 391 s1.force_encoding('Big5')
390 392 end
391 393 assert ar[0].include?(s1)
392 394 assert ar[1].include?(str_big5)
393 395 end
394 396
395 397 def test_csv_cannot_convert_should_be_replaced_big_5
396 398 user = User.find_by_id(3)
397 399 user.language = "zh-TW"
398 400 assert user.save
399 401 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
400 402 if str_utf8.respond_to?(:force_encoding)
401 403 str_utf8.force_encoding('UTF-8')
402 404 end
403 405 @request.session[:user_id] = 3
404 406 post :create, :project_id => 1,
405 407 :time_entry => {:comments => str_utf8,
406 408 # Not the default activity
407 409 :activity_id => '11',
408 410 :issue_id => '',
409 411 :spent_on => '2011-11-10',
410 412 :hours => '7.3'}
411 413 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
412 414
413 415 t = TimeEntry.find_by_comments(str_utf8)
414 416 assert_not_nil t
415 417 assert_equal 11, t.activity_id
416 418 assert_equal 7.3, t.hours
417 419 assert_equal 3, t.user_id
418 420
419 421 get :index, :project_id => 1, :format => 'csv',
420 422 :from => '2011-11-10', :to => '2011-11-10'
421 423 assert_response :success
422 424 assert_equal 'text/csv', @response.content_type
423 425 ar = @response.body.chomp.split("\n")
424 426 s1 = "\xa4\xe9\xb4\xc1"
425 427 if str_utf8.respond_to?(:force_encoding)
426 428 s1.force_encoding('Big5')
427 429 end
428 430 assert ar[0].include?(s1)
429 431 s2 = ar[1].split(",")[8]
430 432 if s2.respond_to?(:force_encoding)
431 433 s3 = "\xa5H?"
432 434 s3.force_encoding('Big5')
433 435 assert_equal s3, s2
434 436 elsif RUBY_PLATFORM == 'java'
435 437 assert_equal "??", s2
436 438 else
437 439 assert_equal "\xa5H???", s2
438 440 end
439 441 end
442
443 def test_csv_tw
444 with_settings :default_language => "zh-TW" do
445 str1 = "test_csv_tw"
446 user = User.find_by_id(3)
447 te1 = TimeEntry.create(:spent_on => '2011-11-10',
448 :hours => 999.9,
449 :project => Project.find(1),
450 :user => user,
451 :activity => TimeEntryActivity.find_by_name('Design'),
452 :comments => str1)
453 te2 = TimeEntry.find_by_comments(str1)
454 assert_not_nil te2
455 assert_equal 999.9, te2.hours
456 assert_equal 3, te2.user_id
457
458 get :index, :project_id => 1, :format => 'csv',
459 :from => '2011-11-10', :to => '2011-11-10'
460 assert_response :success
461 assert_equal 'text/csv', @response.content_type
462
463 ar = @response.body.chomp.split("\n")
464 s2 = ar[1].split(",")[7]
465 assert_equal '999.9', s2
466
467 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
468 if str_tw.respond_to?(:force_encoding)
469 str_tw.force_encoding('UTF-8')
470 end
471 assert_equal str_tw, l(:general_lang_name)
472 assert_equal ',', l(:general_csv_separator)
473 assert_equal '.', l(:general_csv_decimal_separator)
474 end
475 end
476
477 def test_csv_fr
478 with_settings :default_language => "fr" do
479 str1 = "test_csv_fr"
480 user = User.find_by_id(3)
481 te1 = TimeEntry.create(:spent_on => '2011-11-10',
482 :hours => 999.9,
483 :project => Project.find(1),
484 :user => user,
485 :activity => TimeEntryActivity.find_by_name('Design'),
486 :comments => str1)
487 te2 = TimeEntry.find_by_comments(str1)
488 assert_not_nil te2
489 assert_equal 999.9, te2.hours
490 assert_equal 3, te2.user_id
491
492 get :index, :project_id => 1, :format => 'csv',
493 :from => '2011-11-10', :to => '2011-11-10'
494 assert_response :success
495 assert_equal 'text/csv', @response.content_type
496
497 ar = @response.body.chomp.split("\n")
498 s2 = ar[1].split(";")[7]
499 assert_equal '999,9', s2
500
501 str_fr = "Fran\xc3\xa7ais"
502 if str_fr.respond_to?(:force_encoding)
503 str_fr.force_encoding('UTF-8')
504 end
505 assert_equal str_fr, l(:general_lang_name)
506 assert_equal ';', l(:general_csv_separator)
507 assert_equal ',', l(:general_csv_decimal_separator)
508 end
509 end
440 510 end
General Comments 0
You need to be logged in to leave comments. Login now