Chris@0: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@119: require File.expand_path('../../../../../test_helper', __FILE__) Chris@0: Chris@909: class Redmine::WikiFormatting::MacrosTest < ActionView::TestCase Chris@0: include ApplicationHelper Chris@0: include ActionView::Helpers::TextHelper chris@37: include ActionView::Helpers::SanitizeHelper Chris@1115: include ERB::Util chris@37: extend ActionView::Helpers::SanitizeHelper::ClassMethods Chris@909: Chris@0: fixtures :projects, :roles, :enabled_modules, :users, Chris@909: :repositories, :changesets, Chris@0: :trackers, :issue_statuses, :issues, Chris@0: :versions, :documents, Chris@0: :wikis, :wiki_pages, :wiki_contents, Chris@0: :boards, :messages, Chris@0: :attachments Chris@0: Chris@0: def setup Chris@0: super Chris@0: @project = nil Chris@0: end Chris@909: Chris@0: def teardown Chris@0: end Chris@909: Chris@1115: def test_macro_registration Chris@1115: Redmine::WikiFormatting::Macros.register do Chris@1115: macro :foo do |obj, args| Chris@1115: "Foo: #{args.size} (#{args.join(',')}) (#{args.class.name})" Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: assert_equal '

Foo: 0 () (Array)

', textilizable("{{foo}}") Chris@1115: assert_equal '

Foo: 0 () (Array)

', textilizable("{{foo()}}") Chris@1115: assert_equal '

Foo: 1 (arg1) (Array)

', textilizable("{{foo(arg1)}}") Chris@1115: assert_equal '

Foo: 2 (arg1,arg2) (Array)

', textilizable("{{foo(arg1, arg2)}}") Chris@1115: end Chris@1115: Chris@1115: def test_macro_registration_parse_args_set_to_false_should_disable_arguments_parsing Chris@1115: Redmine::WikiFormatting::Macros.register do Chris@1115: macro :bar, :parse_args => false do |obj, args| Chris@1115: "Bar: (#{args}) (#{args.class.name})" Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: assert_equal '

Bar: (args, more args) (String)

', textilizable("{{bar(args, more args)}}") Chris@1115: assert_equal '

Bar: () (String)

', textilizable("{{bar}}") Chris@1115: assert_equal '

Bar: () (String)

', textilizable("{{bar()}}") Chris@1115: end Chris@1115: Chris@1115: def test_macro_registration_with_3_args_should_receive_text_argument Chris@1115: Redmine::WikiFormatting::Macros.register do Chris@1115: macro :baz do |obj, args, text| Chris@1115: "Baz: (#{args.join(',')}) (#{text.class.name}) (#{text})" Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: assert_equal "

Baz: () (NilClass) ()

", textilizable("{{baz}}") Chris@1115: assert_equal "

Baz: () (NilClass) ()

", textilizable("{{baz()}}") Chris@1115: assert_equal "

Baz: () (String) (line1\nline2)

", textilizable("{{baz()\nline1\nline2\n}}") Chris@1115: assert_equal "

Baz: (arg1,arg2) (String) (line1\nline2)

", textilizable("{{baz(arg1, arg2)\nline1\nline2\n}}") Chris@1115: end Chris@1115: Chris@1294: def test_macro_name_with_upper_case Chris@1294: Redmine::WikiFormatting::Macros.macro(:UpperCase) {|obj, args| "Upper"} Chris@1294: Chris@1294: assert_equal "

Upper

", textilizable("{{UpperCase}}") Chris@1294: end Chris@1294: Chris@1115: def test_multiple_macros_on_the_same_line Chris@1115: Redmine::WikiFormatting::Macros.macro :foo do |obj, args| Chris@1115: args.any? ? "args: #{args.join(',')}" : "no args" Chris@1115: end Chris@1115: Chris@1115: assert_equal '

no args no args

', textilizable("{{foo}} {{foo}}") Chris@1115: assert_equal '

args: a,b no args

', textilizable("{{foo(a,b)}} {{foo}}") Chris@1115: assert_equal '

args: a,b args: c,d

', textilizable("{{foo(a,b)}} {{foo(c,d)}}") Chris@1115: assert_equal '

no args args: c,d

