You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application_helper_test.rb 59KB

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