Skip to content

Commit

Permalink
Item13897: All tests are converted.
Browse files Browse the repository at this point in the history
It's only plugins tests left. The plan is to take care of JsonContrib and
JQueryPlugin only. Few more most used may follow but it's not certain yet.

- Manual merge from the latest Item14033 branch (be more specific about
invalidWeb and invalidTopic).

- Foswiki::Engine::Test extended support of setUrl key of initialAttributes
hash.

- NEW Added assert_str_contains method on Unit::TestCase class.

- Disabling all plugins for a big numbers of tests. Mostly due to broken
HomePagePlugin which implicitly sets default web/topic with no respect to
those set by test case.
  • Loading branch information
vrurg committed Jun 29, 2016
1 parent cb0c591 commit 69fe715
Show file tree
Hide file tree
Showing 24 changed files with 377 additions and 239 deletions.
62 changes: 47 additions & 15 deletions UnitTestContrib/lib/Foswiki/Engine/Test.pm
Expand Up @@ -15,11 +15,14 @@ A instance of this class initialize itself using the following sources of data:
* Key =__foswikiEngineTestInit= on =env= attribute hash. This key must be a
hashref which is passed to the parent constructor as a hash of defaults
alongside with user supplied parameters in =new()= call. User parameters
has preference of the defaults.
has preference over the defaults.
* For both defaults and user parameters =setUrl= key of =initialAttributes=
hash may be used to set those parameters which are not set implicitly by
corresponding source.
* =FOSWIKI_TEST_= prefixed =env= attribute hash keys for individual keys of
=*Data= attributes. For example, =$engine->pathData->{path_info}= would be
set from =FOSWIKI_TEST_PATH_INFO=. These are used only when corresponding
=*Data= attribute is not initialized over object construction stage.
=*Data= attribute is not initialized at object construction stage.
* Similar to other engines =FOSWIKI_ACTION= might be used if none of the
above sources provided a value for the =pathData->{action}= key.
* =FOSWIKI_TEST_QUERY_STRING= is used for setting =queryParameters=
Expand Down Expand Up @@ -55,18 +58,14 @@ around BUILDARGS => sub {
if ( defined $params{env}{__foswikiEngineTestInit} ) {
%defaults = %{ $params{env}{__foswikiEngineTestInit} };
}
return $orig->( $class, %defaults, @_ );
};

sub BUILD {
my $this = shift;
my ($args) = @_;

if ( $args->{setUrl} ) {
$this->setUrl( $args->{setUrl} );
}
mergeAttrs(
\%params, parseURL( $params{initialAttributes}{setUrl} ),
\%defaults, parseURL( $defaults{initialAttributes}{setUrl} )
);

}
return $orig->( $class, %params );
};

# Form a data hash using keys either from initialAttributes (higher prio) or
# from env.
Expand Down Expand Up @@ -150,11 +149,43 @@ around _prepareUser => sub {
return $initHash->{user} // $initHash->{remote_user};
};

