]> git.openstreetmap.org Git - rails.git/blob - test/lib/rich_text_test.rb
Merge branch 'pull/3146'
[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 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 noreferrer']", 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
51     r = RichText.new("html", "<table><tr><td>column</td></tr></table>")
52     assert_html r do
53       assert_select "table[class='table table-sm w-auto']"
54     end
55
56     r = RichText.new("html", "<p class='btn btn-warning'>Click Me</p>")
57     assert_html r do
58       assert_select "p[class='btn btn-warning']", false
59       assert_select "p", /^Click Me$/
60     end
61
62     r = RichText.new("html", "<p style='color:red'>Danger</p>")
63     assert_html r do
64       assert_select "p[style='color:red']", false
65       assert_select "p", /^Danger$/
66     end
67   end
68
69   def test_html_to_text
70     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
71     assert_equal "foo <a href='http://example.com/'>bar</a> baz", r.to_text
72   end
73
74   def test_html_spam_score
75     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
76     assert_equal 55, r.spam_score.round
77   end
78
79   def test_markdown_to_html
80     r = RichText.new("markdown", "foo http://example.com/ bar")
81     assert_html r do
82       assert_select "a", 1
83       assert_select "a[href='http://example.com/']", 1
84       assert_select "a[rel='nofollow noopener noreferrer']", 1
85     end
86
87     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
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 example@example.com bar")
95     assert_html r do
96       assert_select "a", 1
97       assert_select "a[href='mailto:example@example.com']", 1
98       assert_select "a[rel='nofollow noopener noreferrer']", 1
99     end
100
101     r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
102     assert_html r do
103       assert_select "a", 1
104       assert_select "a[href='mailto:example@example.com']", 1
105       assert_select "a[rel='nofollow noopener noreferrer']", 1
106     end
107
108     r = RichText.new("markdown", "foo ![bar](http://example.com/example.png) bar")
109     assert_html r do
110       assert_select "img", 1
111       assert_select "img[alt='bar']", 1
112       assert_select "img[src='http://example.com/example.png']", 1
113     end
114
115     r = RichText.new("markdown", "# foo bar baz")
116     assert_html r do
117       assert_select "h1", "foo bar baz"
118     end
119
120     r = RichText.new("markdown", "## foo bar baz")
121     assert_html r do
122       assert_select "h2", "foo bar baz"
123     end
124
125     r = RichText.new("markdown", "### foo bar baz")
126     assert_html r do
127       assert_select "h3", "foo bar baz"
128     end
129
130     r = RichText.new("markdown", "* foo bar baz")
131     assert_html r do
132       assert_select "ul" do
133         assert_select "li", "foo bar baz"
134       end
135     end
136
137     r = RichText.new("markdown", "1. foo bar baz")
138     assert_html r do
139       assert_select "ol" do
140         assert_select "li", "foo bar baz"
141       end
142     end
143
144     r = RichText.new("markdown", "foo *bar* _baz_ qux")
145     assert_html r do
146       assert_select "em", "bar"
147       assert_select "em", "baz"
148     end
149
150     r = RichText.new("markdown", "foo **bar** __baz__ qux")
151     assert_html r do
152       assert_select "strong", "bar"
153       assert_select "strong", "baz"
154     end
155
156     r = RichText.new("markdown", "foo `bar` baz")
157     assert_html r do
158       assert_select "code", "bar"
159     end
160
161     r = RichText.new("markdown", "    foo bar baz")
162     assert_html r do
163       assert_select "pre", /^\s*foo bar baz\s*$/
164     end
165
166     r = RichText.new("markdown", "|column|column")
167     assert_html r do
168       assert_select "table[class='table table-sm w-auto']"
169     end
170
171     r = RichText.new("markdown", "Click Me\n{:.btn.btn-warning}")
172     assert_html r do
173       assert_select "p[class='btn btn-warning']", false
174       assert_select "p", /^Click Me$/
175     end
176
177     r = RichText.new("markdown", "<p style='color:red'>Danger</p>")
178     assert_html r do
179       assert_select "p[style='color:red']", false
180       assert_select "p", /^Danger$/
181     end
182   end
183
184   def test_markdown_to_text
185     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
186     assert_equal "foo [bar](http://example.com/) baz", r.to_text
187   end
188
189   def test_markdown_spam_score
190     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
191     assert_equal 50, r.spam_score.round
192   end
193
194   def test_text_to_html
195     r = RichText.new("text", "foo http://example.com/ bar")
196     assert_html r do
197       assert_select "a", 1
198       assert_select "a[href='http://example.com/']", 1
199       assert_select "a[rel='nofollow noopener noreferrer']", 1
200     end
201
202     r = RichText.new("text", "foo example@example.com bar")
203     assert_html r do
204       assert_select "a", 0
205     end
206
207     r = RichText.new("text", "foo < bar & baz > qux")
208     assert_html r do
209       assert_select "p", "foo < bar & baz > qux"
210     end
211   end
212
213   def test_text_to_text
214     r = RichText.new("text", "foo http://example.com/ bar")
215     assert_equal "foo http://example.com/ bar", r.to_text
216   end
217
218   def test_text_spam_score
219     r = RichText.new("text", "foo http://example.com/ bar")
220     assert_equal 141, r.spam_score.round
221   end
222
223   private
224
225   def assert_html(richtext, &block)
226     html = richtext.to_html
227     assert html.html_safe?
228     root = Nokogiri::HTML::DocumentFragment.parse(html)
229     assert_select root, "*" do
230       yield block
231     end
232   end
233 end