fix malformed time log csv encoding in case of unable to convert (#8549)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7819 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
8433bbab69
commit
4b5d50e40a
|
@ -84,7 +84,6 @@ module TimelogHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def entries_to_csv(entries)
|
def entries_to_csv(entries)
|
||||||
ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
|
|
||||||
decimal_separator = l(:general_csv_decimal_separator)
|
decimal_separator = l(:general_csv_decimal_separator)
|
||||||
custom_fields = TimeEntryCustomField.find(:all)
|
custom_fields = TimeEntryCustomField.find(:all)
|
||||||
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
|
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
|
||||||
|
@ -102,7 +101,9 @@ module TimelogHelper
|
||||||
# Export custom fields
|
# Export custom fields
|
||||||
headers += custom_fields.collect(&:name)
|
headers += custom_fields.collect(&:name)
|
||||||
|
|
||||||
csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
|
csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
|
||||||
|
c.to_s,
|
||||||
|
l(:general_csv_encoding) ) }
|
||||||
# csv lines
|
# csv lines
|
||||||
entries.each do |entry|
|
entries.each do |entry|
|
||||||
fields = [format_date(entry.spent_on),
|
fields = [format_date(entry.spent_on),
|
||||||
|
@ -117,7 +118,9 @@ module TimelogHelper
|
||||||
]
|
]
|
||||||
fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
|
fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
|
||||||
|
|
||||||
csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
|
csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
|
||||||
|
c.to_s,
|
||||||
|
l(:general_csv_encoding) ) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
export
|
export
|
||||||
|
|
|
@ -368,4 +368,50 @@ class TimelogControllerTest < ActionController::TestCase
|
||||||
assert ar[0].include?(s1)
|
assert ar[0].include?(s1)
|
||||||
assert ar[1].include?(str_big5)
|
assert ar[1].include?(str_big5)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_csv_cannot_convert_should_be_replaced_big_5
|
||||||
|
user = User.find_by_id(3)
|
||||||
|
user.language = "zh-TW"
|
||||||
|
assert user.save
|
||||||
|
str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
|
||||||
|
if str_utf8.respond_to?(:force_encoding)
|
||||||
|
str_utf8.force_encoding('UTF-8')
|
||||||
|
end
|
||||||
|
@request.session[:user_id] = 3
|
||||||
|
post :create, :project_id => 1,
|
||||||
|
:time_entry => {:comments => str_utf8,
|
||||||
|
# Not the default activity
|
||||||
|
:activity_id => '11',
|
||||||
|
:issue_id => '',
|
||||||
|
:spent_on => '2011-11-10',
|
||||||
|
:hours => '7.3'}
|
||||||
|
assert_redirected_to :action => 'index', :project_id => 'ecookbook'
|
||||||
|
|
||||||
|
t = TimeEntry.find_by_comments(str_utf8)
|
||||||
|
assert_not_nil t
|
||||||
|
assert_equal 11, t.activity_id
|
||||||
|
assert_equal 7.3, t.hours
|
||||||
|
assert_equal 3, t.user_id
|
||||||
|
|
||||||
|
get :index, :project_id => 1, :format => 'csv',
|
||||||
|
:from => '2011-11-10', :to => '2011-11-10'
|
||||||
|
assert_response :success
|
||||||
|
assert_equal 'text/csv', @response.content_type
|
||||||
|
ar = @response.body.chomp.split("\n")
|
||||||
|
s1 = "\xa4\xe9\xb4\xc1"
|
||||||
|
if str_utf8.respond_to?(:force_encoding)
|
||||||
|
s1.force_encoding('Big5')
|
||||||
|
end
|
||||||
|
assert ar[0].include?(s1)
|
||||||
|
s2 = ar[1].split(",")[8]
|
||||||
|
if s2.respond_to?(:force_encoding)
|
||||||
|
s3 = "\xa5H?"
|
||||||
|
s3.force_encoding('Big5')
|
||||||
|
assert_equal s3, s2
|
||||||
|
elsif RUBY_PLATFORM == 'java'
|
||||||
|
assert_equal "??", s2
|
||||||
|
else
|
||||||
|
assert_equal "\xa5H???", s2
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue