Ready for 7.10.29 development.

This commit is contained in:
Colin Kuskie 2013-03-20 21:38:23 -07:00
commit c806f99b7b
4236 changed files with 1217679 additions and 0 deletions

View file

@ -0,0 +1,412 @@
# vim:syntax=perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#------------------------------------------------------------------
# Write a little about what this script tests.
#
#
use FindBin;
use strict;
use lib "$FindBin::Bin/../../lib";
use Test::More;
use Test::Deep;
use 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::ShipDriver::FlatRate');
my $storage;
my ($driver, $cart, $car, $key);
my $versionTag;
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,
},
groupToUse => {
fieldType => 'group',
label => ignore(),
hoverHelp => ignore(),
defaultValue => 7,
},
}
} ],
'Definition returns an array of hashrefs',
);
#######################################################################
#
# create
#
#######################################################################
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, 14, 'getEditForm: the form has 14 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 => 'webguiCsrfToken',
type => 'hidden',
},
{
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 => 'groupToUse',
type => 'option',
},
{
name => '__groupToUse_isIn',
type => 'hidden',
},
{
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
#
#######################################################################
my $driverId = $driver->getId;
$driver->delete;
my $count = $session->db->quickScalar('select count(*) from shipper where shipperId=?',[$driverId]);
is($count, 0, 'delete deleted the object');
undef $driver;
#######################################################################
#
# calculate
#
#######################################################################
$car = WebGUI::Asset->getImportNode($session)->addChild({
className => 'WebGUI::Asset::Sku::Product',
title => 'Automobiles',
isShippingRequired => 1,
});
my $crappyCar = $car->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => '1987 Ford Escort',
varSku => 'crappy-car',
price => 600,
weight => 1500,
quantity => 5,
}
);
my $goodCar = $car->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => '2004 Honda MPV minivan',
varSku => 'used van',
price => 15_000,
weight => 2000,
quantity => 15,
}
);
my $reallyNiceCar = $car->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Cadillac XLR-V',
varSku => 'nice-car',
price => 90_000,
weight => 3000,
quantity => 3,
}
);
$versionTag = WebGUI::VersionTag->getWorking($session);
$versionTag->commit;
$options = {
label => 'flat rate, ship weight',
enabled => 1,
flatFee => 1.00,
percentageOfPrice => 0,
pricePerWeight => 100,
pricePerItem => 10,
};
$driver = WebGUI::Shop::ShipDriver::FlatRate->create($session, $options);
$cart = WebGUI::Shop::Cart->newBySession($session);
$car->addToCart($car->getCollateral('variantsJSON', 'variantId', $crappyCar));
is($driver->calculate($cart), 1511, 'calculate by weight, perItem and flat fee work');
$car->addToCart($car->getCollateral('variantsJSON', 'variantId', $reallyNiceCar));
is($driver->calculate($cart), 4521, 'calculate by weight, perItem and flat fee work for two items');
$options = {
label => 'percentage of price',
enabled => 1,
flatFee => 0.00,
percentageOfPrice => 1/3*100,
pricePerWeight => 0,
pricePerItem => 0,
};
$driver->update($options);
is($driver->calculate($cart), 30_200, 'calculate by percentage of price');
$cart->empty();
$driver->update({
label => 'flat fee for shipsSeparately test',
enabled => 1,
flatFee => 1,
percentageOfPrice => 0,
pricePerWeight => 0,
pricePerItem => 0,
});
$key = WebGUI::Asset->getImportNode($session)->addChild({
className => 'WebGUI::Asset::Sku::Product',
title => 'Key',
isShippingRequired => 1,
shipsSeparately => 1,
});
my $metalKey = $key->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'metal key',
varSku => 'metal-key',
price => 1.00,
weight => 1.00,
quantity => 1e9,
}
);
my $bioKey = $key->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'biometric key',
varSku => 'bio-key',
price => 5.00,
weight => 1.00,
quantity => 1e9,
}
);
my $boughtCar = $car->addToCart($car->getCollateral('variantsJSON', 'variantId', $reallyNiceCar));
my $firstKey = $key->addToCart($key->getCollateral('variantsJSON', 'variantId', $metalKey));
is($driver->calculate($cart), 2, 'shipsSeparately: returns two, one for ships separately, one for ships bundled');
$boughtCar->adjustQuantity();
is($driver->calculate($cart), 2, '... returns two, one for ships separately, one for ships bundled, even for two items');
$firstKey->adjustQuantity();
is($driver->calculate($cart), 3, '... returns three, two for ships separately, one for ships bundled, even for two items');
$key->update({shipsSeparately => 0});
is($driver->calculate($cart), 1, '... returns one, since all can be bundled together now');
$car->update({shipsSeparately => 1});
$key->update({shipsSeparately => 1});
is($driver->calculate($cart), 4, '... returns four, since all must be shipped separately now');
}
#----------------------------------------------------------------------------
# Cleanup
END {
if (defined $driver && ref $driver eq 'WebGUI::Shop::ShipDriver::FlatRate') {
$driver->delete;
}
if (defined $cart && ref $cart eq 'WebGUI::Shop::Cart') {
$cart->delete;
}
if (defined $car && (ref($car) eq 'WebGUI::Asset::Sku::Product')) {
$car->purge;
}
if (defined $key && (ref($key) eq 'WebGUI::Asset::Sku::Product')) {
$key->purge;
}
if (defined $versionTag) {
$versionTag->rollback;
}
}

539
t/Shop/ShipDriver/UPS.t Normal file
View file

