Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Item9513: Define stub handlers _startAttach and _endAttach
added working unit test git-svn-id: http://svn.foswiki.org/trunk/AttachContentPlugin@8629 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
- Loading branch information
ArthurClemens
authored and
ArthurClemens
committed
Aug 21, 2010
1 parent
9d577fa
commit 0095e14
Showing
3 changed files
with
110 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 105 additions & 17 deletions
122
test/unit/AttachContentPlugin/AttachContentPluginTests.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,125 @@ | ||
use strict; | ||
|
||
package AttachContentPluginTests; | ||
|
||
use FoswikiTestCase; | ||
our @ISA = qw( FoswikiTestCase ); | ||
use FoswikiFnTestCase; | ||
our @ISA = qw( FoswikiFnTestCase ); | ||
|
||
use strict; | ||
use TWiki; | ||
use CGI; | ||
use warnings; | ||
use Error qw( :try ); | ||
use Foswiki; | ||
|
||
my $twiki; | ||
my $DEBUG = 0; | ||
|
||
sub new { | ||
my $self = shift()->SUPER::new(@_); | ||
my $self = shift()->SUPER::new( 'AttachContentPlugin', @_ ); | ||
return $self; | ||
} | ||
|
||
# Set up the test fixture | ||
sub set_up { | ||
sub loadExtraConfig { | ||
my $this = shift; | ||
$this->SUPER::loadExtraConfig(); | ||
setLocalSite(); | ||
} | ||
|
||
sub setLocalSite { | ||
$Foswiki::cfg{Plugins}{AttachContentPlugin}{Enabled} = 1; | ||
$Foswiki::cfg{Plugins}{AttachContentPlugin}{Debug} = $DEBUG; | ||
} | ||
|
||
sub test_STARTATTACH { | ||
my ($this) = @_; | ||
|
||
my $input = '<verbatim>%STARTATTACH{"mysaved.txt"}% | ||
My content | ||
%ENDATTACH%'; | ||
|
||
$this->SUPER::set_up(); | ||
my $expected = <<END_EXPECTED; | ||
<verbatim> | ||
My content | ||
</verbatim> | ||
END_EXPECTED | ||
|
||
$TWiki::Plugins::SESSION = $twiki; | ||
_trimSpaces($input); | ||
_trimSpaces($expected); | ||
|
||
my $result = | ||
Foswiki::Func::expandCommonVariables( $input, $this->{test_topic}, | ||
$this->{test_web} ); | ||
$this->_performTestHtmlOutput( $expected, $result, 0 ); | ||
} | ||
|
||
sub tear_down { | ||
my $this = shift; | ||
$this->SUPER::tear_down(); | ||
sub test_save { | ||
my ($this) = @_; | ||
|
||
my $testText = <<'HERE'; | ||
%META:TOPICINFO{author="ProjectContributor" date="1111931141" format="1.0" version="$Rev$"}% | ||
<verbatim>%STARTATTACH{"mysaved.txt"}% | ||
My content | ||
%ENDATTACH% | ||
after | ||
HERE | ||
my $UI_FN = $this->getUIFn('save'); | ||
Foswiki::Func::saveTopicText( $this->{test_web}, "MyTopic", $testText ); | ||
my $query = new Unit::Request( | ||
{ | ||
action => ['save'], | ||
topic => [ $this->{test_web} . '.MyTopic' ] | ||
} | ||
); | ||
$this->{session}->finish(); | ||
$this->{session} = new Foswiki( $this->{test_user_login}, $query ); | ||
$this->captureWithKey( save => $UI_FN, $this->{session} ); | ||
my $meta = | ||
Foswiki::Meta->load( $this->{session}, $this->{test_web}, 'MyTopic' ); | ||
|
||
my @attachments = $meta->find('FILEATTACHMENT'); | ||
|
||
$this->assert_equals( scalar @attachments, 1 ); | ||
|
||
foreach my $a (@attachments) { | ||
try { | ||
my $fh = $meta->openAttachment( $a->{name}, '<' ); | ||
my $data = <$fh>; | ||
$this->assert_equals( $data, 'My content' ); | ||
} | ||
catch Foswiki::AccessControlException with { | ||
print STDOUT "ERROR reading attachment data\n"; | ||
}; | ||
} | ||
|
||
} | ||
|
||
sub test_self { | ||
my $this = shift; | ||
=pod | ||
_trimSpaces( $text ) -> $text | ||
Removes spaces from both sides of the text. | ||
=cut | ||
|
||
sub _trimSpaces { | ||
|
||
#my $text = $_[0] | ||
|
||
$_[0] =~ s/^[[:space:]]+//s; # trim at start | ||
$_[0] =~ s/[[:space:]]+$//s; # trim at end | ||
} | ||
|
||
=pod | ||
This formats the text up to immediately before <nop>s are removed, so we | ||
can see the nops. | ||
=cut | ||
|
||
sub _performTestHtmlOutput { | ||
my ( $this, $expected, $actual, $doRender ) = @_; | ||
my $session = $this->{session}; | ||
my $webName = $this->{test_web}; | ||
my $topicName = $this->{test_topic}; | ||
|
||
$actual = _renderHtml( $webName, $topicName, $actual ) if ($doRender); | ||
$this->assert_html_equals( $expected, $actual ); | ||
} | ||
|
||
1; |