sub setUrl {
my $this = shift;
sub mergeAttrs {
my @hashes = @_;
ASSERT( UNIVERSAL::isa( $_, 'HASH' ),
"Non-hash parameter in call to mergeAttrs()" )
foreach @hashes;
my $base = shift @hashes;

my %skipKeys;
foreach my $extra (@hashes) {
foreach my $key ( keys %$extra ) {
next if $skipKeys{$key};
if ( UNIVERSAL::isa( $base->{$key}, 'HASH' ) ) {

# Nested hashes.
my @subhashes;
$skipKeys{$key} = 1;
push @subhashes, $_
foreach grep { UNIVERSAL::isa( $_, 'HASH' ) }
map { $_->{$key} } @hashes;
mergeAttrs( $base->{$key}, @subhashes );
}
elsif ( !defined( $base->{$key} ) && defined $extra->{$key} ) {
$skipKeys{$key} = 1
; # Key is now set, no need to check agains the rest of the attribute hashes.
$base->{$key} = $extra->{$key};
}
}
}
}

sub parseURL {
my ($queryString) = @_;

my $initAttrs = $this->initialAttributes;
return () unless $queryString;

my %attrs = ( initialAttributes => {} );
my $initAttrs = $attrs{initialAttributes};
my $path = $queryString;
my $urlParams = '';
if ( $queryString =~ /(.*)\?(.*)/ ) {
Expand All @@ -178,6 +209,7 @@ sub setUrl {

$initAttrs->{query_string} = $urlParams;
$initAttrs->{path_info} = Foswiki::Sandbox::untaintUnchecked($path);
return \%attrs;
}

around finalizeReturn => sub {
Expand Down
20 changes: 20 additions & 0 deletions UnitTestContrib/lib/Unit/TestCase.pm
Expand Up @@ -324,6 +324,23 @@ sub assert_null {

=begin TML
---++ ObjectMethod assert_str_contains($expected, $got [, $message])
Fail the test unless =$got= contains =$expected=. =$message= is optional.
=cut

sub assert_str_contains {
my ( $this, $expected, $got, $mess ) = @_;
$this->assert_not_null( $expected, "Expected value may not be null" );
$this->assert_not_null( $got,
$mess || "Expected:'$expected'\n But got null value\n" );
$this->assert( index( $got, $expected ) != -1,
$mess || "Expected string:'$expected'\nnot found in:'$got'\n" );
}

=begin TML
---++ ObjectMethod assert_str_equals($expected, $got [, $message])
Fail the test unless $got eq $expected. $message is optional.
Expand Down Expand Up @@ -641,6 +658,9 @@ sub assert_URI_equals {
#print "COMPARE $got == $expected\n";
my $e = URI->new($expected);
my $g = URI->new($got);

$mess //= "Expected URI:'$expected'\nBut got:'$got'\n";

$this->assert( $g->eq($e), $mess );
}

Expand Down
2 changes: 1 addition & 1 deletion UnitTestContrib/lib/Unit/TestRunner.pm
Expand Up @@ -24,7 +24,7 @@ extends 'Foswiki::Object';

use Assert;

sub CHECKLEAK { 1 }
sub CHECKLEAK { 0 }

BEGIN {
if (CHECKLEAK) {
Expand Down
45 changes: 23 additions & 22 deletions UnitTestContrib/test/unit/AccessControlTests.pm
Expand Up @@ -77,6 +77,8 @@ THIS
$topicObject->save();
undef $topicObject;

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

return;
};

Expand Down Expand Up @@ -866,9 +868,6 @@ sub test_login_redirect_preserves_anchor {
my $this = shift;
my $test_topic = 'TestAnchor';

my $app = $this->app;
my $cfg = $app->cfg;

# Create a topic with an anchor, viewable only by MrYellow
my ($topicObject) =
Foswiki::Func::readTopic( $this->test_web, $test_topic );
Expand All @@ -882,36 +881,38 @@ THIS
undef $topicObject;

# Request the page with the full UI
my $viewUrl = $cfg->getScriptUrl( 0, 'view', $this->test_web, $test_topic );
my $viewUrl =
$this->app->cfg->getScriptUrl( 0, 'view', $this->test_web, $test_topic );

$this->createNewFoswikiApp(
requestParams => {
initializer => {
webName => [ $this->test_web ],
topicName => [$test_topic],
},
},
engineParams => {
initialAttributes => {
path_info => "/" . $this->test_web . "/$test_topic",
action => 'view',
method => 'GET',
uri => $viewUrl,
},
},
);

#$this->finishFoswikiSession();
my ($text) = $this->capture(
sub {
$this->createNewFoswikiApp(
requestParams => {
initializer => {
webName => [ $this->test_web ],
topicName => [$test_topic],
},
},
engineParams => {
initialAttributes => {
path_info => "/" . $this->test_web . "/$test_topic",
method => 'GET',
action => 'view',
uri => $viewUrl,
},
},
);
return $this->app->handleRequest;
}
);

# Get the login and view URLs to compare
my $loginUrl =
$cfg->getScriptUrl( 0, 'login', $this->test_web, $test_topic );
$this->app->cfg->getScriptUrl( 0, 'login', $this->test_web, $test_topic );
my $fullViewUrl =
$cfg->getScriptUrl( 1, 'view', $this->test_web, $test_topic );
$this->app->cfg->getScriptUrl( 1, 'view', $this->test_web, $test_topic );

# Item11121: the test doesn't tolerate ShortURLs, for example.
# ShortURLs may involve a {ScriptUrlPaths}{view} of '' or something
Expand Down
12 changes: 9 additions & 3 deletions UnitTestContrib/test/unit/AddressTests.pm
Expand Up @@ -274,11 +274,17 @@ around set_up => sub {
my $this = shift;

# We don't want the overhead of creating a new session for each tests
my $query = Unit::Request->new( initializer => "" );
$orig->($this);
$query->path_info( "/" . $this->test_web . "/" . $this->test_topic );

$this->createNewFoswikiSession( $Foswiki::cfg{AdminUserLogin}, $query );
$this->createNewFoswikiApp(
requestParams => { initializer => "", },
engineParams => {
initialAttributes => {
path_info => "/" . $this->test_web . "/" . $this->test_topic,
user => $this->app->cfg->data->{AdminUserLogin},
},
},
);

$this->test_topicObject(
Foswiki::Func::readTopic( $this->test_web, $this->test_topic ) );
Expand Down
38 changes: 20 additions & 18 deletions UnitTestContrib/test/unit/AdminOnlyAccessControlTests.pm
Expand Up @@ -92,6 +92,8 @@ THIS
$topicObject->save();
undef $topicObject;

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

return;
};

Expand Down Expand Up @@ -741,27 +743,27 @@ THIS
my $viewUrl =
$this->app->cfg->getScriptUrl( 0, 'view', $this->test_web, $test_topic );

$this->createNewFoswikiApp(
requestParams => {
initializer => {
webName => [ $this->test_web ],
topicName => ["$test_topic"],
},

},
engineParams => {
initialAttributes => {
path_info => "/" . $this->test_web . "/$test_topic",
method => 'GET',
action => 'view',
uri => $viewUrl,
},
},
);

# Request the page with the full UI
my ($text) = $this->capture(
sub {
$this->createNewFoswikiApp(
requestParams => {
initializer => {
webName => [ $this->test_web ],
topicName => ["$test_topic"],
},

},
engineParams => {
simulate => 'cgi',
initialAttributes => {
path_info => "/" . $this->test_web . "/$test_topic",
method => 'GET',
action => 'view',
uri => $viewUrl,
},
},
);
return $this->app->handleRequest;
}
);
Expand Down
5 changes: 2 additions & 3 deletions UnitTestContrib/test/unit/ClientTests.pm
Expand Up @@ -6,7 +6,6 @@ use v5.14;

use Foswiki();
use Foswiki::LoginManager();
use Unit::Request();
use Try::Tiny;
use Digest::MD5 qw(md5_hex);
use Scalar::Util qw(blessed);
Expand All @@ -26,15 +25,15 @@ around set_up => sub {
my $orig = shift;
my $this = shift;
$orig->( $this, @_ );
$EDIT_UI_FN ||= $this->getUIFn('edit');
$VIEW_UI_FN ||= $this->getUIFn('view');
my ($topicObject) =
Foswiki::Func::readTopic( $this->test_web, $this->test_topic );
$topicObject->text(<<'CONSTRAINT');
* Set ALLOWTOPICCHANGE = AdminGroup
CONSTRAINT
$topicObject->save();

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

return;
};

Expand Down
7 changes: 7 additions & 0 deletions UnitTestContrib/test/unit/ConfigureQueryTests.pm
Expand Up @@ -14,6 +14,13 @@ use Moo;
use namespace::clean;
extends qw( ConfigureTestCase );

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

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

sub test_getcfg {
my $this = shift;
my $params = {
Expand Down
2 changes: 1 addition & 1 deletion UnitTestContrib/test/unit/ExceptionTests.pm
Expand Up @@ -114,7 +114,7 @@ sub test_AccessControlException {
topic => 'FlumpNuts',
reason => 'Because it was there.'
);
$this->assert_str_equals(
$this->assert_str_contains(
"AccessControlException: Access to FRY Spiders.FlumpNuts for burger is denied. Because it was there.",
$ace->stringify()
);
Expand Down
6 changes: 5 additions & 1 deletion UnitTestContrib/test/unit/Fn_GROUPINFO.pm
Expand Up @@ -225,7 +225,11 @@ sub test_expandHiddenUserAsAdmin {
my $this = shift;

$this->createNewFoswikiApp(
user => $this->app->cfg->data->{AdminUserLogin} );
engineParams => {
initialAttributes =>
{ user => $this->app->cfg->data->{AdminUserLogin}, },
},
);
$this->clear_test_topicObject;
$this->test_topicObject(
Foswiki::Func::readTopic( $this->test_web, $this->test_topic ) );
Expand Down
3 changes: 3 additions & 0 deletions UnitTestContrib/test/unit/Fn_ICON.pm
Expand Up @@ -24,13 +24,16 @@ around set_up => sub {
my $orig = shift;
my $this = shift;

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

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

my $it = Foswiki::Func::getPreferencesValue('ICONTOPIC');
$it =~ s/\./\//;
$this->reliconurl( $Foswiki::cfg{PubUrlPath} . '/'
. Foswiki::Func::expandCommonVariables($it) );
$this->absiconurl( $Foswiki::cfg{DefaultUrlHost} . $this->reliconurl );

};

sub test_ICONURL {
Expand Down
9 changes: 6 additions & 3 deletions UnitTestContrib/test/unit/Fn_IF.pm
Expand Up @@ -1306,9 +1306,12 @@ sub test_107 {
return;
}

sub set_up {
around set_up => sub {
my $orig = shift;
my $this = shift;
$this->SUPER::set_up(@_);

$this->app->cfg->data->{DisableAllPlugins} = 1;
$orig->( $this, @_ );

my ($topicObject) =
Foswiki::Func::readTopic( $this->users_web, "GropeGroup" );
Expand All @@ -1325,7 +1328,7 @@ sub set_up {
$topicObject->save();

return;
}
};

sub simpleTest {
my ( $this, %test ) = @_;
Expand Down

0 comments on commit 69fe715

Please sign in to comment.