mock asset test package

This commit is contained in:
Graham Knop 2010-06-09 07:16:15 -05:00
parent dfa1a3d7cb
commit 49be76247e
11 changed files with 213 additions and 188 deletions

View file

@ -22,6 +22,7 @@ BEGIN {
}
use WebGUI::Test;
use WebGUI::Test::MockAsset;
use WebGUI::Session;
use WebGUI::Image;
use WebGUI::Storage;
@ -53,6 +54,7 @@ cmp_bag($storage->getFiles, ['blue.png'], 'Only 1 file in storage with correct n
$session->user({userId=>3});
my $versionTag = WebGUI::VersionTag->getWorking($session);
WebGUI::Test->addToCleanup($versionTag);
$versionTag->set({name=>"Image Asset test"});
my $properties = {
# '1234567890123456789012'
@ -98,11 +100,12 @@ isnt($sth->hashRef, undef, 'Annotations column is defined');
#------------------------------------------------------------------------------
# Template variables
{
my $templateId = 'FILE_IMAGE_TEMPLATE___';
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $templateId);
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($templateId);
$templateMock->set_true('prepare');
my $templateVars;
$templateMock->mock('process', sub { $templateVars = $_[1]; return ''; } );
@ -112,17 +115,13 @@ $asset->update({
templateId => $templateId,
});
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
$asset->prepareView();
$asset->view();
like($templateVars->{parameters}, qr{ id="[^"]{22}"}, 'id in parameters is quoted');
like($templateVars->{parameters}, qr{alt="alternate"}, 'original parameters included');
WebGUI::Test->unmockAssetId($templateId);
}
$versionTag->commit;
WebGUI::Test->addToCleanup($versionTag);
done_testing();

View file

@ -18,6 +18,7 @@ use strict;
use lib "$FindBin::Bin/../../lib";
use Test::More;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Test::MockAsset;
use WebGUI::Session;
use WebGUI::User;
use WebGUI::Group;
@ -181,14 +182,12 @@ my $printRemainingTicketsTemplateId = $ems->get('printRemainingTicketsTemplateId
is($printRemainingTicketsTemplateId, "hreA_bgxiTX-EzWCSZCZJw", 'Default print remaining tickets template id ok');
#Make sure printRemainingTickets template returns the right data
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $printRemainingTicketsTemplateId);
my $templateVars;
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
{
WebGUI::Test->mockAssetId($printRemainingTicketsTemplateId, $templateMock);
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($printRemainingTicketsTemplateId);
my $templateVars;
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
$ems->www_printRemainingTickets();
my $ticket1 = {
@ -351,8 +350,6 @@ $templateMock->mock('process', sub { $templateVars = $_[1]; } );
},
"www_printRemainingTickets: template variables valid"
);
WebGUI::Test->unmockAssetId($printRemainingTicketsTemplateId);
}
#Make sure permissions work on pages

View file

@ -17,15 +17,15 @@ use Test::MockObject::Extends;
##The goal of this test is to test the creation of Article Wobjects.
use WebGUI::Test;
use WebGUI::Test::MockAsset;
use WebGUI::Session;
use Test::More tests => 8; # increment this value for each test you create
use Test::Deep;
use Data::Dumper;
my $templateId = 'INOUTBOARD_TEMPLATE___';
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $templateId);
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($templateId);
my $templateVars;
$templateMock->mock('prepare', sub { } );
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
@ -57,7 +57,6 @@ my $board = $node->addChild({
inOutTemplateId => $templateId,
});
WebGUI::Test->mockAssetId($templateId, $templateMock);
$board->prepareView();
# Test for a sane object type
@ -151,7 +150,6 @@ cmp_bag(
'view: returns one entry for each user, entry is correct for user with status'
);
WebGUI::Test->unmockAssetId($templateId);
################################################################
#
# purge

View file

