You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

codeset_util_test.rb 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../../../test_helper', __FILE__)
  19. class Redmine::CodesetUtilTest < ActiveSupport::TestCase
  20. def test_to_utf8_by_setting_from_latin1
  21. with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
  22. s1 = 'Texte encodé'
  23. s2 = "Texte encod\xe9".b
  24. s3 = s2.dup.force_encoding("UTF-8")
  25. assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
  26. assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
  27. end
  28. end
  29. def test_to_utf8_by_setting_from_euc_jp
  30. with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
  31. s1 = 'レッドマイン'
  32. s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3".b
  33. s3 = s2.dup.force_encoding("UTF-8")
  34. assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
  35. assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
  36. end
  37. end
  38. def test_to_utf8_by_setting_should_be_converted_all_latin1
  39. with_settings :repositories_encodings => 'ISO-8859-1' do
  40. s1 = "Â\u0080"
  41. s2 = "\xC2\x80".b
  42. s3 = s2.dup.force_encoding("UTF-8")
  43. assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
  44. assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
  45. end
  46. end
  47. def test_to_utf8_by_setting_blank_string
  48. assert_equal "", Redmine::CodesetUtil.to_utf8_by_setting("")
  49. assert_nil Redmine::CodesetUtil.to_utf8_by_setting(nil)
  50. end
  51. def test_to_utf8_by_setting_returns_ascii_as_utf8
  52. s1 = 'ASCII'
  53. s2 = s1.dup.force_encoding("ISO-8859-1")
  54. str1 = Redmine::CodesetUtil.to_utf8_by_setting(s1)
  55. str2 = Redmine::CodesetUtil.to_utf8_by_setting(s2)
  56. assert_equal s1, str1
  57. assert_equal s1, str2
  58. assert_equal "UTF-8", str1.encoding.to_s
  59. assert_equal "UTF-8", str2.encoding.to_s
  60. end
  61. def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped
  62. with_settings :repositories_encodings => '' do
  63. s1 = "Texte encod\xe9 en ISO-8859-1.".b
  64. str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
  65. assert str.valid_encoding?
  66. assert_equal "UTF-8", str.encoding.to_s
  67. assert_equal "Texte encod? en ISO-8859-1.", str
  68. end
  69. end
  70. def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped_ja_jis
  71. with_settings :repositories_encodings => 'ISO-2022-JP' do
  72. s1 = "test\xb5\xfetest\xb5\xfe".b
  73. str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
  74. assert str.valid_encoding?
  75. assert_equal "UTF-8", str.encoding.to_s
  76. assert_equal "test??test??", str
  77. end
  78. end
  79. test "#replace_invalid_utf8 should replace invalid utf8" do
  80. s1 = "こんにち\xE3\x81\xFF"
  81. s2 = Redmine::CodesetUtil.replace_invalid_utf8(s1)
  82. assert s2.valid_encoding?
  83. assert_equal "UTF-8", s2.encoding.to_s
  84. assert_equal 'こんにち??', s2
  85. end
  86. test "#to_utf8 should replace invalid non utf8" do
  87. s1 = (+"\xa4\xb3\xa4\xf3\xa4\xcb\xa4\xc1\xa4").force_encoding("EUC-JP")
  88. s2 = Redmine::CodesetUtil.to_utf8(s1, "EUC-JP")
  89. assert s2.valid_encoding?
  90. assert_equal "UTF-8", s2.encoding.to_s
  91. assert_equal 'こんにち?', s2
  92. end
  93. def test_guess_encoding_should_return_guessed_encoding
  94. str = '日本語'.encode('Windows-31J').b
  95. with_settings :repositories_encodings => 'UTF-8,Windows-31J' do
  96. assert_equal 'Windows-31J', Redmine::CodesetUtil.guess_encoding(str)
  97. end
  98. with_settings :repositories_encodings => 'UTF-8,csWindows31J' do
  99. assert_equal 'csWindows31J', Redmine::CodesetUtil.guess_encoding(str)
  100. end
  101. end
  102. def guess_encoding_should_return_nil_if_cannot_guess_encoding
  103. str = '日本語'.encode('Windows-31J').b
  104. with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
  105. assert_nil Redmine::CodesetUtil.guess_encoding(str)
  106. end
  107. end
  108. end