Merge commit '4635b91554' into WebGUI8. Syntax clean up to 7.10.2

This commit is contained in:
Colin Kuskie 2010-10-27 15:59:33 -07:00
commit 87326192a3
46 changed files with 956 additions and 969 deletions

142
t/Shop/Credit.t Normal file
View file

@ -0,0 +1,142 @@
# vim:syntax=perl
#-------------------------------------------------------------------
# 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
#------------------------------------------------------------------
# Write a little about what this script tests.
#
#
use FindBin;
use strict;
use lib "$FindBin::Bin/../lib";
use Test::More;
use Test::Deep;
use Test::Exception;
use Data::Dumper;
use JSON;
use HTML::Form;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
use WebGUI::Shop::Credit;
use WebGUI::User;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 27;
#----------------------------------------------------------------------------
# figure out if the test can actually run
my $e;
#######################################################################
#
# new
#
#######################################################################
my $credit;
my $credit_user = WebGUI::User->create($session);
WebGUI::Test->addToCleanup($credit_user);
# Test incorrect for parameters
eval { $credit = WebGUI::Shop::Credit->new(); };
$e = Exception::Class->caught();
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'new takes exception to not giving it a session object');
cmp_deeply (
$e,
methods(
error => 'Need a session.',
),
'new takes exception to not giving it a session object',
);
lives_ok { $credit = WebGUI::Shop::Credit->new($session, $credit_user->userId); } 'new works with an explicit userId';
can_ok($credit, qw/adjust purge getSum getLedger calculateDeduction session userId/);
#######################################################################
#
# session
#
#######################################################################
isa_ok ($credit->session, 'WebGUI::Session', 'session method returns a session object');
is ($session->getId, $credit->session->getId, 'session method returns OUR session object');
#######################################################################
#
# userId
#
#######################################################################
is ($credit->userId, $credit_user->userId, 'userId accessor returns the userId we set');
$session->user({userId => 3});
lives_ok { $credit = WebGUI::Shop::Credit->new($session); } 'new works without an explicit userId';
is $credit->userId, 3, '... by default, it uses the session user';
$session->user({userId => 1});
lives_ok { $credit = WebGUI::Shop::Credit->new($session); } 'new works for visitor, too';
##Restore the original user for more testing
$credit = WebGUI::Shop::Credit->new($session, $credit_user->userId);
#######################################################################
#
# adjust, getSum, calculateDeduction
#
#######################################################################
my $credit1 = WebGUI::Shop::Credit->new($session, 1);
my $credit3 = WebGUI::Shop::Credit->new($session, 3);
WebGUI::Test->addToCleanup(sub { $credit3->purge });
WebGUI::Test->addToCleanup(sub { $credit->purge });
is $credit1->adjust(300, 'bonus for visitors'), 0, 'visitor cannot have credit';
is $credit1->getSum, "0.00", 'getSum: Formatting and amount for Visitor';
is $credit3->getSum, "0.00", '... for Admin';
is $credit->getSum, "0.00", '... for credit user';
is $credit3->adjust(200, 'Admin never gets enough credit'), 200, 'Give Admin 200 credit';
is $credit3->getSum, "200.00", '... getSum for Admin';
is $credit->getSum, "0.00", '... for credit user';
is $credit->adjust(50, 'Refund'), 50, 'Give credit user 50 credit';
is $credit3->getSum, "200.00", '... getSum for Admin, uniqueness check';
is $credit->getSum, "50.00", '... for credit user';
is $credit->adjust(-10, 'Typo in original refund'), -10, 'Negative adustment';
is $credit3->getSum, "200.00", '... getSum for Admin, uniqueness check';
is $credit->getSum, "40.00", '... for credit user';
#######################################################################
#
# calculateDeduction
#
#######################################################################
is $credit->calculateDeduction(10), "-10.00", 'calculateDeduction returns the max of either the amount';
is $credit->calculateDeduction(80), "-40.00", '... or the available credit';
#######################################################################
#
# purge
#
#######################################################################
$credit->purge;
is $credit->getSum, "0.00", 'user credit purged';
is $credit3->getSum, "200.00", '... but only for credit user';

View file

@ -22,8 +22,11 @@ use HTML::Form;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
use WebGUI::Shop::Cart;
use WebGUI::Shop::Credit;
use WebGUI::Shop::PayDriver;
use Clone;
use WebGUI::User;
#----------------------------------------------------------------------------
# Init
@ -394,11 +397,14 @@ my $blue_widget = $widget->setCollateral('variantsJSON', 'variantId', 'new',
);
$versionTag->commit;
$widget = $widget->cloneFromDb;
$session->user({userId => 3});
my $credited_user = WebGUI::User->create($session);
$session->user({user => $credited_user});
my $cart = WebGUI::Shop::Cart->newBySession($session);
WebGUI::Test->addToCleanup($versionTag, $cart);
WebGUI::Test->addToCleanup($versionTag, $cart, $credited_user);
my $addressBook = $cart->getAddressBook;
my $workAddress = $addressBook->addAddress({
label => 'work',
@ -432,6 +438,26 @@ cmp_deeply(
'appendCartVariables: checking shippableItemsInCart and totalPrice & subtotal formatting'
);
my $credit = WebGUI::Shop::Credit->new($session, $credited_user->userId);
$credit->adjust('1', 'credit for testing');
$cart_variables = {};
$driver->appendCartVariables($cart_variables);
cmp_deeply(
$cart_variables,
{
taxes => ignore(),
shippableItemsInCart => 1,
subtotal => '5.00',
inShopCreditDeduction => '-1.00',
inShopCreditAvailable => '1.00',
totalPrice => '4.00',
shipping => ignore(),
},
'... checking credit display'
);
#######################################################################
#
# delete