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