Chris@441
|
1 # Redmine - project management software
|
Chris@441
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@441
|
3 #
|
Chris@441
|
4 # This program is free software; you can redistribute it and/or
|
Chris@441
|
5 # modify it under the terms of the GNU General Public License
|
Chris@441
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@441
|
7 # of the License, or (at your option) any later version.
|
Chris@441
|
8 #
|
Chris@441
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@441
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@441
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@441
|
12 # GNU General Public License for more details.
|
Chris@441
|
13 #
|
Chris@441
|
14 # You should have received a copy of the GNU General Public License
|
Chris@441
|
15 # along with this program; if not, write to the Free Software
|
Chris@441
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@441
|
17
|
Chris@441
|
18 require File.expand_path('../../../../../test_helper', __FILE__)
|
Chris@441
|
19 require 'iconv'
|
Chris@441
|
20
|
Chris@441
|
21 class PdfTest < ActiveSupport::TestCase
|
Chris@441
|
22 include Redmine::I18n
|
Chris@441
|
23
|
Chris@441
|
24 def test_fix_text_encoding_nil
|
Chris@441
|
25 set_language_if_valid 'ja'
|
Chris@441
|
26 assert_equal 'CP932', l(:general_pdf_encoding)
|
Chris@441
|
27 if RUBY_VERSION < '1.9'
|
Chris@441
|
28 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
|
Chris@441
|
29 end
|
Chris@441
|
30 assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, nil)
|
Chris@441
|
31 end
|
Chris@441
|
32
|
Chris@441
|
33 def test_rdm_pdf_iconv_cannot_convert_ja_cp932
|
Chris@441
|
34 set_language_if_valid 'ja'
|
Chris@441
|
35 assert_equal 'CP932', l(:general_pdf_encoding)
|
Chris@441
|
36 if RUBY_VERSION < '1.9'
|
Chris@441
|
37 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
|
Chris@441
|
38 end
|
Chris@441
|
39 utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b"
|
Chris@441
|
40 utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
|
Chris@441
|
41 utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
|
Chris@441
|
42 if utf8_txt_1.respond_to?(:force_encoding)
|
Chris@441
|
43 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_1)
|
Chris@441
|
44 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2)
|
Chris@441
|
45 txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3)
|
Chris@441
|
46 assert_equal "?\x91\xd4", txt_1
|
Chris@441
|
47 assert_equal "?\x91\xd4?", txt_2
|
Chris@441
|
48 assert_equal "??\x91\xd4?", txt_3
|
Chris@441
|
49 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
|
Chris@441
|
50 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
|
Chris@441
|
51 assert_equal "ASCII-8BIT", txt_3.encoding.to_s
|
Chris@441
|
52 else
|
Chris@441
|
53 assert_equal "???\x91\xd4",
|
Chris@441
|
54 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_1)
|
Chris@441
|
55 assert_equal "???\x91\xd4???",
|
Chris@441
|
56 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2)
|
Chris@441
|
57 assert_equal "??????\x91\xd4???",
|
Chris@441
|
58 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3)
|
Chris@441
|
59 end
|
Chris@441
|
60 end
|
Chris@441
|
61
|
Chris@441
|
62 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en
|
Chris@441
|
63 set_language_if_valid 'en'
|
Chris@441
|
64 assert_equal 'UTF-8', l(:general_pdf_encoding)
|
Chris@441
|
65 str1 = "Texte encod\xe9 en ISO-8859-1"
|
Chris@441
|
66 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
|
Chris@441
|
67 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
|
Chris@441
|
68 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
|
Chris@441
|
69 if RUBY_VERSION < '1.9'
|
Chris@441
|
70 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
|
Chris@441
|
71 end
|
Chris@441
|
72 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1)
|
Chris@441
|
73 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2)
|
Chris@441
|
74 if txt_1.respond_to?(:force_encoding)
|
Chris@441
|
75 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
|
Chris@441
|
76 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
|
Chris@441
|
77 end
|
Chris@441
|
78 assert_equal "Texte encod? en ISO-8859-1", txt_1
|
Chris@441
|
79 assert_equal "?a?b?c?d?e test", txt_2
|
Chris@441
|
80 end
|
Chris@441
|
81
|
Chris@441
|
82 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja
|
Chris@441
|
83 set_language_if_valid 'ja'
|
Chris@441
|
84 assert_equal 'CP932', l(:general_pdf_encoding)
|
Chris@441
|
85 str1 = "Texte encod\xe9 en ISO-8859-1"
|
Chris@441
|
86 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
|
Chris@441
|
87 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
|
Chris@441
|
88 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
|
Chris@441
|
89 if RUBY_VERSION < '1.9'
|
Chris@441
|
90 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
|
Chris@441
|
91 end
|
Chris@441
|
92 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1)
|
Chris@441
|
93 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2)
|
Chris@441
|
94 if txt_1.respond_to?(:force_encoding)
|
Chris@441
|
95 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
|
Chris@441
|
96 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
|
Chris@441
|
97 end
|
Chris@441
|
98 assert_equal "Texte encod? en ISO-8859-1", txt_1
|
Chris@441
|
99 assert_equal "?a?b?c?d?e test", txt_2
|
Chris@441
|
100 end
|
Chris@441
|
101 end
|