@ -15,6 +15,7 @@ use lib "$FindBin::Bin/../../lib";
##The goal of this test is to test the creation of Search Wobjects.
use WebGUI::Test;
use WebGUI::Test::MockAsset;
use WebGUI::Session;
use Test::More tests => 11; # increment this value for each test you create
use Test::Deep;
@ -49,9 +50,8 @@ foreach my $newSetting (keys %{$newSearchSettings}) {
#1234567890123456789012#
my $templateId = '_FAUX_SEARCH_TEMPLATE_';
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $templateId);
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($templateId);
$templateMock->set_true('prepare');
my $templateVars;
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
@ -68,7 +68,6 @@ $search->update({
doit => 1,
keywords => 'building + applications',
});
WebGUI::Test->mockAssetId($templateId, $templateMock);
$search->prepareView;
eval { $search->view; };
ok(! $@, 'view did now error out on standalone regexp wildcard')
@ -85,7 +84,6 @@ $search->update({
eval { $search->view; };
ok(! $@, 'view did now error out on prefix regexp wildcard')
or diag $@;
WebGUI::Test->unmockAssetId($templateId);
$session->request->setup_body({});
}
@ -115,7 +113,6 @@ $search->update({
doit => 1,
keywords => 'shale',
});
WebGUI::Test->mockAssetId($templateId, $templateMock);
$search->prepareView;
$search->view;
$search->update({useContainers => 0});
@ -124,7 +121,6 @@ $search->update({
$search->view;
like $templateVars->{result_set}->[0]->{url}, qr{\?pn=\d}, 'search returns paginated URL for a Thread when useContainers=1';
WebGUI::Test->unmockAssetId($templateId);
$session->request->setup_body({});
$search->update({useContainers => 0});
}

View file

@ -15,6 +15,7 @@ use lib "$FindBin::Bin/../../lib";
##The goal of this test is to test the creation of Thingy Wobjects.
use WebGUI::Test;
use WebGUI::Test::MockAsset;
use WebGUI::Session;
use Test::More tests => 28; # increment this value for each test you create
use Test::Deep;
@ -28,9 +29,8 @@ my $session = WebGUI::Test->session;
my $node = WebGUI::Asset->getImportNode($session);
my $templateId = 'THING_EDIT_TEMPLATE___';
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $templateId);
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($templateId);
my $templateVars;
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
@ -318,7 +318,6 @@ ok( ! $thingy->hasEnteredMaxPerUser($otherThingId), 'hasEnteredMaxPerUser: retur
my @edit_thing_form_fields = qw/form_start form_end form_submit field_loop/;
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
$thingy->editThingData($otherThingId);
my %miniVars;
@miniVars{@edit_thing_form_fields} = @{ $templateVars }{ @edit_thing_form_fields };
@ -338,7 +337,6 @@ $thingy->editThingDataSave($otherThingId, 'new', {"field_".$otherFieldId => 'oth
ok( $thingy->hasEnteredMaxPerUser($otherThingId), 'hasEnteredMaxPerUser returns true with one row entered, and maxEntriesPerUser=1');
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
$thingy->editThingData($otherThingId);
my %miniVars;
@miniVars{@edit_thing_form_fields} = @{ $templateVars }{ @edit_thing_form_fields };

View file

@ -20,6 +20,7 @@ use Test::More; # increment this value for each test you create
use Test::Deep;
use Test::MockObject;
use Test::MockObject::Extends;
use WebGUI::Test::MockAsset;
my $session = WebGUI::Test->session;
@ -35,15 +36,13 @@ $macroMock->set_true('process');
my $templateId = 'PICKLANGUAGE_TEMPLATE_';
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $templateId);
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($templateId);
my $templateVars;
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
WebGUI::Macro::PickLanguage::process($session,$templateMock->getId);
cmp_deeply(
@ -62,19 +61,16 @@ $templateMock->mock('process', sub { $templateVars = $_[1]; } );
},
'some template variables are created'
);
WebGUI::Test->unmockAssetId($templateId);
}
#test when template Id is left empty
$templateId = '';
my $templateNoId = $templateMock->mock('process','');
$templateMock->set_always('getId', $templateId);
$templateMock->mock_id($templateId);
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
WebGUI::Macro::PickLanguage::process($session,$templateMock->getId);
cmp_deeply(
@ -93,19 +89,9 @@ $templateMock->mock('process', sub { $templateVars = $_[1]; } );
},
'some template variables are created, when no templateId is passed on with the macro'
);
WebGUI::Test->unmockAssetId($templateId);
}
#{
# WebGUI::Test->mockAssetId($templateNoId, $templateMock);
# $error = WebGUI::Macro::PickLanguage::process($session,$templateMock->getId);
#
# is($error,'Could not instanciate template with id []',"Empty template Id should return error");
#
# WebGUI::Test->unmockAssetId($templateNoId);
#}
#test for an incorrect template Id
$templateId = '1234567890123456789012';
@ -116,10 +102,8 @@ my $error;
{
WebGUI::Test->mockAssetId($templateWrongId, $templateMock);
$error = WebGUI::Macro::PickLanguage::process($session,$templateMock->getId);
is($error,'Could not instanciate template with id [1234567890123456789012]',"Template from the wrong namespace should not be initiated");
WebGUI::Test->unmockAssetId($templateWrongId);
}

