]> git.openstreetmap.org Git - rails.git/blob - test/lib/rich_text_test.rb
Replace augment.js with conditional polyfills for ES5 and ES6
[rails.git] / test / lib / rich_text_test.rb
1 require "test_helper"
2
3 class RichTextTest < ActiveSupport::TestCase
4   include Rails::Dom::Testing::Assertions::SelectorAssertions
5
6   def test_html_to_html
7     r = RichText.new("html", "foo http://example.com/ bar")
8     assert_html r do
9       assert_select "a", 1
10       assert_select "a[href='http://example.com/']", 1
11       assert_select "a[rel='nofollow noopener noreferer']", 1
12     end
13
14     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
15     assert_html r do
16       assert_select "a", 1
17       assert_select "a[href='http://example.com/']", 1
18       assert_select "a[rel='nofollow noopener noreferer']", 1
19     end
20
21     r = RichText.new("html", "foo example@example.com bar")
22     assert_html r do
23       assert_select "a", 0
24     end
25
26     r = RichText.new("html", "foo <a href='mailto:example@example.com'>bar</a> baz")
27     assert_html r do
28       assert_select "a", 1
29       assert_select "a[href='mailto:example@example.com']", 1
30       assert_select "a[rel='nofollow noopener noreferer']", 1
31     end
32
33     r = RichText.new("html", "foo <div>bar</div> baz")
34     assert_html r do
35       assert_select "div", false
36       assert_select "p", /^foo *bar *baz$/
37     end
38
39     r = RichText.new("html", "foo <script>bar = 1;</script> baz")
40     assert_html r do
41       assert_select "script", false
42       assert_select "p", /^foo *baz$/
43     end
44
45     r = RichText.new("html", "foo <style>div { display: none; }</style> baz")
46     assert_html r do
47       assert_select "style", false
48       assert_select "p", /^foo *baz$/
49     end
50   end
51
52   def test_html_to_text
53     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
54     assert_equal "foo <a href='http://example.com/'>bar</a> baz", r.to_text
55   end
56
57   def test_html_spam_score
58     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
59     assert_equal 55, r.spam_score.round
60   end
61
62   def test_markdown_to_html
63     r = RichText.new("markdown", "foo http://example.com/ bar")
64     assert_html r do
65       assert_select "a", 1
66       assert_select "a[href='http://example.com/']", 1
67       assert_select "a[rel='nofollow noopener noreferer']", 1
68     end
69
70     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
71     assert_html r do
72       assert_select "a", 1
73       assert_select "a[href='http://example.com/']", 1
74       assert_select "a[rel='nofollow noopener noreferer']", 1
75     end
76
77     r = RichText.new("markdown", "foo example@example.com bar")
78     assert_html r do
79       assert_select "a", 1
80       assert_select "a[href='mailto:example@example.com']", 1
81       assert_select "a[rel='nofollow noopener noreferer']", 1
82     end
83
84     r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
85     assert_html r do
86       assert_select "a", 1
87       assert_select "a[href='mailto:example@example.com']", 1
88       assert_select "a[rel='nofollow noopener noreferer']", 1
89     end
90
91     r = RichText.new("markdown", "foo ![bar](http://example.com/example.png) bar")
92     assert_html r do
93       assert_select "img", 1
94       assert_select "img[alt='bar']", 1
95       assert_select "img[src='http://example.com/example.png']", 1
96     end
97
98     r = RichText.new("markdown", "# foo bar baz")
99     assert_html r do
100       assert_select "h1", "foo bar baz"
101     end
102
103     r = RichText.new("markdown", "## foo bar baz")
104     assert_html r do
105       assert_select "h2", "foo bar baz"
106     end
107
108     r = RichText.new("markdown", "### foo bar baz")
109     assert_html r do
110       assert_select "h3", "foo bar baz"
111     end
112
113     r = RichText.new("markdown", "* foo bar baz")
114     assert_html r do
115       assert_select "ul" do
116         assert_select "li", "foo bar baz"
117       end
118     end
119
120     r = RichText.new("markdown", "1. foo bar baz")
121     assert_html r do
122       assert_select "ol" do
123         assert_select "li", "foo bar baz"
124       end
125     end
126
127     r = RichText.new("markdown", "foo *bar* _baz_ qux")
128     assert_html r do
129       assert_select "em", "bar"
130       assert_select "em", "baz"
131     end
132
133     r = RichText.new("markdown", "foo **bar** __baz__ qux")
134     assert_html r do
135       assert_select "strong", "bar"
136       assert_select "strong", "baz"
137     end
138
139     r = RichText.new("markdown", "foo `bar` baz")
140     assert_html r do
141       assert_select "code", "bar"
142     end
143
144     r = RichText.new("markdown", "    foo bar baz")
145     assert_html r do
146       assert_select "pre", /^\s*foo bar baz\s*$/
147     end
148   end
149
150   def test_markdown_to_text
151     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
152     assert_equal "foo [bar](http://example.com/) baz", r.to_text
153   end
154
155   def test_markdown_spam_score
156     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
157     assert_equal 50, r.spam_score.round
158   end
159
160   def test_text_to_html
161     r = RichText.new("text", "foo http://example.com/ bar")
162     assert_html r do
163       assert_select "a", 1
164       assert_select "a[href='http://example.com/']", 1
165       assert_select "a[rel='nofollow noopener noreferer']", 1
166     end
167
168     r = RichText.new("text", "foo example@example.com bar")
169     assert_html r do
170       assert_select "a", 0
171     end
172
173     r = RichText.new("text", "foo < bar & baz > qux")
174     assert_html r do
175       assert_select "p", "foo < bar & baz > qux"
176     end
177   end
178
179   def test_text_to_text
180     r = RichText.new("text", "foo http://example.com/ bar")
181     assert_equal "foo http://example.com/ bar", r.to_text
182   end
183
184   def test_text_spam_score
185     r = RichText.new("text", "foo http://example.com/ bar")
186     assert_equal 141, r.spam_score.round
187   end
188
189   private
190
191   def assert_html(richtext, &block)
192     html = richtext.to_html
193     assert html.html_safe?
194     root = Nokogiri::HTML::DocumentFragment.parse(html)
195     assert_select root, "*" do
196       yield block
197     end
198   end
199 end