webgui/t/Shop/Transaction.t
Doug Bell 277faae8a1 Merge commit 'v7.10.15' into 8
Conflicts:
	docs/gotcha.txt
	docs/previousVersion.sql
	docs/templates.txt
	lib/WebGUI.pm
	lib/WebGUI/Asset.pm
	lib/WebGUI/Asset/Event.pm
	lib/WebGUI/Asset/File.pm
	lib/WebGUI/Asset/MapPoint.pm
	lib/WebGUI/Asset/RichEdit.pm
	lib/WebGUI/Asset/Sku/Product.pm
	lib/WebGUI/Asset/Snippet.pm
	lib/WebGUI/Asset/Story.pm
	lib/WebGUI/Asset/Template.pm
	lib/WebGUI/Asset/Template/TemplateToolkit.pm
	lib/WebGUI/Asset/Wobject/Calendar.pm
	lib/WebGUI/Asset/Wobject/Carousel.pm
	lib/WebGUI/Asset/Wobject/Collaboration.pm
	lib/WebGUI/Asset/Wobject/Dashboard.pm
	lib/WebGUI/Asset/Wobject/DataForm.pm
	lib/WebGUI/Asset/Wobject/Folder.pm
	lib/WebGUI/Asset/Wobject/Map.pm
	lib/WebGUI/Asset/Wobject/Search.pm
	lib/WebGUI/Asset/Wobject/Shelf.pm
	lib/WebGUI/Asset/Wobject/StockData.pm
	lib/WebGUI/Asset/Wobject/StoryTopic.pm
	lib/WebGUI/Asset/Wobject/SyndicatedContent.pm
	lib/WebGUI/Asset/Wobject/Thingy.pm
	lib/WebGUI/Asset/Wobject/WeatherData.pm
	lib/WebGUI/AssetClipboard.pm
	lib/WebGUI/AssetCollateral/DataForm/Entry.pm
	lib/WebGUI/AssetExportHtml.pm
	lib/WebGUI/AssetLineage.pm
	lib/WebGUI/AssetMetaData.pm
	lib/WebGUI/AssetTrash.pm
	lib/WebGUI/AssetVersioning.pm
	lib/WebGUI/Auth.pm
	lib/WebGUI/Cache/CHI.pm
	lib/WebGUI/Content/AssetManager.pm
	lib/WebGUI/Fork/ProgressBar.pm
	lib/WebGUI/Form/JsonTable.pm
	lib/WebGUI/Form/TimeField.pm
	lib/WebGUI/Form/Zipcode.pm
	lib/WebGUI/Group.pm
	lib/WebGUI/International.pm
	lib/WebGUI/Macro/AssetProxy.pm
	lib/WebGUI/Macro/FileUrl.pm
	lib/WebGUI/Operation/SSO.pm
	lib/WebGUI/Operation/User.pm
	lib/WebGUI/Role/Asset/Subscribable.pm
	lib/WebGUI/Shop/Cart.pm
	lib/WebGUI/Shop/Transaction.pm
	lib/WebGUI/Shop/TransactionItem.pm
	lib/WebGUI/Test.pm
	lib/WebGUI/URL/Content.pm
	lib/WebGUI/URL/Uploads.pm
	lib/WebGUI/User.pm
	lib/WebGUI/Workflow/Activity/ExtendCalendarRecurrences.pm
	lib/WebGUI/Workflow/Activity/SendNewsletters.pm
	lib/WebGUI/i18n/English/Asset.pm
	lib/WebGUI/i18n/English/WebGUI.pm
	sbin/installClass.pl
	sbin/rebuildLineage.pl
	sbin/search.pl
	sbin/testEnvironment.pl
	t/Asset/Asset.t
	t/Asset/AssetClipboard.t
	t/Asset/AssetLineage.t
	t/Asset/AssetMetaData.t
	t/Asset/Event.t
	t/Asset/File.t
	t/Asset/File/Image.t
	t/Asset/Post/notification.t
	t/Asset/Sku.t
	t/Asset/Story.t
	t/Asset/Template.t
	t/Asset/Wobject/Collaboration/templateVariables.t
	t/Asset/Wobject/Collaboration/unarchiveAll.t
	t/Asset/Wobject/Shelf.t
	t/Auth.t
	t/Macro/EditableToggle.t
	t/Macro/FilePump.t
	t/Shop/Cart.t
	t/Shop/Transaction.t
	t/Storage.t
	t/User.t
	t/Workflow.t
