Skip to content

Commit

Permalink
Item13897: Tests up to MergeTests.
Browse files Browse the repository at this point in the history
- Added experimental callbacks support role – Foswiki::Aux::Callbacks.

  Currently used to support ManageDotPmTests where checks are built upon
  catching exceptions from Foswiki::UI functions. But in the new model
  exceptions are handled by Foswiki::App::handleRequest method and the only
  way to catch them correctly is to patch into the execution flow and
  rethrow. This is where callbacks are useful.

  Yet, this callback model could be a test field for future new plugins
  model.

- Added method reCreateFoswikiApp in FoswikiTestCase. Used to handle
old-style cases where a request object was passed over from the old session
object to newly created one. Used for cases where we need same app object
but with all the caches fully reset.

   The method is most likely not complete yet as only selected attributes
   are copied over.
  • Loading branch information
vrurg committed Jun 9, 2016
1 parent 4c523cb commit 5225f6c
Show file tree
Hide file tree
Showing 13 changed files with 1,177 additions and 766 deletions.
38 changes: 38 additions & 0 deletions UnitTestContrib/lib/Unit/TestApp.pm
Expand Up @@ -37,6 +37,19 @@ has engineParams => (
default => sub { {} },
);

# Hash of the test callbacks to be registered on the app object.
has callbacks => (
is => 'rw',
lazy => 1,
predicate => 1,
isa => Foswiki::Object::isaHASH('callbacks'),
default => sub { {} },
);
has _cbRegistered => (
is => 'rw',
default => 0,
);

