annotate test/unit/lib/redmine/wiki_formatting/macros_test.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1517 238 def test_macro_collapse_should_not_break_toc
Chris@1517 239 text = <<-RAW
Chris@1517 240 {{toc}}
Chris@1517 241
Chris@1517 242 h1. Title
Chris@1517 243
Chris@1517 244 {{collapse(Show example, Hide example)
Chris@1517 245 h2. Heading
Chris@1517 246 }}"
Chris@1517 247 RAW
Chris@1517 248
Chris@1517 249 expected_toc = '<ul class="toc"><li><a href="#Title">Title</a><ul><li><a href="#Heading">Heading</a></li></ul></li></ul>'
Chris@1517 250
Chris@1517 251 assert_include expected_toc, textilizable(text).gsub(/[\r\n]/, '')
Chris@1517 252 end
Chris@1517 253
Chris@0 254 def test_macro_child_pages
Chris@0 255 expected = "<p><ul class=\"pages-hierarchy\">\n" +
Chris@1115 256 "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a>\n" +
Chris@1115 257 "<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 258 "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
Chris@0 259 "</ul>\n</p>"
Chris@909 260
Chris@0 261 @project = Project.find(1)
Chris@0 262 # child pages of the current wiki page
Chris@0 263 assert_equal expected, textilizable("{{child_pages}}", :object => WikiPage.find(2).content)
Chris@0 264 # child pages of another page
Chris@0 265 assert_equal expected, textilizable("{{child_pages(Another_page)}}", :object => WikiPage.find(1).content)
Chris@909 266
Chris@0 267 @project = Project.find(2)
Chris@0 268 assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page)}}", :object => WikiPage.find(1).content)
Chris@0 269 end
Chris@909 270
Chris@1115 271 def test_macro_child_pages_with_parent_option
Chris@0 272 expected = "<p><ul class=\"pages-hierarchy\">\n" +
Chris@0 273 "<li><a href=\"/projects/ecookbook/wiki/Another_page\">Another page</a>\n" +
Chris@0 274 "<ul class=\"pages-hierarchy\">\n" +
Chris@1115 275 "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a>\n" +
Chris@1115 276 "<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 277 "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
Chris@0 278 "</ul>\n</li>\n</ul>\n</p>"
Chris@909 279
Chris@0 280 @project = Project.find(1)
Chris@0 281 # child pages of the current wiki page
Chris@0 282 assert_equal expected, textilizable("{{child_pages(parent=1)}}", :object => WikiPage.find(2).content)
Chris@0 283 # child pages of another page
Chris@0 284 assert_equal expected, textilizable("{{child_pages(Another_page, parent=1)}}", :object => WikiPage.find(1).content)
Chris@909 285
Chris@0 286 @project = Project.find(2)
Chris@0 287 assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page, parent=1)}}", :object => WikiPage.find(1).content)
Chris@0 288 end
Chris@1115 289
Chris@1115 290 def test_macro_child_pages_with_depth_option
Chris@1115 291 expected = "<p><ul class=\"pages-hierarchy\">\n" +
Chris@1115 292 "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a></li>\n" +
Chris@1115 293 "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
Chris@1115 294 "</ul>\n</p>"
Chris@1115 295
Chris@1115 296 @project = Project.find(1)
Chris@1115 297 assert_equal expected, textilizable("{{child_pages(depth=1)}}", :object => WikiPage.find(2).content)
Chris@1115 298 end
Chris@1115 299
Chris@1115 300 def test_macro_child_pages_without_wiki_page_should_fail
Chris@1115 301 assert_match /can be called from wiki pages only/, textilizable("{{child_pages}}")
Chris@1115 302 end
Chris@1115 303
Chris@1115 304 def test_macro_thumbnail
Chris@1517 305 link = link_to('<img alt="testfile.PNG" src="http://test.host/attachments/thumbnail/17" />'.html_safe,
Chris@1517 306 "http://test.host/attachments/17",
Chris@1517 307 :class => "thumbnail",
Chris@1517 308 :title => "testfile.PNG")
Chris@1517 309 assert_equal "<p>#{link}</p>",
Chris@1517 310 textilizable("{{thumbnail(testfile.png)}}", :object => Issue.find(14))
Chris@1115 311 end
Chris@1115 312
Chris@1115 313 def test_macro_thumbnail_with_size
Chris@1517 314 link = link_to('<img alt="testfile.PNG" src="http://test.host/attachments/thumbnail/17/200" />'.html_safe,
Chris@1517 315 "http://test.host/attachments/17",
Chris@1517 316 :class => "thumbnail",
Chris@1517 317 :title => "testfile.PNG")
Chris@1517 318 assert_equal "<p>#{link}</p>",
Chris@1517 319 textilizable("{{thumbnail(testfile.png, size=200)}}", :object => Issue.find(14))
Chris@1115 320 end
Chris@1115 321
Chris@1115 322 def test_macro_thumbnail_with_title
Chris@1517 323 link = link_to('<img alt="testfile.PNG" src="http://test.host/attachments/thumbnail/17" />'.html_safe,
Chris@1517 324 "http://test.host/attachments/17",
Chris@1517 325 :class => "thumbnail",
Chris@1517 326 :title => "Cool image")
Chris@1517 327 assert_equal "<p>#{link}</p>",
Chris@1517 328 textilizable("{{thumbnail(testfile.png, title=Cool image)}}", :object => Issue.find(14))
Chris@1115 329 end
Chris@1115 330
Chris@1115 331 def test_macro_thumbnail_with_invalid_filename_should_fail
Chris@1115 332 assert_include 'test.png not found',
Chris@1115 333 textilizable("{{thumbnail(test.png)}}", :object => Issue.find(14))
Chris@1115 334 end
Chris@1115 335
Chris@1115 336 def test_macros_should_not_be_executed_in_pre_tags
Chris@1115 337 text = <<-RAW
Chris@1115 338 {{hello_world(foo)}}
Chris@1115 339
Chris@1115 340 <pre>
Chris@1115 341 {{hello_world(pre)}}
Chris@1115 342 !{{hello_world(pre)}}
Chris@1115 343 </pre>
Chris@1115 344
Chris@1115 345 {{hello_world(bar)}}
Chris@1115 346 RAW
Chris@1115 347
Chris@1115 348 expected = <<-EXPECTED
Chris@1115 349 <p>Hello world! Object: NilClass, Arguments: foo and no block of text.</p>
Chris@1115 350
Chris@1115 351 <pre>
Chris@1115 352 {{hello_world(pre)}}
Chris@1115 353 !{{hello_world(pre)}}
Chris@1115 354 </pre>
Chris@1115 355
Chris@1115 356 <p>Hello world! Object: NilClass, Arguments: bar and no block of text.</p>
Chris@1115 357 EXPECTED
Chris@1115 358
Chris@1115 359 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '')
Chris@1115 360 end
Chris@1115 361
Chris@1115 362 def test_macros_should_be_escaped_in_pre_tags
Chris@1115 363 text = "<pre>{{hello_world(<tag>)}}</pre>"
Chris@1115 364 assert_equal "<pre>{{hello_world(&lt;tag&gt;)}}</pre>", textilizable(text)
Chris@1115 365 end
Chris@1115 366
Chris@1115 367 def test_macros_should_not_mangle_next_macros_outputs
Chris@1115 368 text = '{{macro(2)}} !{{macro(2)}} {{hello_world(foo)}}'
Chris@1115 369 assert_equal '<p>{{macro(2)}} {{macro(2)}} Hello world! Object: NilClass, Arguments: foo and no block of text.</p>', textilizable(text)
Chris@1115 370 end
Chris@1115 371
Chris@1115 372 def test_macros_with_text_should_not_mangle_following_macros
Chris@1115 373 text = <<-RAW
Chris@1115 374 {{hello_world
Chris@1115 375 Line of text
Chris@1115 376 }}
Chris@1115 377
Chris@1115 378 {{hello_world
Chris@1115 379 Another line of text
Chris@1115 380 }}
Chris@1115 381 RAW
Chris@1115 382
Chris@1115 383 expected = <<-EXPECTED
Chris@1115 384 <p>Hello world! Object: NilClass, Called with no argument and a 12 bytes long block of text.</p>
Chris@1115 385 <p>Hello world! Object: NilClass, Called with no argument and a 20 bytes long block of text.</p>
Chris@1115 386 EXPECTED
Chris@1115 387
Chris@1115 388 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '')
Chris@1115 389 end
Chris@0 390 end