diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 47ddc5f75..00e89d975 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -54,6 +54,7 @@ - fixed #11004: Subscribable AssetAspect: handling the subscription group - fixed #10990: Survey: View Transposed Results not working - fixed #11009: Shipping address is lost after login + - fixed #11010: Purchasing non-recurring subscription twice does not extend group membership 7.7.19 - fixed #10838: Forwarded forum post email to new CS adds reply to original thread diff --git a/lib/WebGUI/Asset/Sku/Subscription.pm b/lib/WebGUI/Asset/Sku/Subscription.pm index 01469e8ed..82c52996d 100644 --- a/lib/WebGUI/Asset/Sku/Subscription.pm +++ b/lib/WebGUI/Asset/Sku/Subscription.pm @@ -58,19 +58,30 @@ specified by the session variable. sub apply { my $self = shift; - my $userId = shift || $self->session->user->userId; + my $session = $self->session; + my $userId = shift || $session->user->userId; my $groupId = $self->get('subscriptionGroup'); # Make user part of the right group and adjust the expiration date - my $group = WebGUI::Group->new($self->session,$groupId); - $group->addUsers( [$userId], $self->getExpirationOffset ); + my $group = WebGUI::Group->new($session, $groupId); + my $user = WebGUI::User->new($session, $userId); + if ($user->isInGroup($group->getId) && ! $self->isRecurring) { + my $expireDate = $group->userGroupExpireDate($userId); + $expireDate += $self->getExpirationOffset; + $group->userGroupExpireDate($userId, $expireDate); + } + else { + $group->addUsers( [$userId], $self->getExpirationOffset ); + } # Add karma to the user's account - WebGUI::User->new($self->session,$userId)->karma($self->get('karma'), 'Subscription', 'Added for purchasing subscription '.$self->get('title')); + if ($session->setting->get('userKarma')) { + WebGUI::User->new($session,$userId)->karma($self->get('karma'), 'Subscription', 'Added for purchasing subscription '.$self->get('title')); + } # Process the executeOnPurchase field my $command = $self->get('executeOnSubscription'); - WebGUI::Macro::process($self->session,\$command); + WebGUI::Macro::process($session,\$command); system($command) if ($self->get('executeOnSubscription') ne ""); } diff --git a/t/Asset/Sku/Subscription.t b/t/Asset/Sku/Subscription.t new file mode 100644 index 000000000..4e08811d9 --- /dev/null +++ b/t/Asset/Sku/Subscription.t @@ -0,0 +1,73 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 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. +# +# This tests WebGUI::Asset::Sku::Donation + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; +use Test::More; +use Test::Deep; +use Test::Number::Delta; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Asset; +use WebGUI::Asset::Sku::Subscription; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 4; # Increment this number for each test you create + +#---------------------------------------------------------------------------- +# put your tests here +my $root = WebGUI::Asset->getRoot($session); +my $group = WebGUI::Group->new($session, 'new'); +WebGUI::Test->groupsToDelete($group); +my $user = WebGUI::User->create($session); +WebGUI::Test->usersToDelete($user); + +my $sku = $root->addChild({ + className => "WebGUI::Asset::Sku::Subscription", + title => "Test Subscription", + price => 50.00, + recurringSubscription => 0, + subscriptionGroup => $group->getId, + duration => 'Monthly', + }); +my $versionTag = WebGUI::VersionTag->getWorking($session); +WebGUI::Test->tagsToRollback($versionTag); +isa_ok($sku, "WebGUI::Asset::Sku::Subscription"); + +is($sku->getPrice, 50.00, "Price should be 50.00"); + +$sku->apply($user->userId); + +cmp_deeply( + $group->userGroupExpireDate($user->getId)-time(), + num($sku->getExpirationOffset, 5), + "apply: sets user's group expiration offset correctly" +); + +$sku->apply($user->userId); + +cmp_deeply( + $group->userGroupExpireDate($user->getId)-time(), + num(2*$sku->getExpirationOffset, 10), + "... increments user's expiration offset when the subscription is non-recurring and they are already a group member" +);