OSDN Git Service

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