Index: twikiplugins/UnitTestContrib/test/unit/Fn_URLPARAM.pm =================================================================== --- twikiplugins/UnitTestContrib/test/unit/Fn_URLPARAM.pm (revision 0) +++ twikiplugins/UnitTestContrib/test/unit/Fn_URLPARAM.pm (revision 0) @@ -0,0 +1,156 @@ +use strict; + +# tests for the correct expansion of URLPARAM + +package Fn_URLPARAM; + +use base qw( TWikiFnTestCase ); + +use TWiki; +use Error qw( :try ); + +sub new { + my $self = shift()->SUPER::new('GROUPS', @_); + return $self; +} + +sub set_up { + my $this = shift; + $this->SUPER::set_up(@_); +} + +sub test_default { + my $this = shift; + + my $str; + + # test default parameter + + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('', "$str"); + + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default="0"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('0', "$str"); + + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default=""}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('', "$str"); + + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default="bar"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('bar', "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'foo', -value=>'bar'); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default="0"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('bar', "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'foo', -value=>'0'); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default="bar"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('0', "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'foo', -value=>''); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default="bar"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('', "$str"); +} + +sub test_encode { + my $this = shift; + + my $str; + + $this->{twiki}->{cgiQuery}->param( -name=>'foo', -value=>'&?*!"'); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" encode="entity"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('&?*!"', "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'foo', -value=>'&?*!" '); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" encode="url"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('%26%3f*!%22%20', "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'foo', -value=>'&?*!" '); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" encode="quote"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('&?*!\" ', "$str"); +} + +sub test_defaultencode { + my $this = shift; + + my $str; + + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default="&?*!\" " encode="entity"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('&?*!" ', "$str"); + + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default="&?*!\" " encode="url"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('&?*!" ', "$str"); + + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"foo" default="&?*!\" " encode="quote"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals('&?*!" ', "$str"); + + +} + +sub test_multiple { + my $this = shift; + + my $str; + + my @multiple=('foo','bar','baz'); + + $this->{twiki}->{cgiQuery}->param( -name=>'multi', -value=>['foo','bar','baz']); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"multi" multiple="on"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals("foo\nbar\nbaz", "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'multi', -value=>['foo','bar','baz']); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"multi" multiple="on" separator=","}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals("foo,bar,baz", "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'multi', -value=>['foo','bar','baz']); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"multi" multiple="on" separator=""}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals("foobarbaz", "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'multi', -value=>['foo','bar','baz']); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"multi" multiple="-$item-" separator=" "}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals("-foo- -bar- -baz-", "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'multi', -value=>['foo','bar','baz']); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"multi" multiple="-$item-" separator=""}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals("-foo--bar--baz-", "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'multi', -value=>['foo','bar','baz']); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"multi" multiple="-$item-"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals("-foo-\n-bar-\n-baz-", "$str"); +} + +sub test_newline { + my $this = shift; + + my $str; + + $this->{twiki}->{cgiQuery}->param( -name=>'textarea', -value=>"foo\nbar\nbaz\n"); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"textarea" newline="-"}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals("foo-bar-baz-", "$str"); + + $this->{twiki}->{cgiQuery}->param( -name=>'textarea', -value=>"foo\nbar\nbaz\n"); + $str = $this->{twiki}->handleCommonTags( + '%URLPARAM{"textarea" newline=""}%', $this->{test_web}, $this->{test_topic}); + $this->assert_str_equals("foobarbaz", "$str"); +} + +1; Index: lib/TWiki.pm =================================================================== --- lib/TWiki.pm (revision 14505) +++ lib/TWiki.pm (working copy) @@ -3423,12 +3423,13 @@ sub URLPARAM { my( $this, $params ) = @_; my $param = $params->{_DEFAULT} || ''; - my $newLine = $params->{newline} || ''; + my $newLine = $params->{newline}; my $encode = $params->{encode}; my $multiple = $params->{multiple}; - my $separator = $params->{separator} || "\n"; + my $separator = $params->{separator}; + $separator="\n" unless (defined $separator); - my $value = ''; + my $value; if( $this->{cgiQuery} ) { if( TWiki::isTrue( $multiple )) { my @valueArray = $this->{cgiQuery}->param( $param ); @@ -3447,21 +3448,22 @@ } } else { $value = $this->{cgiQuery}->param( $param ); - $value = '' unless( defined $value ); } } - $value =~ s/\r?\n/$newLine/go if( $newLine ); - if ( $encode ) { - if ( $encode =~ /^entit(y|ies)$/i ) { - $value = entityEncode( $value ); - } elsif ( $encode =~ /^quotes?$/i ) { - $value =~ s/\"/\\"/go; # escape quotes with backslash (Bugs:Item3383 fix) - } else { - $value =~ s/\r*\n\r*/
/; # Legacy - $value = urlEncode( $value ); + if(defined $value) { + $value =~ s/\r?\n/$newLine/go if( defined $newLine ); + if ( $encode ) { + if ( $encode =~ /^entit(y|ies)$/i ) { + $value = entityEncode( $value ); + } elsif ( $encode =~ /^quotes?$/i ) { + $value =~ s/\"/\\"/go; # escape quotes with backslash (Bugs:Item3383 fix) + } else { + $value =~ s/\r*\n\r*/
/; # Legacy + $value = urlEncode( $value ); + } } } - unless( $value ) { + unless( defined $value ) { $value = $params->{default}; $value = '' unless defined $value; }