', textilizable("{{foo}} {{foo(c,d)}}") Chris@1115: end Chris@1115: Chris@1115: def test_macro_should_receive_the_object_as_argument_when_with_object_and_attribute Chris@1115: issue = Issue.find(1) Chris@1115: issue.description = "{{hello_world}}" Chris@1115: assert_equal '

Hello world! Object: Issue, Called with no argument and no block of text.

', textilizable(issue, :description) Chris@1115: end Chris@1115: Chris@1115: def test_macro_should_receive_the_object_as_argument_when_called_with_object_option Chris@1115: text = "{{hello_world}}" Chris@1115: assert_equal '

Hello world! Object: Issue, Called with no argument and no block of text.

', textilizable(text, :object => Issue.find(1)) Chris@1115: end Chris@1115: Chris@1115: def test_extract_macro_options_should_with_args Chris@1115: options = extract_macro_options(["arg1", "arg2"], :foo, :size) Chris@1115: assert_equal([["arg1", "arg2"], {}], options) Chris@1115: end Chris@1115: Chris@1115: def test_extract_macro_options_should_with_options Chris@1115: options = extract_macro_options(["foo=bar", "size=2"], :foo, :size) Chris@1115: assert_equal([[], {:foo => "bar", :size => "2"}], options) Chris@1115: end Chris@1115: Chris@1115: def test_extract_macro_options_should_with_args_and_options Chris@1115: options = extract_macro_options(["arg1", "arg2", "foo=bar", "size=2"], :foo, :size) Chris@1115: assert_equal([["arg1", "arg2"], {:foo => "bar", :size => "2"}], options) Chris@1115: end Chris@1115: Chris@1115: def test_extract_macro_options_should_parse_options_lazily Chris@1115: options = extract_macro_options(["params=x=1&y=2"], :params) Chris@1115: assert_equal([[], {:params => "x=1&y=2"}], options) Chris@1115: end Chris@1115: Chris@1115: def test_macro_exception_should_be_displayed Chris@1115: Redmine::WikiFormatting::Macros.macro :exception do |obj, args| Chris@1115: raise "My message" Chris@1115: end Chris@1115: Chris@1115: text = "{{exception}}" Chris@1115: assert_include '
Error executing the exception macro (My message)
', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_macro_arguments_should_not_be_parsed_by_formatters Chris@1115: text = '{{hello_world(http://www.redmine.org, #1)}}' Chris@1115: assert_include 'Arguments: http://www.redmine.org, #1', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_exclamation_mark_should_not_run_macros Chris@1115: text = "!{{hello_world}}" Chris@1115: assert_equal '

{{hello_world}}

', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_exclamation_mark_should_escape_macros Chris@1115: text = "!{{hello_world()}}" Chris@1115: assert_equal '

{{hello_world(<tag>)}}

', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_unknown_macros_should_not_be_replaced Chris@1115: text = "{{unknown}}" Chris@1115: assert_equal '

{{unknown}}

', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_unknown_macros_should_parsed_as_text Chris@1115: text = "{{unknown(*test*)}}" Chris@1115: assert_equal '

{{unknown(test)}}

', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_unknown_macros_should_be_escaped Chris@1115: text = "{{unknown()}}" Chris@1115: assert_equal '

{{unknown(<tag>)}}

', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_html_safe_macro_output_should_not_be_escaped Chris@1115: Redmine::WikiFormatting::Macros.macro :safe_macro do |obj, args| Chris@1115: "".html_safe Chris@1115: end Chris@1115: assert_equal '

', textilizable("{{safe_macro}}") Chris@1115: end Chris@1115: Chris@0: def test_macro_hello_world Chris@0: text = "{{hello_world}}" Chris@0: assert textilizable(text).match(/Hello world!/) Chris@1115: end Chris@1115: Chris@1115: def test_macro_hello_world_should_escape_arguments Chris@1115: text = "{{hello_world()}}" Chris@1115: assert_include 'Arguments: <tag>', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_macro_macro_list Chris@1115: text = "{{macro_list}}" Chris@1115: assert_match %r{hello_world}, textilizable(text) Chris@0: end Chris@909: Chris@0: def test_macro_include Chris@0: @project = Project.find(1) Chris@0: # include a page of the current project wiki Chris@0: text = "{{include(Another page)}}" Chris@1115: assert_include 'This is a link to a ticket', textilizable(text) Chris@909: Chris@0: @project = nil Chris@0: # include a page of a specific project wiki Chris@0: text = "{{include(ecookbook:Another page)}}" Chris@1115: assert_include 'This is a link to a ticket', textilizable(text) Chris@0: Chris@0: text = "{{include(ecookbook:)}}" Chris@1115: assert_include 'CookBook documentation', textilizable(text) Chris@0: Chris@0: text = "{{include(unknowidentifier:somepage)}}" Chris@1115: assert_include 'Page not found', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_macro_collapse Chris@1115: text = "{{collapse\n*Collapsed* block of text\n}}" Chris@1115: result = textilizable(text) Chris@1115: Chris@1115: assert_select_in result, 'div.collapsed-text' Chris@1115: assert_select_in result, 'strong', :text => 'Collapsed' Chris@1115: assert_select_in result, 'a.collapsible.collapsed', :text => 'Show' Chris@1115: assert_select_in result, 'a.collapsible', :text => 'Hide' Chris@1115: end Chris@1115: Chris@1115: def test_macro_collapse_with_one_arg Chris@1115: text = "{{collapse(Example)\n*Collapsed* block of text\n}}" Chris@1115: result = textilizable(text) Chris@1115: Chris@1115: assert_select_in result, 'div.collapsed-text' Chris@1115: assert_select_in result, 'strong', :text => 'Collapsed' Chris@1115: assert_select_in result, 'a.collapsible.collapsed', :text => 'Example' Chris@1115: assert_select_in result, 'a.collapsible', :text => 'Example' Chris@1115: end Chris@1115: Chris@1115: def test_macro_collapse_with_two_args Chris@1115: text = "{{collapse(Show example, Hide example)\n*Collapsed* block of text\n}}" Chris@1115: result = textilizable(text) Chris@1115: Chris@1115: assert_select_in result, 'div.collapsed-text' Chris@1115: assert_select_in result, 'strong', :text => 'Collapsed' Chris@1115: assert_select_in result, 'a.collapsible.collapsed', :text => 'Show example' Chris@1115: assert_select_in result, 'a.collapsible', :text => 'Hide example' Chris@0: end Chris@909: Chris@1517: def test_macro_collapse_should_not_break_toc Chris@1517: text = <<-RAW Chris@1517: {{toc}} Chris@1517: Chris@1517: h1. Title Chris@1517: Chris@1517: {{collapse(Show example, Hide example) Chris@1517: h2. Heading Chris@1517: }}" Chris@1517: RAW Chris@1517: Chris@1517: expected_toc = '' Chris@1517: Chris@1517: assert_include expected_toc, textilizable(text).gsub(/[\r\n]/, '') Chris@1517: end Chris@1517: Chris@0: def test_macro_child_pages Chris@0: expected = "

\n

" Chris@909: Chris@0: @project = Project.find(1) Chris@0: # child pages of the current wiki page Chris@0: assert_equal expected, textilizable("{{child_pages}}", :object => WikiPage.find(2).content) Chris@0: # child pages of another page Chris@0: assert_equal expected, textilizable("{{child_pages(Another_page)}}", :object => WikiPage.find(1).content) Chris@909: Chris@0: @project = Project.find(2) Chris@0: assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page)}}", :object => WikiPage.find(1).content) Chris@0: end Chris@909: Chris@1115: def test_macro_child_pages_with_parent_option Chris@0: expected = "

\n

" Chris@909: Chris@0: @project = Project.find(1) Chris@0: # child pages of the current wiki page Chris@0: assert_equal expected, textilizable("{{child_pages(parent=1)}}", :object => WikiPage.find(2).content) Chris@0: # child pages of another page Chris@0: assert_equal expected, textilizable("{{child_pages(Another_page, parent=1)}}", :object => WikiPage.find(1).content) Chris@909: Chris@0: @project = Project.find(2) Chris@0: assert_equal expected, textilizable("{{child_pages(ecookbook:Another_page, parent=1)}}", :object => WikiPage.find(1).content) Chris@0: end Chris@1115: Chris@1115: def test_macro_child_pages_with_depth_option Chris@1115: expected = "

    \n" + Chris@1115: "
  • Child 1
  • \n" + Chris@1115: "
  • Child 2
  • \n" + Chris@1115: "
\n

" Chris@1115: Chris@1115: @project = Project.find(1) Chris@1115: assert_equal expected, textilizable("{{child_pages(depth=1)}}", :object => WikiPage.find(2).content) Chris@1115: end Chris@1115: Chris@1115: def test_macro_child_pages_without_wiki_page_should_fail Chris@1115: assert_match /can be called from wiki pages only/, textilizable("{{child_pages}}") Chris@1115: end Chris@1115: Chris@1115: def test_macro_thumbnail Chris@1517: link = link_to('testfile.PNG'.html_safe, Chris@1517: "http://test.host/attachments/17", Chris@1517: :class => "thumbnail", Chris@1517: :title => "testfile.PNG") Chris@1517: assert_equal "

#{link}

", Chris@1517: textilizable("{{thumbnail(testfile.png)}}", :object => Issue.find(14)) Chris@1115: end Chris@1115: Chris@1115: def test_macro_thumbnail_with_size Chris@1517: link = link_to('testfile.PNG'.html_safe, Chris@1517: "http://test.host/attachments/17", Chris@1517: :class => "thumbnail", Chris@1517: :title => "testfile.PNG") Chris@1517: assert_equal "

#{link}

", Chris@1517: textilizable("{{thumbnail(testfile.png, size=200)}}", :object => Issue.find(14)) Chris@1115: end Chris@1115: Chris@1115: def test_macro_thumbnail_with_title Chris@1517: link = link_to('testfile.PNG'.html_safe, Chris@1517: "http://test.host/attachments/17", Chris@1517: :class => "thumbnail", Chris@1517: :title => "Cool image") Chris@1517: assert_equal "

#{link}

", Chris@1517: textilizable("{{thumbnail(testfile.png, title=Cool image)}}", :object => Issue.find(14)) Chris@1115: end Chris@1115: Chris@1115: def test_macro_thumbnail_with_invalid_filename_should_fail Chris@1115: assert_include 'test.png not found', Chris@1115: textilizable("{{thumbnail(test.png)}}", :object => Issue.find(14)) Chris@1115: end Chris@1115: Chris@1115: def test_macros_should_not_be_executed_in_pre_tags Chris@1115: text = <<-RAW Chris@1115: {{hello_world(foo)}} Chris@1115: Chris@1115:
Chris@1115: {{hello_world(pre)}}
Chris@1115: !{{hello_world(pre)}}
Chris@1115: 
Chris@1115: Chris@1115: {{hello_world(bar)}} Chris@1115: RAW Chris@1115: Chris@1115: expected = <<-EXPECTED Chris@1115:

Hello world! Object: NilClass, Arguments: foo and no block of text.

Chris@1115: Chris@1115:
Chris@1115: {{hello_world(pre)}}
Chris@1115: !{{hello_world(pre)}}
Chris@1115: 
Chris@1115: Chris@1115:

Hello world! Object: NilClass, Arguments: bar and no block of text.

Chris@1115: EXPECTED Chris@1115: Chris@1115: assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '') Chris@1115: end Chris@1115: Chris@1115: def test_macros_should_be_escaped_in_pre_tags Chris@1115: text = "
{{hello_world()}}
" Chris@1115: assert_equal "
{{hello_world(<tag>)}}
", textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_macros_should_not_mangle_next_macros_outputs Chris@1115: text = '{{macro(2)}} !{{macro(2)}} {{hello_world(foo)}}' Chris@1115: assert_equal '

{{macro(2)}} {{macro(2)}} Hello world! Object: NilClass, Arguments: foo and no block of text.

', textilizable(text) Chris@1115: end Chris@1115: Chris@1115: def test_macros_with_text_should_not_mangle_following_macros Chris@1115: text = <<-RAW Chris@1115: {{hello_world Chris@1115: Line of text Chris@1115: }} Chris@1115: Chris@1115: {{hello_world Chris@1115: Another line of text Chris@1115: }} Chris@1115: RAW Chris@1115: Chris@1115: expected = <<-EXPECTED Chris@1115:

Hello world! Object: NilClass, Called with no argument and a 12 bytes long block of text.

Chris@1115:

Hello world! Object: NilClass, Called with no argument and a 20 bytes long block of text.

Chris@1115: EXPECTED Chris@1115: Chris@1115: assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(text).gsub(%r{[\r\n\t]}, '') Chris@1115: end Chris@0: end