diff --git a/lib/WebGUI/Shop/ShipDriver.pm b/lib/WebGUI/Shop/ShipDriver.pm index f4e2ff0e3..f0fd09518 100644 --- a/lib/WebGUI/Shop/ShipDriver.pm +++ b/lib/WebGUI/Shop/ShipDriver.pm @@ -15,9 +15,8 @@ Package WebGUI::Shop::ShipDriver =head1 DESCRIPTION -This package manages tax information, and calculates taxes on a shopping cart. It isn't a classic object -in that the only data it contains is a WebGUI::Session object, but it does provide several methods for -handling the information in the tax tables. +This package is the base class for all modules which calculate shipping +costs. =head1 SYNOPSIS diff --git a/lib/WebGUI/Shop/ShipDriver/FlatRate.pm b/lib/WebGUI/Shop/ShipDriver/FlatRate.pm new file mode 100644 index 000000000..8507131d1 --- /dev/null +++ b/lib/WebGUI/Shop/ShipDriver/FlatRate.pm @@ -0,0 +1,98 @@ +package WebGUI::Shop::ShipDriver::FlatRate; + +use strict; +use base qw/WebGUI::Shop::ShipDriver/; + +=head1 NAME + +Package WebGUI::Shop::ShipDriver::FlatRate + +=head1 DESCRIPTION + +This Shipping driver allows for calculating shipping costs without any +tie-ins to external shippers. + +=head1 SYNOPSIS + +=head1 METHODS + +See the master class, WebGUI::Shop::ShipDriver for information about +base methods. These methods are customized in this class: + +=cut + +#------------------------------------------------------------------- + +=head2 calculate ( $cart ) + +Returns a shipping price. Calculates the shipping price using the following formula: + + total price of shippable items * percentageOfPrice + + flatFee + + total weight of shippable items * pricePerWeight + + total quantity of shippable items * pricePerItem + +=head3 $cart + +A WebGUI::Shop::Cart object. The contents of the cart are analyzed to calculate +the shipping costs. + +=cut + +sub calculate { + my $self = shift; + return; +} + +#------------------------------------------------------------------- + +=head2 definition ( $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. + +=cut + +sub definition { + my $class = shift; + my $session = shift; + croak "Definition requires a session object" + unless ref $session eq 'WebGUI::Session'; + my $definition = shift || []; + my $i18n = WebGUI::International->new($session, 'ShipDriver'); + tie my %properties, 'Tie::IxHash'; + %properties = ( + name => 'Flat Rate', + fields => { + flatFee => { + fieldType => 'float', + label => $i18n->get('flatFee'), + hoverHelp => $i18n->get('flatFee help'), + defaultValue => 0, + }, + percentageOfPrice => { + fieldType => 'float', + label => $i18n->get('percentageOfPrice'), + hoverHelp => $i18n->get('percentageOfPrice help'), + defaultValue => 0, + }, + pricePerWeight => { + fieldType => 'float', + label => $i18n->get('pricePerWeight'), + hoverHelp => $i18n->get('pricePerWeight help'), + defaultValue => 0, + }, + pricePerItem => { + fieldType => 'float', + label => $i18n->get('pricePerItem'), + hoverHelp => $i18n->get('pricePerItem help'), + defaultValue => 0, + }, + }, + ); + push @{ $definition }, \%properties; + return $class->SUPER::definition($session, $definition); +} + +1; diff --git a/t/Shop/ShipDriver/FlatRate.t b/t/Shop/ShipDriver/FlatRate.t new file mode 100644 index 000000000..1ded56c5e --- /dev/null +++ b/t/Shop/ShipDriver/FlatRate.t @@ -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 = 28; +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; + +eval { $definition = WebGUI::Shop::ShipDriver::FlatRate->definition(); }; +like ($@, qr/^Definition requires a session object/, 'definition croaks without a session object'); + +$definition = WebGUI::Shop::ShipDriver->definition($session); + +cmp_deeply( + $definition, + [ { + name => 'Shipper Driver', + fields => { + 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 => 'Slow and dangerous', + enabled => 1, + }; + +$driver = WebGUI::Shop::ShipDriver->create( $session, $options); + +isa_ok($driver, 'WebGUI::Shop::ShipDriver::FlatRate'); + +isa_ok($driver, 'WebGUI::Shop::ShipDriver'); + + +####################################################################### +# +# getName +# +####################################################################### + +is ($driver->getName, '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, 5, 'getEditForm: the form has 5 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 => 'shipperId', + type => 'hidden', + }, + { + name => 'className', + type => 'hidden', + }, + { + name => 'label', + type => 'text', + }, + { + name => 'enabled', + type => 'radio', + }, + ], + 'getEditForm made the correct form with all the elements' + +); + +####################################################################### +# +# 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; + +####################################################################### +# +# calculate +# +####################################################################### + +} + +#---------------------------------------------------------------------------- +# Cleanup +END { + $session->db->write('delete from shipper'); +}