To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 0e / 0e9c8f11b3db41702079f085c94dfa01133abcd9.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (5.48 KB)

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