annotate .svn/pristine/f4/f4a618fa8832b4a871286e474a6e7c3dd200647e.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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