annotate test/unit/lib/redmine/wiki_formatting/macros_test.rb @ 1155:a34a06ba1e33 redmine-2.2-integration

Provide a JS callback which should (?) be rendered by toggle_active ajax call
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 15 Jan 2013 15:48:21 +0000
parents 433d4f72a19b
children 3e4c3460b6ca
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@1115 81 def test_multiple_macros_on_the_same_line
Chris@1115 82 Redmine::WikiFormatting::Macros.macro :foo do |obj, args|
Chris@1115 83 args.any? ? "args: #{args.join(',')}" : "no args"
Chris@1115 84 end
Chris@1115 85
Chris@1115 86 assert_equal '<p>no args no args</p>', textilizable("{{foo}} {{foo}}")
Chris@1115 87 assert_equal '<p>args: a,b no args</p>', textilizable("{{foo(a,b)}} {{foo}}")
Chris@1115 88 assert_equal '<p>args: a,b args: c,d</p>', textilizable("{{foo(a,b)}} {{foo(c,d)}}")
Chris@1115 89 assert_equal '<p>no args args: c,d</p>', textilizable("{{foo}} {{foo(c,d)}}")
Chris@1115 90 end
Chris@1115 91
Chris@1115 92 def test_macro_should_receive_the_object_as_argument_when_with_object_and_attribute
Chris@1115 93 issue = Issue.find(1)
Chris@1115 94 issue.description = "{{hello_world}}"
Chris@1115 95 assert_equal '<p>Hello world! Object: Issue, Called with no argument and no block of text.</p>', textilizable(issue, :description)
Chris@1115 96 end
Chris@1115 97
Chris@1115 98 def test_macro_should_receive_the_object_as_argument_when_called_with_object_option
Chris@1115 99 text = "{{hello_world}}"
Chris@1115 100 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 101 end
Chris@1115 102
Chris@1115 103 def test_extract_macro_options_should_with_args
Chris@1115 104 options = extract_macro_options(["arg1", "arg2"], :foo, :size)
Chris@1115 105 assert_equal([["arg1", "arg2"], {}], options)
Chris@1115 106 end
Chris@1115 107
Chris@1115 108 def test_extract_macro_options_should_with_options
Chris@1115 109 options = extract_macro_options(["foo=bar", "size=2"], :foo, :size)
Chris@1115 110 assert_equal([[], {:foo => "bar", :size => "2"}], options)
Chris@1115 111 end
Chris@1115 112
Chris@1115 113 def test_extract_macro_options_should_with_args_and_options
Chris@1115 114 options = extract_macro_options(["arg1", "arg2", "foo=bar", "size=2"], :foo, :size)
Chris@1115 115 assert_equal([["arg1", "arg2"], {:foo => "bar", :size => "2"}], options)
Chris@1115 116 end
Chris@1115 117
Chris@1115 118 def test_extract_macro_options_should_parse_options_lazily
Chris@1115 119 options = extract_macro_options(["params=x=1&y=2"], :params)
Chris@1115 120 assert_equal([[], {:params => "x=1&y=2"}], options)
Chris@1115 121 end
Chris@1115 122
Chris@1115 123 def test_macro_exception_should_be_displayed
Chris@1115 124 Redmine::WikiFormatting::Macros.macro :exception do |obj, args|
Chris@1115 125 raise "My message"
Chris@1115 126 end
Chris@1115 127
Chris@1115 128 text = "{{exception}}"
Chris@1115 129 assert_include '<div class="flash error">Error executing the <strong>exception</strong> macro (My message)</div>', textilizable(text)
Chris@1115 130 end
Chris@1115 131
Chris@1115 132 def test_macro_arguments_should_not_be_parsed_by_formatters
Chris@1115 133 text = '{{hello_world(http://www.redmine.org, #1)}}'
Chris@1115 134 assert_include 'Arguments: http://www.redmine.org, #1', textilizable(text)
Chris@1115 135 end
Chris@1115 136
Chris@1115 137 def test_exclamation_mark_should_not_run_macros
Chris@1115 138 text = "!{{hello_world}}"
Chris@1115 139 assert_equal '<p>{{hello_world}}</p>', textilizable(text)
Chris@1115 140 end
Chris@1115 141
Chris@1115 142 def test_exclamation_mark_should_escape_macros
Chris@1115 143 text = "!{{hello_world(<tag>)}}"
Chris@1115 144 assert_equal '<p>{{hello_world(&lt;tag&gt;)}}</p>', textilizable(text)
Chris@1115 145 end
Chris@1115 146
Chris@1115 147 def test_unknown_macros_should_not_be_replaced
Chris@1115 148 text = "{{unknown}}"
Chris@1115 149 assert_equal '<p>{{unknown}}</p>', textilizable(text)
Chris@1115 150 end
Chris@1115 151
Chris@1115 152 def test_unknown_macros_should_parsed_as_text
Chris@1115 153 text = "{{unknown(*test*)}}"
Chris@1115 154 assert_equal '<p>{{unknown(<strong>test</strong>)}}</p>', textilizable(text)
Chris@1115 155 end
Chris@1115 156
Chris@1115 157 def test_unknown_macros_should_be_escaped
Chris@1115 158 text = "{{unknown(<tag>)}}"
Chris@1115 159 assert_equal '<p>{{unknown(&lt;tag&gt;)}}</p>', textilizable(text)
Chris@1115 160 end
Chris@1115 161
Chris@1115 162 def test_html_safe_macro_output_should_not_be_escaped
Chris@1115 163 Redmine::WikiFormatting::Macros.macro :safe_macro do |obj, args|
Chris@1115 164 "<tag>".html_safe
Chris@1115 165 end
Chris@1115 166 assert_equal '<p><tag></p>', textilizable("{{safe_macro}}")
Chris@1115 167 end
Chris@1115 168
Chris@0 169 def test_macro_hello_world
Chris@0 170 text = "{{hello_world}}"
Chris@0 171 assert textilizable(text).match(/Hello world!/)
Chris@1115 172 end
Chris@1115 173
Chris@1115 174 def test_macro_hello_world_should_escape_arguments
Chris@1115 175 text = "{{hello_world(<tag>)}}"
Chris@1115 176 assert_include 'Arguments: &lt;tag&gt;', textilizable(text)
Chris@1115 177 end
Chris@1115 178
Chris@1115 179 def test_macro_macro_list
Chris@1115 180 text = "{{macro_list}}"
Chris@1115 181 assert_match %r{<code>hello_world</code>}, textilizable(text)
Chris@0 182 end
Chris@909 183
Chris@0 184 def test_macro_include
Chris@0 185 @project = Project.find(1)
Chris@0 186 # include a page of the current project wiki
Chris@0 187 text = "{{include(Another page)}}"
Chris@1115 188 assert_include 'This is a link to a ticket', textilizable(text)
Chris@909 189
Chris@0 190 @project = nil
Chris@0 191 # include a page of a specific project wiki
Chris@0 192 text = "{{include(ecookbook:Another page)}}"
Chris@1115 193 assert_include 'This is a link to a ticket', textilizable(text)
Chris@0 194
Chris@0 195 text = "{{include(ecookbook:)}}"
Chris@1115 196 assert_include 'CookBook documentation', textilizable(text)
Chris@0 197
Chris@0 198 text = "{{include(unknowidentifier:somepage)}}"
Chris@1115 199 assert_include 'Page not found', textilizable(text)
Chris@1115 200 end
Chris@1115 201
Chris@1115 202 def test_macro_collapse
Chris@1115 203 text = "{{collapse\n*Collapsed* block of text\n}}"
Chris@1115 204 result = textilizable(text)
Chris@1115 205
Chris@1115 206 assert_select_in result, 'div.collapsed-text'
Chris@1115 207 assert_select_in result, 'strong', :text => 'Collapsed'
Chris@1115 208 assert_select_in result, 'a.collapsible.collapsed', :text => 'Show'
Chris@1115 209 assert_select_in result, 'a.collapsible', :text => 'Hide'
Chris@1115 210 end
Chris@1115 211
Chris@1115 212 def test_macro_collapse_with_one_arg
Chris@1115 213 text = "{{collapse(Example)\n*Collapsed* block of text\n}}"
Chris@1115 214 result = textilizable(text)
Chris@1115 215
Chris@1115 216 assert_select_in result, 'div.collapsed-text'
Chris@1115 217 assert_select_in result, 'strong', :text => 'Collapsed'
Chris@1115 218 assert_select_in result, 'a.collapsible.collapsed', :text => 'Example'
Chris@1115 219 assert_select_in result, 'a.collapsible', :text => 'Example'
Chris@1115 220 end
Chris@1115 221
Chris@1115 222 def test_macro_collapse_with_two_args
Chris@1115 223 text = "{{collapse(Show example, Hide example)\n*Collapsed* block of text\n}}"
Chris@1115 224 result = textilizable(text)
Chris@1115 225
Chris@1115 226 assert_select_in result, 'div.collapsed-text'
Chris@1115 227 assert_select_in result, 'strong', :text => 'Collapsed'
Chris@1115 228 assert_select_in result, 'a.collapsible.collapsed', :text => 'Show example'
Chris@1115 229 assert_select_in result, 'a.collapsible', :text => 'Hide example'
Chris@0 230 end
Chris@909 231
Chris@0 232 def test_macro_child_pages
Chris@0 233 expected = "<p><ul class=\"pages-hierarchy\">\n" +
Chris@1115 234 "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a>\n" +
Chris@1115 235 "<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 236 "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
Chris@0 237 "</ul>\n</p>"
Chris@909 238
Chris@0 239 @project = Project.find(1)
Chris@0 240 # child pages of the current wiki page
Chris@0 241 assert_equal expected, textilizable("{{child_pages}}", :object => WikiPage.find(2).content)
Chris@0 242 # child pages of another page
Chris@0 243 assert_equal expected, textilizable("{{child_pages(Another_page)}}", :object => WikiPage.find(1).content)
Chris@909 244
Chris@0 245 @project = Project.find(2)
Chris@0 246 assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page)}}", :object => WikiPage.find(1).content)
Chris@0 247 end
Chris@909 248
Chris@1115 249 def test_macro_child_pages_with_parent_option
Chris@0 250 expected = "<p><ul class=\"pages-hierarchy\">\n" +
Chris@0 251 "<li><a href=\"/projects/ecookbook/wiki/Another_page\">Another page</a>\n" +
Chris@0 252 "<ul class=\"pages-hierarchy\">\n" +
Chris@1115 253 "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a>\n" +
Chris@1115 254 "<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 255 "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
Chris@0 256 "</ul>\n</li>\n</ul>\n</p>"
Chris@909 257
Chris@0 258 @project = Project.find(1)
Chris@0 259 # child pages of the current wiki page
Chris@0 260 assert_equal expected, textilizable("{{child_pages(parent=1)}}", :object => WikiPage.find(2).content)
Chris@0 261 # child pages of another page
Chris@0 262 assert_equal expected, textilizable("{{child_pages(Another_page, parent=1)}}", :object => WikiPage.find(1).content)
Chris@909 263
Chris@0 264 @project = Project.find(2)
Chris@0 265 assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page, parent=1)}}", :object => WikiPage.find(1).content)
Chris@0 266 end
Chris@1115 267
Chris@1115 268 def test_macro_child_pages_with_depth_option
Chris@1115 269 expected = "<p><ul class=\"pages-hierarchy\">\n" +
Chris@1115 270 "<li><a href=\"/projects/ecookbook/wiki/Child_1\">Child 1</a></li>\n" +
Chris@1115 271 "<li><a href=\"/projects/ecookbook/wiki/Child_2\">Child 2</a></li>\n" +
Chris@1115 272 "</ul>\n</p>"
Chris@1115 273
Chris@1115 274 @project = Project.find(1)
Chris@1115 275 assert_equal expected, textilizable("{{child_pages(depth=1)}}", :object => WikiPage.find(2).content)
Chris@1115 276 end
Chris@1115 277
Chris@1115 278 def test_macro_child_pages_without_wiki_page_should_fail
Chris@1115 279 assert_match /can be called from wiki pages only/, textilizable("{{child_pages}}")
Chris@1115 280 end
Chris@1115 281
Chris@1115 282 def test_macro_thumbnail
Chris@1115 283 assert_equal '<p><a href="/attachments/17" class="thumbnail" title="testfile.PNG"><img alt="testfile.PNG" src="/attachments/thumbnail/17" /></a></p>',
Chris@1115 284 textilizable("{{thumbnail(testfile.png)}}", :object => Issue.find(14))
Chris@1115 285 end
Chris@1115 286
Chris@1115 287 def test_macro_thumbnail_with_size
Chris@1115 288 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 289 textilizable("{{thumbnail(testfile.png, size=200)}}", :object => Issue.find(14))
Chris@1115 290 end
Chris@1115 291
Chris@1115 292 def test_macro_thumbnail_with_title
Chris@1115 293 assert_equal '<p><a href="/attachments/17" class="thumbnail" title="Cool image"><img alt="testfile.PNG" src="/attachments/thumbnail/17" /></a></p>',
Chris@1115 294 textilizable("{{thumbnail(testfile.png, title=Cool image)}}", :object => Issue.find(14))
Chris@1115 295 end
Chris@1115 296
Chris@1115 297 def test_macro_thumbnail_with_invalid_filename_should_fail
Chris@1115 298 assert_include 'test.png not found',
Chris@1115 299 textilizable("{{thumbnail(test.png)}}", :object => Issue.find(14))
Chris@1115 300 end
Chris@1115 301
Chris@1115 302 def test_macros_should_not_be_executed_in_pre_tags
Chris@1115 303 text = <<-RAW
Chris@1115 304 {{hello_world(foo)}}
Chris@1115 305
Chris@1115 306 <pre>
Chris@1115 307 {{hello_world(pre)}}
Chris@1115 308 !{{hello_world(pre)}}
Chris@1115 309 </pre>
Chris@1115 310
Chris@1115 311 {{hello_world(bar)}}
Chris@1115 312 RAW
Chris@1115 313
Chris@1115 314 expected = <<-EXPECTED
Chris@1115 315 <p>Hello world! Object: NilClass, Arguments: foo and no block of text.</p>
Chris@1115 316
Chris@1115 317 <pre>
Chris@1115 318 {{hello_world(pre)}}
Chris@1115 319 !{{hello_world(pre)}}
Chris@1115 320 </pre>
Chris@1115 321
Chris@1115 322 <p>Hello world! Object: NilClass, Arguments: bar and no block of text.</p>
Chris@1115 323 EXPECTED
Chris@1115 324
Chris@1115 325 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '')
Chris@1115 326 end
Chris@1115 327
Chris@1115 328 def test_macros_should_be_escaped_in_pre_tags
Chris@1115 329 text = "<pre>{{hello_world(<tag>)}}</pre>"
Chris@1115 330 assert_equal "<pre>{{hello_world(&lt;tag&gt;)}}</pre>", textilizable(text)
Chris@1115 331 end
Chris@1115 332
Chris@1115 333 def test_macros_should_not_mangle_next_macros_outputs
Chris@1115 334 text = '{{macro(2)}} !{{macro(2)}} {{hello_world(foo)}}'
Chris@1115 335 assert_equal '<p>{{macro(2)}} {{macro(2)}} Hello world! Object: NilClass, Arguments: foo and no block of text.</p>', textilizable(text)
Chris@1115 336 end
Chris@1115 337
Chris@1115 338 def test_macros_with_text_should_not_mangle_following_macros
Chris@1115 339 text = <<-RAW
Chris@1115 340 {{hello_world
Chris@1115 341 Line of text
Chris@1115 342 }}
Chris@1115 343
Chris@1115 344 {{hello_world
Chris@1115 345 Another line of text
Chris@1115 346 }}
Chris@1115 347 RAW
Chris@1115 348
Chris@1115 349 expected = <<-EXPECTED
Chris@1115 350 <p>Hello world! Object: NilClass, Called with no argument and a 12 bytes long block of text.</p>
Chris@1115 351 <p>Hello world! Object: NilClass, Called with no argument and a 20 bytes long block of text.</p>
Chris@1115 352 EXPECTED
Chris@1115 353
Chris@1115 354 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '')
Chris@1115 355 end
Chris@0 356 end