sub BUILD {
my $this = shift;

Expand Down Expand Up @@ -71,6 +84,22 @@ sub cloneEnv {
return $this->_cloneData( $this->env, 'env' );
}

sub registerCallbacks {
my $this = shift;

return if $this->_cbRegistered;

foreach my $cbName ( keys %{ $this->callbacks } ) {
$this->registerCallback(
$cbName,
$this->callbacks->{$cbName},
{ app => $this, }
);
}

$this->_cbRegistered(1);
}

around _prepareRequest => sub {
my $orig = shift;
my $this = shift;
Expand All @@ -85,6 +114,15 @@ around _prepareEngine => sub {
return $orig->( $this, %{ $this->engineParams } );
};

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

$this->registerCallbacks;

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

1;

__DATA__
Expand Down
37 changes: 37 additions & 0 deletions UnitTestContrib/test/unit/FoswikiTestCase.pm
Expand Up @@ -1112,6 +1112,43 @@ sub createNewFoswikiApp {
return $this->app;
}

=begin TML
---++ ObjectMethod reCreateFoswikiApp
Creates a new app object using currently active one as the template.
=cut

sub reCreateFoswikiApp {
my $this = shift;

my $app = $this->app;
my $req = $app->request;
my $engine = $app->engine;

# SMELL This is incomplete set of parameters to be set. Would be extended as
# needed.
$this->createNewFoswikiApp(
requestParams => {
initializer => (
defined $req->_initializer
? $req->_initializer
: ''
),
},
engineParams => {
simulate => $engine->simulate,
initialAttributes => {
path_info => $req->pathInfo,
method => $req->method,
action => $req->action,
},
},
@_
);
}

sub finishFoswikiSession {
my ($this) = @_;

Expand Down
82 changes: 35 additions & 47 deletions UnitTestContrib/test/unit/InitFormTests.pm
Expand Up @@ -29,8 +29,8 @@ The testcases below assume that the correct interpretation is the one used in Ed
=cut

use Foswiki::UI::Edit();
use Unit::Request();
use Foswiki::Attrs ();
use Foswiki::UI::Edit ();
use Try::Tiny;

use Moo;
Expand Down Expand Up @@ -103,21 +103,14 @@ around set_up => sub {
my $this = shift;
$orig->( $this, @_ );

$this->createNewFoswikiSession( $Foswiki::cfg{AdminUserLogin},
$this->request );
Foswiki::Func::createWeb($testweb);
$this->createNewFoswikiSession( undef, $this->request );
if ( $this->session->can('getPubURL') ) {
my $cfgData = $this->app->cfg->data;

# FW 1.2 and later
$aurl =
$this->session->getPubURL( $testweb, $testform, undef,
absolute => 1 );
}
else {
# up to FW 1.1.9
$aurl = $this->session->getPubUrl( 1, $testweb, $testform );
}
$this->createNewFoswikiApp( user => $cfgData->{AdminUserLogin} );
Foswiki::Func::createWeb($testweb);
$this->createNewFoswikiApp;
my $app = $this->app;
my $cfg = $app->cfg;
$aurl = $cfg->getPubURL( $testweb, $testform, undef, absolute => 1 );

my ($to) = Foswiki::Func::readTopic( $testweb, $testtopic1 );
$to->put(
Expand Down Expand Up @@ -340,7 +333,7 @@ around set_up => sub {

Foswiki::Func::saveTopic( $testweb, "MyeditTemplate", undef, $edittmpl1 );

$this->session->enterContext('edit');
$app->enterContext('edit');

return;
};
Expand All @@ -349,7 +342,7 @@ around tear_down => sub {
my $orig = shift;
my $this = shift;

$this->removeWebFixture( $this->session, $testweb );
$this->removeWebFixture($testweb);
$orig->($this);

return;
Expand All @@ -367,26 +360,19 @@ sub get_formfield {

sub setup_formtests {
my ( $this, $web, $topic, $params ) = @_;
my $q = Unit::Request->new();

$q->path_info("/$web/$topic");

#$this->session->webName = $web;
#$this->session->topicName = $topic;

require Foswiki::Attrs;
my $attr = Foswiki::Attrs->new($params);
foreach my $k ( keys %{$attr} ) {
next if $k eq '_RAW';
$q->param( -name => $k, -value => $attr->{$k} );
}
$this->createNewFoswikiSession( undef, $q );
$this->session->enterContext('edit');
$this->createNewFoswikiApp(
requestParams => { initializer => {%$attr}, },
engineParams =>
{ initialAttributes => { path_info => "/$web/$topic", }, },
);
$this->app->enterContext('edit');

# Now generate the form. We pass a template which throws everything away
# but the form to allow for simpler analysis.
my ( $text, $tmpl ) =
Foswiki::UI::Edit::init_edit( $this->session, 'myedit' );
$this->create('Foswiki::UI::Edit')->init_edit('myedit');

return $tmpl;
}
Expand Down Expand Up @@ -823,24 +809,26 @@ Simple description of problem</textarea>', get_formfield( 2, $text )
# Item10874, originally Item10446
# Test that ?formtemplate=MyForm works without web prefix on an unsaved topic
sub test_unsavedtopic_rendersform {
my $this = shift;
my $query = Unit::Request->new(
initializer => {
webName => [$testweb],
topicName => ['MissingTopic'],
formtemplate => ["$testform"]
}
my $this = shift;
my $fatwilly = $this->createNewFoswikiApp(
requestParams => {
initializer => {
webName => [$testweb],
topicName => ['MissingTopic'],
formtemplate => ["$testform"]
}
},
engineParams => {
initialAttributes => {
path_info => "/$testweb/MissingTopic",
method => 'POST',
action => 'edit',
},
},
);
$query->path_info("/$testweb/MissingTopic");
$query->method('POST');
my $fatwilly = $this->createNewFoswikiSession( undef, $query );
my ($text) = $this->capture(
sub {
no strict 'refs';
&{ $this->getUIFn('edit') }($fatwilly);
use strict 'refs';
$Foswiki::engine->finalize( $fatwilly->response,
$fatwilly->request );
return $this->app->handleRequest;
}
);
$this->assert_html_matches(
Expand Down

0 comments on commit 5225f6c

Please sign in to comment.