webgui/t/Session/Scratch.t
Doug Bell babfa74209 Merge branch 'master' into 8-merge
Conflicts:
	docs/gotcha.txt
	lib/WebGUI.pm
	lib/WebGUI/Asset.pm
	lib/WebGUI/Asset/File/GalleryFile/Photo.pm
	lib/WebGUI/Asset/Post.pm
	lib/WebGUI/Asset/Story.pm
	lib/WebGUI/Asset/Template.pm
	lib/WebGUI/Asset/Wobject/Calendar.pm
	lib/WebGUI/Asset/Wobject/GalleryAlbum.pm
	lib/WebGUI/Asset/Wobject/Navigation.pm
	lib/WebGUI/AssetLineage.pm
	lib/WebGUI/AssetTrash.pm
	lib/WebGUI/Config.pm
	lib/WebGUI/Form/Template.pm
	lib/WebGUI/Group.pm
	lib/WebGUI/Inbox.pm
	lib/WebGUI/Workflow/Activity/DeleteExpiredSessions.pm
	lib/WebGUI/Workflow/Activity/TrashExpiredEvents.pm
	sbin/testEnvironment.pl
	t/AdSpace.t
	t/AdSpace/Ad.t
	t/Asset/Asset.t
	t/Asset/AssetExportHtml.t
	t/Asset/AssetLineage.t
	t/Asset/EMSSubmissionForm.t
	t/Asset/Event.t
	t/Asset/File/GalleryFile/Photo/00base.t
	t/Asset/File/GalleryFile/Photo/comment.t
	t/Asset/File/GalleryFile/Photo/download.t
	t/Asset/File/GalleryFile/Photo/edit.t
	t/Asset/File/GalleryFile/Photo/exif.t
	t/Asset/File/GalleryFile/Photo/makeResolutions.t
	t/Asset/File/GalleryFile/Photo/makeShortcut.t
	t/Asset/File/Image/setfile.t
	t/Asset/File/setfile.t
	t/Asset/Post.t
	t/Asset/Post/Thread/getAdjacentThread.t
	t/Asset/Sku.t
	t/Asset/Sku/ProductCollateral.t
	t/Asset/Story.t
	t/Asset/Template.t
	t/Asset/Template/HTMLTemplateExpr.t
	t/Asset/Wobject/Gallery/00base.t
	t/Asset/Wobject/GalleryAlbum/00base.t
	t/Asset/Wobject/GalleryAlbum/ajax.t
	t/Asset/Wobject/GalleryAlbum/delete.t
	t/Asset/Wobject/Matrix.t
	t/Asset/Wobject/StoryArchive.t
	t/Asset/Wobject/Survey/ExpressionEngine.t
	t/Asset/Wobject/Survey/Reports.t
	t/AssetAspect/RssFeed.t
	t/Auth/mech.t
	t/Config.t
	t/Group.t
	t/Help/isa.t
	t/International.t
	t/Mail/Send.t
	t/Operation/AdSpace.t
	t/Operation/Auth.t
	t/Pluggable.t
	t/Session.t
	t/Session/DateTime.t
	t/Session/ErrorHandler.t
	t/Session/Scratch.t
	t/Session/Stow.t
	t/Shop/Cart.t
	t/Shop/Pay.t
	t/Shop/PayDriver/ITransact.t
	t/Shop/PayDriver/PayPalStd.t
	t/Shop/Ship.t
	t/Shop/ShipDriver.t
	t/Shop/TaxDriver/EU.t
	t/Shop/TaxDriver/Generic.t
	t/Shop/Transaction.t
	t/Shop/Vendor.t
	t/VersionTag.t
	t/Workflow/Activity/ArchiveOldStories.t
	t/Workflow/Activity/ExpireIncompleteSurveyResponses.t
	t/lib/WebGUI/Test.pm
2010-07-09 11:48:30 -05:00

