You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pdf_test.rb 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. require File.expand_path('../../../../../test_helper', __FILE__)
  18. class PdfTest < ActiveSupport::TestCase
  19. fixtures :users, :projects, :roles, :members, :member_roles,
  20. :enabled_modules, :issues, :trackers, :attachments
  21. def test_fix_text_encoding_nil
  22. assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "UTF-8")
  23. assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "ISO-8859-1")
  24. end
  25. def test_rdm_pdf_iconv_cannot_convert_ja_cp932
  26. utf8_txt_1 = "\xe7\x8b\x80\xe6\x85\x8b"
  27. utf8_txt_2 = "\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
  28. utf8_txt_3 = "\xe7\x8b\x80\xe7\x8b\x80\xe6\x85\x8b\xe7\x8b\x80"
  29. ["CP932", "SJIS"].each do |encoding|
  30. txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_1, encoding)
  31. txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_2, encoding)
  32. txt_3 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(utf8_txt_3, encoding)
  33. assert_equal "?\x91\xd4".force_encoding("ASCII-8BIT"), txt_1
  34. assert_equal "?\x91\xd4?".force_encoding("ASCII-8BIT"), txt_2
  35. assert_equal "??\x91\xd4?".force_encoding("ASCII-8BIT"), txt_3
  36. assert_equal "ASCII-8BIT", txt_1.encoding.to_s
  37. assert_equal "ASCII-8BIT", txt_2.encoding.to_s
  38. assert_equal "ASCII-8BIT", txt_3.encoding.to_s
  39. end
  40. end
  41. def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_en
  42. str1 = "Texte encod\xe9 en ISO-8859-1".force_encoding("UTF-8")
  43. str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test".force_encoding("ASCII-8BIT")
  44. txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, 'UTF-8')
  45. txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, 'UTF-8')
  46. assert_equal "ASCII-8BIT", txt_1.encoding.to_s
  47. assert_equal "ASCII-8BIT", txt_2.encoding.to_s
  48. assert_equal "Texte encod? en ISO-8859-1", txt_1
  49. assert_equal "?a?b?c?d?e test", txt_2
  50. end
  51. def test_rdm_pdf_iconv_invalid_utf8_should_be_replaced_ja
  52. str1 = "Texte encod\xe9 en ISO-8859-1".force_encoding("UTF-8")
  53. str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test".force_encoding("ASCII-8BIT")
  54. encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
  55. txt_1 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str1, encoding)
  56. txt_2 = Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(str2, encoding)
  57. assert_equal "ASCII-8BIT", txt_1.encoding.to_s
  58. assert_equal "ASCII-8BIT", txt_2.encoding.to_s
  59. assert_equal "Texte encod? en ISO-8859-1", txt_1
  60. assert_equal "?a?b?c?d?e test", txt_2
  61. end
  62. def test_attach
  63. ["CP932", "SJIS"].each do |encoding|
  64. set_fixtures_attachments_directory
  65. str2 = "\x83e\x83X\x83g".force_encoding("ASCII-8BIT")
  66. a1 = Attachment.find(17)
  67. a2 = Attachment.find(19)
  68. User.current = User.find(1)
  69. assert a1.readable?
  70. assert a1.visible?
  71. assert a2.readable?
  72. assert a2.visible?
  73. aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
  74. assert_not_nil aa1
  75. assert_equal 17, aa1.id
  76. aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
  77. assert_not_nil aa2
  78. assert_equal 19, aa2.id
  79. User.current = nil
  80. assert a1.readable?
  81. assert (! a1.visible?)
  82. assert a2.readable?
  83. assert (! a2.visible?)
  84. aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
  85. assert_nil aa1
  86. aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
  87. assert_nil aa2
  88. set_tmp_attachments_directory
  89. end
  90. end
  91. end