Skip to content

Commit

Permalink
Item14152: Mostly finished tests for current extensions implementation.
Browse files Browse the repository at this point in the history
ExtensionsTests now have examples of extensions code and functionality.

- Moved extension version check into prepareDisabledExtenstions() method to
allow processing of manually registered extensions. Tests support mainly.

- %ENV is not any more passed to the application object constructor by its
reference but copied over into a new hash instead. This is to avoid the
side effect of enforced value stringification upon assignment into a %ENV
key.

- Unit::TestApp constructor now support cfgParams similar to engineParams
and requestParams.

- Bug fixes.
  • Loading branch information
vrurg committed Sep 15, 2016
1 parent c2af1d7 commit 2664046
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 177 deletions.
10 changes: 5 additions & 5 deletions UnitTestContrib/lib/Unit/FoswikiTestRole.pm
Expand Up @@ -47,10 +47,8 @@ has app => (
clearer => 1,
isa => Foswiki::Object::isaCLASS( 'app', 'Unit::TestApp', noUndef => 1, ),
default => sub {
if ( defined $Foswiki::app ) {
return $Foswiki::app;
}
return Unit::TestApp->new( env => \%ENV );
return Unit::TestApp->new( env => { map { $_ => $ENV{$_} } keys %ENV },
);
},
handles => [qw(create)],
);
Expand Down Expand Up @@ -593,7 +591,9 @@ sub createNewFoswikiApp {
$app->cfg->data->{Store}{Implementation} ||= 'Foswiki::Store::PlainFile';

$params{env} //= $app->cloneEnv;
my $newApp = Unit::TestApp->new( cfg => $app->cfg->clone, %params );
my $cfgData = $app->cfg->clone->data;
my $newApp =
Unit::TestApp->new( cfgParams => { data => $cfgData, }, %params );

$this->app($newApp);
$this->_fixupAppObjects;
Expand Down
19 changes: 15 additions & 4 deletions UnitTestContrib/lib/Unit/TestApp.pm
Expand Up @@ -41,6 +41,13 @@ has engineParams => (
default => sub { {} },
);

# cfgParams hash is used to initialize a new cfg object.
has cfgParams => (
is => 'rw',
lazy => 1,
default => sub { {} },
);

# Hash of the test callbacks to be registered on the app object.
has callbacks => (
is => 'rw',
Expand All @@ -54,10 +61,6 @@ has _cbRegistered => (
default => 0,
);

before BUILDARGS => sub {

};

around BUILDARGS => sub {
my $orig = shift;
my $this = shift;
Expand Down Expand Up @@ -141,6 +144,14 @@ around _prepareEngine => sub {
return $orig->( $this, %{ $this->engineParams } );
};

around _prepareConfig => sub {
my $orig = shift;
my $this = shift;

my $cfg = $this->create( 'Foswiki::Config', %{ $this->cfgParams } );
return $cfg;
};

around handleRequest => sub {
my $orig = shift;
my $this = shift;
Expand Down
11 changes: 4 additions & 7 deletions UnitTestContrib/test/bin/TestRunner.pl
Expand Up @@ -72,14 +72,11 @@ BEGIN
my ( $app, $cfg );
local %ENV = %ENV;
try {
my $env = \%ENV;
$env->{FOSWIKI_ACTION} =
$ENV{FOSWIKI_ACTION} =
'view'; # SMELL Shan't we add a 'test' action to the SwitchBoard?
$env->{FOSWIKI_ENGINE} = 'Foswiki::Engine::Test';
$app = Unit::TestApp->new(
env => $env,
engineParams => { initialAttributes => { action => 'view', }, },
);
$ENV{FOSWIKI_ENGINE} = 'Foswiki::Engine::Test';
my $env = { map { $_ => $ENV{$_} } keys %ENV };
$app = Unit::TestApp->new( env => $env, );
$cfg = $app->cfg;
}
catch {
Expand Down
140 changes: 109 additions & 31 deletions UnitTestContrib/test/unit/ExtensionsTests.pm
@@ -1,54 +1,70 @@
# See bottom of file for license and copyright information

package Foswiki::ExtensionsTests::SampleClass {
use Foswiki::Class qw(extensible);
extends qw(Foswiki::Object);
package Foswiki::ExtensionsTests::SampleClass;

has allowFlowControl => (
is => 'rw',
default => 0,
);
use Foswiki::Class qw(extensible);
extends qw(Foswiki::Object);

pluggable testPluggableMethod => sub {
my $this = shift;
has allowFlowControl => (
is => 'rw',
default => 0,
);

return wantarray
? ( qw(This is a sample return array), @_ )
: "This is a sample string {" . join( "}{", @_ ) . "}";
};
}
pluggable testPluggableMethod => sub {
my $this = shift;

return wantarray
? ( qw(This is a sample return array), @_ )
: "This is a sample string {" . join( "}{", @_ ) . "}";
};

package ExtensionsTests;

use Assert;
use Foswiki::Exception ();

use version 0.77;

use Foswiki::Class;
extends qw(FoswikiFnTestCase);

$| = 1;

# List of all auto generated extension modules.
has autoGenerated => (
is => 'rw',
default => sub { [] },
has autoGenExt => (
is => 'rw',
lazy => 1,
predicate => 1,
default => sub { [] },
);

around set_up => sub {
my $orig = shift;
my $this = shift;

my $mKey = ( grep { /ExtensionsTests/ } keys %INC )[0];
$this->app->env->{FOSWIKI_EXTLIBS} =
$ENV{FOSWIKI_EXTLIBS} =
File::Spec->catdir( ( File::Spec->splitpath( $INC{$mKey} ) )[1],
'TestExtensions' );

# Disable all extensions generated by previous tests.
$this->app->env->{FOSWIKI_DISABLED_EXTENSIONS} = $this->autoGenerated;
$ENV{FOSWIKI_DISABLED_EXTENSIONS} = join ",", @{ $this->autoGenExt };

$orig->( $this, @_ );

$this->app->cfg->data->{DisableAllPlugins} = 1;
};

$orig->( $this, @_ );
around tear_down => sub {
my $orig = shift;
my $this = shift;

# Disable any extensions a test has generated.
$this->_disableAllCurrentExtensions;

$this->reCreateFoswikiApp;

return $orig->( $this, @_ );
};

sub _getExtName {
Expand All @@ -63,6 +79,8 @@ sub _genExtModules {
my $this = shift;
my ( $count, @extCode ) = @_;

ASSERT($count);

my @extNames;
for ( 1 .. $count ) {
my $extName = $this->_getExtName;
Expand All @@ -72,6 +90,9 @@ package $extName;
use Foswiki::Class qw(extension);
extends qw(Foswiki::Extension);
use version 0.77; our \$VERSION = version->declare(0.0.1);
our \$API_VERSION = version->declare("2.99.0");
Foswiki::Extensions::registerExtModule('$extName');
$code
Expand All @@ -87,7 +108,7 @@ EXT
}

# Remember what extensions have been autogenerated.
push @{ $this->autoGenerated }, @extNames;
push @{ $this->autoGenExt }, @extNames;

return @extNames;
}
Expand Down Expand Up @@ -135,8 +156,12 @@ sub test_orderedList {
sub test_manual_disable {
my $this = shift;

$this->_disableAllCurrentExtensions;

my @ext = $this->_genExtModules(2);
$this->app->env->{FOSWIKI_DISABLED_EXTENSIONS} = $ext[1];

push @{ $this->app->env->{FOSWIKI_DISABLED_EXTENSIONS} }, $ext[1];

$this->reCreateFoswikiApp;

$this->assert_not_null(
Expand All @@ -154,8 +179,11 @@ sub test_manual_disable {
sub test_depend_on_manual_disable {
my $this = shift;

$this->_disableAllCurrentExtensions;

my @ext = $this->_genExtModules(4);
$this->app->env->{FOSWIKI_DISABLED_EXTENSIONS} = $ext[1];

push @{ $this->app->env->{FOSWIKI_DISABLED_EXTENSIONS} }, $ext[1];
$this->_setExtDependencies(
$ext[3] => $ext[2],
$ext[2] => $ext[1],
Expand Down Expand Up @@ -226,8 +254,6 @@ plugBefore 'Foswiki::ExtensionsTests::SampleClass::testPluggableMethod' => sub {
my $this = shift;
my ($params) = @_;
$this->_traceMsg("");
# We expect at least to parameters to be passed in.
$params->{args}[1] = "ext1ArgFromBefore";
};
Expand All @@ -237,8 +263,6 @@ plugAround 'Foswiki::ExtensionsTests::SampleClass::testPluggableMethod' => sub {
my $this = shift;
my ($params) = @_;
$this->_traceMsg("");
# Only if flow control is allowed by the calling test code.
if ($params->{object}->allowFlowControl) {
my $rc = 'ext2ReturnFromAround';
Expand All @@ -264,8 +288,6 @@ plugBefore 'Foswiki::ExtensionsTests::SampleClass::testPluggableMethod' => sub {
my $this = shift;
my ($params) = @_;
$this->_traceMsg("");
$params->{ext3Flag1} = 1;
$params->{ext3Flag2} = 1;
};
Expand All @@ -274,8 +296,6 @@ plugAfter 'Foswiki::ExtensionsTests::SampleClass::testPluggableMethod' => sub {
my $this = shift;
my ($params) = @_;
$this->_traceMsg("");
foreach my $flag (qw(ext3Flag1 ext3Flag2)) {
if ($params->{$flag}) {
my $rc = "with_$flag";
Expand Down Expand Up @@ -347,6 +367,64 @@ EXT3
[@rc] );
}

sub test_API_VERSION {
my $this = shift;

$this->_disableAllCurrentExtensions;

my @ext = $this->_genExtModules(2);
my $ns0 = Foswiki::getNS( $ext[0] );

*{ $ns0->{API_VERSION} } = \version->declare("v2.98.0");

$this->reCreateFoswikiApp;

ASSERT(
!$this->app->extensions->extEnabled( $ext[0] ),
"Extension with API version "
. Foswiki::fetchGlobal("\$$ext[0]::API_VERSION")
. " must have been disabled but it's not"
);

ASSERT(
$this->app->extensions->extEnabled( $ext[1] ),
"Extension with API version "
. Foswiki::fetchGlobal("\$$ext[1]::API_VERSION")
. " must have been enabled but it's not"
);
}

sub test_subClassing {
my $this = shift;

$this->_disableAllCurrentExtensions;

my $configExt = $this->_genExtModules( 1, <<'CFG_EXT');
extClass 'Foswiki::Config' => 'Foswiki::Extension::Sample::Config';
CFG_EXT

$this->reCreateFoswikiApp;

$this->assert(
ref( $Foswiki::cfg{'.version'} ) eq 'version',
"Config key '.version' is expected to be a version object"
);
$this->assert_equals(
$Foswiki::Extension::Sample::Config::VERSION,
$Foswiki::cfg{'.version'},
"Config key '.version' returned unexpected version number"
);

my $testKey = "_ThisKeyMustNotBeUsed";
$Foswiki::cfg{$testKey} = my $testVal =
"Число пі дорівнює 3.1415926";
$this->assert_equals( $testVal, $this->app->cfg->data->{$testKey},
"Value stored to %Foswiki::cfg doesn't match with fetched from application object cfg attribute"
);
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down

This file was deleted.

0 comments on commit 2664046

Please sign in to comment.