PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/lib/redmine/wiki_formatting/macros_test.rb

https://bitbucket.org/eimajenthat/redmine
Ruby | 362 lines | 271 code | 69 blank | 22 comment | 0 complexity | 577763c0e41ecf91dcb91a5dfe929563 MD5 | raw file
Possible License(s): GPL-2.0
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../../../../test_helper', __FILE__)
  18. class Redmine::WikiFormatting::MacrosTest < ActionView::TestCase
  19. include ApplicationHelper
  20. include ActionView::Helpers::TextHelper
  21. include ActionView::Helpers::SanitizeHelper
  22. include ERB::Util
  23. extend ActionView::Helpers::SanitizeHelper::ClassMethods
  24. fixtures :projects, :roles, :enabled_modules, :users,
  25. :repositories, :changesets,
  26. :trackers, :issue_statuses, :issues,
  27. :versions, :documents,
  28. :wikis, :wiki_pages, :wiki_contents,
  29. :boards, :messages,
  30. :attachments
  31. def setup
  32. super
  33. @project = nil
  34. end
  35. def teardown
  36. end
  37. def test_macro_registration
  38. Redmine::WikiFormatting::Macros.register do
  39. macro :foo do |obj, args|
  40. "Foo: #{args.size} (#{args.join(',')}) (#{args.class.name})"
  41. end
  42. end
  43. assert_equal '<p>Foo: 0 () (Array)</p>', textilizable("{{foo}}")
  44. assert_equal '<p>Foo: 0 () (Array)</p>', textilizable("{{foo()}}")
  45. assert_equal '<p>Foo: 1 (arg1) (Array)</p>', textilizable("{{foo(arg1)}}")
  46. assert_equal '<p>Foo: 2 (arg1,arg2) (Array)</p>', textilizable("{{foo(arg1, arg2)}}")
  47. end
  48. def test_macro_registration_parse_args_set_to_false_should_disable_arguments_parsing
  49. Redmine::WikiFormatting::Macros.register do
  50. macro :bar, :parse_args => false do |obj, args|
  51. "Bar: (#{args}) (#{args.class.name})"
  52. end
  53. end
  54. assert_equal '<p>Bar: (args, more args) (String)</p>', textilizable("{{bar(args, more args)}}")
  55. assert_equal '<p>Bar: () (String)</p>', textilizable("{{bar}}")
  56. assert_equal '<p>Bar: () (String)</p>', textilizable("{{bar()}}")
  57. end
  58. def test_macro_registration_with_3_args_should_receive_text_argument
  59. Redmine::WikiFormatting::Macros.register do
  60. macro :baz do |obj, args, text|
  61. "Baz: (#{args.join(',')}) (#{text.class.name}) (#{text})"
  62. end
  63. end
  64. assert_equal "<p>Baz: () (NilClass) ()</p>", textilizable("{{baz}}")
  65. assert_equal "<p>Baz: () (NilClass) ()</p>", textilizable("{{baz()}}")
  66. assert_equal "<p>Baz: () (String) (line1\nline2)</p>", textilizable("{{baz()\nline1\nline2\n}}")
  67. assert_equal "<p>Baz: (arg1,arg2) (String) (line1\nline2)</p>", textilizable("{{baz(arg1, arg2)\nline1\nline2\n}}")
  68. end
  69. def test_macro_name_with_upper_case
  70. Redmine::WikiFormatting::Macros.macro(:UpperCase) {|obj, args| "Upper"}
  71. assert_equal "<p>Upper</p>", textilizable("{{UpperCase}}")
  72. end
  73. def test_multiple_macros_on_the_same_line
  74. Redmine::WikiFormatting::Macros.macro :foo do |obj, args|
  75. args.any? ? "args: #{args.join(',')}" : "no args"
  76. end
  77. assert_equal '<p>no args no args</p>', textilizable("{{foo}} {{foo}}")
  78. assert_equal '<p>args: a,b no args</p>', textilizable("{{foo(a,b)}} {{foo}}")
  79. assert_equal '<p>args: a,b args: c,d</p>', textilizable("{{foo(a,b)}} {{foo(c,d)}}")
  80. assert_equal '<p>no args args: c,d</p>', textilizable("{{foo}} {{foo(c,d)}}")
  81. end
  82. def test_macro_should_receive_the_object_as_argument_when_with_object_and_attribute
  83. issue = Issue.find(1)
  84. issue.description = "{{hello_world}}"
  85. assert_equal '<p>Hello world! Object: Issue, Called with no argument and no block of text.</p>', textilizable(issue, :description)
  86. end
  87. def test_macro_should_receive_the_object_as_argument_when_called_with_object_option
  88. text = "{{hello_world}}"
  89. assert_equal '<p>Hello world! Object: Issue, Called with no argument and no block of text.</p>', textilizable(text, :object => Issue.find(1))
  90. end
  91. def test_extract_macro_options_should_with_args
  92. options = extract_macro_options(["arg1", "arg2"], :foo, :size)
  93. assert_equal([["arg1", "arg2"], {}], options)
  94. end
  95. def test_extract_macro_options_should_with_options
  96. options = extract_macro_options(["foo=bar", "size=2"], :foo, :size)
  97. assert_equal([[], {:foo => "bar", :size => "2"}], options)
  98. end
  99. def test_extract_macro_options_should_with_args_and_options
  100. options = extract_macro_options(["arg1", "arg2", "foo=bar", "size=2"], :foo, :size)
  101. assert_equal([["arg1", "arg2"], {:foo => "bar", :size => "2"}], options)
  102. end
  103. def test_extract_macro_options_should_parse_options_lazily
  104. options = extract_macro_options(["params=x=1&y=2"], :params)
  105. assert_equal([[], {:params => "x=1&y=2"}], options)
  106. end
  107. def test_macro_exception_should_be_displayed
  108. Redmine::WikiFormatting::Macros.macro :exception do |obj, args|
  109. raise "My message"
  110. end
  111. text = "{{exception}}"
  112. assert_include '<div class="flash error">Error executing the <strong>exception</strong> macro (My message)</div>', textilizable(text)
  113. end
  114. def test_macro_arguments_should_not_be_parsed_by_formatters
  115. text = '{{hello_world(http://www.redmine.org, #1)}}'
  116. assert_include 'Arguments: http://www.redmine.org, #1', textilizable(text)
  117. end
  118. def test_exclamation_mark_should_not_run_macros
  119. text = "!{{hello_world}}"
  120. assert_equal '<p>{{hello_world}}</p>', textilizable(text)
  121. end
  122. def test_exclamation_mark_should_escape_macros
  123. text = "!{{hello_world(<tag>)}}"
  124. assert_equal '<p>{{hello_world(&lt;tag&gt;)}}</p>', textilizable(text)
  125. end
  126. def test_unknown_macros_should_not_be_replaced
  127. text = "{{unknown}}"
  128. assert_equal '<p>{{unknown}}</p>', textilizable(text)
  129. end
  130. def test_unknown_macros_should_parsed_as_text
  131. text = "{{unknown(*test*)}}"
  132. assert_equal '<p>{{unknown(<strong>test</strong>)}}</p>', textilizable(text)
  133. end
  134. def test_unknown_macros_should_be_escaped
  135. text = "{{unknown(<tag>)}}"
  136. assert_equal '<p>{{unknown(&lt;tag&gt;)}}</p>', textilizable(text)
  137. end
  138. def test_html_safe_macro_output_should_not_be_escaped
  139. Redmine::WikiFormatting::Macros.macro :safe_macro do |obj, args|
  140. "<tag>".html_safe
  141. end
  142. assert_equal '<p><tag></p>', textilizable("{{safe_macro}}")
  143. end
  144. def test_macro_hello_world
  145. text = "{{hello_world}}"
  146. assert textilizable(text).match(/Hello world!/)
  147. end
  148. def test_macro_hello_world_should_escape_arguments
  149. text = "{{hello_world(<tag>)}}"
  150. assert_include 'Arguments: &lt;tag&gt;', textilizable(text)
  151. end
  152. def test_macro_macro_list
  153. text = "{{macro_list}}"
  154. assert_match %r{<code>hello_world</code>}, textilizable(text)
  155. end
  156. def test_macro_include
  157. @project = Project.find(1)
  158. # include a page of the current project wiki
  159. text = "{{include(Another page)}}"
  160. assert_include 'This is a link to a ticket', textilizable(text)
  161. @project = nil
  162. # include a page of a specific project wiki
  163. text = "{{include(ecookbook:Another page)}}"
  164. assert_include 'This is a link to a ticket', textilizable(text)
  165. text = "{{include(ecookbook:)}}"
  166. assert_include 'CookBook documentation', textilizable(text)
  167. text = "{{include(unknowidentifier:somepage)}}"
  168. assert_include 'Page not found', textilizable(text)
  169. end
  170. def test_macro_collapse
  171. text = "{{collapse\n*Collapsed* block of text\n}}"
  172. result = textilizable(text)
  173. assert_select_in result, 'div.collapsed-text'
  174. assert_select_in result, 'strong', :text => 'Collapsed'
  175. assert_select_in result, 'a.collapsible.collapsed', :text => 'Show'
  176. assert_select_in result, 'a.collapsible', :text => 'Hide'
  177. end
  178. def test_macro_collapse_with_one_arg
  179. text = "{{collapse(Example)\n*Collapsed* block of text\n}}"
  180. result = textilizable(text)
  181. assert_select_in result, 'div.collapsed-text'
  182. assert_select_in result, 'strong', :text => 'Collapsed'
  183. assert_select_in result, 'a.collapsible.collapsed', :text => 'Example'
  184. assert_select_in result, 'a.collapsible', :text => 'Example'
  185. end
  186. def test_macro_collapse_with_two_args
  187. text = "{{collapse(Show example, Hide example)\n*Collapsed* block of text\n}}"
  188. result = textilizable(text)
  189. assert_select_in result, 'div.collapsed-text'
  190. assert_select_in result, 'strong', :text => 'Collapsed'
  191. assert_select_in result, 'a.collapsible.collapsed', :text => 'Show example'
  192. assert_select_in result, 'a.collapsible', :text => 'Hide example'
  193. end
  194. def test_macro_child_pages
  195. expected = "<p><ul class=\"pages-hierarchy\">\n" +
  196. "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a>\n" +
  197. "<ul class=\"pages-hierarchy\">\n<li><a href=\"/projects/ecookbook/wiki/Child_1_1\">Child 1 1</a></li>\n</ul>\n</li>\n" +
  198. "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
  199. "</ul>\n</p>"
  200. @project = Project.find(1)
  201. # child pages of the current wiki page
  202. assert_equal expected, textilizable("{{child_pages}}", :object => WikiPage.find(2).content)
  203. # child pages of another page
  204. assert_equal expected, textilizable("{{child_pages(Another_page)}}", :object => WikiPage.find(1).content)
  205. @project = Project.find(2)
  206. assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page)}}", :object => WikiPage.find(1).content)
  207. end
  208. def test_macro_child_pages_with_parent_option
  209. expected = "<p><ul class=\"pages-hierarchy\">\n" +
  210. "<li><a href=\"/projects/ecookbook/wiki/Another_page\">Another page</a>\n" +
  211. "<ul class=\"pages-hierarchy\">\n" +
  212. "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a>\n" +
  213. "<ul class=\"pages-hierarchy\">\n<li><a href=\"/projects/ecookbook/wiki/Child_1_1\">Child 1 1</a></li>\n</ul>\n</li>\n" +
  214. "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
  215. "</ul>\n</li>\n</ul>\n</p>"
  216. @project = Project.find(1)
  217. # child pages of the current wiki page
  218. assert_equal expected, textilizable("{{child_pages(parent=1)}}", :object => WikiPage.find(2).content)
  219. # child pages of another page
  220. assert_equal expected, textilizable("{{child_pages(Another_page, parent=1)}}", :object => WikiPage.find(1).content)
  221. @project = Project.find(2)
  222. assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page, parent=1)}}", :object => WikiPage.find(1).content)
  223. end
  224. def test_macro_child_pages_with_depth_option
  225. expected = "<p><ul class=\"pages-hierarchy\">\n" +
  226. "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a></li>\n" +
  227. "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
  228. "</ul>\n</p>"
  229. @project = Project.find(1)
  230. assert_equal expected, textilizable("{{child_pages(depth=1)}}", :object => WikiPage.find(2).content)
  231. end
  232. def test_macro_child_pages_without_wiki_page_should_fail
  233. assert_match /can be called from wiki pages only/, textilizable("{{child_pages}}")
  234. end
  235. def test_macro_thumbnail
  236. assert_equal '<p><a href="/attachments/17" class="thumbnail" title="testfile.PNG"><img alt="testfile.PNG" src="/attachments/thumbnail/17" /></a></p>',
  237. textilizable("{{thumbnail(testfile.png)}}", :object => Issue.find(14))
  238. end
  239. def test_macro_thumbnail_with_size
  240. assert_equal '<p><a href="/attachments/17" class="thumbnail" title="testfile.PNG"><img alt="testfile.PNG" src="/attachments/thumbnail/17/200" /></a></p>',
  241. textilizable("{{thumbnail(testfile.png, size=200)}}", :object => Issue.find(14))
  242. end
  243. def test_macro_thumbnail_with_title
  244. assert_equal '<p><a href="/attachments/17" class="thumbnail" title="Cool image"><img alt="testfile.PNG" src="/attachments/thumbnail/17" /></a></p>',
  245. textilizable("{{thumbnail(testfile.png, title=Cool image)}}", :object => Issue.find(14))
  246. end
  247. def test_macro_thumbnail_with_invalid_filename_should_fail
  248. assert_include 'test.png not found',
  249. textilizable("{{thumbnail(test.png)}}", :object => Issue.find(14))
  250. end
  251. def test_macros_should_not_be_executed_in_pre_tags
  252. text = <<-RAW
  253. {{hello_world(foo)}}
  254. <pre>
  255. {{hello_world(pre)}}
  256. !{{hello_world(pre)}}
  257. </pre>
  258. {{hello_world(bar)}}
  259. RAW
  260. expected = <<-EXPECTED
  261. <p>Hello world! Object: NilClass, Arguments: foo and no block of text.</p>
  262. <pre>
  263. {{hello_world(pre)}}
  264. !{{hello_world(pre)}}
  265. </pre>
  266. <p>Hello world! Object: NilClass, Arguments: bar and no block of text.</p>
  267. EXPECTED
  268. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '')
  269. end
  270. def test_macros_should_be_escaped_in_pre_tags
  271. text = "<pre>{{hello_world(<tag>)}}</pre>"
  272. assert_equal "<pre>{{hello_world(&lt;tag&gt;)}}</pre>", textilizable(text)
  273. end
  274. def test_macros_should_not_mangle_next_macros_outputs
  275. text = '{{macro(2)}} !{{macro(2)}} {{hello_world(foo)}}'
  276. assert_equal '<p>{{macro(2)}} {{macro(2)}} Hello world! Object: NilClass, Arguments: foo and no block of text.</p>', textilizable(text)
  277. end
  278. def test_macros_with_text_should_not_mangle_following_macros
  279. text = <<-RAW
  280. {{hello_world
  281. Line of text
  282. }}
  283. {{hello_world
  284. Another line of text
  285. }}
  286. RAW
  287. expected = <<-EXPECTED
  288. <p>Hello world! Object: NilClass, Called with no argument and a 12 bytes long block of text.</p>
  289. <p>Hello world! Object: NilClass, Called with no argument and a 20 bytes long block of text.</p>
  290. EXPECTED
  291. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '')
  292. end
  293. end