From 323edd888e8405b3713b499d4d085cbebf4df9bc Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 21 Sep 2009 19:44:43 -0700 Subject: [PATCH] More tests for bug fix #11009. Abstract out making a session in WebGUI::Test. Update POD in Shop/Address. Add tests for newBySession, showing that the previous commit that allows update to update addressBookId actually fixed the bug in question. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Shop/Address.pm | 3 +- lib/WebGUI/Shop/AddressBook.pm | 2 + t/Shop/AddressBook.t | 46 ++++++++++++++++------- t/lib/WebGUI/Test.pm | 69 +++++++++++++++++++++------------- 5 files changed, 79 insertions(+), 42 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 444c35d96..47ddc5f75 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -53,6 +53,7 @@ - fixed #11003: Subscribable AssetAspect: no i18n - fixed #11004: Subscribable AssetAspect: handling the subscription group - fixed #10990: Survey: View Transposed Results not working + - fixed #11009: Shipping address is lost after login 7.7.19 - fixed #10838: Forwarded forum post email to new CS adds reply to original thread diff --git a/lib/WebGUI/Shop/Address.pm b/lib/WebGUI/Shop/Address.pm index 220ac845f..8448a6f85 100644 --- a/lib/WebGUI/Shop/Address.pm +++ b/lib/WebGUI/Shop/Address.pm @@ -24,7 +24,8 @@ Package WebGUI::Shop::Address =head1 DESCRIPTION -An address is used to track shipping or payment addresses in the commerce system. +An address is used to track shipping or payment addresses in the commerce system. Because of +object caching in the AddressBook, addresses should never, ever be accessed directly. =head1 SYNOPSIS diff --git a/lib/WebGUI/Shop/AddressBook.pm b/lib/WebGUI/Shop/AddressBook.pm index 6352a9c24..916381bf7 100644 --- a/lib/WebGUI/Shop/AddressBook.pm +++ b/lib/WebGUI/Shop/AddressBook.pm @@ -85,7 +85,9 @@ Deletes this address book and all addresses contained in it. sub delete { my ($self) = @_; + my $myId = id $self; foreach my $address (@{$self->getAddresses}) { + delete $addressCache{$myId}{$address->getId}; $address->delete; } $self->session->db->write("delete from addressBook where addressBookId=?",[$self->getId]); diff --git a/t/Shop/AddressBook.t b/t/Shop/AddressBook.t index 658fa69ed..fc21d22df 100644 --- a/t/Shop/AddressBook.t +++ b/t/Shop/AddressBook.t @@ -31,19 +31,12 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 23; -plan tests => 1 + $tests; +plan tests => 25; #---------------------------------------------------------------------------- # put your tests here -my $loaded = use_ok('WebGUI::Shop::AddressBook'); - my $storage; - -SKIP: { - -skip 'Unable to load module WebGUI::Shop::AddressBook', $tests unless $loaded; my $e; my $book; @@ -155,9 +148,9 @@ my $address2 = $book->addAddress({ label => q{Norton's office} }); my @addresses = @{ $book->getAddresses() }; -cmp_deeply( - \@addresses, - [$address1, $address2], +cmp_bag( + [ map { $_->getId } @addresses ], + [$address1->getId, $address2->getId], 'getAddresses returns all address objects for this book' ); @@ -204,11 +197,36 @@ $bookClone->delete(); $bookCount = $session->db->quickScalar('select count(*) from addressBook'); my $addrCount = $session->db->quickScalar('select count(*) from address'); -is($bookCount, 0, 'delete: book deleted'); -is($addrCount, 0, 'delete: also deletes addresses in the book'); +is($bookCount, 0, '... book deleted'); +is($addrCount, 0, '... also deletes addresses in the book'); undef $book; -} +####################################################################### +# +# newBySession +# +####################################################################### + + +my $otherSession = WebGUI::Test->newSession; +my $mergeUser = WebGUI::User->create($otherSession); +WebGUI::Test->usersToDelete($mergeUser); +$otherSession->user({user => $mergeUser}); +my $adminBook = WebGUI::Shop::AddressBook->create($otherSession); +my $goodAddress = $adminBook->addAddress({label => 'first'}); + +my $session2 = WebGUI::Test->newSession; +$session2->user({user => $mergeUser}); +my $bookAdmin = WebGUI::Shop::AddressBook->newBySession($session2); + +cmp_bag( + [ map { $_->getId } @{ $bookAdmin->getAddresses } ], + [ $goodAddress->getId, ], + 'newBySession merges address books by userId' +); + +$adminBook->delete; +$bookAdmin->delete; END { $session->db->write('delete from addressBook'); diff --git a/t/lib/WebGUI/Test.pm b/t/lib/WebGUI/Test.pm index 6811a99c9..edf3aea52 100644 --- a/t/lib/WebGUI/Test.pm +++ b/t/lib/WebGUI/Test.pm @@ -71,6 +71,36 @@ my $smtpdSelect; my $mocker; +BEGIN { + +#---------------------------------------------------------------------------- + +=head2 sessionsToDelete ( $session, [$session, ...] ) + +Push a list of session objects onto the stack of groups to be automatically deleted +at the end of the test. Note, this will be the last group of objects to be +cleaned up. + +This is a class method. + +=cut + +sub sessionsToDelete { + my $class = shift; + push @sessionsToDelete, @_; +} + + +sub newSession { + my $pseudoRequest = WebGUI::PseudoRequest->new; + my $session = WebGUI::Session->open( $WEBGUI_ROOT, $CONFIG_FILE ); + $session->{_request} = $pseudoRequest; + WebGUI::Test->sessionsToDelete($session); + return $session; +} + +} + BEGIN { $mocker = Test::MockObject->fake_module( @@ -162,10 +192,8 @@ BEGIN { exit(1); } - my $pseudoRequest = WebGUI::PseudoRequest->new; - #$SESSION = WebGUI::Session->open( $WEBGUI_ROOT, $CONFIG_FILE, $pseudoRequest ); - $SESSION = WebGUI::Session->open( $WEBGUI_ROOT, $CONFIG_FILE ); - $SESSION->{_request} = $pseudoRequest; + + $SESSION = WebGUI::Test->newSession; $originalSetting = clone $SESSION->setting->get; } @@ -193,10 +221,6 @@ END { $stor->delete; } } - SESSION: foreach my $session (@sessionsToDelete) { - $session->var->end; - $session->close; - } ASSET: foreach my $asset (@assetsToPurge) { $asset->purge; } @@ -243,8 +267,10 @@ END { while (my ($param, $value) = each %{ $originalSetting }) { $SESSION->setting->set($param, $value); } - $SESSION->var->end; - $SESSION->close if defined $SESSION; + SESSION: foreach my $session (@sessionsToDelete) { + $session->var->end; + $session->close; + } # Close SMTPD if ($smtpdPid) { @@ -257,6 +283,12 @@ END { } } +=head2 newSession ( ) + +Builds a WebGUI session object for testing. + +=cut + =head2 mockAssetId ( $assetId, $object ) Causes WebGUI::Asset->new* initializers to return the specified @@ -716,23 +748,6 @@ sub storagesToDelete { #---------------------------------------------------------------------------- -=head2 sessionsToDelete ( $session, [$session, ...] ) - -Push a list of session objects onto the stack of groups to be automatically deleted -at the end of the test. Note, this will be the last group of objects to be -cleaned up. - -This is a class method. - -=cut - -sub sessionsToDelete { - my $class = shift; - push @sessionsToDelete, @_; -} - -#---------------------------------------------------------------------------- - =head2 tagsToRollback ( $tag ) Push a list of version tags to rollback at the end of the test.