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 67KB

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