]> source.dussan.org Git - redmine.git/commitdiff
fix malformed time entry report csv encoding in case of unable to convert (#8549)
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Wed, 16 Nov 2011 08:29:18 +0000 (08:29 +0000)
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Wed, 16 Nov 2011 08:29:18 +0000 (08:29 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7820 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/timelog_helper.rb
test/functional/time_entry_reports_controller_test.rb

index 6b34e60b0ac3863da6a61e8996a34f5a0d041b3f..86acfe586ba82c10afb3fa01d14f4b9ba626dbcd 100644 (file)
@@ -147,7 +147,9 @@ module TimelogHelper
       headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
       headers += periods
       headers << l(:label_total)
-      csv << headers.collect {|c| to_utf8(c) }
+      csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
+                                    c.to_s,
+                                    l(:general_csv_encoding) ) }
       # Content
       report_criteria_to_csv(csv, criterias, periods, hours)
       # Total row
@@ -169,7 +171,9 @@ module TimelogHelper
       hours_for_value = select_hours(hours, criterias[level], value)
       next if hours_for_value.empty?
       row = [''] * level
-      row << to_utf8(format_criteria_value(criterias[level], value))
+      row << Redmine::CodesetUtil.from_utf8(
+                        format_criteria_value(criterias[level], value).to_s,
+                        l(:general_csv_encoding) )
       row += [''] * (criterias.length - level - 1)
       total = 0
       periods.each do |period|
@@ -185,9 +189,4 @@ module TimelogHelper
       end
     end
   end
-
-  def to_utf8(s)
-    @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
-    begin; @ic.iconv(s.to_s); rescue; s.to_s; end
-  end
 end
index 04b755a10475ab3d7d438875a11e818b535fd610..c6a41122e2a8eabcb20e627d4207a892af58515c 100644 (file)
@@ -188,4 +188,52 @@ class TimeEntryReportsControllerTest < ActionController::TestCase
     # Total row
     assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
   end
+
+  def test_csv_cannot_convert_should_be_replaced_big_5
+    Setting.default_language = "zh-TW"
+    str_utf8  = "\xe4\xbb\xa5\xe5\x86\x85"
+    if str_utf8.respond_to?(:force_encoding)
+      str_utf8.force_encoding('UTF-8')
+    end
+    user = User.find_by_id(3)
+    user.firstname = str_utf8
+    user.lastname  = "test-lastname"
+    assert user.save
+    comments = "test_replaced"
+    te1 = TimeEntry.create(:spent_on => '2011-11-11',
+                           :hours    => 7.3,
+                           :project  => Project.find(1),
+                           :user     => user,
+                           :activity => TimeEntryActivity.find_by_name('Design'),
+                           :comments => comments)
+
+    te2 = TimeEntry.find_by_comments(comments)
+    assert_not_nil te2
+    assert_equal 7.3, te2.hours
+    assert_equal 3, te2.user_id
+
+    get :report, :project_id => 1, :columns => 'day',
+        :from => "2011-11-11", :to => "2011-11-11",
+        :criterias => ["member"], :format => "csv"
+    assert_response :success
+    assert_equal 'text/csv', @response.content_type
+    lines = @response.body.chomp.split("\n")    
+    # Headers
+    s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
+    if s1.respond_to?(:force_encoding)
+      s1.force_encoding('Big5')
+    end
+    assert_equal s1, lines.first
+    # Total row
+    s2 = ""
+    if s2.respond_to?(:force_encoding)
+      s2 = "\xa5H?"
+      s2.force_encoding('Big5')
+    elsif RUBY_PLATFORM == 'java'
+      s2 = "??"
+    else
+      s2 = "\xa5H???"
+    end
+    assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
+  end
 end