annotate test/unit/lib/redmine/wiki_formatting/macros_test.rb @ 1459:cf78a7ade302 luisf

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