@ -0,0 +1,539 @@
# vim:syntax=perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#------------------------------------------------------------------
# Write a little about what this script tests.
#
#
use FindBin;
use strict;
use lib "$FindBin::Bin/../../lib";
use Test::More;
use Test::Deep;
use XML::Simple;
use Data::Dumper;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
use WebGUI::Shop::ShipDriver::UPS;
use Locales;
#my $locales = Locales->new('en');
#diag Dumper [ $locales->get_territory_names() ];
#diag $locales->get_code_from_territory('United States');
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
my $user = WebGUI::User->create($session);
WebGUI::Test->addToCleanup($user);
$session->user({user => $user});
#----------------------------------------------------------------------------
# Tests
plan tests => 41;
#----------------------------------------------------------------------------
# put your tests here
my $storage;
my ($driver);
my $versionTag = WebGUI::VersionTag->getWorking($session);
my $home = WebGUI::Asset->getDefault($session);
my $rockHammer = $home->addChild({
className => 'WebGUI::Asset::Sku::Product',
isShippingRequired => 1, title => 'Rock Hammers',
shipsSeparately => 0,
});
my $smallHammer = $rockHammer->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Small rock hammer', price => 7.50,
varSku => 'small-hammer', weight => 1.5,
quantity => 9999,
}
);
my $bigHammer = $rockHammer->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Big rock hammer', price => 19.99,
varSku => 'big-hammer', weight => 12,
quantity => 9999,
}
);
my $bible = $home->addChild({
className => 'WebGUI::Asset::Sku::Product',
isShippingRequired => 1, title => 'Bibles, individuall wrapped and shipped',
shipsSeparately => 1,
});
my $kjvBible = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'King James Bible', price => 17.50,
varSku => 'kjv-bible', weight => 2.5,
quantity => 99999,
}
);
my $nivBible = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'NIV Bible', price => 22.50,
varSku => 'niv-bible', weight => 2.0,
quantity => 999999,
}
);
my $feather = $home->addChild({
className => 'WebGUI::Asset::Sku::Product',
isShippingRequired => 1, title => 'Feathers',
shipsSeparately => 0,
});
my $blueFeather = $feather->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'blue feather', price => 1.00,
varSku => 'blue', weight => 0.001,
quantity => 999999,
}
);
$versionTag->commit;
addToCleanup($versionTag);
#######################################################################
#
# definition
#
#######################################################################
my $definition;
my $e; ##Exception variable, used throughout the file
eval { $definition = WebGUI::Shop::ShipDriver::UPS->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',
),
'... checking error message',
);
isa_ok(
$definition = WebGUI::Shop::ShipDriver::UPS->definition($session),
'ARRAY'
);
#######################################################################
#
# create
#
#######################################################################
my $options = {
label => 'UPS Driver',
enabled => 1,
};
$driver = WebGUI::Shop::ShipDriver::UPS->create($session, $options);
isa_ok($driver, 'WebGUI::Shop::ShipDriver::UPS');
isa_ok($driver, 'WebGUI::Shop::ShipDriver');
#######################################################################
#
# getName
#
#######################################################################
is (WebGUI::Shop::ShipDriver::UPS->getName($session), 'UPS', 'getName returns the human readable name of this driver');
#######################################################################
#
# delete
#
#######################################################################
my $driverId = $driver->getId;
$driver->delete;
my $count = $session->db->quickScalar('select count(*) from shipper where shipperId=?',[$driverId]);
is($count, 0, 'delete deleted the object');
undef $driver;
#######################################################################
#
# calculate, and private methods.
#
#######################################################################
$driver = WebGUI::Shop::ShipDriver::UPS->create($session, {
label => 'Shipping from Shawshank',
enabled => 1,
shipType => 'PARCEL',
});
addToCleanup($driver);
eval { $driver->calculate() };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no zipcode has been set');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a source zipcode.',
),
'... checking error message',
);
my $properties = $driver->get();
$properties->{sourceZip} = '97123';
$driver->update($properties);
eval { $driver->calculate() };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no source country');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a source country.',
),
'... checking error message',
);
$properties = $driver->get();
$properties->{sourceCountry} = 'United States';
$driver->update($properties);
eval { $driver->calculate() };
$e = WebGUI::Error->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no userId');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a UPS userId.',
),
'... checking error message',
);
$properties = $driver->get();
$properties->{userId} = 'Me';
$driver->update($properties);
eval { $driver->calculate() };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no password');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a UPS password.',
),
'... checking error message',
);
$properties = $driver->get();
$properties->{password} = 'knock knock';
$driver->update($properties);
eval { $driver->calculate() };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no license number');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a UPS license number.',
),
'... checking error message',
);
my $cart = WebGUI::Shop::Cart->newBySession($session);
addToCleanup($cart);
my $addressBook = $cart->getAddressBook;
my $workAddress = $addressBook->addAddress({
label => 'work',
organization => 'Plain Black Corporation',
address1 => '1360 Regent St. #145',
city => 'Madison', state => 'WI', code => '53715',
country => 'United States',
});
my $wucAddress = $addressBook->addAddress({
label => 'wuc',
organization => 'Madison Concourse Hotel',
address1 => '1 W Dayton St',
city => 'Madison', state => 'WI', code => '53703',
country => 'United States',
});
$cart->update({shippingAddressId => $workAddress->getId});
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[(), ],
'_getShippableUnits: empty cart'
);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[ [ [ ignore() ], ], ],
'_getShippableUnits: one loose item in the cart'
);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
cmp_bag(
[$driver->_getShippableUnits($cart)],
[ [ [ ignore(), ignore() ], ], ],
'_getShippableUnits: two loose items in the cart'
);
$bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $kjvBible));
cmp_bag(
[$driver->_getShippableUnits($cart)],
[ bag( [ ignore(), ignore() ], [ ignore() ], ), ],
'_getShippableUnits: two loose items, and 1 ships separately item in the cart'
);
my $bibleItem = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
$bibleItem->setQuantity(3);
cmp_bag(
[$driver->_getShippableUnits($cart)],
[ bag( [ ignore(), ignore() ], [ ignore() ], [ ignore() ], [ ignore() ], [ ignore() ] ) ],
'_getShippableUnits: two loose items, and 4 ships separately item in the cart, due to quantity'
);
my $rockHammer2 = $bible->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
$rockHammer2->update({shippingAddressId => $wucAddress->getId});
cmp_bag(
[$driver->_getShippableUnits($cart)],
[
bag( [ ignore(), ignore() ], [ ignore() ], [ ignore() ], [ ignore() ], [ ignore() ] ),
[ [ ignore() ], ],
],
'_getShippableUnits: two loose items, and 4 ships separately item in the cart, and another loose item sorted by zipcode'
);
$cart->empty;
$bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[ [ ignore() ], ],
'_getShippableUnits: only 1 ships separately item in the cart'
);
$cart->empty;
my $userId = $session->config->get('testing/UPS_userId');
my $hasUPSCredentials = 1;
##If there isn't a userId, set a fake one for XML testing.
if (! $userId) {
$hasUPSCredentials = 0;
$userId = "blahBlahBlah";
}
my $password = $session->config->get('testing/UPS_password');
##If there isn't a password, set a fake one for XML testing.
if (! $password) {
$hasUPSCredentials = 0;
$password = "nyaahNyaah";
}
my $license = $session->config->get('testing/UPS_licenseNo');
##If there isn't a license, set a fake one for XML testing.
if (! $license) {
$hasUPSCredentials = 0;
$license = "bogey";
}
$properties = $driver->get();
$properties->{userId} = $userId;
$properties->{password} = $password;
$properties->{licenseNo} = $license;
$properties->{sourceZip} = '97123';
$properties->{sourceCountry} = 'United States';
$properties->{shipService} = '03';
$properties->{pickupType} = '01';
$properties->{customerClassification} = '04';
$properties->{residentialIndicator} = 'residential';
$driver->update($properties);
$driver->testMode(1);
my $rockItem = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
my @shippableUnits = $driver->_getShippableUnits($cart);
##Must look them up one zip at a time
my $xml = $driver->buildXML($cart, $shippableUnits[0]);
like($xml, qr/^<.xml version='1.0'.+?<.xml version=/ms, 'buildXML: has two xml declarations');
like($xml, qr/<AccessRequest xml:lang/, '... xml:lang is an attribute of AccessRequest');
#diag $xml;
my ($xmlA, $xmlR) = split /\n(?=<\?xml)/, $xml;
my $xmlAcc = XMLin($xmlA,
KeepRoot => 1,
);
cmp_deeply(
$xmlAcc,
{
AccessRequest => {
Password => $password,
UserId => $userId,
'xml:lang' => 'en-US',
AccessLicenseNumber => $license,
},
},
'... correct access request data structure for 1 package'
);
my $xmlRate = XMLin($xmlR,
KeepRoot => 1,
);
cmp_deeply(
$xmlRate, {
RatingServiceSelectionRequest => {
'xml:lang' => 'en-US',
PickupType => { Code => '01', },
CustomerClassification => { Code => '04', },
Request => { RequestAction => 'Rate', },
Shipment => {
Shipper => {
Address => { PostalCode => 97123, CountryCode => 'us', },
},
ShipTo => {
Address => { PostalCode => 53715, CountryCode => 'us', ResidentialAddressIndicator => {}, },
},
Service => { Code => '03', },
Package => {
PackagingType => { Code => '02', },
PackageWeight => { Weight => '1.5', },
},
},
}
},
'... correct access rating request structure for 1 package'
);
SKIP: {
skip 'No UPS credentials for testing', 3 unless $hasUPSCredentials;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to UPS successful for 1 package');
#diag $response->content;
my $xmlData = XMLin($response->content, ForceArray => [qw/RatedPackage/],);
ok($xmlData->{Response}->{ResponseStatusCode}, '... responseCode is successful');
ok($xmlData->{RatedShipment}->{TotalCharges}->{MonetaryValue}, '... total charges returned');
#diag($xmlData->{RatedShipment}->{TotalCharges}->{MonetaryValue});
}
$rockItem->setQuantity(2);
@shippableUnits = $driver->_getShippableUnits($cart);
$xml = $driver->buildXML($cart, $shippableUnits[0]);
SKIP: {
skip 'No UPS credentials for testing', 3 unless $hasUPSCredentials;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to UPS successful for 1 item, quantity=2');
#diag $response->content;
my $xmlData = XMLin($response->content, ForceArray => [qw/RatedPackage/],);
ok($xmlData->{Response}->{ResponseStatusCode}, '... responseCode is successful');
ok($xmlData->{RatedShipment}->{TotalCharges}->{MonetaryValue}, '... total charges returned');
#diag($xmlData->{RatedShipment}->{TotalCharges}->{MonetaryValue});
}
TODO: {
local $TODO = 'single item shipping cost calculation';
ok(0, 'call _calculateFromXML with arranged data');
}
$bibleItem = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
@shippableUnits = $driver->_getShippableUnits($cart);
$xml = $driver->buildXML($cart, @shippableUnits);
($xmlA, $xmlR) = split /\n(?=<\?xml)/, $xml;
#diag $xmlR;
$xmlRate = XMLin( $xmlR,
KeepRoot => 1,
ForceArray => ['Package'],
);
#diag Dumper $xmlRate;
cmp_deeply(
$xmlRate, {
RatingServiceSelectionRequest => {
'xml:lang' => 'en-US',
PickupType => { Code => '01', },
CustomerClassification => { Code => '04', },
Request => { RequestAction => 'Rate', },
Shipment => {
Shipper => {
Address => { PostalCode => 97123, CountryCode => 'us', },
},
ShipTo => {
Address => { PostalCode => 53715, CountryCode => 'us', ResidentialAddressIndicator => {}, },
},
Service => { Code => '03', },
Package => bag(
{
PackagingType => { Code => '02', },
PackageWeight => { Weight => '3.0', },
},
{
PackagingType => { Code => '02', },
PackageWeight => { Weight => '2.0', },
},
),
},
}
},
'... correct access rating request structure for two packages in cart'
);
SKIP: {
skip 'No UPS credentials for testing', 3 unless $hasUPSCredentials;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to UPS successful for two package in 1 request');
my $xmlData = XMLin($response->content, ForceArray => [qw/RatedPackage/],);
ok($xmlData->{Response}->{ResponseStatusCode}, '... responseCode is successful');
ok($xmlData->{RatedShipment}->{TotalCharges}->{MonetaryValue}, '... total charges returned');
}
ok($driver->getEditForm(), 'getEditForm');
$cart->empty;
$feather->addToCart($feather->getCollateral('variantsJSON', 'variantId', $blueFeather));
$xml = $driver->buildXML($cart, $driver->_getShippableUnits($cart));
($xmlA, $xmlR) = split /\n(?=<\?xml)/, $xml;
$xmlRate = XMLin( $xmlR,
KeepRoot => 1,
ForceArray => ['Package'],
);
is (
$xmlRate->{RatingServiceSelectionRequest}->{Shipment}->{Package}->[0]->{PackageWeight}->{Weight},
'0.1',
'Weight is clipped at 0.1 pounds.'
);
$cart->empty;

