]> git.openstreetmap.org Git - rails.git/blob - test/lib/rich_text_test.rb
Shorten matching wiki urls in linkify
[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 noreferrer']", 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 noreferrer']", 1
19     end
20
21     r = RichText.new("html", "foo <a rel='junk me trash' href='http://example.com/'>bar</a> baz")
22     assert_html r do
23       assert_select "a", 1
24       assert_select "a[href='http://example.com/']", 1
25       assert_select "a[rel='me nofollow noopener noreferrer']", 1
26     end
27
28     r = RichText.new("html", "foo example@example.com bar")
29     assert_html r do
30       assert_select "a", 0
31     end
32
33     r = RichText.new("html", "foo <a href='mailto:example@example.com'>bar</a> baz")
34     assert_html r do
35       assert_select "a", 1
36       assert_select "a[href='mailto:example@example.com']", 1
37       assert_select "a[rel='nofollow noopener noreferrer']", 1
38     end
39
40     r = RichText.new("html", "foo <div>bar</div> baz")
41     assert_html r do
42       assert_select "div", false
43       assert_select "p", /^foo *bar *baz$/
44     end
45
46     r = RichText.new("html", "foo <script>bar = 1;</script> baz")
47     assert_html r do
48       assert_select "script", false
49       assert_select "p", /^foo *baz$/
50     end
51
52     r = RichText.new("html", "foo <style>div { display: none; }</style> baz")
53     assert_html r do
54       assert_select "style", false
55       assert_select "p", /^foo *baz$/
56     end
57
58     r = RichText.new("html", "<table><tr><td>column</td></tr></table>")
59     assert_html r do
60       assert_select "table[class='table table-sm w-auto']"
61     end
62
63     r = RichText.new("html", "<p class='btn btn-warning'>Click Me</p>")
64     assert_html r do
65       assert_select "p[class='btn btn-warning']", false
66       assert_select "p", /^Click Me$/
67     end
68
69     r = RichText.new("html", "<p style='color:red'>Danger</p>")
70     assert_html r do
71       assert_select "p[style='color:red']", false
72       assert_select "p", /^Danger$/
73     end
74   end
75
76   def test_html_to_text
77     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
78     assert_equal "foo <a href='http://example.com/'>bar</a> baz", r.to_text
79   end
80
81   def test_html_spam_score
82     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
83     assert_equal 55, r.spam_score.round
84   end
85
86   def test_markdown_to_html
87     r = RichText.new("markdown", "foo http://example.com/ bar")
88     assert_html r do
89       assert_select "a", 1
90       assert_select "a[href='http://example.com/']", 1
91       assert_select "a[rel='nofollow noopener noreferrer']", 1
92     end
93
94     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
95     assert_html r do
96       assert_select "a", 1
97       assert_select "a[href='http://example.com/']", 1
98       assert_select "a[rel='nofollow noopener noreferrer']", 1
99     end
100
101     r = RichText.new("markdown", "foo <a rel='junk me trash' href='http://example.com/'>bar</a>) baz")
102     assert_html r do
103       assert_select "a", 1
104       assert_select "a[href='http://example.com/']", 1
105       assert_select "a[rel='me nofollow noopener noreferrer']", 1
106     end
107
108     r = RichText.new("markdown", "foo example@example.com bar")
109     assert_html r do
110       assert_select "a", 1
111       assert_select "a[href='mailto:example@example.com']", 1
112       assert_select "a[rel='nofollow noopener noreferrer']", 1
113     end
114
115     r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
116     assert_html r do
117       assert_select "a", 1
118       assert_select "a[href='mailto:example@example.com']", 1
119       assert_select "a[rel='nofollow noopener noreferrer']", 1
120     end
121
122     r = RichText.new("markdown", "foo ![bar](http://example.com/example.png) bar")
123     assert_html r do
124       assert_select "img", 1
125       assert_select "img[alt='bar']", 1
126       assert_select "img[src='http://example.com/example.png']", 1
127     end
128
129     r = RichText.new("markdown", "# foo bar baz")
130     assert_html r do
131       assert_select "h1", "foo bar baz"
132     end
133
134     r = RichText.new("markdown", "## foo bar baz")
135     assert_html r do
136       assert_select "h2", "foo bar baz"
137     end
138
139     r = RichText.new("markdown", "### foo bar baz")
140     assert_html r do
141       assert_select "h3", "foo bar baz"
142     end
143
144     r = RichText.new("markdown", "* foo bar baz")
145     assert_html r do
146       assert_select "ul" do
147         assert_select "li", "foo bar baz"
148       end
149     end
150
151     r = RichText.new("markdown", "1. foo bar baz")
152     assert_html r do
153       assert_select "ol" do
154         assert_select "li", "foo bar baz"
155       end
156     end
157
158     r = RichText.new("markdown", "foo *bar* _baz_ qux")
159     assert_html r do
160       assert_select "em", "bar"
161       assert_select "em", "baz"
162     end
163
164     r = RichText.new("markdown", "foo **bar** __baz__ qux")
165     assert_html r do
166       assert_select "strong", "bar"
167       assert_select "strong", "baz"
168     end
169
170     r = RichText.new("markdown", "foo `bar` baz")
171     assert_html r do
172       assert_select "code", "bar"
173     end
174
175     r = RichText.new("markdown", "    foo bar baz")
176     assert_html r do
177       assert_select "pre", /^\s*foo bar baz\s*$/
178     end
179
180     r = RichText.new("markdown", "|column|column")
181     assert_html r do
182       assert_select "table[class='table table-sm w-auto']"
183     end
184
185     r = RichText.new("markdown", "Click Me\n{:.btn.btn-warning}")
186     assert_html r do
187       assert_select "p[class='btn btn-warning']", false
188       assert_select "p", /^Click Me$/
189     end
190
191     r = RichText.new("markdown", "<p style='color:red'>Danger</p>")
192     assert_html r do
193       assert_select "p[style='color:red']", false
194       assert_select "p", /^Danger$/
195     end
196   end
197
198   def test_markdown_table_alignment
199     # Ensure that kramdown table alignment styles are converted to bootstrap classes
200     markdown_table = <<~MARKDOWN
201       | foo  | bar |
202       |:----:|----:|
203       |center|right|
204     MARKDOWN
205     r = RichText.new("markdown", markdown_table)
206     assert_html r do
207       assert_select "td[style='text-align:center']", false
208       assert_select "td[class='text-center']", true
209       assert_select "td[style='text-align:right']", false
210       assert_select "td[class='text-end']", true
211     end
212   end
213
214   def test_markdown_to_text
215     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
216     assert_equal "foo [bar](http://example.com/) baz", r.to_text
217   end
218
219   def test_markdown_spam_score
220     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
221     assert_equal 50, r.spam_score.round
222   end
223
224   def test_text_to_html_linkify
225     with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do
226       r = RichText.new("text", "foo http://example.com/ bar")
227       assert_html r do
228         assert_dom "a", :count => 1, :text => "http://example.com/" do
229           assert_dom "> @href", "http://example.com/"
230           assert_dom "> @rel", "nofollow noopener noreferrer"
231         end
232       end
233     end
234   end
235
236   def test_text_to_html_linkify_replace
237     with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do
238       r = RichText.new("text", "foo https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12 bar")
239       assert_html r do
240         assert_dom "a", :count => 1, :text => "repl.example.com/some/path?query=te<st&limit=20>10#result12" do
241           assert_dom "> @href", "https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12"
242           assert_dom "> @rel", "nofollow noopener noreferrer"
243         end
244       end
245     end
246   end
247
248   def test_text_to_html_linkify_replace_other_scheme
249     with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => "repl.example.com") do
250       r = RichText.new("text", "foo ftp://replace-me.example.com/some/path?query=te<st&limit=20>10#result12 bar")
251       assert_html r do
252         assert_dom "a", :count => 1, :text => "ftp://replace-me.example.com/some/path?query=te<st&limit=20>10#result12" do
253           assert_dom "> @href", "ftp://replace-me.example.com/some/path?query=te<st&limit=20>10#result12"
254           assert_dom "> @rel", "nofollow noopener noreferrer"
255         end
256       end
257     end
258   end
259
260   def test_text_to_html_linkify_replace_undefined
261     with_settings(:linkify_hosts => ["replace-me.example.com"], :linkify_hosts_replacement => nil) do
262       r = RichText.new("text", "foo https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12 bar")
263       assert_html r do
264         assert_dom "a", :count => 1, :text => "https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12" do
265           assert_dom "> @href", "https://replace-me.example.com/some/path?query=te<st&limit=20>10#result12"
266           assert_dom "> @rel", "nofollow noopener noreferrer"
267         end
268       end
269     end
270   end
271
272   def test_text_to_html_linkify_wiki_replace
273     with_settings(:linkify_wiki_hosts => ["replace-me-wiki.example.com"], :linkify_wiki_hosts_replacement => "wiki.example.com") do
274       r = RichText.new("text", "foo https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal bar")
275       assert_html r do
276         assert_dom "a", :count => 1, :text => "wiki.example.com/wiki/Tag:surface%3Dmetal" do
277           assert_dom "> @href", "https://replace-me-wiki.example.com/wiki/Tag:surface%3Dmetal"
278           assert_dom "> @rel", "nofollow noopener noreferrer"
279         end
280       end
281     end
282   end
283
284   def test_text_to_html_email
285     r = RichText.new("text", "foo example@example.com bar")
286     assert_html r do
287       assert_select "a", 0
288     end
289   end
290
291   def test_text_to_html_escape
292     r = RichText.new("text", "foo < bar & baz > qux")
293     assert_html r do
294       assert_select "p", "foo < bar & baz > qux"
295     end
296   end
297
298   def test_text_to_text
299     r = RichText.new("text", "foo http://example.com/ bar")
300     assert_equal "foo http://example.com/ bar", r.to_text
301   end
302
303   def test_text_spam_score
304     r = RichText.new("text", "foo http://example.com/ bar")
305     assert_equal 141, r.spam_score.round
306   end
307
308   def test_text_no_opengraph_properties
309     r = RichText.new("text", "foo https://example.com/ bar")
310     assert_nil r.image
311     assert_nil r.image_alt
312     assert_nil r.description
313   end
314
315   def test_html_no_opengraph_properties
316     r = RichText.new("html", "foo <a href='https://example.com/'>bar</a> baz")
317     assert_nil r.image
318     assert_nil r.image_alt
319     assert_nil r.description
320   end
321
322   def test_markdown_no_image
323     r = RichText.new("markdown", "foo [bar](https://example.com/) baz")
324     assert_nil r.image
325     assert_nil r.image_alt
326   end
327
328   def test_markdown_image
329     r = RichText.new("markdown", "foo ![bar](https://example.com/image.jpg) baz")
330     assert_equal "https://example.com/image.jpg", r.image
331     assert_equal "bar", r.image_alt
332   end
333
334   def test_markdown_first_image
335     r = RichText.new("markdown", "foo ![bar1](https://example.com/image1.jpg) baz\nfoo ![bar2](https://example.com/image2.jpg) baz")
336     assert_equal "https://example.com/image1.jpg", r.image
337     assert_equal "bar1", r.image_alt
338   end
339
340   def test_markdown_image_with_empty_src
341     r = RichText.new("markdown", "![invalid]()")
342     assert_nil r.image
343     assert_nil r.image_alt
344   end
345
346   def test_markdown_skip_image_with_empty_src
347     r = RichText.new("markdown", "![invalid]() ![valid](https://example.com/valid.gif)")
348     assert_equal "https://example.com/valid.gif", r.image
349     assert_equal "valid", r.image_alt
350   end
351
352   def test_markdown_html_image
353     r = RichText.new("markdown", "<img src='https://example.com/img_element.png' alt='alt text here'>")
354     assert_equal "https://example.com/img_element.png", r.image
355     assert_equal "alt text here", r.image_alt
356   end
357
358   def test_markdown_html_image_without_alt
359     r = RichText.new("markdown", "<img src='https://example.com/img_element.png'>")
360     assert_equal "https://example.com/img_element.png", r.image
361     assert_nil r.image_alt
362   end
363
364   def test_markdown_html_image_with_empty_src
365     r = RichText.new("markdown", "<img src='' alt='forgot src'>")
366     assert_nil r.image
367     assert_nil r.image_alt
368   end
369
370   def test_markdown_skip_html_image_with_empty_src
371     r = RichText.new("markdown", "<img src='' alt='forgot src'> <img src='https://example.com/next_img_element.png' alt='have src'>")
372     assert_equal "https://example.com/next_img_element.png", r.image
373     assert_equal "have src", r.image_alt
374   end
375
376   def test_markdown_html_image_without_src
377     r = RichText.new("markdown", "<img alt='totally forgot src'>")
378     assert_nil r.image
379     assert_nil r.image_alt
380   end
381
382   def test_markdown_skip_html_image_without_src
383     r = RichText.new("markdown", "<img alt='totally forgot src'> <img src='https://example.com/next_img_element.png' alt='have src'>")
384     assert_equal "https://example.com/next_img_element.png", r.image
385     assert_equal "have src", r.image_alt
386   end
387
388   def test_markdown_no_description
389     r = RichText.new("markdown", "#Nope")
390     assert_nil r.description
391   end
392
393   def test_markdown_description
394     r = RichText.new("markdown", "This is an article about something.")
395     assert_equal "This is an article about something.", r.description
396   end
397
398   def test_markdown_description_after_heading
399     r = RichText.new("markdown", "#Heading\n\nHere starts the text.")
400     assert_equal "Here starts the text.", r.description
401   end
402
403   def test_markdown_description_after_image
404     r = RichText.new("markdown", "![bar](https://example.com/image.jpg)\n\nThis is below the image.")
405     assert_equal "This is below the image.", r.description
406   end
407
408   def test_markdown_description_only_first_paragraph
409     r = RichText.new("markdown", "This thing.\n\nMaybe also that thing.")
410     assert_equal "This thing.", r.description
411   end
412
413   def test_markdown_description_elements
414     r = RichText.new("markdown", "*Something* **important** [here](https://example.com/).")
415     assert_equal "Something important here.", r.description
416   end
417
418   def test_markdown_html_description
419     r = RichText.new("markdown", "<p>Can use HTML tags.</p>")
420     assert_equal "Can use HTML tags.", r.description
421   end
422
423   def test_markdown_description_max_length
424     r = RichText.new("markdown", "x" * RichText::MAX_DESCRIPTION_LENGTH)
425     assert_equal "x" * RichText::MAX_DESCRIPTION_LENGTH, r.description
426
427     r = RichText.new("markdown", "y" * (RichText::MAX_DESCRIPTION_LENGTH + 1))
428     assert_equal "#{'y' * (RichText::MAX_DESCRIPTION_LENGTH - 3)}...", r.description
429
430     r = RichText.new("markdown", "*zzzzzzzzz*z" * ((RichText::MAX_DESCRIPTION_LENGTH + 1) / 10.0).ceil)
431     assert_equal "#{'z' * (RichText::MAX_DESCRIPTION_LENGTH - 3)}...", r.description
432   end
433
434   private
435
436   def assert_html(richtext, &block)
437     html = richtext.to_html
438     assert_predicate html, :html_safe?
439     root = Nokogiri::HTML::DocumentFragment.parse(html)
440     assert_select root, "*" do
441       yield block
442     end
443   end
444 end