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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  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=\"bold\"&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>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
  1221. "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
  1222. # remove attributes including class
  1223. "<pre class='foo'>some text</pre>" => "<pre>some text</pre>",
  1224. '<pre class="foo">some text</pre>' => '<pre>some text</pre>',
  1225. "<pre class='foo bar'>some text</pre>" => "<pre>some text</pre>",
  1226. '<pre class="foo bar">some text</pre>' => '<pre>some text</pre>',
  1227. "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
  1228. # xss
  1229. '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
  1230. '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
  1231. }
  1232. with_settings :text_formatting => 'textile' do
  1233. to_test.each {|text, result| assert_equal result, textilizable(text)}
  1234. end
  1235. end
  1236. def test_allowed_html_tags
  1237. to_test = {
  1238. "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
  1239. "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
  1240. "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
  1241. }
  1242. with_settings :text_formatting => 'textile' do
  1243. to_test.each {|text, result| assert_equal result, textilizable(text)}
  1244. end
  1245. end
  1246. def test_pre_tags
  1247. raw = <<~RAW
  1248. Before
  1249. <pre>
  1250. <prepared-statement-cache-size>32</prepared-statement-cache-size>
  1251. </pre>
  1252. After
  1253. RAW
  1254. expected = <<~EXPECTED
  1255. <p>Before</p>
  1256. <pre>
  1257. &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
  1258. </pre>
  1259. <p>After</p>
  1260. EXPECTED
  1261. with_settings :text_formatting => 'textile' do
  1262. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1263. end
  1264. end
  1265. def test_pre_content_should_not_parse_wiki_and_redmine_links
  1266. raw = <<~RAW
  1267. [[CookBook documentation]]
  1268. #1
  1269. <pre>
  1270. [[CookBook documentation]]
  1271. #1
  1272. </pre>
  1273. RAW
  1274. result1 = link_to("CookBook documentation",
  1275. "/projects/ecookbook/wiki/CookBook_documentation",
  1276. :class => "wiki-page")
  1277. result2 = link_to('#1',
  1278. "/issues/1",
  1279. :class => Issue.find(1).css_classes,
  1280. :title => "Bug: Cannot print recipes (New)")
  1281. expected = <<~EXPECTED
  1282. <p>#{result1}</p>
  1283. <p>#{result2}</p>
  1284. <pre>
  1285. [[CookBook documentation]]
  1286. #1
  1287. </pre>
  1288. EXPECTED
  1289. @project = Project.find(1)
  1290. with_settings :text_formatting => 'textile' do
  1291. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1292. end
  1293. end
  1294. def test_non_closing_pre_blocks_should_be_closed
  1295. raw = <<~RAW
  1296. <pre><code>
  1297. RAW
  1298. expected = <<~EXPECTED
  1299. <pre><code>
  1300. </code></pre>
  1301. EXPECTED
  1302. @project = Project.find(1)
  1303. with_settings :text_formatting => 'textile' do
  1304. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1305. end
  1306. end
  1307. def test_unbalanced_closing_pre_tag_should_not_error
  1308. assert_nothing_raised do
  1309. textilizable("unbalanced</pre>")
  1310. end
  1311. end
  1312. def test_syntax_highlight
  1313. raw = <<~RAW
  1314. <pre><code class="ECMA_script">
  1315. /* Hello */
  1316. document.write("Hello World!");
  1317. </code></pre>
  1318. RAW
  1319. expected = <<~EXPECTED
  1320. <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="nx">write</span><span class="p">(</span><span class="dl">"</span><span class="s2">Hello World!</span><span class="dl">"</span><span class="p">);</span></code></pre>
  1321. EXPECTED
  1322. with_settings :text_formatting => 'textile' do
  1323. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1324. end
  1325. end
  1326. def test_syntax_highlight_ampersand_in_textile
  1327. raw = <<~RAW
  1328. <pre><code class="ruby">
  1329. x = a & b
  1330. </code></pre>
  1331. RAW
  1332. expected = <<~EXPECTED
  1333. <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>
  1334. EXPECTED
  1335. with_settings :text_formatting => 'textile' do
  1336. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
  1337. end
  1338. end
  1339. def test_syntax_highlight_should_normalize_line_endings
  1340. assert_equal "line 1\nline 2\n", syntax_highlight("test.txt", "line 1\rline 2\r\n")
  1341. end
  1342. def test_to_path_param
  1343. assert_equal 'test1/test2', to_path_param('test1/test2')
  1344. assert_equal 'test1/test2', to_path_param('/test1/test2/')
  1345. assert_equal 'test1/test2', to_path_param('//test1/test2/')
  1346. assert_nil to_path_param('/')
  1347. end
  1348. def test_wiki_links_in_tables
  1349. text = "|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|"
  1350. link1 = link_to("Link title", "/projects/ecookbook/wiki/Page", :class => "wiki-page new")
  1351. link2 = link_to("Other title", "/projects/ecookbook/wiki/Other_Page", :class => "wiki-page new")
  1352. link3 = link_to("Last page", "/projects/ecookbook/wiki/Last_page", :class => "wiki-page new")
  1353. result = "<tr><td>#{link1}</td>" +
  1354. "<td>#{link2}</td>" +
  1355. "</tr><tr><td>Cell 21</td><td>#{link3}</td></tr>"
  1356. @project = Project.find(1)
  1357. with_settings :text_formatting => 'textile' do
  1358. assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '')
  1359. end
  1360. end
  1361. def test_text_formatting
  1362. to_test = {
  1363. '*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
  1364. '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
  1365. 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
  1366. 'a H *umane* W *eb* T *ext* G *enerator*' =>
  1367. 'a H <strong>umane</strong> W <strong>eb</strong> T <strong>ext</strong> G <strong>enerator</strong>',
  1368. 'a *H* umane *W* eb *T* ext *G* enerator' =>
  1369. 'a <strong>H</strong> umane <strong>W</strong> eb <strong>T</strong> ext <strong>G</strong> enerator',
  1370. }
  1371. with_settings :text_formatting => 'textile' do
  1372. to_test.each {|text, result| assert_equal "<p>#{result}</p>", textilizable(text)}
  1373. end
  1374. end
  1375. def test_wiki_horizontal_rule
  1376. with_settings :text_formatting => 'textile' do
  1377. assert_equal '<hr />', textilizable('---')
  1378. assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
  1379. end
  1380. end
  1381. def test_headings
  1382. raw = 'h1. Some heading'
  1383. expected =
  1384. %|<a name="Some-heading"></a>\n<h1 >Some heading| +
  1385. %|<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
  1386. with_settings :text_formatting => 'textile' do
  1387. assert_equal expected, textilizable(raw)
  1388. end
  1389. end
  1390. def test_headings_with_special_chars
  1391. # This test makes sure that the generated anchor names match the expected
  1392. # ones even if the heading text contains unconventional characters
  1393. raw = 'h1. Some heading related to version 0.5'
  1394. anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5")
  1395. expected =
  1396. %|<a name="#{anchor}"></a>\n<h1 >Some heading related to version 0.5| +
  1397. %|<a href="##{anchor}" class="wiki-anchor">&para;</a></h1>|
  1398. with_settings :text_formatting => 'textile' do
  1399. assert_equal expected, textilizable(raw)
  1400. end
  1401. end
  1402. def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title
  1403. page = WikiPage.new(:title => 'Page Title', :wiki_id => 1)
  1404. content = WikiContent.new(:text => 'h1. Some heading', :page => page)
  1405. expected =
  1406. %|<a name="Page_Title_Some-heading"></a>\n<h1 >Some heading| +
  1407. %|<a href="#Page_Title_Some-heading" class="wiki-anchor">&para;</a></h1>|
  1408. with_settings :text_formatting => 'textile' do
  1409. assert_equal expected, textilizable(content, :text, :wiki_links => :anchor)
  1410. end
  1411. end
  1412. def test_table_of_content
  1413. set_language_if_valid 'en'
  1414. raw = <<~RAW
  1415. {{toc}}
  1416. h1. Title
  1417. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
  1418. h2. Subtitle with a [[Wiki]] link
  1419. Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
  1420. h2. Subtitle with [[Wiki|another Wiki]] link
  1421. h2. Subtitle with %{color:red}red text%
  1422. <pre>
  1423. some code
  1424. </pre>
  1425. h3. Subtitle with *some* _modifiers_
  1426. h3. Subtitle with @inline code@
  1427. h1. Another title
  1428. h3. An "Internet link":http://www.redmine.org/ inside subtitle
  1429. h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
  1430. RAW
  1431. expected = '<ul class="toc">' +
  1432. '<li><strong>Table of contents</strong></li>' +
  1433. '<li><a href="#Title">Title</a>' +
  1434. '<ul>' +
  1435. '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
  1436. '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
  1437. '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
  1438. '<ul>' +
  1439. '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
  1440. '<li><a href="#Subtitle-with-inline-code">Subtitle with inline code</a></li>' +
  1441. '</ul>' +
  1442. '</li>' +
  1443. '</ul>' +
  1444. '</li>' +
  1445. '<li><a href="#Another-title">Another title</a>' +
  1446. '<ul>' +
  1447. '<li>' +
  1448. '<ul>' +
  1449. '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
  1450. '</ul>' +
  1451. '</li>' +
  1452. '<li><a href="#Project-Name">Project Name</a></li>' +
  1453. '</ul>' +
  1454. '</li>' +
  1455. '</ul>'
  1456. @project = Project.find(1)
  1457. with_settings :text_formatting => 'textile' do
  1458. assert textilizable(raw).delete("\n").include?(expected)
  1459. end
  1460. end
  1461. def test_table_of_content_should_generate_unique_anchors
  1462. set_language_if_valid 'en'
  1463. raw = <<~RAW
  1464. {{toc}}
  1465. h1. Title
  1466. h2. Subtitle
  1467. h2. Subtitle
  1468. RAW
  1469. expected = '<ul class="toc">' +
  1470. '<li><strong>Table of contents</strong></li>' +
  1471. '<li><a href="#Title">Title</a>' +
  1472. '<ul>' +
  1473. '<li><a href="#Subtitle">Subtitle</a></li>' +
  1474. '<li><a href="#Subtitle-2">Subtitle</a></li>' +
  1475. '</ul>' +
  1476. '</li>' +
  1477. '</ul>'
  1478. @project = Project.find(1)
  1479. with_settings :text_formatting => 'textile' do
  1480. result = textilizable(raw).delete("\n")
  1481. assert_include expected, result
  1482. assert_include '<a name="Subtitle">', result
  1483. assert_include '<a name="Subtitle-2">', result
  1484. end
  1485. end
  1486. def test_table_of_content_should_contain_included_page_headings
  1487. set_language_if_valid 'en'
  1488. raw = <<~RAW
  1489. {{toc}}
  1490. h1. Included
  1491. {{include(Child_1)}}
  1492. RAW
  1493. expected = '<ul class="toc">' +
  1494. '<li><strong>Table of contents</strong></li>' +
  1495. '<li><a href="#Included">Included</a></li>' +
  1496. '<li><a href="#Child-page-1">Child page 1</a></li>' +
  1497. '</ul>'
  1498. @project = Project.find(1)
  1499. with_settings :text_formatting => 'textile' do
  1500. assert textilizable(raw).delete("\n").include?(expected)
  1501. end
  1502. end
  1503. def test_toc_with_textile_formatting_should_be_parsed
  1504. with_settings :text_formatting => 'textile' do
  1505. assert_select_in textilizable("{{toc}}\n\nh1. Heading"), 'ul.toc li', :text => 'Heading'
  1506. assert_select_in textilizable("{{<toc}}\n\nh1. Heading"), 'ul.toc.left li', :text => 'Heading'
  1507. assert_select_in textilizable("{{>toc}}\n\nh1. Heading"), 'ul.toc.right li', :text => 'Heading'
  1508. end
  1509. end
  1510. if Object.const_defined?(:Redcarpet)
  1511. def test_toc_with_markdown_formatting_should_be_parsed
  1512. with_settings :text_formatting => 'markdown' do
  1513. assert_select_in textilizable("{{toc}}\n\n# Heading"), 'ul.toc li', :text => 'Heading'
  1514. assert_select_in textilizable("{{<toc}}\n\n# Heading"), 'ul.toc.left li', :text => 'Heading'
  1515. assert_select_in textilizable("{{>toc}}\n\n# Heading"), 'ul.toc.right li', :text => 'Heading'
  1516. end
  1517. end
  1518. end
  1519. def test_section_edit_links
  1520. raw = <<~RAW
  1521. h1. Title
  1522. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
  1523. h2. Subtitle with a [[Wiki]] link
  1524. h2. Subtitle with *some* _modifiers_
  1525. h2. Subtitle with @inline code@
  1526. <pre>
  1527. some code
  1528. h2. heading inside pre
  1529. <h2>html heading inside pre</h2>
  1530. </pre>
  1531. h2. Subtitle after pre tag
  1532. RAW
  1533. @project = Project.find(1)
  1534. set_language_if_valid 'en'
  1535. with_settings :text_formatting => 'textile' do
  1536. result =
  1537. textilizable(
  1538. raw,
  1539. :edit_section_links =>
  1540. {:controller => 'wiki', :action => 'edit',
  1541. :project_id => '1', :id => 'Test'}
  1542. ).delete("\n")
  1543. # heading that contains inline code
  1544. assert_match(
  1545. Regexp.new(
  1546. '<div class="contextual heading-2" title="Edit this section" id="section-4">' \
  1547. '<a class="icon-only icon-edit" href="/projects/1/wiki/Test/edit\?section=4">' \
  1548. 'Edit this section' \
  1549. '</a></div>' \
  1550. '<a name="Subtitle-with-inline-code"></a>' \
  1551. '<h2 >Subtitle with ' \
  1552. '<code>inline code</code><a href="#Subtitle-with-inline-code" class="wiki-anchor">&para;</a>' \
  1553. '</h2>'
  1554. ),
  1555. result
  1556. )
  1557. # last heading
  1558. assert_match(
  1559. Regexp.new(
  1560. '<div class="contextual heading-2" title="Edit this section" id="section-5">' \
  1561. '<a class="icon-only icon-edit" href="/projects/1/wiki/Test/edit\?section=5">' \
  1562. 'Edit this section' \
  1563. '</a></div>' \
  1564. '<a name="Subtitle-after-pre-tag"></a>' \
  1565. '<h2 >Subtitle after pre tag' \
  1566. '<a href="#Subtitle-after-pre-tag" class="wiki-anchor">&para;</a></h2>'
  1567. ),
  1568. result
  1569. )
  1570. end
  1571. end
  1572. def test_default_formatter
  1573. with_settings :text_formatting => 'unknown' do
  1574. text = 'a *link*: http://www.example.net/'
  1575. assert_equal(
  1576. '<p>a *link*: <a class="external" href="http://www.example.net/">' \
  1577. 'http://www.example.net/</a></p>',
  1578. textilizable(text)
  1579. )
  1580. end
  1581. end
  1582. def test_textilizable_with_formatting_set_to_false_should_not_format_text
  1583. assert_equal '*text*', textilizable("*text*", :formatting => false)
  1584. end
  1585. def test_textilizable_with_formatting_set_to_true_should_format_text
  1586. with_settings :text_formatting => 'textile' do
  1587. assert_equal '<p><strong>text</strong></p>', textilizable("*text*", :formatting => true)
  1588. end
  1589. end
  1590. def test_parse_redmine_links_should_handle_a_tag_without_attributes
  1591. text = +'<a>http://example.com</a>'
  1592. expected = text.dup
  1593. parse_redmine_links(text, nil, nil, nil, true, {})
  1594. assert_equal expected, text
  1595. end
  1596. def test_due_date_distance_in_words
  1597. to_test = {
  1598. Date.today => 'Due in 0 days',
  1599. Date.today + 1 => 'Due in 1 day',
  1600. Date.today + 100 => 'Due in about 3 months',
  1601. Date.today + 20000 => 'Due in over 54 years',
  1602. Date.today - 1 => '1 day late',
  1603. Date.today - 100 => 'about 3 months late',
  1604. Date.today - 20000 => 'over 54 years late',
  1605. }
  1606. ::I18n.locale = :en
  1607. to_test.each do |date, expected|
  1608. assert_equal expected, due_date_distance_in_words(date)
  1609. end
  1610. end
  1611. def test_render_page_hierarchy
  1612. parent_page = WikiPage.find(1)
  1613. child_page = WikiPage.find_by(parent_id: parent_page.id)
  1614. pages_by_parent_id = {nil => [parent_page], parent_page.id => [child_page]}
  1615. result = render_page_hierarchy(pages_by_parent_id, nil)
  1616. assert_select_in(
  1617. result, 'ul.pages-hierarchy li a[href=?]',
  1618. project_wiki_page_path(project_id: parent_page.project,
  1619. id: parent_page.title, version: nil))
  1620. assert_select_in(
  1621. result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]',
  1622. project_wiki_page_path(project_id: child_page.project,
  1623. id: child_page.title, version: nil))
  1624. end
  1625. def test_render_page_hierarchy_with_timestamp
  1626. parent_page = WikiPage.find(1)
  1627. child_page = WikiPage.find_by(parent_id: parent_page.id)
  1628. pages_by_parent_id = {nil => [parent_page], parent_page.id => [child_page]}
  1629. result = render_page_hierarchy(pages_by_parent_id, nil, :timestamp => true)
  1630. assert_select_in(
  1631. result, 'ul.pages-hierarchy li a[title=?]',
  1632. l(:label_updated_time,
  1633. distance_of_time_in_words(Time.now, parent_page.updated_on)))
  1634. assert_select_in(
  1635. result, 'ul.pages-hierarchy li ul.pages-hierarchy a[title=?]',
  1636. l(:label_updated_time,
  1637. distance_of_time_in_words(Time.now, child_page.updated_on)))
  1638. end
  1639. def test_render_page_hierarchy_when_action_is_export
  1640. parent_page = WikiPage.find(1)
  1641. child_page = WikiPage.find_by(parent_id: parent_page.id)
  1642. pages_by_parent_id = {nil => [parent_page], parent_page.id => [child_page]}
  1643. # Change controller and action using stub
  1644. controller.stubs(:controller_name).returns('wiki')
  1645. controller.stubs(:action_name).returns("export")
  1646. result = render_page_hierarchy(pages_by_parent_id, nil)
  1647. assert_select_in result, 'ul.pages-hierarchy li a[href=?]', "##{parent_page.title}"
  1648. assert_select_in result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]', "##{child_page.title}"
  1649. end
  1650. def test_link_to_user
  1651. user = User.find(2)
  1652. result = link_to("John Smith", "/users/2", :class => "user active")
  1653. assert_equal result, link_to_user(user)
  1654. end
  1655. def test_link_to_user_should_not_link_to_locked_user
  1656. with_current_user nil do
  1657. user = User.find(5)
  1658. assert user.locked?
  1659. assert_equal 'Dave2 Lopper2', link_to_user(user)
  1660. end
  1661. end
  1662. def test_link_to_user_should_link_to_locked_user_only_if_current_user_is_admin
  1663. user = User.find(5)
  1664. assert user.locked?
  1665. with_current_user User.find(1) do
  1666. result = link_to('Dave2 Lopper2', '/users/5', :class => 'user locked assigned_to')
  1667. assert_equal result, link_to_user(user, :class => 'assigned_to')
  1668. end
  1669. with_current_user User.find(2) do
  1670. result = 'Dave2 Lopper2'
  1671. assert_equal result, link_to_user(user, :class => 'assigned_to')
  1672. end
  1673. end
  1674. def test_link_to_principal_should_link_to_user
  1675. user = User.find(2)
  1676. result = link_to('John Smith', '/users/2', :class => 'user active')
  1677. assert_equal result, link_to_principal(user)
  1678. end
  1679. def test_link_to_principal_should_link_to_group
  1680. group = Group.find(10)
  1681. result = link_to('A Team', '/groups/10', :class => 'group')
  1682. assert_equal result, link_to_principal(group)
  1683. end
  1684. def test_link_to_principal_should_return_string_representation_for_unknown_type_principal
  1685. unknown_principal = 'foo'
  1686. result = unknown_principal.to_s
  1687. assert_equal result, link_to_principal(unknown_principal, :class => 'bar')
  1688. end
  1689. def test_link_to_principal_should_escape_principal_name
  1690. user = User.generate!(firstname: "firstname<>'", lastname: 'lastname&"')
  1691. group = Group.generate!(lastname: "group<>'&")
  1692. assert_include "firstname&lt;&gt;&#39; lastname&amp;&quot;", link_to_principal(user)
  1693. assert_include "@firstname&lt;&gt;&#39; lastname&amp;&quot;", link_to_principal(user, { mention: true })
  1694. assert_include "group&lt;&gt;&#39;&amp;", link_to_principal(group)
  1695. assert_include "&lt;&gt;&#39;&amp;", link_to_principal("<>'&")
  1696. end
  1697. def test_link_to_group_should_return_only_group_name_for_non_admin_users
  1698. User.current = nil
  1699. group = Group.find(10)
  1700. assert_equal "A Team", link_to_group(group)
  1701. end
  1702. def test_link_to_group_should_link_to_group_edit_page_for_admin_users
  1703. User.current = User.find(1)
  1704. group = Group.find(10)
  1705. result = link_to("A Team", "/groups/10/edit")
  1706. assert_equal result, link_to_group(group)
  1707. end
  1708. def test_link_to_user_should_not_link_to_anonymous
  1709. user = User.anonymous
  1710. assert user.anonymous?
  1711. t = link_to_user(user)
  1712. assert_equal ::I18n.t(:label_user_anonymous), t
  1713. end
  1714. def test_link_to_attachment
  1715. a = Attachment.find(3)
  1716. assert_equal(
  1717. '<a href="/attachments/3">logo.gif</a>',
  1718. link_to_attachment(a))
  1719. assert_equal(
  1720. '<a href="/attachments/3">Text</a>',
  1721. link_to_attachment(a, :text => 'Text'))
  1722. result = link_to("logo.gif", "/attachments/3", :class => "foo")
  1723. assert_equal(
  1724. result,
  1725. link_to_attachment(a, :class => 'foo'))
  1726. assert_equal(
  1727. '<a href="/attachments/download/3/logo.gif">logo.gif</a>',
  1728. link_to_attachment(a, :download => true))
  1729. assert_equal(
  1730. '<a href="http://test.host/attachments/3">logo.gif</a>',
  1731. link_to_attachment(a, :only_path => false))
  1732. end
  1733. def test_thumbnail_tag
  1734. a = Attachment.find(3)
  1735. assert_select_in(
  1736. thumbnail_tag(a),
  1737. 'a[href=?][title=?] img[src=?][loading="lazy"]',
  1738. "/attachments/3", "logo.gif", "/attachments/thumbnail/3/200")
  1739. end
  1740. def test_link_to_project
  1741. project = Project.find(1)
  1742. assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
  1743. link_to_project(project)
  1744. assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
  1745. link_to_project(project, {:only_path => false, :jump => 'blah'})
  1746. end
  1747. def test_link_to_project_settings
  1748. project = Project.find(1)
  1749. assert_equal '<a href="/projects/ecookbook/settings">eCookbook</a>', link_to_project_settings(project)
  1750. project.status = Project::STATUS_CLOSED
  1751. assert_equal '<a href="/projects/ecookbook">eCookbook</a>', link_to_project_settings(project)
  1752. project.status = Project::STATUS_ARCHIVED
  1753. assert_equal 'eCookbook', link_to_project_settings(project)
  1754. end
  1755. def test_link_to_legacy_project_with_numerical_identifier_should_use_id
  1756. # numeric identifier are no longer allowed
  1757. Project.where(:id => 1).update_all(:identifier => 25)
  1758. assert_equal '<a href="/projects/1">eCookbook</a>',
  1759. link_to_project(Project.find(1))
  1760. end
  1761. def test_link_to_record
  1762. [
  1763. [custom_values(:custom_values_007), '<a href="/projects/ecookbook">eCookbook</a>'],
  1764. [documents(:documents_001), '<a href="/documents/1">Test document</a>'],
  1765. [Group.find(10), '<a href="/groups/10">A Team</a>'],
  1766. [issues(:issues_001), link_to_issue(issues(:issues_001), :subject => false)],
  1767. [messages(:messages_001), '<a href="/boards/1/topics/1">First post</a>'],
  1768. [news(:news_001), '<a href="/news/1">eCookbook first release !</a>'],
  1769. [projects(:projects_001), '<a href="/projects/ecookbook">eCookbook</a>'],
  1770. [users(:users_001), '<a class="user active" href="/users/1">Redmine Admin</a>'],
  1771. [versions(:versions_001), '<a title="07/01/2006" href="/versions/1">eCookbook - 0.1</a>'],
  1772. [wiki_pages(:wiki_pages_001),
  1773. '<a href="/projects/ecookbook/wiki/CookBook_documentation">CookBook documentation</a>']
  1774. ].each do |record, link|
  1775. assert_equal link, link_to_record(record)
  1776. end
  1777. end
  1778. def test_link_to_attachment_container
  1779. field = ProjectCustomField.generate!(:name => "File", :field_format => 'attachment')
  1780. project = projects(:projects_001)
  1781. project_custom_value_attachment = new_record(Attachment) do
  1782. project.custom_field_values = {field.id => {:file => mock_file}}
  1783. project.save
  1784. end
  1785. news_attachment = attachments(:attachments_004)
  1786. news_attachment.container = news(:news_001)
  1787. news_attachment.save!
  1788. [
  1789. [project_custom_value_attachment, '<a href="/projects/ecookbook">eCookbook</a>'],
  1790. [attachments(:attachments_002), '<a href="/documents/1">Test document</a>'],
  1791. [attachments(:attachments_001), link_to_issue(issues(:issues_003), :subject => false)],
  1792. [attachments(:attachments_013), '<a href="/boards/1/topics/1">First post</a>'],
  1793. [news_attachment, '<a href="/news/1">eCookbook first release !</a>'],
  1794. [attachments(:attachments_008), '<a href="/projects/ecookbook/files">Files</a>'],
  1795. [attachments(:attachments_009), '<a href="/projects/ecookbook/files">Files</a>'],
  1796. [attachments(:attachments_003),
  1797. '<a href="/projects/ecookbook/wiki/Page_with_an_inline_image">Page with an inline image</a>'],
  1798. ].each do |attachment, link|
  1799. assert_equal link, link_to_attachment_container(attachment.container)
  1800. end
  1801. end
  1802. def test_principals_check_box_tag_with_avatar
  1803. principals = [User.find(1), Group.find(10)]
  1804. with_settings :gravatar_enabled => '1' do
  1805. tags = principals_check_box_tags("watcher[user_ids][]", principals)
  1806. principals.each do |principal|
  1807. assert_include avatar(principal, :size => 16), tags
  1808. assert_not_include content_tag('span', nil, :class => "name icon icon-#{principal.class.name.downcase}"), tags
  1809. end
  1810. end
  1811. end
  1812. def test_principals_check_box_tag_without_avatar
  1813. principals = [User.find(1), Group.find(10)]
  1814. Setting.gravatar_enabled = '1'
  1815. avatar_tags = principals.collect{|p| avatar(p, :size => 16)}
  1816. with_settings :gravatar_enabled => '0' do
  1817. tags = principals_check_box_tags(name, principals)
  1818. principals.each_with_index do |principal, i|
  1819. assert_not_include avatar_tags[i], tags
  1820. assert_include content_tag('span', nil, :class => "name icon icon-#{principal.class.name.downcase}"), tags
  1821. end
  1822. end
  1823. end
  1824. def test_principals_options_for_select_with_users
  1825. User.current = nil
  1826. users = [User.find(2), User.find(4)]
  1827. assert_equal(
  1828. %(<option value="2">John Smith</option><option value="4">Robert Hill</option>),
  1829. principals_options_for_select(users))
  1830. end
  1831. def test_principals_options_for_select_with_selected
  1832. User.current = nil
  1833. users = [User.find(2), User.find(4)]
  1834. assert_equal(
  1835. %(<option value="2">John Smith</option><option value="4" selected="selected">Robert Hill</option>),
  1836. principals_options_for_select(users, User.find(4)))
  1837. end
  1838. def test_principals_options_for_select_with_users_and_groups
  1839. User.current = nil
  1840. set_language_if_valid 'en'
  1841. users = [User.find(2), Group.find(11), User.find(4), Group.find(10)]
  1842. assert_equal(
  1843. %(<option value="2">John Smith</option><option value="4">Robert Hill</option>) +
  1844. %(<optgroup label="Groups"><option value="10">A Team</option><option value="11">B Team</option></optgroup>),
  1845. principals_options_for_select(users))
  1846. end
  1847. def test_principals_options_for_select_with_empty_collection
  1848. assert_equal '', principals_options_for_select([])
  1849. end
  1850. def test_principals_options_for_select_should_include_me_option_when_current_user_is_in_collection
  1851. set_language_if_valid 'en'
  1852. users = [User.find(2), User.find(4)]
  1853. User.current = User.find(4)
  1854. assert_include '<option value="4">&lt;&lt; me &gt;&gt;</option>',
  1855. principals_options_for_select(users)
  1856. end
  1857. def test_stylesheet_link_tag_should_pick_the_default_stylesheet
  1858. assert_match 'href="/stylesheets/styles.css"', stylesheet_link_tag("styles")
  1859. end
  1860. def test_stylesheet_link_tag_for_plugin_should_pick_the_plugin_stylesheet
  1861. assert_match 'href="/plugin_assets/foo/stylesheets/styles.css"',
  1862. stylesheet_link_tag("styles", :plugin => :foo)
  1863. end
  1864. def test_image_tag_should_pick_the_default_image
  1865. assert_match 'src="/images/image.png"', image_tag("image.png")
  1866. end
  1867. def test_image_tag_should_pick_the_theme_image_if_it_exists
  1868. theme = Redmine::Themes.themes.last
  1869. theme.images << 'image.png'
  1870. with_settings :ui_theme => theme.id do
  1871. assert_match %|src="/themes/#{theme.dir}/images/image.png"|, image_tag("image.png")
  1872. assert_match %|src="/images/other.png"|, image_tag("other.png")
  1873. end
  1874. ensure
  1875. theme.images.delete 'image.png'
  1876. end
  1877. def test_image_tag_sfor_plugin_should_pick_the_plugin_image
  1878. assert_match 'src="/plugin_assets/foo/images/image.png"', image_tag("image.png", :plugin => :foo)
  1879. end
  1880. def test_javascript_include_tag_should_pick_the_default_javascript
  1881. assert_match 'src="/javascripts/scripts.js"', javascript_include_tag("scripts")
  1882. end
  1883. def test_javascript_include_tag_for_plugin_should_pick_the_plugin_javascript
  1884. assert_match 'src="/plugin_assets/foo/javascripts/scripts.js"', javascript_include_tag("scripts", :plugin => :foo)
  1885. end
  1886. def test_raw_json_should_escape_closing_tags
  1887. s = raw_json(["<foo>bar</foo>"])
  1888. assert_include '\/foo', s
  1889. end
  1890. def test_raw_json_should_be_html_safe
  1891. s = raw_json(["foo"])
  1892. assert s.html_safe?
  1893. end
  1894. def test_html_title_should_app_title_if_not_set
  1895. assert_equal 'Redmine', html_title
  1896. end
  1897. def test_html_title_should_join_items
  1898. html_title 'Foo', 'Bar'
  1899. assert_equal 'Foo - Bar - Redmine', html_title
  1900. end
  1901. def test_html_title_should_append_current_project_name
  1902. @project = Project.find(1)
  1903. html_title 'Foo', 'Bar'
  1904. assert_equal 'Foo - Bar - eCookbook - Redmine', html_title
  1905. end
  1906. def test_title_should_return_a_h2_tag
  1907. assert_equal '<h2>Foo</h2>', title('Foo')
  1908. end
  1909. def test_title_should_set_html_title
  1910. title('Foo')
  1911. assert_equal 'Foo - Redmine', html_title
  1912. end
  1913. def test_title_should_turn_arrays_into_links
  1914. assert_equal '<h2><a href="/foo">Foo</a></h2>', title(['Foo', '/foo'])
  1915. assert_equal 'Foo - Redmine', html_title
  1916. end
  1917. def test_title_should_join_items
  1918. assert_equal '<h2>Foo &#187; Bar</h2>', title('Foo', 'Bar')
  1919. assert_equal 'Bar - Foo - Redmine', html_title
  1920. end
  1921. def test_favicon_path
  1922. assert_match %r{^/favicon\.ico}, favicon_path
  1923. end
  1924. def test_favicon_path_with_suburi
  1925. Redmine::Utils.relative_url_root = '/foo'
  1926. assert_match %r{^/foo/favicon\.ico}, favicon_path
  1927. ensure
  1928. Redmine::Utils.relative_url_root = ''
  1929. end
  1930. def test_favicon_url
  1931. assert_match %r{^http://test\.host/favicon\.ico}, favicon_url
  1932. end
  1933. def test_favicon_url_with_suburi
  1934. Redmine::Utils.relative_url_root = '/foo'
  1935. assert_match %r{^http://test\.host/foo/favicon\.ico}, favicon_url
  1936. ensure
  1937. Redmine::Utils.relative_url_root = ''
  1938. end
  1939. def test_truncate_single_line
  1940. str = "01234"
  1941. result = truncate_single_line_raw("#{str}\n#{str}", 10)
  1942. assert_equal "01234 0...", result
  1943. assert !result.html_safe?
  1944. result = truncate_single_line_raw("#{str}<&#>\n#{str}#{str}", 16)
  1945. assert_equal "01234<&#> 012...", result
  1946. assert !result.html_safe?
  1947. end
  1948. def test_truncate_single_line_non_ascii
  1949. ja = '日本語'
  1950. result = truncate_single_line_raw("#{ja}\n#{ja}\n#{ja}", 10)
  1951. assert_equal "#{ja} #{ja}...", result
  1952. assert !result.html_safe?
  1953. end
  1954. def test_hours_formatting
  1955. set_language_if_valid 'en'
  1956. with_settings :timespan_format => 'minutes' do
  1957. assert_equal '0:45', format_hours(0.75)
  1958. assert_equal '0:45 h', l_hours_short(0.75)
  1959. assert_equal '0:45 hour', l_hours(0.75)
  1960. end
  1961. with_settings :timespan_format => 'decimal' do
  1962. assert_equal '0.75', format_hours(0.75)
  1963. assert_equal '0.75 h', l_hours_short(0.75)
  1964. assert_equal '0.75 hour', l_hours(0.75)
  1965. end
  1966. end
  1967. def test_html_hours
  1968. assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">:45</span>',
  1969. html_hours('0:45')
  1970. assert_equal '<span class="hours hours-int">0</span><span class="hours hours-dec">.75</span>',
  1971. html_hours('0.75')
  1972. end
  1973. def test_form_for_includes_name_attribute
  1974. assert_match(/name="new_issue-[a-z0-9]{8}"/, form_for(Issue.new){})
  1975. end
  1976. def test_labelled_form_for_includes_name_attribute
  1977. assert_match(/name="new_issue-[a-z0-9]{8}"/, labelled_form_for(Issue.new){})
  1978. end
  1979. def test_redner_if_exist_should_be_render_partial
  1980. controller.prepend_view_path "test/fixtures/views"
  1981. assert_equal "partial html\n", render_if_exist(:partial => 'partial')
  1982. end
  1983. def test_redner_if_exist_should_be_render_nil
  1984. controller.prepend_view_path "test/fixtures/views"
  1985. assert_nil render_if_exist(:partial => 'non_exist_partial')
  1986. end
  1987. def test_export_csv_encoding_select_tag_should_return_nil_when_general_csv_encoding_is_UTF8
  1988. with_locale 'az' do
  1989. assert_equal l(:general_csv_encoding), 'UTF-8'
  1990. assert_nil export_csv_encoding_select_tag
  1991. end
  1992. end
  1993. def test_export_csv_encoding_select_tag_should_have_two_option_when_general_csv_encoding_is_not_UTF8
  1994. with_locale 'en' do
  1995. assert_not_equal l(:general_csv_encoding), 'UTF-8'
  1996. result = export_csv_encoding_select_tag
  1997. assert_select_in result,
  1998. "option[selected='selected'][value=#{l(:general_csv_encoding)}]",
  1999. :text => l(:general_csv_encoding)
  2000. assert_select_in result, "option[value='UTF-8']", :text => 'UTF-8'
  2001. end
  2002. end
  2003. def test_time_tag
  2004. user = User.find(1)
  2005. user.pref.update(time_zone: 'UTC')
  2006. User.current = user
  2007. assert_nil time_tag(nil)
  2008. with_locale 'en' do
  2009. travel_to Time.zone.parse('2022-12-30T01:00:00Z') do
  2010. assert_equal "<abbr title=\"12/28/2022 01:00 AM\">2 days</abbr>", time_tag(2.days.ago)
  2011. @project = Project.find(1)
  2012. 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)
  2013. end
  2014. end
  2015. end
  2016. private
  2017. def wiki_links_with_special_characters
  2018. {
  2019. '[[Jack & Coke]]' =>
  2020. link_to("Jack & Coke",
  2021. "/projects/ecookbook/wiki/Jack_&_Coke",
  2022. :class => "wiki-page new"),
  2023. '[[a "quoted" name]]' =>
  2024. link_to("a \"quoted\" name",
  2025. "/projects/ecookbook/wiki/A_%22quoted%22_name",
  2026. :class => "wiki-page new"),
  2027. '[[le français, c\'est super]]' =>
  2028. link_to("le français, c'est super",
  2029. "/projects/ecookbook/wiki/Le_fran%C3%A7ais_c'est_super",
  2030. :class => "wiki-page new"),
  2031. '[[broken < less]]' =>
  2032. link_to("broken < less",
  2033. "/projects/ecookbook/wiki/Broken_%3C_less",
  2034. :class => "wiki-page new"),
  2035. '[[broken > more]]' =>
  2036. link_to("broken > more",
  2037. "/projects/ecookbook/wiki/Broken_%3E_more",
  2038. :class => "wiki-page new"),
  2039. '[[[foo]Including [square brackets] in wiki title]]' =>
  2040. link_to("[foo]Including [square brackets] in wiki title",
  2041. "/projects/ecookbook/wiki/%5Bfoo%5DIncluding_%5Bsquare_brackets%5D_in_wiki_title",
  2042. :class => "wiki-page new"),
  2043. }
  2044. end
  2045. end