Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

application_helper_test.rb 86KB

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