Fix getCollateral, which used to return direct copies instead of safe copies, and

add a test for that.
addToCart does not adjust inventory, that is handled by the cart.
Invert the sense of quantity in onAdjustQuantity.
This commit is contained in:
Colin Kuskie 2008-06-10 01:48:20 +00:00
parent ad92f6b628
commit 227745d975
2 changed files with 19 additions and 8 deletions

View file

@ -65,8 +65,8 @@ Override/extend Sku's addToCart method to handle inventory control.
=head3 variant
A hashref of variant information for the variant of the Product
that is being added to the cart. Adding the variant to the
cart decrements the quantity by 1.
that is being added to the cart. Inventory management is handled
by the cart, which calls onAdjustQuantity.
=cut
@ -74,8 +74,6 @@ sub addToCart {
my $self = shift;
my $variant = shift;
my $i18n = WebGUI::International->new($self->session, 'Asset_Product');
$variant->{quantity} -= 1;
$self->setCollateral('variantsJSON', 'variantId', $variant);
$self->SUPER::addToCart($variant);
}
@ -308,7 +306,8 @@ sub getCollateral {
my $table = $self->getAllCollateral($tableName);
my $index = $self->getCollateralDataIndex($table, $keyName, $keyValue);
return {} if $index == -1;
return $table->[$index];
my %copy = %{ $table->[$index] };
return \%copy;
}
@ -598,11 +597,11 @@ sub onAdjustQuantityInCart {
my $item = shift;
my $amount = shift;
##Update myself, as options
$self->getOptions->{quantity} += $amount;
$self->getOptions->{quantity} -= $amount;
##Update my collateral
my $vid = $self->getOptions->{variantId};
my $collateral = $self->getCollateral('variantsJSON', 'variantId', $vid);
$collateral->{quantity} += $amount;
$collateral->{quantity} -= $amount;
$self->setCollateral('variantsJSON', 'variantId', $vid, $collateral);
}

View file

@ -35,7 +35,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 32; # Increment this number for each test you create
plan tests => 33; # Increment this number for each test you create
#----------------------------------------------------------------------------
# put your tests here
@ -279,6 +279,18 @@ ok($session->id->valid($newVid), '... and it is a valid GUID');
$product4->purge;
my $product5 = $root->addChild({
className => "WebGUI::Asset::Sku::Product",
title => "Working in the laundry",
});
$newVid = $product5->setCollateral('variantsJSON', 'vid', 'new', { check => 'no leaks', vid => 'new' });
my $leak = $product5->getCollateral('variantsJSON', 'vid', $newVid);
$leak->{check} = 'leaky';
is( $product5->getCollateral('variantsJSON', 'vid', $newVid)->{check}, 'no leaks', 'getCollateral returns a safe copy');
$product5->purge;
#----------------------------------------------------------------------------
# Cleanup
END {