PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/helpers/application_helper_test.rb

https://bitbucket.org/eimajenthat/redmine
Ruby | 1201 lines | 944 code | 179 blank | 78 comment | 3 complexity | 30bfa689bfa4804e2b0894d133bafce7 MD5 | raw file
Possible License(s): GPL-2.0
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2013 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require File.expand_path('../../../test_helper', __FILE__)
  20. class ApplicationHelperTest < ActionView::TestCase
  21. include ERB::Util
  22. include Rails.application.routes.url_helpers
  23. fixtures :projects, :roles, :enabled_modules, :users,
  24. :repositories, :changesets,
  25. :trackers, :issue_statuses, :issues, :versions, :documents,
  26. :wikis, :wiki_pages, :wiki_contents,
  27. :boards, :messages, :news,
  28. :attachments, :enumerations
  29. def setup
  30. super
  31. set_tmp_attachments_directory
  32. end
  33. context "#link_to_if_authorized" do
  34. context "authorized user" do
  35. should "be tested"
  36. end
  37. context "unauthorized user" do
  38. should "be tested"
  39. end
  40. should "allow using the :controller and :action for the target link" do
  41. User.current = User.find_by_login('admin')
  42. @project = Issue.first.project # Used by helper
  43. response = link_to_if_authorized("By controller/action",
  44. {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
  45. assert_match /href/, response
  46. end
  47. end
  48. def test_auto_links
  49. to_test = {
  50. 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
  51. 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
  52. 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
  53. 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
  54. 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
  55. 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
  56. '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>.',
  57. 'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
  58. '(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>)',
  59. '(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>)',
  60. '(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>).',
  61. '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
  62. '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
  63. '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
  64. 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
  65. '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>',
  66. 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
  67. 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
  68. 'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
  69. 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
  70. 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
  71. 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
  72. # two exclamation marks
  73. '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>',
  74. # escaping
  75. 'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo&quot;bar</a>',
  76. # wrap in angle brackets
  77. '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;'
  78. }
  79. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  80. end
  81. if 'ruby'.respond_to?(:encoding)
  82. def test_auto_links_with_non_ascii_characters
  83. to_test = {
  84. 'http://foo.bar/тест' => '<a class="external" href="http://foo.bar/тест">http://foo.bar/тест</a>'
  85. }
  86. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  87. end
  88. else
  89. puts 'Skipping test_auto_links_with_non_ascii_characters, unsupported ruby version'
  90. end
  91. def test_auto_mailto
  92. assert_equal '<p><a class="email" href="mailto:test@foo.bar">test@foo.bar</a></p>',
  93. textilizable('test@foo.bar')
  94. end
  95. def test_inline_images
  96. to_test = {
  97. '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
  98. 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
  99. 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
  100. 'with style !{width:100px;height:100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" style="width:100px;height:100px;" alt="" />',
  101. '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" />',
  102. '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;" />',
  103. }
  104. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  105. end
  106. def test_inline_images_inside_tags
  107. raw = <<-RAW
  108. h1. !foo.png! Heading
  109. Centered image:
  110. p=. !bar.gif!
  111. RAW
  112. assert textilizable(raw).include?('<img src="foo.png" alt="" />')
  113. assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
  114. end
  115. def test_attached_images
  116. to_test = {
  117. 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
  118. 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
  119. 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
  120. 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
  121. # link image
  122. '!logo.gif!:http://foo.bar/' => '<a href="http://foo.bar/"><img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" /></a>',
  123. }
  124. attachments = Attachment.all
  125. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
  126. end
  127. def test_attached_images_filename_extension
  128. set_tmp_attachments_directory
  129. a1 = Attachment.new(
  130. :container => Issue.find(1),
  131. :file => mock_file_with_options({:original_filename => "testtest.JPG"}),
  132. :author => User.find(1))
  133. assert a1.save
  134. assert_equal "testtest.JPG", a1.filename
  135. assert_equal "image/jpeg", a1.content_type
  136. assert a1.image?
  137. a2 = Attachment.new(
  138. :container => Issue.find(1),
  139. :file => mock_file_with_options({:original_filename => "testtest.jpeg"}),
  140. :author => User.find(1))
  141. assert a2.save
  142. assert_equal "testtest.jpeg", a2.filename
  143. assert_equal "image/jpeg", a2.content_type
  144. assert a2.image?
  145. a3 = Attachment.new(
  146. :container => Issue.find(1),
  147. :file => mock_file_with_options({:original_filename => "testtest.JPE"}),
  148. :author => User.find(1))
  149. assert a3.save
  150. assert_equal "testtest.JPE", a3.filename
  151. assert_equal "image/jpeg", a3.content_type
  152. assert a3.image?
  153. a4 = Attachment.new(
  154. :container => Issue.find(1),
  155. :file => mock_file_with_options({:original_filename => "Testtest.BMP"}),
  156. :author => User.find(1))
  157. assert a4.save
  158. assert_equal "Testtest.BMP", a4.filename
  159. assert_equal "image/x-ms-bmp", a4.content_type
  160. assert a4.image?
  161. to_test = {
  162. 'Inline image: !testtest.jpg!' =>
  163. 'Inline image: <img src="/attachments/download/' + a1.id.to_s + '/testtest.JPG" alt="" />',
  164. 'Inline image: !testtest.jpeg!' =>
  165. 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testtest.jpeg" alt="" />',
  166. 'Inline image: !testtest.jpe!' =>
  167. 'Inline image: <img src="/attachments/download/' + a3.id.to_s + '/testtest.JPE" alt="" />',
  168. 'Inline image: !testtest.bmp!' =>
  169. 'Inline image: <img src="/attachments/download/' + a4.id.to_s + '/Testtest.BMP" alt="" />',
  170. }
  171. attachments = [a1, a2, a3, a4]
  172. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
  173. end
  174. def test_attached_images_should_read_later
  175. set_fixtures_attachments_directory
  176. a1 = Attachment.find(16)
  177. assert_equal "testfile.png", a1.filename
  178. assert a1.readable?
  179. assert (! a1.visible?(User.anonymous))
  180. assert a1.visible?(User.find(2))
  181. a2 = Attachment.find(17)
  182. assert_equal "testfile.PNG", a2.filename
  183. assert a2.readable?
  184. assert (! a2.visible?(User.anonymous))
  185. assert a2.visible?(User.find(2))
  186. assert a1.created_on < a2.created_on
  187. to_test = {
  188. 'Inline image: !testfile.png!' =>
  189. 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
  190. 'Inline image: !Testfile.PNG!' =>
  191. 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
  192. }
  193. attachments = [a1, a2]
  194. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
  195. set_tmp_attachments_directory
  196. end
  197. def test_textile_external_links
  198. to_test = {
  199. 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
  200. 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
  201. '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
  202. '"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>',
  203. "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
  204. # no multiline link text
  205. "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",
  206. # mailto link
  207. "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
  208. # two exclamation marks
  209. '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
  210. # escaping
  211. '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
  212. }
  213. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  214. end
  215. if 'ruby'.respond_to?(:encoding)
  216. def test_textile_external_links_with_non_ascii_characters
  217. to_test = {
  218. 'This is a "link":http://foo.bar/тест' => 'This is a <a href="http://foo.bar/тест" class="external">link</a>'
  219. }
  220. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  221. end
  222. else
  223. puts 'Skipping test_textile_external_links_with_non_ascii_characters, unsupported ruby version'
  224. end
  225. def test_redmine_links
  226. issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
  227. :class => 'issue status-1 priority-4 priority-lowest overdue', :title => 'Error 281 when updating a recipe (New)')
  228. note_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3, :anchor => 'note-14'},
  229. :class => 'issue status-1 priority-4 priority-lowest overdue', :title => 'Error 281 when updating a recipe (New)')
  230. changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
  231. :class => 'changeset', :title => 'My very first commit')
  232. changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
  233. :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
  234. document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
  235. :class => 'document')
  236. version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
  237. :class => 'version')
  238. board_url = {:controller => 'boards', :action => 'show', :id => 2, :project_id => 'ecookbook'}
  239. message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
  240. news_url = {:controller => 'news', :action => 'show', :id => 1}
  241. project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
  242. source_url = '/projects/ecookbook/repository/entry/some/file'
  243. source_url_with_rev = '/projects/ecookbook/repository/revisions/52/entry/some/file'
  244. source_url_with_ext = '/projects/ecookbook/repository/entry/some/file.ext'
  245. source_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/entry/some/file.ext'
  246. source_url_with_branch = '/projects/ecookbook/repository/revisions/branch/entry/some/file'
  247. export_url = '/projects/ecookbook/repository/raw/some/file'
  248. export_url_with_rev = '/projects/ecookbook/repository/revisions/52/raw/some/file'
  249. export_url_with_ext = '/projects/ecookbook/repository/raw/some/file.ext'
  250. export_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/raw/some/file.ext'
  251. export_url_with_branch = '/projects/ecookbook/repository/revisions/branch/raw/some/file'
  252. to_test = {
  253. # tickets
  254. '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
  255. # ticket notes
  256. '#3-14' => note_link,
  257. '#3#note-14' => note_link,
  258. # should not ignore leading zero
  259. '#03' => '#03',
  260. # changesets
  261. 'r1' => changeset_link,
  262. 'r1.' => "#{changeset_link}.",
  263. 'r1, r2' => "#{changeset_link}, #{changeset_link2}",
  264. 'r1,r2' => "#{changeset_link},#{changeset_link2}",
  265. # documents
  266. 'document#1' => document_link,
  267. 'document:"Test document"' => document_link,
  268. # versions
  269. 'version#2' => version_link,
  270. 'version:1.0' => version_link,
  271. 'version:"1.0"' => version_link,
  272. # source
  273. 'source:some/file' => link_to('source:some/file', source_url, :class => 'source'),
  274. 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
  275. 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
  276. 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
  277. 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
  278. 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
  279. 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
  280. 'source:/some/file@52' => link_to('source:/some/file@52', source_url_with_rev, :class => 'source'),
  281. 'source:/some/file@branch' => link_to('source:/some/file@branch', source_url_with_branch, :class => 'source'),
  282. 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_rev_and_ext, :class => 'source'),
  283. 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url + "#L110", :class => 'source'),
  284. 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext + "#L110", :class => 'source'),
  285. 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url_with_rev + "#L110", :class => 'source'),
  286. # export
  287. 'export:/some/file' => link_to('export:/some/file', export_url, :class => 'source download'),
  288. 'export:/some/file.ext' => link_to('export:/some/file.ext', export_url_with_ext, :class => 'source download'),
  289. 'export:/some/file@52' => link_to('export:/some/file@52', export_url_with_rev, :class => 'source download'),
  290. 'export:/some/file.ext@52' => link_to('export:/some/file.ext@52', export_url_with_rev_and_ext, :class => 'source download'),
  291. 'export:/some/file@branch' => link_to('export:/some/file@branch', export_url_with_branch, :class => 'source download'),
  292. # forum
  293. 'forum#2' => link_to('Discussion', board_url, :class => 'board'),
  294. 'forum:Discussion' => link_to('Discussion', board_url, :class => 'board'),
  295. # message
  296. 'message#4' => link_to('Post 2', message_url, :class => 'message'),
  297. 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5', :r => 5), :class => 'message'),
  298. # news
  299. 'news#1' => link_to('eCookbook first release !', news_url, :class => 'news'),
  300. 'news:"eCookbook first release !"' => link_to('eCookbook first release !', news_url, :class => 'news'),
  301. # project
  302. 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
  303. 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
  304. 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
  305. # not found
  306. '#0123456789' => '#0123456789',
  307. # invalid expressions
  308. 'source:' => 'source:',
  309. # url hash
  310. "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
  311. }
  312. @project = Project.find(1)
  313. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
  314. end
  315. def test_redmine_links_with_a_different_project_before_current_project
  316. vp1 = Version.generate!(:project_id => 1, :name => '1.4.4')
  317. vp3 = Version.generate!(:project_id => 3, :name => '1.4.4')
  318. @project = Project.find(3)
  319. assert_equal %(<p><a href="/versions/#{vp1.id}" class="version">1.4.4</a> <a href="/versions/#{vp3.id}" class="version">1.4.4</a></p>),
  320. textilizable("ecookbook:version:1.4.4 version:1.4.4")
  321. end
  322. def test_escaped_redmine_links_should_not_be_parsed
  323. to_test = [
  324. '#3.',
  325. '#3-14.',
  326. '#3#-note14.',
  327. 'r1',
  328. 'document#1',
  329. 'document:"Test document"',
  330. 'version#2',
  331. 'version:1.0',
  332. 'version:"1.0"',
  333. 'source:/some/file'
  334. ]
  335. @project = Project.find(1)
  336. to_test.each { |text| assert_equal "<p>#{text}</p>", textilizable("!" + text), "#{text} failed" }
  337. end
  338. def test_cross_project_redmine_links
  339. source_link = link_to('ecookbook:source:/some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']},
  340. :class => 'source')
  341. changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
  342. :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
  343. to_test = {
  344. # documents
  345. 'document:"Test document"' => 'document:"Test document"',
  346. 'ecookbook:document:"Test document"' => '<a href="/documents/1" class="document">Test document</a>',
  347. 'invalid:document:"Test document"' => 'invalid:document:"Test document"',
  348. # versions
  349. 'version:"1.0"' => 'version:"1.0"',
  350. 'ecookbook:version:"1.0"' => '<a href="/versions/2" class="version">1.0</a>',
  351. 'invalid:version:"1.0"' => 'invalid:version:"1.0"',
  352. # changeset
  353. 'r2' => 'r2',
  354. 'ecookbook:r2' => changeset_link,
  355. 'invalid:r2' => 'invalid:r2',
  356. # source
  357. 'source:/some/file' => 'source:/some/file',
  358. 'ecookbook:source:/some/file' => source_link,
  359. 'invalid:source:/some/file' => 'invalid:source:/some/file',
  360. }
  361. @project = Project.find(3)
  362. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
  363. end
  364. def test_multiple_repositories_redmine_links
  365. svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///foo/hg')
  366. Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
  367. hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
  368. Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
  369. changeset_link = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
  370. :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
  371. svn_changeset_link = link_to('svn1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn1', :rev => 123},
  372. :class => 'changeset', :title => '')
  373. hg_changeset_link = link_to('hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
  374. :class => 'changeset', :title => '')
  375. source_link = link_to('source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
  376. hg_source_link = link_to('source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
  377. to_test = {
  378. 'r2' => changeset_link,
  379. 'svn1|r123' => svn_changeset_link,
  380. 'invalid|r123' => 'invalid|r123',
  381. 'commit:hg1|abcd' => hg_changeset_link,
  382. 'commit:invalid|abcd' => 'commit:invalid|abcd',
  383. # source
  384. 'source:some/file' => source_link,
  385. 'source:hg1|some/file' => hg_source_link,
  386. 'source:invalid|some/file' => 'source:invalid|some/file',
  387. }
  388. @project = Project.find(1)
  389. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
  390. end
  391. def test_cross_project_multiple_repositories_redmine_links
  392. svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///foo/hg')
  393. Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
  394. hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
  395. Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
  396. changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
  397. :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
  398. svn_changeset_link = link_to('ecookbook:svn1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn1', :rev => 123},
  399. :class => 'changeset', :title => '')
  400. hg_changeset_link = link_to('ecookbook:hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
  401. :class => 'changeset', :title => '')
  402. source_link = link_to('ecookbook:source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
  403. hg_source_link = link_to('ecookbook:source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
  404. to_test = {
  405. 'ecookbook:r2' => changeset_link,
  406. 'ecookbook:svn1|r123' => svn_changeset_link,
  407. 'ecookbook:invalid|r123' => 'ecookbook:invalid|r123',
  408. 'ecookbook:commit:hg1|abcd' => hg_changeset_link,
  409. 'ecookbook:commit:invalid|abcd' => 'ecookbook:commit:invalid|abcd',
  410. 'invalid:commit:invalid|abcd' => 'invalid:commit:invalid|abcd',
  411. # source
  412. 'ecookbook:source:some/file' => source_link,
  413. 'ecookbook:source:hg1|some/file' => hg_source_link,
  414. 'ecookbook:source:invalid|some/file' => 'ecookbook:source:invalid|some/file',
  415. 'invalid:source:invalid|some/file' => 'invalid:source:invalid|some/file',
  416. }
  417. @project = Project.find(3)
  418. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
  419. end
  420. def test_redmine_links_git_commit
  421. changeset_link = link_to('abcd',
  422. {
  423. :controller => 'repositories',
  424. :action => 'revision',
  425. :id => 'subproject1',
  426. :rev => 'abcd',
  427. },
  428. :class => 'changeset', :title => 'test commit')
  429. to_test = {
  430. 'commit:abcd' => changeset_link,
  431. }
  432. @project = Project.find(3)
  433. r = Repository::Git.create!(:project => @project, :url => '/tmp/test/git')
  434. assert r
  435. c = Changeset.new(:repository => r,
  436. :committed_on => Time.now,
  437. :revision => 'abcd',
  438. :scmid => 'abcd',
  439. :comments => 'test commit')
  440. assert( c.save )
  441. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  442. end
  443. # TODO: Bazaar commit id contains mail address, so it contains '@' and '_'.
  444. def test_redmine_links_darcs_commit
  445. changeset_link = link_to('20080308225258-98289-abcd456efg.gz',
  446. {
  447. :controller => 'repositories',
  448. :action => 'revision',
  449. :id => 'subproject1',
  450. :rev => '123',
  451. },
  452. :class => 'changeset', :title => 'test commit')
  453. to_test = {
  454. 'commit:20080308225258-98289-abcd456efg.gz' => changeset_link,
  455. }
  456. @project = Project.find(3)
  457. r = Repository::Darcs.create!(
  458. :project => @project, :url => '/tmp/test/darcs',
  459. :log_encoding => 'UTF-8')
  460. assert r
  461. c = Changeset.new(:repository => r,
  462. :committed_on => Time.now,
  463. :revision => '123',
  464. :scmid => '20080308225258-98289-abcd456efg.gz',
  465. :comments => 'test commit')
  466. assert( c.save )
  467. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  468. end
  469. def test_redmine_links_mercurial_commit
  470. changeset_link_rev = link_to('r123',
  471. {
  472. :controller => 'repositories',
  473. :action => 'revision',
  474. :id => 'subproject1',
  475. :rev => '123' ,
  476. },
  477. :class => 'changeset', :title => 'test commit')
  478. changeset_link_commit = link_to('abcd',
  479. {
  480. :controller => 'repositories',
  481. :action => 'revision',
  482. :id => 'subproject1',
  483. :rev => 'abcd' ,
  484. },
  485. :class => 'changeset', :title => 'test commit')
  486. to_test = {
  487. 'r123' => changeset_link_rev,
  488. 'commit:abcd' => changeset_link_commit,
  489. }
  490. @project = Project.find(3)
  491. r = Repository::Mercurial.create!(:project => @project, :url => '/tmp/test')
  492. assert r
  493. c = Changeset.new(:repository => r,
  494. :committed_on => Time.now,
  495. :revision => '123',
  496. :scmid => 'abcd',
  497. :comments => 'test commit')
  498. assert( c.save )
  499. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  500. end
  501. def test_attachment_links
  502. to_test = {
  503. 'attachment:error281.txt' => '<a href="/attachments/download/1/error281.txt" class="attachment">error281.txt</a>'
  504. }
  505. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" }
  506. end
  507. def test_attachment_link_should_link_to_latest_attachment
  508. set_tmp_attachments_directory
  509. a1 = Attachment.generate!(:filename => "test.txt", :created_on => 1.hour.ago)
  510. a2 = Attachment.generate!(:filename => "test.txt")
  511. assert_equal %(<p><a href="/attachments/download/#{a2.id}/test.txt" class="attachment">test.txt</a></p>),
  512. textilizable('attachment:test.txt', :attachments => [a1, a2])
  513. end
  514. def test_wiki_links
  515. to_test = {
  516. '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
  517. '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
  518. # title content should be formatted
  519. '[[Another page|With _styled_ *title*]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">With <em>styled</em> <strong>title</strong></a>',
  520. '[[Another page|With title containing <strong>HTML entities &amp; markups</strong>]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">With title containing &lt;strong&gt;HTML entities &amp; markups&lt;/strong&gt;</a>',
  521. # link with anchor
  522. '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
  523. '[[Another page#anchor|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page#anchor" class="wiki-page">Page</a>',
  524. # UTF8 anchor
  525. '[[Another_page#Тест|Тест]]' => %|<a href="/projects/ecookbook/wiki/Another_page##{CGI.escape 'Тест'}" class="wiki-page">Тест</a>|,
  526. # page that doesn't exist
  527. '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
  528. '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
  529. # link to another project wiki
  530. '[[onlinestore:]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">onlinestore</a>',
  531. '[[onlinestore:|Wiki]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">Wiki</a>',
  532. '[[onlinestore:Start page]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Start page</a>',
  533. '[[onlinestore:Start page|Text]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Text</a>',
  534. '[[onlinestore:Unknown page]]' => '<a href="/projects/onlinestore/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
  535. # striked through link
  536. '-[[Another page|Page]]-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a></del>',
  537. '-[[Another page|Page]] link-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a> link</del>',
  538. # escaping
  539. '![[Another page|Page]]' => '[[Another page|Page]]',
  540. # project does not exist
  541. '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
  542. '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
  543. }
  544. @project = Project.find(1)
  545. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  546. end
  547. def test_wiki_links_within_local_file_generation_context
  548. to_test = {
  549. # link to a page
  550. '[[CookBook documentation]]' => '<a href="CookBook_documentation.html" class="wiki-page">CookBook documentation</a>',
  551. '[[CookBook documentation|documentation]]' => '<a href="CookBook_documentation.html" class="wiki-page">documentation</a>',
  552. '[[CookBook documentation#One-section]]' => '<a href="CookBook_documentation.html#One-section" class="wiki-page">CookBook documentation</a>',
  553. '[[CookBook documentation#One-section|documentation]]' => '<a href="CookBook_documentation.html#One-section" class="wiki-page">documentation</a>',
  554. # page that doesn't exist
  555. '[[Unknown page]]' => '<a href="Unknown_page.html" class="wiki-page new">Unknown page</a>',
  556. '[[Unknown page|404]]' => '<a href="Unknown_page.html" class="wiki-page new">404</a>',
  557. '[[Unknown page#anchor]]' => '<a href="Unknown_page.html#anchor" class="wiki-page new">Unknown page</a>',
  558. '[[Unknown page#anchor|404]]' => '<a href="Unknown_page.html#anchor" class="wiki-page new">404</a>',
  559. }
  560. @project = Project.find(1)
  561. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :local) }
  562. end
  563. def test_wiki_links_within_wiki_page_context
  564. page = WikiPage.find_by_title('Another_page' )
  565. to_test = {
  566. # link to another page
  567. '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
  568. '[[CookBook documentation|documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">documentation</a>',
  569. '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
  570. '[[CookBook documentation#One-section|documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">documentation</a>',
  571. # link to the current page
  572. '[[Another page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Another page</a>',
  573. '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
  574. '[[Another page#anchor]]' => '<a href="#anchor" class="wiki-page">Another page</a>',
  575. '[[Another page#anchor|Page]]' => '<a href="#anchor" class="wiki-page">Page</a>',
  576. # page that doesn't exist
  577. '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page" class="wiki-page new">Unknown page</a>',
  578. '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page" class="wiki-page new">404</a>',
  579. '[[Unknown page#anchor]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor" class="wiki-page new">Unknown page</a>',
  580. '[[Unknown page#anchor|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor" class="wiki-page new">404</a>',
  581. }
  582. @project = Project.find(1)
  583. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(WikiContent.new( :text => text, :page => page ), :text) }
  584. end
  585. def test_wiki_links_anchor_option_should_prepend_page_title_to_href
  586. to_test = {
  587. # link to a page
  588. '[[CookBook documentation]]' => '<a href="#CookBook_documentation" class="wiki-page">CookBook documentation</a>',
  589. '[[CookBook documentation|documentation]]' => '<a href="#CookBook_documentation" class="wiki-page">documentation</a>',
  590. '[[CookBook documentation#One-section]]' => '<a href="#CookBook_documentation_One-section" class="wiki-page">CookBook documentation</a>',
  591. '[[CookBook documentation#One-section|documentation]]' => '<a href="#CookBook_documentation_One-section" class="wiki-page">documentation</a>',
  592. # page that doesn't exist
  593. '[[Unknown page]]' => '<a href="#Unknown_page" class="wiki-page new">Unknown page</a>',
  594. '[[Unknown page|404]]' => '<a href="#Unknown_page" class="wiki-page new">404</a>',
  595. '[[Unknown page#anchor]]' => '<a href="#Unknown_page_anchor" class="wiki-page new">Unknown page</a>',
  596. '[[Unknown page#anchor|404]]' => '<a href="#Unknown_page_anchor" class="wiki-page new">404</a>',
  597. }
  598. @project = Project.find(1)
  599. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :anchor) }
  600. end
  601. def test_html_tags
  602. to_test = {
  603. "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
  604. "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
  605. "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
  606. # do not escape pre/code tags
  607. "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
  608. "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
  609. "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
  610. "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
  611. "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
  612. # remove attributes except class
  613. "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
  614. '<pre class="foo">some text</pre>' => '<pre class="foo">some text</pre>',
  615. "<pre class='foo bar'>some text</pre>" => "<pre class='foo bar'>some text</pre>",
  616. '<pre class="foo bar">some text</pre>' => '<pre class="foo bar">some text</pre>',
  617. "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
  618. # xss
  619. '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
  620. '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
  621. }
  622. to_test.each { |text, result| assert_equal result, textilizable(text) }
  623. end
  624. def test_allowed_html_tags
  625. to_test = {
  626. "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
  627. "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
  628. "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
  629. }
  630. to_test.each { |text, result| assert_equal result, textilizable(text) }
  631. end
  632. def test_pre_tags
  633. raw = <<-RAW
  634. Before
  635. <pre>
  636. <prepared-statement-cache-size>32</prepared-statement-cache-size>
  637. </pre>
  638. After
  639. RAW
  640. expected = <<-EXPECTED
  641. <p>Before</p>
  642. <pre>
  643. &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
  644. </pre>
  645. <p>After</p>
  646. EXPECTED
  647. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  648. end
  649. def test_pre_content_should_not_parse_wiki_and_redmine_links
  650. raw = <<-RAW
  651. [[CookBook documentation]]
  652. #1
  653. <pre>
  654. [[CookBook documentation]]
  655. #1
  656. </pre>
  657. RAW
  658. expected = <<-EXPECTED
  659. <p><a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a></p>
  660. <p><a href="/issues/1" class="issue status-1 priority-4 priority-lowest" title="Can&#x27;t print recipes (New)">#1</a></p>
  661. <pre>
  662. [[CookBook documentation]]
  663. #1
  664. </pre>
  665. EXPECTED
  666. @project = Project.find(1)
  667. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  668. end
  669. def test_non_closing_pre_blocks_should_be_closed
  670. raw = <<-RAW
  671. <pre><code>
  672. RAW
  673. expected = <<-EXPECTED
  674. <pre><code>
  675. </code></pre>
  676. EXPECTED
  677. @project = Project.find(1)
  678. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  679. end
  680. def test_syntax_highlight
  681. raw = <<-RAW
  682. <pre><code class="ruby">
  683. # Some ruby code here
  684. </code></pre>
  685. RAW
  686. expected = <<-EXPECTED
  687. <pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="comment"># Some ruby code here</span></span>
  688. </code></pre>
  689. EXPECTED
  690. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  691. end
  692. def test_to_path_param
  693. assert_equal 'test1/test2', to_path_param('test1/test2')
  694. assert_equal 'test1/test2', to_path_param('/test1/test2/')
  695. assert_equal 'test1/test2', to_path_param('//test1/test2/')
  696. assert_equal nil, to_path_param('/')
  697. end
  698. def test_wiki_links_in_tables
  699. to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
  700. '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
  701. '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
  702. '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
  703. }
  704. @project = Project.find(1)
  705. to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
  706. end
  707. def test_text_formatting
  708. to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
  709. '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
  710. 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
  711. '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>',
  712. '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',
  713. }
  714. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  715. end
  716. def test_wiki_horizontal_rule
  717. assert_equal '<hr />', textilizable('---')
  718. assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
  719. end
  720. def test_footnotes
  721. raw = <<-RAW
  722. This is some text[1].
  723. fn1. This is the foot note
  724. RAW
  725. expected = <<-EXPECTED
  726. <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
  727. <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
  728. EXPECTED
  729. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  730. end
  731. def test_headings
  732. raw = 'h1. Some heading'
  733. expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
  734. assert_equal expected, textilizable(raw)
  735. end
  736. def test_headings_with_special_chars
  737. # This test makes sure that the generated anchor names match the expected
  738. # ones even if the heading text contains unconventional characters
  739. raw = 'h1. Some heading related to version 0.5'
  740. anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5")
  741. expected = %|<a name="#{anchor}"></a>\n<h1 >Some heading related to version 0.5<a href="##{anchor}" class="wiki-anchor">&para;</a></h1>|
  742. assert_equal expected, textilizable(raw)
  743. end
  744. def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title
  745. page = WikiPage.new( :title => 'Page Title', :wiki_id => 1 )
  746. content = WikiContent.new( :text => 'h1. Some heading', :page => page )
  747. expected = %|<a name="Page_Title_Some-heading"></a>\n<h1 >Some heading<a href="#Page_Title_Some-heading" class="wiki-anchor">&para;</a></h1>|
  748. assert_equal expected, textilizable(content, :text, :wiki_links => :anchor )
  749. end
  750. def test_table_of_content
  751. raw = <<-RAW
  752. {{toc}}
  753. h1. Title
  754. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
  755. h2. Subtitle with a [[Wiki]] link
  756. Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
  757. h2. Subtitle with [[Wiki|another Wiki]] link
  758. h2. Subtitle with %{color:red}red text%
  759. <pre>
  760. some code
  761. </pre>
  762. h3. Subtitle with *some* _modifiers_
  763. h3. Subtitle with @inline code@
  764. h1. Another title
  765. h3. An "Internet link":http://www.redmine.org/ inside subtitle
  766. h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
  767. RAW
  768. expected = '<ul class="toc">' +
  769. '<li><a href="#Title">Title</a>' +
  770. '<ul>' +
  771. '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
  772. '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
  773. '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
  774. '<ul>' +
  775. '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
  776. '<li><a href="#Subtitle-with-inline-code">Subtitle with inline code</a></li>' +
  777. '</ul>' +
  778. '</li>' +
  779. '</ul>' +
  780. '</li>' +
  781. '<li><a href="#Another-title">Another title</a>' +
  782. '<ul>' +
  783. '<li>' +
  784. '<ul>' +
  785. '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
  786. '</ul>' +
  787. '</li>' +
  788. '<li><a href="#Project-Name">Project Name</a></li>' +
  789. '</ul>' +
  790. '</li>' +
  791. '</ul>'
  792. @project = Project.find(1)
  793. assert textilizable(raw).gsub("\n", "").include?(expected)
  794. end
  795. def test_table_of_content_should_generate_unique_anchors
  796. raw = <<-RAW
  797. {{toc}}
  798. h1. Title
  799. h2. Subtitle
  800. h2. Subtitle
  801. RAW
  802. expected = '<ul class="toc">' +
  803. '<li><a href="#Title">Title</a>' +
  804. '<ul>' +
  805. '<li><a href="#Subtitle">Subtitle</a></li>' +
  806. '<li><a href="#Subtitle-2">Subtitle</a></li>'
  807. '</ul>'
  808. '</li>' +
  809. '</ul>'
  810. @project = Project.find(1)
  811. result = textilizable(raw).gsub("\n", "")
  812. assert_include expected, result
  813. assert_include '<a name="Subtitle">', result
  814. assert_include '<a name="Subtitle-2">', result
  815. end
  816. def test_table_of_content_should_contain_included_page_headings
  817. raw = <<-RAW
  818. {{toc}}
  819. h1. Included
  820. {{include(Child_1)}}
  821. RAW
  822. expected = '<ul class="toc">' +
  823. '<li><a href="#Included">Included</a></li>' +
  824. '<li><a href="#Child-page-1">Child page 1</a></li>' +
  825. '</ul>'
  826. @project = Project.find(1)
  827. assert textilizable(raw).gsub("\n", "").include?(expected)
  828. end
  829. def test_section_edit_links
  830. raw = <<-RAW
  831. h1. Title
  832. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
  833. h2. Subtitle with a [[Wiki]] link
  834. h2. Subtitle with *some* _modifiers_
  835. h2. Subtitle with @inline code@
  836. <pre>
  837. some code
  838. h2. heading inside pre
  839. <h2>html heading inside pre</h2>
  840. </pre>
  841. h2. Subtitle after pre tag
  842. RAW
  843. @project = Project.find(1)
  844. set_language_if_valid 'en'
  845. result = textilizable(raw, :edit_section_links => {:controller => 'wiki', :action => 'edit', :project_id => '1', :id => 'Test'}).gsub("\n", "")
  846. # heading that contains inline code
  847. assert_match Regexp.new('<div class="contextual" title="Edit this section">' +
  848. '<a href="/projects/1/wiki/Test/edit\?section=4"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
  849. '<a name="Subtitle-with-inline-code"></a>' +
  850. '<h2 >Subtitle with <code>inline code</code><a href="#Subtitle-with-inline-code" class="wiki-anchor">&para;</a></h2>'),
  851. result
  852. # last heading
  853. assert_match Regexp.new('<div class="contextual" title="Edit this section">' +
  854. '<a href="/projects/1/wiki/Test/edit\?section=5"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
  855. '<a name="Subtitle-after-pre-tag"></a>' +
  856. '<h2 >Subtitle after pre tag<a href="#Subtitle-after-pre-tag" class="wiki-anchor">&para;</a></h2>'),
  857. result
  858. end
  859. def test_default_formatter
  860. with_settings :text_formatting => 'unknown' do
  861. text = 'a *link*: http://www.example.net/'
  862. assert_equal '<p>a *link*: <a class="external" href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
  863. end
  864. end
  865. def test_due_date_distance_in_words
  866. to_test = { Date.today => 'Due in 0 days',
  867. Date.today + 1 => 'Due in 1 day',
  868. Date.today + 100 => 'Due in about 3 months',
  869. Date.today + 20000 => 'Due in over 54 years',
  870. Date.today - 1 => '1 day late',
  871. Date.today - 100 => 'about 3 months late',
  872. Date.today - 20000 => 'over 54 years late',
  873. }
  874. ::I18n.locale = :en
  875. to_test.each do |date, expected|
  876. assert_equal expected, due_date_distance_in_words(date)
  877. end
  878. end
  879. def test_avatar_enabled
  880. with_settings :gravatar_enabled => '1' do
  881. assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
  882. assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
  883. # Default size is 50
  884. assert avatar('jsmith <jsmith@somenet.foo>').include?('size=50')
  885. assert avatar('jsmith <jsmith@somenet.foo>', :size => 24).include?('size=24')
  886. # Non-avatar options should be considered html options
  887. assert avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith').include?('title="John Smith"')
  888. # The default class of the img tag should be gravatar
  889. assert avatar('jsmith <jsmith@somenet.foo>').include?('class="gravatar"')
  890. assert !avatar('jsmith <jsmith@somenet.foo>', :class => 'picture').include?('class="gravatar"')
  891. assert_nil avatar('jsmith')
  892. assert_nil avatar(nil)
  893. end
  894. end
  895. def test_avatar_disabled
  896. with_settings :gravatar_enabled => '0' do
  897. assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
  898. end
  899. end
  900. def test_link_to_user
  901. user = User.find(2)
  902. assert_equal '<a href="/users/2" class="user active">John Smith</a>', link_to_user(user)
  903. end
  904. def test_link_to_user_should_not_link_to_locked_user
  905. with_current_user nil do
  906. user = User.find(5)
  907. assert user.locked?
  908. assert_equal 'Dave2 Lopper2', link_to_user(user)
  909. end
  910. end
  911. def test_link_to_user_should_link_to_locked_user_if_current_user_is_admin
  912. with_current_user User.find(1) do
  913. user = User.find(5)
  914. assert user.locked?
  915. assert_equal '<a href="/users/5" class="user locked">Dave2 Lopper2</a>', link_to_user(user)
  916. end
  917. end
  918. def test_link_to_user_should_not_link_to_anonymous
  919. user = User.anonymous
  920. assert user.anonymous?
  921. t = link_to_user(user)
  922. assert_equal ::I18n.t(:label_user_anonymous), t
  923. end
  924. def test_link_to_attachment
  925. a = Attachment.find(3)
  926. assert_equal '<a href="/attachments/3/logo.gif">logo.gif</a>',
  927. link_to_attachment(a)
  928. assert_equal '<a href="/attachments/3/logo.gif">Text</a>',
  929. link_to_attachment(a, :text => 'Text')
  930. assert_equal '<a href="/attachments/3/logo.gif" class="foo">logo.gif</a>',
  931. link_to_attachment(a, :class => 'foo')
  932. assert_equal '<a href="/attachments/download/3/logo.gif">logo.gif</a>',
  933. link_to_attachment(a, :download => true)
  934. assert_equal '<a href="http://test.host/attachments/3/logo.gif">logo.gif</a>',
  935. link_to_attachment(a, :only_path => false)
  936. end
  937. def test_thumbnail_tag
  938. a = Attachment.find(3)
  939. assert_equal '<a href="/attachments/3/logo.gif" title="logo.gif"><img alt="3" src="/attachments/thumbnail/3" /></a>',
  940. thumbnail_tag(a)
  941. end
  942. def test_link_to_project
  943. project = Project.find(1)
  944. assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
  945. link_to_project(project)
  946. assert_equal %(<a href="/projects/ecookbook/settings">eCookbook</a>),
  947. link_to_project(project, :action => 'settings')
  948. assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
  949. link_to_project(project, {:only_path => false, :jump => 'blah'})
  950. assert_equal %(<a href="/projects/ecookbook/settings" class="project">eCookbook</a>),
  951. link_to_project(project, {:action => 'settings'}, :class => "project")
  952. end
  953. def test_link_to_project_settings
  954. project = Project.find(1)
  955. assert_equal '<a href="/projects/ecookbook/settings">eCookbook</a>', link_to_project_settings(project)
  956. project.status = Project::STATUS_CLOSED
  957. assert_equal '<a href="/projects/ecookbook">eCookbook</a>', link_to_project_settings(project)
  958. project.status = Project::STATUS_ARCHIVED
  959. assert_equal 'eCookbook', link_to_project_settings(project)
  960. end
  961. def test_link_to_legacy_project_with_numerical_identifier_should_use_id
  962. # numeric identifier are no longer allowed
  963. Project.update_all "identifier=25", "id=1"
  964. assert_equal '<a href="/projects/1">eCookbook</a>',
  965. link_to_project(Project.find(1))
  966. end
  967. def test_principals_options_for_select_with_users
  968. User.current = nil
  969. users = [User.find(2), User.find(4)]
  970. assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>),
  971. principals_options_for_select(users)
  972. end
  973. def test_principals_options_for_select_with_selected
  974. User.current = nil
  975. users = [User.find(2), User.find(4)]
  976. assert_equal %(<option value="2">John Smith</option><option value="4" selected="selected">Robert Hill</option>),
  977. principals_options_for_select(users, User.find(4))
  978. end
  979. def test_principals_options_for_select_with_users_and_groups
  980. User.current = nil
  981. users = [User.find(2), Group.find(11), User.find(4), Group.find(10)]
  982. assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>) +
  983. %(<optgroup label="Groups"><option value="10">A Team</option><option value="11">B Team</option></optgroup>),
  984. principals_options_for_select(users)
  985. end
  986. def test_principals_options_for_select_with_empty_collection
  987. assert_equal '', principals_options_for_select([])
  988. end
  989. def test_principals_options_for_select_should_include_me_option_when_current_user_is_in_collection
  990. users = [User.find(2), User.find(4)]
  991. User.current = User.find(4)
  992. assert_include '<option value="4">&lt;&lt; me &gt;&gt;</option>', principals_options_for_select(users)
  993. end
  994. def test_stylesheet_link_tag_should_pick_the_default_stylesheet
  995. assert_match 'href="/stylesheets/styles.css"', stylesheet_link_tag("styles")
  996. end
  997. def test_stylesheet_link_tag_for_plugin_should_pick_the_plugin_stylesheet
  998. assert_match 'href="/plugin_assets/foo/stylesheets/styles.css"', stylesheet_link_tag("styles", :plugin => :foo)
  999. end
  1000. def test_image_tag_should_pick_the_default_image
  1001. assert_match 'src="/images/image.png"', image_tag("image.png")
  1002. end
  1003. def test_image_tag_should_pick_the_theme_image_if_it_exists
  1004. theme = Redmine::Themes.themes.last
  1005. theme.images << 'image.png'
  1006. with_settings :ui_theme => theme.id do
  1007. assert_match %|src="/themes/#{theme.dir}/images/image.png"|, image_tag("image.png")
  1008. assert_match %|src="/images/other.png"|, image_tag("other.png")
  1009. end
  1010. ensure
  1011. theme.images.delete 'image.png'
  1012. end
  1013. def test_image_tag_sfor_plugin_should_pick_the_plugin_image
  1014. assert_match 'src="/plugin_assets/foo/images/image.png"', image_tag("image.png", :plugin => :foo)
  1015. end
  1016. def test_javascript_include_tag_should_pick_the_default_javascript
  1017. assert_match 'src="/javascripts/scripts.js"', javascript_include_tag("scripts")
  1018. end
  1019. def test_javascript_include_tag_for_plugin_should_pick_the_plugin_javascript
  1020. assert_match 'src="/plugin_assets/foo/javascripts/scripts.js"', javascript_include_tag("scripts", :plugin => :foo)
  1021. end
  1022. end