2011-05-13 18:15:11 -05:00

344 lines
14 KiB
Perl

# 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
#------------------------------------------------------------------
# Tests the transaction backend for the shop.
#
#
use strict;
use Test::More;
use Test::Deep;
use Test::LongString;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Test::MockAsset;
use WebGUI::Session;
use WebGUI::Shop::Transaction;
use WebGUI::Inbox;
use Clone qw/clone/;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 83; # Increment this number for each test you create
#----------------------------------------------------------------------------
# put your tests here
my $transaction = WebGUI::Shop::Transaction->new($session,{
amount => 40,
shippingAddressId => 'xxx1',
shippingOrganization => 'Ship To Us',
shippingAddressName => 'abc',
shippingAddress1 => 'def',
shippingAddress2 => 'hij',
shippingAddress3 => 'lmn',
shippingCity => 'opq',
shippingState => 'wxy',
shippingCountry => 'z',
shippingCode => '53333',
shippingPhoneNumber => '123456',
shippingDriverId => 'xxx2',
shippingDriverLabel => 'foo',
shippingPrice => 5,
paymentAddressId => 'xxx3',
paymentAddressName => 'abc1',
paymentOrganization => 'Pay To Us',
paymentAddress1 => 'def1',
paymentAddress2 => 'hij1',
paymentAddress3 => 'lmn1',
paymentCity => 'opq1',
paymentState => 'wxy1',
paymentCountry => 'z1',
paymentCode => '66666',
paymentPhoneNumber => '908765',
paymentDriverId => 'xxx4',
paymentDriverLabel => 'kkk',
taxes => 7,
});
addToCleanup($transaction);
# objects work
isa_ok($transaction, "WebGUI::Shop::Transaction");
isa_ok($transaction->session, "WebGUI::Session");
# basic transaction properties
is($transaction->amount, 40, "set and get amount");
is($transaction->shippingAddressId, 'xxx1', "set and get shipping address id");
is($transaction->shippingAddressName, 'abc', "set and get shipping address name");
is($transaction->shippingOrganization, 'Ship To Us', "set and get shipping organization");
is($transaction->shippingAddress1, 'def', "set and get shipping address 1");
is($transaction->shippingAddress2, 'hij', "set and get shipping address 2");
is($transaction->shippingAddress3, 'lmn', "set and get shipping address 3");
is($transaction->shippingCity, 'opq', "set and get shipping city");
is($transaction->shippingState, 'wxy', "set and get shipping state");
is($transaction->shippingCountry, 'z', "set and get shipping country");
is($transaction->shippingCode, '53333', "set and get shipping code");
is($transaction->shippingPhoneNumber, '123456', "set and get shipping phone number");
is($transaction->shippingDriverId, 'xxx2', "set and get shipping driver id");
is($transaction->shippingDriverLabel, 'foo', "set and get shipping driver label");
is($transaction->shippingPrice, 5, "set and get shipping price");
is($transaction->paymentAddressId, 'xxx3', "set and get payment address id");
is($transaction->paymentAddressName, 'abc1', "set and get payment address name");
is($transaction->paymentOrganization, 'Pay To Us', "set and get payment organization");
is($transaction->paymentAddress1, 'def1', "set and get payment address 1");
is($transaction->paymentAddress2, 'hij1', "set and get payment address 2");
is($transaction->paymentAddress3, 'lmn1', "set and get payment address 3");
is($transaction->paymentCity, 'opq1', "set and get payment city");
is($transaction->paymentState, 'wxy1', "set and get payment state");
is($transaction->paymentCountry, 'z1', "set and get payment country");
is($transaction->paymentCode, '66666', "set and get payment code");
is($transaction->paymentPhoneNumber, '908765', "set and get payment phone number");
is($transaction->paymentDriverId, 'xxx4', "set and get payment driver id");
is($transaction->paymentDriverLabel, 'kkk', "set and get payment driver label");
is($transaction->taxes, 7, "set and get taxes");
$transaction->update({
isSuccessful => 1,
transactionCode => 'yyy',
statusCode => 'jd31',
statusMessage => 'was a success',
});
is($transaction->isSuccessful, 1,"update and get isSuccessful");
is($transaction->transactionCode, 'yyy',"update and get transaction code");
is($transaction->statusCode, 'jd31',"update and get status code");
is($transaction->statusMessage, 'was a success',"update and get status message");
is($transaction->taxes, 7, 'update does not modify things it was not sent');
# make sure new() works
my $tcopy = WebGUI::Shop::Transaction->new($session, $transaction->getId);
isa_ok($tcopy, "WebGUI::Shop::Transaction");
is($tcopy->getId, $transaction->getId, "is it the same object");
# basic item properties
my $item = $transaction->addItem({
assetId => 'a',
configuredTitle => 'b',
options => {color=>'blue'},
shippingOrganization => 'organized',
shippingAddressId => 'c',
shippingName => 'd',
shippingAddress1 => 'e',
shippingAddress2 => 'f',
shippingAddress3 => 'g',
shippingCity => 'h',
shippingState => 'i',
shippingCountry => 'j',
shippingCode => 'k',
shippingPhoneNumber => 'l',
quantity => 5,
price => 33,
taxRate => 19,
});
isa_ok($item, "WebGUI::Shop::TransactionItem");
isa_ok($item->transaction, "WebGUI::Shop::Transaction");
is($item->get("assetId"), 'a', "set and get asset id");
is($item->get("configuredTitle"), 'b', "set and get configured title");
cmp_deeply($item->get("options"), {color=>'blue'}, "set and get options");
is($item->get("shippingAddressId"), 'c', "set and get shipping address id");
is($item->get("shippingName"), 'd', "set and get shipping name");
is($item->get("shippingAddress1"), 'e', "set and get shipping address 1");
is($item->get("shippingAddress2"), 'f', "set and get shipping address 2");
is($item->get("shippingAddress3"), 'g', "set and get shipping address 3");
is($item->get("shippingCity"), 'h', "set and get shipping city");
is($item->get("shippingState"), 'i', "set and get shipping state");
is($item->get("shippingCountry"), 'j', "set and get shipping country");
is($item->get("shippingCode"), 'k', "set and get shipping code");
is($item->get("shippingPhoneNumber"), 'l', "set and get shipping phone number");
is($item->get("quantity"), 5, "set and get quantity");
is($item->get("price"), 33, "set and get price");
is($item->get('taxRate'), 19, 'set and get taxRate' );
is($item->shippingOrganization, 'organized', 'set and get shipping organization' );
$item->update({
shippingTrackingNumber => 'adfs',
orderStatus => 'BackOrdered',
});
is($item->get("shippingTrackingNumber"), 'adfs', "update and get shipping tracking number");
is($item->get("orderStatus"), 'BackOrdered', "update and get shipping status");
# make sure new() works
my $icopy = $transaction->getItem($item->getId);
isa_ok($icopy, "WebGUI::Shop::TransactionItem");
is($icopy->getId, $item->getId, "items are the same");
# get items
is(scalar @{$transaction->getItems}, 1, "can retrieve items");
# delete
$item->delete;
is(scalar @{$transaction->getItems}, 0, "can delete items");
#######################################################################
#
# www_getTransactionsAsJson
#
#######################################################################
$session->user({userId=>3});
my $json = WebGUI::Shop::Transaction->www_getTransactionsAsJson($session);
ok($json, 'www_getTransactionsAsJson returned something');
is($session->response->content_type, 'application/json', 'MIME type set to application/json');
my $jsonTransactions = JSON::from_json($json);
cmp_deeply(
$jsonTransactions,
{
sort => undef,
startIndex => 0,
totalRecords => 1,
recordsReturned => 1,
dir => 'desc',
records => array_each({
orderNumber=>ignore,
transactionId=>ignore,
transactionCode=>ignore,
paymentDriverLabel=>ignore,
dateOfPurchase=>ignore,
username=>ignore,
amount=>ignore,
isSuccessful=>ignore,
statusCode=>ignore,
statusMessage=>ignore,
}),
},
'Check major elements of transaction JSON',
);
TODO: {
local $TODO = 'More getTransactionsAsJson tests';
ok(0, 'test group privileges to this method');
ok(0, 'test startIndex variable');
ok(0, 'test results form variable');
ok(0, 'test keywords');
}
#######################################################################
#
# sendNotification
#
#######################################################################
my $shopUser = WebGUI::User->create($session);
$shopUser->username('shopUser');
my $shopGroup = WebGUI::Group->new($session, 'new');
my $shopAdmin = WebGUI::User->create($session);
$shopUser->username('shopAdmin');
$shopGroup->addUsers([$shopAdmin->getId]);
addToCleanup($shopUser, $shopAdmin, $shopGroup);
$session->setting->set('shopSaleNotificationGroupId', $shopGroup->getId);
$session->user({userId => $shopUser->getId});
my $trans = WebGUI::Shop::Transaction->new($session, {});
ok($trans->can('sendNotifications'), 'sendNotifications: valid method for transactions');
addToCleanup($trans);
##Disable sending email
my $sendmock = Test::MockObject->new( {} );
$sendmock->set_isa('WebGUI::Mail::Send');
$sendmock->set_true('addText', 'send', 'addHeaderField', 'addHtml', 'queue', 'addFooter');
local *WebGUI::Mail::Send::create;
$sendmock->fake_module('WebGUI::Mail::Send',
create => sub { return $sendmock },
);
#1234567890123456789012#
my $templateId = 'SHOP_NOTIFICATION_____';
my $templateMock = WebGUI::Test::MockAsset->new('WebGUI::Asset::Template');
$templateMock->mock_id($templateId);
my @templateVars;
$templateMock->mock('process', sub { push @templateVars, clone $_[1]; } );
$session->setting->set('shopReceiptEmailTemplateId', $templateId);
{
WebGUI::Test->addToCleanup(sub { WebGUI::Test->cleanupAdminInbox(); });
$trans->sendNotifications;
is(@templateVars, 2, '... called template->process twice');
my $inbox = WebGUI::Inbox->new($session);
my $userMessages = $inbox->getMessagesForUser($shopUser);
my $adminMessages = $inbox->getMessagesForUser($shopAdmin);
is(@{ $userMessages }, 1, '... sent one message to shop user');
is(@{ $adminMessages }, 1, '... sent one message to shop admin, via shopSaleNotificationGroupId');
like($userMessages->[0]->get('subject'), qr/^Receipt for Order #/, '... subject for user email okay');
like($adminMessages->[0]->get('subject'), qr/^A sale has been made/, '... subject for admin email okay');
like($templateVars[0]->{viewDetailUrl}, qr/shop=transaction;method=viewMy;/, '... viewDetailUrl okay for user');
like($templateVars[1]->{viewDetailUrl}, qr/shop=transaction;method=view;/ , '... viewDetailUrl okay for admin');
}
#######################################################################
#
# formatAddress
#
#######################################################################
my $formattedAddress = $transaction->formatAddress({
name => 'Red',
address1 => 'Cell Block #5',
city => 'Shawshank',
state => 'MN',
code => 55555,
country => 'USA',
phoneNumber => '555.555.5555',
});
is_string $formattedAddress, 'Red<br />Cell Block #5<br />Shawshank, MN 55555<br />USA<br />555.555.5555', 'formatAddress: a regular address';
my $formattedAddress = $transaction->formatAddress({
name => 'Red',
address1 => 'Cell Block #5',
address2 => 'Next to Andy',
city => 'Shawshank',
state => 'MN',
code => 55555,
country => 'USA',
phoneNumber => '555.555.5555',
});
is_string $formattedAddress, 'Red<br />Cell Block #5<br />Next to Andy<br />Shawshank, MN 55555<br />USA<br />555.555.5555', '... a regular address with address2';
my $formattedAddress = $transaction->formatAddress({
name => 'Red',
organization => 'Shawshank Prison',
address1 => 'Cell Block #5',
city => 'Shawshank',
state => 'MN',
code => 55555,
country => 'USA',
phoneNumber => '555.555.5555',
});
is_string $formattedAddress, 'Red<br />Shawshank Prison<br />Cell Block #5<br />Shawshank, MN 55555<br />USA<br />555.555.5555', '... a regular address with address2';
#######################################################################
#
# delete
#
#######################################################################
$transaction->delete;
is($session->db->quickScalar("select count(*) from transaction where transactionId=?",[$transaction->getId]),
0, "delete: deleted transaction");
is($session->db->quickScalar("select count(*) from transactionItem where transactionId=?",[$transaction->getId]),
0, "... deleted transactionItems associated with this transaction");