##// END OF EJS Templates
fix malformed time log csv encoding in case of unable to convert (#8549)...
Toshi MARUYAMA -
r7699:4b5d50e40a49
parent child
Show More
@@ -84,7 +84,6 module TimelogHelper
84 84 end
85 85
86 86 def entries_to_csv(entries)
87 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
88 87 decimal_separator = l(:general_csv_decimal_separator)
89 88 custom_fields = TimeEntryCustomField.find(:all)
90 89 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
@@ -102,7 +101,9 module TimelogHelper
102 101 # Export custom fields
103 102 headers += custom_fields.collect(&:name)
104 103
105 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
104 csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
105 c.to_s,
106 l(:general_csv_encoding) ) }
106 107 # csv lines
107 108 entries.each do |entry|
108 109 fields = [format_date(entry.spent_on),
@@ -117,7 +118,9 module TimelogHelper
117 118 ]
118 119 fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
119 120
120 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
121 csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
122 c.to_s,
123 l(:general_csv_encoding) ) }
121 124 end
122 125 end
123 126 export
@@ -368,4 +368,50 class TimelogControllerTest < ActionController::TestCase
368 368 assert ar[0].include?(s1)
369 369 assert ar[1].include?(str_big5)
370 370 end
371
372 def test_csv_cannot_convert_should_be_replaced_big_5
373 user = User.find_by_id(3)
374 user.language = "zh-TW"
375 assert user.save
376 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
377 if str_utf8.respond_to?(:force_encoding)
378 str_utf8.force_encoding('UTF-8')
379 end
380 @request.session[:user_id] = 3
381 post :create, :project_id => 1,
382 :time_entry => {:comments => str_utf8,
383 # Not the default activity
384 :activity_id => '11',
385 :issue_id => '',
386 :spent_on => '2011-11-10',
387 :hours => '7.3'}
388 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
389
390 t = TimeEntry.find_by_comments(str_utf8)
391 assert_not_nil t
392 assert_equal 11, t.activity_id
393 assert_equal 7.3, t.hours
394 assert_equal 3, t.user_id
395
396 get :index, :project_id => 1, :format => 'csv',
397 :from => '2011-11-10', :to => '2011-11-10'
398 assert_response :success
399 assert_equal 'text/csv', @response.content_type
400 ar = @response.body.chomp.split("\n")
401 s1 = "\xa4\xe9\xb4\xc1"
402 if str_utf8.respond_to?(:force_encoding)
403 s1.force_encoding('Big5')
404 end
405 assert ar[0].include?(s1)
406 s2 = ar[1].split(",")[8]
407 if s2.respond_to?(:force_encoding)
408 s3 = "\xa5H?"
409 s3.force_encoding('Big5')
410 assert_equal s3, s2
411 elsif RUBY_PLATFORM == 'java'
412 assert_equal "??", s2
413 else
414 assert_equal "\xa5H???", s2
415 end
416 end
371 417 end
General Comments 0
You need to be logged in to leave comments. Login now