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

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