merging commerce branch back into head
This commit is contained in:
commit
5fb4807edc
1326 changed files with 55293 additions and 41167 deletions
86
t/Asset/Sku.t
Normal file
86
t/Asset/Sku.t
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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.
|
||||
#
|
||||
# This tests WebGUI::Asset::Sku, which is the base class for commerce items
|
||||
|
||||
use FindBin;
|
||||
use strict;
|
||||
use lib "$FindBin::Bin/../lib";
|
||||
use Test::More;
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Asset;
|
||||
use WebGUI::Asset::Sku;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
plan tests => 19; # Increment this number for each test you create
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
my $root = WebGUI::Asset->getRoot($session);
|
||||
my $sku = $root->addChild({
|
||||
className=>"WebGUI::Asset::Sku",
|
||||
title=>"Test Sku",
|
||||
});
|
||||
isa_ok($sku, "WebGUI::Asset::Sku");
|
||||
|
||||
$sku->addToCart;
|
||||
|
||||
$sku->applyOptions({
|
||||
test1 => "YY"
|
||||
});
|
||||
|
||||
my $options = $sku->getOptions;
|
||||
is($options->{test1}, "YY", "Can set and get an option.");
|
||||
|
||||
|
||||
is($sku->getMaxAllowedInCart, 99999999, "Got a valid default max in cart.");
|
||||
is($sku->getQuantityAvailable, 99999999, "skus should have an unlimited quantity by default");
|
||||
is($sku->getQuantityAvailable, $sku->getMaxAllowedInCart, "quantity available and max allowed in cart should be the same");
|
||||
is($sku->getPrice, 0.00, "Got a valid default price.");
|
||||
is($sku->getWeight, 0, "Got a valid default weight.");
|
||||
is($sku->getTaxRate, undef, "Tax rate is not overridden.");
|
||||
$sku->update({overrideTaxRate=>1, taxRateOverride=>5});
|
||||
is($sku->getTaxRate, 5, "Tax rate is overridden.");
|
||||
isnt($sku->processStyle, "", "Got some style information.");
|
||||
is($sku->onAdjustQuantityInCart, undef, "onAdjustQuantityInCart should exist and return undef");
|
||||
is($sku->onCompletePurchase, undef, "onCompletePurchase should exist and return undef");
|
||||
is($sku->onRemoveFromCart, undef, "onRemoveFromCart should exist and return undef");
|
||||
is($sku->isRecurring, 0, "skus are not recurring by default");
|
||||
is($sku->isShippingRequired, 0, "skus are not shippable by default");
|
||||
is($sku->getConfiguredTitle, $sku->getTitle, "configured title and title should be the same by default");
|
||||
|
||||
isa_ok($sku->getCart, "WebGUI::Shop::Cart", "can get a cart object");
|
||||
my $item = $sku->addToCart;
|
||||
isa_ok($item, "WebGUI::Shop::CartItem", "can add to cart");
|
||||
$item->cart->delete;
|
||||
|
||||
my $loadSku = WebGUI::Asset::Sku->newBySku($session, $sku->get("sku"));
|
||||
is($loadSku->getId, $sku->getId, "newBySku() works.");
|
||||
|
||||
$sku->purge;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
63
t/Asset/Sku/Donation.t
Normal file
63
t/Asset/Sku/Donation.t
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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.
|
||||
#
|
||||
# This tests WebGUI::Asset::Sku::Donation
|
||||
|
||||
use FindBin;
|
||||
use strict;
|
||||
use lib "$FindBin::Bin/../../lib";
|
||||
use Test::More;
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Asset;
|
||||
use WebGUI::Asset::Sku::Donation;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
plan tests => 4; # Increment this number for each test you create
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
my $root = WebGUI::Asset->getRoot($session);
|
||||
my $sku = $root->addChild({
|
||||
className=>"WebGUI::Asset::Sku::Donation",
|
||||
title=>"Test Donation",
|
||||
defaultPrice => 50.00,
|
||||
});
|
||||
isa_ok($sku, "WebGUI::Asset::Sku::Donation");
|
||||
|
||||
is($sku->getPrice, 50.00, "Price should be 50.00");
|
||||
|
||||
$sku->applyOptions({
|
||||
price => 200.00
|
||||
});
|
||||
is($sku->getPrice, 200.00, "Price should be 200.00");
|
||||
|
||||
is($sku->getConfiguredTitle, "Test Donation (200)", "getConfiguredTitle()");
|
||||
|
||||
$sku->purge;
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
91
t/Macro/ViewCart.t
Normal file
91
t/Macro/ViewCart.t
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
use FindBin;
|
||||
use strict;
|
||||
use lib "$FindBin::Bin/../lib";
|
||||
|
||||
use WebGUI::Test;
|
||||
use WebGUI::Session;
|
||||
use HTML::TokeParser;
|
||||
use Data::Dumper;
|
||||
|
||||
use Test::More; # increment this value for each test you create
|
||||
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
||||
|
||||
my @testSets = (
|
||||
{
|
||||
comment => 'default',
|
||||
label => q!!,
|
||||
output => '<a href="'.$session->url->page('shop=cart').'"><img src="/extras/macro/ViewCart/cart.gif" alt="View Cart" style="border: 0px;vertical-align: middle;" /></a> <a href="'.$session->url->page('shop=cart').'">View Cart</a>',
|
||||
},
|
||||
{
|
||||
comment => 'custom text',
|
||||
label => q!A Rock Hammer!,
|
||||
output => '<a href="'.$session->url->page('shop=cart').'"><img src="/extras/macro/ViewCart/cart.gif" alt="A Rock Hammer" style="border: 0px;vertical-align: middle;" /></a> <a href="'.$session->url->page('shop=cart').'">A Rock Hammer</a>',
|
||||
},
|
||||
);
|
||||
|
||||
my $numTests = 0;
|
||||
foreach my $testSet (@testSets) {
|
||||
$numTests += 1 + (ref $testSet->{output} eq 'CODE');
|
||||
}
|
||||
|
||||
$numTests += 1; #For the use_ok
|
||||
|
||||
plan tests => $numTests;
|
||||
|
||||
my $macro = 'WebGUI::Macro::ViewCart';
|
||||
my $loaded = use_ok($macro);
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip "Unable to load $macro", $numTests-1 unless $loaded;
|
||||
|
||||
foreach my $testSet (@testSets) {
|
||||
my $output = WebGUI::Macro::ViewCart::process( $session, $testSet->{label});
|
||||
if (ref $testSet->{output} eq 'CODE') {
|
||||
my ($url, $label) = $testSet->{output}->($output);
|
||||
is($label, $testSet->{label}, $testSet->{comment}.", label");
|
||||
is($url, $testSet->{url}, $testSet->{comment}.", url");
|
||||
}
|
||||
else {
|
||||
is($output, $testSet->{output}, $testSet->{comment});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
sub simpleHTMLParser {
|
||||
my ($text) = @_;
|
||||
my $p = HTML::TokeParser->new(\$text);
|
||||
|
||||
my $token = $p->get_tag("a");
|
||||
my $url = $token->[1]{href} || "-";
|
||||
my $label = $p->get_trimmed_text("/a");
|
||||
|
||||
return ($url, $label);
|
||||
}
|
||||
|
||||
sub simpleTextParser {
|
||||
my ($text) = @_;
|
||||
|
||||
my ($url) = $text =~ /^HREF=(.+)$/m;
|
||||
my ($label) = $text =~ /^LABEL=(.+)$/m;
|
||||
|
||||
return ($url, $label);
|
||||
}
|
||||
|
||||
END {
|
||||
}
|
||||
23
t/SQL.t
23
t/SQL.t
|
|
@ -17,7 +17,7 @@ use WebGUI::Session;
|
|||
use Data::Dumper;
|
||||
use Test::Deep;
|
||||
|
||||
use Test::More tests => 52; # increment this value for each test you create
|
||||
use Test::More tests => 53; # increment this value for each test you create
|
||||
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
|
@ -263,6 +263,27 @@ $hrefHref = $session->db->buildHashRefOfHashRefs('select message, myIndex from t
|
|||
grep { $_->[2] eq 'B' } @tableData;
|
||||
cmp_deeply($hrefHref, \%expected, 'buildHashRefOfHashRefs, 2 columns, 1 param');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# buildDataTableStructure
|
||||
#
|
||||
# Uses the testTable data from the preceeding *RefOf*Ref tests above
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my %tableStruct = $session->db->buildDataTableStructure('select * from testTable');
|
||||
|
||||
my @hashedTableData = map { { myIndex=>$_->[0], message=>$_->[1], myKey=>$_->[2]} } @tableData;
|
||||
|
||||
cmp_deeply(
|
||||
\%tableStruct,
|
||||
{
|
||||
totalRecords => 8,
|
||||
recordsReturned => 8,
|
||||
records => \@hashedTableData,
|
||||
},
|
||||
'Check table structure',
|
||||
);
|
||||
|
||||
END: {
|
||||
$session->db->dbh->do('DROP TABLE IF EXISTS testTable');
|
||||
|
|
|
|||
249
t/Shop/Address.t
Normal file
249
t/Shop/Address.t
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 Exception::Class;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Shop::AddressBook;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 27;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $loaded = use_ok('WebGUI::Shop::Address');
|
||||
|
||||
my $storage;
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip 'Unable to load module WebGUI::Shop::Address', $tests unless $loaded;
|
||||
my $e;
|
||||
my $address;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# create
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $address = WebGUI::Shop::Address->create(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidObject', 'create takes exception to not giving it an address book');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need an address book.',
|
||||
expected => 'WebGUI::Shop::AddressBook',
|
||||
got => '',
|
||||
param => undef,
|
||||
),
|
||||
'create takes exception to not giving it address book',
|
||||
);
|
||||
|
||||
eval { $address = WebGUI::Shop::Address->create($session); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidObject', 'create takes exception to not giving it a session variable');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need an address book.',
|
||||
expected => 'WebGUI::Shop::AddressBook',
|
||||
got => 'WebGUI::Session',
|
||||
param => $session,
|
||||
),
|
||||
'create takes exception to giving it a session variable',
|
||||
);
|
||||
|
||||
my $book = WebGUI::Shop::AddressBook->create($session);
|
||||
|
||||
eval { $address = WebGUI::Shop::Address->create($book); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it address data');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need a hash reference.',
|
||||
param => undef,
|
||||
),
|
||||
'create takes exception to giving it address data',
|
||||
);
|
||||
|
||||
$address = WebGUI::Shop::Address->create($book, {});
|
||||
isa_ok($address, 'WebGUI::Shop::Address', 'create returns an Address object with an empty hashref');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# addressBook
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
cmp_deeply(
|
||||
$address->addressBook,
|
||||
$book,
|
||||
'The address has a reference back to the book used to create it'
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getId
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
ok( $session->id->valid($address->getId), 'Address has a valid GUID');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# get
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
ok( $session->id->valid($address->getId), 'Address has a valid GUID');
|
||||
is($address->getId, $address->get('addressId'), 'getId is an alias for get addressId');
|
||||
cmp_deeply(
|
||||
$address->get,
|
||||
{
|
||||
label => undef,
|
||||
name => undef,
|
||||
address1 => undef,
|
||||
address2 => undef,
|
||||
address3 => undef,
|
||||
city => undef,
|
||||
state => undef,
|
||||
country => undef,
|
||||
code => undef,
|
||||
phoneNumber => undef,
|
||||
addressId => ignore(), #checked elsewhere
|
||||
addressBookId => $book->getId,
|
||||
},
|
||||
'get the whole thing and check a new, blank object'
|
||||
);
|
||||
|
||||
my $addressGuts = $address->get();
|
||||
$addressGuts->{'label'} = 'hacked';
|
||||
is($address->get('label'), undef, 'get returns a safe copy of the hash');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# update
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$address->update({ label => 'home'});
|
||||
is($address->get('label'), 'home', 'update updates the object properties cache');
|
||||
$address->update({ address1 => 'Shawshank Prison', 'state' => 'Maine'});
|
||||
is($address->get('address1'), 'Shawshank Prison', 'update updates the object properties cache for more than one key');
|
||||
is($address->get('state'), 'Maine', 'update updates the object properties cache for more than one key');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $address = WebGUI::Shop::Address->new(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidObject', 'new takes exception to not giving it an address book');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need an address book.',
|
||||
expected => 'WebGUI::Shop::AddressBook',
|
||||
got => '',
|
||||
param => ignore,
|
||||
),
|
||||
'new takes exception to not giving it address book',
|
||||
);
|
||||
|
||||
eval { $address = WebGUI::Shop::Address->new($session); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidObject', 'new takes exception to not giving it a session variable');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need an address book.',
|
||||
expected => 'WebGUI::Shop::AddressBook',
|
||||
got => 'WebGUI::Session',
|
||||
param => ignore,
|
||||
),
|
||||
'new takes exception to giving it a session variable',
|
||||
);
|
||||
|
||||
eval { $address = WebGUI::Shop::Address->new($book); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new takes exception to not giving it an address to instanciate');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need an addressId.',
|
||||
param => undef,
|
||||
),
|
||||
'new takes exception to giving it an address to instanciate',
|
||||
);
|
||||
|
||||
eval { $address = WebGUI::Shop::Address->new($book, 'neverAnId'); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::ObjectNotFound', 'new takes exception to not giving it a bad address instanciate');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Address not found.',
|
||||
id => 'neverAnId',
|
||||
),
|
||||
'new takes exception to giving it a bad address to instanciate',
|
||||
);
|
||||
|
||||
TODO: {
|
||||
local $TODO = 'More tests for new';
|
||||
ok(0, 'Make a second address book, add an address to it, then try to call a valid address from the wrong book');
|
||||
}
|
||||
|
||||
my $addressCopy = WebGUI::Shop::Address->new($book, $address->getId);
|
||||
cmp_deeply(
|
||||
$address,
|
||||
$addressCopy,
|
||||
'new: gets an exact copy of the object from the db. Also checks that update writes to the db correctly.'
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# delete
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$address->delete;
|
||||
my $check = $session->db->quickScalar('select count(*) from address where addressId=?',[$address->getId]);
|
||||
is( $check, 0, 'delete worked');
|
||||
|
||||
}
|
||||
|
||||
END: {
|
||||
$session->db->write('delete from addressBook');
|
||||
$session->db->write('delete from address');
|
||||
}
|
||||
211
t/Shop/AddressBook.t
Normal file
211
t/Shop/AddressBook.t
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 Exception::Class;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Text;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 22;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $loaded = use_ok('WebGUI::Shop::AddressBook');
|
||||
|
||||
my $storage;
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip 'Unable to load module WebGUI::Shop::AddressBook', $tests unless $loaded;
|
||||
my $e;
|
||||
my $book;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $book = WebGUI::Shop::AddressBook->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.',
|
||||
expected => 'WebGUI::Session',
|
||||
got => '',
|
||||
),
|
||||
'new takes exception to not giving it a session object',
|
||||
);
|
||||
|
||||
eval { $book = WebGUI::Shop::AddressBook->new($session); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new takes exception to not giving it a addressBookId');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need an addressBookId.',
|
||||
),
|
||||
'new takes exception to not giving it a addressBook Id',
|
||||
);
|
||||
|
||||
eval { $book = WebGUI::Shop::AddressBook->new($session, 'neverAGUID'); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::ObjectNotFound', 'new takes exception to not giving it an existing addressBookId');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'No such address book.',
|
||||
id => 'neverAGUID',
|
||||
),
|
||||
'new takes exception to not giving it a addressBook Id',
|
||||
);
|
||||
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# create
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $book = WebGUI::Shop::AddressBook->create(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a session object');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need a session.',
|
||||
expected => 'WebGUI::Session',
|
||||
got => '',
|
||||
),
|
||||
'create takes exception to not giving it a session object',
|
||||
);
|
||||
|
||||
$session->user({userId => 1});
|
||||
|
||||
$book = WebGUI::Shop::AddressBook->create($session);
|
||||
isa_ok($book, 'WebGUI::Shop::AddressBook', 'create returns the right kind of object');
|
||||
|
||||
isa_ok($book->session, 'WebGUI::Session', 'session method returns a session object');
|
||||
|
||||
is($session->getId, $book->session->getId, 'session method returns OUR session object');
|
||||
|
||||
ok($session->id->valid($book->getId), 'create makes a valid GUID style addressBookId');
|
||||
|
||||
is(undef, $book->get('userId'), 'create does not automatically set the userId');
|
||||
|
||||
my $bookCount = $session->db->quickScalar('select count(*) from addressBook');
|
||||
is($bookCount, 1, 'only 1 address book was created');
|
||||
|
||||
my $alreadyHaveBook = WebGUI::Shop::AddressBook->create($session);
|
||||
is($book->getId, $alreadyHaveBook->getId, 'creating an addressbook as visitor, when you already have one, returns the one already created');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getId
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
is($book->getId, $book->get('addressBookId'), 'getId is a shortcut for ->get');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# addAddress
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $address1 = $book->addAddress({ label => q{Red's cell} });
|
||||
isa_ok($address1, 'WebGUI::Shop::Address', 'addAddress returns an object');
|
||||
|
||||
my $address2 = $book->addAddress({ label => q{Norton's office} });
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getAddresses
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my @addresses = @{ $book->getAddresses() };
|
||||
|
||||
cmp_deeply(
|
||||
\@addresses,
|
||||
[$address1, $address2],
|
||||
'getAddresses returns all address objects for this book'
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# update
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$book->update({ lastShipId => $address1->getId, lastPayId => $address2->getId});
|
||||
|
||||
cmp_deeply(
|
||||
$book->get(),
|
||||
{
|
||||
userId => ignore,
|
||||
sessionId => ignore,
|
||||
addressBookId => ignore,
|
||||
lastShipId => $address1->getId,
|
||||
lastPayId => $address2->getId,
|
||||
},
|
||||
'update updates the object properties cache'
|
||||
);
|
||||
|
||||
my $bookClone = WebGUI::Shop::AddressBook->new($session, $book->getId);
|
||||
|
||||
cmp_deeply(
|
||||
$bookClone,
|
||||
$book,
|
||||
'update updates the db, too'
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# delete
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$bookClone->delete();
|
||||
$bookCount = $session->db->quickScalar('select count(*) from addressBook');
|
||||
my $addrCount = $session->db->quickScalar('select count(*) from address');
|
||||
|
||||
is($bookCount, 0, 'delete: book deleted');
|
||||
is($addrCount, 0, 'delete: also deletes addresses in the book');
|
||||
undef $book;
|
||||
|
||||
}
|
||||
|
||||
END: {
|
||||
$session->db->write('delete from addressBook');
|
||||
$session->db->write('delete from address');
|
||||
}
|
||||
102
t/Shop/Cart.t
Normal file
102
t/Shop/Cart.t
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Asset;
|
||||
use WebGUI::Shop::Cart;
|
||||
use WebGUI::TestException;
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
plan tests => 20; # Increment this number for each test you create
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
throws_deeply ( sub { my $cart = WebGUI::Shop::Cart->getCartBySession(); },
|
||||
'WebGUI::Error::InvalidObject',
|
||||
{
|
||||
error => 'Need a session.',
|
||||
got => '',
|
||||
expected => 'WebGUI::Session',
|
||||
},
|
||||
'newBySession takes an exception to not giving it a session variable'
|
||||
);
|
||||
|
||||
my $cart = WebGUI::Shop::Cart->getCartBySession($session);
|
||||
|
||||
isa_ok($cart, "WebGUI::Shop::Cart");
|
||||
isa_ok($cart->session, "WebGUI::Session");
|
||||
|
||||
my $root = WebGUI::Asset->getRoot($session);
|
||||
my $product = $root->addChild({
|
||||
className=>"WebGUI::Asset::Sku::Donation",
|
||||
title=>"Test Product",
|
||||
});
|
||||
$product->applyOptions({price=>50.25});
|
||||
my $item = $cart->addItem($product);
|
||||
isa_ok($item, "WebGUI::Shop::CartItem");
|
||||
isa_ok($item->cart, "WebGUI::Shop::Cart", "Does the item have a cart?");
|
||||
is(ref($item->get), "HASH", "Do we have a hash of properties?");
|
||||
|
||||
is($item->get("quantity"), 1, "Should have 1 of these in the cart.");
|
||||
is($item->adjustQuantity(2), 3, "adjustQuantity() should tell us how many items of this type are in the cart");
|
||||
is($item->get("quantity"), 3, "Should have 3 of these in the cart.");
|
||||
is(scalar(@{$cart->getItems}), 1, "Should have 1 item type in cart regardless of quanity.");
|
||||
|
||||
$item->update({shippingAddressId => "XXXX"});
|
||||
is($item->get("shippingAddressId"), "XXXX", "Can set values to the cart item properties.");
|
||||
|
||||
like($cart->getId, qr/[A-Za-z0-9\_\-]{22}/, "Id looks like a guid.");
|
||||
|
||||
is(ref($cart->get), "HASH", "Cart properties are a hash reference.");
|
||||
is($cart->get("sessionId"), $session->getId, "Can retrieve a value from the cart properties.");
|
||||
|
||||
is($cart->formatCurrency(11.1), "11.10", "can format currency");
|
||||
|
||||
is($cart->calculateSubtotal, 150.75, "can determine the price of the items in the cart");
|
||||
|
||||
$cart->update({shippingAddressId => "XXXX"});
|
||||
is($cart->get("shippingAddressId"), "XXXX", "Can set values to the cart properties.");
|
||||
|
||||
isa_ok($cart->getAddressBook, "WebGUI::Shop::AddressBook", "can get an address book");
|
||||
|
||||
$cart->empty;
|
||||
is($session->db->quickScalar("select count(*) from cartItem where cartId=?",[$cart->getId]), 0, "Items are removed from cart.");
|
||||
|
||||
|
||||
$cart->delete;
|
||||
is($cart->delete, undef, "Can destroy cart.");
|
||||
|
||||
|
||||
$product->purge;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
|
||||
}
|
||||
246
t/Shop/Pay.t
Normal file
246
t/Shop/Pay.t
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 JSON;
|
||||
use HTML::Form;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::TestException;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 18;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $loaded = use_ok('WebGUI::Shop::Pay');
|
||||
|
||||
my $storage;
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip 'Unable to load module WebGUI::Shop::Pay', $tests unless $loaded;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $e;
|
||||
my $pay;
|
||||
|
||||
|
||||
throws_deeply ( sub { $pay = WebGUI::Shop::Pay->new(); },
|
||||
'WebGUI::Error::InvalidObject',
|
||||
{
|
||||
error => 'Must provide a session variable',
|
||||
got => '',
|
||||
expected => 'WebGUI::Session',
|
||||
},
|
||||
'new takes an exception to not giving it a session variable'
|
||||
);
|
||||
|
||||
$pay = WebGUI::Shop::Pay->new($session);
|
||||
isa_ok($pay, 'WebGUI::Shop::Pay', 'new returned the right kind of object');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# session
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
isa_ok($pay->session, 'WebGUI::Session', 'session method returns a session object');
|
||||
is($session->getId, $pay->session->getId, 'session method returns OUR session object');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# addPaymentGateway
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $gateway;
|
||||
|
||||
throws_deeply ( sub { $gateway = $pay->addPaymentGateway(); },
|
||||
'WebGUI::Error::InvalidParam',
|
||||
{
|
||||
error => 'Must provide a class to create an object'
|
||||
},
|
||||
'addPaymentGateway croaks without a class',
|
||||
);
|
||||
|
||||
throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::NoSuchDriver'); },
|
||||
'WebGUI::Error::InvalidParam',
|
||||
{
|
||||
error => 'The requested class is not enabled in your WebGUI configuration file',
|
||||
param => 'WebGUI::Shop::PayDriver::NoSuchDriver',
|
||||
},
|
||||
'addPaymentGateway croaks without a configured class',
|
||||
);
|
||||
|
||||
throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash'); },
|
||||
'WebGUI::Error::InvalidParam',
|
||||
{
|
||||
error => 'Must provide a label to create an object',
|
||||
},
|
||||
'addPaymentGateway requires a label',
|
||||
);
|
||||
|
||||
|
||||
throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL'); },
|
||||
'WebGUI::Error::InvalidParam',
|
||||
{
|
||||
error => 'You must pass a hashref of options to create a new PayDriver object',
|
||||
},
|
||||
'addPaymentGateway croaks without options to build a object with',
|
||||
);
|
||||
|
||||
throws_deeply ( sub { $gateway = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL', {}); },
|
||||
'WebGUI::Error::InvalidParam',
|
||||
{
|
||||
error => 'You must pass a hashref of options to create a new PayDriver object',
|
||||
},
|
||||
'addPaymentGateway croaks without options to build a object with',
|
||||
);
|
||||
|
||||
my $options = {
|
||||
enabled => 1,
|
||||
label => 'Cold, stone hard cash',
|
||||
};
|
||||
my $newDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'JAL', $options);
|
||||
isa_ok($newDriver, 'WebGUI::Shop::PayDriver::Cash', 'added a new, configured Cash driver');
|
||||
is($newDriver->label, 'JAL', 'label passed correctly to paydriver');
|
||||
|
||||
|
||||
#TODO: check if options are stored.
|
||||
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getDrivers
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $drivers = $pay->getDrivers();
|
||||
|
||||
my $defaultPayDrivers = {
|
||||
'WebGUI::Shop::PayDriver::Cash' => 'Cash',
|
||||
};
|
||||
|
||||
cmp_deeply( $drivers, $defaultPayDrivers, 'getDrivers returns the default PayDrivers');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getOptions
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
throws_deeply( sub { $drivers = $pay->getOptions(); },
|
||||
'WebGUI::Error::InvalidParam',
|
||||
{
|
||||
error => 'Need a cart.',
|
||||
},
|
||||
'getOptions takes exception to not giving it a cart',
|
||||
);
|
||||
|
||||
#TODO: Check th crap getOptions returns
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getPaymentGateway
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
throws_deeply( sub { $gateway = $pay->getPaymentGateway(); },
|
||||
'WebGUI::Error::InvalidParam',
|
||||
{
|
||||
error => q{Must provide a paymentGatewayId},
|
||||
},
|
||||
'getPaymentGateway throws exception without paymentGatewayId',
|
||||
);
|
||||
|
||||
throws_deeply( sub { $gateway = $pay->getPaymentGateway('NoSuchThing'); },
|
||||
'WebGUI::Error::ObjectNotFound',
|
||||
{
|
||||
error => q{payment gateway not found in db},
|
||||
id => 'NoSuchThing',
|
||||
},
|
||||
'getPaymentGateway throws exception when called with a non-existant paymentGatewayId',
|
||||
);
|
||||
|
||||
$gateway = $pay->getPaymentGateway( $newDriver->getId );
|
||||
isa_ok($gateway, 'WebGUI::Shop::PayDriver::Cash', 'returned payment gateway has correct class');
|
||||
is($gateway->getId, $newDriver->getId, 'getPaymentGateway instantiated the requested driver');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getPaymentGateways
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
# Create an extra driver for testing purposes
|
||||
my $otherOptions = {
|
||||
enabled => 1,
|
||||
label => 'Even harder cash',
|
||||
};
|
||||
my $anotherDriver = $pay->addPaymentGateway('WebGUI::Shop::PayDriver::Cash', 'Pomade', $otherOptions);
|
||||
|
||||
my $gateways = $pay->getPaymentGateways;
|
||||
my @returnedIds = map {$_->getId} @{ $gateways };
|
||||
cmp_bag(
|
||||
\@returnedIds,
|
||||
[
|
||||
$newDriver->getId,
|
||||
$anotherDriver->getId,
|
||||
],
|
||||
'getPaymentGateways returns all create payment drivers',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# www_do
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# www_manage
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
$session->db->write('delete from paymentGateway');
|
||||
}
|
||||
472
t/Shop/PayDriver.t
Normal file
472
t/Shop/PayDriver.t
Normal file
|
|
@ -0,0 +1,472 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 JSON;
|
||||
use HTML::Form;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 46;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# figure out if the test can actually run
|
||||
|
||||
my $e;
|
||||
|
||||
my $loaded = use_ok('WebGUI::Shop::PayDriver');
|
||||
|
||||
my $storage;
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip 'Unable to load module WebGUI::Shop::PayDriver', $tests unless $loaded;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# definition
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $definition;
|
||||
|
||||
eval { $definition = WebGUI::Shop::PayDriver->definition(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'definition takes an exception to not giving it a session variable');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a session variable',
|
||||
),
|
||||
'definition: requires a session variable',
|
||||
);
|
||||
|
||||
$definition = WebGUI::Shop::PayDriver->definition($session);
|
||||
|
||||
cmp_deeply (
|
||||
$definition,
|
||||
[ {
|
||||
name => 'Payment Driver',
|
||||
properties => {
|
||||
label => {
|
||||
fieldType => 'text',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => "Credit Card",
|
||||
},
|
||||
enabled => {
|
||||
fieldType => 'yesNo',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => 1,
|
||||
},
|
||||
groupToUse => {
|
||||
fieldType => 'group',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => 1,
|
||||
},
|
||||
receiptMessage => {
|
||||
fieldType => 'text',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => undef,
|
||||
},
|
||||
}
|
||||
} ],
|
||||
,
|
||||
'Definition returns an array of hashrefs',
|
||||
);
|
||||
|
||||
$definition = WebGUI::Shop::PayDriver->definition($session, [ { name => 'Red' }]);
|
||||
|
||||
cmp_deeply (
|
||||
$definition,
|
||||
[
|
||||
{
|
||||
name => 'Red',
|
||||
},
|
||||
{
|
||||
name => 'Payment Driver',
|
||||
properties => ignore(),
|
||||
}
|
||||
],
|
||||
,
|
||||
'New data is appended correctly',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# create
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $driver;
|
||||
|
||||
# Test incorrect for parameters
|
||||
|
||||
eval { $driver = WebGUI::Shop::PayDriver->create(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a session object');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a session variable',
|
||||
),
|
||||
'create takes exception to not giving it a session object',
|
||||
);
|
||||
|
||||
eval { $driver = WebGUI::Shop::PayDriver->create($session); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a label');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a human readable label in the hashref of options',
|
||||
),
|
||||
'create takes exception to not giving it a hashref of options',
|
||||
);
|
||||
|
||||
eval { $driver = WebGUI::Shop::PayDriver->create($session, 'Very human readable label'); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a hashref of options');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a hashref of options',
|
||||
),
|
||||
'create takes exception to not giving it a hashref of options',
|
||||
);
|
||||
|
||||
eval { $driver = WebGUI::Shop::PayDriver->create($session, 'Very human readable label', {}); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it an empty hashref of options');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a hashref of options',
|
||||
),
|
||||
'create takes exception to not giving it an empty hashref of options',
|
||||
);
|
||||
|
||||
# Test functionality
|
||||
|
||||
my $label = 'Human Readable Label';
|
||||
my $options = {
|
||||
label => 'Fast and harmless',
|
||||
enabled => 1,
|
||||
group => 3,
|
||||
receiptMessage => 'Pannenkoeken zijn nog lekkerder met spek',
|
||||
};
|
||||
|
||||
$driver = WebGUI::Shop::PayDriver->create( $session, $label, $options );
|
||||
|
||||
isa_ok ($driver, 'WebGUI::Shop::PayDriver', 'create creates WebGUI::Shop::PayDriver object');
|
||||
|
||||
my $dbData = $session->db->quickHashRef('select * from paymentGateway where paymentGatewayId=?', [ $driver->getId ]);
|
||||
|
||||
#diag ($driver->getId);
|
||||
cmp_deeply (
|
||||
$dbData,
|
||||
{
|
||||
paymentGatewayId => $driver->getId,
|
||||
className => ref $driver,
|
||||
label => $driver->label,
|
||||
options => q|{"group":3,"receiptMessage":"Pannenkoeken zijn nog lekkerder met spek","label":"Fast and harmless","enabled":1}|,
|
||||
},
|
||||
'Correct data written to the db',
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# session
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
isa_ok ($driver->session, 'WebGUI::Session', 'session method returns a session object');
|
||||
is ($session->getId, $driver->session->getId, 'session method returns OUR session object');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# paymentGatewayId, getId
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
like ($driver->paymentGatewayId, $session->id->getValidator, 'got a valid GUID for paymentGatewayId');
|
||||
is ($driver->getId, $driver->paymentGatewayId, 'getId returns the same thing as paymentGatewayId');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# className
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
is ($driver->className, ref $driver, 'className property set correctly');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# options
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
cmp_deeply ($driver->options, $options, 'options accessor works');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getName
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { WebGUI::Shop::PayDriver->getName(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'getName requires a session object passed to it');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a session variable',
|
||||
),
|
||||
'getName requires a session object passed to it',
|
||||
);
|
||||
|
||||
is (WebGUI::Shop::PayDriver->getName($session), 'Payment Driver', 'getName returns the human readable name of this driver');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# get
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
cmp_deeply ($driver->get, $driver->options, 'get works like the options method with no param passed');
|
||||
is ($driver->get('enabled'), 1, 'get the enabled entry from the options');
|
||||
is ($driver->get('label'), 'Fast and harmless', 'get the label entry from the options');
|
||||
|
||||
my $optionsCopy = $driver->get;
|
||||
$optionsCopy->{label} = 'And now for something completely different';
|
||||
isnt ($driver->get('label'), 'And now for something completely different',
|
||||
'hashref returned by get() is a copy of the internal hashref');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getCart
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $cart = $driver->getCart;
|
||||
isa_ok ($cart, 'WebGUI::Shop::Cart', 'getCart returns an instantiated WebGUI::Shop::Cart object');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getEditForm
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $form = $driver->getEditForm;
|
||||
|
||||
isa_ok ($form, 'WebGUI::HTMLForm', 'getEditForm returns an HTMLForm object');
|
||||
|
||||
my $html = $form->print;
|
||||
|
||||
##Any URL is fine, really
|
||||
my @forms = HTML::Form->parse($html, 'http://www.webgui.org');
|
||||
is (scalar @forms, 1, 'getEditForm generates just 1 form');
|
||||
|
||||
my @inputs = $forms[0]->inputs;
|
||||
is (scalar @inputs, 10, 'getEditForm: the form has 10 controls');
|
||||
|
||||
my @interestingFeatures;
|
||||
foreach my $input (@inputs) {
|
||||
my $name = $input->name;
|
||||
my $type = $input->type;
|
||||
push @interestingFeatures, { name => $name, type => $type };
|
||||
}
|
||||
|
||||
cmp_deeply(
|
||||
\@interestingFeatures,
|
||||
[
|
||||
{
|
||||
name => undef,
|
||||
type => 'submit',
|
||||
},
|
||||
{
|
||||
name => 'shop',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'method',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'do',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'paymentGatewayId',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'className',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'label',
|
||||
type => 'text',
|
||||
},
|
||||
{
|
||||
name => 'enabled',
|
||||
type => 'radio',
|
||||
},
|
||||
{
|
||||
name => 'groupToUse',
|
||||
type => 'option',
|
||||
},
|
||||
{
|
||||
name => 'receiptMessage',
|
||||
type => 'text',
|
||||
},
|
||||
],
|
||||
'getEditForm made the correct form with all the elements'
|
||||
|
||||
);
|
||||
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $oldDriver;
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::PayDriver->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 => 'Must provide a session variable',
|
||||
),
|
||||
'new takes exception to not giving it a session object',
|
||||
);
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::PayDriver->new($session); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'new takes exception to not giving it a paymentGatewayId');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a paymentGatewayId',
|
||||
),
|
||||
'new takes exception to not giving it a paymentGatewayId',
|
||||
);
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::PayDriver->new($session, 'notEverAnId'); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::ObjectNotFound', 'new croaks unless the requested paymentGatewayId object exists in the db');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'paymentGatewayId not found in db',
|
||||
id => 'notEverAnId',
|
||||
),
|
||||
'new croaks unless the requested paymentGatewayId object exists in the db',
|
||||
);
|
||||
|
||||
my $driverCopy = WebGUI::Shop::PayDriver->new($session, $driver->getId);
|
||||
|
||||
is ($driver->getId, $driverCopy->getId, 'same id');
|
||||
is ($driver->className, $driverCopy->className, 'same className');
|
||||
cmp_deeply ($driver->options, $driverCopy->options, 'same options');
|
||||
|
||||
TODO: {
|
||||
local $TODO = 'tests for new';
|
||||
ok(0, 'Test broken options in the db');
|
||||
}
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# update
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $driver->update(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok ($e, 'WebGUI::Error::InvalidParam', 'update takes exception to not giving it a hashref of options');
|
||||
cmp_deeply (
|
||||
$e,
|
||||
methods(
|
||||
error => 'update was not sent a hashref of options to store in the database',
|
||||
),
|
||||
'update takes exception to not giving it a hashref of options',
|
||||
);
|
||||
|
||||
my $newOptions = {
|
||||
label => 'Yet another label',
|
||||
enabled => 0,
|
||||
group => 4,
|
||||
receiptMessage => 'Dropjes!',
|
||||
};
|
||||
|
||||
$driver->update($newOptions);
|
||||
my $storedOptions = $session->db->quickScalar('select options from paymentGateway where paymentGatewayId=?', [
|
||||
$driver->getId,
|
||||
]);
|
||||
cmp_deeply(
|
||||
$newOptions,
|
||||
from_json($storedOptions),
|
||||
,
|
||||
'update() actually stores data',
|
||||
);
|
||||
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# delete
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$driver->delete;
|
||||
|
||||
my $count = $session->db->quickScalar('select count(*) from paymentGateway where paymentGatewayId=?', [
|
||||
$driver->paymentGatewayId
|
||||
]);
|
||||
|
||||
is ($count, 0, 'delete deleted the object');
|
||||
|
||||
undef $driver;
|
||||
|
||||
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
$session->db->write('delete from paymentGateway');
|
||||
}
|
||||
191
t/Shop/Ship.t
Normal file
191
t/Shop/Ship.t
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 JSON;
|
||||
use HTML::Form;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 19;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $loaded = use_ok('WebGUI::Shop::Ship');
|
||||
|
||||
my $storage;
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip 'Unable to load module WebGUI::Shop::Ship', $tests unless $loaded;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $e;
|
||||
my $ship;
|
||||
|
||||
eval { $ship = WebGUI::Shop::Ship->new(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new takes an exception to not giving it a session variable');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a session variable',
|
||||
got => '',
|
||||
expected => 'WebGUI::Session',
|
||||
),
|
||||
'new: requires a session variable',
|
||||
);
|
||||
|
||||
$ship = WebGUI::Shop::Ship->new($session);
|
||||
isa_ok($ship, 'WebGUI::Shop::Ship', 'new returned the right kind of object');
|
||||
|
||||
isa_ok($ship->session, 'WebGUI::Session', 'session method returns a session object');
|
||||
|
||||
is($session->getId, $ship->session->getId, 'session method returns OUR session object');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getDrivers
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $drivers;
|
||||
|
||||
$drivers = $ship->getDrivers();
|
||||
my @driverClasses = keys %{$drivers};
|
||||
cmp_deeply(
|
||||
\@driverClasses,
|
||||
[ 'WebGUI::Shop::ShipDriver::FlatRate' ],
|
||||
'getDrivers: WebGUI only ships with 1 default shipping driver',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# addShipper
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $shipper;
|
||||
|
||||
eval { $shipper = $ship->addShipper(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addShipper croaks without a class');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a class to create an object',
|
||||
),
|
||||
'addShipper croaks without a class',
|
||||
);
|
||||
|
||||
eval { $shipper = $ship->addShipper('WebGUI::Shop::ShipDriver::FreeShipping'); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addShipper croaks without a configured class');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'The requested class is not enabled in your WebGUI configuration file',
|
||||
param => 'WebGUI::Shop::ShipDriver::FreeShipping',
|
||||
),
|
||||
'addShipper croaks without a configured class',
|
||||
);
|
||||
|
||||
eval { $shipper = $ship->addShipper('WebGUI::Shop::ShipDriver::FlatRate'); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addShipper croaks without options to build a object with');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'You must pass a hashref of options to create a new ShipDriver object',
|
||||
),
|
||||
'addShipper croaks without options to build a object with',
|
||||
);
|
||||
|
||||
eval { $shipper = $ship->addShipper('WebGUI::Shop::ShipDriver::FlatRate', {}); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'addShipper croaks without options to build a object with');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'You must pass a hashref of options to create a new ShipDriver object',
|
||||
),
|
||||
'addShipper croaks without options to build a object with',
|
||||
);
|
||||
|
||||
my $driver = $ship->addShipper('WebGUI::Shop::ShipDriver::FlatRate', { enabled=>1, label=>q{Jake's Jailbird Airmail}});
|
||||
isa_ok($driver, 'WebGUI::Shop::ShipDriver::FlatRate', 'added a new, configured FlatRate driver');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getShippers
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $shippers;
|
||||
my $driver2 = $ship->addShipper('WebGUI::Shop::ShipDriver::FlatRate', { enabled=>1, label=>q{Tommy's cut-rate shipping}});
|
||||
|
||||
$shippers = $ship->getShippers();
|
||||
is(scalar @{$shippers}, 2, 'getShippers: got both shippers');
|
||||
|
||||
my @shipperNames = map { $_->get("label") } @{ $shippers };
|
||||
cmp_bag(
|
||||
\@shipperNames,
|
||||
[q{Jake's Jailbird Airmail},q{Tommy's cut-rate shipping}],
|
||||
'Returned shippers have the right data'
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getOptions
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $shippers = $ship->getOptions(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'getOptions takes exception to not giving it a cart');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need a cart.',
|
||||
),
|
||||
'getOptions takes exception to not giving it a cart',
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
$session->db->write('delete from shipper');
|
||||
}
|
||||
358
t/Shop/ShipDriver.t
Normal file
358
t/Shop/ShipDriver.t
Normal file
|
|
@ -0,0 +1,358 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 JSON;
|
||||
use HTML::Form;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 35;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $e;
|
||||
|
||||
my $loaded = use_ok('WebGUI::Shop::ShipDriver');
|
||||
|
||||
my $storage;
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip 'Unable to load module WebGUI::Shop::ShipDriver', $tests unless $loaded;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# definition
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $definition;
|
||||
|
||||
eval { $definition = WebGUI::Shop::ShipDriver->definition(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'definition takes an exception to not giving it a session variable');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a session variable',
|
||||
),
|
||||
'definition: requires a session variable',
|
||||
);
|
||||
|
||||
$definition = WebGUI::Shop::ShipDriver->definition($session);
|
||||
|
||||
cmp_deeply(
|
||||
$definition,
|
||||
[ {
|
||||
name => 'Shipper Driver',
|
||||
properties => {
|
||||
label => {
|
||||
fieldType => 'text',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => undef,
|
||||
},
|
||||
enabled => {
|
||||
fieldType => 'yesNo',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => 1,
|
||||
}
|
||||
}
|
||||
} ],
|
||||
,
|
||||
'Definition returns an array of hashrefs',
|
||||
);
|
||||
|
||||
$definition = WebGUI::Shop::ShipDriver->definition($session, [ { name => 'Red' }]);
|
||||
|
||||
cmp_deeply(
|
||||
$definition,
|
||||
[
|
||||
{
|
||||
name => 'Red',
|
||||
},
|
||||
{
|
||||
name => 'Shipper Driver',
|
||||
properties => ignore(),
|
||||
}
|
||||
],
|
||||
,
|
||||
'New data is appended correctly',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# create
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $driver;
|
||||
|
||||
eval { $driver = WebGUI::Shop::ShipDriver->create(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a session object');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a session variable',
|
||||
),
|
||||
'create takes exception to not giving it a session object',
|
||||
);
|
||||
|
||||
eval { $driver = WebGUI::Shop::ShipDriver->create($session); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it a hashref of options');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a hashref of options',
|
||||
),
|
||||
'create takes exception to not giving it a hashref of options',
|
||||
);
|
||||
|
||||
|
||||
eval { $driver = WebGUI::Shop::ShipDriver->create($session, {}); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'create takes exception to not giving it an empty hashref of options');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a hashref of options',
|
||||
),
|
||||
'create takes exception to not giving it an empty hashref of options',
|
||||
);
|
||||
|
||||
my $options = {
|
||||
label => 'Slow and dangerous',
|
||||
enabled => 1,
|
||||
};
|
||||
|
||||
$driver = WebGUI::Shop::ShipDriver->create( $session, $options );
|
||||
|
||||
isa_ok($driver, 'WebGUI::Shop::ShipDriver');
|
||||
|
||||
isa_ok($driver->session, 'WebGUI::Session', 'session method returns a session object');
|
||||
|
||||
is($session->getId, $driver->session->getId, 'session method returns OUR session object');
|
||||
|
||||
like($driver->getId, $session->id->getValidator, 'got a valid GUID for shipperId');
|
||||
|
||||
|
||||
cmp_deeply($driver->get, $options, 'options accessor works');
|
||||
|
||||
my $dbData = $session->db->quickHashRef('select * from shipper where shipperId=?',[$driver->getId]);
|
||||
cmp_deeply(
|
||||
$dbData,
|
||||
{
|
||||
shipperId => $driver->getId,
|
||||
className => ref($driver),
|
||||
options => q|{"label":"Slow and dangerous","enabled":1}|,
|
||||
},
|
||||
'Correct data written to the db',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getName
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
is (WebGUI::Shop::ShipDriver->getName($session), 'Shipper Driver', 'getName returns the human readable name of this driver');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# get
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
is($driver->get('enabled'), 1, 'get the enabled entry from the options');
|
||||
is($driver->get('label'), 'Slow and dangerous', 'get the label entry from the options');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getEditForm
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $form = $driver->getEditForm;
|
||||
|
||||
isa_ok($form, 'WebGUI::HTMLForm', 'getEditForm returns an HTMLForm object');
|
||||
|
||||
my $html = $form->print;
|
||||
|
||||
##Any URL is fine, really
|
||||
my @forms = HTML::Form->parse($html, 'http://www.webgui.org');
|
||||
is (scalar @forms, 1, 'getEditForm generates just 1 form');
|
||||
|
||||
my @inputs = $forms[0]->inputs;
|
||||
is (scalar @inputs, 7, 'getEditForm: the form has 7 controls');
|
||||
|
||||
my @interestingFeatures;
|
||||
foreach my $input (@inputs) {
|
||||
my $name = $input->name;
|
||||
my $type = $input->type;
|
||||
push @interestingFeatures, { name => $name, type => $type };
|
||||
}
|
||||
|
||||
cmp_deeply(
|
||||
\@interestingFeatures,
|
||||
[
|
||||
{
|
||||
name => undef,
|
||||
type => 'submit',
|
||||
},
|
||||
{
|
||||
name => 'driverId',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'shop',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'method',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'do',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'label',
|
||||
type => 'text',
|
||||
},
|
||||
{
|
||||
name => 'enabled',
|
||||
type => 'radio',
|
||||
},
|
||||
],
|
||||
'getEditForm made the correct form with all the elements'
|
||||
|
||||
);
|
||||
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $oldDriver;
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::ShipDriver->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 => 'Must provide a session variable',
|
||||
),
|
||||
'new takes exception to not giving it a session object',
|
||||
);
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new takes exception to not giving it a shipperId');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a shipperId',
|
||||
),
|
||||
'new takes exception to not giving it a shipperId',
|
||||
);
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session, 'notEverAnId'); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::ObjectNotFound', 'new croaks unless the requested shipperId object exists in the db');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'shipperId not found in db',
|
||||
id => 'notEverAnId',
|
||||
),
|
||||
'new croaks unless the requested shipperId object exists in the db',
|
||||
);
|
||||
|
||||
my $driverCopy = WebGUI::Shop::ShipDriver->new($session, $driver->getId);
|
||||
|
||||
is($driver->getId, $driverCopy->getId, 'same id');
|
||||
is(ref $driver, ref $driverCopy, 'same className');
|
||||
cmp_deeply($driver->get, $driverCopy->get, 'same options');
|
||||
|
||||
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# calculate
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $driver->calculate; };
|
||||
like ($@, qr/^You must override the calculate method/, 'calculate croaks to force overriding it in the child classes');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# set
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $driver->update(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'update takes exception to not giving it a hashref of options');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'update was not sent a hashref of options to store in the database',
|
||||
),
|
||||
'update takes exception to not giving it a hashref of options',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# delete
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
#$driver->delete;
|
||||
#
|
||||
#my $count = $session->db->quickScalar('select count(*) from shipper where shipperId=?',[$driver->shipperId]);
|
||||
#is($count, 0, 'delete deleted the object');
|
||||
#
|
||||
#undef $driver;
|
||||
|
||||
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
#$session->db->write('delete from shipper');
|
||||
}
|
||||
256
t/Shop/ShipDriver/FlatRate.t
Normal file
256
t/Shop/ShipDriver/FlatRate.t
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 JSON;
|
||||
use HTML::Form;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 11;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $loaded = use_ok('WebGUI::Shop::ShipDriver::FlatRate');
|
||||
|
||||
my $storage;
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip 'Unable to load module WebGUI::Shop::ShipDriver::FlatRate', $tests unless $loaded;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# definition
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $definition;
|
||||
my $e; ##Exception variable, used throughout the file
|
||||
|
||||
eval { $definition = WebGUI::Shop::ShipDriver::FlatRate->definition(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'definition takes an exception to not giving it a session variable');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Must provide a session variable',
|
||||
),
|
||||
'definition: requires a session variable',
|
||||
);
|
||||
|
||||
|
||||
$definition = WebGUI::Shop::ShipDriver::FlatRate->definition($session);
|
||||
|
||||
cmp_deeply(
|
||||
$definition,
|
||||
[ {
|
||||
name => 'Flat Rate',
|
||||
properties => {
|
||||
flatFee => {
|
||||
fieldType => 'float',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => 0,
|
||||
},
|
||||
percentageOfPrice => {
|
||||
fieldType => 'float',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => 0,
|
||||
},
|
||||
pricePerWeight => {
|
||||
fieldType => 'float',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => 0,
|
||||
},
|
||||
pricePerItem => {
|
||||
fieldType => 'float',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => 0,
|
||||
},
|
||||
}
|
||||
},
|
||||
{
|
||||
name => 'Shipper Driver',
|
||||
properties => {
|
||||
label => {
|
||||
fieldType => 'text',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => undef,
|
||||
},
|
||||
enabled => {
|
||||
fieldType => 'yesNo',
|
||||
label => ignore(),
|
||||
hoverHelp => ignore(),
|
||||
defaultValue => 1,
|
||||
},
|
||||
}
|
||||
} ],
|
||||
'Definition returns an array of hashrefs',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# create
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $driver;
|
||||
|
||||
my $options = {
|
||||
label => 'flat rate, ship weight, items in the cart',
|
||||
enabled => 1,
|
||||
flatFee => 1.00,
|
||||
percentageOfPrice => 5,
|
||||
pricePerWeight => 0.5,
|
||||
pricePerItem => 0.1,
|
||||
};
|
||||
|
||||
$driver = WebGUI::Shop::ShipDriver::FlatRate->create($session, $options);
|
||||
|
||||
isa_ok($driver, 'WebGUI::Shop::ShipDriver::FlatRate');
|
||||
|
||||
isa_ok($driver, 'WebGUI::Shop::ShipDriver');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getName
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
is (WebGUI::Shop::ShipDriver::FlatRate->getName($session), 'Flat Rate', 'getName returns the human readable name of this driver');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getEditForm
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $form = $driver->getEditForm;
|
||||
|
||||
isa_ok($form, 'WebGUI::HTMLForm', 'getEditForm returns an HTMLForm object');
|
||||
|
||||
my $html = $form->print;
|
||||
|
||||
##Any URL is fine, really
|
||||
my @forms = HTML::Form->parse($html, 'http://www.webgui.org');
|
||||
is (scalar @forms, 1, 'getEditForm generates just 1 form');
|
||||
|
||||
my @inputs = $forms[0]->inputs;
|
||||
is (scalar @inputs, 11, 'getEditForm: the form has 11 controls');
|
||||
|
||||
my @interestingFeatures;
|
||||
foreach my $input (@inputs) {
|
||||
my $name = $input->name;
|
||||
my $type = $input->type;
|
||||
push @interestingFeatures, { name => $name, type => $type };
|
||||
}
|
||||
|
||||
cmp_deeply(
|
||||
\@interestingFeatures,
|
||||
[
|
||||
{
|
||||
name => undef,
|
||||
type => 'submit',
|
||||
},
|
||||
{
|
||||
name => 'driverId',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'shop',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'method',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'do',
|
||||
type => 'hidden',
|
||||
},
|
||||
{
|
||||
name => 'label',
|
||||
type => 'text',
|
||||
},
|
||||
{
|
||||
name => 'enabled',
|
||||
type => 'radio',
|
||||
},
|
||||
{
|
||||
name => 'flatFee',
|
||||
type => 'text',
|
||||
},
|
||||
{
|
||||
name => 'percentageOfPrice',
|
||||
type => 'text',
|
||||
},
|
||||
{
|
||||
name => 'pricePerWeight',
|
||||
type => 'text',
|
||||
},
|
||||
{
|
||||
name => 'pricePerItem',
|
||||
type => 'text',
|
||||
},
|
||||
],
|
||||
'getEditForm made the correct form with all the elements'
|
||||
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# delete
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$driver->delete;
|
||||
|
||||
my $count = $session->db->quickScalar('select count(*) from shipper where shipperId=?',[$driver->getId]);
|
||||
is($count, 0, 'delete deleted the object');
|
||||
|
||||
undef $driver;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# calculate
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
$session->db->write('delete from shipper');
|
||||
}
|
||||
690
t/Shop/Tax.t
Normal file
690
t/Shop/Tax.t
Normal file
|
|
@ -0,0 +1,690 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 Exception::Class;
|
||||
use Data::Dumper;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Text;
|
||||
use WebGUI::Shop::Cart;
|
||||
use WebGUI::Shop::AddressBook;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $addExceptions = getAddExceptions($session);
|
||||
|
||||
my $tests = 79 + 2*scalar(@{$addExceptions});
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $loaded = use_ok('WebGUI::Shop::Tax');
|
||||
|
||||
my $storage;
|
||||
|
||||
SKIP: {
|
||||
|
||||
skip 'Unable to load module WebGUI::Shop::Tax', $tests unless $loaded;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $taxer = WebGUI::Shop::Tax->new($session);
|
||||
|
||||
isa_ok($taxer, 'WebGUI::Shop::Tax');
|
||||
|
||||
isa_ok($taxer->session, 'WebGUI::Session', 'session method returns a session object');
|
||||
|
||||
is($session->getId, $taxer->session->getId, 'session method returns OUR session object');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getItems
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $taxIterator = $taxer->getItems;
|
||||
|
||||
isa_ok($taxIterator, 'WebGUI::SQL::ResultSet');
|
||||
|
||||
is($taxIterator->rows, 0, 'WebGUI ships with no predefined tax data');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# add
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $e;
|
||||
|
||||
eval{$taxer->add()};
|
||||
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'add: correct type of exception thrown for missing hashref');
|
||||
is($e->error, 'Must pass in a hashref of params', 'add: correct message for a missing hashref');
|
||||
|
||||
foreach my $inputSet ( @{ $addExceptions } ){
|
||||
eval{$taxer->add($inputSet->{args})};
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'add: '.$inputSet->{comment});
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => $inputSet->{error},
|
||||
param => $inputSet->{param},
|
||||
),
|
||||
'add: '.$inputSet->{comment},
|
||||
);
|
||||
}
|
||||
|
||||
my $taxData = {
|
||||
country => 'USA',
|
||||
state => 'OR',
|
||||
taxRate => '0',
|
||||
};
|
||||
|
||||
my $oregonTaxId = $taxer->add($taxData);
|
||||
|
||||
ok($session->id->valid($oregonTaxId), 'add method returns a valid GUID');
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 1, 'add added only 1 row to the tax table');
|
||||
|
||||
my $addedData = $taxIterator->hashRef;
|
||||
$taxData->{taxId} = $oregonTaxId;
|
||||
$taxData->{city} = undef;
|
||||
$taxData->{code} = undef;
|
||||
|
||||
cmp_deeply($addedData, $taxData, 'add put the right data into the database for Oregon');
|
||||
|
||||
$taxData = {
|
||||
country => 'USA',
|
||||
state => 'Wisconsin',
|
||||
city => 'Madcity',
|
||||
code => '53702',
|
||||
taxRate => '5',
|
||||
};
|
||||
|
||||
my $wisconsinTaxId = $taxer->add($taxData);
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 2, 'add added another row to the tax table');
|
||||
|
||||
$taxData = {
|
||||
country => 'USA',
|
||||
state => 'Oregon',
|
||||
taxRate => '0.1',
|
||||
};
|
||||
|
||||
my $dupId = $taxer->add($taxData);
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 3, 'add permits adding duplicate information.');
|
||||
|
||||
##Madison zip codes:
|
||||
##53701-53709
|
||||
##city rate: 0.5%
|
||||
##Wisconsin rate 5.0%
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getAllItems
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $expectedTaxData = [
|
||||
{
|
||||
country => 'USA',
|
||||
state => 'OR',
|
||||
city => undef,
|
||||
code => undef,
|
||||
taxRate => 0,
|
||||
},
|
||||
{
|
||||
country => 'USA',
|
||||
state => 'Wisconsin',
|
||||
city => 'Madcity',
|
||||
code => '53702',
|
||||
taxRate => 5,
|
||||
},
|
||||
{
|
||||
country => 'USA',
|
||||
state => 'Oregon',
|
||||
city => undef,
|
||||
code => undef,
|
||||
taxRate => 0.1,
|
||||
},
|
||||
];
|
||||
|
||||
cmp_bag(
|
||||
$taxer->getAllItems,
|
||||
$expectedTaxData,
|
||||
'getAllItems returns the whole set of tax data',
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# delete
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval{$taxer->delete()};
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'delete: error handling for missing hashref');
|
||||
is($e->error, 'Must pass in a hashref of params', 'delete: error message for missing hashref');
|
||||
|
||||
eval{$taxer->delete({})};
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'delete: error handling for missing key in hashref');
|
||||
is($e->error, 'Hash ref must contain a taxId key with a defined value', 'delete: error message for missing key in hashref');
|
||||
|
||||
eval{$taxer->delete({ taxId => undef })};
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'delete: error handling for an undefined taxId value');
|
||||
is($e->error, 'Hash ref must contain a taxId key with a defined value', 'delete: error message for an undefined taxId value');
|
||||
|
||||
$taxer->delete({ taxId => $dupId });
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 2, 'One row was deleted from the tax table, even though another row has duplicate information');
|
||||
|
||||
$taxer->delete({ taxId => $oregonTaxId });
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 1, 'Another row was deleted from the tax table');
|
||||
|
||||
$taxer->delete({ taxId => $session->id->generate });
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 1, 'No rows were deleted from the table since the requested id does not exist');
|
||||
is($taxIterator->hashRef->{taxId}, $wisconsinTaxId, 'The correct tax information was deleted');
|
||||
|
||||
########################################################################
|
||||
##
|
||||
## exportTaxData
|
||||
##
|
||||
########################################################################
|
||||
|
||||
$storage = $taxer->exportTaxData();
|
||||
isa_ok($storage, 'WebGUI::Storage', 'exportTaxData returns a WebGUI::Storage object');
|
||||
is($storage->{_part1}, 'temp', 'The storage object is in the temporary area');
|
||||
ok(-e $storage->getPath('siteTaxData.csv'), 'siteTaxData.csv file exists in the storage object');
|
||||
cmp_ok($storage->getFileSize('siteTaxData.csv'), '!=', 0, 'CSV file is not empty');
|
||||
my @fileLines = split /\n+/, $storage->getFileContentsAsScalar('siteTaxData.csv');
|
||||
#my @fileLines = ();
|
||||
my @header = WebGUI::Text::splitCSV($fileLines[0]);
|
||||
my @expectedHeader = qw/country state city code taxRate/;
|
||||
cmp_deeply(\@header, \@expectedHeader, 'exportTaxData: header line is correct');
|
||||
my @row1 = WebGUI::Text::splitCSV($fileLines[1]);
|
||||
my $wiData = $taxer->getItems->hashRef;
|
||||
##Need to ignore the taxId from the database
|
||||
cmp_bag([ @{ $wiData }{ @expectedHeader } ], \@row1, 'exportTaxData: first line of data is correct');
|
||||
|
||||
my $newTaxId = $taxer->add({
|
||||
country => 'USA|U.S.A.',
|
||||
state => 'washington|WA',
|
||||
taxRate => '7',
|
||||
code => '',
|
||||
city => '',
|
||||
});
|
||||
$taxer->delete({taxId => $wisconsinTaxId});
|
||||
$storage = $taxer->exportTaxData();
|
||||
@fileLines = split /\n+/, $storage->getFileContentsAsScalar('siteTaxData.csv');
|
||||
my @row1 = WebGUI::Text::splitCSV($fileLines[1]);
|
||||
my $wiData = $taxer->getItems->hashRef;
|
||||
##Need to ignore the taxId from the database
|
||||
cmp_bag([ @{ $wiData }{ @expectedHeader } ], \@row1, 'exportTaxData: first line of data is correct');
|
||||
|
||||
$taxer->delete({taxId => $newTaxId});
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# import
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $taxer->importTaxData(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'importTaxData: error handling for an undefined taxId value');
|
||||
is($e->error, 'Must provide the path to a file', 'importTaxData: error handling for an undefined taxId value');
|
||||
|
||||
eval { $taxer->importTaxData('/path/to/nowhere'); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidFile', 'importTaxData: error handling for file that does not exist in the filesystem');
|
||||
is($e->error, 'File could not be found', 'importTaxData: error handling for file that does not exist in the filesystem');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
brokenFile => '/path/to/nowhere',
|
||||
),
|
||||
'importTaxData: error handling for file that does not exist in the filesystem',
|
||||
);
|
||||
|
||||
my $taxFile = WebGUI::Test->getTestCollateralPath('taxTables/goodTaxTable.csv');
|
||||
|
||||
SKIP: {
|
||||
skip 'Root will cause this test to fail since it does not obey file permissions', 1
|
||||
if $< == 0;
|
||||
|
||||
my $originalChmod = (stat $taxFile)[2];
|
||||
chmod oct(0000), $taxFile;
|
||||
|
||||
eval { $taxer->importTaxData($taxFile); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidFile', 'importTaxData: error handling for file that cannot be read');
|
||||
is($e->error, 'File is not readable', 'importTaxData: error handling for file that that cannot be read');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
brokenFile => $taxFile,
|
||||
),
|
||||
'importTaxData: error handling for file that that cannot be read',
|
||||
);
|
||||
|
||||
chmod $originalChmod, $taxFile;
|
||||
|
||||
}
|
||||
|
||||
my $expectedTaxData = [
|
||||
{
|
||||
country => 'USA',
|
||||
state => '',
|
||||
city => '',
|
||||
code => '',
|
||||
taxRate => 0,
|
||||
},
|
||||
{
|
||||
country => 'USA',
|
||||
state => 'Wisconsin',
|
||||
city => '',
|
||||
code => '',
|
||||
taxRate => 5,
|
||||
},
|
||||
{
|
||||
country => 'USA',
|
||||
state => 'Wisconsin',
|
||||
city => 'Madison',
|
||||
code => '53701',
|
||||
taxRate => 0.5,
|
||||
},
|
||||
];
|
||||
|
||||
ok(
|
||||
$taxer->importTaxData(
|
||||
$taxFile
|
||||
),
|
||||
'Good tax data inserted',
|
||||
);
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 3, 'import: Old data deleted, new data imported');
|
||||
cmp_bag(
|
||||
$taxer->getAllItems,
|
||||
$expectedTaxData,
|
||||
'Correct data inserted.',
|
||||
);
|
||||
|
||||
ok(
|
||||
$taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/orderedTaxTable.csv')
|
||||
),
|
||||
'Reordered tax data inserted',
|
||||
);
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 3, 'import: Old data deleted, new data imported again');
|
||||
cmp_bag(
|
||||
$taxer->getAllItems,
|
||||
$expectedTaxData,
|
||||
'Correct data inserted, with CSV in different columnar order.',
|
||||
);
|
||||
|
||||
ok(
|
||||
$taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/commentedTaxTable.csv')
|
||||
),
|
||||
'Commented tax data inserted',
|
||||
);
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 3, 'import: Old data deleted, new data imported the third time');
|
||||
cmp_bag(
|
||||
$taxer->getAllItems,
|
||||
$expectedTaxData,
|
||||
'Correct data inserted, with comments in the CSV file',
|
||||
);
|
||||
|
||||
ok(
|
||||
! $taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/emptyTaxTable.csv')
|
||||
),
|
||||
'Empty tax data not inserted',
|
||||
);
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 3, 'import: Old data still exists and was not deleted');
|
||||
|
||||
my $failure;
|
||||
eval {
|
||||
$failure = $taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/badTaxTable.csv')
|
||||
);
|
||||
};
|
||||
ok (!$failure, 'Tax data not imported');
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidFile', 'importTaxData: a file with an error on 1 line');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Error found in the CSV file',
|
||||
brokenFile => WebGUI::Test->getTestCollateralPath('taxTables/badTaxTable.csv'),
|
||||
brokenLine => 1,
|
||||
),
|
||||
'importTaxData: error handling for file with errors in the CSV data',
|
||||
);
|
||||
|
||||
eval {
|
||||
$failure = $taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/missingHeaders.csv')
|
||||
);
|
||||
};
|
||||
ok (!$failure, 'Tax data not imported when headers are missing');
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidFile', 'importTaxData: a file with a missing header column');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Bad header found in the CSV file',
|
||||
brokenFile => WebGUI::Test->getTestCollateralPath('taxTables/missingHeaders.csv'),
|
||||
),
|
||||
'importTaxData: error handling for a file with a missing header',
|
||||
);
|
||||
|
||||
eval {
|
||||
$failure = $taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/badHeaders.csv')
|
||||
);
|
||||
};
|
||||
ok (!$failure, 'Tax data not imported when headers are wrong');
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidFile', 'importTaxData: a file with a bad header column');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Bad header found in the CSV file',
|
||||
brokenFile => WebGUI::Test->getTestCollateralPath('taxTables/badHeaders.csv'),
|
||||
),
|
||||
'importTaxData: error handling for a file with a bad header',
|
||||
);
|
||||
|
||||
ok(
|
||||
$taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/alternations.csv')
|
||||
),
|
||||
'Tax data with alternations inserted',
|
||||
);
|
||||
|
||||
my $altData = $taxer->getItems->hashRef; ##Just 1 row
|
||||
cmp_deeply(
|
||||
$altData,
|
||||
{
|
||||
taxId => ignore,
|
||||
country => q{U.S.A.,USA},
|
||||
state => q{WI,Wisconsin},
|
||||
city => q{Madison},
|
||||
code => 53701,
|
||||
taxRate => 0.5,
|
||||
},
|
||||
'import: Data correctly loaded with alternations'
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# getTaxRates
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
##Set up the tax information
|
||||
$taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/largeTaxTable.csv')
|
||||
),
|
||||
my $book = WebGUI::Shop::AddressBook->create($session);
|
||||
my $taxingAddress = $book->addAddress({
|
||||
label => 'taxing',
|
||||
city => 'Madison',
|
||||
state => 'WI',
|
||||
code => '53701',
|
||||
country => 'USA',
|
||||
});
|
||||
my $taxFreeAddress = $book->addAddress({
|
||||
label => 'no tax',
|
||||
city => 'Portland',
|
||||
state => 'OR',
|
||||
code => '97123',
|
||||
country => 'USA',
|
||||
});
|
||||
my $alternateAddress = $book->addAddress({
|
||||
label => 'using alternations',
|
||||
city => 'Los Angeles',
|
||||
state => 'CalifornIA',
|
||||
code => '92801',
|
||||
country => 'USA',
|
||||
});
|
||||
|
||||
eval { $taxer->getTaxRates(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidObject', 'calculate: error handling for not sending a cart');
|
||||
cmp_deeply(
|
||||
$e,
|
||||
methods(
|
||||
error => 'Need an address.',
|
||||
got => '',
|
||||
expected => 'WebGUI::Shop::Address',
|
||||
),
|
||||
'importTaxData: error handling for file that does not exist in the filesystem',
|
||||
);
|
||||
|
||||
cmp_deeply(
|
||||
$taxer->getTaxRates($taxingAddress),
|
||||
[0, 5, 0.5],
|
||||
'getTaxRates: return correct data for a state with tax data'
|
||||
);
|
||||
|
||||
cmp_deeply(
|
||||
$taxer->getTaxRates($taxFreeAddress),
|
||||
[0,0],
|
||||
'getTaxRates: return correct data for a state with no tax data'
|
||||
);
|
||||
|
||||
cmp_deeply(
|
||||
$taxer->getTaxRates($alternateAddress),
|
||||
[0.0, 8.25], #Hits USA and Los Angeles, California using the alternate spelling of the state
|
||||
'getTaxRates: return correct data for a state when the address has alternations'
|
||||
);
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# calculate
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $taxer->calculate(); };
|
||||
$e = Exception::Class->caught();
|
||||
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate: error handling for not sending a cart');
|
||||
is($e->error, 'Must pass in a WebGUI::Shop::Cart object', 'calculate: error handling for not sending a cart');
|
||||
|
||||
##Build a cart, add some Donation SKUs to it. Set one to be taxable.
|
||||
|
||||
my $cart = WebGUI::Shop::Cart->getCartBySession($session);
|
||||
|
||||
is($taxer->calculate($cart), 0, 'calculate returns 0 if there is no shippingAddressId in the cart');
|
||||
|
||||
$cart->update({ shippingAddressId => $taxingAddress->getId});
|
||||
|
||||
##Set up the tax information
|
||||
$taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/largeTaxTable.csv')
|
||||
),
|
||||
|
||||
my $taxableDonation = WebGUI::Asset->getRoot($session)->addChild({
|
||||
className => 'WebGUI::Asset::Sku::Donation',
|
||||
title => 'Taxable donation',
|
||||
defaultPrice => 100.00,
|
||||
});
|
||||
|
||||
$cart->addItem($taxableDonation);
|
||||
|
||||
foreach my $item (@{ $cart->getItems }) {
|
||||
$item->setQuantity(1);
|
||||
}
|
||||
|
||||
my $tax = $taxer->calculate($cart);
|
||||
is($tax, 5.5, 'calculate: simple tax calculation on 1 item in the cart');
|
||||
|
||||
$cart->update({ shippingAddressId => $taxFreeAddress->getId});
|
||||
is($taxer->calculate($cart), 0, 'calculate: simple tax calculation on 1 item in the cart, tax free location');
|
||||
|
||||
foreach my $item (@{ $cart->getItems }) {
|
||||
$item->setQuantity(2);
|
||||
}
|
||||
|
||||
$cart->update({ shippingAddressId => $taxingAddress->getId});
|
||||
is($taxer->calculate($cart), 11, 'calculate: simple tax calculation on 1 item in the cart, qty 2');
|
||||
|
||||
my $taxFreeDonation = WebGUI::Asset->getRoot($session)->addChild({
|
||||
className => 'WebGUI::Asset::Sku::Donation',
|
||||
title => 'Tax Free Donation',
|
||||
defaultPrice => 100.00,
|
||||
overrideTaxRate => 1,
|
||||
taxRateOverride => 0,
|
||||
});
|
||||
|
||||
$cart->addItem($taxFreeDonation);
|
||||
|
||||
foreach my $item (@{ $cart->getItems }) {
|
||||
$item->setQuantity(1);
|
||||
}
|
||||
is($taxer->calculate($cart), 5.5, 'calculate: simple tax calculation on 2 items in the cart, 1 without taxes');
|
||||
|
||||
my $remoteItem = $cart->addItem($taxableDonation);
|
||||
$remoteItem->update({shippingAddressId => $taxFreeAddress->getId});
|
||||
|
||||
foreach my $item (@{ $cart->getItems }) {
|
||||
$item->setQuantity(1);
|
||||
}
|
||||
is($taxer->calculate($cart), 5.5, 'calculate: simple tax calculation on 2 items in the cart, 1 without taxes, 1 shipped to a location with no taxes');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# www_getTaxesAsJson
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$session->user({userId=>3});
|
||||
my $json = $taxer->www_getTaxesAsJson();
|
||||
ok($json, 'www_getTaxesAsJson returned something');
|
||||
my $jsonTax = JSON::from_json($json);
|
||||
cmp_deeply(
|
||||
$jsonTax,
|
||||
{
|
||||
sort => undef,
|
||||
startIndex => 0,
|
||||
totalRecords => 1778,
|
||||
recordsReturned => 25,
|
||||
dir => 'desc',
|
||||
records => array_each({
|
||||
taxId=>ignore,
|
||||
country => 'USA',
|
||||
state=>ignore,
|
||||
city=>ignore,
|
||||
code=>ignore,
|
||||
taxRate=>re('^\d+\.\d+$')
|
||||
}),
|
||||
},
|
||||
'Check major elements of tax JSON',
|
||||
);
|
||||
|
||||
TODO: {
|
||||
local $TODO = 'More getTaxesAsJson tests';
|
||||
ok(0, 'test group privileges to this method');
|
||||
ok(0, 'test startIndex variable');
|
||||
ok(0, 'test results form variable');
|
||||
ok(0, 'test keywords');
|
||||
}
|
||||
|
||||
$cart->delete;
|
||||
$book->delete;
|
||||
$taxableDonation->purge;
|
||||
$taxFreeDonation->purge;
|
||||
}
|
||||
|
||||
sub getAddExceptions {
|
||||
my $session = shift;
|
||||
my $inputValidion = [
|
||||
{
|
||||
args => {},
|
||||
error => q{Missing required information.},
|
||||
param => q{country},
|
||||
comment => q{missing country},
|
||||
},
|
||||
{
|
||||
args => {country => undef},
|
||||
error => q{Missing required information.},
|
||||
param => q{country},
|
||||
comment => q{undef country},
|
||||
},
|
||||
{
|
||||
args => {country => ''},
|
||||
error => q{Missing required information.},
|
||||
param => q{country},
|
||||
comment => q{empty country},
|
||||
},
|
||||
{
|
||||
args => {country => 'USA'},
|
||||
error => q{Missing required information.},
|
||||
param => q{taxRate},
|
||||
comment => q{missing taxRate},
|
||||
},
|
||||
{
|
||||
args => {country => 'USA', taxRate => undef},
|
||||
error => q{Missing required information.},
|
||||
param => q{taxRate},
|
||||
comment => q{empty taxRate},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
$session->db->write('delete from tax');
|
||||
$session->db->write('delete from cart');
|
||||
$session->db->write('delete from addressBook');
|
||||
$session->db->write('delete from address');
|
||||
#$storage->delete;
|
||||
}
|
||||
240
t/Shop/Transaction.t
Normal file
240
t/Shop/Transaction.t
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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
|
||||
#------------------------------------------------------------------
|
||||
|
||||
# Tests the transaction backend for the shop.
|
||||
#
|
||||
#
|
||||
|
||||
use FindBin;
|
||||
use strict;
|
||||
use lib "$FindBin::Bin/../lib";
|
||||
use Test::More;
|
||||
use Test::Deep;
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Shop::Transaction;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
plan tests => 67; # Increment this number for each test you create
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
my $transaction = WebGUI::Shop::Transaction->create($session,{
|
||||
amount => 40,
|
||||
shippingAddressId => 'xxx1',
|
||||
shippingAddressName => 'abc',
|
||||
shippingAddress1 => 'def',
|
||||
shippingAddress2 => 'hij',
|
||||
shippingAddress3 => 'lmn',
|
||||
shippingCity => 'opq',
|
||||
shippingState => 'wxy',
|
||||
shippingCountry => 'z',
|
||||
shippingCode => '53333',
|
||||
shippingPhoneNumber => '123456',
|
||||
shippingDriverId => 'xxx2',
|
||||
shippingDriverLabel => 'foo',
|
||||
shippingPrice => 5,
|
||||
paymentAddressId => 'xxx3',
|
||||
paymentAddressName => 'abc1',
|
||||
paymentAddress1 => 'def1',
|
||||
paymentAddress2 => 'hij1',
|
||||
paymentAddress3 => 'lmn1',
|
||||
paymentCity => 'opq1',
|
||||
paymentState => 'wxy1',
|
||||
paymentCountry => 'z1',
|
||||
paymentCode => '66666',
|
||||
paymentPhoneNumber => '908765',
|
||||
paymentDriverId => 'xxx4',
|
||||
paymentDriverLabel => 'kkk',
|
||||
taxes => 7,
|
||||
});
|
||||
|
||||
# objects work
|
||||
isa_ok($transaction, "WebGUI::Shop::Transaction");
|
||||
isa_ok($transaction->session, "WebGUI::Session");
|
||||
|
||||
|
||||
# basic transaction properties
|
||||
is($transaction->get("amount"), 40, "set and get amount");
|
||||
is($transaction->get("shippingAddressId"), 'xxx1', "set and get shipping address id");
|
||||
is($transaction->get("shippingAddressName"), 'abc', "set and get shipping address name");
|
||||
is($transaction->get("shippingAddress1"), 'def', "set and get shipping address 1");
|
||||
is($transaction->get("shippingAddress2"), 'hij', "set and get shipping address 2");
|
||||
is($transaction->get("shippingAddress3"), 'lmn', "set and get shipping address 3");
|
||||
is($transaction->get("shippingCity"), 'opq', "set and get shipping city");
|
||||
is($transaction->get("shippingState"), 'wxy', "set and get shipping state");
|
||||
is($transaction->get("shippingCountry"), 'z', "set and get shipping country");
|
||||
is($transaction->get("shippingCode"), '53333', "set and get shipping code");
|
||||
is($transaction->get("shippingPhoneNumber"), '123456', "set and get shipping phone number");
|
||||
is($transaction->get("shippingDriverId"), 'xxx2', "set and get shipping driver id");
|
||||
is($transaction->get("shippingDriverLabel"), 'foo', "set and get shipping driver label");
|
||||
is($transaction->get("shippingPrice"), 5, "set and get shipping price");
|
||||
is($transaction->get("paymentAddressId"), 'xxx3', "set and get payment address id");
|
||||
is($transaction->get("paymentAddressName"), 'abc1', "set and get payment address name");
|
||||
is($transaction->get("paymentAddress1"), 'def1', "set and get payment address 1");
|
||||
is($transaction->get("paymentAddress2"), 'hij1', "set and get payment address 2");
|
||||
is($transaction->get("paymentAddress3"), 'lmn1', "set and get payment address 3");
|
||||
is($transaction->get("paymentCity"), 'opq1', "set and get payment city");
|
||||
is($transaction->get("paymentState"), 'wxy1', "set and get payment state");
|
||||
is($transaction->get("paymentCountry"), 'z1', "set and get payment country");
|
||||
is($transaction->get("paymentCode"), '66666', "set and get payment code");
|
||||
is($transaction->get("paymentPhoneNumber"), '908765', "set and get payment phone number");
|
||||
is($transaction->get("paymentDriverId"), 'xxx4', "set and get payment driver id");
|
||||
is($transaction->get("paymentDriverLabel"), 'kkk', "set and get payment driver label");
|
||||
is($transaction->get("taxes"), 7, "set and get taxes");
|
||||
|
||||
|
||||
$transaction->update({
|
||||
isSuccessful => 1,
|
||||
transactionCode => 'yyy',
|
||||
statusCode => 'jd31',
|
||||
statusMessage => 'was a success',
|
||||
});
|
||||
|
||||
is($transaction->get("isSuccessful"), 1,"update and get isSuccessful");
|
||||
is($transaction->get("transactionCode"), 'yyy',"update and get transaction code");
|
||||
is($transaction->get("statusCode"), 'jd31',"update and get status code");
|
||||
is($transaction->get("statusMessage"), 'was a success',"update and get status message");
|
||||
|
||||
# make sure new() works
|
||||
my $tcopy = WebGUI::Shop::Transaction->new($session, $transaction->getId);
|
||||
|
||||
isa_ok($tcopy, "WebGUI::Shop::Transaction");
|
||||
is($tcopy->getId, $transaction->getId, "is it the same object");
|
||||
|
||||
|
||||
# basic item properties
|
||||
my $item = $transaction->addItem({
|
||||
assetId => 'a',
|
||||
configuredTitle => 'b',
|
||||
options => {color=>'blue'},
|
||||
shippingAddressId => 'c',
|
||||
shippingName => 'd',
|
||||
shippingAddress1 => 'e',
|
||||
shippingAddress2 => 'f',
|
||||
shippingAddress3 => 'g',
|
||||
shippingCity => 'h',
|
||||
shippingState => 'i',
|
||||
shippingCountry => 'j',
|
||||
shippingCode => 'k',
|
||||
shippingPhoneNumber => 'l',
|
||||
quantity => 5,
|
||||
price => 33,
|
||||
});
|
||||
|
||||
isa_ok($item, "WebGUI::Shop::TransactionItem");
|
||||
isa_ok($item->transaction, "WebGUI::Shop::Transaction");
|
||||
|
||||
is($item->get("assetId"), 'a', "set and get asset id");
|
||||
is($item->get("configuredTitle"), 'b', "set and get configured title");
|
||||
cmp_deeply($item->get("options"), {color=>'blue'}, "set and get options");
|
||||
is($item->get("shippingAddressId"), 'c', "set and get shipping address id");
|
||||
is($item->get("shippingName"), 'd', "set and get shipping name");
|
||||
is($item->get("shippingAddress1"), 'e', "set and get shipping address 1");
|
||||
is($item->get("shippingAddress2"), 'f', "set and get shipping address 2");
|
||||
is($item->get("shippingAddress3"), 'g', "set and get shipping address 3");
|
||||
is($item->get("shippingCity"), 'h', "set and get shipping city");
|
||||
is($item->get("shippingState"), 'i', "set and get shipping state");
|
||||
is($item->get("shippingCountry"), 'j', "set and get shipping country");
|
||||
is($item->get("shippingCode"), 'k', "set and get shipping code");
|
||||
is($item->get("shippingPhoneNumber"), 'l', "set and get shipping phone number");
|
||||
is($item->get("quantity"), 5, "set and get quantity");
|
||||
is($item->get("price"), 33, "set and get price");
|
||||
|
||||
$item->update({
|
||||
shippingTrackingNumber => 'adfs',
|
||||
shippingStatus => 'BackOrdered',
|
||||
});
|
||||
|
||||
is($item->get("shippingTrackingNumber"), 'adfs', "update and get shipping tracking number");
|
||||
is($item->get("shippingStatus"), 'BackOrdered', "update and get shipping status");
|
||||
|
||||
# make sure shipping date is updated when shipping status is changed
|
||||
like($item->get("shippingDate"), qr/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/, "shipping date is set");
|
||||
my $dateNow = $item->get('shippingDate');
|
||||
sleep(1);
|
||||
$item->update({shippingStatus=>'Cancelled'});
|
||||
isnt($item->get('shippingDate'), $dateNow, 'shipping date is updated');
|
||||
|
||||
# make sure new() works
|
||||
my $icopy = $transaction->getItem($item->getId);
|
||||
isa_ok($icopy, "WebGUI::Shop::TransactionItem");
|
||||
is($icopy->getId, $item->getId, "items are the same");
|
||||
|
||||
# get items
|
||||
is(scalar @{$transaction->getItems}, 1, "can retrieve items");
|
||||
|
||||
# delete
|
||||
$item->delete;
|
||||
is(scalar @{$transaction->getItems}, 0, "can delete items");
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# www_getTaxesAsJson
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$session->user({userId=>3});
|
||||
my $json = WebGUI::Shop::Transaction->www_getTransactionsAsJson($session);
|
||||
ok($json, 'www_getTransactionsAsJson returned something');
|
||||
diag $json;
|
||||
my $jsonTransactions = JSON::from_json($json);
|
||||
cmp_deeply(
|
||||
$jsonTransactions,
|
||||
{
|
||||
sort => undef,
|
||||
startIndex => 0,
|
||||
totalRecords => 1,
|
||||
recordsReturned => 1,
|
||||
dir => 'desc',
|
||||
records => array_each({
|
||||
orderNumber=>ignore,
|
||||
transactionId=>ignore,
|
||||
transactionCode=>ignore,
|
||||
paymentDriverLabel=>ignore,
|
||||
dateOfPurchase=>ignore,
|
||||
username=>ignore,
|
||||
amount=>ignore,
|
||||
isSuccessful=>ignore,
|
||||
statusCode=>ignore,
|
||||
statusMessage=>ignore,
|
||||
}),
|
||||
},
|
||||
'Check major elements of transaction JSON',
|
||||
);
|
||||
|
||||
TODO: {
|
||||
local $TODO = 'More getTaxesAsJson tests';
|
||||
ok(0, 'test group privileges to this method');
|
||||
ok(0, 'test startIndex variable');
|
||||
ok(0, 'test results form variable');
|
||||
ok(0, 'test keywords');
|
||||
}
|
||||
|
||||
|
||||
$transaction->delete;
|
||||
is($session->db->quickScalar("select transactionId from transaction where transactionId=?",[$transaction->getId]), undef, "can delete transactions");
|
||||
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Cleanup
|
||||
END {
|
||||
$session->db->write('delete from transaction');
|
||||
}
|
||||
74
t/Shop/loadProducts.pl
Normal file
74
t/Shop/loadProducts.pl
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 qw(no_plan);
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Shop::Tax;
|
||||
use WebGUI::Asset::Wobject::Product;
|
||||
use WebGUI::VersionTag;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
##Create products by hand
|
||||
|
||||
my $tag = WebGUI::VersionTag->getWorking($session);
|
||||
|
||||
my $properties1 = {
|
||||
className => 'WebGUI::Asset::Wobject::Product',
|
||||
url => 'one',
|
||||
price => 10.00,
|
||||
productNumber => '#1',
|
||||
title => 'product 1',
|
||||
description => 'First product',
|
||||
};
|
||||
|
||||
my $root = WebGUI::Asset->getRoot($session);
|
||||
my $product1 = $root->addChild($properties1);
|
||||
|
||||
diag ref $product1;
|
||||
|
||||
my $properties2 = {
|
||||
className => 'WebGUI::Asset::Wobject::Product',
|
||||
url => 'two',
|
||||
price => 20.00,
|
||||
productNumber => '#2',
|
||||
title => 'product 2',
|
||||
description => 'Second product',
|
||||
};
|
||||
|
||||
my $product2 = $root->addChild($properties2);
|
||||
|
||||
diag ref $product2;
|
||||
|
||||
$tag->commit;
|
||||
sleep 2;
|
||||
|
||||
$tag = WebGUI::VersionTag->getWorking($session);
|
||||
|
||||
my $product1a = $product1->addRevision({price => 11.11});
|
||||
|
||||
$tag->commit;
|
||||
|
||||
diag "Done.";
|
||||
41
t/Shop/loadTaxes.pl
Normal file
41
t/Shop/loadTaxes.pl
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# vim:syntax=perl
|
||||
#-------------------------------------------------------------------
|
||||
# WebGUI is Copyright 2001-2008 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 qw(no_plan);
|
||||
use Test::Deep;
|
||||
use Exception::Class;
|
||||
use Data::Dumper;
|
||||
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Text;
|
||||
use WebGUI::Shop::Cart;
|
||||
use WebGUI::Shop::AddressBook;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# Init
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
|
||||
##Set up the tax information
|
||||
my $taxer = WebGUI::Shop::Tax->new($session);
|
||||
$taxer->importTaxData(
|
||||
WebGUI::Test->getTestCollateralPath('taxTables/largeTaxTable.csv')
|
||||
);
|
||||
123
t/lib/WebGUI/TestException.pm
Normal file
123
t/lib/WebGUI/TestException.pm
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
package WebGUI::TestException;
|
||||
|
||||
use strict;
|
||||
|
||||
use Test::Builder;
|
||||
use WebGUI::Exception;
|
||||
use Sub::Uplevel qw( uplevel );
|
||||
|
||||
our @EXPORT = qw( throws_deeply );
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::TestException
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This module provides a convenient way to test for thrown exceptions. The idea is based on Test::Exception, which
|
||||
does provide a means to test for a specific exception class, but cannot test attributes of that class, which is
|
||||
necessary in the WebGUI test suite. This module can do that.
|
||||
|
||||
=head1 CAVEATS
|
||||
|
||||
This module uses Sub::Uplevel. In Test::Exception some hocus pocus is being done with the caller() function. The
|
||||
functions _quiet_caller and _try_as_caller are directly copied from Test::Exception. I do not know why this
|
||||
hocuspocus is being in that module however, since doing 'eval { uplevel 1, $codeRef }' seems to work too. On my
|
||||
platform at least =). For the time being, I leave those subs in here so that they may be used. They are commented
|
||||
out by default, though.
|
||||
=cut
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub _quiet_caller (;$) { ## no critic Prototypes
|
||||
my $height = $_[0];
|
||||
$height++;
|
||||
if( wantarray and !@_ ) {
|
||||
return (CORE::caller($height))[0..2];
|
||||
}
|
||||
else {
|
||||
return CORE::caller($height);
|
||||
}
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub _try_as_caller {
|
||||
my $coderef = shift;
|
||||
|
||||
# local works here because Sub::Uplevel has already overridden caller
|
||||
local *CORE::GLOBAL::caller;
|
||||
{ no warnings 'redefine'; *CORE::GLOBAL::caller = \&_quiet_caller; }
|
||||
|
||||
eval { uplevel 3, $coderef };
|
||||
return $@;
|
||||
};
|
||||
|
||||
=head2 throws_deeply ( $codeRef, $expectClass, $fields, $message )
|
||||
|
||||
Executes the code ref and verifies it throws an exception of the given class with the given fields.
|
||||
|
||||
=head3 $codeRef
|
||||
|
||||
The code ref containing the code to be evalled.
|
||||
|
||||
=head3 $expectClass
|
||||
|
||||
The class name the thrown exception should have.
|
||||
|
||||
=head3 $fields
|
||||
|
||||
Hashref containg the exception fields and their expected values.
|
||||
|
||||
=head3 $message
|
||||
|
||||
The message that should be displayed by prove for this test.
|
||||
|
||||
=cut
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub throws_deeply {
|
||||
my $evalBlock = shift;
|
||||
my $expectClass = shift;
|
||||
my $fields = shift;
|
||||
my $message = shift;
|
||||
my $testBuilder = Test::Builder->new;
|
||||
|
||||
# Dunno why uplevel 1 might not work and why caller is redefined.
|
||||
# Copied _try_as_caller and _quiet_caller are from Test::Exception.
|
||||
# Uplevel 1 seems to work though.
|
||||
#_try_as_caller( $evalBlock );
|
||||
eval { uplevel 1, $evalBlock };
|
||||
|
||||
my $e = Exception::Class->caught();
|
||||
my $gotClass = ref $e;
|
||||
|
||||
# Check class
|
||||
unless ($gotClass eq $expectClass) {
|
||||
$testBuilder->ok(0, $message);
|
||||
$testBuilder->diag("Wrong class:\n\texpected : '$expectClass'\n\t got : '$gotClass'");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
# Check fields
|
||||
my $errors;
|
||||
foreach (keys %$fields) {
|
||||
my $result = $e->$_;
|
||||
|
||||
unless ( $result eq $fields->{$_} ) {
|
||||
$errors .= "'$_' => \n\texpected : '".$fields->{$_}."'\n\t got : '$result'\n";
|
||||
}
|
||||
}
|
||||
if ($errors) {
|
||||
$testBuilder->ok(0, $message);
|
||||
$testBuilder->diag("Fields do not match:\n$errors");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
# Test passed.
|
||||
$testBuilder->ok(1, $message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
2
t/supporting_collateral/taxTables/alternations.csv
Normal file
2
t/supporting_collateral/taxTables/alternations.csv
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
country,state,city,code,taxRate
|
||||
U.S.A.|USA,WI|Wisconsin,Madison,53701,0.5
|
||||
|
7
t/supporting_collateral/taxTables/badHeaders.csv
Normal file
7
t/supporting_collateral/taxTables/badHeaders.csv
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
country,state,city,zip,taxRate
|
||||
USA,,,,0.0
|
||||
USA,Wisconsin,,,5.0
|
||||
USA,Wisconsin,Madison,53701,0.5
|
||||
where,value,taxRates
|
||||
state,5.0
|
||||
code,53701,0.5
|
||||
|
4
t/supporting_collateral/taxTables/badTaxTable.csv
Normal file
4
t/supporting_collateral/taxTables/badTaxTable.csv
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
country,state,city,code,taxRate
|
||||
USA,,,,0.0,
|
||||
USA,Wisconsin,,,5.0
|
||||
USA,Wisconsin,Madison,53701,0.5
|
||||
|
9
t/supporting_collateral/taxTables/commentedTaxTable.csv
Normal file
9
t/supporting_collateral/taxTables/commentedTaxTable.csv
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
country,state,city,code,taxRate
|
||||
#header lines above
|
||||
|
||||
#This is just a country.
|
||||
USA,,,,0.0
|
||||
USA,Wisconsin,,,5.0 #Wisconsin is expensive
|
||||
|
||||
|
||||
USA,Wisconsin,Madison,53701,0.5
|
||||
|
3
t/supporting_collateral/taxTables/emptyTaxTable.csv
Normal file
3
t/supporting_collateral/taxTables/emptyTaxTable.csv
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
country,state,city,code,taxRate
|
||||
#state,Wisconsin,5.0
|
||||
#code,53701,0.5
|
||||
|
4
t/supporting_collateral/taxTables/goodTaxTable.csv
Normal file
4
t/supporting_collateral/taxTables/goodTaxTable.csv
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
country,state,city,code,taxRate
|
||||
USA,,,,0.0
|
||||
USA,Wisconsin,,,5.0
|
||||
USA,Wisconsin,Madison,53701,0.5
|
||||
|
1779
t/supporting_collateral/taxTables/largeTaxTable.csv
Normal file
1779
t/supporting_collateral/taxTables/largeTaxTable.csv
Normal file
File diff suppressed because it is too large
Load diff
4
t/supporting_collateral/taxTables/missingHeaders.csv
Normal file
4
t/supporting_collateral/taxTables/missingHeaders.csv
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
country,city,code,taxRate
|
||||
USA,,,,0.0
|
||||
USA,Wisconsin,,,5.0
|
||||
USA,Wisconsin,Madison,53701,0.5
|
||||
|
4
t/supporting_collateral/taxTables/orderedTaxTable.csv
Normal file
4
t/supporting_collateral/taxTables/orderedTaxTable.csv
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
taxRate,country,state,city,code
|
||||
0.0,USA,,,
|
||||
5.0,USA,Wisconsin,,
|
||||
0.5,USA,Wisconsin,Madison,53701
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue