Fixed manipulation of string in html2text. Added unit tests.

This commit is contained in:
Martin Edenhofer 2015-01-09 14:17:34 +01:00
parent 5367c34540
commit 39e724ca07
4 changed files with 147 additions and 91 deletions

View file

@ -215,6 +215,7 @@ class Observer::Ticket::Notification::BackgroundJob
#{article.body.text2html} #{article.body.text2html}
</blockquote> </blockquote>
&lt;/snip&gt; &lt;/snip&gt;
<br>
<br>' <br>'
end end
@ -280,6 +281,7 @@ State: i18n(#{ticket.state.name.text2html})<br>
#{article.body.text2html} #{article.body.text2html}
</blockquote> </blockquote>
&lt;/snip&gt; &lt;/snip&gt;
<br>
<br>' <br>'
end end
if user.preferences[:locale] =~ /^de/i if user.preferences[:locale] =~ /^de/i

View file

@ -19,8 +19,18 @@ class String
line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
end * "\n" end * "\n"
end end
=begin
filename = 'Some::Module'.to_filename
returns
'some/module'
=end
def to_filename def to_filename
camel_cased_word = self.to_s camel_cased_word = "#{self}"
camel_cased_word.gsub(/::/, '/').downcase camel_cased_word.gsub(/::/, '/').downcase
end end
@ -43,10 +53,14 @@ class String
text = html_string.html2text text = html_string.html2text
returns
'string with text'
=end =end
def html2text def html2text
string = self string = "#{self}"
# in case of invalid encodeing, strip invalid chars # in case of invalid encodeing, strip invalid chars
# see also test/fixtures/mail21.box # see also test/fixtures/mail21.box

View file

@ -0,0 +1,129 @@
# encoding: utf-8
require 'test_helper'
class AaaStringTest < ActiveSupport::TestCase
test 'to_filename ref' do
modul = 'test'
result = 'test'
modul.to_filename
assert_equal( result, modul )
modul = 'Some::File'
result = 'Some::File'
modul.to_filename
assert_equal( result, modul )
end
test 'to_filename function' do
modul = 'test'
result = 'test'
assert_equal( result, modul.to_filename )
modul = 'Some::File'
result = 'some/file'
assert_equal( result, modul.to_filename )
end
test 'html2text ref' do
html = 'test'
result = 'test'
html.html2text
assert_equal( result, html )
html = '<div>test</div>'
result = '<div>test</div>'
html.html2text
assert_equal( result, html )
end
test 'html2text function' do
html = 'test'
result = 'test'
assert_equal( result, html.html2text )
html = ' test '
result = 'test'
assert_equal( result, html.html2text )
html = "\n\n test \n\n\n"
result = 'test'
assert_equal( result, html.html2text )
html = '<div>test</div>'
result = 'test'
assert_equal( result, html.html2text )
html = '<div>test<br></div>'
result = 'test'
assert_equal( result, html.html2text )
html = "<div>test<br><br><br>\n<br>\n<br>\n</div>"
result = 'test'
assert_equal( result, html.html2text )
html = "<pre>test\n\ntest</pre>"
result = "test\ntest"
assert_equal( result, html.html2text )
html = "<code>test\n\ntest</code>"
result = "test\ntest"
assert_equal( result, html.html2text )
html = "<table><tr><td>test</td><td>col</td></td></tr><tr><td>test</td><td>4711</td></tr></table>"
result = "test col \ntest 4711"
assert_equal( result, html.html2text )
html = "<!-- some comment -->
<div>
test<br><br><br>\n<br>\n<br>\n
</div>"
result = 'test'
assert_equal( result, html.html2text )
html = "\n<div><a href=\"http://zammad.org\">Best Tool of the World</a>
some other text</div>
<div>"
result = "[1] Best Tool of the Worldsome other text\n\n\n[1] http://zammad.org"
assert_equal( result, html.html2text )
html = "<!-- some comment -->
<div>
test<br><br><br>\n<hr/>\n<br>\n
</div>"
result = "test\n\n___"
assert_equal( result, html.html2text )
html = ' line&nbsp;1<br>
you<br/>
-----&amp;'
should = 'line 1
you
-----&'
assert_equal( should, html.html2text )
html = ' <ul><li>#1</li><li>#2</li></ul>'
should = '* #1
* #2'
assert_equal( should, html.html2text )
html = '<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
<body style="font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;">
<div>&gt; Welcome!</div><div>&gt;</div><div>&gt; Thank you for installing Zammad.</div><div>&gt;</div>
</body>
</html>'
should = '> Welcome!
>
> Thank you for installing Zammad.
>'
assert_equal( should, html.html2text )
end
end

View file

@ -163,93 +163,4 @@ class EmailBuildTest < ActiveSupport::TestCase
end end
test 'html2text' do
html = 'test'
result = 'test'
assert_equal( result, html.html2text )
html = ' test '
result = 'test'
assert_equal( result, html.html2text )
html = "\n\n test \n\n\n"
result = 'test'
assert_equal( result, html.html2text )
html = '<div>test</div>'
result = 'test'
assert_equal( result, html.html2text )
html = '<div>test<br></div>'
result = 'test'
assert_equal( result, html.html2text )
html = "<div>test<br><br><br>\n<br>\n<br>\n</div>"
result = 'test'
assert_equal( result, html.html2text )
html = "<pre>test\n\ntest</pre>"
result = "test\ntest"
assert_equal( result, html.html2text )
html = "<code>test\n\ntest</code>"
result = "test\ntest"
assert_equal( result, html.html2text )
html = "<table><tr><td>test</td><td>col</td></td></tr><tr><td>test</td><td>4711</td></tr></table>"
result = "test col \ntest 4711"
assert_equal( result, html.html2text )
html = "<!-- some comment -->
<div>
test<br><br><br>\n<br>\n<br>\n
</div>"
result = 'test'
assert_equal( result, html.html2text )
html = "\n<div><a href=\"http://zammad.org\">Best Tool of the World</a>
some other text</div>
<div>"
result = "[1] Best Tool of the Worldsome other text\n\n\n[1] http://zammad.org"
assert_equal( result, html.html2text )
html = "<!-- some comment -->
<div>
test<br><br><br>\n<hr/>\n<br>\n
</div>"
result = "test\n\n___"
assert_equal( result, html.html2text )
html = ' line&nbsp;1<br>
you<br/>
-----&amp;'
should = 'line 1
you
-----&'
assert_equal( should, html.html2text )
html = ' <ul><li>#1</li><li>#2</li></ul>'
should = '* #1
* #2'
assert_equal( should, html.html2text )
html = '<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
<body style="font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;">
<div>&gt; Welcome!</div><div>&gt;</div><div>&gt; Thank you for installing Zammad.</div><div>&gt;</div>
</body>
</html>'
should = '> Welcome!
>
> Thank you for installing Zammad.
>'
assert_equal( should, html.html2text )
end
end end