972
t/Shop/ShipDriver/USPS.t Normal file
View file

@ -0,0 +1,972 @@
# vim:syntax=perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#------------------------------------------------------------------
# Write a little about what this script tests.
#
#
use FindBin;
use strict;
use lib "$FindBin::Bin/../../lib";
use Test::More;
use Test::Deep;
use XML::Simple;
use Data::Dumper;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
use WebGUI::Shop::ShipDriver::USPS;
plan tests => 69;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
my $user = WebGUI::User->create($session);
WebGUI::Test->addToCleanup($user);
$session->user({user => $user});
#----------------------------------------------------------------------------
# Tests
#----------------------------------------------------------------------------
# put your tests here
my ($driver2, $cart);
my $insuranceTable = <<EOTABLE;
5:1.00
10:2.00
15:3.00
20:4.00
25:5.00
30:6.00
EOTABLE
my $versionTag = WebGUI::VersionTag->getWorking($session);
my $home = WebGUI::Asset->getDefault($session);
my $rockHammer = $home->addChild({
className => 'WebGUI::Asset::Sku::Product',
isShippingRequired => 1, title => 'Rock Hammers',
shipsSeparately => 0,
});
my $smallHammer = $rockHammer->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Small rock hammer', price => 7.50,
varSku => 'small-hammer', weight => 1.5,
quantity => 9999,
}
);
my $bigHammer = $rockHammer->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Big rock hammer', price => 19.99,
varSku => 'big-hammer', weight => 12,
quantity => 9999,
}
);
my $bible = $home->addChild({
className => 'WebGUI::Asset::Sku::Product',
isShippingRequired => 1, title => 'Bibles, individuall wrapped and shipped',
shipsSeparately => 1,
});
my $kjvBible = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'King James Bible', price => 17.50,
varSku => 'kjv-bible', weight => 2.5,
quantity => 99999,
}
);
my $nivBible = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'NIV Bible', price => 22.50,
varSku => 'niv-bible', weight => 2.0,
quantity => 999999,
}
);
my $gospels = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Gospels from the new Testament',
price => 1.50, varSku => 'gospels',
weight => 2.0, quantity => 999999,
}
);
$versionTag->commit;
addToCleanup($versionTag);
#######################################################################
#
# definition
#
#######################################################################
my $definition;
my $e; ##Exception variable, used throughout the file
eval { $definition = WebGUI::Shop::ShipDriver::USPS->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',
),
'... checking error message',
);
isa_ok(
$definition = WebGUI::Shop::ShipDriver::USPS->definition($session),
'ARRAY'
);
#######################################################################
#
# create
#
#######################################################################
my $options = {
label => 'USPS Driver',
enabled => 1,
};
$driver2 = WebGUI::Shop::ShipDriver::USPS->create($session, $options);
addToCleanup($driver2);
isa_ok($driver2, 'WebGUI::Shop::ShipDriver::USPS');
isa_ok($driver2, 'WebGUI::Shop::ShipDriver');
#######################################################################
#
# getName
#
#######################################################################
is (WebGUI::Shop::ShipDriver::USPS->getName($session), 'U.S. Postal Service', 'getName returns the human readable name of this driver');
#######################################################################
#
# delete
#
#######################################################################
my $driverId = $driver2->getId;
$driver2->delete;
my $count = $session->db->quickScalar('select count(*) from shipper where shipperId=?',[$driverId]);
is($count, 0, 'delete deleted the object');
undef $driver2;
#######################################################################
#
# calculate, and private methods.
#
#######################################################################
my $driver = WebGUI::Shop::ShipDriver::USPS->create($session, {
label => 'Shipping from Shawshank',
enabled => 1,
shipType => 'PARCEL',
});
addToCleanup($driver);
eval { $driver->calculate() };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no zipcode has been set');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a source zipcode.',
),
'... checking error message',
);
my $properties = $driver->get();
$properties->{sourceZip} = '97123';
$driver->update($properties);
eval { $driver->calculate() };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no userId');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a USPS userId.',
),
'... checking error message',
);
$cart = WebGUI::Shop::Cart->newBySession($session);
addToCleanup($cart);
my $addressBook = $cart->getAddressBook;
my $workAddress = $addressBook->addAddress({
label => 'work',
organization => 'Plain Black Corporation',
address1 => '1360 Regent St. #145',
city => 'Madison', state => 'WI', code => '53715',
country => 'United States',
});
my $wucAddress = $addressBook->addAddress({
label => 'wuc',
organization => 'Madison Concourse Hotel',
address1 => '1 W Dayton St',
city => 'Madison', state => 'WI', code => '53703',
country => 'United States',
});
my $zip4Address = $addressBook->addAddress({
label => 'work-zip4',
organization => 'Plain Black Corporation',
address1 => '1360 Regent St. #145',
city => 'Madison', state => 'WI', code => '53715-1255',
country => 'United States',
});
$cart->update({shippingAddressId => $workAddress->getId});
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[(), ],
'_getShippableUnits: empty cart'
);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[[ ignore() ], ],
'_getShippableUnits: one loose item in the cart'
);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], ],
'_getShippableUnits: two loose items in the cart'
);
$bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $kjvBible));
cmp_bag(
[$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], [ ignore(), ], ],
'_getShippableUnits: two loose items, and 1 ships separately item in the cart'
);
my $bibleItem = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
$bibleItem->setQuantity(5);
cmp_bag(
[$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], [ ignore() ], [ ignore() ], ],
'_getShippableUnits: two loose items, and 2 ships separately item in the cart, regarless of quantity for the new item'
);
my $rockHammer2 = $bible->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
$rockHammer2->update({shippingAddressId => $wucAddress->getId});
cmp_bag(
[$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], [ ignore() ], [ ignore() ], [ ignore() ], ],
'_getShippableUnits: two loose items, and 2 ships separately item in the cart, and another loose item sorted by zipcode'
);
$cart->empty;
$bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[ [ ignore() ], ],
'_getShippableUnits: only 1 ships separately item in the cart'
);
$cart->empty;
my $userId = $session->config->get('testing/USPS_userId');
my $hasRealUserId = 1;
##If there isn't a userId, set a fake one for XML testing.
if (! $userId) {
$hasRealUserId = 0;
$userId = "blahBlahBlah";
}
$properties = $driver->get();
$properties->{userId} = $userId;
$properties->{sourceZip} = '97123';
$driver->update($properties);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
my @shippableUnits = $driver->_getShippableUnits($cart);
$properties = $driver->get();
$properties->{addInsurance} = 1;
$properties->{insuranceRates} = $insuranceTable;
$driver->update($properties);
is($driver->_calculateInsurance(@shippableUnits), 2, '_calculateInsurance: one item in cart with quantity=1, calculates insurance');
$properties->{addInsurance} = 0;
$driver->update($properties);
is($driver->_calculateInsurance(@shippableUnits), 0, '_calculateInsurance: returns 0 if insurance is not enabled');
$properties->{addInsurance} = 1;
$properties->{insuranceRates} = '';
$driver->update($properties);
is($driver->_calculateInsurance(@shippableUnits), 0, '_calculateInsurance: returns 0 if rates are not set');
my $xml = $driver->buildXML($cart, @shippableUnits);
like($xml, qr/<RateV3Request USERID="[^"]+"/, 'buildXML: checking userId is an attribute of the RateV3Request tag');
like($xml, qr/<Package ID="0"/, 'buildXML: checking ID is an attribute of the Package tag');
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'PARCEL',
Machinable => 'true',
},
],
}
},
'buildXML: PARCEL service, 1 item in cart'
);
like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order');
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => ignore(),
Pounds => ignore(), Size => ignore(),
Zone => ignore(), Container => {},
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format. If this test fails, the driver may need to be updated'
);
}
my $cost = $driver->_calculateFromXML(
{
RateV3Response => {
Package => [
{
ID => 0,
Postage => {
Rate => 5.25,
},
},
],
},
},
@shippableUnits
);
is($cost, 5.25, '_calculateFromXML calculates shipping cost correctly for 1 item in the cart');
$bibleItem = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
@shippableUnits = $driver->_getShippableUnits($cart);
is(calculateInsurance($driver), 7, '_calculateInsurance: two items in cart with quantity=1, calculates insurance');
$xml = $driver->buildXML($cart, @shippableUnits);
$xmlData = XMLin( $xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '2', Ounces => '0.0',
Size => 'REGULAR', Service => 'PARCEL',
Machinable => 'true',
},
{
ID => 1,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'PARCEL',
Machinable => 'true',
},
],
}
},
'Validate XML structure and content for 2 items in the cart'
);
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful for 2 items in cart');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => '0.0',
Pounds => 2, Size => ignore(),
Zone => ignore(), Container => {},
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
{
ID => 1,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => '8.0',
Pounds => 1, Size => ignore(),
Zone => ignore(), Container => {},
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format for 2 items in cart. If this test fails, the driver may need to be updated'
);
}
$cost = $driver->_calculateFromXML(
{
RateV3Response => {
Package => [
{
ID => 0,
Postage => {
Rate => 7.00,
},
},
{
ID => 1,
Postage => {
Rate => 5.25,
},
},
],
},
},
@shippableUnits
);
is($cost, 12.25, '_calculateFromXML calculates shipping cost correctly for 2 items in the cart');
$bibleItem->setQuantity(2);
@shippableUnits = $driver->_getShippableUnits($cart);
is(calculateInsurance($driver), 8, '_calculateInsurance: two items in cart with quantity=2, calculates insurance');
$cost = $driver->_calculateFromXML(
{
RateV3Response => {
Package => [
{
ID => 0,
Postage => {
Rate => 7.00,
},
},
{
ID => 1,
Postage => {
Rate => 5.25,
},
},
],
},
},
@shippableUnits
);
is($cost, 19.25, '_calculateFromXML calculates shipping cost correctly for 2 items in the cart, with quantity of 2');
$rockHammer2 = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
$rockHammer2->update({shippingAddressId => $wucAddress->getId});
@shippableUnits = $driver->_getShippableUnits($cart);
is(calculateInsurance($driver), 12, '_calculateInsurance: calculates insurance');
$xml = $driver->buildXML($cart, @shippableUnits);
$xmlData = XMLin( $xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '2', Ounces => '0.0',
Size => 'REGULAR', Service => 'PARCEL',
Machinable => 'true',
},
{
ID => 1,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'PARCEL',
Machinable => 'true',
},
{
ID => 2,
ZipDestination => '53703', ZipOrigination => '97123',
Pounds => '12', Ounces => '0.0',
Size => 'REGULAR', Service => 'PARCEL',
Machinable => 'true',
},
],
}
},
'Validate XML structure and content for 3 items in the cart, 3 shippable items'
);
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful for 3 items in cart');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => '0.0',
Pounds => 2, Size => ignore(),
Zone => ignore(), Container => {},
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
{
ID => 1,
ZipOrigination => ignore(), ZipDestination => ignore(),
Machinable => ignore(), Ounces => '8.0',
Pounds => 1, Size => ignore(),
Zone => ignore(), Container => {},
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
{
ID => 2,
ZipOrigination => ignore(), ZipDestination => 53703,
Machinable => ignore(), Ounces => '0.0',
Pounds => 12, Size => ignore(),
Zone => ignore(), Container => {},
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(20,20), ##A number around 20...
}
},
],
},
'... returned data from USPS in correct format for 3 items in cart. If this test fails, the driver may need to be updated'
);
}
my $xmlData = XMLin(q{<?xml version="1.0"?>
<RateV3Response><Package ID="0"><ZipOrigination>97123</ZipOrigination><ZipDestination>53715</ZipDestination><Pounds>2</Pounds><Ounces>0.0</Ounces><Size>REGULAR</Size><Machinable>TRUE</Machinable><Zone>7</Zone><Postage CLASSID="4"><MailService>Parcel Post</MailService><Rate>7.62</Rate></Postage></Package><Package ID="1"><ZipOrigination>97123</ZipOrigination><ZipDestination>53715</ZipDestination><Pounds>1</Pounds><Ounces>8.0</Ounces><Size>REGULAR</Size><Machinable>TRUE</Machinable><Zone>7</Zone><Postage CLASSID="4"><MailService>Parcel Post</MailService><Rate>7.62</Rate></Postage></Package><Package ID="2"><ZipOrigination>97123</ZipOrigination><ZipDestination>53703</ZipDestination><Pounds>12</Pounds><Ounces>0.0</Ounces><Size>REGULAR</Size><Machinable>TRUE</Machinable><Zone>7</Zone><Postage CLASSID="4"><MailService>Parcel Post</MailService><Rate>16.67</Rate></Postage></Package></RateV3Response>
}, KeepRoot => 1, ForceArray => [qw/Package/],);
my $cost = $driver->_calculateFromXML($xmlData, @shippableUnits);
is $cost, "39.53", 'calculating shipping cost for separate shipping addreses in 1 transaction';
#######################################################################
#
# Test Priority shipping setup
#
#######################################################################
$cart->empty;
$properties = $driver->get();
$properties->{shipType} = 'PRIORITY';
$driver->update($properties);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
@shippableUnits = $driver->_getShippableUnits($cart);
$xml = $driver->buildXML($cart, @shippableUnits);
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'PRIORITY',
Machinable => 'true', Container => 'FLAT RATE BOX',
},
],
}
},
'buildXML: PRIORITY service, 1 item in cart'
);
like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Container.+?Size.+?Machinable/, '... and tag order');
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
ZipOrigination => ignore(), ZipDestination => ignore(),
Container => ignore(), Ounces => ignore(), ##Machinable missing, added Container
Pounds => ignore(), Size => ignore(),
Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(10,10), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format. If this test fails, the driver may need to be updated'
);
}
#######################################################################
#
# Test EXPRESS shipping setup
#
#######################################################################
$properties = $driver->get();
$properties->{shipType} = 'EXPRESS';
$driver->update($properties);
$xml = $driver->buildXML($cart, @shippableUnits);
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'EXPRESS',
Machinable => 'true',
},
],
}
},
'buildXML: EXPRESS service, 1 item in cart'
);
like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order');
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '... _doXmlRequest to USPS successful');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0, Container => {},
ZipOrigination => ignore(), ZipDestination => ignore(),
Ounces => ignore(), Pounds => ignore(),
Size => ignore(), Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(30,30), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format. If this test fails, the driver may need to be updated'
);
}
#######################################################################
#
# Test PRIORITY VARIABLE shipping setup
#
#######################################################################
$properties = $driver->get();
$properties->{shipType} = 'PRIORITY VARIABLE';
$driver->update($properties);
$xml = $driver->buildXML($cart, @shippableUnits);
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'PRIORITY',
Machinable => 'true',# Container => 'VARIABLE',
},
],
}
},
'buildXML: PRIORITY, VARIABLE service, 1 item in cart'
);
like($xml, qr/RateV3Request USERID.+?Package ID=.+?Service.+?ZipOrigination.+?ZipDestination.+?Pounds.+?Ounces.+?Size.+?Machinable/, '... and tag order');
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '... _doXmlRequest to USPS successful');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0, Container => {},
ZipOrigination => ignore(), ZipDestination => ignore(),
Ounces => ignore(), Pounds => ignore(),
Size => ignore(), Zone => ignore(),
Postage => {
CLASSID => ignore(),
MailService => ignore(),
Rate => num(8,8), ##A number around 10...
}
},
],
},
'... returned data from USPS in correct format. If this test fails, the driver may need to be updated'
);
}
#######################################################################
#
# Test ZIP+4 format domestic code
#
#######################################################################
$cart->update({shippingAddressId => $zip4Address->getId});
my $xmlData = XMLin($driver->buildXML($cart, @shippableUnits),
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
RateV3Request => {
USERID => $userId,
Package => [
{
ID => 0,
ZipDestination => '53715', ZipOrigination => '97123',
Pounds => '1', Ounces => '8.0',
Size => 'REGULAR', Service => 'PRIORITY',
Machinable => 'true',# Container => 'VARIABLE',
},
],
}
},
'buildXML: removed plus4 part of zipcode'
);
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $cost = eval { $driver->calculate($cart); };
my $e = Exception::Class->caught();
ok( ! ref $e, 'no exception thrown for zip+4 address');
cmp_deeply($cost, num(10,9.99), 'zip+4 address returns a valid cost');
}
$cart->update({shippingAddressId => $workAddress->getId});
#######################################################################
#
# Check for throwing an exception
#
#######################################################################
my $userId = $driver->get('userId');
$properties = $driver->get();
$properties->{userId} = '__NEVER_GOING_TO_HAPPEN__';
$driver->update($properties);
$cost = eval { $driver->calculate($cart); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::Shop::RemoteShippingRate', 'calculate throws an exception when a bad userId is used');
$properties->{userId} = $userId;
$driver->update($properties);
my $dutchAddress = $addressBook->addAddress({
label => 'dutch',
address1 => 'Rotterdamseweg 183C',
city => 'Delft', code => '2629HD',
country => 'Netherlands',
});
$cart->update({shippingAddressId => $dutchAddress->getId});
$cost = eval { $driver->calculate($cart); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', "calculate won't calculate for foreign countries");
$cart->update({shippingAddressId => $workAddress->getId});
#<?xml version="1.0"?>
#<RateV3Response><Package ID="0"><Error><Number>-2147219500</Number>
#<Source>DomesticRatesV3;clsRateV3.ValidateWeight;RateEngineV3.ProcessRequest</Source>
#<Description>Please enter the package weight. </Description>
#<HelpFile></HelpFile><HelpContext>1000440</HelpContext></Error></Package></RateV3Response>
#######################################################################
#
# _calculateInsurance edge case
#
#######################################################################
$cart->empty;
$bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $gospels));
@shippableUnits = $driver->_getShippableUnits($cart);
is(calculateInsurance($driver), 1, '_calculateInsurance: calculates insurance using the first bin');
#######################################################################
#
# _parseInsuranceRates
#
#######################################################################
my @rates;
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("");
cmp_deeply(\@rates, [], '_parseInsuranceRates: empty string returns empty array');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates();
cmp_deeply(\@rates, [], '_parseInsuranceRates: undef returns empty array');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2");
cmp_deeply(\@rates, [], '... bad rates #1');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates(":2");
cmp_deeply(\@rates, [], '... bad rates #2');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("a:b");
cmp_deeply(\@rates, [], '... bad rates #3');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2:2");
cmp_deeply(\@rates, [ ['2', '2'] ], '... one line of good rates');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2.0:2.0");
cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with decimal points');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2.0:2.0\n");
cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with newline');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2.0:2.0\r\n");
cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with cr/newline');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates("2.0 : 2.0\r\n");
cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with cr/newline and spaces');
@rates = WebGUI::Shop::ShipDriver::USPS::_parseInsuranceRates(" 2.0 : 2.0 \r\n");
cmp_deeply(\@rates, [ ['2.0', '2.0'] ], '... one line of good rates with cr/newline and more spaces');
#----------------------------------------------------------------------------
# Cleanup
sub calculateInsurance {
my $driver = shift;
my $properties = $driver->get();
$properties->{addInsurance} = 1;
$properties->{insuranceRates} = $insuranceTable;
$driver->update($properties);
my $insurance = $driver->_calculateInsurance(@shippableUnits);
$properties->{addInsurance} = 0;
$driver->update($properties);
return $insurance;
}

