##// END OF EJS Templates
Adds tests for class attribute parsing on pre/code tags....
Jean-Philippe Lang -
r4423:a7595ec19154
parent child
Show More
@@ -1,655 +1,658
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2010 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 20 class ApplicationHelperTest < ActionView::TestCase
21 21
22 22 fixtures :projects, :roles, :enabled_modules, :users,
23 23 :repositories, :changesets,
24 24 :trackers, :issue_statuses, :issues, :versions, :documents,
25 25 :wikis, :wiki_pages, :wiki_contents,
26 26 :boards, :messages,
27 27 :attachments,
28 28 :enumerations
29 29
30 30 def setup
31 31 super
32 32 end
33 33
34 34 context "#link_to_if_authorized" do
35 35 context "authorized user" do
36 36 should "be tested"
37 37 end
38 38
39 39 context "unauthorized user" do
40 40 should "be tested"
41 41 end
42 42
43 43 should "allow using the :controller and :action for the target link" do
44 44 User.current = User.find_by_login('admin')
45 45
46 46 @project = Issue.first.project # Used by helper
47 47 response = link_to_if_authorized("By controller/action",
48 48 {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
49 49 assert_match /href/, response
50 50 end
51 51
52 52 end
53 53
54 54 def test_auto_links
55 55 to_test = {
56 56 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
57 57 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
58 58 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
59 59 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
60 60 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
61 61 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
62 62 'http://foo.bar/foo.bar#foo.bar.' => '<a class="external" href="http://foo.bar/foo.bar#foo.bar">http://foo.bar/foo.bar#foo.bar</a>.',
63 63 'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
64 64 '(see inline link : http://www.foo.bar/Test_(foobar))' => '(see inline link : <a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>)',
65 65 '(see inline link : http://www.foo.bar/Test)' => '(see inline link : <a class="external" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>)',
66 66 '(see inline link : http://www.foo.bar/Test).' => '(see inline link : <a class="external" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>).',
67 67 '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
68 68 '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
69 69 '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
70 70 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
71 71 'http://foo.bar/page?p=1&t=z&s=' => '<a class="external" href="http://foo.bar/page?p=1&#38;t=z&#38;s=">http://foo.bar/page?p=1&#38;t=z&#38;s=</a>',
72 72 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
73 73 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
74 74 'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
75 75 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
76 76 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
77 77 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
78 78 # two exclamation marks
79 79 'http://example.net/path!602815048C7B5C20!302.html' => '<a class="external" href="http://example.net/path!602815048C7B5C20!302.html">http://example.net/path!602815048C7B5C20!302.html</a>',
80 80 # escaping
81 81 'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo"bar</a>',
82 82 # wrap in angle brackets
83 83 '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;'
84 84 }
85 85 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
86 86 end
87 87
88 88 def test_auto_mailto
89 89 assert_equal '<p><a class="email" href="mailto:test@foo.bar">test@foo.bar</a></p>',
90 90 textilizable('test@foo.bar')
91 91 end
92 92
93 93 def test_inline_images
94 94 to_test = {
95 95 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
96 96 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
97 97 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
98 98 # inline styles should be stripped
99 99 'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" alt="" />',
100 100 'with title !http://foo.bar/image.jpg(This is a title)!' => 'with title <img src="http://foo.bar/image.jpg" title="This is a title" alt="This is a title" />',
101 101 'with title !http://foo.bar/image.jpg(This is a double-quoted "title")!' => 'with title <img src="http://foo.bar/image.jpg" title="This is a double-quoted &quot;title&quot;" alt="This is a double-quoted &quot;title&quot;" />',
102 102 }
103 103 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
104 104 end
105 105
106 106 def test_inline_images_inside_tags
107 107 raw = <<-RAW
108 108 h1. !foo.png! Heading
109 109
110 110 Centered image:
111 111
112 112 p=. !bar.gif!
113 113 RAW
114 114
115 115 assert textilizable(raw).include?('<img src="foo.png" alt="" />')
116 116 assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
117 117 end
118 118
119 119 def test_attached_images
120 120 to_test = {
121 121 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
122 122 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
123 123 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
124 124 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
125 125 # link image
126 126 '!logo.gif!:http://foo.bar/' => '<a href="http://foo.bar/"><img src="/attachments/download/3" title="This is a logo" alt="This is a logo" /></a>',
127 127 }
128 128 attachments = Attachment.find(:all)
129 129 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
130 130 end
131 131
132 132 def test_textile_external_links
133 133 to_test = {
134 134 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
135 135 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
136 136 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
137 137 '"link (Link title with "double-quotes")":http://foo.bar' => '<a href="http://foo.bar" title="Link title with &quot;double-quotes&quot;" class="external">link</a>',
138 138 "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
139 139 # no multiline link text
140 140 "This is a double quote \"on the first line\nand another on a second line\":test" => "This is a double quote \"on the first line<br />and another on a second line\":test",
141 141 # mailto link
142 142 "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
143 143 # two exclamation marks
144 144 '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
145 145 # escaping
146 146 '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
147 147 }
148 148 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
149 149 end
150 150
151 151 def test_redmine_links
152 152 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
153 153 :class => 'issue status-1 priority-1 overdue', :title => 'Error 281 when updating a recipe (New)')
154 154
155 155 changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
156 156 :class => 'changeset', :title => 'My very first commit')
157 157 changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
158 158 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
159 159
160 160 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
161 161 :class => 'document')
162 162
163 163 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
164 164 :class => 'version')
165 165
166 166 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
167 167
168 168 project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
169 169
170 170 source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}
171 171 source_url_with_ext = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file.ext']}
172 172
173 173 to_test = {
174 174 # tickets
175 175 '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
176 176 # changesets
177 177 'r1' => changeset_link,
178 178 'r1.' => "#{changeset_link}.",
179 179 'r1, r2' => "#{changeset_link}, #{changeset_link2}",
180 180 'r1,r2' => "#{changeset_link},#{changeset_link2}",
181 181 # documents
182 182 'document#1' => document_link,
183 183 'document:"Test document"' => document_link,
184 184 # versions
185 185 'version#2' => version_link,
186 186 'version:1.0' => version_link,
187 187 'version:"1.0"' => version_link,
188 188 # source
189 189 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
190 190 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
191 191 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
192 192 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
193 193 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
194 194 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
195 195 'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'),
196 196 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_ext.merge(:rev => 52), :class => 'source'),
197 197 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'),
198 198 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext.merge(:anchor => 'L110'), :class => 'source'),
199 199 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'),
200 200 'export:/some/file' => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'),
201 201 # message
202 202 'message#4' => link_to('Post 2', message_url, :class => 'message'),
203 203 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5'), :class => 'message'),
204 204 # project
205 205 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
206 206 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
207 207 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
208 208 # escaping
209 209 '!#3.' => '#3.',
210 210 '!r1' => 'r1',
211 211 '!document#1' => 'document#1',
212 212 '!document:"Test document"' => 'document:"Test document"',
213 213 '!version#2' => 'version#2',
214 214 '!version:1.0' => 'version:1.0',
215 215 '!version:"1.0"' => 'version:"1.0"',
216 216 '!source:/some/file' => 'source:/some/file',
217 217 # not found
218 218 '#0123456789' => '#0123456789',
219 219 # invalid expressions
220 220 'source:' => 'source:',
221 221 # url hash
222 222 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
223 223 }
224 224 @project = Project.find(1)
225 225 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
226 226 end
227 227
228 228 def test_attachment_links
229 229 attachment_link = link_to('error281.txt', {:controller => 'attachments', :action => 'download', :id => '1'}, :class => 'attachment')
230 230 to_test = {
231 231 'attachment:error281.txt' => attachment_link
232 232 }
233 233 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" }
234 234 end
235 235
236 236 def test_wiki_links
237 237 to_test = {
238 238 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
239 239 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
240 240 # link with anchor
241 241 '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
242 242 '[[Another page#anchor|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page#anchor" class="wiki-page">Page</a>',
243 243 # page that doesn't exist
244 244 '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
245 245 '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
246 246 # link to another project wiki
247 247 '[[onlinestore:]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">onlinestore</a>',
248 248 '[[onlinestore:|Wiki]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">Wiki</a>',
249 249 '[[onlinestore:Start page]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Start page</a>',
250 250 '[[onlinestore:Start page|Text]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Text</a>',
251 251 '[[onlinestore:Unknown page]]' => '<a href="/projects/onlinestore/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
252 252 # striked through link
253 253 '-[[Another page|Page]]-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a></del>',
254 254 '-[[Another page|Page]] link-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a> link</del>',
255 255 # escaping
256 256 '![[Another page|Page]]' => '[[Another page|Page]]',
257 257 # project does not exist
258 258 '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
259 259 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
260 260 }
261 261 @project = Project.find(1)
262 262 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
263 263 end
264 264
265 265 def test_html_tags
266 266 to_test = {
267 267 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
268 268 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
269 269 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
270 270 # do not escape pre/code tags
271 271 "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
272 272 "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
273 273 "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
274 274 "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
275 275 "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
276 276 # remove attributes except class
277 277 "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
278 278 '<pre class="foo">some text</pre>' => '<pre class="foo">some text</pre>',
279 279 "<pre class='foo bar'>some text</pre>" => "<pre class='foo bar'>some text</pre>",
280 280 '<pre class="foo bar">some text</pre>' => '<pre class="foo bar">some text</pre>',
281 281 "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
282 # xss
283 '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
284 '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
282 285 }
283 286 to_test.each { |text, result| assert_equal result, textilizable(text) }
284 287 end
285 288
286 289 def test_allowed_html_tags
287 290 to_test = {
288 291 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
289 292 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
290 293 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
291 294 }
292 295 to_test.each { |text, result| assert_equal result, textilizable(text) }
293 296 end
294 297
295 298 def test_pre_tags
296 299 raw = <<-RAW
297 300 Before
298 301
299 302 <pre>
300 303 <prepared-statement-cache-size>32</prepared-statement-cache-size>
301 304 </pre>
302 305
303 306 After
304 307 RAW
305 308
306 309 expected = <<-EXPECTED
307 310 <p>Before</p>
308 311 <pre>
309 312 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
310 313 </pre>
311 314 <p>After</p>
312 315 EXPECTED
313 316
314 317 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
315 318 end
316 319
317 320 def test_pre_content_should_not_parse_wiki_and_redmine_links
318 321 raw = <<-RAW
319 322 [[CookBook documentation]]
320 323
321 324 #1
322 325
323 326 <pre>
324 327 [[CookBook documentation]]
325 328
326 329 #1
327 330 </pre>
328 331 RAW
329 332
330 333 expected = <<-EXPECTED
331 334 <p><a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a></p>
332 335 <p><a href="/issues/1" class="issue status-1 priority-1" title="Can't print recipes (New)">#1</a></p>
333 336 <pre>
334 337 [[CookBook documentation]]
335 338
336 339 #1
337 340 </pre>
338 341 EXPECTED
339 342
340 343 @project = Project.find(1)
341 344 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
342 345 end
343 346
344 347 def test_non_closing_pre_blocks_should_be_closed
345 348 raw = <<-RAW
346 349 <pre><code>
347 350 RAW
348 351
349 352 expected = <<-EXPECTED
350 353 <pre><code>
351 354 </code></pre>
352 355 EXPECTED
353 356
354 357 @project = Project.find(1)
355 358 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
356 359 end
357 360
358 361 def test_syntax_highlight
359 362 raw = <<-RAW
360 363 <pre><code class="ruby">
361 364 # Some ruby code here
362 365 </code></pre>
363 366 RAW
364 367
365 368 expected = <<-EXPECTED
366 369 <pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="no">1</span> <span class="c"># Some ruby code here</span></span>
367 370 </code></pre>
368 371 EXPECTED
369 372
370 373 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
371 374 end
372 375
373 376 def test_wiki_links_in_tables
374 377 to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
375 378 '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
376 379 '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
377 380 '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
378 381 }
379 382 @project = Project.find(1)
380 383 to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
381 384 end
382 385
383 386 def test_text_formatting
384 387 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
385 388 '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
386 389 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
387 390 'a H *umane* W *eb* T *ext* G *enerator*' => 'a H <strong>umane</strong> W <strong>eb</strong> T <strong>ext</strong> G <strong>enerator</strong>',
388 391 'a *H* umane *W* eb *T* ext *G* enerator' => 'a <strong>H</strong> umane <strong>W</strong> eb <strong>T</strong> ext <strong>G</strong> enerator',
389 392 }
390 393 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
391 394 end
392 395
393 396 def test_wiki_horizontal_rule
394 397 assert_equal '<hr />', textilizable('---')
395 398 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
396 399 end
397 400
398 401 def test_footnotes
399 402 raw = <<-RAW
400 403 This is some text[1].
401 404
402 405 fn1. This is the foot note
403 406 RAW
404 407
405 408 expected = <<-EXPECTED
406 409 <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
407 410 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
408 411 EXPECTED
409 412
410 413 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
411 414 end
412 415
413 416 def test_table_of_content
414 417 raw = <<-RAW
415 418 {{toc}}
416 419
417 420 h1. Title
418 421
419 422 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
420 423
421 424 h2. Subtitle with a [[Wiki]] link
422 425
423 426 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
424 427
425 428 h2. Subtitle with [[Wiki|another Wiki]] link
426 429
427 430 h2. Subtitle with %{color:red}red text%
428 431
429 432 h3. Subtitle with *some* _modifiers_
430 433
431 434 h1. Another title
432 435
433 436 h3. An "Internet link":http://www.redmine.org/ inside subtitle
434 437
435 438 h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
436 439
437 440 RAW
438 441
439 442 expected = '<ul class="toc">' +
440 443 '<li><a href="#Title">Title</a>' +
441 444 '<ul>' +
442 445 '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
443 446 '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
444 447 '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
445 448 '<ul>' +
446 449 '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
447 450 '</ul>' +
448 451 '</li>' +
449 452 '</ul>' +
450 453 '</li>' +
451 454 '<li><a href="#Another-title">Another title</a>' +
452 455 '<ul>' +
453 456 '<li>' +
454 457 '<ul>' +
455 458 '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
456 459 '</ul>' +
457 460 '</li>' +
458 461 '<li><a href="#Project-Name">Project Name</a></li>' +
459 462 '</ul>' +
460 463 '</li>' +
461 464 '</ul>'
462 465
463 466 @project = Project.find(1)
464 467 assert textilizable(raw).gsub("\n", "").include?(expected)
465 468 end
466 469
467 470 def test_table_of_content_should_contain_included_page_headings
468 471 raw = <<-RAW
469 472 {{toc}}
470 473
471 474 h1. Included
472 475
473 476 {{include(Child_1)}}
474 477 RAW
475 478
476 479 expected = '<ul class="toc">' +
477 480 '<li><a href="#Included">Included</a></li>' +
478 481 '<li><a href="#Child-page-1">Child page 1</a></li>' +
479 482 '</ul>'
480 483
481 484 @project = Project.find(1)
482 485 assert textilizable(raw).gsub("\n", "").include?(expected)
483 486 end
484 487
485 488 def test_blockquote
486 489 # orig raw text
487 490 raw = <<-RAW
488 491 John said:
489 492 > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
490 493 > Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
491 494 > * Donec odio lorem,
492 495 > * sagittis ac,
493 496 > * malesuada in,
494 497 > * adipiscing eu, dolor.
495 498 >
496 499 > >Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.
497 500 > Proin a tellus. Nam vel neque.
498 501
499 502 He's right.
500 503 RAW
501 504
502 505 # expected html
503 506 expected = <<-EXPECTED
504 507 <p>John said:</p>
505 508 <blockquote>
506 509 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
507 510 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
508 511 <ul>
509 512 <li>Donec odio lorem,</li>
510 513 <li>sagittis ac,</li>
511 514 <li>malesuada in,</li>
512 515 <li>adipiscing eu, dolor.</li>
513 516 </ul>
514 517 <blockquote>
515 518 <p>Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.</p>
516 519 </blockquote>
517 520 <p>Proin a tellus. Nam vel neque.</p>
518 521 </blockquote>
519 522 <p>He's right.</p>
520 523 EXPECTED
521 524
522 525 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
523 526 end
524 527
525 528 def test_table
526 529 raw = <<-RAW
527 530 This is a table with empty cells:
528 531
529 532 |cell11|cell12||
530 533 |cell21||cell23|
531 534 |cell31|cell32|cell33|
532 535 RAW
533 536
534 537 expected = <<-EXPECTED
535 538 <p>This is a table with empty cells:</p>
536 539
537 540 <table>
538 541 <tr><td>cell11</td><td>cell12</td><td></td></tr>
539 542 <tr><td>cell21</td><td></td><td>cell23</td></tr>
540 543 <tr><td>cell31</td><td>cell32</td><td>cell33</td></tr>
541 544 </table>
542 545 EXPECTED
543 546
544 547 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
545 548 end
546 549
547 550 def test_table_with_line_breaks
548 551 raw = <<-RAW
549 552 This is a table with line breaks:
550 553
551 554 |cell11
552 555 continued|cell12||
553 556 |-cell21-||cell23
554 557 cell23 line2
555 558 cell23 *line3*|
556 559 |cell31|cell32
557 560 cell32 line2|cell33|
558 561
559 562 RAW
560 563
561 564 expected = <<-EXPECTED
562 565 <p>This is a table with line breaks:</p>
563 566
564 567 <table>
565 568 <tr>
566 569 <td>cell11<br />continued</td>
567 570 <td>cell12</td>
568 571 <td></td>
569 572 </tr>
570 573 <tr>
571 574 <td><del>cell21</del></td>
572 575 <td></td>
573 576 <td>cell23<br/>cell23 line2<br/>cell23 <strong>line3</strong></td>
574 577 </tr>
575 578 <tr>
576 579 <td>cell31</td>
577 580 <td>cell32<br/>cell32 line2</td>
578 581 <td>cell33</td>
579 582 </tr>
580 583 </table>
581 584 EXPECTED
582 585
583 586 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
584 587 end
585 588
586 589 def test_textile_should_not_mangle_brackets
587 590 assert_equal '<p>[msg1][msg2]</p>', textilizable('[msg1][msg2]')
588 591 end
589 592
590 593 def test_default_formatter
591 594 Setting.text_formatting = 'unknown'
592 595 text = 'a *link*: http://www.example.net/'
593 596 assert_equal '<p>a *link*: <a href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
594 597 Setting.text_formatting = 'textile'
595 598 end
596 599
597 600 def test_due_date_distance_in_words
598 601 to_test = { Date.today => 'Due in 0 days',
599 602 Date.today + 1 => 'Due in 1 day',
600 603 Date.today + 100 => 'Due in about 3 months',
601 604 Date.today + 20000 => 'Due in over 54 years',
602 605 Date.today - 1 => '1 day late',
603 606 Date.today - 100 => 'about 3 months late',
604 607 Date.today - 20000 => 'over 54 years late',
605 608 }
606 609 to_test.each do |date, expected|
607 610 assert_equal expected, due_date_distance_in_words(date)
608 611 end
609 612 end
610 613
611 614 def test_avatar
612 615 # turn on avatars
613 616 Setting.gravatar_enabled = '1'
614 617 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
615 618 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
616 619 assert_nil avatar('jsmith')
617 620 assert_nil avatar(nil)
618 621
619 622 # turn off avatars
620 623 Setting.gravatar_enabled = '0'
621 624 assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
622 625 end
623 626
624 627 def test_link_to_user
625 628 user = User.find(2)
626 629 t = link_to_user(user)
627 630 assert_equal "<a href=\"/users/2\">#{ user.name }</a>", t
628 631 end
629 632
630 633 def test_link_to_user_should_not_link_to_locked_user
631 634 user = User.find(5)
632 635 assert user.locked?
633 636 t = link_to_user(user)
634 637 assert_equal user.name, t
635 638 end
636 639
637 640 def test_link_to_user_should_not_link_to_anonymous
638 641 user = User.anonymous
639 642 assert user.anonymous?
640 643 t = link_to_user(user)
641 644 assert_equal ::I18n.t(:label_user_anonymous), t
642 645 end
643 646
644 647 def test_link_to_project
645 648 project = Project.find(1)
646 649 assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
647 650 link_to_project(project)
648 651 assert_equal %(<a href="/projects/ecookbook/settings">eCookbook</a>),
649 652 link_to_project(project, :action => 'settings')
650 653 assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
651 654 link_to_project(project, {:only_path => false, :jump => 'blah'})
652 655 assert_equal %(<a href="/projects/ecookbook/settings" class="project">eCookbook</a>),
653 656 link_to_project(project, {:action => 'settings'}, :class => "project")
654 657 end
655 658 end
General Comments 0
You need to be logged in to leave comments. Login now