PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/prdixit/redmine
Ruby | 295 lines | 217 code | 56 blank | 22 comment | 0 complexity | 8f954c4ddc37f8b677dcaa2c85362b32 MD5 | raw file
Possible License(s): GPL-2.0
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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_multiple_macros_on_the_same_line
  70. Redmine::WikiFormatting::Macros.macro :foo do |obj, args|
  71. args.any? ? "args: #{args.join(',')}" : "no args"
  72. end
  73. assert_equal '<p>no args no args</p>', textilizable("{{foo}} {{foo}}")
  74. assert_equal '<p>args: a,b no args</p>', textilizable("{{foo(a,b)}} {{foo}}")
  75. assert_equal '<p>args: a,b args: c,d</p>', textilizable("{{foo(a,b)}} {{foo(c,d)}}")
  76. assert_equal '<p>no args args: c,d</p>', textilizable("{{foo}} {{foo(c,d)}}")
  77. end
  78. def test_macro_should_receive_the_object_as_argument_when_with_object_and_attribute
  79. issue = Issue.find(1)
  80. issue.description = "{{hello_world}}"
  81. assert_equal '<p>Hello world! Object: Issue, Called with no argument and no block of text.</p>', textilizable(issue, :description)
  82. end
  83. def test_macro_should_receive_the_object_as_argument_when_called_with_object_option
  84. text = "{{hello_world}}"
  85. assert_equal '<p>Hello world! Object: Issue, Called with no argument and no block of text.</p>', textilizable(text, :object => Issue.find(1))
  86. end
  87. def test_macro_exception_should_be_displayed
  88. Redmine::WikiFormatting::Macros.macro :exception do |obj, args|
  89. raise "My message"
  90. end
  91. text = "{{exception}}"
  92. assert_include '<div class="flash error">Error executing the <strong>exception</strong> macro (My message)</div>', textilizable(text)
  93. end
  94. def test_macro_arguments_should_not_be_parsed_by_formatters
  95. text = '{{hello_world(http://www.redmine.org, #1)}}'
  96. assert_include 'Arguments: http://www.redmine.org, #1', textilizable(text)
  97. end
  98. def test_exclamation_mark_should_not_run_macros
  99. text = "!{{hello_world}}"
  100. assert_equal '<p>{{hello_world}}</p>', textilizable(text)
  101. end
  102. def test_exclamation_mark_should_escape_macros
  103. text = "!{{hello_world(<tag>)}}"
  104. assert_equal '<p>{{hello_world(&lt;tag&gt;)}}</p>', textilizable(text)
  105. end
  106. def test_unknown_macros_should_not_be_replaced
  107. text = "{{unknown}}"
  108. assert_equal '<p>{{unknown}}</p>', textilizable(text)
  109. end
  110. def test_unknown_macros_should_parsed_as_text
  111. text = "{{unknown(*test*)}}"
  112. assert_equal '<p>{{unknown(<strong>test</strong>)}}</p>', textilizable(text)
  113. end
  114. def test_unknown_macros_should_be_escaped
  115. text = "{{unknown(<tag>)}}"
  116. assert_equal '<p>{{unknown(&lt;tag&gt;)}}</p>', textilizable(text)
  117. end
  118. def test_html_safe_macro_output_should_not_be_escaped
  119. Redmine::WikiFormatting::Macros.macro :safe_macro do |obj, args|
  120. "<tag>".html_safe
  121. end
  122. assert_equal '<p><tag></p>', textilizable("{{safe_macro}}")
  123. end
  124. def test_macro_hello_world
  125. text = "{{hello_world}}"
  126. assert textilizable(text).match(/Hello world!/)
  127. end
  128. def test_macro_hello_world_should_escape_arguments
  129. text = "{{hello_world(<tag>)}}"
  130. assert_include 'Arguments: &lt;tag&gt;', textilizable(text)
  131. end
  132. def test_macro_macro_list
  133. text = "{{macro_list}}"
  134. assert_match %r{<code>hello_world</code>}, textilizable(text)
  135. end
  136. def test_macro_include
  137. @project = Project.find(1)
  138. # include a page of the current project wiki
  139. text = "{{include(Another page)}}"
  140. assert_include 'This is a link to a ticket', textilizable(text)
  141. @project = nil
  142. # include a page of a specific project wiki
  143. text = "{{include(ecookbook:Another page)}}"
  144. assert_include 'This is a link to a ticket', textilizable(text)
  145. text = "{{include(ecookbook:)}}"
  146. assert_include 'CookBook documentation', textilizable(text)
  147. text = "{{include(unknowidentifier:somepage)}}"
  148. assert_include 'Page not found', textilizable(text)
  149. end
  150. def test_macro_child_pages
  151. expected = "<p><ul class=\"pages-hierarchy\">\n" +
  152. "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a></li>\n" +
  153. "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
  154. "</ul>\n</p>"
  155. @project = Project.find(1)
  156. # child pages of the current wiki page
  157. assert_equal expected, textilizable("{{child_pages}}", :object => WikiPage.find(2).content)
  158. # child pages of another page
  159. assert_equal expected, textilizable("{{child_pages(Another_page)}}", :object => WikiPage.find(1).content)
  160. @project = Project.find(2)
  161. assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page)}}", :object => WikiPage.find(1).content)
  162. end
  163. def test_macro_child_pages_with_option
  164. expected = "<p><ul class=\"pages-hierarchy\">\n" +
  165. "<li><a href=\"/projects/ecookbook/wiki/Another_page\">Another page</a>\n" +
  166. "<ul class=\"pages-hierarchy\">\n" +
  167. "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a></li>\n" +
  168. "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
  169. "</ul>\n</li>\n</ul>\n</p>"
  170. @project = Project.find(1)
  171. # child pages of the current wiki page
  172. assert_equal expected, textilizable("{{child_pages(parent=1)}}", :object => WikiPage.find(2).content)
  173. # child pages of another page
  174. assert_equal expected, textilizable("{{child_pages(Another_page, parent=1)}}", :object => WikiPage.find(1).content)
  175. @project = Project.find(2)
  176. assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page, parent=1)}}", :object => WikiPage.find(1).content)
  177. end
  178. def test_macro_child_pages_without_wiki_page_should_fail
  179. assert_match /can be called from wiki pages only/, textilizable("{{child_pages}}")
  180. end
  181. def test_macro_thumbnail
  182. assert_equal '<p><a href="/attachments/17" class="thumbnail" title="testfile.PNG"><img alt="testfile.PNG" src="/attachments/thumbnail/17" /></a></p>',
  183. textilizable("{{thumbnail(testfile.png)}}", :object => Issue.find(14))
  184. end
  185. def test_macro_thumbnail_with_size
  186. assert_equal '<p><a href="/attachments/17" class="thumbnail" title="testfile.PNG"><img alt="testfile.PNG" src="/attachments/thumbnail/17/200" /></a></p>',
  187. textilizable("{{thumbnail(testfile.png, size=200)}}", :object => Issue.find(14))
  188. end
  189. def test_macro_thumbnail_with_title
  190. assert_equal '<p><a href="/attachments/17" class="thumbnail" title="Cool image"><img alt="testfile.PNG" src="/attachments/thumbnail/17" /></a></p>',
  191. textilizable("{{thumbnail(testfile.png, title=Cool image)}}", :object => Issue.find(14))
  192. end
  193. def test_macro_thumbnail_with_invalid_filename_should_fail
  194. assert_include 'test.png not found',
  195. textilizable("{{thumbnail(test.png)}}", :object => Issue.find(14))
  196. end
  197. def test_macros_should_not_be_executed_in_pre_tags
  198. text = <<-RAW
  199. {{hello_world(foo)}}
  200. <pre>
  201. {{hello_world(pre)}}
  202. !{{hello_world(pre)}}
  203. </pre>
  204. {{hello_world(bar)}}
  205. RAW
  206. expected = <<-EXPECTED
  207. <p>Hello world! Object: NilClass, Arguments: foo and no block of text.</p>
  208. <pre>
  209. {{hello_world(pre)}}
  210. !{{hello_world(pre)}}
  211. </pre>
  212. <p>Hello world! Object: NilClass, Arguments: bar and no block of text.</p>
  213. EXPECTED
  214. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '')
  215. end
  216. def test_macros_should_be_escaped_in_pre_tags
  217. text = "<pre>{{hello_world(<tag>)}}</pre>"
  218. assert_equal "<pre>{{hello_world(&lt;tag&gt;)}}</pre>", textilizable(text)
  219. end
  220. def test_macros_should_not_mangle_next_macros_outputs
  221. text = '{{macro(2)}} !{{macro(2)}} {{hello_world(foo)}}'
  222. assert_equal '<p>{{macro(2)}} {{macro(2)}} Hello world! Object: NilClass, Arguments: foo and no block of text.</p>', textilizable(text)
  223. end
  224. def test_macros_with_text_should_not_mangle_following_macros
  225. text = <<-RAW
  226. {{hello_world
  227. Line of text
  228. }}
  229. {{hello_world
  230. Another line of text
  231. }}
  232. RAW
  233. expected = <<-EXPECTED
  234. <p>Hello world! Object: NilClass, Called with no argument and a 12 bytes long block of text.</p>
  235. <p>Hello world! Object: NilClass, Called with no argument and a 20 bytes long block of text.</p>
  236. EXPECTED
  237. assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '')
  238. end
  239. end