From f2c0a672f4edf8863647c750178538b788a84b03 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 26 Feb 2008 22:45:06 +0000 Subject: [PATCH] Add the shippingDrivers entry to the config file. Have the update script remove the old shipping plugin and add the new shipping driver entry to config files. Tests for Ship.t. getDrivers and some "new" code for Shop::Ship. --- docs/upgrades/upgrade_7.5.2-7.5.3.pl | 10 +++ etc/WebGUI.conf.original | 4 +- lib/WebGUI/Shop/Ship.pm | 33 +++++++-- t/Shop/Ship.t | 101 +++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 t/Shop/Ship.t diff --git a/docs/upgrades/upgrade_7.5.2-7.5.3.pl b/docs/upgrades/upgrade_7.5.2-7.5.3.pl index a03b244cf..a3f22fd1f 100644 --- a/docs/upgrades/upgrade_7.5.2-7.5.3.pl +++ b/docs/upgrades/upgrade_7.5.2-7.5.3.pl @@ -28,6 +28,7 @@ insertCommerceTaxTable($session); insertCommerceShipDriverTable($session); migrateToNewCart($session); createSkuAsset($session); +addShippingDrivers($session); finish($session); # this line required @@ -108,6 +109,15 @@ EOSQL } +#------------------------------------------------- +sub addShippingDrivers { + my $session = shift; + print "\tSet up the default shipping.\n" unless ($quiet); + # and here's our code + $session->config->delete('shippingPlugins'); + $session->config->addToArray('shippingDrivers', 'WebGUI::Shop::ShipDriver::FlatRate'); +} + # --------------- DO NOT EDIT BELOW THIS LINE -------------------------------- diff --git a/etc/WebGUI.conf.original b/etc/WebGUI.conf.original index da9ca791d..b14db2ee8 100644 --- a/etc/WebGUI.conf.original +++ b/etc/WebGUI.conf.original @@ -173,10 +173,10 @@ "paymentPlugins" : ["ITransact","Cash"], -# List the shipping plugins you have installed and wish to be +# List the shipping drivers you have installed and wish to be # available for configuration on the site. -"shippingPlugins" : ["ByPrice", "ByWeight", "PerTransaction"], +"shippingDrivers" : ["WebGUI::Shop::ShipDriver::FlatRate"], # Specify the list of template parsers available in the system. diff --git a/lib/WebGUI/Shop/Ship.pm b/lib/WebGUI/Shop/Ship.pm index e989a2aee..325828deb 100644 --- a/lib/WebGUI/Shop/Ship.pm +++ b/lib/WebGUI/Shop/Ship.pm @@ -5,6 +5,8 @@ use strict; use Carp qw(croak); use WebGUI::International; use WebGUI::Shop::ShipDriver; +use WebGUI::Pluggable; +use WebGUI::Utility; =head1 NAME @@ -58,29 +60,38 @@ A list of properties to assign to this ShipperDriver. See C for det sub create { my $class = shift; my $session = shift; - croak "Definition requires a session object" + croak "create requires a session object" unless ref $session eq 'WebGUI::Session'; my $requestedClass = shift; + croak "create requires the name of a class to create an object" + unless defined $requestedClass; + croak "The requested class $class is not enabled in your WebGUI configuration file" + unless isIn($requestedClass, @{ WebGUI::Shop::Ship->getDrivers($session) } ); my $options = shift; croak "You must pass a hashref of options to create a new ShipDriver object" unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options }; + } #------------------------------------------------------------------- =head2 getDrivers ( $session ) -This subroutine returns an arrayref of hashrefs, used to validate data put into -the object by the user, and to automatically generate the edit form to show -the user. +This subroutine returns an arrayref of available shipping driver classes +from the WebGUI config file. + +=head3 $session + +A WebGUI::Session object. =cut sub getDrivers { my $class = shift; my $session = shift; - croak "Definition requires a session object" + croak "getDrivers requires a session object" unless ref $session eq 'WebGUI::Session'; + return $session->config->get('shippingDrivers'); } #------------------------------------------------------------------- @@ -90,6 +101,10 @@ sub getDrivers { Returns a list of options for the user to ship, along with the cost of using each one. It is a hash of hashrefs, with the key of the primary hash being the shipperId of the driver, and sub keys of label and price. +=head3 $session + +A WebGUI::Session object. + =head3 =cut @@ -108,6 +123,14 @@ sub getOptions { Looks up an existing ShipperDriver in the db by shipperId and returns that object. +=head3 $session + +A WebGUI::Session object. + +=head3 $shipperId + +The ID of a shipper to look up and instanciate. + =cut sub new { diff --git a/t/Shop/Ship.t b/t/Shop/Ship.t new file mode 100644 index 000000000..61a149d78 --- /dev/null +++ b/t/Shop/Ship.t @@ -0,0 +1,101 @@ +# 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 = 7; +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; + +####################################################################### +# +# getDrivers +# +####################################################################### + +my $drivers; + +eval { $drivers = WebGUI::Shop::Ship->getDrivers(); }; +like ($@, qr/getDrivers requires a session object/, 'getDrivers croaks without session'); + +$drivers = WebGUI::Shop::Ship->getDrivers($session); + +cmp_deeply( + $drivers, + [ 'WebGUI::Shop::ShipDriver::FlatRate' ], + 'getDrivers: WebGUI only ships with 1 default shipping driver', +); + +####################################################################### +# +# create +# +####################################################################### + +eval { $drivers = WebGUI::Shop::Ship->create(); }; +like ($@, qr/create requires a session object/, 'create croaks without session'); + +eval { $drivers = WebGUI::Shop::Ship->create($session); }; +like ($@, qr/create requires the name of a class/, 'create croaks without a class'); + +eval { $drivers = WebGUI::Shop::Ship->create($session, 'WebGUI::Shop::ShipDriver::FreeShipping'); }; +like ($@, qr/The requested class \S+ is not enabled in your WebGUI configuration file/, 'create croaks without a configured class'); + +eval { $drivers = WebGUI::Shop::Ship->create($session, 'WebGUI::Shop::ShipDriver::FlatRate'); }; +like ($@, qr/You must pass a hashref of options to create a new ShipDriver object/, 'create croaks without options to build a object with'); + +eval { $drivers = WebGUI::Shop::Ship->create($session, 'WebGUI::Shop::ShipDriver::FlatRate', {}); }; +like ($@, qr/You must pass a hashref of options to create a new ShipDriver object/, 'create croaks without options to build a object with'); + + +####################################################################### +# +# new +# +####################################################################### + +} + +#---------------------------------------------------------------------------- +# Cleanup +END { + $session->db->write('delete from shipper'); +}