annotate .svn/pristine/ab/abce92f16794eda42d844c3ac0c0ab72c8c6b0ec.svn-base @ 1628:9c5f8e24dadc live tip

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