comparison test/unit/lib/redmine/export/pdf_test.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents 0c939c159af4
children 433d4f72a19b
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
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 fixtures :users, :projects, :roles, :members, :member_roles,
23 :enabled_modules, :issues, :trackers, :attachments
23 24
24 def test_fix_text_encoding_nil 25 def test_fix_text_encoding_nil
25 set_language_if_valid 'ja' 26 assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "UTF-8")
26 assert_equal 'CP932', l(:general_pdf_encoding) 27 assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "ISO-8859-1")
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 28 end
36 29
37 def test_rdm_pdf_iconv_cannot_convert_ja_cp932 30 def test_rdm_pdf_iconv_cannot_convert_ja_cp932
38 set_language_if_valid 'ja' 31 encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
39 assert_equal 'CP932', l(:general_pdf_encoding)
40 if RUBY_VERSION < '1.9'
41 if RUBY_PLATFORM == 'java'
42 ic = Iconv.new("SJIS", 'UTF-8')
43 else
44 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
45 end
46 end
47 utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b" 32 utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b"
48 utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80" 33 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" 34 utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
50 if utf8_txt_1.respond_to?(:force_encoding) 35 if utf8_txt_1.respond_to?(:force_encoding)
51 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_1) 36 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
52 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2) 37 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
53 txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3) 38 txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding)
54 assert_equal "?\x91\xd4", txt_1 39 assert_equal "?\x91\xd4", txt_1
55 assert_equal "?\x91\xd4?", txt_2 40 assert_equal "?\x91\xd4?", txt_2
56 assert_equal "??\x91\xd4?", txt_3 41 assert_equal "??\x91\xd4?", txt_3
57 assert_equal "ASCII-8BIT", txt_1.encoding.to_s 42 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
58 assert_equal "ASCII-8BIT", txt_2.encoding.to_s 43 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
59 assert_equal "ASCII-8BIT", txt_3.encoding.to_s 44 assert_equal "ASCII-8BIT", txt_3.encoding.to_s
60 elsif RUBY_PLATFORM == 'java' 45 elsif RUBY_PLATFORM == 'java'
61 assert_equal "??", 46 assert_equal "??",
62 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_1) 47 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
63 assert_equal "???", 48 assert_equal "???",
64 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2) 49 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
65 assert_equal "????", 50 assert_equal "????",
66 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3) 51 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding)
67 else 52 else
68 assert_equal "???\x91\xd4", 53 assert_equal "???\x91\xd4",
69 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_1) 54 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
70 assert_equal "???\x91\xd4???", 55 assert_equal "???\x91\xd4???",
71 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_2) 56 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
72 assert_equal "??????\x91\xd4???", 57 assert_equal "??????\x91\xd4???",
73 Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, utf8_txt_3) 58 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding)
74 end 59 end
75 end 60 end
76 61
77 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en 62 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en
78 set_language_if_valid 'en'
79 assert_equal 'UTF-8', l(:general_pdf_encoding)
80 str1 = "Texte encod\xe9 en ISO-8859-1" 63 str1 = "Texte encod\xe9 en ISO-8859-1"
81 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test" 64 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
82 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding) 65 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
83 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding) 66 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
84 if RUBY_VERSION < '1.9' 67 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, 'UTF-8')
85 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8') 68 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, 'UTF-8')
86 end
87 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1)
88 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2)
89 if txt_1.respond_to?(:force_encoding) 69 if txt_1.respond_to?(:force_encoding)
90 assert_equal "ASCII-8BIT", txt_1.encoding.to_s 70 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
91 assert_equal "ASCII-8BIT", txt_2.encoding.to_s 71 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
92 end 72 end
93 assert_equal "Texte encod? en ISO-8859-1", txt_1 73 assert_equal "Texte encod? en ISO-8859-1", txt_1
94 assert_equal "?a?b?c?d?e test", txt_2 74 assert_equal "?a?b?c?d?e test", txt_2
95 end 75 end
96 76
97 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja 77 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja
98 set_language_if_valid 'ja'
99 assert_equal 'CP932', l(:general_pdf_encoding)
100 str1 = "Texte encod\xe9 en ISO-8859-1" 78 str1 = "Texte encod\xe9 en ISO-8859-1"
101 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test" 79 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
102 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding) 80 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
103 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding) 81 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
104 if RUBY_VERSION < '1.9' 82 encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
105 if RUBY_PLATFORM == 'java' 83 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, encoding)
106 ic = Iconv.new("SJIS", 'UTF-8') 84 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, encoding)
107 else
108 ic = Iconv.new(l(:general_pdf_encoding), 'UTF-8')
109 end
110 end
111 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str1)
112 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_pdf_iconv(ic, str2)
113 if txt_1.respond_to?(:force_encoding) 85 if txt_1.respond_to?(:force_encoding)
114 assert_equal "ASCII-8BIT", txt_1.encoding.to_s 86 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
115 assert_equal "ASCII-8BIT", txt_2.encoding.to_s 87 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
116 end 88 end
117 assert_equal "Texte encod? en ISO-8859-1", txt_1 89 assert_equal "Texte encod? en ISO-8859-1", txt_1
118 assert_equal "?a?b?c?d?e test", txt_2 90 assert_equal "?a?b?c?d?e test", txt_2
119 end 91 end
92
93 def test_attach
94 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
95
96 str2 = "\x83e\x83X\x83g"
97 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
98 encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
99
100 a1 = Attachment.find(17)
101 a2 = Attachment.find(19)
102
103 User.current = User.find(1)
104 assert a1.readable?
105 assert a1.visible?
106 assert a2.readable?
107 assert a2.visible?
108
109 aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
110 assert_equal 17, aa1.id
111 aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
112 assert_equal 19, aa2.id
113
114 User.current = nil
115 assert a1.readable?
116 assert (! a1.visible?)
117 assert a2.readable?
118 assert (! a2.visible?)
119
120 aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
121 assert_equal nil, aa1
122 aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
123 assert_equal nil, aa2
124
125 set_tmp_attachments_directory
126 end
120 end 127 end