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@507
|
27 if RUBY_VERSION < '1.9'
|
Chris@507
|
28 if RUBY_PLATFORM == 'java'
|
Chris@507
|
29 ic = Iconv.new("SJIS", 'UTF-8')
|
Chris@507
|
30 else
|
Chris@507
|
31 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
|
Chris@507
|
32 end
|
Chris@441
|
33 end
|
Chris@441
|
34 assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, nil)
|
Chris@441
|
35 end
|
Chris@441
|
36
|
Chris@441
|
37 def test_rdm_pdf_iconv_cannot_convert_ja_cp932
|
Chris@441
|
38 set_language_if_valid 'ja'
|
Chris@441
|
39 assert_equal 'CP932', l(:general_pdf_encoding)
|
Chris@441
|
40 if RUBY_VERSION < '1.9'
|
Chris@507
|
41 if RUBY_PLATFORM == 'java'
|
Chris@507
|
42 ic = Iconv.new("SJIS", 'UTF-8')
|
Chris@507
|
43 else
|
Chris@507
|
44 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
|
Chris@507
|
45 end
|
Chris@441
|
46 end
|
Chris@441
|
47 utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b"
|
Chris@441
|
48 utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
|
Chris@441
|
49 utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
|
Chris@441
|
50 if utf8_txt_1.respond_to?(:force_encoding)
|
Chris@441
|
51 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_1)
|
Chris@441
|
52 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2)
|
Chris@441
|
53 txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3)
|
Chris@441
|
54 assert_equal "?\x91\xd4", txt_1
|
Chris@441
|
55 assert_equal "?\x91\xd4?", txt_2
|
Chris@441
|
56 assert_equal "??\x91\xd4?", txt_3
|
Chris@441
|
57 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
|
Chris@441
|
58 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
|
Chris@441
|
59 assert_equal "ASCII-8BIT", txt_3.encoding.to_s
|
Chris@507
|
60 elsif RUBY_PLATFORM == 'java'
|
Chris@507
|
61 assert_equal "??",
|
Chris@507
|
62 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_1)
|
Chris@507
|
63 assert_equal "???",
|
Chris@507
|
64 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2)
|
Chris@507
|
65 assert_equal "????",
|
Chris@507
|
66 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3)
|
Chris@441
|
67 else
|
Chris@441
|
68 assert_equal "???\x91\xd4",
|
Chris@441
|
69 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_1)
|
Chris@441
|
70 assert_equal "???\x91\xd4???",
|
Chris@441
|
71 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2)
|
Chris@441
|
72 assert_equal "??????\x91\xd4???",
|
Chris@441
|
73 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3)
|
Chris@441
|
74 end
|
Chris@441
|
75 end
|
Chris@441
|
76
|
Chris@441
|
77 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en
|
Chris@441
|
78 set_language_if_valid 'en'
|
Chris@441
|
79 assert_equal 'UTF-8', l(:general_pdf_encoding)
|
Chris@441
|
80 str1 = "Texte encod\xe9 en ISO-8859-1"
|
Chris@441
|
81 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
|
Chris@441
|
82 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
|
Chris@441
|
83 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
|
Chris@441
|
84 if RUBY_VERSION < '1.9'
|
Chris@441
|
85 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
|
Chris@441
|
86 end
|
Chris@441
|
87 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1)
|
Chris@441
|
88 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2)
|
Chris@441
|
89 if txt_1.respond_to?(:force_encoding)
|
Chris@441
|
90 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
|
Chris@441
|
91 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
|
Chris@441
|
92 end
|
Chris@441
|
93 assert_equal "Texte encod? en ISO-8859-1", txt_1
|
Chris@441
|
94 assert_equal "?a?b?c?d?e test", txt_2
|
Chris@441
|
95 end
|
Chris@441
|
96
|
Chris@441
|
97 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja
|
Chris@441
|
98 set_language_if_valid 'ja'
|
Chris@441
|
99 assert_equal 'CP932', l(:general_pdf_encoding)
|
Chris@441
|
100 str1 = "Texte encod\xe9 en ISO-8859-1"
|
Chris@441
|
101 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
|
Chris@441
|
102 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
|
Chris@441
|
103 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
|
Chris@441
|
104 if RUBY_VERSION < '1.9'
|
Chris@507
|
105 if RUBY_PLATFORM == 'java'
|
Chris@507
|
106 ic = Iconv.new("SJIS", 'UTF-8')
|
Chris@507
|
107 else
|
Chris@507
|
108 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
|
Chris@507
|
109 end
|
Chris@441
|
110 end
|
Chris@441
|
111 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1)
|
Chris@441
|
112 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2)
|
Chris@441
|
113 if txt_1.respond_to?(:force_encoding)
|
Chris@441
|
114 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
|
Chris@441
|
115 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
|
Chris@441
|
116 end
|
Chris@441
|
117 assert_equal "Texte encod? en ISO-8859-1", txt_1
|
Chris@441
|
118 assert_equal "?a?b?c?d?e test", txt_2
|
Chris@441
|
119 end
|
Chris@441
|
120 end
|