OSDN Git Service

Move database.yml to template
[redminele/redminele.git] / redmine / test / unit / helpers / application_helper_test.rb
1 # Redmine - project management software
2 # Copyright (C) 2006-2009  Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 require File.dirname(__FILE__) + '/../../test_helper'
19
20 class ApplicationHelperTest < HelperTestCase
21   include ApplicationHelper
22   include ActionView::Helpers::TextHelper
23   include ActionView::Helpers::DateHelper
24   
25   fixtures :projects, :roles, :enabled_modules, :users,
26                       :repositories, :changesets, 
27                       :trackers, :issue_statuses, :issues, :versions, :documents,
28                       :wikis, :wiki_pages, :wiki_contents,
29                       :boards, :messages,
30                       :attachments
31
32   def setup
33     super
34   end
35   
36   def test_auto_links
37     to_test = {
38       'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
39       'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
40       'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
41       'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
42       'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
43       'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
44       '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>.',
45       'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
46       '(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>)',
47       '(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>)',
48       '(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>).',
49       '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
50       '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
51       '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
52       'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
53       '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>',
54       'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
55       'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
56       'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
57       'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
58       'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
59       'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
60       # two exclamation marks
61       '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>',
62     }
63     to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
64   end
65   
66   def test_auto_mailto
67     assert_equal '<p><a href="mailto:test@foo.bar" class="email">test@foo.bar</a></p>', 
68       textilizable('test@foo.bar')
69   end
70   
71   def test_inline_images
72     to_test = {
73       '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
74       'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
75       'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
76       # inline styles should be stripped
77       'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" alt="" />',
78       '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" />',
79       '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;" />',
80     }
81     to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
82   end
83   
84   def test_inline_images_inside_tags
85     raw = <<-RAW
86 h1. !foo.png! Heading
87
88 Centered image:
89
90 p=. !bar.gif!
91 RAW
92
93     assert textilizable(raw).include?('<img src="foo.png" alt="" />')
94     assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
95   end
96   
97   def test_acronyms
98     to_test = {
99       'this is an acronym: GPL(General Public License)' => 'this is an acronym: <acronym title="General Public License">GPL</acronym>',
100       'GPL(This is a double-quoted "title")' => '<acronym title="This is a double-quoted &quot;title&quot;">GPL</acronym>',
101     }
102     to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
103     
104   end
105   
106   def test_attached_images
107     to_test = {
108       'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
109       'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
110       'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
111       'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
112       # link image
113       '!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>',
114     }
115     attachments = Attachment.find(:all)
116     to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
117   end
118   
119   def test_textile_external_links
120     to_test = {
121       'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
122       'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
123       '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
124       '"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>',
125       "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
126       # no multiline link text
127       "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",
128       # mailto link
129       "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
130       # two exclamation marks
131       '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
132     }
133     to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
134   end
135   
136   def test_redmine_links
137     issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3}, 
138                                :class => 'issue status-1 priority-1 overdue', :title => 'Error 281 when updating a recipe (New)')
139     
140     changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
141                                    :class => 'changeset', :title => 'My very first commit')
142     changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
143                                     :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
144     
145     document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
146                                              :class => 'document')
147     
148     version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
149                                   :class => 'version')
150
151     message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
152     
153     source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}
154     source_url_with_ext = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file.ext']}
155     
156     to_test = {
157       # tickets
158       '#3, #3 and #3.'              => "#{issue_link}, #{issue_link} and #{issue_link}.",
159       # changesets
160       'r1'                          => changeset_link,
161       'r1.'                         => "#{changeset_link}.",
162       'r1, r2'                      => "#{changeset_link}, #{changeset_link2}",
163       'r1,r2'                       => "#{changeset_link},#{changeset_link2}",
164       # documents
165       'document#1'                  => document_link,
166       'document:"Test document"'    => document_link,
167       # versions
168       'version#2'                   => version_link,
169       'version:1.0'                 => version_link,
170       'version:"1.0"'               => version_link,
171       # source
172       'source:/some/file'           => link_to('source:/some/file', source_url, :class => 'source'),
173       'source:/some/file.'          => link_to('source:/some/file', source_url, :class => 'source') + ".",
174       'source:/some/file.ext.'      => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
175       'source:/some/file. '         => link_to('source:/some/file', source_url, :class => 'source') + ".",
176       'source:/some/file.ext. '     => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
177       'source:/some/file, '         => link_to('source:/some/file', source_url, :class => 'source') + ",",
178       'source:/some/file@52'        => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'),
179       'source:/some/file.ext@52'    => link_to('source:/some/file.ext@52', source_url_with_ext.merge(:rev => 52), :class => 'source'),
180       'source:/some/file#L110'      => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'),
181       'source:/some/file.ext#L110'  => link_to('source:/some/file.ext#L110', source_url_with_ext.merge(:anchor => 'L110'), :class => 'source'),
182       'source:/some/file@52#L110'   => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'),
183       'export:/some/file'           => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'),
184       # message
185       'message#4'                   => link_to('Post 2', message_url, :class => 'message'),
186       'message#5'                   => link_to('RE: post 2', message_url.merge(:anchor => 'message-5'), :class => 'message'),
187       # escaping
188       '!#3.'                        => '#3.',
189       '!r1'                         => 'r1',
190       '!document#1'                 => 'document#1',
191       '!document:"Test document"'   => 'document:"Test document"',
192       '!version#2'                  => 'version#2',
193       '!version:1.0'                => 'version:1.0',
194       '!version:"1.0"'              => 'version:"1.0"',
195       '!source:/some/file'          => 'source:/some/file',
196       # not found
197       '#0123456789'                 => '#0123456789',
198       # invalid expressions
199       'source:'                     => 'source:',
200       # url hash
201       "http://foo.bar/FAQ#3"       => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
202     }
203     @project = Project.find(1)
204     to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
205   end
206   
207   def test_wiki_links
208     to_test = {
209       '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
210       '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
211       # link with anchor
212       '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
213       '[[Another page#anchor|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page#anchor" class="wiki-page">Page</a>',
214       # page that doesn't exist
215       '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
216       '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
217       # link to another project wiki
218       '[[onlinestore:]]' => '<a href="/projects/onlinestore/wiki/" class="wiki-page">onlinestore</a>',
219       '[[onlinestore:|Wiki]]' => '<a href="/projects/onlinestore/wiki/" class="wiki-page">Wiki</a>',
220       '[[onlinestore:Start page]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Start page</a>',
221       '[[onlinestore:Start page|Text]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Text</a>',
222       '[[onlinestore:Unknown page]]' => '<a href="/projects/onlinestore/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
223       # striked through link
224       '-[[Another page|Page]]-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a></del>',
225       '-[[Another page|Page]] link-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a> link</del>',
226       # escaping
227       '![[Another page|Page]]' => '[[Another page|Page]]',
228       # project does not exist
229       '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
230       '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
231     }
232     @project = Project.find(1)
233     to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
234   end
235   
236   def test_html_tags
237     to_test = {
238       "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
239       "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
240       "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
241       # do not escape pre/code tags
242       "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
243       "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
244       "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
245       "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
246       "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
247       # remove attributes except class
248       "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
249       "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
250     }
251     to_test.each { |text, result| assert_equal result, textilizable(text) }
252   end
253   
254   def test_allowed_html_tags
255     to_test = {
256       "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
257       "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
258       "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
259     }
260     to_test.each { |text, result| assert_equal result, textilizable(text) }
261   end
262   
263   def test_pre_tags
264     raw = <<-RAW
265 Before
266
267 <pre>
268 <prepared-statement-cache-size>32</prepared-statement-cache-size>
269 </pre>
270
271 After
272 RAW
273
274     expected = <<-EXPECTED
275 <p>Before</p>
276 <pre>
277 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
278 </pre>
279 <p>After</p>
280 EXPECTED
281     
282     assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
283   end
284   
285   def test_syntax_highlight
286     raw = <<-RAW
287 <pre><code class="ruby">
288 # Some ruby code here
289 </pre></code>
290 RAW
291
292     expected = <<-EXPECTED
293 <pre><code class="ruby CodeRay"><span class="no">1</span> <span class="c"># Some ruby code here</span>
294 </pre></code>
295 EXPECTED
296
297     assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
298   end
299   
300   def test_wiki_links_in_tables
301     to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
302                  '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
303                  '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
304                  '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
305     }
306     @project = Project.find(1)
307     to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
308   end
309   
310   def test_text_formatting
311     to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
312                '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
313                'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
314                '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>',
315                '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',
316               }
317     to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
318   end
319   
320   def test_wiki_horizontal_rule
321     assert_equal '<hr />', textilizable('---')
322     assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
323   end
324   
325   def test_acronym
326     assert_equal '<p>This is an acronym: <acronym title="American Civil Liberties Union">ACLU</acronym>.</p>',
327                  textilizable('This is an acronym: ACLU(American Civil Liberties Union).')
328   end
329   
330   def test_footnotes
331     raw = <<-RAW
332 This is some text[1].
333
334 fn1. This is the foot note
335 RAW
336
337     expected = <<-EXPECTED
338 <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
339 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
340 EXPECTED
341
342     assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
343   end
344   
345   def test_table_of_content
346     raw = <<-RAW
347 {{toc}}
348
349 h1. Title
350
351 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
352
353 h2. Subtitle with a [[Wiki]] link
354
355 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
356
357 h2. Subtitle with [[Wiki|another Wiki]] link
358
359 h2. Subtitle with %{color:red}red text%
360
361 h1. Another title
362
363 RAW
364
365     expected = '<ul class="toc">' +
366                '<li class="heading1"><a href="#Title">Title</a></li>' +
367                '<li class="heading2"><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' + 
368                '<li class="heading2"><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' + 
369                '<li class="heading2"><a href="#Subtitle-with-red-text">Subtitle with red text</a></li>' +
370                '<li class="heading1"><a href="#Another-title">Another title</a></li>' +
371                '</ul>'
372                
373     assert textilizable(raw).gsub("\n", "").include?(expected)
374   end
375   
376   def test_blockquote
377     # orig raw text
378     raw = <<-RAW
379 John said:
380 > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
381 > Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
382 > * Donec odio lorem,
383 > * sagittis ac,
384 > * malesuada in,
385 > * adipiscing eu, dolor.
386 >
387 > >Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.
388 > Proin a tellus. Nam vel neque.
389
390 He's right.
391 RAW
392     
393     # expected html
394     expected = <<-EXPECTED
395 <p>John said:</p>
396 <blockquote>
397 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
398 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
399 <ul>
400   <li>Donec odio lorem,</li>
401   <li>sagittis ac,</li>
402   <li>malesuada in,</li>
403   <li>adipiscing eu, dolor.</li>
404 </ul>
405 <blockquote>
406 <p>Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.</p>
407 </blockquote>
408 <p>Proin a tellus. Nam vel neque.</p>
409 </blockquote>
410 <p>He's right.</p>
411 EXPECTED
412     
413     assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
414   end
415   
416   def test_table
417     raw = <<-RAW
418 This is a table with empty cells:
419
420 |cell11|cell12||
421 |cell21||cell23|
422 |cell31|cell32|cell33|
423 RAW
424
425     expected = <<-EXPECTED
426 <p>This is a table with empty cells:</p>
427
428 <table>
429   <tr><td>cell11</td><td>cell12</td><td></td></tr>
430   <tr><td>cell21</td><td></td><td>cell23</td></tr>
431   <tr><td>cell31</td><td>cell32</td><td>cell33</td></tr>
432 </table>
433 EXPECTED
434
435     assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
436   end
437   
438   def test_table_with_line_breaks
439     raw = <<-RAW
440 This is a table with line breaks:
441
442 |cell11
443 continued|cell12||
444 |-cell21-||cell23
445 cell23 line2
446 cell23 *line3*|
447 |cell31|cell32
448 cell32 line2|cell33|
449
450 RAW
451
452     expected = <<-EXPECTED
453 <p>This is a table with line breaks:</p>
454
455 <table>
456   <tr>
457     <td>cell11<br />continued</td>
458     <td>cell12</td>
459     <td></td>
460   </tr>
461   <tr>
462     <td><del>cell21</del></td>
463     <td></td>
464     <td>cell23<br/>cell23 line2<br/>cell23 <strong>line3</strong></td>
465   </tr>
466   <tr>
467     <td>cell31</td>
468     <td>cell32<br/>cell32 line2</td>
469     <td>cell33</td>
470   </tr>
471 </table>
472 EXPECTED
473
474     assert_equal expected.gsub(%r{\s+}, ''), textilizable(raw).gsub(%r{\s+}, '')
475   end
476   
477   def test_textile_should_not_mangle_brackets
478     assert_equal '<p>[msg1][msg2]</p>', textilizable('[msg1][msg2]')
479   end
480   
481   def test_default_formatter
482     Setting.text_formatting = 'unknown'
483     text = 'a *link*: http://www.example.net/'
484     assert_equal '<p>a *link*: <a href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
485     Setting.text_formatting = 'textile'
486   end
487   
488   def test_due_date_distance_in_words
489     to_test = { Date.today => 'Due in 0 days',
490                 Date.today + 1 => 'Due in 1 day',
491                 Date.today + 100 => 'Due in about 3 months',
492                 Date.today + 20000 => 'Due in over 54 years',
493                 Date.today - 1 => '1 day late',
494                 Date.today - 100 => 'about 3 months late',
495                 Date.today - 20000 => 'over 54 years late',
496                }
497     to_test.each do |date, expected|
498       assert_equal expected, due_date_distance_in_words(date)
499     end
500   end
501   
502   def test_avatar
503     # turn on avatars
504     Setting.gravatar_enabled = '1'
505     assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
506     assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
507     assert_nil avatar('jsmith')
508     assert_nil avatar(nil)
509     
510     # turn off avatars
511     Setting.gravatar_enabled = '0'
512     assert_nil avatar(User.find_by_mail('jsmith@somenet.foo'))
513   end
514   
515   def test_link_to_user
516     user = User.find(2)
517     t = link_to_user(user)
518     assert_equal "<a href=\"/users/2\">#{ user.name }</a>", t
519   end
520                                       
521   def test_link_to_user_should_not_link_to_locked_user
522     user = User.find(5)
523     assert user.locked?
524     t = link_to_user(user)
525     assert_equal user.name, t
526   end
527                                                                           
528   def test_link_to_user_should_not_link_to_anonymous
529     user = User.anonymous
530     assert user.anonymous?
531     t = link_to_user(user)
532     assert_equal ::I18n.t(:label_user_anonymous), t
533   end
534 end