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