From 227745d975c986ce152c4585d1b7b9f537dcab89 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 10 Jun 2008 01:48:20 +0000 Subject: [PATCH] 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. --- lib/WebGUI/Asset/Sku/Product.pm | 13 ++++++------- t/Asset/Sku/ProductCollateral.t | 14 +++++++++++++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/WebGUI/Asset/Sku/Product.pm b/lib/WebGUI/Asset/Sku/Product.pm index 5d73f7e89..4ac55af17 100644 --- a/lib/WebGUI/Asset/Sku/Product.pm +++ b/lib/WebGUI/Asset/Sku/Product.pm @@ -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); } diff --git a/t/Asset/Sku/ProductCollateral.t b/t/Asset/Sku/ProductCollateral.t index 33b5b0e94..2789709aa 100644 --- a/t/Asset/Sku/ProductCollateral.t +++ b/t/Asset/Sku/ProductCollateral.t @@ -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 {