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

app/helpers/issues_helper.rb
test/functional/issues_controller_test.rb

index b52200659bdad2a0dab9e7a7396959dc77c3ec49..9ef9e0458260ede514d3fbadaa33d84f2b6e9572 100644 (file)
@@ -264,7 +264,6 @@ module IssuesHelper
   end
 
   def issues_to_csv(issues, project = nil)
-    ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
     decimal_separator = l(:general_csv_decimal_separator)
     export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
       # csv header fields
@@ -292,7 +291,9 @@ module IssuesHelper
       custom_fields.each {|f| headers << f.name}
       # Description in the last column
       headers << l(:field_description)
-      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
       issues.each do |issue|
         fields = [issue.id,
@@ -315,7 +316,9 @@ module IssuesHelper
                   ]
         custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
         fields << issue.description
-        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
     export
index bdee16eadde82758d8a97c98aae0c109d48cbc34..b17e2aabe4f1480ae76809a8331812fda56a59b1 100644 (file)
@@ -324,6 +324,41 @@ class IssuesControllerTest < ActionController::TestCase
     end
   end
 
+  def test_index_csv_cannot_convert_should_be_replaced_big_5
+    with_settings :default_language => "zh-TW" do
+      str_utf8  = "\xe4\xbb\xa5\xe5\x86\x85"
+      if str_utf8.respond_to?(:force_encoding)
+        str_utf8.force_encoding('UTF-8')
+      end
+      issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
+                        :status_id => 1, :priority => IssuePriority.all.first,
+                        :subject => str_utf8)
+      assert issue.save
+
+      get :index, :project_id => 1, 
+                  :f => ['subject'], 
+                  :op => '=', :values => [str_utf8],
+                  :format => 'csv'
+      assert_equal 'text/csv', @response.content_type
+      lines = @response.body.chomp.split("\n")    
+      s1 = "\xaa\xac\xbaA"
+      if str_utf8.respond_to?(:force_encoding)
+        s1.force_encoding('Big5')
+      end
+      assert lines[0].include?(s1)
+      s2 = lines[1].split(",")[5]
+      if s1.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
+
   def test_index_pdf
     get :index, :format => 'pdf'
     assert_response :success