diff --git a/lib/WebGUI/Session/Var.pm b/lib/WebGUI/Session/Var.pm index a91e51eb8..e7f86d7e6 100644 --- a/lib/WebGUI/Session/Var.pm +++ b/lib/WebGUI/Session/Var.pm @@ -185,7 +185,7 @@ sub new { $self->{_var}{expires} = $session->datetime->time() + $session->setting->get("sessionTimeout"); $self->session->{_sessionId} = $self->{_var}{sessionId}; $session->db->setRow("userSession","sessionId",$self->{_var}); - } else { ##Start a new session with the requested id. + } else { ##Start a new default session with the requested, non-existant id. $self->start(1,$sessionId); } } diff --git a/t/Session/Var.t b/t/Session/Var.t index f672a8b8e..b213bbd65 100644 --- a/t/Session/Var.t +++ b/t/Session/Var.t @@ -16,7 +16,7 @@ use WebGUI::Test; use WebGUI::Session; use WebGUI::Session::Var; -use Test::More tests => 25; # increment this value for each test you create +use Test::More tests => 40; # increment this value for each test you create use Test::Deep; my $session = WebGUI::Test->session; @@ -86,18 +86,122 @@ $var2->end; ok($count == 0,"end() removes current entry from database"); $var->end; -$session->env->{_env} = $origEnv; +$var2 = WebGUI::Session::Var->new($session, 'illegalSessionIdThatIsTooLong'); +# '1234567890123456789012' +isa_ok($var2, 'WebGUI::Session::Var', 'invalid sessionId will still produce a Var object'); +($count) = $session->db->quickArray("select count(*) from userSession where sessionId=?",[$var2->getId]); +is($count, 0, "object store of sessionId does not match database record"); +$var2Id = $var2->getId; +$var2->end; +my $idToDelete = substr $var2Id,0,22; +($count) = $session->db->quickArray("select count(*) from userSession where sessionId=?",[$idToDelete]); +is($count, 1, "Unable to delete database record for Var object with invalid sessionId"); -TODO: { - local $TODO = "Stuff to write later"; - ok(0, 'check fetching a non-existant sessionId'); - ok(0, 'check fetching a non-existant sessionId with noFuss'); -} +my $varId3 = 'nonExistantIdButValid0'; +# '1234567890123456789012' +$var = WebGUI::Session::Var->new($session, $varId3); +isa_ok($var, 'WebGUI::Session::Var', 'non-existant sessionId will still produce a Var object'); +is($var->getId, $varId3, 'user session Id set to non-existant Id'); +is($session->getId, $varId3, 'session Id set to non-existant Id'); + +cmp_deeply( + $var, + methods( + ['get', 'sessionId'] => $varId3, + ['get', 'userId'] => 1, + ['get', 'adminOn'] => 0, + ['get', 'lastIP'] => '10.0.5.5', + ), + 'non-existant Id returns default values' +); + +$var->end; + +##Grab a new Var object that we'll expire. We'll detect the expiration +##by looking for admin status and userId +$var2 = WebGUI::Session::Var->new($session); +$var2->switchAdminOn; +$session->db->write("update userSession set userId=? where sessionId=?", + [3, $var2->getId]); +$session->db->write("update userSession set expires=? where sessionId=?", + [$var2->get('lastPageView')-1, $var2->getId]); + +my $var3 = WebGUI::Session::Var->new($session, $var2->getId); +is($var3->getId, $var2->getId, 'new Var object has correct id'); +isnt($var3->isAdminOn, $var2->isAdminOn, 'new adminOn not equal to old adminOn'); +is($var3->isAdminOn, 0, 'new Var object has default adminOn'); +isnt($var3->get('userId'), 3, 'new userId not equal to old userId'); +$var2->end; +$var3->end; + +##Var objects for noFuss tests +my $var4 = WebGUI::Session::Var->new($session); +my $varExpiring = WebGUI::Session::Var->new($session); +$session->db->write("update userSession set expires=? where sessionId=?", + [$varExpiring->get('lastPageView')-1, $varExpiring->getId]); +$varExpiring->{_var}{expires} = $varExpiring->get('lastPageView')-1; + +sleep 1; + +$newEnvHash{REMOTE_ADDR} = '127.0.0.1'; + +##Test a valid fetch +my $varTest = WebGUI::Session::Var->new($session, $var4->getId, 1); + +cmp_deeply( + $varTest, + methods( + ['get', 'sessionId'] => $var4->getId, + ['get', 'userId'] => 1, + ['get', 'adminOn'] => 0, + ['get', 'lastIP'] => '10.0.5.5', + ['get', 'expires'] => $var4->get('expires'), + ['get', 'lastPageView'] => $var4->get('lastPageView'), + ), + 'fetching a valid session with noFuss does not update the object info' +); + +$varTest->end; +$var4->end; + +##Test a valid fetch +$varTest = WebGUI::Session::Var->new($session, $varExpiring->getId, 1); + +cmp_deeply( + $varTest, + methods( + ['get', 'sessionId'] => $varExpiring->getId, + ['get', 'userId'] => 1, + ['get', 'adminOn'] => 0, + ['get', 'lastIP'] => '10.0.5.5', + ['get', 'lastPageView'] => $varExpiring->get('lastPageView'), + ['get', 'expires'] => $varExpiring->get('expires'), + ), + 'fetching a valid session with noFuss does not update the object info, even if it has expired' +); + +$varExpiring->end; +$varTest->end; + +my $varId4 = 'idDoesNotExist00779988'; +# '1234567890123456789012' +$varTest = WebGUI::Session::Var->new($session, $varId4, 1); +isa_ok($varTest, "WebGUI::Session::Var", "non-existant Id with noFuss returns a valid object..."); +is($varTest->getId, $varId4, "...and we got our requested Id"); + +$varTest->start(3, $varTest->getId); +is($varTest->get('userId'), 3, 'userId set via start'); +$varTest->start("", $varTest->getId); +is($varTest->get('userId'), 1, 'calling start with null userId returns default user (visitor)'); END: { - foreach my $varObj ($var, $var2) { + + $session->env->{_env} = $origEnv; + + foreach my $varObj ($var, $var2, $var3, $var4, $varExpiring, $varTest) { if (defined $varObj and ref $varObj eq 'WebGUI::Session::Var') { $varObj->end(); } } + $session->db->write("delete from userSession where sessionId=?",[$idToDelete]); }