2019-03-15 01:32:57 +00:00
|
|
|
# frozen_string_literal: false
|
|
|
|
|
|
2011-02-23 07:04:52 +00:00
|
|
|
# Redmine - project management software
|
2017-06-25 08:40:31 +00:00
|
|
|
# Copyright (C) 2006-2017 Jean-Philippe Lang
|
2011-02-23 07:04:52 +00:00
|
|
|
#
|
|
|
|
|
# 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.
|
2011-06-22 00:57:17 +00:00
|
|
|
#
|
2011-02-23 07:04:52 +00:00
|
|
|
# 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.
|
2011-06-22 00:57:17 +00:00
|
|
|
#
|
2011-02-23 07:04:52 +00:00
|
|
|
# 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.
|
|
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
require File.expand_path('../../../../test_helper', __FILE__)
|
2011-02-23 07:04:52 +00:00
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
class Redmine::CodesetUtilTest < ActiveSupport::TestCase
|
2011-02-23 07:04:52 +00:00
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
def test_to_utf8_by_setting_from_latin1
|
2011-02-23 07:04:52 +00:00
|
|
|
with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
|
2019-03-19 15:43:55 +00:00
|
|
|
s1 = 'Texte encodé'
|
|
|
|
|
s2 = (+"Texte encod\xe9").force_encoding("ASCII-8BIT")
|
2014-10-22 17:37:16 +00:00
|
|
|
s3 = s2.dup.force_encoding("UTF-8")
|
2011-11-18 14:57:58 +00:00
|
|
|
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
|
|
|
|
|
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
|
2011-02-23 07:04:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
def test_to_utf8_by_setting_from_euc_jp
|
2011-02-23 07:04:52 +00:00
|
|
|
with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
|
2019-03-19 15:43:55 +00:00
|
|
|
s1 = 'レッドマイン'
|
|
|
|
|
s2 = (+"\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3").force_encoding("ASCII-8BIT")
|
2014-10-22 17:37:16 +00:00
|
|
|
s3 = s2.dup.force_encoding("UTF-8")
|
2011-11-18 14:57:58 +00:00
|
|
|
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
|
|
|
|
|
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
|
2011-02-23 07:04:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
def test_to_utf8_by_setting_should_be_converted_all_latin1
|
2011-02-23 07:04:52 +00:00
|
|
|
with_settings :repositories_encodings => 'ISO-8859-1' do
|
2019-03-19 15:43:55 +00:00
|
|
|
s1 = "Â\u0080"
|
|
|
|
|
s2 = (+"\xC2\x80").force_encoding("ASCII-8BIT")
|
2014-10-22 17:37:16 +00:00
|
|
|
s3 = s2.dup.force_encoding("UTF-8")
|
2011-11-18 14:57:58 +00:00
|
|
|
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
|
|
|
|
|
assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
|
2011-02-23 07:04:52 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-03-17 03:51:46 +00:00
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
def test_to_utf8_by_setting_blank_string
|
|
|
|
|
assert_equal "", Redmine::CodesetUtil.to_utf8_by_setting("")
|
2016-12-11 15:26:41 +00:00
|
|
|
assert_nil Redmine::CodesetUtil.to_utf8_by_setting(nil)
|
2011-03-17 03:51:46 +00:00
|
|
|
end
|
2011-02-23 07:04:52 +00:00
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
def test_to_utf8_by_setting_returns_ascii_as_utf8
|
2019-03-19 15:43:55 +00:00
|
|
|
s1 = 'ASCII'
|
2014-10-22 17:37:16 +00:00
|
|
|
s2 = s1.dup.force_encoding("ISO-8859-1")
|
2011-11-18 14:57:58 +00:00
|
|
|
str1 = Redmine::CodesetUtil.to_utf8_by_setting(s1)
|
|
|
|
|
str2 = Redmine::CodesetUtil.to_utf8_by_setting(s2)
|
2011-03-19 05:46:25 +00:00
|
|
|
assert_equal s1, str1
|
|
|
|
|
assert_equal s1, str2
|
2014-10-22 17:37:16 +00:00
|
|
|
assert_equal "UTF-8", str1.encoding.to_s
|
|
|
|
|
assert_equal "UTF-8", str2.encoding.to_s
|
2011-03-19 05:46:25 +00:00
|
|
|
end
|
2011-03-19 05:46:47 +00:00
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped
|
2011-03-19 05:46:47 +00:00
|
|
|
with_settings :repositories_encodings => '' do
|
2019-03-19 15:43:55 +00:00
|
|
|
s1 = (+"Texte encod\xe9 en ISO-8859-1.").force_encoding("ASCII-8BIT")
|
2011-11-18 14:57:58 +00:00
|
|
|
str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
|
2014-10-22 17:37:16 +00:00
|
|
|
assert str.valid_encoding?
|
|
|
|
|
assert_equal "UTF-8", str.encoding.to_s
|
2011-04-12 05:03:59 +00:00
|
|
|
assert_equal "Texte encod? en ISO-8859-1.", str
|
2011-03-19 05:46:47 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-04-12 05:04:45 +00:00
|
|
|
|
2011-11-18 14:57:58 +00:00
|
|
|
def test_to_utf8_by_setting_invalid_utf8_sequences_should_be_stripped_ja_jis
|
2011-04-12 05:04:45 +00:00
|
|
|
with_settings :repositories_encodings => 'ISO-2022-JP' do
|
2019-03-19 15:43:55 +00:00
|
|
|
s1 = (+"test\xb5\xfetest\xb5\xfe").force_encoding("ASCII-8BIT")
|
2011-11-18 14:57:58 +00:00
|
|
|
str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
|
2014-10-22 17:37:16 +00:00
|
|
|
assert str.valid_encoding?
|
|
|
|
|
assert_equal "UTF-8", str.encoding.to_s
|
2011-04-12 05:04:45 +00:00
|
|
|
assert_equal "test??test??", str
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-01-28 05:43:22 +00:00
|
|
|
|
|
|
|
|
test "#replace_invalid_utf8 should replace invalid utf8" do
|
2019-03-19 15:43:55 +00:00
|
|
|
s1 = "こんにち\xE3\x81\xFF"
|
2017-01-28 05:43:22 +00:00
|
|
|
s2 = Redmine::CodesetUtil.replace_invalid_utf8(s1)
|
|
|
|
|
assert s2.valid_encoding?
|
|
|
|
|
assert_equal "UTF-8", s2.encoding.to_s
|
2019-03-19 15:43:55 +00:00
|
|
|
assert_equal 'こんにち??', s2
|
2017-01-28 05:43:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "#to_utf8 should replace invalid non utf8" do
|
2019-03-19 15:43:55 +00:00
|
|
|
s1 = (+"\xa4\xb3\xa4\xf3\xa4\xcb\xa4\xc1\xa4").force_encoding("EUC-JP")
|
2017-01-28 05:43:22 +00:00
|
|
|
s2 = Redmine::CodesetUtil.to_utf8(s1, "EUC-JP")
|
|
|
|
|
assert s2.valid_encoding?
|
|
|
|
|
assert_equal "UTF-8", s2.encoding.to_s
|
2019-03-19 15:43:55 +00:00
|
|
|
assert_equal 'こんにち?', s2
|
2017-01-28 05:43:22 +00:00
|
|
|
end
|
2011-03-19 05:46:25 +00:00
|
|
|
end
|