PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/helpers/application_helper_test.rb

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