Skip to content

Commit

Permalink
Item13897: Tests up to FuncTests are converted.
Browse files Browse the repository at this point in the history
FuncTests has one failing test.

- urlHost Foswiki::Config attribute has been converted into a function.
It's too sensitive to changes in many different places to cache the initial
result. Was breaking tests.

- Foswiki::Config got a new method – patch(). Adds a chunk of configuration
to the config data hash.

- Added a new parameter key for Foswiki::Engine::Test constructor: setUrl.
Simulates the old Unit::Request setUrl() method. It doesn't map into an
attribute but overrides path_info, query_string, secure keys of
initialAttributes attribute; also sets Host query parameter.

- Added support for query parameters in Foswiki::Engine::Test.

- Deleted Unit::Request.
  • Loading branch information
vrurg committed Jun 1, 2016
1 parent c8a6fae commit 05fc005
Show file tree
Hide file tree
Showing 34 changed files with 581 additions and 508 deletions.
11 changes: 7 additions & 4 deletions TopicUserMappingContrib/lib/Foswiki/Users/TopicUserMapping.pm
Expand Up @@ -829,7 +829,8 @@ sub groupAllowsView {
$Group = Foswiki::Sandbox::untaint( $Group,
\&Foswiki::Sandbox::validateTopicName );
my ( $groupWeb, $groupName ) =
$this->app->normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $Group );
$this->app->request->normalizeWebTopicName( $Foswiki::cfg{UsersWebName},
$Group );

# If a Group or User topic normalized somewhere else, doesn't make sense, so ignore the Webname
$groupWeb = $Foswiki::cfg{UsersWebName};
Expand Down Expand Up @@ -860,7 +861,8 @@ sub groupAllowsChange {
$Group = Foswiki::Sandbox::untaint( $Group,
\&Foswiki::Sandbox::validateTopicName );
my ( $groupWeb, $groupName ) =
$this->app->normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $Group );
$this->app->request->normalizeWebTopicName( $Foswiki::cfg{UsersWebName},
$Group );

# SMELL: Should NobodyGroup be configurable?
return 0 if $groupName eq 'NobodyGroup';
Expand Down Expand Up @@ -891,7 +893,8 @@ sub addUserToGroup {
$Group = Foswiki::Sandbox::untaint( $Group,
\&Foswiki::Sandbox::validateTopicName );
my ( $groupWeb, $groupName ) =
$this->app->normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $Group );
$this->app->request->normalizeWebTopicName( $Foswiki::cfg{UsersWebName},
$Group );

