Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

rewrite codeset_util specs

ulfertsd12b1e52eb92
diff --git a/spec/lib/redmine/codeset_util_spec.rb b/spec/lib/redmine/codeset_util_spec.rb
new file mode 100644
index 00000000000..73137387322
--- /dev/null
+++ b/spec/lib/redmine/codeset_util_spec.rb
@@ -0,0 +1,183 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# 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.
+#
+# 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.
+#
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe Redmine::CodesetUtil do
+ context 'with utf8 and latin1 setting', with_settings: { repositories_encodings: 'UTF-8,ISO-8859-1' } do
+ let(:s1) do
+ "Texte encod\xc3\xa9".tap do |s|
+ s.force_encoding('UTF-8')
+ end
+ end
+ let(:s2) do
+ "Texte encod\xe9".tap do |s|
+ s.force_encoding('ASCII-8BIT')
+ end
+ end
+ let(:s3) do
+ "Texte encod\xe9".tap do |s|
+ s.force_encoding('UTF-8')
+ end
+ end
+
+ it 'transforms from 8bit' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s2))
+ .to eql s1
+
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s2).encoding.to_s)
+ .to eql 'UTF-8'
+ end
+
+ it 'transforms from utf8' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s3))
+ .to eql s1
+
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s3).encoding.to_s)
+ .to eql 'UTF-8'
+ end
+ end
+
+ context 'with utf-8 and euc jp setting', with_settings: { repositories_encodings: 'UTF-8,EUC-JP' } do
+ let(:s1) do
+ "\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe3\x83\x9e\xe3\x82\xa4\xe3\x83\xb3".tap do |s|
+ s.force_encoding('UTF-8')
+ end
+ end
+ let(:s2) do
+ "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3".tap do |s|
+ s.force_encoding('ASCII-8BIT')
+ end
+ end
+ let(:s3) do
+ "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3".tap do |s|
+ s.force_encoding('UTF-8')
+ end
+ end
+
+ it 'transforms from 8bit' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s2))
+ .to eql s1
+
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s2).encoding.to_s)
+ .to eql 'UTF-8'
+ end
+
+ it 'transforms from utf8' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s3))
+ .to eql s1
+
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s3).encoding.to_s)
+ .to eql 'UTF-8'
+ end
+ end
+
+ context 'with latin1 only setting', with_settings: { repositories_encodings: 'ISO-8859-1' } do
+ let(:s1) do
+ "\xc3\x82\xc2\x80".tap do |s|
+ s.force_encoding('UTF-8')
+ end
+ end
+ let(:s2) do
+ "\xC2\x80".tap do |s|
+ s.force_encoding('ASCII-8BIT')
+ end
+ end
+ let(:s3) do
+ "\xC2\x80".tap do |s|
+ s.force_encoding('UTF-8')
+ end
+ end
+
+ it 'transforms from 8bit' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s2))
+ .to eql s1
+
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s2).encoding.to_s)
+ .to eql 'UTF-8'
+ end
+
+ it 'transforms from utf8' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s3))
+ .to eql s1
+
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s3).encoding.to_s)
+ .to eql 'UTF-8'
+ end
+ end
+
+ context 'with blank setting', with_settings: { repositories_encodings: '' } do
+ it 'transforms blank string' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(''))
+ .to eql ''
+ end
+
+ it 'transforms nil' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(nil))
+ .to eql nil
+ end
+
+ context 'with an invalid utf8 sequences' do
+ let(:s1) do
+ "Texte encod\xe9 en ISO-8859-1.".tap do |s|
+ s.force_encoding('ASCII-8BIT')
+ end
+ end
+
+ it 'returns a valid utf-8 encoding' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s1).encoding.to_s)
+ .to eql 'UTF-8'
+ end
+
+ it 'strips out the invalid char' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s1))
+ .to eql 'Texte encod? en ISO-8859-1.'
+ end
+ end
+ end
+
+ context 'with ja jis setting', with_settings: { repositories_encodings: 'ISO-2022-JP' } do
+ context 'with an invalid utf8 sequences' do
+ let(:s1) do
+ "test\xb5\xfetest\xb5\xfe".tap do |s|
+ s.force_encoding('ASCII-8BIT')
+ end
+ end
+
+ it 'returns a valid utf-8 encoding' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s1).encoding.to_s)
+ .to eql 'UTF-8'
+ end
+
+ it 'strips out the invalid char' do
+ expect(Redmine::CodesetUtil.to_utf8_by_setting(s1))
+ .to eql 'test??test??'
+ end
+ end
+ end
+end
diff --git a/spec_legacy/unit/lib/redmine/codeset_util_spec.rb b/spec_legacy/unit/lib/redmine/codeset_util_spec.rb
deleted file mode 100644
index 62a1f0b5d1c..00000000000
--- a/spec_legacy/unit/lib/redmine/codeset_util_spec.rb
+++ /dev/null
@@ -1,130 +0,0 @@
-#-- copyright
-# OpenProject is a project management system.
-# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2017 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# 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.
-#
-# 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.
-#
-# 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.
-#
-# See docs/COPYRIGHT.rdoc for more details.
-#++
-
-#-- encoding: UTF-8
-#-- copyright
-# ChiliProject is a project management system.
-#
-# Copyright (C) 2010-2011 the ChiliProject Team
-#
-# 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.
-#
-require 'legacy_spec_helper'
-
-describe Redmine::CodesetUtil do
- it 'should to utf8 by setting from latin1' do
- Setting.repositories_encodings = 'UTF-8,ISO-8859-1'
- s1 = "Texte encod\xc3\xa9"
- s2 = "Texte encod\xe9"
- s3 = s2.dup
- if s1.respond_to?(:force_encoding)
- s1.force_encoding('UTF-8')
- s2.force_encoding('ASCII-8BIT')
- s3.force_encoding('UTF-8')
- end
- assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
- assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
- end
-
- it 'should to utf8 by setting from euc jp' do
- Setting.repositories_encodings = 'UTF-8,EUC-JP'
- s1 = "\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe3\x83\x9e\xe3\x82\xa4\xe3\x83\xb3"
- s2 = "\xa5\xec\xa5\xc3\xa5\xc9\xa5\xde\xa5\xa4\xa5\xf3"
- s3 = s2.dup
- if s1.respond_to?(:force_encoding)
- s1.force_encoding('UTF-8')
- s2.force_encoding('ASCII-8BIT')
- s3.force_encoding('UTF-8')
- end
- assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
- assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
- end
-
- it 'should to utf8 by setting should be converted all latin1' do
- Setting.repositories_encodings = 'ISO-8859-1'
- s1 = "\xc3\x82\xc2\x80"
- s2 = "\xC2\x80"
- s3 = s2.dup
- if s1.respond_to?(:force_encoding)
- s1.force_encoding('UTF-8')
- s2.force_encoding('ASCII-8BIT')
- s3.force_encoding('UTF-8')
- end
- assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s2)
- assert_equal s1, Redmine::CodesetUtil.to_utf8_by_setting(s3)
- end
-
- it 'should to utf8 by setting blank string' do
- assert_equal '', Redmine::CodesetUtil.to_utf8_by_setting('')
- assert_equal nil, Redmine::CodesetUtil.to_utf8_by_setting(nil)
- end
-
- it 'should to utf8 by setting returns ascii as utf8' do
- s1 = 'ASCII'
- s2 = s1.dup
- if s1.respond_to?(:force_encoding)
- s1.force_encoding('UTF-8')
- s2.force_encoding('ISO-8859-1')
- end
- str1 = Redmine::CodesetUtil.to_utf8_by_setting(s1)
- str2 = Redmine::CodesetUtil.to_utf8_by_setting(s2)
- assert_equal s1, str1
- assert_equal s1, str2
- if s1.respond_to?(:force_encoding)
- assert_equal 'UTF-8', str1.encoding.to_s
- assert_equal 'UTF-8', str2.encoding.to_s
- end
- end
-
- it 'should to utf8 by setting invalid utf8 sequences should be stripped' do
- Setting.repositories_encodings = ''
- s1 = "Texte encod\xe9 en ISO-8859-1."
- s1.force_encoding('ASCII-8BIT') if s1.respond_to?(:force_encoding)
- str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
- if str.respond_to?(:force_encoding)
- assert str.valid_encoding?
- assert_equal 'UTF-8', str.encoding.to_s
- end
- assert_equal 'Texte encod? en ISO-8859-1.', str
- end
-
- it 'should to utf8 by setting invalid utf8 sequences should be stripped ja jis' do
- Setting.repositories_encodings = 'ISO-2022-JP'
- s1 = "test\xb5\xfetest\xb5\xfe"
- s1.force_encoding('ASCII-8BIT') if s1.respond_to?(:force_encoding)
- str = Redmine::CodesetUtil.to_utf8_by_setting(s1)
- if str.respond_to?(:force_encoding)
- assert str.valid_encoding?
- assert_equal 'UTF-8', str.encoding.to_s
- end
- assert_equal 'test??test??', str
- end
-end