View file

@ -0,0 +1,756 @@
# vim:syntax=perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#------------------------------------------------------------------
# Write a little about what this script tests.
#
#
use FindBin;
use strict;
use lib "$FindBin::Bin/../../lib";
use Test::More;
use Test::Deep;
use XML::Simple;
use Data::Dumper;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
use WebGUI::Shop::ShipDriver::USPSInternational;
plan tests => 40;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
my $user = WebGUI::User->create($session);
WebGUI::Test->addToCleanup($user);
$session->user({user => $user});
#----------------------------------------------------------------------------
# Tests
#----------------------------------------------------------------------------
# put your tests here
my ($driver2, $cart);
my $versionTag = WebGUI::VersionTag->getWorking($session);
my $home = WebGUI::Asset->getDefault($session);
my $rockHammer = $home->addChild({
className => 'WebGUI::Asset::Sku::Product',
isShippingRequired => 1, title => 'Rock Hammers',
shipsSeparately => 0,
});
my $smallHammer = $rockHammer->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Small rock hammer', price => 7.50,
varSku => 'small-hammer', weight => 1.5,
quantity => 9999,
}
);
my $bigHammer = $rockHammer->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Big rock hammer', price => 19.99,
varSku => 'big-hammer', weight => 12,
quantity => 9999,
}
);
my $bible = $home->addChild({
className => 'WebGUI::Asset::Sku::Product',
isShippingRequired => 1, title => 'Bibles, individuall wrapped and shipped',
shipsSeparately => 1,
});
my $kjvBible = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'King James Bible', price => 17.50,
varSku => 'kjv-bible', weight => 2.5,
quantity => 99999,
}
);
my $nivBible = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'NIV Bible', price => 22.50,
varSku => 'niv-bible', weight => 2.0,
quantity => 999999,
}
);
my $gospels = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Gospels from the new Testament',
price => 1.50, varSku => 'gospels',
weight => 2.0, quantity => 999999,
}
);
my $singlePage = $bible->setCollateral('variantsJSON', 'variantId', 'new',
{
shortdesc => 'Single page from bible',
price => 0.01, varSku => 'page',
weight => 0.0001, quantity => 999999,
}
);
$versionTag->commit;
addToCleanup($versionTag);
#######################################################################
#
# definition
#
#######################################################################
my $definition;
my $e; ##Exception variable, used throughout the file
eval { $definition = WebGUI::Shop::ShipDriver::USPSInternational->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',
),
'... checking error message',
);
isa_ok(
$definition = WebGUI::Shop::ShipDriver::USPSInternational->definition($session),
'ARRAY'
);
#######################################################################
#
# create
#
#######################################################################
my $options = {
label => 'Intl USPS Driver',
enabled => 1,
};
$driver2 = WebGUI::Shop::ShipDriver::USPSInternational->create($session, $options);
addToCleanup($driver2);
isa_ok($driver2, 'WebGUI::Shop::ShipDriver::USPSInternational');
isa_ok($driver2, 'WebGUI::Shop::ShipDriver');
#######################################################################
#
# getName
#
#######################################################################
is (WebGUI::Shop::ShipDriver::USPSInternational->getName($session), 'U.S. Postal Service, International', 'getName returns the human readable name of this driver');
#######################################################################
#
# delete
#
#######################################################################
my $driverId = $driver2->getId;
$driver2->delete;
my $count = $session->db->quickScalar('select count(*) from shipper where shipperId=?',[$driverId]);
is($count, 0, 'delete deleted the object');
undef $driver2;
#######################################################################
#
# calculate, and private methods.
#
#######################################################################
my $driver = WebGUI::Shop::ShipDriver::USPSInternational->create($session, {
label => 'Shipping from Shawshank',
enabled => 1,
});
addToCleanup($driver);
eval { $driver->calculate() };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'calculate throws an exception when no userId');
cmp_deeply(
$e,
methods(
error => 'Driver configured without a USPS userId.',
),
'... checking error message',
);
$cart = WebGUI::Shop::Cart->newBySession($session);
addToCleanup($cart);
my $addressBook = $cart->getAddressBook;
my $workAddress = $addressBook->addAddress({
label => 'work',
organization => 'ProcoliX',
address1 => 'Rotterdamseweg 183C',
city => 'Delft', code => '2629HD',
country => 'Netherlands',
});
my $sdhAddress = $addressBook->addAddress({
label => 'other side of planet',
organization => 'SDH',
country => 'Australia',
});
$cart->update({shippingAddressId => $workAddress->getId});
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[(), ],
'_getShippableUnits: empty cart'
);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[[ ignore() ], ],
'_getShippableUnits: one loose item in the cart'
);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], ],
'_getShippableUnits: two loose items in the cart'
);
$bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $kjvBible));
cmp_bag(
[$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], [ ignore(), ], ],
'_getShippableUnits: two loose items, and 1 ships separately item in the cart'
);
my $bibleItem = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
$bibleItem->setQuantity(5);
cmp_bag(
[$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], [ ignore() ], [ ignore() ], ],
'_getShippableUnits: two loose items, and 2 ships separately item in the cart, regarless of quantity for the new item'
);
my $rockHammer2 = $bible->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
$rockHammer2->update({shippingAddressId => $sdhAddress->getId});
cmp_bag(
[$driver->_getShippableUnits($cart)],
[[ ignore(), ignore() ], [ ignore() ], [ ignore() ], [ ignore() ], ],
'_getShippableUnits: two loose items, and 2 ships separately item in the cart, and another loose item sorted by zipcode'
);
$cart->empty;
$bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
cmp_deeply(
[$driver->_getShippableUnits($cart)],
[ [ ignore() ], ],
'_getShippableUnits: only 1 ships separately item in the cart'
);
$cart->empty;
my $userId = $session->config->get('testing/USPS_userId');
my $hasRealUserId = 1;
##If there isn't a userId, set a fake one for XML testing.
if (! $userId) {
$hasRealUserId = 0;
$userId = "blahBlahBlah";
}
my $properties = $driver->get();
$properties->{userId} = $userId;
$properties->{shipType} = '9';
$driver->update($properties);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $smallHammer));
my @shippableUnits = $driver->_getShippableUnits($cart);
my $xml = $driver->buildXML($cart, @shippableUnits);
like($xml, qr/<IntlRateRequest USERID="[^"]+"/, 'buildXML: checking userId is an attribute of the IntlRateRequest tag');
like($xml, qr/<Package ID="0"/, 'buildXML: checking ID is an attribute of the Package tag');
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
IntlRateRequest => {
USERID => $userId,
Package => [
{
ID => 0,
Pounds => '1', Ounces => '8.0',
Machinable => 'true', Country => 'Netherlands',
MailType => 'Package',
},
],
}
},
'buildXML: 1 item in cart'
);
like($xml, qr/IntlRateRequest USERID.+?Package ID=.+?Pounds.+?Ounces.+?Machinable.+?MailType.+?Country.+?/, '... and tag order');
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful');
my $xmlData = XMLin($response->content, ForceArray => [qw/Package/],);
cmp_deeply(
$xmlData,
{
Package => [
{
ID => 0,
AreasServed => ignore(), Prohibitions => ignore(),
ExpressMail => ignore(), CustomsForms => ignore(),
Observations => ignore(), Restrictions => ignore(),
Service => superbagof(
{
ID => ignore(),
MaxWeight => ignore(),
MaxDimensions => ignore(),
MailType => 'Package',
Ounces => '8',
Pounds => '1',
Country => 'NETHERLANDS',
Machinable => 'true',
Postage => num(100,99),
SvcCommitments => ignore(),
SvcDescription => ignore(),
},
),
},
],
},
'... returned data from USPS in correct format. If this test fails, the driver may need to be updated'
) or diag Dumper $xmlData;
}
my $cost = $driver->_calculateFromXML(
{
IntlRateResponse => {
Package => [
{
ID => 0,
Service => [
{
ID => '9',
Postage => '5.25',
MaxWeight => '70'
},
{
ID => '11',
Postage => '7.25',
MaxWeight => '70'
},
],
},
],
},
},
@shippableUnits
);
is($cost, 5.25, '_calculateFromXML calculates shipping cost correctly for 1 item in the cart');
$bibleItem = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $nivBible));
@shippableUnits = $driver->_getShippableUnits($cart);
$xml = $driver->buildXML($cart, @shippableUnits);
$xmlData = XMLin( $xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
IntlRateRequest => {
USERID => $userId,
Package => [
{
ID => 0,
Pounds => '2', Ounces => '0.0',
Machinable => 'true', Country => 'Netherlands',
MailType => 'Package',
},
{
ID => 1,
Pounds => '1', Ounces => '8.0',
Machinable => 'true', Country => 'Netherlands',
MailType => 'Package',
},
],
}
},
'Validate XML structure and content for 2 items in the cart'
);
SKIP: {
skip 'No userId for testing', 1 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful for 2 items in cart');
}
$cost = $driver->_calculateFromXML(
{
IntlRateResponse => {
Package => [
{
ID => 0,
Service => [
{
ID => '9',
Postage => '7.00',
MaxWeight => '70'
},
{
ID => '11',
Postage => '9.00',
MaxWeight => '70'
},
],
},
{
ID => 1,
Service => [
{
ID => '9',
Postage => '5.25',
MaxWeight => '70'
},
{
ID => '11',
Postage => '7.25',
MaxWeight => '70'
},
],
},
],
},
},
@shippableUnits
);
is($cost, 12.25, '_calculateFromXML calculates shipping cost correctly for 2 items in the cart');
$bibleItem->setQuantity(2);
@shippableUnits = $driver->_getShippableUnits($cart);
$cost = $driver->_calculateFromXML(
{
IntlRateResponse => {
Package => [
{
ID => 0,
Service => [
{
ID => '9',
Postage => '7.00',
MaxWeight => '70'
},
{
ID => '11',
Postage => '9.00',
MaxWeight => '70'
},
],
},
{
ID => 1,
Service => [
{
ID => '9',
Postage => '5.25',
MaxWeight => '70'
},
{
ID => '11',
Postage => '7.25',
MaxWeight => '70'
},
],
},
],
},
},
@shippableUnits
);
is($cost, 19.25, '_calculateFromXML calculates shipping cost correctly for 2 items in the cart, with quantity of 2');
$rockHammer2 = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
$rockHammer2->update({shippingAddressId => $sdhAddress->getId});
@shippableUnits = $driver->_getShippableUnits($cart);
$xml = $driver->buildXML($cart, @shippableUnits);
$xmlData = XMLin( $xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
IntlRateRequest => {
USERID => $userId,
Package => [
{
ID => 0,
Pounds => '2', Ounces => '0.0',
Machinable => 'true', Country => 'Netherlands',
MailType => 'Package',
},
{
ID => 1,
Pounds => '12', Ounces => '0.0',
Machinable => 'true', Country => 'Australia',
MailType => 'Package',
},
{
ID => 2,
Pounds => '1', Ounces => '8.0',
Machinable => 'true', Country => 'Netherlands',
MailType => 'Package',
},
],
}
},
'Validate XML structure and content for 3 items in the cart, 3 shippable items'
);
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
my $response = $driver->_doXmlRequest($xml);
ok($response->is_success, '_doXmlRequest to USPS successful for 3 items in cart');
}
#######################################################################
#
# Check for minimum weight allowed
#
#######################################################################
$cart->empty;
$properties = $driver->get();
$properties->{shipType} = '9';
$properties->{addInsurance} = 0;
$driver->update($properties);
my $page1 = $bible->addToCart($bible->getCollateral('variantsJSON', 'variantId', $singlePage));
@shippableUnits = $driver->_getShippableUnits($cart);
$xml = $driver->buildXML($cart, @shippableUnits);
$xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
IntlRateRequest => {
USERID => $userId,
Package => [
{
ID => 0,
Pounds => '0', Ounces => '0.1',
Machinable => 'true', Country => 'Netherlands',
MailType => 'Package',
},
],
}
},
'buildXML: minimum weight'
);
#######################################################################
#
# Check too heavy for my shipping type
#
#######################################################################
SKIP: {
skip 'No userId for testing', 2 unless $hasRealUserId;
$cart->empty;
$properties = $driver->get();
$properties->{shipType} = '9';
$driver->update($properties);
my $heavyHammer = $rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
$heavyHammer->setQuantity(2);
$cost = eval { $driver->calculate($cart); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::Shop::RemoteShippingRate', "USPS returns error when package is too heavy for the selected service");
cmp_deeply(
$e,
methods(
error => 'Selected shipping service not available',
),
'... checking error message',
);
$heavyHammer->setQuantity(20);
$cost = eval { $driver->calculate($cart); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::Shop::RemoteShippingRate', "USPS returns error when package is too heavy for any service");
}
#######################################################################
#
# Insurance
#
#######################################################################
SKIP: {
skip 'No userId for testing', 3 unless $hasRealUserId;
$cart->empty;
$properties = $driver->get();
$properties->{shipType} = '9';
$driver->update($properties);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
my $noInsuranceCost = $driver->calculate($cart);
$properties->{addInsurance} = 1;
$driver->update($properties);
@shippableUnits = $driver->_getShippableUnits($cart);
my $xml = $driver->buildXML($cart, @shippableUnits);
my $xmlData = XMLin($xml,
KeepRoot => 1,
ForceArray => ['Package'],
);
cmp_deeply(
$xmlData,
{
IntlRateRequest => {
USERID => $userId,
Package => [
{
ID => 0,
Pounds => '12', Ounces => '0.0',
Machinable => 'true', Country => 'Netherlands',
MailType => 'Package', ValueOfContents => '19.99',
},
],
}
},
'buildXML: 1 item in cart'
);
like($xml, qr/IntlRateRequest USERID.+?Package ID=.+?Pounds.+?Ounces.+?Machinable.+?MailType.+?ValueOfContents.+?Country.+?/, '... and tag order');
my $insuredCost = $driver->calculate($cart);
cmp_ok $noInsuranceCost, '<', $insuredCost, 'insured cost is higher than uninsured cost';
$properties->{addInsurance} = 0;
$driver->update($properties);
}
#######################################################################
#
# _calculateFromXML
#
#######################################################################
$cart->empty;
$properties = $driver->get();
$properties->{shipType} = '9';
$properties->{addInsurance} = 1;
$driver->update($properties);
$rockHammer->addToCart($rockHammer->getCollateral('variantsJSON', 'variantId', $bigHammer));
@shippableUnits = $driver->_getShippableUnits($cart);
$cost = eval { $driver->_calculateFromXML(
{
IntlRateResponse => {
Package => [
{
ID => 11,
Service => [
{
ID => '9',
Postage => '5.25',
MaxWeight => '70'
},
],
},
],
},
},
@shippableUnits
); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::Shop::RemoteShippingRate', '_calculateFromXML throws an exception for illegal package ids');
cmp_deeply(
$e,
methods(
error => 'Illegal package index returned by USPS: 11',
),
'... checking error message',
);
#######################################################################
#
# Check for throwing an exception
#
#######################################################################
$userId = $driver->get('userId');
$properties = $driver->get();
$properties->{userId} = '_NO_NO_NO_NO';
$driver->update($properties);
$cost = eval { $driver->calculate($cart); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::Shop::RemoteShippingRate', 'calculate throws an exception when a bad userId is used');
$properties->{userId} = $userId;
$driver->update($properties);
my $dutchAddress = $addressBook->addAddress({
label => 'american',
organization => 'Plain Black Corporation',
address1 => '1360 Regent St. #145',
city => 'Madison', state => 'WI', code => '53715',
country => 'United States',
});
$cart->update({shippingAddressId => $dutchAddress->getId});
$cost = eval { $driver->calculate($cart); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', "calculate won't calculate for domestic countries");
$cart->update({shippingAddressId => $workAddress->getId});