Skip to content

Commit

Permalink
Item9513: Define stub handlers _startAttach and _endAttach
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 18 deletions.
1 change: 1 addition & 0 deletions data/System/AttachContentPlugin.txt
Expand Up @@ -100,6 +100,7 @@ After installation, configure this plugin by changing settings in [[%SCRIPTURL{c
| Version: | %$VERSION% |
| Release: | %$RELEASE% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 21 Aug 2010 (2.3.3) | Created stub handlers =_startAttach= and =_endAttach=. |
| 25 May 2010 (2.3.2) | Foswikitask:Item8579 - Fix example to resolve save errors |
| 31 Aug 2009 (2.3) | Arthur Clemens: Added param =hidecontent=. Moved plugin settings to configure. |
| 23 Nov 2008 (2.2.1) | Foswiki compatible. |
Expand Down
5 changes: 4 additions & 1 deletion lib/Foswiki/Plugins/AttachContentPlugin.pm
Expand Up @@ -29,7 +29,7 @@ use Foswiki::Func;
# status of the plugin. It is used by the build automation tools, so
# you should leave it alone.
our $VERSION = '$Rev: 11069$';
our $RELEASE = '2.3.2';
our $RELEASE = '2.3.3';

# Short description of this plugin
# One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
Expand Down Expand Up @@ -58,6 +58,9 @@ sub initPlugin {
return 1;
}

sub _startAttach {}
sub _endAttach {}

=pod
=cut
Expand Down
122 changes: 105 additions & 17 deletions test/unit/AttachContentPlugin/AttachContentPluginTests.pm
@@ -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;

0 comments on commit 0095e14

Please sign in to comment.