comparison test/unit/helpers/application_helper_test.rb @ 441:cbce1fd3b1b7 redmine-1.2

Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author Chris Cannam
date Mon, 06 Jun 2011 14:24:13 +0100
parents 051f544170fe
children 753f1380d6bc cbb26bc654de
comparison
equal deleted inserted replaced
245:051f544170fe 441:cbce1fd3b1b7
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2010 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
8 # 8 #
9 # This program is distributed in the hope that it will be useful, 9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details. 12 # GNU General Public License for more details.
13 # 13 #
14 # You should have received a copy of the GNU General Public License 14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software 15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 require File.expand_path('../../../test_helper', __FILE__) 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 class ApplicationHelperTest < ActionView::TestCase 20 class ApplicationHelperTest < ActionView::TestCase
21 21
22 fixtures :projects, :roles, :enabled_modules, :users, 22 fixtures :projects, :roles, :enabled_modules, :users,
23 :repositories, :changesets, 23 :repositories, :changesets,
24 :trackers, :issue_statuses, :issues, :versions, :documents, 24 :trackers, :issue_statuses, :issues, :versions, :documents,
25 :wikis, :wiki_pages, :wiki_contents, 25 :wikis, :wiki_pages, :wiki_contents,
26 :boards, :messages, 26 :boards, :messages,
27 :attachments, 27 :attachments,
28 :enumerations 28 :enumerations
33 33
34 context "#link_to_if_authorized" do 34 context "#link_to_if_authorized" do
35 context "authorized user" do 35 context "authorized user" do
36 should "be tested" 36 should "be tested"
37 end 37 end
38 38
39 context "unauthorized user" do 39 context "unauthorized user" do
40 should "be tested" 40 should "be tested"
41 end 41 end
42 42
43 should "allow using the :controller and :action for the target link" do 43 should "allow using the :controller and :action for the target link" do
44 User.current = User.find_by_login('admin') 44 User.current = User.find_by_login('admin')
45 45
46 @project = Issue.first.project # Used by helper 46 @project = Issue.first.project # Used by helper
47 response = link_to_if_authorized("By controller/action", 47 response = link_to_if_authorized("By controller/action",
48 {:controller => 'issues', :action => 'edit', :id => Issue.first.id}) 48 {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
49 assert_match /href/, response 49 assert_match /href/, response
50 end 50 end
51 51
52 end 52 end
53 53
54 def test_auto_links 54 def test_auto_links
55 to_test = { 55 to_test = {
56 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>', 56 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
57 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>', 57 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
58 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.', 58 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
82 # wrap in angle brackets 82 # wrap in angle brackets
83 '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;' 83 '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;'
84 } 84 }
85 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } 85 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
86 end 86 end
87 87
88 def test_auto_mailto 88 def test_auto_mailto
89 assert_equal '<p><a class="email" href="mailto:test@foo.bar">test@foo.bar</a></p>', 89 assert_equal '<p><a class="email" href="mailto:test@foo.bar">test@foo.bar</a></p>',
90 textilizable('test@foo.bar') 90 textilizable('test@foo.bar')
91 end 91 end
92 92
93 def test_inline_images 93 def test_inline_images
94 to_test = { 94 to_test = {
95 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />', 95 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
96 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>', 96 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
97 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />', 97 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
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" />', 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 '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;" />', 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 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } 103 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
104 end 104 end
105 105
106 def test_inline_images_inside_tags 106 def test_inline_images_inside_tags
107 raw = <<-RAW 107 raw = <<-RAW
108 h1. !foo.png! Heading 108 h1. !foo.png! Heading
109 109
110 Centered image: 110 Centered image:
113 RAW 113 RAW
114 114
115 assert textilizable(raw).include?('<img src="foo.png" alt="" />') 115 assert textilizable(raw).include?('<img src="foo.png" alt="" />')
116 assert textilizable(raw).include?('<img src="bar.gif" alt="" />') 116 assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
117 end 117 end
118 118
119 def test_attached_images 119 def test_attached_images
120 to_test = { 120 to_test = {
121 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />', 121 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
122 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />', 122 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
123 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />', 123 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
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>', 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 attachments = Attachment.find(:all) 128 attachments = Attachment.find(:all)
129 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) } 129 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
130 end 130 end
131 131
132 def test_textile_external_links 132 def test_textile_external_links
133 to_test = { 133 to_test = {
134 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>', 134 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
135 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>', 135 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
136 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>', 136 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
147 } 147 }
148 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } 148 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
149 end 149 end
150 150
151 def test_redmine_links 151 def test_redmine_links
152 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3}, 152 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
153 :class => 'issue status-1 priority-1 overdue', :title => 'Error 281 when updating a recipe (New)') 153 :class => 'issue status-1 priority-1 overdue', :title => 'Error 281 when updating a recipe (New)')
154 154
155 changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1}, 155 changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
156 :class => 'changeset', :title => 'My very first commit') 156 :class => 'changeset', :title => 'My very first commit')
157 changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2}, 157 changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
158 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3') 158 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
159 159
160 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1}, 160 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
161 :class => 'document') 161 :class => 'document')
162 162
163 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2}, 163 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
164 :class => 'version') 164 :class => 'version')
165 165
166 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4} 166 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
167 167
168 project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'} 168 project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
169 169
170 source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']} 170 source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}
171 source_url_with_ext = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file.ext']} 171 source_url_with_ext = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file.ext']}
172 172
173 to_test = { 173 to_test = {
174 # tickets 174 # tickets
175 '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.", 175 '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
176 # changesets 176 # changesets
177 'r1' => changeset_link, 177 'r1' => changeset_link,
222 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>', 222 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
223 } 223 }
224 @project = Project.find(1) 224 @project = Project.find(1)
225 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" } 225 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
226 end 226 end
227 227
228 def test_cross_project_redmine_links 228 def test_cross_project_redmine_links
229 source_link = link_to('ecookbook:source:/some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, 229 source_link = link_to('ecookbook:source:/some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']},
230 :class => 'source') 230 :class => 'source')
231 231
232 changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2}, 232 changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
233 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3') 233 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
234 234
235 to_test = { 235 to_test = {
236 # documents 236 # documents
237 'document:"Test document"' => 'document:"Test document"', 237 'document:"Test document"' => 'document:"Test document"',
238 'ecookbook:document:"Test document"' => '<a href="/documents/1" class="document">Test document</a>', 238 'ecookbook:document:"Test document"' => '<a href="/documents/1" class="document">Test document</a>',
239 'invalid:document:"Test document"' => 'invalid:document:"Test document"', 239 'invalid:document:"Test document"' => 'invalid:document:"Test document"',
343 to_test = { 343 to_test = {
344 'attachment:error281.txt' => attachment_link 344 'attachment:error281.txt' => attachment_link
345 } 345 }
346 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" } 346 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" }
347 end 347 end
348 348
349 def test_wiki_links 349 def test_wiki_links
350 to_test = { 350 to_test = {
351 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>', 351 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
352 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>', 352 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
353 # link with anchor 353 # link with anchor
372 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]', 372 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
373 } 373 }
374 @project = Project.find(1) 374 @project = Project.find(1)
375 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } 375 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
376 end 376 end
377 377
378 def test_html_tags 378 def test_html_tags
379 to_test = { 379 to_test = {
380 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>", 380 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
381 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>", 381 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
382 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>", 382 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
396 '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>', 396 '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
397 '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>', 397 '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
398 } 398 }
399 to_test.each { |text, result| assert_equal result, textilizable(text) } 399 to_test.each { |text, result| assert_equal result, textilizable(text) }
400 end 400 end
401 401
402 def test_allowed_html_tags 402 def test_allowed_html_tags
403 to_test = { 403 to_test = {
404 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>", 404 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
405 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting", 405 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
406 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;" 406 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
407 } 407 }
408 to_test.each { |text, result| assert_equal result, textilizable(text) } 408 to_test.each { |text, result| assert_equal result, textilizable(text) }
409 end 409 end
410 410
411 def test_pre_tags 411 def test_pre_tags
412 raw = <<-RAW 412 raw = <<-RAW
413 Before 413 Before
414 414
415 <pre> 415 <pre>
424 <pre> 424 <pre>
425 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt; 425 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
426 </pre> 426 </pre>
427 <p>After</p> 427 <p>After</p>
428 EXPECTED 428 EXPECTED
429 429
430 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '') 430 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
431 end 431 end
432 432
433 def test_pre_content_should_not_parse_wiki_and_redmine_links 433 def test_pre_content_should_not_parse_wiki_and_redmine_links
434 raw = <<-RAW 434 raw = <<-RAW
435 [[CookBook documentation]] 435 [[CookBook documentation]]
436 436
437 #1 437 #1
450 [[CookBook documentation]] 450 [[CookBook documentation]]
451 451
452 #1 452 #1
453 </pre> 453 </pre>
454 EXPECTED 454 EXPECTED
455 455
456 @project = Project.find(1) 456 @project = Project.find(1)
457 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '') 457 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
458 end 458 end
459 459
460 def test_non_closing_pre_blocks_should_be_closed 460 def test_non_closing_pre_blocks_should_be_closed
461 raw = <<-RAW 461 raw = <<-RAW
462 <pre><code> 462 <pre><code>
463 RAW 463 RAW
464 464
465 expected = <<-EXPECTED 465 expected = <<-EXPECTED
466 <pre><code> 466 <pre><code>
467 </code></pre> 467 </code></pre>
468 EXPECTED 468 EXPECTED
469 469
470 @project = Project.find(1) 470 @project = Project.find(1)
471 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '') 471 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
472 end 472 end
473 473
474 def test_syntax_highlight 474 def test_syntax_highlight
475 raw = <<-RAW 475 raw = <<-RAW
476 <pre><code class="ruby"> 476 <pre><code class="ruby">
477 # Some ruby code here 477 # Some ruby code here
478 </code></pre> 478 </code></pre>
483 </code></pre> 483 </code></pre>
484 EXPECTED 484 EXPECTED
485 485
486 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '') 486 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
487 end 487 end
488 488
489 def test_wiki_links_in_tables 489 def test_wiki_links_in_tables
490 to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" => 490 to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
491 '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' + 491 '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
492 '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' + 492 '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
493 '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>' 493 '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
494 } 494 }
495 @project = Project.find(1) 495 @project = Project.find(1)
496 to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') } 496 to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
497 end 497 end
498 498
499 def test_text_formatting 499 def test_text_formatting
500 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>', 500 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
501 '(_text within parentheses_)' => '(<em>text within parentheses</em>)', 501 '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
502 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator', 502 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
503 '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>', 503 '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>',
504 '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', 504 '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',
505 } 505 }
506 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) } 506 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
507 end 507 end
508 508
509 def test_wiki_horizontal_rule 509 def test_wiki_horizontal_rule
510 assert_equal '<hr />', textilizable('---') 510 assert_equal '<hr />', textilizable('---')
511 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---') 511 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
512 end 512 end
513 513
514 def test_footnotes 514 def test_footnotes
515 raw = <<-RAW 515 raw = <<-RAW
516 This is some text[1]. 516 This is some text[1].
517 517
518 fn1. This is the foot note 518 fn1. This is the foot note
523 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p> 523 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
524 EXPECTED 524 EXPECTED
525 525
526 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '') 526 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
527 end 527 end
528 528
529 def test_headings
530 raw = 'h1. Some heading'
531 expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
532
533 assert_equal expected, textilizable(raw)
534 end
535
529 def test_table_of_content 536 def test_table_of_content
530 raw = <<-RAW 537 raw = <<-RAW
531 {{toc}} 538 {{toc}}
532 539
533 h1. Title 540 h1. Title
557 RAW 564 RAW
558 565
559 expected = '<ul class="toc">' + 566 expected = '<ul class="toc">' +
560 '<li><a href="#Title">Title</a>' + 567 '<li><a href="#Title">Title</a>' +
561 '<ul>' + 568 '<ul>' +
562 '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' + 569 '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
563 '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' + 570 '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
564 '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' + 571 '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
565 '<ul>' + 572 '<ul>' +
566 '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' + 573 '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
567 '</ul>' + 574 '</ul>' +
568 '</li>' + 575 '</li>' +
581 '</ul>' 588 '</ul>'
582 589
583 @project = Project.find(1) 590 @project = Project.find(1)
584 assert textilizable(raw).gsub("\n", "").include?(expected), textilizable(raw) 591 assert textilizable(raw).gsub("\n", "").include?(expected), textilizable(raw)
585 end 592 end
586 593
587 def test_table_of_content_should_contain_included_page_headings 594 def test_table_of_content_should_contain_included_page_headings
588 raw = <<-RAW 595 raw = <<-RAW
589 {{toc}} 596 {{toc}}
590 597
591 h1. Included 598 h1. Included
593 {{include(Child_1)}} 600 {{include(Child_1)}}
594 RAW 601 RAW
595 602
596 expected = '<ul class="toc">' + 603 expected = '<ul class="toc">' +
597 '<li><a href="#Included">Included</a></li>' + 604 '<li><a href="#Included">Included</a></li>' +
598 '<li><a href="#Child-page-1">Child page 1</a></li>' + 605 '<li><a href="#Child-page-1">Child page 1</a></li>' +
599 '</ul>' 606 '</ul>'
600 607
601 @project = Project.find(1) 608 @project = Project.find(1)
602 assert textilizable(raw).gsub("\n", "").include?(expected) 609 assert textilizable(raw).gsub("\n", "").include?(expected)
603 end 610 end
604 611
605 def test_blockquote
606 # orig raw text
607 raw = <<-RAW
608 John said:
609 > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
610 > Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
611 > * Donec odio lorem,
612 > * sagittis ac,
613 > * malesuada in,
614 > * adipiscing eu, dolor.
615 >
616 > >Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.
617 > Proin a tellus. Nam vel neque.
618
619 He's right.
620 RAW
621
622 # expected html
623 expected = <<-EXPECTED
624 <p>John said:</p>
625 <blockquote>
626 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
627 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
628 <ul>
629 <li>Donec odio lorem,</li>
630 <li>sagittis ac,</li>
631 <li>malesuada in,</li>
632 <li>adipiscing eu, dolor.</li>
633 </ul>
634 <blockquote>
635 <p>Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.</p>
636 </blockquote>
637 <p>Proin a tellus. Nam vel neque.</p>
638 </blockquote>
639 <p>He's right.</p>
640 EXPECTED
641
642 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
643 end
644
645 def test_table
646 raw = <<-RAW
647 This is a table with empty cells:
648
649 |cell11|cell12||
650 |cell21||cell23|
651 |cell31|cell32|cell33|
652 RAW
653
654 expected = <<-EXPECTED
655 <p>This is a table with empty cells:</p>
656
657 <table>
658 <tr><td>cell11</td><td>cell12</td><td></td></tr>
659 <tr><td>cell21</td><td></td><td>cell23</td></tr>
660 <tr><td>cell31</td><td>cell32</td><td>cell33</td></tr>
661 </table>
662 EXPECTED
663
664 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
665 end
666
667 def test_table_with_line_breaks
668 raw = <<-RAW
669 This is a table with line breaks:
670
671 |cell11
672 continued|cell12||
673 |-cell21-||cell23
674 cell23 line2
675 cell23 *line3*|
676 |cell31|cell32
677 cell32 line2|cell33|
678
679 RAW
680
681 expected = <<-EXPECTED
682 <p>This is a table with line breaks:</p>
683
684 <table>
685 <tr>
686 <td>cell11<br />continued</td>
687 <td>cell12</td>
688 <td></td>
689 </tr>
690 <tr>
691 <td><del>cell21</del></td>
692 <td></td>
693 <td>cell23<br/>cell23 line2<br/>cell23 <strong>line3</strong></td>
694 </tr>
695 <tr>
696 <td>cell31</td>
697 <td>cell32<br/>cell32 line2</td>
698 <td>cell33</td>
699 </tr>
700 </table>
701 EXPECTED
702
703 assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
704 end
705
706 def test_textile_should_not_mangle_brackets
707 assert_equal '<p>[msg1][msg2]</p>', textilizable('[msg1][msg2]')
708 end
709
710 def test_default_formatter 612 def test_default_formatter
711 Setting.text_formatting = 'unknown' 613 Setting.text_formatting = 'unknown'
712 text = 'a *link*: http://www.example.net/' 614 text = 'a *link*: http://www.example.net/'
713 assert_equal '<p>a *link*: <a href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text) 615 assert_equal '<p>a *link*: <a href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
714 Setting.text_formatting = 'textile' 616 Setting.text_formatting = 'textile'
715 end 617 end
716 618
717 def test_due_date_distance_in_words 619 def test_due_date_distance_in_words
718 to_test = { Date.today => 'Due in 0 days', 620 to_test = { Date.today => 'Due in 0 days',
719 Date.today + 1 => 'Due in 1 day', 621 Date.today + 1 => 'Due in 1 day',
720 Date.today + 100 => 'Due in about 3 months', 622 Date.today + 100 => 'Due in about 3 months',
721 Date.today + 20000 => 'Due in over 54 years', 623 Date.today + 20000 => 'Due in over 54 years',
726 ::I18n.locale = :en 628 ::I18n.locale = :en
727 to_test.each do |date, expected| 629 to_test.each do |date, expected|
728 assert_equal expected, due_date_distance_in_words(date) 630 assert_equal expected, due_date_distance_in_words(date)
729 end 631 end
730 end 632 end
731 633
732 def test_avatar 634 def test_avatar
733 # turn on avatars 635 # turn on avatars
734 Setting.gravatar_enabled = '1' 636 Setting.gravatar_enabled = '1'
735 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo')) 637 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
736 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo')) 638 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
737 assert_nil avatar('jsmith') 639 assert_nil avatar('jsmith')
738 assert_nil avatar(nil) 640 assert_nil avatar(nil)
739 641
740 # turn off avatars 642 # turn off avatars
741 Setting.gravatar_enabled = '0' 643 Setting.gravatar_enabled = '0'
742 assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo')) 644 assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
743 end 645 end
744 646
745 def test_link_to_user 647 def test_link_to_user
746 user = User.find(2) 648 user = User.find(2)
747 t = link_to_user(user) 649 t = link_to_user(user)
748 assert_equal "<a href=\"/users/2\">#{ user.name }</a>", t 650 assert_equal "<a href=\"/users/2\">#{ user.name }</a>", t
749 end 651 end
750 652
751 def test_link_to_user_should_not_link_to_locked_user 653 def test_link_to_user_should_not_link_to_locked_user
752 user = User.find(5) 654 user = User.find(5)
753 assert user.locked? 655 assert user.locked?
754 t = link_to_user(user) 656 t = link_to_user(user)
755 assert_equal user.name, t 657 assert_equal user.name, t
756 end 658 end
757 659
758 def test_link_to_user_should_not_link_to_anonymous 660 def test_link_to_user_should_not_link_to_anonymous
759 user = User.anonymous 661 user = User.anonymous
760 assert user.anonymous? 662 assert user.anonymous?
761 t = link_to_user(user) 663 t = link_to_user(user)
762 assert_equal ::I18n.t(:label_user_anonymous), t 664 assert_equal ::I18n.t(:label_user_anonymous), t