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

application_helper_test.rb 95KB

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