annotate .svn/pristine/85/85306fd51daa2a7ce741b92cbd12f18f043a6b0d.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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