annotate test/unit/lib/redmine/export/pdf_test.rb @ 1465:ab8bd24eeb65 bug_635

Close obsolete branch bug_635
author Chris Cannam
date Fri, 19 Jul 2013 12:13:20 +0100
parents 433d4f72a19b
children 622f24f53b42
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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@909 22 fixtures :users, :projects, :roles, :members, :member_roles,
Chris@909 23 :enabled_modules, :issues, :trackers, :attachments
Chris@441 24
Chris@441 25 def test_fix_text_encoding_nil
Chris@909 26 assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "UTF-8")
Chris@909 27 assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "ISO-8859-1")
Chris@441 28 end
Chris@441 29
Chris@441 30 def test_rdm_pdf_iconv_cannot_convert_ja_cp932
Chris@909 31 encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
Chris@441 32 utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b"
Chris@441 33 utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
Chris@441 34 utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
Chris@441 35 if utf8_txt_1.respond_to?(:force_encoding)
Chris@909 36 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
Chris@909 37 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
Chris@909 38 txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding)
Chris@441 39 assert_equal "?\x91\xd4", txt_1
Chris@441 40 assert_equal "?\x91\xd4?", txt_2
Chris@441 41 assert_equal "??\x91\xd4?", txt_3
Chris@441 42 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
Chris@441 43 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
Chris@441 44 assert_equal "ASCII-8BIT", txt_3.encoding.to_s
Chris@507 45 elsif RUBY_PLATFORM == 'java'
Chris@507 46 assert_equal "??",
Chris@909 47 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
Chris@507 48 assert_equal "???",
Chris@909 49 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
Chris@507 50 assert_equal "????",
Chris@909 51 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding)
Chris@441 52 else
Chris@441 53 assert_equal "???\x91\xd4",
Chris@909 54 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
Chris@441 55 assert_equal "???\x91\xd4???",
Chris@909 56 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
Chris@441 57 assert_equal "??????\x91\xd4???",
Chris@909 58 Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding)
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 str1 = "Texte encod\xe9 en ISO-8859-1"
Chris@441 64 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
Chris@441 65 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
Chris@441 66 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@909 67 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, 'UTF-8')
Chris@909 68 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, 'UTF-8')
Chris@441 69 if txt_1.respond_to?(:force_encoding)
Chris@441 70 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
Chris@441 71 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
Chris@441 72 end
Chris@441 73 assert_equal "Texte encod? en ISO-8859-1", txt_1
Chris@441 74 assert_equal "?a?b?c?d?e test", txt_2
Chris@441 75 end
Chris@441 76
Chris@441 77 def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja
Chris@441 78 str1 = "Texte encod\xe9 en ISO-8859-1"
Chris@441 79 str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
Chris@441 80 str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
Chris@441 81 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@909 82 encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
Chris@909 83 txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, encoding)
Chris@909 84 txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, encoding)
Chris@441 85 if txt_1.respond_to?(:force_encoding)
Chris@441 86 assert_equal "ASCII-8BIT", txt_1.encoding.to_s
Chris@441 87 assert_equal "ASCII-8BIT", txt_2.encoding.to_s
Chris@441 88 end
Chris@441 89 assert_equal "Texte encod? en ISO-8859-1", txt_1
Chris@441 90 assert_equal "?a?b?c?d?e test", txt_2
Chris@441 91 end
Chris@909 92
Chris@909 93 def test_attach
Chris@1115 94 set_fixtures_attachments_directory
Chris@909 95
Chris@909 96 str2 = "\x83e\x83X\x83g"
Chris@909 97 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
Chris@909 98 encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
Chris@909 99
Chris@909 100 a1 = Attachment.find(17)
Chris@909 101 a2 = Attachment.find(19)
Chris@909 102
Chris@909 103 User.current = User.find(1)
Chris@909 104 assert a1.readable?
Chris@909 105 assert a1.visible?
Chris@909 106 assert a2.readable?
Chris@909 107 assert a2.visible?
Chris@909 108
Chris@909 109 aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
Chris@1115 110 assert_not_nil aa1
Chris@909 111 assert_equal 17, aa1.id
Chris@909 112 aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
Chris@1115 113 assert_not_nil aa2
Chris@909 114 assert_equal 19, aa2.id
Chris@909 115
Chris@909 116 User.current = nil
Chris@909 117 assert a1.readable?
Chris@909 118 assert (! a1.visible?)
Chris@909 119 assert a2.readable?
Chris@909 120 assert (! a2.visible?)
Chris@909 121
Chris@909 122 aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
Chris@909 123 assert_equal nil, aa1
Chris@909 124 aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
Chris@909 125 assert_equal nil, aa2
Chris@909 126
Chris@909 127 set_tmp_attachments_directory
Chris@909 128 end
Chris@441 129 end