View file

@ -19,6 +19,7 @@ use lib "$FindBin::Bin/../lib";
use Test::More;
use Test::Deep;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Test::MockAsset;
use WebGUI::Session;
use WebGUI::Shop::Transaction;
use WebGUI::Inbox;
@ -258,16 +259,14 @@ $sendmock->fake_module('WebGUI::Mail::Send',
#1234567890123456789012#
my $templateId = 'SHOP_NOTIFICATION_____';
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $templateId);
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($templateId);
my @templateVars;
$templateMock->mock('process', sub { push @templateVars, clone $_[1]; } );
$session->setting->set('shopReceiptEmailTemplateId', $templateId);
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
$trans->sendNotifications;
is(@templateVars, 2, '... called template->process twice');
my $inbox = WebGUI::Inbox->new($session);
@ -279,7 +278,6 @@ $session->setting->set('shopReceiptEmailTemplateId', $templateId);
like($adminMessages->[0]->get('subject'), qr/^A sale has been made/, '... subject for admin email okay');
like($templateVars[0]->{viewDetailUrl}, qr/shop=transaction;method=viewMy;/, '... viewDetailUrl okay for user');
like($templateVars[1]->{viewDetailUrl}, qr/shop=transaction;method=view;/ , '... viewDetailUrl okay for admin');
WebGUI::Test->unmockAssetId($templateId);
}
#######################################################################

View file

@ -14,7 +14,9 @@ use strict;
use lib "$FindBin::Bin/../../lib";
use Test::More;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Test::MockAsset;
use WebGUI::Session;
use WebGUI::Exception;
use Test::MockObject;
use Test::MockObject::Extends;
@ -102,7 +104,7 @@ $post2mock->set_always('getId', $post2_id);
{
# simulate asset not found
WebGUI::Test->mockAssetId($post2_id, undef);
WebGUI::Test::MockAsset->mock_id($post2_id, sub { WebGUI::Error::ObjectNotFound->throw });
$getmock->set_series('getNextMessage', {
from => 'admin@localhost',
parts => ['parts'],
@ -112,11 +114,11 @@ $post2mock->set_always('getId', $post2_id);
});
$activity->execute($csmock);
is $parentAsset->getId, $cs_id, 'add as new thread to current cs if reply to nonexistant post';
WebGUI::Test->unmockAssetId($post2_id);
WebGUI::Test::MockAsset->unmock_id($post2_id);
}
{
WebGUI::Test->mockAssetId($post2_id, $post2mock);
WebGUI::Test::MockAsset->mock_id($post2_id, $post2mock);
$getmock->set_series('getNextMessage', {
from => 'admin@localhost',
parts => ['parts'],
@ -126,11 +128,11 @@ $post2mock->set_always('getId', $post2_id);
});
$activity->execute($csmock);
is $parentAsset->getId, $cs_id, 'add as new thread to current cs if reply to post in another CS';
WebGUI::Test->unmockAssetId($post2_id);
WebGUI::Test::MockAsset->unmock_id($post2_id);
}
{
WebGUI::Test->mockAssetId($post_id, $postmock);
WebGUI::Test::MockAsset->mock_id($post_id, $postmock);
$getmock->set_series('getNextMessage', {
from => 'admin@localhost',
parts => ['parts'],
@ -140,7 +142,7 @@ $post2mock->set_always('getId', $post2_id);
});
$activity->execute($csmock);
is $parentAsset->getId, $post_id, 'add as reply to post if reply to post in current CS';
WebGUI::Test->unmockAssetId($post_id);
WebGUI::Test::MockAsset->unmock_id($post_id);
}
#vim:ft=perl

View file

