annotate test/unit/lib/redmine/export/pdf_test.rb @ 1628:9c5f8e24dadc live tip

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