codeset_util_test.rb 3.83 KB
Newer Older
1
# frozen_string_literal: true
2

3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
5 6 7 8 9
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
10
#
11 12 13 14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
#
16 17 18 19
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

20
require File.expand_path('../../../../test_helper', __FILE__)
21

22
class Redmine::CodesetUtilTest < ActiveSupport::TestCase
23

24
  def test_to_utf8_by_setting_from_latin1
25
    with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
26
      s1 = 'Texte encodé'
27
      s2 = "Texte encod\xe9".b
28
      s3 = s2.dup.force_encoding("UTF-8")
29 30
      assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
      assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
31 32 33
    end
  end

34
  def test_to_utf8_by_setting_from_euc_jp
35
    with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
36
      s1 = 'レッドマイン'
37
      s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3".b
38
      s3 = s2.dup.force_encoding("UTF-8")
39 40
      assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
      assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
41 42 43
    end
  end

44
  def test_to_utf8_by_setting_should_be_converted_all_latin1
45
    with_settings :repositories_encodings => 'ISO-8859-1' do
46
      s1 = \u0080"
47
      s2 = "\xC2\x80".b
48
      s3 = s2.dup.force_encoding("UTF-8")
49 50
      assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
      assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
51 52
    end
  end
53

54 55
  def test_to_utf8_by_setting_blank_string
    assert_equal "",  Redmine::CodesetUtil.to_utf8_by_setting("")
56
    assert_nil Redmine::CodesetUtil.to_utf8_by_setting(nil)
57
  end
58

59
  def test_to_utf8_by_setting_returns_ascii_as_utf8
60
    s1 = 'ASCII'
61
    s2 = s1.dup.force_encoding("ISO-8859-1")
62 63
    str1 = Redmine::CodesetUtil.to_utf8_by_setting(s1)
    str2 = Redmine::CodesetUtil.to_utf8_by_setting(s2)
64 65
    assert_equal s1, str1
    assert_equal s1, str2
66 67
    assert_equal "UTF-8", str1.encoding.to_s
    assert_equal "UTF-8", str2.encoding.to_s
68
  end
69

70
  def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped
71
    with_settings :repositories_encodings => '' do
72
      s1 = "Texte encod\xe9 en ISO-8859-1.".b
73
      str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
74 75
      assert str.valid_encoding?
      assert_equal "UTF-8", str.encoding.to_s
76
      assert_equal "Texte encod? en ISO-8859-1.", str
77 78
    end
  end
79

80
  def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped_ja_jis
81
    with_settings :repositories_encodings => 'ISO-2022-JP' do
82
      s1 = "test\xb5\xfetest\xb5\xfe".b
83
      str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
84 85
      assert str.valid_encoding?
      assert_equal "UTF-8", str.encoding.to_s
86 87 88
      assert_equal "test??test??", str
    end
  end
marutosijp's avatar
marutosijp committed
89 90

  test "#replace_invalid_utf8 should replace invalid utf8" do
91
    s1 = "こんにち\xE3\x81\xFF"
marutosijp's avatar
marutosijp committed
92 93 94
    s2 = Redmine::CodesetUtil.replace_invalid_utf8(s1)
    assert s2.valid_encoding?
    assert_equal "UTF-8", s2.encoding.to_s
95
    assert_equal 'こんにち??', s2
marutosijp's avatar
marutosijp committed
96 97 98
  end

  test "#to_utf8 should replace invalid non utf8" do
99
    s1 = (+"\xa4\xb3\xa4\xf3\xa4\xcb\xa4\xc1\xa4").force_encoding("EUC-JP")
marutosijp's avatar
marutosijp committed
100 101 102
    s2 = Redmine::CodesetUtil.to_utf8(s1, "EUC-JP")
    assert s2.valid_encoding?
    assert_equal "UTF-8", s2.encoding.to_s
103
    assert_equal 'こんにち?', s2
marutosijp's avatar
marutosijp committed
104
  end
105
end