@ -19,6 +19,7 @@ use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
use Test::MockObject;
use Test::MockObject::Extends;
use WebGUI::Test::MockAsset;
#----------------------------------------------------------------------------
# Init
@ -46,9 +47,8 @@ my $versionTag = WebGUI::VersionTag->getWorking($session);
#1234567890123456789012#
my $templateId = 'NEWSLETTER_TEMPLATE___';
my $templateMock = Test::MockObject->new({});
$templateMock->set_isa('WebGUI::Asset::Template');
$templateMock->set_always('getId', $templateId);
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($templateId);
my $templateVars;
$templateMock->mock('process', sub { $templateVars = $_[1]; } );
@ -97,7 +97,6 @@ $activity->set_always('getTTL', 60);
$activity->set_always('COMPLETE', 'complete');
{
WebGUI::Test->mockAssetId($templateId, $templateMock);
$activity->execute();
cmp_deeply(
$templateVars,
@ -116,7 +115,6 @@ $activity->set_always('COMPLETE', 'complete');
],
}
);
WebGUI::Test->unmockAssetId($templateId);
}
foreach my $metadataId (keys %{ $cs->getMetaDataFields }) {

View file

@ -38,7 +38,6 @@ use List::MoreUtils qw(any);
use File::Copy ();
use File::Temp ();
use Try::Tiny;
#use Plack::Test;
use WebGUI::PseudoRequest;
use Scope::Guard;
use Try::Tiny;
@ -163,124 +162,6 @@ sub newSession {
#----------------------------------------------------------------------------
=head2 mockAssetId ( $assetId, $object )
Causes WebGUI::Asset->new* initializers to return the specified
object instead of retreiving it from the database for the given
asset ID.
=cut
my %mockedAssetIds;
sub mockAssetId {
my ($class, $assetId, $object) = @_;
_mockAssetInits();
$mockedAssetIds{$assetId} = $object;
}
=head2 unmockAssetId ( $assetId )
Removes a given asset ID from being mocked.
=cut
sub unmockAssetId {
my ($class, $assetId) = @_;
delete $mockedAssetIds{$assetId};
}
=head2 mockAssetUrl ( $url, $object )
Causes WebGUI::Asset->newByUrl to return the specified object instead
of retreiving it from the database for the given URL.
=cut
my %mockedAssetUrls;
sub mockAssetUrl {
my ($class, $url, $object) = @_;
_mockAssetInits();
$mockedAssetUrls{$url} = $object;
}
=head2 unmockAssetUrl ( $url )
Removes a given asset URL from being mocked.
=cut
sub unmockAssetUrl {
my ($class, $url) = @_;
delete $mockedAssetUrls{$url};
}
=head2 unmockAllAssets ( )
Removes all asset IDs and URLs from being mocked.
=cut
sub unmockAllAssets {
my ($class) = @_;
keys %mockedAssetIds = ();
keys %mockedAssetUrls = ();
return;
}
my $mockedNew;
sub _mockAssetInits {
no warnings 'redefine';
return
if $mockedNew;
require WebGUI::Asset;
my $original_new = \&WebGUI::Asset::new;
# *WebGUI::Asset::new = sub {
# my ($class, $session, $assetId, $className, $revisionDate) = @_;
# if ($mockedAssetIds{$assetId}) {
# return $mockedAssetIds{$assetId};
# }
# goto $original_new;
# };
my $original_newById = \&WebGUI::Asset::newById;
*WebGUI::Asset::newById = sub {
my ($class, $session, $assetId, $revisionDate) = @_;
if ($mockedAssetIds{$assetId}) {
return $mockedAssetIds{$assetId};
}
goto $original_newById;
};
my $original_newPending = \&WebGUI::Asset::newPending;
*WebGUI::Asset::newPending = sub {
my ($class, $session, $assetId, $revisionDate) = @_;
if ($assetId && $mockedAssetIds{$assetId}) {
return $mockedAssetIds{$assetId};
}
goto $original_newPending;
};
my $original_newByPropertyHashRef = \&WebGUI::Asset::newByPropertyHashRef;
*WebGUI::Asset::newByPropertyHashRef = sub {
my ($class, $session, $url, $revisionDate) = @_;
if ($url && $mockedAssetUrls{$url}) {
return $mockedAssetUrls{$url};
}
goto $original_newByPropertyHashRef;
};
my $original_newByUrl = \&WebGUI::Asset::newByUrl;
*WebGUI::Asset::newByUrl = sub {
my ($class, $session, $url, $revisionDate) = @_;
if ($url && $mockedAssetUrls{$url}) {
return $mockedAssetUrls{$url};
}
goto $original_newByUrl;
};
$mockedNew = 1;
}
#----------------------------------------------------------------------------
=head2 interceptLogging
Intercept logging request and capture them in buffer variables for testing. Also,

View file

@ -0,0 +1,174 @@
package WebGUI::Test::MockAsset;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=head1 NAME
Package WebGUI::Test::MockAsset
=head1 DESCRIPTION
Utility module for making testing in WebGUI easier.
=cut
use strict;
use warnings;
use Test::MockObject::Extends;
use WebGUI::Asset;
use Package::Stash;
use Scalar::Util qw(weaken);
my $CLASS = __PACKAGE__;
my %mocked_assetIds;
my %mocked_assetUrls;
{
my $asset_meta = WebGUI::Asset->meta;
$asset_meta->make_mutable;
for my $method (qw(newById newPending)) {
$asset_meta->add_around_method_modifier($method, sub {
my $orig = shift;
my $assetId = $_[2];
if ($assetId && exists $mocked_assetIds{$assetId}) {
my $asset = $mocked_assetIds{$assetId};
return $asset->()
if ref $asset eq 'CODE';
return $asset;
}
goto $orig;
});
}
for my $method (qw(newByUrl)) {
$asset_meta->add_around_method_modifier($method, sub {
my $orig = shift;
my $assetUrl = $_[2];
if ($assetUrl && exists $mocked_assetUrls{$assetUrl}) {
my $asset = $mocked_assetUrls{$assetUrl};
return $asset->()
if ref $asset eq 'CODE';
return $asset;
}
goto $orig;
});
}
$asset_meta->make_immutable;
}
sub new {
my $class = shift;
my $mock = shift;
my $id = shift;
$mock ||= 'WebGUI::Asset';
$mock = Test::MockObject::Extends->new($mock);
my $mocked_id;
my $mocked_url;
my @ns_path = map { $_ . '::' } split /::/, ref $mock;
my $ns_last = pop @ns_path;
my $ns_root = do {
no strict 'refs';
\%{ join('', @ns_path) };
};
my $stash = Package::Stash->new(ref $mock);
$stash->add_package_symbol('&DESTROY', sub {
my $self = shift;
$self->unmock_id;
$self->unmock_url;
if ( my $super = $self->can('SUPER::DESTROY') ) {
$self->$super;
}
undef $self;
# remove our namespace
delete $ns_root->{ $ns_last };
});
$stash->add_package_symbol('&mock_id', sub {
my $self = shift;
$self->unmock_id;
$mocked_id = shift;
$CLASS->mock_id($mocked_id, $self);
$self->set_always('assetId', $mocked_id);
$self->set_always('getId', $mocked_id);
return $self;
});
$stash->add_package_symbol('&unmock_id', sub {
my $self = shift;
if ($mocked_id) {
$CLASS->unmock_id($mocked_id);
}
return $self;
});
$stash->add_package_symbol('&mock_url', sub {
my $self = shift;
$self->unmock_url;
$mocked_url = shift;
$CLASS->mock_url($mocked_url, $self);
$self->set_always('url', $mocked_url);
return $self;
});
$stash->add_package_symbol('&unmock_url', sub {
my $self = shift;
if ($mocked_url) {
$CLASS->unmock_url($mocked_url);
}
return $self;
});
return $mock;
}
sub mock_id {
my $class = shift;
my $id = shift;
my $asset = shift;
$mocked_assetIds{$id} = $asset;
weaken $mocked_assetIds{$id};
return;
}
sub unmock_id {
my $class = shift;
my $id = shift;
delete $mocked_assetIds{$id};
return;
}
sub mock_url {
my $class = shift;
my $url = shift;
my $asset = shift;
$mocked_assetUrls{$url} = $asset;
weaken $mocked_assetUrls{$url};
return;
}
sub unmock_url {
my $class = shift;
my $url = shift;
delete $mocked_assetUrls{$url};
return;
}
1;