X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/b1cb7bb8191ec2c25edb12d71670cfb883449c98..b9daf066842c9e15489d0ccd387d694021975222:/test/unit/rich_text_test.rb?ds=sidebyside diff --git a/test/unit/rich_text_test.rb b/test/unit/rich_text_test.rb index a628eed20..c43dd0e9a 100644 --- a/test/unit/rich_text_test.rb +++ b/test/unit/rich_text_test.rb @@ -1,4 +1,169 @@ require File.dirname(__FILE__) + '/../test_helper' class RichTextTest < ActiveSupport::TestCase + include ActionDispatch::Assertions::SelectorAssertions + + def test_html_to_html + r = RichText.new("html", "foo http://example.com/ bar") + assert_html r do + assert_select "a", 1 + assert_select "a[href='http://example.com/']", 1 + assert_select "a[rel='nofollow']", 1 + end + + r = RichText.new("html", "foo bar baz") + assert_html r do + assert_select "a", 1 + assert_select "a[href='http://example.com/']", 1 + assert_select "a[rel='nofollow']", 1 + end + + r = RichText.new("html", "foo example@example.com bar") + assert_html r do + assert_select "a", 0 + end + + r = RichText.new("html", "foo bar baz") + assert_html r do + assert_select "a", 1 + assert_select "a[href='mailto:example@example.com']", 1 + assert_select "a[rel='nofollow']", 1 + end + + r = RichText.new("html", "foo
bar
baz") + assert_html r do + assert_select "div", false + assert_select "p", /^foo *bar *baz$/ + end + + r = RichText.new("html", "foo baz") + assert_html r do + assert_select "script", false + assert_select "p", /^foo *baz$/ + end + + r = RichText.new("html", "foo baz") + assert_html r do + assert_select "style", false + assert_select "p", /^foo *baz$/ + end + end + + def test_markdown_to_html + r = RichText.new("markdown", "foo http://example.com/ bar") + assert_html r do + assert_select "a", 1 + assert_select "a[href='http://example.com/']", 1 + assert_select "a[rel='nofollow']", 1 + end + + r = RichText.new("markdown", "foo [bar](http://example.com/) baz") + assert_html r do + assert_select "a", 1 + assert_select "a[href='http://example.com/']", 1 + assert_select "a[rel='nofollow']", 1 + end + + r = RichText.new("markdown", "foo example@example.com bar") + assert_html r do + assert_select "a", 1 + assert_select "a[href='mailto:example@example.com']", 1 + assert_select "a[rel='nofollow']", 1 + end + + r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar") + assert_html r do + assert_select "a", 1 + assert_select "a[href='mailto:example@example.com']", 1 + assert_select "a[rel='nofollow']", 1 + end + + r = RichText.new("markdown", "foo ![bar](http://example.com/example.png) bar") + assert_html r do + assert_select "img", 1 + assert_select "img[alt='bar']", 1 + assert_select "img[src='http://example.com/example.png']", 1 + end + + r = RichText.new("markdown", "# foo bar baz") + assert_html r do + assert_select "h1", "foo bar baz" + end + + r = RichText.new("markdown", "## foo bar baz") + assert_html r do + assert_select "h2", "foo bar baz" + end + + r = RichText.new("markdown", "### foo bar baz") + assert_html r do + assert_select "h3", "foo bar baz" + end + + r = RichText.new("markdown", "* foo bar baz") + assert_html r do + assert_select "ul" do + assert_select "li", "foo bar baz" + end + end + + r = RichText.new("markdown", "1. foo bar baz") + assert_html r do + assert_select "ol" do + assert_select "li", "foo bar baz" + end + end + + r = RichText.new("markdown", "foo *bar* _baz_ qux") + assert_html r do + assert_select "em", "bar" + assert_select "em", "baz" + end + + r = RichText.new("markdown", "foo **bar** __baz__ qux") + assert_html r do + assert_select "strong", "bar" + assert_select "strong", "baz" + end + + r = RichText.new("markdown", "foo `bar` baz") + assert_html r do + assert_select "code", "bar" + end + + r = RichText.new("markdown", " foo bar baz") + assert_html r do + assert_select "pre", /^\s*foo bar baz\s*$/ + end + end + + def test_text_to_html + r = RichText.new("text", "foo http://example.com/ bar") + assert_html r do + assert_select "a", 1 + assert_select "a[href='http://example.com/']", 1 + assert_select "a[rel='nofollow']", 1 + end + + r = RichText.new("text", "foo example@example.com bar") + assert_html r do + assert_select "a", 0 + end + + r = RichText.new("text", "foo < bar & baz > qux") + assert_html r do + assert_select "p", "foo < bar & baz > qux" + end + end + +private + + def assert_html(richtext, &block) + html = richtext.to_html + assert html.html_safe? + root = HTML::Document.new(richtext.to_html, false, true).root + assert_select root, "*" do + yield block + end + end end