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

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