Foswki::Exception->throw( text =>
$this->app->i18n->maketext( 'Users cannot be added to [_1]', $Group )
Expand Down Expand Up @@ -1105,7 +1108,7 @@ sub removeUserFromGroup {
$groupName = Foswiki::Sandbox::untaint( $groupName,
\&Foswiki::Sandbox::validateTopicName );
my ( $groupWeb, $groupTopic ) =
$this->app->normalizeWebTopicName( $Foswiki::cfg{UsersWebName},
$this->app->request->normalizeWebTopicName( $Foswiki::cfg{UsersWebName},
$groupName );

Foswiki::Exception->throw(
Expand Down
70 changes: 69 additions & 1 deletion UnitTestContrib/lib/Foswiki/Engine/Test.pm
Expand Up @@ -27,6 +27,8 @@ A instance of this class initialize itself using the following sources of data:
=cut

use Assert;

use Moo;
use namespace::clean;
extends qw(Foswiki::Engine);
Expand All @@ -42,7 +44,7 @@ has simulate => (
return $method;
},
);
has initialAttributes => ( is => 'rw', default => sub { {} }, );
has initialAttributes => ( is => 'rw', default => sub { { headers => {}, } }, );

around BUILDARGS => sub {
my $orig = shift;
Expand All @@ -56,6 +58,16 @@ around BUILDARGS => sub {
return $orig->( $class, %defaults, @_ );
};

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

if ( $args->{setUrl} ) {
$this->setUrl( $args->{setUrl} );
}

}

# Form a data hash using keys either from initialAttributes (higher prio) or
# from env.
sub initFromEnv {
Expand Down Expand Up @@ -105,6 +117,62 @@ around _prepareQueryParameters => sub {
return [];
};

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

my $headers = $orig->($this);
foreach my $header ( keys %{ $this->env } ) {
next unless $header =~ m/^FOSWIKI_TEST_(?:HTTP|CONTENT|COOKIE)/i;
( my $field = $header ) =~ s/^FOSWIKI_TEST_//;
$field =~ s/^HTTPS?_//;
$headers->{$field} = $this->env->{$header};
}

# Initial attributes override environment values.
my $initAttrs = $this->initialAttributes;
if ( defined $initAttrs->{headers} ) {
ASSERT(
ref( $initAttrs->{headers} ) eq 'HASH',
"Initial test engine headers key is a hashref"
);
$headers->{$_} = $initAttrs->{headers}{$_}
foreach keys %{ $initAttrs->{headers} };
}

return $headers;
};

sub setUrl {
my $this = shift;
my ($queryString) = @_;

my $initAttrs = $this->initialAttributes;
my $path = $queryString;
my $urlParams = '';
if ( $queryString =~ /(.*)\?(.*)/ ) {
$path = $1;
$urlParams = $2;
}

if ( $path =~ s/(https?):\/\/(.*?)\/// ) {
my $protocol = $1;
my $host = $2;
if ( $protocol =~ /https/i ) {
$initAttrs->{secure} = 1;
}
else {
$initAttrs->{secure} = 0;
}

#print STDERR "setting Host to $host\n";
$initAttrs->{headers}{Host} = $host;
}

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

around finalizeReturn => sub {
my $orig = shift;
my $this = shift;
Expand Down
97 changes: 0 additions & 97 deletions UnitTestContrib/lib/Unit/Request.pm

This file was deleted.

8 changes: 5 additions & 3 deletions UnitTestContrib/test/unit/Fn_FORMFIELD.pm
Expand Up @@ -163,8 +163,10 @@ sub test_FORMFIELD_format {
sub test_FORMFIELD_topic {
my $this = shift;
my $req = $this->app->request;
my ($topicObject) = Foswiki::Func::readTopic( $this->test_web, 'TestForm' );
$this->session->webName( $this->test_web );
$req->web( $this->test_web );
my $result = $topicObject->expandMacros('%FORMFIELD{"Marjorie"}%');
$this->assert_str_equals( '', $result );
$result = $topicObject->expandMacros(
Expand Down Expand Up @@ -197,7 +199,7 @@ sub test_FORMFIELD_web {
$this->_createTopic( $this->other_web, $topicObject );
($topicObject) = Foswiki::Func::readTopic( $this->test_web, 'TestForm' );
$this->session->webName( $this->test_web );
$this->app->request->web( $this->test_web );
my $result = $topicObject->expandMacros('%FORMFIELD{"Marjorie"}%');
$this->assert_str_equals( '', $result );
Expand All @@ -219,7 +221,7 @@ sub test_FORMFIELD_web {
$this->assert_str_equals( '99', $result );
# remove other web
$this->removeWebFixture( $this->session, $this->other_web );
$this->removeWebFixture( $this->other_web );
}
# Check if ! and <nop> are properly rendered
Expand Down
3 changes: 2 additions & 1 deletion UnitTestContrib/test/unit/Fn_GROUPINFO.pm
Expand Up @@ -224,7 +224,8 @@ sub test_expandHiddenUser {
sub test_expandHiddenUserAsAdmin {
my $this = shift;

$this->createNewFoswikiSession( $Foswiki::cfg{AdminUserLogin} );
$this->createNewFoswikiApp(
user => $this->app->cfg->data->{AdminUserLogin} );
$this->clear_test_topicObject;
$this->test_topicObject(
Foswiki::Func::readTopic( $this->test_web, $this->test_topic ) );
Expand Down

0 comments on commit 05fc005

Please sign in to comment.