133 lines
6.1 KiB
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
#-------------------------------------------------------------------
use FindBin;
use strict;
use lib "$FindBin::Bin/../lib";
use WebGUI::Test;
use WebGUI::Session;
use Test::More tests => 62; # increment this value for each test you create
use Test::Deep;
my $session = WebGUI::Test->session;
my $scratch = $session->scratch;
my $maxCount = 10;
$scratch->deleteAll();
for (my $count = 1; $count <= $maxCount; $count++){
$scratch->set("Test$count",$count);
}
for (my $count = 1; $count <= $maxCount; $count++){
is($scratch->get("Test$count"), $count, "Passed set/get $count");
}
is($scratch->delete("nonExistantVariable"), undef, 'delete returns value if deleted, otherwise undef');
is($scratch->delete("Test1"), 1, 'delete returns number deleted');
is($scratch->delete(), undef, 'delete without name of variable to delete returns undef');
is($scratch->get("Test1"), undef, "delete()");
$scratch->deleteAll;
is($scratch->get("Test2"), undef, "deleteAll()");
my $testScratchSession = $scratch->session();
is($testScratchSession, $session, "session()");
##Build some variables to test database persistency
for (my $count = 1; $count <= $maxCount; $count++){
$scratch->set("dBase$count",$count);
my ($setValue) = $session->db->quickArray("select value from userSessionScratch where sessionId=? and name=?",[$session->getId, "dBase$count"]);
is($setValue, $count, "database store for set on $count");
}
##Creating a new session with the previous session's Id should clone the scratch data
my $newSession = WebGUI::Session->open(WebGUI::Test->file, undef, $session->getId);
WebGUI::Test->addToCleanup($newSession);
is($newSession->getId, $session->getId, "Successful session duplication");
for (my $count = 1; $count <= $maxCount; $count++){
is($newSession->scratch->get("dBase$count"), $count, "Passed set/get $count");
}
$scratch->set("dBase5", 15);
my ($changedValue) = $session->db->quickArray("select value from userSessionScratch where sessionId=? and name=?",[$session->getId, "dBase5"]);
is($changedValue, 15, "changing stored scratch value");
is($scratch->get("dBase5"), 15, "checking cached scratch value");
$newSession->scratch->deleteAll;
$newSession->close;
is($scratch->set('retVal',2), 1, 'set returns number of rows affected');
is($scratch->set(), undef, 'set returns undef unless it gets a name');
is($scratch->set('','value'), undef, 'set returns undef unless it gets a name even if there is a value');
############################################
#
# Multi-session deleting
#
############################################
my @sessionBank = map { WebGUI::Session->open(WebGUI::Test->file) } 0..3;
WebGUI::Test->addToCleanup(@sessionBank);
##Set variables to be deleted by name
foreach my $i (0..3) {
$sessionBank[$i]->scratch->set('deletableName', $i);
}
##Set variables to be deleted by name and value
$sessionBank[0]->scratch->set('deletableValue', 'a');
$sessionBank[1]->scratch->set('deletableValue', 'a');
$sessionBank[2]->scratch->set('deletableValue', 'b');
$sessionBank[2]->scratch->set('falseValue', '');
$sessionBank[3]->scratch->set('deletableValue', 'c');
$sessionBank[3]->scratch->set('falseValue', '0');
is($scratch->deleteName(), undef, 'deleteName without name of variable to delete returns undef');
is($sessionBank[2]->scratch->deleteName("deletableName"), 4, 'deleteName returns number of elements deleted');
is($sessionBank[2]->scratch->get("deletableName"), undef, 'deleteName clears session cached in the object that calls it');
is($sessionBank[1]->scratch->get('deletableName'), 1, "deleteName does not change session cached vriables");
my ($entries) = $session->db->quickArray("select count(name) from userSessionScratch where name=?",['deletableName']);
is($entries, 0, "deleteName deletes entries in the database");
is($sessionBank[1]->scratch->deleteNameByValue('deletableValue', 'a'), 2, 'deleteNameByValue deleted two rows');
($entries) = $session->db->quickArray("select count(name) from userSessionScratch where name=?",['deletableValue']);
is($entries, 2, "deleteNameByValue deleted entries in the database");
is($sessionBank[1]->scratch->get('deletableValue'), undef, 'deleteNameByValue removes session cache in object that called it...');
is($sessionBank[0]->scratch->get('deletableValue'), 'a', 'but not in any other object whose database entry was cleared');
cmp_bag($session->db->buildArrayRef('select value from userSessionScratch where name=?',['deletableValue']), ['b', 'c'], 'deleteNameByValue values that were not deleted');
is($sessionBank[2]->scratch->deleteNameByValue('deletableValue', 'c'), 1, 'deleteNameByValue deleted one row');
is($sessionBank[0]->scratch->deleteNameByValue('',35), undef, 'deleteNameByValue requires a NAME');
is($sessionBank[0]->scratch->deleteNameByValue('scratch'), undef, 'deleteNameByValue requires a value');
is($sessionBank[0]->scratch->deleteNameByValue('',''), undef, 'deleteNameByValue require a NAME and a VALUE');
is($sessionBank[3]->scratch->deleteNameByValue('falseValue','0'), 1, 'deleteNameByValue will delete values that are false (0)');
is($sessionBank[2]->scratch->deleteNameByValue('falseValue',''), 1, "deleteNameByValue will delete values that are false ('')");
$scratch->setLanguageOverride('English');
is($scratch->getLanguageOverride, 'English', 'session scratch language is not correctly set');
$scratch->removeLanguageOverride;
is($scratch->getLanguageOverride, undef, 'The session scratch variable language is not removed');
$scratch->setLanguageOverride('myimmaginarylanguagethatisnotinstalled');
is($scratch->getLanguageOverride, undef, 'A non-existing language is set');
$scratch->setLanguageOverride('English');
$scratch->setLanguageOverride();
is($scratch->getLanguageOverride, 'English', 'A empty string is falsely recognised as a language');
#vim:ft=perl