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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class ApplicationHelperTest < Redmine::HelperTest
  20. include ERB::Util
  21. include Rails.application.routes.url_helpers
  22. include AvatarsHelper
  23. fixtures :projects, :enabled_modules,
  24. :users, :email_addresses,
  25. :members, :member_roles, :roles,
  26. :repositories, :changesets,
  27. :projects_trackers,
  28. :trackers, :issue_statuses, :issues, :versions, :documents, :journals,
  29. :wikis, :wiki_pages, :wiki_contents,
  30. :boards, :messages, :news,
  31. :attachments, :enumerations,
  32. :custom_values, :custom_fields, :custom_fields_projects
  33. def setup
  34. super
  35. set_tmp_attachments_directory
  36. @russian_test = 'тест'
  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(
  42. 'By controller/actionr',
  43. {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
  44. assert_match /href/, response
  45. end
  46. test "#link_to_if_authorized for unauthorized user should display nothing if user isn't authorized" do
  47. User.current = User.find_by_login('dlopper')
  48. @project = Project.find('private-child')
  49. issue = @project.issues.first
  50. assert !issue.visible?
  51. response = link_to_if_authorized(
  52. 'Never displayed',
  53. {:controller => 'issues', :action => 'show', :id => issue})
  54. assert_nil response
  55. end
  56. def test_auto_links
  57. to_test = {
  58. 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
  59. 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
  60. 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
  61. 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
  62. 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
  63. 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
  64. '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>.',
  65. 'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
  66. '(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>)',
  67. '(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>)',
  68. '(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>).',
  69. '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
  70. '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
  71. '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
  72. 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
  73. '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>',
  74. 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
  75. 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
  76. 'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
  77. 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
  78. 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
  79. 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
  80. # two exclamation marks
  81. '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>',
  82. # escaping
  83. 'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo&quot;bar</a>',
  84. # wrap in angle brackets
  85. '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;',
  86. # invalid urls
  87. 'http://' => 'http://',
  88. 'www.' => 'www.',
  89. 'test-www.bar.com' => 'test-www.bar.com',
  90. # ends with a hyphen
  91. 'http://www.redmine.org/example-' => '<a class="external" href="http://www.redmine.org/example-">http://www.redmine.org/example-</a>',
  92. }
  93. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  94. end
  95. def test_auto_links_with_non_ascii_characters
  96. to_test = {
  97. "http://foo.bar/#{@russian_test}" =>
  98. %|<a class="external" href="http://foo.bar/#{@russian_test}">http://foo.bar/#{@russian_test}</a>|
  99. }
  100. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  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 <span style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></span>',
  113. 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="wiki-class-some-class" alt="" />',
  114. 'with class !(wiki-class-foo)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="wiki-class-foo" alt="" />',
  115. '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="" />',
  116. '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" />',
  117. '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;" />',
  118. }
  119. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  120. end
  121. def test_inline_images_inside_tags
  122. raw = <<~RAW
  123. h1. !foo.png! Heading
  124. Centered image:
  125. p=. !bar.gif!
  126. RAW
  127. assert textilizable(raw).include?('<img src="foo.png" alt="" />')
  128. assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
  129. end
  130. def test_attached_images
  131. to_test = {
  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. 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
  134. 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
  135. 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
  136. # link image
  137. '!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>',
  138. }
  139. attachments = Attachment.all
  140. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
  141. end
  142. def test_attached_images_with_textile_and_non_ascii_filename
  143. to_test = {
  144. 'CAFÉ.JPG' => 'CAF%C3%89.JPG',
  145. 'crème.jpg' => 'cr%C3%A8me.jpg',
  146. }
  147. with_settings :text_formatting => 'textile' do
  148. to_test.each do |filename, result|
  149. attachment = Attachment.generate!(:filename => filename)
  150. assert_include %(<img src="/attachments/download/#{attachment.id}/#{result}" alt="" />), textilizable("!#{filename}!", :attachments => [attachment])
  151. end
  152. end
  153. end
  154. def test_attached_images_with_markdown_and_non_ascii_filename
  155. skip unless Object.const_defined?(:Redcarpet)
  156. to_test = {
  157. 'CAFÉ.JPG' => 'CAF%C3%89.JPG',
  158. 'crème.jpg' => 'cr%C3%A8me.jpg',
  159. }
  160. with_settings :text_formatting => 'markdown' do
  161. to_test.each do |filename, result|
  162. attachment = Attachment.generate!(:filename => filename)
  163. assert_include %(<img src="/attachments/download/#{attachment.id}/#{result}" alt="" />), textilizable("![](#{filename})", :attachments => [attachment])
  164. end
  165. end
  166. end
  167. def test_attached_images_with_hires_naming
  168. attachment = Attachment.generate!(:filename => 'image@2x.png')
  169. assert_equal(
  170. %(<p><img src="/attachments/download/#{attachment.id}/image@2x.png" srcset="/attachments/download/#{attachment.id}/image@2x.png 2x" alt="" /></p>),
  171. textilizable("!image@2x.png!", :attachments => [attachment]))
  172. end
  173. def test_attached_images_filename_extension
  174. a1 = Attachment.new(
  175. :container => Issue.find(1),
  176. :file => mock_file_with_options({:original_filename => "testtest.JPG"}),
  177. :author => User.find(1))
  178. assert a1.save
  179. assert_equal "testtest.JPG", a1.filename
  180. assert_equal "image/jpeg", a1.content_type
  181. assert a1.image?
  182. a2 = Attachment.new(
  183. :container => Issue.find(1),
  184. :file => mock_file_with_options({:original_filename => "testtest.jpeg"}),
  185. :author => User.find(1))
  186. assert a2.save
  187. assert_equal "testtest.jpeg", a2.filename
  188. assert_equal "image/jpeg", a2.content_type
  189. assert a2.image?
  190. a3 = Attachment.new(
  191. :container => Issue.find(1),
  192. :file => mock_file_with_options({:original_filename => "testtest.JPE"}),
  193. :author => User.find(1))
  194. assert a3.save
  195. assert_equal "testtest.JPE", a3.filename
  196. assert_equal "image/jpeg", a3.content_type
  197. assert a3.image?
  198. a4 = Attachment.new(
  199. :container => Issue.find(1),
  200. :file => mock_file_with_options({:original_filename => "Testtest.BMP"}),
  201. :author => User.find(1))
  202. assert a4.save
  203. assert_equal "Testtest.BMP", a4.filename
  204. assert_equal "image/x-ms-bmp", a4.content_type
  205. assert a4.image?
  206. to_test = {
  207. 'Inline image: !testtest.jpg!' =>
  208. 'Inline image: <img src="/attachments/download/' + a1.id.to_s + '/testtest.JPG" alt="" />',
  209. 'Inline image: !testtest.jpeg!' =>
  210. 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testtest.jpeg" alt="" />',
  211. 'Inline image: !testtest.jpe!' =>
  212. 'Inline image: <img src="/attachments/download/' + a3.id.to_s + '/testtest.JPE" alt="" />',
  213. 'Inline image: !testtest.bmp!' =>
  214. 'Inline image: <img src="/attachments/download/' + a4.id.to_s + '/Testtest.BMP" alt="" />',
  215. }
  216. attachments = [a1, a2, a3, a4]
  217. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
  218. end
  219. def test_attached_images_should_read_later
  220. set_fixtures_attachments_directory
  221. a1 = Attachment.find(16)
  222. assert_equal "testfile.png", a1.filename
  223. assert a1.readable?
  224. assert_not a1.visible?(User.anonymous)
  225. assert a1.visible?(User.find(2))
  226. a2 = Attachment.find(17)
  227. assert_equal "testfile.PNG", a2.filename
  228. assert a2.readable?
  229. assert_not a2.visible?(User.anonymous)
  230. assert a2.visible?(User.find(2))
  231. assert a1.created_on < a2.created_on
  232. to_test = {
  233. 'Inline image: !testfile.png!' =>
  234. 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
  235. 'Inline image: !Testfile.PNG!' =>
  236. 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
  237. }
  238. attachments = [a1, a2]
  239. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
  240. ensure
  241. set_tmp_attachments_directory
  242. end
  243. def test_textile_external_links
  244. to_test = {
  245. 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
  246. 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
  247. '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
  248. '"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>',
  249. "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
  250. # no multiline link text
  251. "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",
  252. # mailto link
  253. "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
  254. # two exclamation marks
  255. '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
  256. # escaping
  257. '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
  258. # ends with a hyphen
  259. '(see "inline link":http://www.foo.bar/Test-)' => '(see <a href="http://www.foo.bar/Test-" class="external">inline link</a>)',
  260. '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>',
  261. 'This is an intern "link":/foo/bar-' => 'This is an intern <a href="/foo/bar-">link</a>', }
  262. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  263. end
  264. def test_textile_external_links_with_non_ascii_characters
  265. to_test = {
  266. %|This is a "link":http://foo.bar/#{@russian_test}| =>
  267. %|This is a <a href="http://foo.bar/#{@russian_test}" class="external">link</a>|
  268. }
  269. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  270. end
  271. def test_redmine_links
  272. user_with_email_login = User.generate!(:login => 'abcd@example.com')
  273. user_with_email_login_2 = User.generate!(:login => 'foo.bar@example.com')
  274. u_email_id = user_with_email_login.id
  275. u_email_id_2 = user_with_email_login_2.id
  276. issue_link = link_to('#3',
  277. {:controller => 'issues', :action => 'show', :id => 3},
  278. :class => Issue.find(3).css_classes,
  279. :title => 'Bug: Error 281 when updating a recipe (New)')
  280. ext_issue_link = link_to(
  281. 'Bug #3: Error 281 when updating a recipe',
  282. {:controller => 'issues', :action => 'show', :id => 3},
  283. :class => Issue.find(3).css_classes,
  284. :title => 'Status: New')
  285. note_link = link_to(
  286. '#3-14',
  287. {:controller => 'issues', :action => 'show',
  288. :id => 3, :anchor => 'note-14'},
  289. :class => Issue.find(3).css_classes,
  290. :title => 'Bug: Error 281 when updating a recipe (New)')
  291. ext_note_link = link_to(
  292. 'Bug #3-14: Error 281 when updating a recipe',
  293. {:controller => 'issues', :action => 'show',
  294. :id => 3, :anchor => 'note-14'},
  295. :class => Issue.find(3).css_classes,
  296. :title => 'Status: New')
  297. note_link2 = link_to(
  298. '#3#note-14',
  299. {:controller => 'issues', :action => 'show',
  300. :id => 3, :anchor => 'note-14'},
  301. :class => Issue.find(3).css_classes,
  302. :title => 'Bug: Error 281 when updating a recipe (New)')
  303. ext_note_link2 = link_to(
  304. 'Bug #3#note-14: Error 281 when updating a recipe',
  305. {:controller => 'issues', :action => 'show',
  306. :id => 3, :anchor => 'note-14'},
  307. :class => Issue.find(3).css_classes,
  308. :title => 'Status: New')
  309. revision_link = link_to(
  310. 'r1',
  311. {:controller => 'repositories', :action => 'revision',
  312. :id => 'ecookbook', :repository_id => 10, :rev => 1},
  313. :class => 'changeset',
  314. :title => 'My very first commit do not escaping #<>&')
  315. revision_link2 = link_to(
  316. 'r2',
  317. {:controller => 'repositories', :action => 'revision',
  318. :id => 'ecookbook', :repository_id => 10, :rev => 2},
  319. :class => 'changeset',
  320. :title => 'This commit fixes #1, #2 and references #1 & #3')
  321. changeset_link2 = link_to(
  322. '691322a8eb01e11fd7',
  323. {:controller => 'repositories', :action => 'revision',
  324. :id => 'ecookbook', :repository_id => 10, :rev => 1},
  325. :class => 'changeset', :title => 'My very first commit do not escaping #<>&')
  326. document_link = link_to(
  327. 'Test document',
  328. {:controller => 'documents', :action => 'show', :id => 1},
  329. :class => 'document')
  330. version_link = link_to('1.0',
  331. {:controller => 'versions', :action => 'show', :id => 2},
  332. :class => 'version')
  333. board_url = {:controller => 'boards', :action => 'show', :id => 2, :project_id => 'ecookbook'}
  334. message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
  335. news_url = {:controller => 'news', :action => 'show', :id => 1}
  336. project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
  337. source_url = '/projects/ecookbook/repository/10/entry/some/file'
  338. source_url_with_rev = '/projects/ecookbook/repository/10/revisions/52/entry/some/file'
  339. source_url_with_ext = '/projects/ecookbook/repository/10/entry/some/file.ext'
  340. source_url_with_rev_and_ext = '/projects/ecookbook/repository/10/revisions/52/entry/some/file.ext'
  341. source_url_with_branch = '/projects/ecookbook/repository/10/revisions/branch/entry/some/file'
  342. export_url = '/projects/ecookbook/repository/10/raw/some/file'
  343. export_url_with_rev = '/projects/ecookbook/repository/10/revisions/52/raw/some/file'
  344. export_url_with_ext = '/projects/ecookbook/repository/10/raw/some/file.ext'
  345. export_url_with_rev_and_ext = '/projects/ecookbook/repository/10/revisions/52/raw/some/file.ext'
  346. export_url_with_branch = '/projects/ecookbook/repository/10/revisions/branch/raw/some/file'
  347. to_test = {
  348. # tickets
  349. '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
  350. # ticket notes
  351. '#3-14' => note_link,
  352. '#3#note-14' => note_link2,
  353. # should not ignore leading zero
  354. '#03' => '#03',
  355. # tickets with more info
  356. '##3, [##3], (##3) and ##3.' => "#{ext_issue_link}, [#{ext_issue_link}], (#{ext_issue_link}) and #{ext_issue_link}.",
  357. '##3-14' => ext_note_link,
  358. '##3#note-14' => ext_note_link2,
  359. '##03' => '##03',
  360. # changesets
  361. 'r1' => revision_link,
  362. 'r1.' => "#{revision_link}.",
  363. 'r1, r2' => "#{revision_link}, #{revision_link2}",
  364. 'r1,r2' => "#{revision_link},#{revision_link2}",
  365. 'commit:691322a8eb01e11fd7' => changeset_link2,
  366. # documents
  367. 'document#1' => document_link,
  368. 'document:"Test document"' => document_link,
  369. # versions
  370. 'version#2' => version_link,
  371. 'version:1.0' => version_link,
  372. 'version:"1.0"' => version_link,
  373. # source
  374. 'source:some/file' => link_to('source:some/file', source_url, :class => 'source'),
  375. 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
  376. 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
  377. 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
  378. 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
  379. 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
  380. 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
  381. 'source:/some/file@52' => link_to('source:/some/file@52', source_url_with_rev, :class => 'source'),
  382. 'source:/some/file@branch' => link_to('source:/some/file@branch', source_url_with_branch, :class => 'source'),
  383. 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_rev_and_ext, :class => 'source'),
  384. 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url + "#L110", :class => 'source'),
  385. 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext + "#L110", :class => 'source'),
  386. 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url_with_rev + "#L110", :class => 'source'),
  387. # export
  388. 'export:/some/file' => link_to('export:/some/file', export_url, :class => 'source download'),
  389. 'export:/some/file.ext' => link_to('export:/some/file.ext', export_url_with_ext, :class => 'source download'),
  390. 'export:/some/file@52' => link_to('export:/some/file@52', export_url_with_rev, :class => 'source download'),
  391. 'export:/some/file.ext@52' => link_to('export:/some/file.ext@52', export_url_with_rev_and_ext, :class => 'source download'),
  392. 'export:/some/file@branch' => link_to('export:/some/file@branch', export_url_with_branch, :class => 'source download'),
  393. # forum
  394. 'forum#2' => link_to('Discussion', board_url, :class => 'board'),
  395. 'forum:Discussion' => link_to('Discussion', board_url, :class => 'board'),
  396. # message
  397. 'message#4' => link_to('Post 2', message_url, :class => 'message'),
  398. 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5', :r => 5), :class => 'message'),
  399. # news
  400. 'news#1' => link_to('eCookbook first release !', news_url, :class => 'news'),
  401. 'news:"eCookbook first release !"' => link_to('eCookbook first release !', news_url, :class => 'news'),
  402. # project
  403. 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
  404. 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
  405. 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
  406. # not found
  407. '#0123456789' => '#0123456789',
  408. # invalid expressions
  409. 'source:' => 'source:',
  410. # url hash
  411. "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
  412. # user
  413. 'user:jsmith' => link_to_user(User.find_by_id(2)),
  414. 'user:JSMITH' => link_to_user(User.find_by_id(2)),
  415. 'user#2' => link_to_user(User.find_by_id(2)),
  416. '@jsmith' => link_to_user(User.find_by_id(2)),
  417. '@JSMITH' => link_to_user(User.find_by_id(2)),
  418. '@abcd@example.com' => link_to_user(User.find_by_id(u_email_id)),
  419. 'user:abcd@example.com' => link_to_user(User.find_by_id(u_email_id)),
  420. '@foo.bar@example.com' => link_to_user(User.find_by_id(u_email_id_2)),
  421. 'user:foo.bar@example.com' => link_to_user(User.find_by_id(u_email_id_2)),
  422. # invalid user
  423. 'user:foobar' => 'user:foobar',
  424. }
  425. @project = Project.find(1)
  426. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
  427. end
  428. def test_link_to_note_within_the_same_page
  429. issue = Issue.find(1)
  430. assert_equal '<p><a href="#note-14">#note-14</a></p>', textilizable('#note-14', :object => issue)
  431. journal = Journal.find(2)
  432. assert_equal '<p><a href="#note-2">#note-2</a></p>', textilizable('#note-2', :object => journal)
  433. end
  434. def test_user_links_with_email_as_login_name_should_not_be_parsed_textile
  435. with_settings :text_formatting => 'textile' do
  436. u = User.generate!(:login => 'jsmith@somenet.foo')
  437. # user link format: @jsmith@somenet.foo
  438. raw = "@jsmith@somenet.foo should not be parsed in jsmith@somenet.foo"
  439. assert_match(
  440. %r{<p><a class="user active".*>#{u.name}</a> should not be parsed in <a class="email" href="mailto:jsmith@somenet.foo">jsmith@somenet.foo</a></p>},
  441. textilizable(raw, :project => Project.find(1)))
  442. # user link format: user:jsmith@somenet.foo
  443. raw = "user:jsmith@somenet.foo should not be parsed in jsmith@somenet.foo"
  444. assert_match(
  445. %r{<p><a class="user active".*>#{u.name}</a> should not be parsed in <a class="email" href="mailto:jsmith@somenet.foo">jsmith@somenet.foo</a></p>},
  446. textilizable(raw, :project => Project.find(1)))
  447. end
  448. end
  449. def test_user_links_with_email_as_login_name_should_not_be_parsed_markdown
  450. with_settings :text_formatting => 'markdown' do
  451. u = User.generate!(:login => 'jsmith@somenet.foo')
  452. # user link format: @jsmith@somenet.foo
  453. raw = "@jsmith@somenet.foo should not be parsed in jsmith@somenet.foo"
  454. assert_match(
  455. %r{<p><a class=\"user active\".*>#{u.name}</a> should not be parsed in <a href=\"mailto:jsmith@somenet.foo\">jsmith@somenet.foo</a></p>},
  456. textilizable(raw, :project => Project.find(1)))
  457. # user link format: user:jsmith@somenet.foo
  458. raw = "user:jsmith@somenet.foo should not be parsed in jsmith@somenet.foo"
  459. assert_match(
  460. %r{<p><a class=\"user active\".*>#{u.name}</a> should not be parsed in <a href=\"mailto:jsmith@somenet.foo\">jsmith@somenet.foo</a></p>},
  461. textilizable(raw, :project => Project.find(1)))
  462. end
  463. end
  464. def test_should_not_parse_redmine_links_inside_link
  465. raw = "r1 should not be parsed in http://example.com/url-r1/"
  466. assert_match(
  467. %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>},
  468. textilizable(raw, :project => Project.find(1)))
  469. end
  470. def test_redmine_links_with_a_different_project_before_current_project
  471. vp1 = Version.generate!(:project_id => 1, :name => '1.4.4')
  472. vp3 = Version.generate!(:project_id => 3, :name => '1.4.4')
  473. @project = Project.find(3)
  474. result1 = link_to("1.4.4", "/versions/#{vp1.id}", :class => "version")
  475. result2 = link_to("1.4.4", "/versions/#{vp3.id}", :class => "version")
  476. assert_equal "<p>#{result1} #{result2}</p>",
  477. textilizable("ecookbook:version:1.4.4 version:1.4.4")
  478. end
  479. def test_escaped_redmine_links_should_not_be_parsed
  480. to_test = [
  481. '#3.',
  482. '#3-14.',
  483. '#3#-note14.',
  484. 'r1',
  485. 'document#1',
  486. 'document:"Test document"',
  487. 'version#2',
  488. 'version:1.0',
  489. 'version:"1.0"',
  490. 'source:/some/file'
  491. ]
  492. @project = Project.find(1)
  493. to_test.each { |text| assert_equal "<p>#{text}</p>", textilizable("!" + text), "#{text} failed" }
  494. end
  495. def test_cross_project_redmine_links
  496. source_link = link_to('ecookbook:source:/some/file',
  497. {:controller => 'repositories', :action => 'entry',
  498. :id => 'ecookbook', :repository_id => 10, :path => ['some', 'file']},
  499. :class => 'source')
  500. changeset_link = link_to('ecookbook:r2',
  501. {:controller => 'repositories', :action => 'revision',
  502. :id => 'ecookbook', :repository_id => 10, :rev => 2},
  503. :class => 'changeset',
  504. :title => 'This commit fixes #1, #2 and references #1 & #3')
  505. to_test = {
  506. # documents
  507. 'document:"Test document"' => 'document:"Test document"',
  508. 'ecookbook:document:"Test document"' =>
  509. link_to("Test document", "/documents/1", :class => "document"),
  510. 'invalid:document:"Test document"' => 'invalid:document:"Test document"',
  511. # versions
  512. 'version:"1.0"' => 'version:"1.0"',
  513. 'ecookbook:version:"1.0"' =>
  514. link_to("1.0", "/versions/2", :class => "version"),
  515. 'invalid:version:"1.0"' => 'invalid:version:"1.0"',
  516. # changeset
  517. 'r2' => 'r2',
  518. 'ecookbook:r2' => changeset_link,
  519. 'invalid:r2' => 'invalid:r2',
  520. # source
  521. 'source:/some/file' => 'source:/some/file',
  522. 'ecookbook:source:/some/file' => source_link,
  523. 'invalid:source:/some/file' => 'invalid:source:/some/file',
  524. }
  525. @project = Project.find(3)
  526. to_test.each do |text, result|
  527. assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed"
  528. end
  529. end
  530. def test_redmine_links_by_name_should_work_with_html_escaped_characters
  531. v = Version.generate!(:name => "Test & Show.txt", :project_id => 1)
  532. link = link_to("Test & Show.txt", "/versions/#{v.id}", :class => "version")
  533. @project = v.project
  534. assert_equal "<p>#{link}</p>", textilizable('version:"Test & Show.txt"')
  535. end
  536. def test_link_to_issue_subject
  537. issue = Issue.generate!(:subject => "01234567890123456789")
  538. str = link_to_issue(issue, :truncate => 10)
  539. result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
  540. assert_equal "#{result}: 0123456...", str
  541. issue = Issue.generate!(:subject => "<&>")
  542. str = link_to_issue(issue)
  543. result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
  544. assert_equal "#{result}: &lt;&amp;&gt;", str
  545. issue = Issue.generate!(:subject => "<&>0123456789012345")
  546. str = link_to_issue(issue, :truncate => 10)
  547. result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
  548. assert_equal "#{result}: &lt;&amp;&gt;0123...", str
  549. end
  550. def test_link_to_issue_title
  551. long_str = "0123456789" * 5
  552. issue = Issue.generate!(:subject => "#{long_str}01234567890123456789")
  553. str = link_to_issue(issue, :subject => false)
  554. result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}",
  555. :class => issue.css_classes,
  556. :title => "#{long_str}0123456...")
  557. assert_equal result, str
  558. issue = Issue.generate!(:subject => "<&>#{long_str}01234567890123456789")
  559. str = link_to_issue(issue, :subject => false)
  560. result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}",
  561. :class => issue.css_classes,
  562. :title => "<&>#{long_str}0123...")
  563. assert_equal result, str
  564. end
  565. def test_multiple_repositories_redmine_links
  566. svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn_repo-1', :url => 'file:///foo/hg')
  567. Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
  568. hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
  569. Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
  570. changeset_link = link_to(
  571. 'r2',
  572. {:controller => 'repositories', :action => 'revision',
  573. :id => 'ecookbook', :repository_id => 10, :rev => 2},
  574. :class => 'changeset',
  575. :title => 'This commit fixes #1, #2 and references #1 & #3')
  576. svn_changeset_link = link_to(
  577. 'svn_repo-1|r123',
  578. {:controller => 'repositories', :action => 'revision',
  579. :id => 'ecookbook', :repository_id => 'svn_repo-1', :rev => 123},
  580. :class => 'changeset', :title => '')
  581. hg_changeset_link = link_to(
  582. 'hg1|abcd',
  583. {:controller => 'repositories', :action => 'revision',
  584. :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
  585. :class => 'changeset', :title => '')
  586. source_link = link_to('source:some/file',
  587. {:controller => 'repositories', :action => 'entry',
  588. :id => 'ecookbook', :repository_id => 10,
  589. :path => ['some', 'file']},
  590. :class => 'source')
  591. hg_source_link = link_to('source:hg1|some/file',
  592. {:controller => 'repositories', :action => 'entry',
  593. :id => 'ecookbook', :repository_id => 'hg1',
  594. :path => ['some', 'file']},
  595. :class => 'source')
  596. to_test = {
  597. 'r2' => changeset_link,
  598. 'svn_repo-1|r123' => svn_changeset_link,
  599. 'invalid|r123' => 'invalid|r123',
  600. 'commit:hg1|abcd' => hg_changeset_link,
  601. 'commit:invalid|abcd' => 'commit:invalid|abcd',
  602. # source
  603. 'source:some/file' => source_link,
  604. 'source:hg1|some/file' => hg_source_link,
  605. 'source:invalid|some/file' => 'source:invalid|some/file',
  606. }
  607. @project = Project.find(1)
  608. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
  609. end
  610. def test_cross_project_multiple_repositories_redmine_links
  611. svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///foo/hg')
  612. Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
  613. hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
  614. Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
  615. changeset_link = link_to(
  616. 'ecookbook:r2',
  617. {:controller => 'repositories', :action => 'revision',
  618. :id => 'ecookbook', :repository_id => 10, :rev => 2},
  619. :class => 'changeset',
  620. :title => 'This commit fixes #1, #2 and references #1 & #3')
  621. svn_changeset_link = link_to(
  622. 'ecookbook:svn1|r123',
  623. {:controller => 'repositories', :action => 'revision',
  624. :id => 'ecookbook', :repository_id => 'svn1', :rev => 123},
  625. :class => 'changeset', :title => '')
  626. hg_changeset_link = link_to(
  627. 'ecookbook:hg1|abcd',
  628. {:controller => 'repositories', :action => 'revision',
  629. :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
  630. :class => 'changeset', :title => '')
  631. source_link = link_to('ecookbook:source:some/file',
  632. {:controller => 'repositories', :action => 'entry',
  633. :id => 'ecookbook', :repository_id => 10,
  634. :path => ['some', 'file']}, :class => 'source')
  635. hg_source_link = link_to('ecookbook:source:hg1|some/file',
  636. {:controller => 'repositories', :action => 'entry',
  637. :id => 'ecookbook', :repository_id => 'hg1',
  638. :path => ['some', 'file']}, :class => 'source')
  639. to_test = {
  640. 'ecookbook:r2' => changeset_link,
  641. 'ecookbook:svn1|r123' => svn_changeset_link,
  642. 'ecookbook:invalid|r123' => 'ecookbook:invalid|r123',
  643. 'ecookbook:commit:hg1|abcd' => hg_changeset_link,
  644. 'ecookbook:commit:invalid|abcd' => 'ecookbook:commit:invalid|abcd',
  645. 'invalid:commit:invalid|abcd' => 'invalid:commit:invalid|abcd',
  646. # source
  647. 'ecookbook:source:some/file' => source_link,
  648. 'ecookbook:source:hg1|some/file' => hg_source_link,
  649. 'ecookbook:source:invalid|some/file' => 'ecookbook:source:invalid|some/file',
  650. 'invalid:source:invalid|some/file' => 'invalid:source:invalid|some/file',
  651. }
  652. @project = Project.find(3)
  653. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
  654. end
  655. def test_redmine_links_git_commit
  656. @project = Project.find(3)
  657. r = Repository::Git.create!(:project => @project, :url => '/tmp/test/git')
  658. c = Changeset.create!(
  659. :repository => r,
  660. :committed_on => Time.now,
  661. :revision => 'abcd',
  662. :scmid => 'abcd',
  663. :comments => 'test commit')
  664. changeset_link = link_to('abcd',
  665. {
  666. :controller => 'repositories',
  667. :action => 'revision',
  668. :id => 'subproject1',
  669. :repository_id => r.id,
  670. :rev => 'abcd',
  671. },
  672. :class => 'changeset', :title => 'test commit')
  673. to_test = {
  674. 'commit:abcd' => changeset_link,
  675. }
  676. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  677. end
  678. # TODO: Bazaar commit id contains mail address, so it contains '@' and '_'.
  679. def test_redmine_links_mercurial_commit
  680. @project = Project.find(3)
  681. r = Repository::Mercurial.create!(:project => @project, :url => '/tmp/test')
  682. c = Changeset.create!(
  683. :repository => r,
  684. :committed_on => Time.now,
  685. :revision => '123',
  686. :scmid => 'abcd',
  687. :comments => 'test commit')
  688. changeset_link_rev = link_to(
  689. 'r123',
  690. {
  691. :controller => 'repositories',
  692. :action => 'revision',
  693. :id => 'subproject1',
  694. :repository_id => r.id,
  695. :rev => '123',
  696. },
  697. :class => 'changeset', :title => 'test commit')
  698. changeset_link_commit = link_to(
  699. 'abcd',
  700. {
  701. :controller => 'repositories',
  702. :action => 'revision',
  703. :id => 'subproject1',
  704. :repository_id => r.id,
  705. :rev => 'abcd',
  706. },
  707. :class => 'changeset', :title => 'test commit')
  708. to_test = {
  709. 'r123' => changeset_link_rev,
  710. 'commit:abcd' => changeset_link_commit,
  711. }
  712. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  713. end
  714. def test_attachment_links
  715. text = 'attachment:error281.txt'
  716. result = link_to("error281.txt", "/attachments/1",
  717. :class => "attachment")
  718. assert_equal "<p>#{result}</p>",
  719. textilizable(text,
  720. :attachments => Issue.find(3).attachments),
  721. "#{text} failed"
  722. end
  723. def test_attachment_link_should_link_to_latest_attachment
  724. a1 = Attachment.generate!(:filename => "test.txt", :created_on => 1.hour.ago)
  725. a2 = Attachment.generate!(:filename => "test.txt")
  726. result = link_to("test.txt", "/attachments/#{a2.id}",
  727. :class => "attachment")
  728. assert_equal "<p>#{result}</p>",
  729. textilizable('attachment:test.txt', :attachments => [a1, a2])
  730. end
  731. def test_attachment_links_to_images_with_email_format_should_not_be_parsed
  732. attachment = Attachment.generate!(:filename => 'image@2x.png')
  733. with_settings :text_formatting => 'textile' do
  734. raw = "attachment:image@2x.png should not be parsed in image@2x.png"
  735. assert_match(
  736. %r{<p><a class="attachment" href="/attachments/#{attachment.id}">image@2x.png</a> should not be parsed in image@2x.png</p>},
  737. textilizable(raw, :attachments => [attachment]))
  738. end
  739. with_settings :text_formatting => 'markdown' do
  740. raw = "attachment:image@2x.png should not be parsed in image@2x.png"
  741. assert_match(
  742. %r{<p><a class="attachment" href="/attachments/#{attachment.id}">image@2x.png</a> should not be parsed in image@2x.png</p>},
  743. textilizable(raw, :attachments => [attachment]))
  744. end
  745. end
  746. def test_wiki_links
  747. User.current = User.find_by_login('jsmith')
  748. russian_eacape = CGI.escape(@russian_test)
  749. to_test = {
  750. '[[CookBook documentation]]' =>
  751. link_to("CookBook documentation",
  752. "/projects/ecookbook/wiki/CookBook_documentation",
  753. :class => "wiki-page"),
  754. '[[Another page|Page]]' =>
  755. link_to("Page",
  756. "/projects/ecookbook/wiki/Another_page",
  757. :class => "wiki-page"),
  758. # title content should be formatted
  759. '[[Another page|With _styled_ *title*]]' =>
  760. link_to("With <em>styled</em> <strong>title</strong>".html_safe,
  761. "/projects/ecookbook/wiki/Another_page",
  762. :class => "wiki-page"),
  763. '[[Another page|With title containing <strong>HTML entities &amp; markups</strong>]]' =>
  764. link_to("With title containing &lt;strong&gt;HTML entities &amp; markups&lt;/strong&gt;".html_safe,
  765. "/projects/ecookbook/wiki/Another_page",
  766. :class => "wiki-page"),
  767. # link with anchor
  768. '[[CookBook documentation#One-section]]' =>
  769. link_to("CookBook documentation",
  770. "/projects/ecookbook/wiki/CookBook_documentation#One-section",
  771. :class => "wiki-page"),
  772. '[[Another page#anchor|Page]]' =>
  773. link_to("Page",
  774. "/projects/ecookbook/wiki/Another_page#anchor",
  775. :class => "wiki-page"),
  776. # UTF8 anchor
  777. "[[Another_page##{@russian_test}|#{@russian_test}]]" =>
  778. link_to(@russian_test,
  779. "/projects/ecookbook/wiki/Another_page##{russian_eacape}",
  780. :class => "wiki-page"),
  781. # link to anchor
  782. '[[#anchor]]' =>
  783. link_to("#anchor",
  784. "#anchor",
  785. :class => "wiki-page"),
  786. '[[#anchor|One-section]]' =>
  787. link_to("One-section",
  788. "#anchor",
  789. :class => "wiki-page"),
  790. # page that doesn't exist
  791. '[[Unknown page]]' =>
  792. link_to("Unknown page",
  793. "/projects/ecookbook/wiki/Unknown_page",
  794. :class => "wiki-page new"),
  795. '[[Unknown page|404]]' =>
  796. link_to("404",
  797. "/projects/ecookbook/wiki/Unknown_page",
  798. :class => "wiki-page new"),
  799. # link to another project wiki
  800. '[[onlinestore:]]' =>
  801. link_to("onlinestore",
  802. "/projects/onlinestore/wiki",
  803. :class => "wiki-page"),
  804. '[[onlinestore:|Wiki]]' =>
  805. link_to("Wiki",
  806. "/projects/onlinestore/wiki",
  807. :class => "wiki-page"),
  808. '[[onlinestore:Start page]]' =>
  809. link_to("Start page",
  810. "/projects/onlinestore/wiki/Start_page",
  811. :class => "wiki-page"),
  812. '[[onlinestore:Start page|Text]]' =>
  813. link_to("Text",
  814. "/projects/onlinestore/wiki/Start_page",
  815. :class => "wiki-page"),
  816. '[[onlinestore:Unknown page]]' =>
  817. link_to("Unknown page",
  818. "/projects/onlinestore/wiki/Unknown_page",
  819. :class => "wiki-page new"),
  820. # struck through link
  821. '-[[Another page|Page]]-' =>
  822. "<del>".html_safe +
  823. link_to("Page",
  824. "/projects/ecookbook/wiki/Another_page",
  825. :class => "wiki-page").html_safe +
  826. "</del>".html_safe,
  827. '-[[Another page|Page]] link-' =>
  828. "<del>".html_safe +
  829. link_to("Page",
  830. "/projects/ecookbook/wiki/Another_page",
  831. :class => "wiki-page").html_safe +
  832. " link</del>".html_safe,
  833. # escaping
  834. '![[Another page|Page]]' => '[[Another page|Page]]',
  835. # project does not exist
  836. '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
  837. '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
  838. # missing permission to view wiki in project
  839. '[[private-child:]]' => '[[private-child:]]',
  840. '[[private-child:Wiki]]' => '[[private-child:Wiki]]',
  841. }
  842. @project = Project.find(1)
  843. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  844. end
  845. def test_wiki_links_with_special_characters_should_work_in_textile
  846. to_test = wiki_links_with_special_characters
  847. @project = Project.find(1)
  848. with_settings :text_formatting => 'textile' do
  849. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  850. end
  851. end
  852. def test_wiki_links_with_special_characters_should_work_in_markdown
  853. to_test = wiki_links_with_special_characters
  854. @project = Project.find(1)
  855. with_settings :text_formatting => 'markdown' do
  856. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text).strip }
  857. end
  858. end
  859. def test_wiki_links_with_square_brackets_in_project_name
  860. User.current = User.find_by_login('jsmith')
  861. another_project = Project.find(1) # eCookbook
  862. another_project.name = "[foo]#{another_project.name}"
  863. another_project.save
  864. page = another_project.wiki.find_page('Another page')
  865. page.title = "[bar]#{page.title}"
  866. page.save
  867. to_test = {
  868. '[[[foo]eCookbook:]]' =>
  869. link_to("[foo]eCookbook",
  870. "/projects/ecookbook/wiki",
  871. :class => "wiki-page"),
  872. '[[[foo]eCookbook:CookBook documentation]]' =>
  873. link_to("CookBook documentation",
  874. "/projects/ecookbook/wiki/CookBook_documentation",
  875. :class => "wiki-page"),
  876. '[[[foo]eCookbook:[bar]Another page]]' =>
  877. link_to("[bar]Another page",
  878. "/projects/ecookbook/wiki/%5Bbar%5DAnother_page",
  879. :class => "wiki-page"),
  880. '[[[foo]eCookbook:Unknown page]]' =>
  881. link_to("Unknown page",
  882. "/projects/ecookbook/wiki/Unknown_page",
  883. :class => "wiki-page new"),
  884. '[[[foo]eCookbook:[baz]Unknown page]]' =>
  885. link_to("[baz]Unknown page",
  886. "/projects/ecookbook/wiki/%5Bbaz%5DUnknown_page",
  887. :class => "wiki-page new"),
  888. }
  889. @project = Project.find(2) # OnlineStore
  890. with_settings :text_formatting => 'textile' do
  891. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  892. end
  893. with_settings :text_formatting => 'markdown' do
  894. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text).strip }
  895. end
  896. end
  897. def test_wiki_links_within_local_file_generation_context
  898. to_test = {
  899. # link to a page
  900. '[[CookBook documentation]]' =>
  901. link_to("CookBook documentation", "CookBook_documentation.html",
  902. :class => "wiki-page"),
  903. '[[CookBook documentation|documentation]]' =>
  904. link_to("documentation", "CookBook_documentation.html",
  905. :class => "wiki-page"),
  906. '[[CookBook documentation#One-section]]' =>
  907. link_to("CookBook documentation", "CookBook_documentation.html#One-section",
  908. :class => "wiki-page"),
  909. '[[CookBook documentation#One-section|documentation]]' =>
  910. link_to("documentation", "CookBook_documentation.html#One-section",
  911. :class => "wiki-page"),
  912. # page that doesn't exist
  913. '[[Unknown page]]' =>
  914. link_to("Unknown page", "Unknown_page.html",
  915. :class => "wiki-page new"),
  916. '[[Unknown page|404]]' =>
  917. link_to("404", "Unknown_page.html",
  918. :class => "wiki-page new"),
  919. '[[Unknown page#anchor]]' =>
  920. link_to("Unknown page", "Unknown_page.html#anchor",
  921. :class => "wiki-page new"),
  922. '[[Unknown page#anchor|404]]' =>
  923. link_to("404", "Unknown_page.html#anchor",
  924. :class => "wiki-page new"),
  925. }
  926. @project = Project.find(1)
  927. to_test.each do |text, result|
  928. assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :local)
  929. end
  930. end
  931. def test_wiki_links_within_wiki_page_context
  932. page = WikiPage.find_by_title('Another_page')
  933. to_test = {
  934. '[[CookBook documentation]]' =>
  935. link_to("CookBook documentation",
  936. "/projects/ecookbook/wiki/CookBook_documentation",
  937. :class => "wiki-page"),
  938. '[[CookBook documentation|documentation]]' =>
  939. link_to("documentation",
  940. "/projects/ecookbook/wiki/CookBook_documentation",
  941. :class => "wiki-page"),
  942. '[[CookBook documentation#One-section]]' =>
  943. link_to("CookBook documentation",
  944. "/projects/ecookbook/wiki/CookBook_documentation#One-section",
  945. :class => "wiki-page"),
  946. '[[CookBook documentation#One-section|documentation]]' =>
  947. link_to("documentation",
  948. "/projects/ecookbook/wiki/CookBook_documentation#One-section",
  949. :class => "wiki-page"),
  950. # link to the current page
  951. '[[Another page]]' =>
  952. link_to("Another page",
  953. "/projects/ecookbook/wiki/Another_page",
  954. :class => "wiki-page"),
  955. '[[Another page|Page]]' =>
  956. link_to("Page",
  957. "/projects/ecookbook/wiki/Another_page",
  958. :class => "wiki-page"),
  959. '[[Another page#anchor]]' =>
  960. link_to("Another page",
  961. "#anchor",
  962. :class => "wiki-page"),
  963. '[[Another page#anchor|Page]]' =>
  964. link_to("Page",
  965. "#anchor",
  966. :class => "wiki-page"),
  967. # page that doesn't exist
  968. '[[Unknown page]]' =>
  969. link_to("Unknown page",
  970. "/projects/ecookbook/wiki/Unknown_page?parent=Another_page",
  971. :class => "wiki-page new"),
  972. '[[Unknown page|404]]' =>
  973. link_to("404",
  974. "/projects/ecookbook/wiki/Unknown_page?parent=Another_page",
  975. :class => "wiki-page new"),
  976. '[[Unknown page#anchor]]' =>
  977. link_to("Unknown page",
  978. "/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor",
  979. :class => "wiki-page new"),
  980. '[[Unknown page#anchor|404]]' =>
  981. link_to("404",
  982. "/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor",
  983. :class => "wiki-page new"),
  984. }
  985. @project = Project.find(1)
  986. to_test.each do |text, result|
  987. assert_equal "<p>#{result}</p>",
  988. textilizable(WikiContent.new(:text => text, :page => page), :text)
  989. end
  990. end
  991. def test_wiki_links_anchor_option_should_prepend_page_title_to_href
  992. to_test = {
  993. # link to a page
  994. '[[CookBook documentation]]' =>
  995. link_to("CookBook documentation",
  996. "#CookBook_documentation",
  997. :class => "wiki-page"),
  998. '[[CookBook documentation|documentation]]' =>
  999. link_to("documentation",
  1000. "#CookBook_documentation",
  1001. :class => "wiki-page"),
  1002. '[[CookBook documentation#One-section]]' =>
  1003. link_to("CookBook documentation",
  1004. "#CookBook_documentation_One-section",
  1005. :class => "wiki-page"),
  1006. '[[CookBook documentation#One-section|documentation]]' =>
  1007. link_to("documentation",
  1008. "#CookBook_documentation_One-section",
  1009. :class => "wiki-page"),
  1010. # page that doesn't exist
  1011. '[[Unknown page]]' =>
  1012. link_to("Unknown page",
  1013. "#Unknown_page",
  1014. :class => "wiki-page new"),
  1015. '[[Unknown page|404]]' =>
  1016. link_to("404",
  1017. "#Unknown_page",
  1018. :class => "wiki-page new"),
  1019. '[[Unknown page#anchor]]' =>
  1020. link_to("Unknown page",
  1021. "#Unknown_page_anchor",
  1022. :class => "wiki-page new"),
  1023. '[[Unknown page#anchor|404]]' =>
  1024. link_to("404",
  1025. "#Unknown_page_anchor",
  1026. :class => "wiki-page new"),
  1027. }
  1028. @project = Project.find(1)
  1029. to_test.each do |text, result|
  1030. assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :anchor)
  1031. end
  1032. end
  1033. def test_html_tags
  1034. to_test = {
  1035. "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
  1036. "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
  1037. "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
  1038. # do not escape pre/code tags
  1039. "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
  1040. "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
  1041. "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
  1042. "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
  1043. "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
  1044. # remove attributes including class
  1045. "<pre class='foo'>some text</pre>" => "<pre>some text</pre>",
  1046. '<pre class="foo">some text</pre>' => '<pre>some text</pre>',
  1047. "<pre class='foo bar'>some text</pre>" => "<pre>some text</pre>",
  1048. '<pre class="foo bar">some text</pre>' => '<pre>some text</pre>',
  1049. "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
  1050. # xss
  1051. '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
  1052. '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
  1053. }
  1054. to_test.each { |text, result| assert_equal result, textilizable(text) }
  1055. end
  1056. def test_allowed_html_tags
  1057. to_test = {
  1058. "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
  1059. "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
  1060. "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
  1061. }
  1062. to_test.each { |text, result| assert_equal result, textilizable(text) }
  1063. end
  1064. def test_pre_tags
  1065. raw = <<~RAW
  1066. Before
  1067. <pre>
  1068. <prepared-statement-cache-size>32</prepared-statement-cache-size>
  1069. </pre>
  1070. After
  1071. RAW
  1072. expected = <<~EXPECTED
  1073. <p>Before</p>
  1074. <pre>
  1075. &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
  1076. </pre>
  1077. <p>After</p>
  1078. EXPECTED
  1079. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1080. end
  1081. def test_pre_content_should_not_parse_wiki_and_redmine_links
  1082. raw = <<~RAW
  1083. [[CookBook documentation]]
  1084. #1
  1085. <pre>
  1086. [[CookBook documentation]]
  1087. #1
  1088. </pre>
  1089. RAW
  1090. result1 = link_to("CookBook documentation",
  1091. "/projects/ecookbook/wiki/CookBook_documentation",
  1092. :class => "wiki-page")
  1093. result2 = link_to('#1',
  1094. "/issues/1",
  1095. :class => Issue.find(1).css_classes,
  1096. :title => "Bug: Cannot print recipes (New)")
  1097. expected = <<~EXPECTED
  1098. <p>#{result1}</p>
  1099. <p>#{result2}</p>
  1100. <pre>
  1101. [[CookBook documentation]]
  1102. #1
  1103. </pre>
  1104. EXPECTED
  1105. @project = Project.find(1)
  1106. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1107. end
  1108. def test_non_closing_pre_blocks_should_be_closed
  1109. raw = <<~RAW
  1110. <pre><code>
  1111. RAW
  1112. expected = <<~EXPECTED
  1113. <pre><code>
  1114. </code></pre>
  1115. EXPECTED
  1116. @project = Project.find(1)
  1117. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1118. end
  1119. def test_unbalanced_closing_pre_tag_should_not_error
  1120. assert_nothing_raised do
  1121. textilizable("unbalanced</pre>")
  1122. end
  1123. end
  1124. def test_syntax_highlight
  1125. raw = <<~RAW
  1126. <pre><code class="ECMA_script">
  1127. /* Hello */
  1128. document.write("Hello World!");
  1129. </code></pre>
  1130. RAW
  1131. expected = <<~EXPECTED
  1132. <pre><code class="ECMA_script syntaxhl"><span class="cm">/* Hello */</span><span class="nb">document</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="dl">"</span><span class="s2">Hello World!</span><span class="dl">"</span><span class="p">);</span></code></pre>
  1133. EXPECTED
  1134. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1135. end
  1136. def test_syntax_highlight_ampersand_in_textile
  1137. raw = <<~RAW
  1138. <pre><code class="ruby">
  1139. x = a & b
  1140. </code></pre>
  1141. RAW
  1142. expected = <<~EXPECTED
  1143. <pre><code class=\"ruby syntaxhl\"><span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"n\">a</span> <span class=\"o\">&amp;</span> <span class=\"n\">b</span></code></pre>
  1144. EXPECTED
  1145. with_settings :text_formatting => 'textile' do
  1146. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1147. end
  1148. end
  1149. def test_syntax_highlight_should_normalize_line_endings
  1150. assert_equal "line 1\nline 2\n", syntax_highlight("test.txt", "line 1\rline 2\r\n")
  1151. end
  1152. def test_to_path_param
  1153. assert_equal 'test1/test2', to_path_param('test1/test2')
  1154. assert_equal 'test1/test2', to_path_param('/test1/test2/')
  1155. assert_equal 'test1/test2', to_path_param('//test1/test2/')
  1156. assert_nil to_path_param('/')
  1157. end
  1158. def test_wiki_links_in_tables
  1159. text = "|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|"
  1160. link1 = link_to("Link title", "/projects/ecookbook/wiki/Page", :class => "wiki-page new")
  1161. link2 = link_to("Other title", "/projects/ecookbook/wiki/Other_Page", :class => "wiki-page new")
  1162. link3 = link_to("Last page", "/projects/ecookbook/wiki/Last_page", :class => "wiki-page new")
  1163. result = "<tr><td>#{link1}</td>" +
  1164. "<td>#{link2}</td>" +
  1165. "</tr><tr><td>Cell 21</td><td>#{link3}</td></tr>"
  1166. @project = Project.find(1)
  1167. assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '')
  1168. end
  1169. def test_text_formatting
  1170. to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
  1171. '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
  1172. 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
  1173. '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>',
  1174. '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',
  1175. }
  1176. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  1177. end
  1178. def test_wiki_horizontal_rule
  1179. assert_equal '<hr />', textilizable('---')
  1180. assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
  1181. end
  1182. def test_headings
  1183. raw = 'h1. Some heading'
  1184. expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
  1185. assert_equal expected, textilizable(raw)
  1186. end
  1187. def test_headings_with_special_chars
  1188. # This test makes sure that the generated anchor names match the expected
  1189. # ones even if the heading text contains unconventional characters
  1190. raw = 'h1. Some heading related to version 0.5'
  1191. anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5")
  1192. expected = %|<a name="#{anchor}"></a>\n<h1 >Some heading related to version 0.5<a href="##{anchor}" class="wiki-anchor">&para;</a></h1>|
  1193. assert_equal expected, textilizable(raw)
  1194. end
  1195. def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title
  1196. page = WikiPage.new(:title => 'Page Title', :wiki_id => 1)
  1197. content = WikiContent.new(:text => 'h1. Some heading', :page => page)
  1198. expected = %|<a name="Page_Title_Some-heading"></a>\n<h1 >Some heading<a href="#Page_Title_Some-heading" class="wiki-anchor">&para;</a></h1>|
  1199. assert_equal expected, textilizable(content, :text, :wiki_links => :anchor)
  1200. end
  1201. def test_table_of_content
  1202. set_language_if_valid 'en'
  1203. raw = <<~RAW
  1204. {{toc}}
  1205. h1. Title
  1206. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
  1207. h2. Subtitle with a [[Wiki]] link
  1208. Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
  1209. h2. Subtitle with [[Wiki|another Wiki]] link
  1210. h2. Subtitle with %{color:red}red text%
  1211. <pre>
  1212. some code
  1213. </pre>
  1214. h3. Subtitle with *some* _modifiers_
  1215. h3. Subtitle with @inline code@
  1216. h1. Another title
  1217. h3. An "Internet link":http://www.redmine.org/ inside subtitle
  1218. h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
  1219. RAW
  1220. expected = '<ul class="toc">' +
  1221. '<li><strong>Table of contents</strong></li>' +
  1222. '<li><a href="#Title">Title</a>' +
  1223. '<ul>' +
  1224. '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
  1225. '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
  1226. '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
  1227. '<ul>' +
  1228. '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
  1229. '<li><a href="#Subtitle-with-inline-code">Subtitle with inline code</a></li>' +
  1230. '</ul>' +
  1231. '</li>' +
  1232. '</ul>' +
  1233. '</li>' +
  1234. '<li><a href="#Another-title">Another title</a>' +
  1235. '<ul>' +
  1236. '<li>' +
  1237. '<ul>' +
  1238. '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
  1239. '</ul>' +
  1240. '</li>' +
  1241. '<li><a href="#Project-Name">Project Name</a></li>' +
  1242. '</ul>' +
  1243. '</li>' +
  1244. '</ul>'
  1245. @project = Project.find(1)
  1246. assert textilizable(raw).gsub("\n", "").include?(expected)
  1247. end
  1248. def test_table_of_content_should_generate_unique_anchors
  1249. set_language_if_valid 'en'
  1250. raw = <<~RAW
  1251. {{toc}}
  1252. h1. Title
  1253. h2. Subtitle
  1254. h2. Subtitle
  1255. RAW
  1256. expected = '<ul class="toc">' +
  1257. '<li><strong>Table of contents</strong></li>' +
  1258. '<li><a href="#Title">Title</a>' +
  1259. '<ul>' +
  1260. '<li><a href="#Subtitle">Subtitle</a></li>' +
  1261. '<li><a href="#Subtitle-2">Subtitle</a></li>' +
  1262. '</ul>' +
  1263. '</li>' +
  1264. '</ul>'
  1265. @project = Project.find(1)
  1266. result = textilizable(raw).gsub("\n", "")
  1267. assert_include expected, result
  1268. assert_include '<a name="Subtitle">', result
  1269. assert_include '<a name="Subtitle-2">', result
  1270. end
  1271. def test_table_of_content_should_contain_included_page_headings
  1272. set_language_if_valid 'en'
  1273. raw = <<~RAW
  1274. {{toc}}
  1275. h1. Included
  1276. {{include(Child_1)}}
  1277. RAW
  1278. expected = '<ul class="toc">' +
  1279. '<li><strong>Table of contents</strong></li>' +
  1280. '<li><a href="#Included">Included</a></li>' +
  1281. '<li><a href="#Child-page-1">Child page 1</a></li>' +
  1282. '</ul>'
  1283. @project = Project.find(1)
  1284. assert textilizable(raw).gsub("\n", "").include?(expected)
  1285. end
  1286. def test_toc_with_textile_formatting_should_be_parsed
  1287. with_settings :text_formatting => 'textile' do
  1288. assert_select_in textilizable("{{toc}}\n\nh1. Heading"), 'ul.toc li', :text => 'Heading'
  1289. assert_select_in textilizable("{{<toc}}\n\nh1. Heading"), 'ul.toc.left li', :text => 'Heading'
  1290. assert_select_in textilizable("{{>toc}}\n\nh1. Heading"), 'ul.toc.right li', :text => 'Heading'
  1291. end
  1292. end
  1293. if Object.const_defined?(:Redcarpet)
  1294. def test_toc_with_markdown_formatting_should_be_parsed
  1295. with_settings :text_formatting => 'markdown' do
  1296. assert_select_in textilizable("{{toc}}\n\n# Heading"), 'ul.toc li', :text => 'Heading'
  1297. assert_select_in textilizable("{{<toc}}\n\n# Heading"), 'ul.toc.left li', :text => 'Heading'
  1298. assert_select_in textilizable("{{>toc}}\n\n# Heading"), 'ul.toc.right li', :text => 'Heading'
  1299. end
  1300. end
  1301. end
  1302. def test_section_edit_links
  1303. raw = <<~RAW
  1304. h1. Title
  1305. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
  1306. h2. Subtitle with a [[Wiki]] link
  1307. h2. Subtitle with *some* _modifiers_
  1308. h2. Subtitle with @inline code@
  1309. <pre>
  1310. some code
  1311. h2. heading inside pre
  1312. <h2>html heading inside pre</h2>
  1313. </pre>
  1314. h2. Subtitle after pre tag
  1315. RAW
  1316. @project = Project.find(1)
  1317. set_language_if_valid 'en'
  1318. result = textilizable(
  1319. raw,
  1320. :edit_section_links =>
  1321. {:controller => 'wiki', :action => 'edit',
  1322. :project_id => '1', :id => 'Test'}
  1323. ).gsub("\n", "")
  1324. # heading that contains inline code
  1325. assert_match(
  1326. Regexp.new('<div class="contextual heading-2" title="Edit this section" id="section-4">' +
  1327. '<a class="icon-only icon-edit" href="/projects/1/wiki/Test/edit\?section=4">Edit this section</a></div>' +
  1328. '<a name="Subtitle-with-inline-code"></a>' +
  1329. '<h2 >Subtitle with <code>inline code</code><a href="#Subtitle-with-inline-code" class="wiki-anchor">&para;</a></h2>'),
  1330. result)
  1331. # last heading
  1332. assert_match(
  1333. Regexp.new('<div class="contextual heading-2" title="Edit this section" id="section-5">' +
  1334. '<a class="icon-only icon-edit" href="/projects/1/wiki/Test/edit\?section=5">Edit this section</a></div>' +
  1335. '<a name="Subtitle-after-pre-tag"></a>' +
  1336. '<h2 >Subtitle after pre tag<a href="#Subtitle-after-pre-tag" class="wiki-anchor">&para;</a></h2>'),
  1337. result)
  1338. end
  1339. def test_default_formatter
  1340. with_settings :text_formatting => 'unknown' do
  1341. text = 'a *link*: http://www.example.net/'
  1342. assert_equal '<p>a *link*: <a class="external" href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
  1343. end
  1344. end
  1345. def test_textilizable_with_formatting_set_to_false_should_not_format_text
  1346. assert_equal '*text*', textilizable("*text*", :formatting => false)
  1347. end
  1348. def test_textilizable_with_formatting_set_to_true_should_format_text
  1349. assert_equal '<p><strong>text</strong></p>', textilizable("*text*", :formatting => true)
  1350. end
  1351. def test_parse_redmine_links_should_handle_a_tag_without_attributes
  1352. text = +'<a>http://example.com</a>'
  1353. expected = text.dup
  1354. parse_redmine_links(text, nil, nil, nil, true, {})
  1355. assert_equal expected, text
  1356. end
  1357. def test_due_date_distance_in_words
  1358. to_test = { Date.today => 'Due in 0 days',
  1359. Date.today + 1 => 'Due in 1 day',
  1360. Date.today + 100 => 'Due in about 3 months',
  1361. Date.today + 20000 => 'Due in over 54 years',
  1362. Date.today - 1 => '1 day late',
  1363. Date.today - 100 => 'about 3 months late',
  1364. Date.today - 20000 => 'over 54 years late',
  1365. }
  1366. ::I18n.locale = :en
  1367. to_test.each do |date, expected|
  1368. assert_equal expected, due_date_distance_in_words(date)
  1369. end
  1370. end
  1371. def test_render_page_hierarchy
  1372. parent_page = WikiPage.find(1)
  1373. child_page = WikiPage.find_by(parent_id: parent_page.id)
  1374. pages_by_parent_id = { nil => [parent_page], parent_page.id => [child_page] }
  1375. result = render_page_hierarchy(pages_by_parent_id, nil)
  1376. assert_select_in(
  1377. result, 'ul.pages-hierarchy li a[href=?]',
  1378. project_wiki_page_path(project_id: parent_page.project,
  1379. id: parent_page.title, version: nil))
  1380. assert_select_in(
  1381. result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]',
  1382. project_wiki_page_path(project_id: child_page.project,
  1383. id: child_page.title, version: nil))
  1384. end
  1385. def test_render_page_hierarchy_with_timestamp
  1386. parent_page = WikiPage.find(1)
  1387. child_page = WikiPage.find_by(parent_id: parent_page.id)
  1388. pages_by_parent_id = { nil => [parent_page], parent_page.id => [child_page] }
  1389. result = render_page_hierarchy(pages_by_parent_id, nil, :timestamp => true)
  1390. assert_select_in(
  1391. result, 'ul.pages-hierarchy li a[title=?]',
  1392. l(:label_updated_time,
  1393. distance_of_time_in_words(Time.now, parent_page.updated_on)))
  1394. assert_select_in(
  1395. result, 'ul.pages-hierarchy li ul.pages-hierarchy a[title=?]',
  1396. l(:label_updated_time,
  1397. distance_of_time_in_words(Time.now, child_page.updated_on)))
  1398. end
  1399. def test_render_page_hierarchy_when_action_is_export
  1400. parent_page = WikiPage.find(1)
  1401. child_page = WikiPage.find_by(parent_id: parent_page.id)
  1402. pages_by_parent_id = { nil => [parent_page], parent_page.id => [child_page] }
  1403. # Change controller and action using stub
  1404. controller.stubs(:controller_name).returns('wiki')
  1405. controller.stubs(:action_name).returns("export")
  1406. result = render_page_hierarchy(pages_by_parent_id, nil)
  1407. assert_select_in result, 'ul.pages-hierarchy li a[href=?]', "##{parent_page.title}"
  1408. assert_select_in result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]', "##{child_page.title}"
  1409. end
  1410. def test_link_to_user
  1411. user = User.find(2)
  1412. result = link_to("John Smith", "/users/2", :class => "user active")
  1413. assert_equal result, link_to_user(user)
  1414. end
  1415. def test_link_to_user_should_not_link_to_locked_user
  1416. with_current_user nil do
  1417. user = User.find(5)
  1418. assert user.locked?
  1419. assert_equal 'Dave2 Lopper2', link_to_user(user)
  1420. end
  1421. end
  1422. def test_link_to_user_should_link_to_locked_user_if_current_user_is_admin
  1423. with_current_user User.find(1) do
  1424. user = User.find(5)
  1425. assert user.locked?
  1426. result = link_to("Dave2 Lopper2", "/users/5", :class => "user locked")
  1427. assert_equal result, link_to_user(user)
  1428. end
  1429. end
  1430. def test_link_to_group_should_return_only_group_name_for_non_admin_users
  1431. User.current = nil
  1432. group = Group.find(10)
  1433. assert_equal "A Team", link_to_group(group)
  1434. end
  1435. def test_link_to_group_should_link_to_group_edit_page_for_admin_users
  1436. User.current = User.find(1)
  1437. group = Group.find(10)
  1438. result = link_to("A Team", "/groups/10/edit")
  1439. assert_equal result, link_to_group(group)
  1440. end
  1441. def test_link_to_user_should_not_link_to_anonymous
  1442. user = User.anonymous
  1443. assert user.anonymous?
  1444. t = link_to_user(user)
  1445. assert_equal ::I18n.t(:label_user_anonymous), t
  1446. end
  1447. def test_link_to_attachment
  1448. a = Attachment.find(3)
  1449. assert_equal(
  1450. '<a href="/attachments/3">logo.gif</a>',
  1451. link_to_attachment(a))
  1452. assert_equal(
  1453. '<a href="/attachments/3">Text</a>',
  1454. link_to_attachment(a, :text => 'Text'))
  1455. result = link_to("logo.gif", "/attachments/3", :class => "foo")
  1456. assert_equal(
  1457. result,
  1458. link_to_attachment(a, :class => 'foo'))
  1459. assert_equal(
  1460. '<a href="/attachments/download/3/logo.gif">logo.gif</a>',
  1461. link_to_attachment(a, :download => true))
  1462. assert_equal(
  1463. '<a href="http://test.host/attachments/3">logo.gif</a>',
  1464. link_to_attachment(a, :only_path => false))
  1465. end
  1466. def test_thumbnail_tag
  1467. a = Attachment.find(3)
  1468. assert_select_in(
  1469. thumbnail_tag(a),
  1470. 'a[href=?][title=?] img[src=?]',
  1471. "/attachments/3", "logo.gif", "/attachments/thumbnail/3")
  1472. end
  1473. def test_link_to_project
  1474. project = Project.find(1)
  1475. assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
  1476. link_to_project(project)
  1477. assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
  1478. link_to_project(project, {:only_path => false, :jump => 'blah'})
  1479. end
  1480. def test_link_to_project_settings
  1481. project = Project.find(1)
  1482. assert_equal '<a href="/projects/ecookbook/settings">eCookbook</a>', link_to_project_settings(project)
  1483. project.status = Project::STATUS_CLOSED
  1484. assert_equal '<a href="/projects/ecookbook">eCookbook</a>', link_to_project_settings(project)
  1485. project.status = Project::STATUS_ARCHIVED
  1486. assert_equal 'eCookbook', link_to_project_settings(project)
  1487. end
  1488. def test_link_to_legacy_project_with_numerical_identifier_should_use_id
  1489. # numeric identifier are no longer allowed
  1490. Project.where(:id => 1).update_all(:identifier => 25)
  1491. assert_equal '<a href="/projects/1">eCookbook</a>',
  1492. link_to_project(Project.find(1))
  1493. end
  1494. def test_link_to_record
  1495. [
  1496. [custom_values(:custom_values_007), '<a href="/projects/ecookbook">eCookbook</a>'],
  1497. [documents(:documents_001), '<a href="/documents/1">Test document</a>'],
  1498. [Group.find(10), '<a href="/groups/10">A Team</a>'],
  1499. [issues(:issues_001), link_to_issue(issues(:issues_001), :subject => false)],
  1500. [messages(:messages_001), '<a href="/boards/1/topics/1">First post</a>'],
  1501. [news(:news_001), '<a href="/news/1">eCookbook first release !</a>'],
  1502. [projects(:projects_001), '<a href="/projects/ecookbook">eCookbook</a>'],
  1503. [users(:users_001), '<a class="user active" href="/users/1">Redmine Admin</a>'],
  1504. [versions(:versions_001), '<a title="07/01/2006" href="/versions/1">eCookbook - 0.1</a>'],
  1505. [wiki_pages(:wiki_pages_001), '<a href="/projects/ecookbook/wiki/CookBook_documentation">CookBook documentation</a>']
  1506. ].each do |record, link|
  1507. assert_equal link, link_to_record(record)
  1508. end
  1509. end
  1510. def test_link_to_attachment_container
  1511. field = ProjectCustomField.generate!(:name => "File", :field_format => 'attachment')
  1512. project = projects(:projects_001)
  1513. project_custom_value_attachment = new_record(Attachment) do
  1514. project.custom_field_values = {field.id => {:file => mock_file}}
  1515. project.save
  1516. end
  1517. news_attachment = attachments(:attachments_004)
  1518. news_attachment.container = news(:news_001)
  1519. news_attachment.save!
  1520. [
  1521. [project_custom_value_attachment, '<a href="/projects/ecookbook">eCookbook</a>'],
  1522. [attachments(:attachments_002), '<a href="/documents/1">Test document</a>'],
  1523. [attachments(:attachments_001), link_to_issue(issues(:issues_003), :subject => false)],
  1524. [attachments(:attachments_013), '<a href="/boards/1/topics/1">First post</a>'],
  1525. [news_attachment, '<a href="/news/1">eCookbook first release !</a>'],
  1526. [attachments(:attachments_008), '<a href="/projects/ecookbook/files">Files</a>'],
  1527. [attachments(:attachments_009), '<a href="/projects/ecookbook/files">Files</a>'],
  1528. [attachments(:attachments_003), '<a href="/projects/ecookbook/wiki/Page_with_an_inline_image">Page with an inline image</a>'],
  1529. ].each do |attachment, link|
  1530. assert_equal link, link_to_attachment_container(attachment.container)
  1531. end
  1532. end
  1533. def test_principals_check_box_tag_with_avatar
  1534. principals = [User.find(1), Group.find(10)]
  1535. with_settings :gravatar_enabled => '1' do
  1536. tags = principals_check_box_tags("watcher[user_ids][]", principals)
  1537. principals.each do |principal|
  1538. assert_include avatar(principal, :size => 16), tags
  1539. assert_not_include content_tag('span', nil, :class => "name icon icon-#{principal.class.name.downcase}"), tags
  1540. end
  1541. end
  1542. end
  1543. def test_principals_check_box_tag_without_avatar
  1544. principals = [User.find(1), Group.find(10)]
  1545. Setting.gravatar_enabled = '1'
  1546. avatar_tags = principals.collect{|p| avatar(p, :size => 16) }
  1547. with_settings :gravatar_enabled => '0' do
  1548. tags = principals_check_box_tags(name, principals)
  1549. principals.each_with_index do |principal, i|
  1550. assert_not_include avatar_tags[i], tags
  1551. assert_include content_tag('span', nil, :class => "name icon icon-#{principal.class.name.downcase}"), tags
  1552. end
  1553. end
  1554. end
  1555. def test_principals_options_for_select_with_users
  1556. User.current = nil
  1557. users = [User.find(2), User.find(4)]
  1558. assert_equal(
  1559. %(<option value="2">John Smith</option><option value="4">Robert Hill</option>),
  1560. principals_options_for_select(users))
  1561. end
  1562. def test_principals_options_for_select_with_selected
  1563. User.current = nil
  1564. users = [User.find(2), User.find(4)]
  1565. assert_equal(
  1566. %(<option value="2">John Smith</option><option value="4" selected="selected">Robert Hill</option>),
  1567. principals_options_for_select(users, User.find(4)))
  1568. end
  1569. def test_principals_options_for_select_with_users_and_groups
  1570. User.current = nil
  1571. set_language_if_valid 'en'
  1572. users = [User.find(2), Group.find(11), User.find(4), Group.find(10)]
  1573. assert_equal(
  1574. %(<option value="2">John Smith</option><option value="4">Robert Hill</option>) +
  1575. %(<optgroup label="Groups"><option value="10">A Team</option><option value="11">B Team</option></optgroup>),
  1576. principals_options_for_select(users))
  1577. end
  1578. def test_principals_options_for_select_with_empty_collection
  1579. assert_equal '', principals_options_for_select([])
  1580. end
  1581. def test_principals_options_for_select_should_include_me_option_when_current_user_is_in_collection
  1582. set_language_if_valid 'en'
  1583. users = [User.find(2), User.find(4)]
  1584. User.current = User.find(4)
  1585. assert_include '<option value="4">&lt;&lt; me &gt;&gt;</option>', principals_options_for_select(users)
  1586. end
  1587. def test_stylesheet_link_tag_should_pick_the_default_stylesheet
  1588. assert_match 'href="/stylesheets/styles.css"', stylesheet_link_tag("styles")
  1589. end
  1590. def test_stylesheet_link_tag_for_plugin_should_pick_the_plugin_stylesheet
  1591. assert_match 'href="/plugin_assets/foo/stylesheets/styles.css"', stylesheet_link_tag("styles", :plugin => :foo)
  1592. end
  1593. def test_image_tag_should_pick_the_default_image
  1594. assert_match 'src="/images/image.png"', image_tag("image.png")
  1595. end
  1596. def test_image_tag_should_pick_the_theme_image_if_it_exists
  1597. theme = Redmine::Themes.themes.last
  1598. theme.images << 'image.png'
  1599. with_settings :ui_theme => theme.id do
  1600. assert_match %|src="/themes/#{theme.dir}/images/image.png"|, image_tag("image.png")
  1601. assert_match %|src="/images/other.png"|, image_tag("other.png")
  1602. end
  1603. ensure
  1604. theme.images.delete 'image.png'
  1605. end
  1606. def test_image_tag_sfor_plugin_should_pick_the_plugin_image
  1607. assert_match 'src="/plugin_assets/foo/images/image.png"', image_tag("image.png", :plugin => :foo)
  1608. end
  1609. def test_javascript_include_tag_should_pick_the_default_javascript
  1610. assert_match 'src="/javascripts/scripts.js"', javascript_include_tag("scripts")
  1611. end
  1612. def test_javascript_include_tag_for_plugin_should_pick_the_plugin_javascript
  1613. assert_match 'src="/plugin_assets/foo/javascripts/scripts.js"', javascript_include_tag("scripts", :plugin => :foo)
  1614. end
  1615. def test_raw_json_should_escape_closing_tags
  1616. s = raw_json(["<foo>bar</foo>"])
  1617. assert_include '\/foo', s
  1618. end
  1619. def test_raw_json_should_be_html_safe
  1620. s = raw_json(["foo"])
  1621. assert s.html_safe?
  1622. end
  1623. def test_html_title_should_app_title_if_not_set
  1624. assert_equal 'Redmine', html_title
  1625. end
  1626. def test_html_title_should_join_items
  1627. html_title 'Foo', 'Bar'
  1628. assert_equal 'Foo - Bar - Redmine', html_title
  1629. end
  1630. def test_html_title_should_append_current_project_name
  1631. @project = Project.find(1)
  1632. html_title 'Foo', 'Bar'
  1633. assert_equal 'Foo - Bar - eCookbook - Redmine', html_title
  1634. end
  1635. def test_title_should_return_a_h2_tag
  1636. assert_equal '<h2>Foo</h2>', title('Foo')
  1637. end
  1638. def test_title_should_set_html_title
  1639. title('Foo')
  1640. assert_equal 'Foo - Redmine', html_title
  1641. end
  1642. def test_title_should_turn_arrays_into_links
  1643. assert_equal '<h2><a href="/foo">Foo</a></h2>', title(['Foo', '/foo'])
  1644. assert_equal 'Foo - Redmine', html_title
  1645. end
  1646. def test_title_should_join_items
  1647. assert_equal '<h2>Foo &#187; Bar</h2>', title('Foo', 'Bar')
  1648. assert_equal 'Bar - Foo - Redmine', html_title
  1649. end
  1650. def test_favicon_path
  1651. assert_match %r{^/favicon\.ico}, favicon_path
  1652. end
  1653. def test_favicon_path_with_suburi
  1654. Redmine::Utils.relative_url_root = '/foo'
  1655. assert_match %r{^/foo/favicon\.ico}, favicon_path
  1656. ensure
  1657. Redmine::Utils.relative_url_root = ''
  1658. end
  1659. def test_favicon_url
  1660. assert_match %r{^http://test\.host/favicon\.ico}, favicon_url
  1661. end
  1662. def test_favicon_url_with_suburi
  1663. Redmine::Utils.relative_url_root = '/foo'
  1664. assert_match %r{^http://test\.host/foo/favicon\.ico}, favicon_url
  1665. ensure
  1666. Redmine::Utils.relative_url_root = ''
  1667. end
  1668. def test_truncate_single_line
  1669. str = "01234"
  1670. result = truncate_single_line_raw("#{str}\n#{str}", 10)
  1671. assert_equal "01234 0...", result
  1672. assert !result.html_safe?
  1673. result = truncate_single_line_raw("#{str}<&#>\n#{str}#{str}", 16)
  1674. assert_equal "01234<&#> 012...", result
  1675. assert !result.html_safe?
  1676. end
  1677. def test_truncate_single_line_non_ascii
  1678. ja = '日本語'
  1679. result = truncate_single_line_raw("#{ja}\n#{ja}\n#{ja}", 10)
  1680. assert_equal "#{ja} #{ja}...", result
  1681. assert !result.html_safe?
  1682. end
  1683. def test_hours_formatting
  1684. set_language_if_valid 'en'
  1685. with_settings :timespan_format => 'minutes' do
  1686. assert_equal '0:45', format_hours(0.75)
  1687. assert_equal '0:45 h', l_hours_short(0.75)
  1688. assert_equal '0:45 hour', l_hours(0.75)
  1689. end
  1690. with_settings :timespan_format => 'decimal' do
  1691. assert_equal '0.75', format_hours(0.75)
  1692. assert_equal '0.75 h', l_hours_short(0.75)
  1693. assert_equal '0.75 hour', l_hours(0.75)
  1694. end
  1695. end
  1696. def test_html_hours
  1697. assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">:45</span>', html_hours('0:45')
  1698. assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">.75</span>', html_hours('0.75')
  1699. end
  1700. def test_form_for_includes_name_attribute
  1701. assert_match(/name="new_issue-[a-z0-9]{8}"/, form_for(Issue.new){})
  1702. end
  1703. def test_labelled_form_for_includes_name_attribute
  1704. assert_match(/name="new_issue-[a-z0-9]{8}"/, labelled_form_for(Issue.new){})
  1705. end
  1706. def test_redner_if_exist_should_be_render_partial
  1707. controller.prepend_view_path "test/fixtures/views"
  1708. assert_equal "partial html\n", render_if_exist(:partial => 'partial')
  1709. end
  1710. def test_redner_if_exist_should_be_render_nil
  1711. controller.prepend_view_path "test/fixtures/views"
  1712. assert_nil render_if_exist(:partial => 'non_exist_partial')
  1713. end
  1714. private
  1715. def wiki_links_with_special_characters
  1716. return {
  1717. '[[Jack & Coke]]' =>
  1718. link_to("Jack & Coke",
  1719. "/projects/ecookbook/wiki/Jack_&_Coke",
  1720. :class => "wiki-page new"),
  1721. '[[a "quoted" name]]' =>
  1722. link_to("a \"quoted\" name",
  1723. "/projects/ecookbook/wiki/A_%22quoted%22_name",
  1724. :class => "wiki-page new"),
  1725. '[[le français, c\'est super]]' =>
  1726. link_to("le français, c\'est super",
  1727. "/projects/ecookbook/wiki/Le_fran%C3%A7ais_c'est_super",
  1728. :class => "wiki-page new"),
  1729. '[[broken < less]]' =>
  1730. link_to("broken < less",
  1731. "/projects/ecookbook/wiki/Broken_%3C_less",
  1732. :class => "wiki-page new"),
  1733. '[[broken > more]]' =>
  1734. link_to("broken > more",
  1735. "/projects/ecookbook/wiki/Broken_%3E_more",
  1736. :class => "wiki-page new"),
  1737. '[[[foo]Including [square brackets] in wiki title]]' =>
  1738. link_to("[foo]Including [square brackets] in wiki title",
  1739. "/projects/ecookbook/wiki/%5Bfoo%5DIncluding_%5Bsquare_brackets%5D_in_wiki_title",
  1740. :class => "wiki-page new"),
  1741. }
  1742. end
  1743. def test_export_csv_encoding_select_tag_should_return_nil_when_general_csv_encoding_is_UTF8
  1744. with_locale 'az' do
  1745. assert_equal l(:general_csv_encoding), 'UTF-8'
  1746. assert_nil export_csv_encoding_select_tag
  1747. end
  1748. end
  1749. def test_export_csv_encoding_select_tag_should_have_two_option_when_general_csv_encoding_is_not_UTF8
  1750. with_locale 'en' do
  1751. assert_not_equal l(:general_csv_encoding), 'UTF-8'
  1752. result = export_csv_encoding_select_tag
  1753. assert_select_in result, "option[selected='selected'][value=#{l(:general_csv_encoding)}]", :text => l(:general_csv_encoding)
  1754. assert_select_in result, "option[value='UTF-8']", :text => 'UTF-8'
  1755. end
  1756. end
  1757. end