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