From 3758eeb758f0f4dc7a7f64542c1e17973e356d98 Mon Sep 17 00:00:00 2001 From: OlivierRaginel Date: Thu, 2 Sep 2010 13:45:51 +0000 Subject: [PATCH] Item9318: Some cleanup and some perltidy (+ perlcritic even, 3 arguments open) git-svn-id: http://svn.foswiki.org/trunk@8852 0b4bb1d4-4e5a-0410-9cc4-b2b747904278 --- UnitTestContrib/test/unit/FuncTests.pm | 858 +++++++++++++------------ 1 file changed, 464 insertions(+), 394 deletions(-) diff --git a/UnitTestContrib/test/unit/FuncTests.pm b/UnitTestContrib/test/unit/FuncTests.pm index 4ae8e921e9..84aa653cab 100644 --- a/UnitTestContrib/test/unit/FuncTests.pm +++ b/UnitTestContrib/test/unit/FuncTests.pm @@ -32,9 +32,9 @@ sub set_up { $this->registerUser( 'white', 'Mr', "White", 'white@example.com' ); $MrWhite = $this->{session}->{users}->getCanonicalUserID('white'); - $this->{tmpdatafile} = $Foswiki::cfg{TempfileDir} . '/tmpity-tmp.gif'; + $this->{tmpdatafile} = $Foswiki::cfg{TempfileDir} . '/tmpity-tmp.gif'; $this->{tmpdatafile2} = $Foswiki::cfg{TempfileDir} . '/tmpity-tmp2.gif'; - $this->{test_web2} = $this->{test_web} . 'Extra'; + $this->{test_web2} = $this->{test_web} . 'Extra'; my $webObject = Foswiki::Meta->new( $this->{session}, $this->{test_web2} ); $webObject->populateNewWeb(); } @@ -47,6 +47,35 @@ sub tear_down { $this->SUPER::tear_down(); } +# Helper function to wrap asserts around file accesses +sub write_file { + my $this = shift; + my ( $filename, $data, $options ) = @_; + + # Check input parameters + $options ||= {}; + $this->assert_not_null( $filename, + "Need to pass a filename to write_file" ); + + # Write data into the file + my $filehandle; + $this->assert( + open( $filehandle, '>', $filename ), + "Failed to open $filename for writing: $!" + ); + binmode $filehandle if $options->{binmode}; + print $filehandle $data; + close($filehandle); + + return unless $options->{read}; + $this->assert( + open( $filehandle, '<', $filename ), + "Failed to open $filename for reading: $!" + ); + binmode $filehandle if $options->{binmode}; + return $filehandle; +} + sub test_createWeb_permissions { my $this = shift; use Error qw( :try ); @@ -67,20 +96,31 @@ HERE # Verify that create of a root web is denied by default user. try { Foswiki::Func::createWeb("Blahweb"); - } catch Foswiki::AccessControlException with { + } + catch Foswiki::AccessControlException with { my $e = shift; - $this->assert_matches( qr/access not allowed on root/, $e, "Unexpected error $e"); + $this->assert_matches( qr/access not allowed on root/, + $e, "Unexpected error $e" ); }; - $this->assert(! Foswiki::Func::webExists("Blahweb"), "Test should not have created the web"); + $this->assert( + !Foswiki::Func::webExists("Blahweb"), + "Test should not have created the web" + ); - # Verify that create of a sub web is denied by default user if denied in webPreferences. +# Verify that create of a sub web is denied by default user if denied in webPreferences. try { Foswiki::Func::createWeb("$this->{test_web}/Blahsub"); - } catch Foswiki::AccessControlException with { + } + catch Foswiki::AccessControlException with { my $e = shift; - $this->assert_matches( qr/Access to CHANGE TemporaryFuncTestWebFunc\/Blahsub. for BaseUserMapping_666 is denied. access denied on web/, $e, "Unexpected error $e"); + $this->assert_matches( +qr/Access to CHANGE TemporaryFuncTestWebFunc\/Blahsub. for BaseUserMapping_666 is denied. access denied on web/, + $e, + "Unexpected error $e" + ); }; - $this->assert(! Foswiki::Func::webExists("$this->{test_web}/Blahsub"), "Test should not have created the web"); + $this->assert( !Foswiki::Func::webExists("$this->{test_web}/Blahsub"), + "Test should not have created the web" ); $this->{session}->finish(); $this->{session} = new Foswiki( $Foswiki::cfg{AdminUserLogin} ); @@ -94,14 +134,16 @@ END $this->{session}->finish(); $this->{session} = new Foswiki(); - # Verify that create of a sub web is allowed by default user if allowed in webPreferences. +# Verify that create of a sub web is allowed by default user if allowed in webPreferences. try { Foswiki::Func::createWeb("$this->{test_web}/Blahsub"); - } catch Foswiki::AccessControlException with { + } + catch Foswiki::AccessControlException with { my $e = shift; $this->assert("Unexpected error $e"); }; - $this->assert( Foswiki::Func::webExists("$this->{test_web}/Blahsub"), "Test should have created the web"); + $this->assert( Foswiki::Func::webExists("$this->{test_web}/Blahsub"), + "Test should have created the web" ); } sub test_Item9021 { @@ -114,13 +156,19 @@ sub test_Item9021 { $this->{session} = new Foswiki( $Foswiki::cfg{AdminUserLogin} ); try { - Foswiki::Func::createWeb($this->{test_web}."Missing/Blah"); - } catch Error::Simple with { + Foswiki::Func::createWeb( $this->{test_web} . "Missing/Blah" ); + } + catch Error::Simple with { my $e = shift; - $this->assert_matches( qr/^Parent web TemporaryFuncTestWebFuncMissing does not exist.*/, $e, "Unexpected error $e"); + $this->assert_matches( + qr/^Parent web TemporaryFuncTestWebFuncMissing does not exist.*/, + $e, "Unexpected error $e" ); }; - $this->assert(! Foswiki::Func::webExists($this->{test_web}."Missing"), "test should not have created the web"); - $this->assert(! Foswiki::Func::webExists($this->{test_web}."Missing/Blah"), "Test should not have created the web"); + $this->assert( !Foswiki::Func::webExists( $this->{test_web} . "Missing" ), + "test should not have created the web" ); + $this->assert( + !Foswiki::Func::webExists( $this->{test_web} . "Missing/Blah" ), + "Test should not have created the web" ); } sub test_createWeb_InvalidBase { @@ -133,12 +181,16 @@ sub test_createWeb_InvalidBase { $this->{session} = new Foswiki( $Foswiki::cfg{AdminUserLogin} ); try { - Foswiki::Func::createWeb($this->{test_web}."InvaliBase", "Invalidbase"); - } catch Error::Simple with { + Foswiki::Func::createWeb( $this->{test_web} . "InvaliBase", + "Invalidbase" ); + } + catch Error::Simple with { my $e = shift; - $this->assert_matches( qr/^Template web Invalidbase does not exist.*/, $e, "Unexpected error $e"); + $this->assert_matches( qr/^Template web Invalidbase does not exist.*/, + $e, "Unexpected error $e" ); }; - $this->assert(! Foswiki::Func::webExists($this->{test_web}."invaliBase")); + $this->assert( + !Foswiki::Func::webExists( $this->{test_web} . "invaliBase" ) ); } sub test_createWeb_hierarchyDisabled { @@ -151,23 +203,27 @@ sub test_createWeb_hierarchyDisabled { $this->{session} = new Foswiki( $Foswiki::cfg{AdminUserLogin} ); try { - Foswiki::Func::createWeb($this->{test_web} . "/Subweb"); - } catch Error::Simple with { + Foswiki::Func::createWeb( $this->{test_web} . "/Subweb" ); + } + catch Error::Simple with { my $e = shift; - $this->assert_matches( qr/^Unable to create .*- Hierarchical webs are disabled.*/, $e, "Unexpected error $e"); + $this->assert_matches( + qr/^Unable to create .*- Hierarchical webs are disabled.*/, + $e, "Unexpected error $e" ); }; - $this->assert(! Foswiki::Func::webExists($this->{test_web}."/Subweb")); + $this->assert( !Foswiki::Func::webExists( $this->{test_web} . "/Subweb" ) ); } - sub test_moveWeb { my $this = shift; $Foswiki::cfg{EnableHierarchicalWebs} = 1; - my $webObject = Foswiki::Meta->new( $this->{session}, $this->{test_web} . "Blah" ); + my $webObject = + Foswiki::Meta->new( $this->{session}, $this->{test_web} . "Blah" ); $webObject->populateNewWeb(); undef $webObject; - $webObject = Foswiki::Meta->new( $this->{session}, $this->{test_web} . "Blah/SubWeb" ); + $webObject = + Foswiki::Meta->new( $this->{session}, $this->{test_web} . "Blah/SubWeb" ); $webObject->populateNewWeb(); $this->assert( Foswiki::Func::webExists( $this->{test_web} . 'Blah' ) ); @@ -181,19 +237,19 @@ sub test_moveWeb { $this->assert( Foswiki::Func::webExists( $this->{test_web} . 'Blah2' ) ); $this->assert( Foswiki::Func::webExists( $this->{test_web} . 'Blah2/SubWeb' ) ); - $this->removeWebFixture( $this->{session}, $this->{test_web}.'Blah2' ); + $this->removeWebFixture( $this->{session}, $this->{test_web} . 'Blah2' ); } sub test_getViewUrl { my $this = shift; my $ss; - if (defined $Foswiki::cfg{ScriptUrlPaths}{view} ) { + if ( defined $Foswiki::cfg{ScriptUrlPaths}{view} ) { $ss = $Foswiki::cfg{ScriptUrlPaths}{view}; - } + } else { $ss = 'view' . $Foswiki::cfg{ScriptSuffix}; - } + } # relative to specified web my $result = Foswiki::Func::getViewUrl( $this->{users_web}, "WebHome" ); @@ -300,72 +356,76 @@ sub test_leases { # As much as I'd like to remove this dumb function, make sure it's still # compatible sub test_saveTopicText { - my $this = shift; + my $this = shift; my $topic = 'SaveTopicText'; - Foswiki::Func::saveTopicText( - $this->{test_web}, $topic, <{test_web}, $topic, <assert(! - Foswiki::Func::checkAccessPermission( 'CHANGE', - Foswiki::Func::getWikiName(), undef, $topic, $this->{test_web} )); + $this->assert( + !Foswiki::Func::checkAccessPermission( + 'CHANGE', Foswiki::Func::getWikiName(), + undef, $topic, $this->{test_web} + ) + ); # This should fail and return an oopsUrl (FFS, what a shit spec) - my $oopsURL = Foswiki::Func::saveTopicText( - $this->{test_web}, $topic, 'Gasp' ); + my $oopsURL = + Foswiki::Func::saveTopicText( $this->{test_web}, $topic, 'Gasp' ); $this->assert($oopsURL); - my @ri = Foswiki::Func::getRevisionInfo($this->{test_web}, $topic); - $this->assert_matches(qr/1$/, $ri[2]); + my @ri = Foswiki::Func::getRevisionInfo( $this->{test_web}, $topic ); + $this->assert_matches( qr/1$/, $ri[2] ); # This should succeed and return undef - $oopsURL = Foswiki::Func::saveTopicText( - $this->{test_web}, $topic, 'Beam', 1 ); - $this->assert(!$oopsURL, $oopsURL); + $oopsURL = + Foswiki::Func::saveTopicText( $this->{test_web}, $topic, 'Beam', 1 ); + $this->assert( !$oopsURL, $oopsURL ); } sub test_saveTopic { - my $this = shift; + my $this = shift; my $topic = 'SaveTopic'; - Foswiki::Func::saveTopic( - $this->{test_web}, $topic, undef, <{test_web}, $topic, undef, <assert(! - Foswiki::Func::checkAccessPermission( 'CHANGE', - Foswiki::Func::getWikiName(), undef, $topic, $this->{test_web} )); - my @ri = Foswiki::Func::getRevisionInfo($this->{test_web}, $topic); - $this->assert_matches(qr/1$/, $ri[2]); + $this->assert( + !Foswiki::Func::checkAccessPermission( + 'CHANGE', Foswiki::Func::getWikiName(), + undef, $topic, $this->{test_web} + ) + ); + my @ri = Foswiki::Func::getRevisionInfo( $this->{test_web}, $topic ); + $this->assert_matches( qr/1$/, $ri[2] ); # Make sure the meta got into the topic - my ($m, $t) = Foswiki::Func::readTopic($this->{test_web}, $topic); - my $el = $m->get('PREFERENCE', 'Bird'); - $this->assert_equals('Kakapo', $el->{value}); + my ( $m, $t ) = Foswiki::Func::readTopic( $this->{test_web}, $topic ); + my $el = $m->get( 'PREFERENCE', 'Bird' ); + $this->assert_equals( 'Kakapo', $el->{value} ); # This should succeed - Foswiki::Func::saveTopic($this->{test_web}, $topic, undef, 'Gasp', - { forcenewrevision => 1, - ignorepermissions => 1 }); - @ri = Foswiki::Func::getRevisionInfo($this->{test_web}, $topic); - $this->assert_matches(qr/2$/, $ri[2]); + Foswiki::Func::saveTopic( $this->{test_web}, $topic, undef, 'Gasp', + { forcenewrevision => 1, ignorepermissions => 1 } ); + @ri = Foswiki::Func::getRevisionInfo( $this->{test_web}, $topic ); + $this->assert_matches( qr/2$/, $ri[2] ); } sub test_Item8713 { - my $this = shift; - my $tweb = 'A:B'; + my $this = shift; + my $tweb = 'A:B'; my $topic = 'C:D'; try { - Foswiki::Func::saveTopic( - $tweb, $topic, undef, <assert_matches( qr/Unable to save topic C:D - web A:B does not exist.*/, $e, "Unexpected error $e"); + $this->assert_matches( + qr/Unable to save topic C:D - web A:B does not exist.*/, + $e, "Unexpected error $e" ); } - #$this->assert( # ! Foswiki::Func::webExists( $tweb )); #$this->assert( @@ -377,33 +437,24 @@ sub test_attachments { $Foswiki::cfg{EnableHierarchicalWebs} = 1; my $data = "\0b\1l\2a\3h\4b\5l\6a\7h"; - my $data2 = "\0h\1a\2l\3b\4h\5a\6l\7b"; + my $data2 = "\0h\1a\2l\3b\4h\5a\6l\7b"; my $attnm = 'blahblahblah.gif'; + #$attnm = Assert::TAINT($attnm); my $name1 = 'blahblahblah.gif'; + #$name1 = Assert::TAINT($name1); my $name2 = 'bleagh.sniff'; + #$name2 = Assert::TAINT($name2); my $topic = "BlahBlahBlah"; - #$topic = Assert::TAINT($topic); - - my $stream; - $this->assert( open( $stream, ">$this->{tmpdatafile}" ) ); - binmode($stream); - print $stream $data; - close($stream); - $this->assert( open( $stream, "<$this->{tmpdatafile}" ) ); - binmode($stream); - - my $stream2; - $this->assert( open( $stream2, ">$this->{tmpdatafile2}" ) ); - binmode($stream2); - print $stream2 $data2; - close($stream2); + #$topic = Assert::TAINT($topic); - $this->assert( open( $stream2, "<$this->{tmpdatafile2}" ) ); - binmode($stream2); + my $stream = + $this->write_file( $this->{tmpdatafile}, $data, + { binmode => 1, read => 1 } ); + $this->write_file( $this->{tmpdatafile2}, $data2, { binmode => 1 } ); Foswiki::Func::saveTopicText( $this->{test_web}, $topic, '' ); @@ -458,11 +509,19 @@ sub test_attachments { # This should fail - attachment is not present $this->assert( - !(Foswiki::Func::attachmentExists( $this->{test_web}, $topic, "NotExists" )) ); + !( + Foswiki::Func::attachmentExists( + $this->{test_web}, $topic, "NotExists" + ) + ) + ); # This should fail - attachment is not present - $this->assert( - !Foswiki::Func::readAttachment( $this->{test_web}, $topic, "NotExists" )); + $this->assert( + !Foswiki::Func::readAttachment( + $this->{test_web}, $topic, "NotExists" + ) + ); # Update the attachment and check that the data is updated. $e = Foswiki::Func::saveAttachment( @@ -482,7 +541,8 @@ sub test_attachments { $this->assert_str_equals( $data2, $x ); # Verify that the prior revision contains the old data - $x = Foswiki::Func::readAttachment( $this->{test_web}, $topic, $name2, "1"); + $x = + Foswiki::Func::readAttachment( $this->{test_web}, $topic, $name2, "1" ); $this->assert_str_equals( $data, $x ); } @@ -492,89 +552,117 @@ sub test_noauth_saveAttachment { use Foswiki::AccessControlException; $Foswiki::cfg{EnableHierarchicalWebs} = 1; - my $data = "\0b\1l\2a\3h\4b\5l\6a\7h"; + my $data = "\0b\1l\2a\3h\4b\5l\6a\7h"; my $name1 = 'blahblahblah.gif'; my $topic = "BlahBlahBlah"; - my $stream; - $this->assert(open($stream,">$this->{tmpdatafile}")); - binmode($stream); - print $stream $data; - close($stream); + my $stream = + $this->write_file( $this->{tmpdatafile}, $data, + { binmode => 1, read => 1 } ); - $this->assert(open($stream, "<$this->{tmpdatafile}")); - binmode($stream); - - Foswiki::Func::saveTopicText( $this->{test_web}, $topic," \n * Set ALLOWTOPICCHANGE = SomeUser\n" ); + Foswiki::Func::saveTopicText( $this->{test_web}, $topic, + " \n * Set ALLOWTOPICCHANGE = SomeUser\n" ); try { - my $e = Foswiki::Func::saveAttachment( - $this->{test_web}, $topic, $name1, - { - dontlog => 1, - comment => 'Feasgar Bha', - stream => $stream, - filepath => '/local/file', - filesize => 999, - filedate => 0, - } ); - $this->assert(0, "saveAttachment worked for unauthorized user"); + my $e = Foswiki::Func::saveAttachment( + $this->{test_web}, + $topic, $name1, + { + dontlog => 1, + comment => 'Feasgar Bha', + stream => $stream, + filepath => '/local/file', + filesize => 999, + filedate => 0, + } + ); + $this->assert( 0, "saveAttachment worked for unauthorized user" ); - } catch Foswiki::AccessControlException with { + } + catch Foswiki::AccessControlException with { my $e = shift; - $this->assert_matches( qr/^AccessControlException: Access to CHANGE TemporaryFuncTestWebFunc.BlahBlahBlah for BaseUserMapping_666 is denied.*/, $e, "Unexpected error $e"); + $this->assert_matches( +qr/^AccessControlException: Access to CHANGE TemporaryFuncTestWebFunc.BlahBlahBlah for BaseUserMapping_666 is denied.*/, + $e, + "Unexpected error $e" + ); }; } sub test_noauth_saveTopic { my $this = shift; - my $curUser = 'MrWhite'; + my $curUser = 'MrWhite'; my $userLogin = 'white'; - my $topic = "BlahBlahcwBlah"; - my $ttext = " APPLE \n * Set ALLOWTOPICVIEW = SomeUser \n * Set DENYTOPICCHANGE = BaseUserMapping_666,MrWhite \n "; + my $topic = "BlahBlahcwBlah"; + my $ttext = +" APPLE \n * Set ALLOWTOPICVIEW = SomeUser \n * Set DENYTOPICCHANGE = BaseUserMapping_666,MrWhite \n "; my $query = new Unit::Request(); $this->{session} = new Foswiki( $userLogin, $query ); - Foswiki::Func::saveTopicText( $this->{test_web}, $topic, $ttext ); + Foswiki::Func::saveTopicText( $this->{test_web}, $topic, $ttext ); - $this->assert(Foswiki::Func::topicExists( $this->{test_web}, $topic )); + $this->assert( Foswiki::Func::topicExists( $this->{test_web}, $topic ) ); - $this->assert(!Foswiki::Func::checkAccessPermission( 'VIEW', $curUser, '', $topic, $this->{test_web} ), "VIEW check failed - $curUser should be denied"); - $this->assert(!Foswiki::Func::checkAccessPermission( 'CHANGE', $curUser, '', $topic, $this->{test_web} ), "CHANGE check failed - $curUser should be denied"); + $this->assert( + !Foswiki::Func::checkAccessPermission( + 'VIEW', $curUser, '', $topic, $this->{test_web} + ), + "VIEW check failed - $curUser should be denied" + ); + $this->assert( + !Foswiki::Func::checkAccessPermission( + 'CHANGE', $curUser, '', $topic, $this->{test_web} + ), + "CHANGE check failed - $curUser should be denied" + ); # Validate that saveTopicText throws an exception - $this->assert_matches( qr/oopsattention;def=topic_access/, - Foswiki::Func::saveTopicText( $this->{test_web}, $topic," \n * Set ALLOWTOPIVIEW = SomeUser \n blah" )) ; - $this->assert(!Foswiki::Func::checkAccessPermission( 'CHANGE', $curUser, '', $topic, $this->{test_web} )); + $this->assert_matches( + qr/oopsattention;def=topic_access/, + Foswiki::Func::saveTopicText( + $this->{test_web}, $topic, + " \n * Set ALLOWTOPIVIEW = SomeUser \n blah" + ) + ); + $this->assert( + !Foswiki::Func::checkAccessPermission( + 'CHANGE', $curUser, '', $topic, $this->{test_web} + ) + ); # Also validate that saveTopic throws an exception - my( $meta, $text ) = Foswiki::Func::readTopic( $this->{test_web}, $topic ); + my ( $meta, $text ) = Foswiki::Func::readTopic( $this->{test_web}, $topic ); try { Foswiki::Func::saveTopic( $this->{test_web}, $topic, $meta, $text ); - $this->assert(0, "saveTopic worked for unauthorized user"); - } catch Foswiki::AccessControlException with { + $this->assert( 0, "saveTopic worked for unauthorized user" ); + } + catch Foswiki::AccessControlException with { my $e = shift; - $this->assert_matches( qr/^AccessControlException: Access to CHANGE TemporaryFuncTestWebFunc.BlahBlahcwBlah for white is denied.*/, $e, "Unexpected error $e"); + $this->assert_matches( +qr/^AccessControlException: Access to CHANGE TemporaryFuncTestWebFunc.BlahBlahcwBlah for white is denied.*/, + $e, + "Unexpected error $e" + ); }; } sub test_resaveDenied { my $this = shift; - Foswiki::Func::saveTopic( - $this->{test_web}, $this->{test_topic}, undef, <{test_web}, $this->{test_topic}, undef, + <{test_web}, $this->{test_topic}, undef, < 1} ); + Foswiki::Func::saveTopic( $this->{test_web}, $this->{test_topic}, undef, + < 1 } ); * Set DENYTOPICCHANGE = $Foswiki::cfg{DefaultUserWikiName} NONNY eval { - Foswiki::Func::saveTopic( - $this->{test_web}, $this->{test_topic}, undef, "No!" ); + Foswiki::Func::saveTopic( $this->{test_web}, $this->{test_topic}, undef, + "No!" ); }; - $this->assert_matches(qr/AccessControlException: Access to CHANGE/, $@); + $this->assert_matches( qr/AccessControlException: Access to CHANGE/, $@ ); } sub test_subweb_attachments { @@ -582,42 +670,30 @@ sub test_subweb_attachments { $Foswiki::cfg{EnableHierarchicalWebs} = 1; my $data = "\0b\1l\2a\3h\4b\5l\6a\7h"; - my $data2 = "\0h\1a\2l\3b\4h\5a\6l\7b"; + my $data2 = "\0h\1a\2l\3b\4h\5a\6l\7b"; my $attnm = 'blahblahblah.gif'; my $name1 = 'blahblahblah.gif'; my $name2 = 'bleagh.sniff'; my $topic = "BlahBlahBlah"; + #$topic = Assert::TAINT($topic); - my $web = $this->{test_web}."/SubWeb"; + my $web = $this->{test_web} . "/SubWeb"; + #$web = Assert::TAINT($web); # my $webObject = Foswiki::Meta->new( $this->{session}, $web ); $webObject->populateNewWeb(); - my $stream; - $this->assert( open( $stream, ">$this->{tmpdatafile}" ) ); - binmode($stream); - print $stream $data; - close($stream); - - $this->assert( open( $stream, "<$this->{tmpdatafile}" ) ); - binmode($stream); - - my $stream2; - $this->assert( open( $stream2, ">$this->{tmpdatafile2}" ) ); - binmode($stream2); - print $stream2 $data2; - close($stream2); - - $this->assert( open( $stream2, "<$this->{tmpdatafile2}" ) ); - binmode($stream2); + my $stream = + $this->write_file( $this->{tmpdatafile}, $data, + { binmode => 1, read => 1 } ); + $this->write_file( $this->{tmpdatafile2}, $data2, { binmode => 1 } ); Foswiki::Func::saveTopicText( $this->{test_web}, $topic, '' ); #$name1 = Assert::TAINT($name1); my $e = Foswiki::Func::saveAttachment( - $web, - $topic, $name1, + $web, $topic, $name1, { dontlog => 1, comment => 'Feasgar Bha', @@ -643,43 +719,38 @@ sub test_subweb_attachments { my $fileSize = $stats[7]; my $fileDate = $stats[9]; $e = Foswiki::Func::saveAttachment( - $web, - $topic, - $name2, + $web, $topic, $name2, { - dontlog => 1, - file => $infile, + dontlog => 1, + file => $infile, filedate => $fileDate, filesize => $fileSize, comment => 'DirectedGraphPlugin: DOT graph', hide => 1, } - ); + ); $this->assert( !$e, $e ); # Verify that the files and directories actually were created # my $ft = ''; - $ft = Foswiki::Func::getPubDir(). "/" . $web; - $this->assert( (-d $ft), "Web directory for attachment not created" ); - $ft .= "/".$topic; - $this->assert( (-d $ft), "Topic directory for attachment not created?" ); - $this->assert( - (-e $ft."/$name1" ), + $ft = Foswiki::Func::getPubDir() . "/" . $web; + $this->assert( ( -d $ft ), "Web directory for attachment not created" ); + $ft .= "/" . $topic; + $this->assert( ( -d $ft ), "Topic directory for attachment not created?" ); + $this->assert( ( -e $ft . "/$name1" ), "Attachment file $ft/$name1 was not written to disk?" ); - $this->assert( - (-e $ft."/$name1,v" ), + $this->assert( ( -e $ft . "/$name1,v" ), "Attachment RCS Filename $ft/$name1,v was not written to disk?" ); - $this->assert( - (-e $ft."/$name2,v" ), + $this->assert( ( -e $ft . "/$name2,v" ), "Attachment RCS Filename $ft/$name2,v was not written to disk?" ); - $this->assert( - (-e $ft."/$name2" ), + $this->assert( ( -e $ft . "/$name2" ), "Attachment file $ft/$name2 was not written to disk?" ); ( $meta, $text ) = Foswiki::Func::readTopic( $web, $topic ); @attachments = $meta->find('FILEATTACHMENT'); $this->assert_str_equals( $name1, $attachments[0]->{name} ); + # Make sure it has a non-0 date $this->assert( $attachments[1]->{date} ); $this->assert_str_equals( $name2, $attachments[1]->{name} ); @@ -691,21 +762,19 @@ sub test_subweb_attachments { $this->assert_str_equals( $data, $x ); # This should succeed - attachment exists - $this->assert( - Foswiki::Func::attachmentExists( $web, $topic, $name1 ) ); + $this->assert( Foswiki::Func::attachmentExists( $web, $topic, $name1 ) ); # This should fail - attachment is not present $this->assert( - !(Foswiki::Func::attachmentExists( $web, $topic, "NotExists" )) ); + !( Foswiki::Func::attachmentExists( $web, $topic, "NotExists" ) ) ); # This should fail - attachment is not present - $this->assert( - !Foswiki::Func::readAttachment( $web, $topic, "NotExists" )); + $this->assert( + !Foswiki::Func::readAttachment( $web, $topic, "NotExists" ) ); # Update the attachment and check that the data is updated. $e = Foswiki::Func::saveAttachment( - $web, - $topic, $name2, + $web, $topic, $name2, { dontlog => 1, comment => 'Ciamar a tha u', @@ -720,7 +789,7 @@ sub test_subweb_attachments { $this->assert_str_equals( $data2, $x ); # Verify that the prior revision contains the old data - $x = Foswiki::Func::readAttachment( $web, $topic, $name2, "1"); + $x = Foswiki::Func::readAttachment( $web, $topic, $name2, "1" ); $this->assert_str_equals( $data, $x ); } @@ -728,10 +797,11 @@ sub test_subweb_attachments { sub test_getrevinfo { my $this = shift; my $topic = "RevInfo"; - my $now = time(); + my $now = time(); $Foswiki::cfg{EnableHierarchicalWebs} = 1; - my $webObject = Foswiki::Meta->new( $this->{session}, $this->{test_web} . "/Blah" ); + my $webObject = + Foswiki::Meta->new( $this->{session}, $this->{test_web} . "/Blah" ); $webObject->populateNewWeb(); Foswiki::Func::saveTopicText( $this->{test_web}, $topic, 'blah' ); @@ -743,17 +813,17 @@ sub test_getrevinfo { my $wikiname = Foswiki::Func::getWikiName(); $this->assert_str_equals( $wikiname, $user ); $this->assert_equals( 1, $rev ); - $this->assert( $date >= $now, $date); + $this->assert( $date >= $now, $date ); ( $date, $user, $rev, $comment ) = Foswiki::Func::getRevisionInfo( "$this->{test_web}/Blah", $topic ); $this->assert_str_equals( $wikiname, $user ); $this->assert_equals( 1, $rev ); - $this->assert( $date >= $now, $date); + $this->assert( $date >= $now, $date ); ( $date, $user, $rev, $comment ) = Foswiki::Func::getRevisionInfo( "$this->{test_web}.Blah", $topic ); $this->assert_str_equals( $wikiname, $user ); $this->assert_equals( 1, $rev ); - $this->assert( $date >= $now, $date); + $this->assert( $date >= $now, $date ); } # Helper function for test_moveTopic @@ -761,27 +831,18 @@ sub _checkMoveTopic($$$$) { my ( $oldWeb, $oldTopic, $newWeb, $newTopic ) = @_; my $meta; my $text; - my $count = 0; $newWeb ||= $oldWeb; $newTopic ||= $oldTopic; ( $meta, $text ) = Foswiki::Func::readTopic( $newWeb, $newTopic ); - #print STDERR "looking for $oldWeb.$oldTopic -> $newWeb.$newTopic\n"; - $count = @{ $meta->{TOPICMOVED} } if $meta->{TOPICMOVED}; - - #print STDERR "there are $count TOPICMOVED entries.\n"; foreach ( @{ $meta->{TOPICMOVED} } ) { - #print STDERR "$_->{from} -> $_->{to}\n"; # pick out the meta entry that indicates this move. next if ( $_->{to} ne "$newWeb.$newTopic" ); next if ( $_->{from} ne "$oldWeb.$oldTopic" ); - - #print STDERR "Found it!\n"; return 1; } - #print STDERR "Didn't find it!\n"; return 0; } @@ -799,13 +860,12 @@ sub test_moveTopic { $this->assert( !Foswiki::Func::topicExists( $this->{test_web2}, "TargetTopic" ) ); - # TEST: Attach a file and make sure it exists. It should follow the topic through moves. - my $stream; +# TEST: Attach a file and make sure it exists. It should follow the topic through moves. my $data = "\0b\1l\2a\3h\4b\5l\6a\7h"; - $this->assert( open( $stream, ">$this->{tmpdatafile}" ) ); - binmode($stream); - print $stream $data; - close($stream); + my $stream = + $this->write_file( $this->{tmpdatafile}, $data, + { binmode => 1, read => 1 } ); + Foswiki::Func::saveAttachment( $this->{test_web}, "SourceTopic", @@ -825,7 +885,6 @@ sub test_moveTopic { ) ); - # TEST: move within the test web. Foswiki::Func::moveTopic( $this->{test_web}, "SourceTopic", $this->{test_web}, "TargetTopic" ); @@ -836,8 +895,8 @@ sub test_moveTopic { $this->assert( Foswiki::Func::attachmentExists( $this->{test_web}, "TargetTopic", "Name1" - ) ); - + ) + ); # TEST: Move with undefined destination web; should stay in test_web. Foswiki::Func::moveTopic( $this->{test_web}, "TargetTopic", undef, @@ -849,7 +908,8 @@ sub test_moveTopic { $this->assert( Foswiki::Func::attachmentExists( $this->{test_web}, "SourceTopic", "Name1" - ) ); + ) + ); # TEST: Move to test_web2. Foswiki::Func::moveTopic( $this->{test_web}, "SourceTopic", @@ -867,7 +927,8 @@ sub test_moveTopic { $this->assert( Foswiki::Func::attachmentExists( $this->{test_web2}, "SourceTopic", "Name1" - ) ); + ) + ); # TEST: move with undefined destination topic. Foswiki::Func::moveTopic( $this->{test_web2}, "SourceTopic", @@ -885,7 +946,8 @@ sub test_moveTopic { $this->assert( Foswiki::Func::attachmentExists( $this->{test_web}, "SourceTopic", "Name1" - ) ); + ) + ); # TEST: move to test_web2 again. Foswiki::Func::moveTopic( $this->{test_web}, "SourceTopic", @@ -903,19 +965,20 @@ sub test_moveTopic { $this->assert( Foswiki::Func::attachmentExists( $this->{test_web2}, "TargetTopic", "Name1" - ) ); + ) + ); } sub test_moveAttachment { my $this = shift; Foswiki::Func::saveTopicText( $this->{test_web}, "SourceTopic", "Wibble" ); - my $stream; + my $data = "\0b\1l\2a\3h\4b\5l\6a\7h"; - $this->assert( open( $stream, ">$this->{tmpdatafile}" ) ); - binmode($stream); - print $stream $data; - close($stream); + my $stream = + $this->write_file( $this->{tmpdatafile}, $data, + { binmode => 1, read => 1 } ); + Foswiki::Func::saveAttachment( $this->{test_web}, "SourceTopic", @@ -936,16 +999,17 @@ sub test_moveAttachment { ); # Verify that the source topic contains the string "Wibble" - my ( $meta, $text ) = Foswiki::Func::readTopic( $this->{test_web}, "SourceTopic" ); + my ( $meta, $text ) = + Foswiki::Func::readTopic( $this->{test_web}, "SourceTopic" ); $this->assert( $text =~ m/Wibble/o ); Foswiki::Func::saveTopicText( $this->{test_web}, "TargetTopic", "Wibble" ); Foswiki::Func::saveTopicText( $this->{test_web2}, "TargetTopic", "Wibble" ); - # ############### - # Rename an attachment - from/to web/topic the same - # Old attachment removed, new attachment exists, and source topic text unchanged - # ############### +# ############### +# Rename an attachment - from/to web/topic the same +# Old attachment removed, new attachment exists, and source topic text unchanged +# ############### Foswiki::Func::moveAttachment( $this->{test_web}, "SourceTopic", "Name1", $this->{test_web}, "SourceTopic", "Name2" ); $this->assert( @@ -958,14 +1022,16 @@ sub test_moveAttachment { $this->{test_web}, "SourceTopic", "Name2" ) ); - # Verify that the source topic still contains the string "Wibble" following attachment move - ( $meta, $text ) = Foswiki::Func::readTopic( $this->{test_web}, "SourceTopic" ); + +# Verify that the source topic still contains the string "Wibble" following attachment move + ( $meta, $text ) = + Foswiki::Func::readTopic( $this->{test_web}, "SourceTopic" ); $this->assert( $text =~ m/Wibble/o ); - # ############### - # Move an attachment - from/to topic in the same web - # Old attachment removed, new attachment exists, and source topic text unchanged - # ############### +# ############### +# Move an attachment - from/to topic in the same web +# Old attachment removed, new attachment exists, and source topic text unchanged +# ############### Foswiki::Func::moveAttachment( $this->{test_web}, "SourceTopic", "Name2", $this->{test_web}, "TargetTopic", undef ); $this->assert( @@ -978,14 +1044,16 @@ sub test_moveAttachment { $this->{test_web}, "TargetTopic", "Name2" ) ); + # Verify that the target topic contains the string "Wibble" - ( $meta, $text ) = Foswiki::Func::readTopic( $this->{test_web}, "TargetTopic" ); + ( $meta, $text ) = + Foswiki::Func::readTopic( $this->{test_web}, "TargetTopic" ); $this->assert( $text =~ m/Wibble/o ); - # ############### - # Move an attachment - to topic in a different web - # Old attachment removed, new attachment exists, and source topic text unchanged - # ############### +# ############### +# Move an attachment - to topic in a different web +# Old attachment removed, new attachment exists, and source topic text unchanged +# ############### Foswiki::Func::moveAttachment( $this->{test_web}, "TargetTopic", "Name2", $this->{test_web2}, "TargetTopic", "Name1" ); $this->assert( @@ -998,8 +1066,10 @@ sub test_moveAttachment { $this->{test_web2}, "TargetTopic", "Name1" ) ); - # Verify that the target topic still contains the string "Wibble" following attachment move - ( $meta, $text ) = Foswiki::Func::readTopic( $this->{test_web}, "TargetTopic" ); + +# Verify that the target topic still contains the string "Wibble" following attachment move + ( $meta, $text ) = + Foswiki::Func::readTopic( $this->{test_web}, "TargetTopic" ); $this->assert( $text =~ m/Wibble/o ); } @@ -1008,12 +1078,12 @@ sub test_copyAttachment { my $this = shift; Foswiki::Func::saveTopicText( $this->{test_web}, "SourceTopic", "Wibble" ); - my $stream; + my $data = "\0b\1l\2a\3h\4b\5l\6a\7h"; - $this->assert( open( $stream, ">$this->{tmpdatafile}" ) ); - binmode($stream); - print $stream $data; - close($stream); + my $stream = + $this->write_file( $this->{tmpdatafile}, $data, + { binmode => 1, read => 1 } ); + Foswiki::Func::saveAttachment( $this->{test_web}, "SourceTopic", @@ -1034,14 +1104,12 @@ sub test_copyAttachment { ); # Verify that the source topic contains the string "Wibble" - my ( $meta, $text ) = Foswiki::Func::readTopic( - $this->{test_web}, "SourceTopic" ); + my ( $meta, $text ) = + Foswiki::Func::readTopic( $this->{test_web}, "SourceTopic" ); $this->assert( $text =~ m/Wibble/o ); - Foswiki::Func::saveTopicText( - $this->{test_web}, "TargetTopic", "Wibble" ); - Foswiki::Func::saveTopicText( - $this->{test_web2}, "TargetTopic", "Wibble" ); + Foswiki::Func::saveTopicText( $this->{test_web}, "TargetTopic", "Wibble" ); + Foswiki::Func::saveTopicText( $this->{test_web2}, "TargetTopic", "Wibble" ); # ############### Foswiki::Func::copyAttachment( $this->{test_web}, "SourceTopic", "Name1", @@ -1056,10 +1124,11 @@ sub test_copyAttachment { $this->{test_web}, "SourceTopic", "Name2" ) ); + # Verify that the source topic still contains the string "Wibble" # following attachment copy - ( $meta, $text ) = Foswiki::Func::readTopic( - $this->{test_web}, "SourceTopic" ); + ( $meta, $text ) = + Foswiki::Func::readTopic( $this->{test_web}, "SourceTopic" ); $this->assert( $text =~ m/Wibble/o ); # ############### @@ -1079,9 +1148,10 @@ sub test_copyAttachment { $this->{test_web}, "TargetTopic", "Name2" ) ); + # Verify that the target topic contains the string "Wibble" - ( $meta, $text ) = Foswiki::Func::readTopic( - $this->{test_web}, "TargetTopic" ); + ( $meta, $text ) = + Foswiki::Func::readTopic( $this->{test_web}, "TargetTopic" ); $this->assert( $text =~ m/Wibble/o ); # ############### @@ -1101,10 +1171,11 @@ sub test_copyAttachment { $this->{test_web2}, "TargetTopic", "Name1" ) ); + # Verify that the target topic still contains the string "Wibble" # following attachment copy - ( $meta, $text ) = Foswiki::Func::readTopic( - $this->{test_web}, "TargetTopic" ); + ( $meta, $text ) = + Foswiki::Func::readTopic( $this->{test_web}, "TargetTopic" ); $this->assert( $text =~ m/Wibble/o ); } @@ -1114,25 +1185,32 @@ sub test_attachmentExists { my $topic = "AttachmentExists"; - open( FILE, ">", "$Foswiki::cfg{TempfileDir}/testfile.txt" ); - print FILE "one two three"; - close( FILE ); + $this->write_file( "$Foswiki::cfg{TempfileDir}/testfile.txt", + "one two three" ); Foswiki::Func::saveTopicText( $this->{test_web}, $topic, 'foo' ); Foswiki::Func::saveAttachment( - $this->{test_web}, $topic, "testfile.txt", - { file => "$Foswiki::cfg{TempfileDir}/testfile.txt", - comment => "a comment" } ); + $this->{test_web}, + $topic, + "testfile.txt", + { + file => "$Foswiki::cfg{TempfileDir}/testfile.txt", + comment => "a comment" + } + ); - $this->assert(Foswiki::Func::attachmentExists( $this->{test_web}, $topic, - "testfile.txt")); + $this->assert( + Foswiki::Func::attachmentExists( + $this->{test_web}, $topic, "testfile.txt" + ) + ); } sub test_attachmentExistsInMetaOnly { my $this = shift; my $topic = "AttachmentExistsInMetaOnly"; - my $text = <<'HERE'; + my $text = <<'HERE'; foo %META:FILEATTACHMENT{name="Sample.txt" attr="" comment="Just a sample" date="964294620" path="Sample.txt" size="30" user="ProjectContributor" version=""}% @@ -1140,8 +1218,11 @@ HERE Foswiki::Func::saveTopicText( $this->{test_web}, $topic, $text ); - $this->assert(not Foswiki::Func::attachmentExists( $this->{test_web}, $topic, - "Sample.txt")); + $this->assert( + not Foswiki::Func::attachmentExists( + $this->{test_web}, $topic, "Sample.txt" + ) + ); } sub test_workarea { @@ -1185,10 +1266,10 @@ sub test_normalizeWebTopicName { $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '', 'Topic' ); $this->assert_str_equals( $Foswiki::cfg{UsersWebName}, $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '', '' ); $this->assert_str_equals( $Foswiki::cfg{UsersWebName}, $w ); - $this->assert_str_equals( 'WebHome', $t ); + $this->assert_str_equals( 'WebHome', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '', 'Web/Topic' ); $this->assert_str_equals( 'Web', $w ); $this->assert_str_equals( 'Topic', $t ); @@ -1200,29 +1281,29 @@ sub test_normalizeWebTopicName { $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%USERSWEB%', 'Topic' ); $this->assert_str_equals( $Foswiki::cfg{UsersWebName}, $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%USERSWEB%', 'Topic' ); $this->assert_str_equals( $Foswiki::cfg{UsersWebName}, $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( 'Web', '' ); - $this->assert_str_equals( 'Web', $w ); + $this->assert_str_equals( 'Web', $w ); $this->assert_str_equals( $Foswiki::cfg{HomeTopicName}, $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%SYSTEMWEB%', 'Topic' ); $this->assert_str_equals( $Foswiki::cfg{SystemWebName}, $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%SYSTEMWEB%', 'Topic' ); $this->assert_str_equals( $Foswiki::cfg{SystemWebName}, $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%DOCWEB%', 'Topic' ); $this->assert_str_equals( $Foswiki::cfg{SystemWebName}, $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '', '%USERSWEB%.Topic' ); $this->assert_str_equals( $Foswiki::cfg{UsersWebName}, $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '', '%SYSTEMWEB%.Topic' ); $this->assert_str_equals( $Foswiki::cfg{SystemWebName}, $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%USERSWEB%', 'Web2.Topic' ); $this->assert_str_equals( 'Web2', $w ); @@ -1250,11 +1331,11 @@ sub test_normalizeWebTopicName { ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%USERSWEB%.Wibble', 'Topic' ); $this->assert_str_equals( $Foswiki::cfg{UsersWebName} . '/Wibble', $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%SYSTEMWEB%.Wibble', 'Topic' ); $this->assert_str_equals( $Foswiki::cfg{SystemWebName} . '/Wibble', $w ); - $this->assert_str_equals( 'Topic', $t ); + $this->assert_str_equals( 'Topic', $t ); ( $w, $t ) = Foswiki::Func::normalizeWebTopicName( '%USERSWEB%.Wibble', 'Wibble.Web2.Topic' ); $this->assert_str_equals( 'Wibble/Web2', $w ); @@ -1266,7 +1347,7 @@ sub test_normalizeWebTopicName { } sub test_checkWebAccessPermission { - my $this = shift; + my $this = shift; $this->{session}->finish(); $this->{session} = new Foswiki( $Foswiki::cfg{AdminUserLogin} ); @@ -1276,8 +1357,7 @@ sub test_checkWebAccessPermission { \t* Set DENYWEBCHANGE = $Foswiki::cfg{DefaultUserWikiName} HERE - Foswiki::Func::saveTopicText( $this->{test_web}, - 'WeeblesWobble', <{test_web}, 'WeeblesWobble', <{test_web} ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'CHANGE', @@ -1304,7 +1384,7 @@ HERE Foswiki::Func::checkAccessPermission( 'VIEW', $Foswiki::cfg{DefaultUserWikiName}, undef, '', $this->{test_web} ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'CHANGE', @@ -1317,20 +1397,20 @@ HERE Foswiki::Func::checkAccessPermission( 'VIEW', $Foswiki::cfg{DefaultUserWikiName}, undef, 'WeeblesWobble', $this->{test_web} ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'CHANGE', $Foswiki::cfg{DefaultUserWikiName}, undef, 'WeeblesWobble', $this->{test_web} ); - $this->assert( $access ); + $this->assert($access); # Test with invalid topic - web permissions applied (Item2380) $access = Foswiki::Func::checkAccessPermission( 'VIEW', $Foswiki::cfg{DefaultUserWikiName}, undef, 'WibblesWobble', $this->{test_web} ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'CHANGE', @@ -1343,14 +1423,14 @@ HERE Foswiki::Func::checkAccessPermission( 'CHANGE', $Foswiki::cfg{DefaultUserWikiName}, undef, 'WibblesWobble', undef ); - $this->assert( $access ); + $this->assert($access); # Test for null web name, default to Users web (Main) $access = Foswiki::Func::checkAccessPermission( 'CHANGE', $Foswiki::cfg{DefaultUserWikiName}, undef, 'WibblesWobble', '' ); - $this->assert( $access ); + $this->assert($access); # Test for simple tainted topic name my $taintedTopic = 'WeeblesWobble'; @@ -1359,7 +1439,7 @@ HERE Foswiki::Func::checkAccessPermission( 'CHANGE', $Foswiki::cfg{DefaultUserWikiName}, undef, $taintedTopic, $this->{test_web} ); - $this->assert( $access ); + $this->assert($access); # Test with illegal, tainted topic name. # - Untainting the name detects the illegal characters. @@ -1372,7 +1452,6 @@ HERE $this->assert( !$access ); } - sub test_checkAccessPermission { my $this = shift; my $topic = "NoWayJose"; @@ -1406,6 +1485,7 @@ END "Please me, let me go", $topic, $this->{test_web} ); + # Supplied text should override text from topic $this->assert($access); @@ -1447,7 +1527,7 @@ END $meta ); $this->assert( !$access ); - + #I'm not clear from the docco so... #what happens if we check the perms on a topic that doesn't exist. #first up on a web we can write to @@ -1455,23 +1535,24 @@ END Foswiki::Func::checkAccessPermission( 'VIEW', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', $this->{test_web} ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'CHANGE', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', $this->{test_web} ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'DONTTHINGTHEREISSUCHAPERM', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', 'System' ); - $this->assert( $access ); + $this->assert($access); + #next System, which we shouldn't be able to write to $access = Foswiki::Func::checkAccessPermission( 'VIEW', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', 'System' ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'CHANGE', $Foswiki::cfg{DefaultUserWikiName}, @@ -1481,7 +1562,7 @@ END Foswiki::Func::checkAccessPermission( 'DONTTHINGTHEREISSUCHAPERM', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', 'System' ); - $this->assert( $access ); + $this->assert($access); # next _default, which Sven Dowideit thinks we shouldn't be able to view, # but CDot can't see any good reason for that as restriction, given that @@ -1490,7 +1571,7 @@ END Foswiki::Func::checkAccessPermission( 'VIEW', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', '_default' ); - $this->assert( $access ); + $this->assert($access); # However CHANGE access is denied for non-admins $access = @@ -1506,7 +1587,7 @@ END Foswiki::Func::checkAccessPermission( 'DONTTHINKTHEREISSUCHAPERM', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', '_default' ); - $this->assert( $access ); + $this->assert($access); #next NonExistantWeb, which doesn't exist # If a web doesn't exist, then there is no WebPreferences and @@ -1517,19 +1598,19 @@ END Foswiki::Func::checkAccessPermission( 'VIEW', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', 'NonExistantWeb' ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'CHANGE', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', 'NonExistantWeb' ); - $this->assert( $access ); + $this->assert($access); $access = Foswiki::Func::checkAccessPermission( 'DONTTHINGTHEREISSUCHAPERM', $Foswiki::cfg{DefaultUserWikiName}, '', 'NoSuchTopicPleaseDontMakeIt', 'NonExistantWeb' ); - $this->assert( $access ); + $this->assert($access); } sub test_checkAccessPermission_login_name { @@ -1611,11 +1692,13 @@ sub test_getExternalResource { my $this = shift; # need a known, simple, robust URL to get - my $response = Foswiki::Func::getExternalResource('http://foswiki.org/System/WhatIsWikiWiki'); + my $response = Foswiki::Func::getExternalResource( + 'http://foswiki.org/System/WhatIsWikiWiki'); $this->assert_equals( 200, $response->code() ); $this->assert_matches( - qr/A set of pages of information that are open and free for anyone to edit as they wish. They are stored in a server and managed using some software. The system creates cross-reference hyperlinks between pages automatically./s, - $response->content() ); +qr/A set of pages of information that are open and free for anyone to edit as they wish. They are stored in a server and managed using some software. The system creates cross-reference hyperlinks between pages automatically./s, + $response->content() + ); $this->assert( !$response->is_error() ); $this->assert( !$response->is_redirect() ); } @@ -1722,9 +1805,9 @@ sub test_eachChangeSince { require Foswiki::Users::BaseUserMapping; my $user1 = $Foswiki::cfg{DefaultUserLogin}; - my $user2 = $this->{session}->{users}->findUserByWikiName("ScumBag")->[0]; + my $user2 = $this->{session}->{users}->findUserByWikiName("ScumBag")->[0]; - sleep(1); # to move into a new time step + sleep(1); # to move into a new time step my $start = time(); $this->{session}->finish(); @@ -1764,9 +1847,8 @@ sub test_eachChangeSince { $this->assert( $it->hasNext() ); $change = $it->next(); $this->assert_str_equals( "PiggleNut", $change->{topic} ); - $this->assert_equals( 2, $change->{revision} ); - $this->assert_equals( - $Foswiki::cfg{DefaultUserWikiName}, $change->{user} ); + $this->assert_equals( 2, $change->{revision} ); + $this->assert_equals( $Foswiki::cfg{DefaultUserWikiName}, $change->{user} ); $this->assert( $it->hasNext() ); $change = $it->next(); $this->assert_str_equals( "ClutterBuck", $change->{topic} ); @@ -1779,18 +1861,16 @@ sub test_eachChangeSince { $this->assert( $it->hasNext() ); $change = $it->next(); $this->assert_str_equals( "ClutterBuck", $change->{topic} ); - $this->assert_equals( 1, $change->{revision} ); - $this->assert_equals( - $Foswiki::cfg{DefaultUserWikiName}, $change->{user} ); + $this->assert_equals( 1, $change->{revision} ); + $this->assert_equals( $Foswiki::cfg{DefaultUserWikiName}, $change->{user} ); $this->assert( !$it->hasNext() ); $it = Foswiki::Func::eachChangeSince( $this->{test_web}, $mid ); $this->assert( $it->hasNext() ); $change = $it->next(); $this->assert_str_equals( "PiggleNut", $change->{topic} ); - $this->assert_equals( 2, $change->{revision} ); - $this->assert_equals( - $Foswiki::cfg{DefaultUserWikiName}, $change->{user} ); + $this->assert_equals( 2, $change->{revision} ); + $this->assert_equals( $Foswiki::cfg{DefaultUserWikiName}, $change->{user} ); $change = $it->next(); $this->assert_str_equals( "ClutterBuck", $change->{topic} ); $this->assert_equals( 2, $change->{revision} ); @@ -1933,24 +2013,24 @@ sub test_getRevisionAtTime { sub test_getAttachmentList { my $this = shift; - my $f = "$Foswiki::cfg{TempfileDir}/testfile.gif"; - $this->assert(open(F, ">", $f)); - print F "Naff\n"; - close(F); + my $shortname = "testfile.gif"; + my $filename = "$Foswiki::cfg{TempfileDir}/$shortname"; + $this->write_file( $filename, "Naff\n" ); my $meta = - Foswiki::Meta->new( - $this->{session}, $this->{test_web}, $this->{test_topic}, "One" ); + Foswiki::Meta->new( $this->{session}, $this->{test_web}, + $this->{test_topic}, "One" ); $meta->attach( - name => "testfile.gif", - file => "$Foswiki::cfg{TempfileDir}/testfile.gif", + name => $shortname, + file => $filename, comment => "a comment" ); $meta->save(); - unlink $f; - my @list = Foswiki::Func::getAttachmentList( - $this->{test_web}, $this->{test_topic}); - my $list = join(' ', @list); - $this->assert_str_equals("testfile.gif", $list); + unlink $filename; + my @list = + Foswiki::Func::getAttachmentList( $this->{test_web}, + $this->{test_topic} ); + my $list = join( ' ', @list ); + $this->assert_str_equals( $shortname, $list ); } sub test_searchInWebContent { @@ -1958,12 +2038,11 @@ sub test_searchInWebContent { } sub test_pushPopContext { - my $this = shift; + my $this = shift; my $topic1 = "DanTien"; my $topic2 = "SandWich"; - Foswiki::Func::saveTopicText( - $this->{test_web}, $topic1, <{test_web}, $topic1, < "$this->{test_web}.$topic1" } ) ); - Foswiki::Func::saveTopicText( - $this->{test_web}, $topic2, <{test_web}, $topic2, <assert_equals($this->{test_web}, - $this->{session}->{webName}); - $this->assert_equals($topic1, - $this->{session}->{topicName}); - $this->assert_equals($this->{test_web}, - Foswiki::Func::getPreferencesValue('BASEWEB')); - $this->assert_equals($topic1, - Foswiki::Func::getPreferencesValue('BASETOPIC')); - $this->assert_equals($this->{test_web}, - Foswiki::Func::getPreferencesValue('INCLUDINGWEB')); - $this->assert_equals($topic1, - Foswiki::Func::getPreferencesValue('INCLUDINGTOPIC')); - $this->assert_equals("COLD", - Foswiki::Func::getPreferencesValue('ICE')); - - Foswiki::Func::pushTopicContext($this->{test_web}, $topic2); - - $this->assert_equals($this->{test_web}, - $this->{session}->{webName}); - $this->assert_equals($topic2, - $this->{session}->{topicName}); - $this->assert_equals( - $this->{test_web}, Foswiki::Func::getPreferencesValue('BASEWEB')); - $this->assert_equals( - $topic2, Foswiki::Func::getPreferencesValue('BASETOPIC')); - $this->assert_equals( - $this->{test_web}, Foswiki::Func::getPreferencesValue('INCLUDINGWEB')); - $this->assert_equals( - $topic2, Foswiki::Func::getPreferencesValue('INCLUDINGTOPIC')); - $this->assert_equals("SLIPPERY", - Foswiki::Func::getPreferencesValue('ICE')); + $this->assert_equals( $this->{test_web}, $this->{session}->{webName} ); + $this->assert_equals( $topic1, $this->{session}->{topicName} ); + $this->assert_equals( $this->{test_web}, + Foswiki::Func::getPreferencesValue('BASEWEB') ); + $this->assert_equals( $topic1, + Foswiki::Func::getPreferencesValue('BASETOPIC') ); + $this->assert_equals( $this->{test_web}, + Foswiki::Func::getPreferencesValue('INCLUDINGWEB') ); + $this->assert_equals( $topic1, + Foswiki::Func::getPreferencesValue('INCLUDINGTOPIC') ); + $this->assert_equals( "COLD", Foswiki::Func::getPreferencesValue('ICE') ); + + Foswiki::Func::pushTopicContext( $this->{test_web}, $topic2 ); + + $this->assert_equals( $this->{test_web}, $this->{session}->{webName} ); + $this->assert_equals( $topic2, $this->{session}->{topicName} ); + $this->assert_equals( $this->{test_web}, + Foswiki::Func::getPreferencesValue('BASEWEB') ); + $this->assert_equals( $topic2, + Foswiki::Func::getPreferencesValue('BASETOPIC') ); + $this->assert_equals( $this->{test_web}, + Foswiki::Func::getPreferencesValue('INCLUDINGWEB') ); + $this->assert_equals( $topic2, + Foswiki::Func::getPreferencesValue('INCLUDINGTOPIC') ); + $this->assert_equals( "SLIPPERY", + Foswiki::Func::getPreferencesValue('ICE') ); Foswiki::Func::popTopicContext(); - $this->assert_equals($this->{test_web}, - $this->{session}->{webName}); - $this->assert_equals($topic1, - $this->{session}->{topicName}); - $this->assert_equals($this->{test_web}, - Foswiki::Func::getPreferencesValue('BASEWEB')); - $this->assert_equals($topic1, - Foswiki::Func::getPreferencesValue('BASETOPIC')); - $this->assert_equals($this->{test_web}, - Foswiki::Func::getPreferencesValue('INCLUDINGWEB')); - $this->assert_equals($topic1, - Foswiki::Func::getPreferencesValue('INCLUDINGTOPIC')); - $this->assert_equals("COLD", - Foswiki::Func::getPreferencesValue('ICE')); + $this->assert_equals( $this->{test_web}, $this->{session}->{webName} ); + $this->assert_equals( $topic1, $this->{session}->{topicName} ); + $this->assert_equals( $this->{test_web}, + Foswiki::Func::getPreferencesValue('BASEWEB') ); + $this->assert_equals( $topic1, + Foswiki::Func::getPreferencesValue('BASETOPIC') ); + $this->assert_equals( $this->{test_web}, + Foswiki::Func::getPreferencesValue('INCLUDINGWEB') ); + $this->assert_equals( $topic1, + Foswiki::Func::getPreferencesValue('INCLUDINGTOPIC') ); + $this->assert_equals( "COLD", Foswiki::Func::getPreferencesValue('ICE') ); } sub test_writeEvent { my $this = shift; - my $now = time(); - Foswiki::Func::writeEvent("cereal", "milk"); - select(undef, undef, undef, 0.25) while (time() == $now); + my $now = time(); + Foswiki::Func::writeEvent( "cereal", "milk" ); + select( undef, undef, undef, 0.25 ) while ( time() == $now ); $now = time(); - Foswiki::Func::writeEvent("sausage", "eggs"); + Foswiki::Func::writeEvent( "sausage", "eggs" ); Foswiki::Func::writeEvent("bacon"); Foswiki::Func::writeEvent(); - Foswiki::Func::writeEvent("toast", "jam"); + Foswiki::Func::writeEvent( "toast", "jam" ); my $it = Foswiki::Func::eachEventSince($now); - $this->assert($it->hasNext()); + $this->assert( $it->hasNext() ); my $e = $it->next(); - $this->assert_equals($now, $e->[0]); - $this->assert_equals('sausage', $e->[2]); - $this->assert_equals('eggs', $e->[4]); - $e = $it->next() while ($it->hasNext() && $e->[2] ne 'bacon'); - $e = $it->next() while ($it->hasNext() && $e->[2] ne ''); - $e = $it->next() while ($it->hasNext() && $e->[2] ne 'toast'); - $this->assert_equals('jam', $e->[4]); + $this->assert_equals( $now, $e->[0] ); + $this->assert_equals( 'sausage', $e->[2] ); + $this->assert_equals( 'eggs', $e->[4] ); + $e = $it->next() while ( $it->hasNext() && $e->[2] ne 'bacon' ); + $e = $it->next() while ( $it->hasNext() && $e->[2] ne '' ); + $e = $it->next() while ( $it->hasNext() && $e->[2] ne 'toast' ); + $this->assert_equals( 'jam', $e->[4] ); } sub test_loadTemplate { my $this = shift; my $view = Foswiki::Func::loadTemplate('view'); - $this->assert(length($view)); - my $print = Foswiki::Func::loadTemplate('view', 'print'); - $this->assert(length($print)); - $this->assert($print ne $view); - $this->assert_str_equals('', Foswiki::Func::loadTemplate('crud')); + $this->assert( length($view) ); + my $print = Foswiki::Func::loadTemplate( 'view', 'print' ); + $this->assert( length($print) ); + $this->assert( $print ne $view ); + $this->assert_str_equals( '', Foswiki::Func::loadTemplate('crud') ); } sub test_readTemplate { my $this = shift; my $view = Foswiki::Func::readTemplate('view'); - $this->assert(length($view)); - my $print = Foswiki::Func::readTemplate('view', 'print'); - $this->assert(length($print)); - $this->assert($print ne $view); - $this->assert_str_equals('', Foswiki::Func::readTemplate('crud')); + $this->assert( length($view) ); + my $print = Foswiki::Func::readTemplate( 'view', 'print' ); + $this->assert( length($print) ); + $this->assert( $print ne $view ); + $this->assert_str_equals( '', Foswiki::Func::readTemplate('crud') ); } 1;