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

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