Skip to content

Commit

Permalink
Item13897: Tests up to HTMLValidationTests
Browse files Browse the repository at this point in the history
Started working on Foswiki::UI::* modules.

Note: HTMLValidationTests viewfile testing isn't compatible with the new
approach of full life-cycle application testing. I.e. currently viewfile
fails with 404 because attachment name isn't defined in request. I made 404
the expected status for viewfile but it doesn't look good.

- Converted Foswiki::UI::Rdiff, Foswiki::UI::Viewfile

- Added OopsException handling on application's handleRequest().

- Added engine method stringifyBody() – similar to stringifyHeaders().

- Object cloning is now skipping attributes starting with __. Though it
might be more useful to preserve them on a stack-like attribute to make it
possible to follow full object history.

- Deprecated finalize() response method. It used to be utilized by
FoswikiTestCase only.

- Made FoswikiTestCase capture method compatible with PSGI-style return.
  • Loading branch information
vrurg committed Jun 4, 2016
1 parent 05fc005 commit cf86898
Show file tree
Hide file tree
Showing 21 changed files with 944 additions and 809 deletions.
1 change: 0 additions & 1 deletion UnitTestContrib/test/unit/AccessControlTests.pm
Expand Up @@ -895,7 +895,6 @@ THIS
},
},
engineParams => {
simulate => 'cgi',
initialAttributes => {
path_info => "/" . $this->test_web . "/$test_topic",
method => 'GET',
Expand Down
6 changes: 0 additions & 6 deletions UnitTestContrib/test/unit/FoswikiFnTestCase.pm
Expand Up @@ -119,20 +119,14 @@ around set_up => sub {

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

my $env = $this->app->cloneEnv;

# Note: some tests are testing Foswiki::UI which also creates a session
$this->createNewFoswikiApp(

#env => $env,
requestParams => { initializer => "" },
engineParams => {
initialAttributes =>
{ path_info => "/" . $this->test_web . "/" . $this->test_topic },
},
);

#$this->response( $this->create('Unit::Response') );
@mails = ();
$this->app->net->setMailHandler( \&FoswikiFnTestCase::sentMail );
my $webObject = $this->populateNewWeb( $this->test_web );
Expand Down
102 changes: 52 additions & 50 deletions UnitTestContrib/test/unit/FoswikiTestCase.pm
Expand Up @@ -22,6 +22,7 @@ use Data::Dumper;
use Scalar::Util qw(blessed);

require Digest::MD5;
require Foswiki::Validation;

use Foswiki();
use Foswiki::Meta();
Expand Down Expand Up @@ -955,9 +956,12 @@ sub capture {

my $app = $this->app;
my $response = $app->response;
my $engine = $app->engine;

my $psgiMode = !$engine->isa('Engine::Test') || $engine->simulate eq 'psgi';

my $responseText = '';
if ( $response->outputHasStarted ) {
if ( !$psgiMode && $response->outputHasStarted ) {

#we're streaming the output as we generate it
#in 2010 (foswiki 1.1) this is used in the statistics script
Expand All @@ -966,57 +970,34 @@ sub capture {
else {

# Capture headers
$response->finalize;
foreach my $header ( keys %{ $response->headers } ) {
$responseText .= $header . ': ' . $_ . "\x0D\x0A"
foreach $response->getHeader($header);
}
$responseText .= "\x0D\x0A";
my $return = $response->as_array;

# Put back Status header which is expected by a number of tests.
push @{ $return->[1] }, 'Status' => $return->[0];
$responseText = $engine->stringifyHeaders($return);

# Capture body
$responseText .= $response->body if $response->body;
$responseText .= join( '', @{ $return->[2] } );
}

return ( $responseText, $result, $stdout, $stderr );
}

=begin TML
---++ ObjectMethod captureWithKey(\&fn, [,$app], ...) -> ($responseText, $result, $stdout, $stderr)
Invoke capture with first setting a strikeone validation key
so it's authorized. First parameter is the action name,
rest is passed over to capture.
=cut
sub _generateValidation {
my $this = shift;
my ($action) = @_;

sub captureWithKey {
my $this = shift;
my $action = shift;
my $app = $this->app;

# Shortcut if user doesn't want validation
return $this->capture(@_) if $Foswiki::cfg{Validation}{Method} eq 'none';

# If we pass a Foswiki object to capture, use that
# otherwise take $Foswiki::app
# and we fallback to the one from the test object
my $fatwilly;
if ( UNIVERSAL::isa( $_[1], 'Unit::TestApp' ) ) {
$fatwilly = $_[1];
}
elsif ( UNIVERSAL::isa( $Foswiki::app, 'Unit::TestApp' ) ) {
$fatwilly = $Foswiki::app;
}
else {
$fatwilly = $this->twiki;
}
$this->assert( $fatwilly->isa('Unit::TestApp'),
$this->assert( $app->isa('Unit::TestApp'),
"Could not find the Foswiki object" );

my $req = $app->request;
my $cfg = $app->cfg;

# Now we have to manually craft the validation checkings
require Foswiki::Validation;
my $cgis = $fatwilly->users->getCGISession;
my $strikeone = $Foswiki::cfg{Validation}{Method} eq 'strikeone';
my $cgis = $app->users->getCGISession;
my $strikeone = $cfg->data->{Validation}{Method} eq 'strikeone';
my $key =
Foswiki::Validation::addValidationKey( $cgis, $action, $strikeone );
unless (
Expand All @@ -1025,19 +1006,39 @@ sub captureWithKey {
$this->assert( 0, "Could not extract validation key from $key" );
}
my ( $k, $v ) = ( $1, $2 );
my $request = $fatwilly->request;
$this->assert( $request->isa('Foswiki::Request'),
$this->assert( $req->isa('Foswiki::Request'),
"Could not find the Foswiki::Request object" );

# As we won't be clicking using javascript, we have to fake that part too
if ($strikeone) {
$v = Digest::MD5::md5_hex( $v, Foswiki::Validation::_getSecret($cgis) );
}
$request->param(
$req->param(
-name => $k,
-value => $v
);
$request->method('POST');
$req->method('POST');
}

=begin TML
---++ ObjectMethod captureWithKey(\&fn, [,$app], ...) -> ($responseText, $result, $stdout, $stderr)
Invoke capture with first setting a strikeone validation key
so it's authorized. First parameter is the action name,
rest is passed over to capture.
=cut

sub captureWithKey {
my $this = shift;
my $action = shift;

# Shortcut if user doesn't want validation
return $this->capture(@_)
if $this->app->cfg->data->{Validation}{Method} eq 'none';

$this->_generateValidation($action);
$this->capture(@_);
}

Expand All @@ -1057,18 +1058,19 @@ sub getUIFn {
my $this = shift;
my $script = shift;
require Foswiki::UI;
$this->assert( $Foswiki::cfg{SwitchBoard}{$script}, $script );
$this->assert( $Foswiki::cfg{SwitchBoard}{$script}->{package},
my $cfg = $this->app->cfg;
$this->assert( $cfg->data->{SwitchBoard}{$script}, $script );
$this->assert( $cfg->data->{SwitchBoard}{$script}->{package},
"$script package not set" );
my $fn = $Foswiki::cfg{SwitchBoard}{$script}->{package};
my $fn = $cfg->data->{SwitchBoard}{$script}->{package};
Foswiki::load_package( $fn,
method => $Foswiki::cfg{SwitchBoard}{$script}->{function}, );
method => $cfg->data->{SwitchBoard}{$script}->{function}, );

#eval "require $fn";
#die "DIED during (require $fn)\n" . $@ if $@;
$this->assert( $Foswiki::cfg{SwitchBoard}{$script}->{function},
$this->assert( $cfg->data->{SwitchBoard}{$script}->{function},
"$script function not set" );
$fn .= '::' . $Foswiki::cfg{SwitchBoard}{$script}->{function};
$fn .= '::' . $cfg->data->{SwitchBoard}{$script}->{function};
return \&$fn;
}

Expand Down
12 changes: 9 additions & 3 deletions UnitTestContrib/test/unit/FuncTests.pm
Expand Up @@ -2414,8 +2414,14 @@ HERE

#my $q = Foswiki::Func::getRequestObject();
#$this->createNewFoswikiSession( $this->app->cfg->data->{GuestUserLogin}, $q );
$this->createNewFoswikiApp( user => $this->app->cfg->data->{GuestUserLogin},
my %appInitParams = (
requestParams =>
$this->app->_cloneData( $this->app->requestParams, 'requestParams' ),
engineParams =>
$this->app->_cloneData( $this->app->engineParams, 'engineParams' ),
);
$this->createNewFoswikiApp( %appInitParams,
user => $this->app->cfg->data->{GuestUserLogin}, );
$this->assert_str_equals( "naff",
Foswiki::Func::getPreferencesValue("PSIBG") );
Foswiki::Func::setPreferencesValue( "PSIBG", "KJHD" );
Expand All @@ -2428,8 +2434,8 @@ HERE
HERE

#$this->createNewFoswikiSession( $this->app->cfg->data->{GuestUserLogin}, $q );
$this->createNewFoswikiApp( user => $this->app->cfg->data->{GuestUserLogin},
);
$this->createNewFoswikiApp( %appInitParams,
user => $this->app->cfg->data->{GuestUserLogin}, );
$this->assert_str_equals( "naff",
Foswiki::Func::getPreferencesValue("PSIBG") );
Foswiki::Func::setPreferencesValue( "PSIBG", "KJHD" );
Expand Down

0 comments on commit cf86898

Please sign in to comment.