From eea7b941c9c2d5b0b01af88cd54a1ea71ede2298 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 25 Sep 2006 03:49:25 +0000 Subject: [PATCH] A few more tests to kick coverage up to 100 on WebGUI::Session::Setting Convert it to use placeholders as well. More WebGUI::Session::Url tests, and some bug fixes: getRefererUrl returned parameters and accepted weird methods that looked like http but weren't. --- lib/WebGUI/Session/Setting.pm | 6 ++-- lib/WebGUI/Session/Url.pm | 4 +-- t/Session/Setting.t | 5 +++- t/Session/Url.t | 54 +++++++++++++++++++++++++++++++++-- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/lib/WebGUI/Session/Setting.pm b/lib/WebGUI/Session/Setting.pm index 536c7240c..bedf254e6 100644 --- a/lib/WebGUI/Session/Setting.pm +++ b/lib/WebGUI/Session/Setting.pm @@ -67,7 +67,7 @@ sub add { my $name = shift; my $value = shift; $self->{_settings}{$name} = $value; - $self->session->db->write("insert into settings (name,value) values (".$self->session->db->quote($name).",".$self->session->db->quote($value).")"); + $self->session->db->write("insert into settings (name,value) values (?,?)",[$name, $value]); } #------------------------------------------------------------------- @@ -136,7 +136,7 @@ sub remove { my $self = shift; my $name = shift; delete $self->{_settings}{$name}; - $self->session->db->write("delete from settings where name=".$self->session->db->quote($name)); + $self->session->db->write("delete from settings where name=?",[$name]); } @@ -175,7 +175,7 @@ sub set { my $name = shift; my $value = shift; $self->{_settings}{$name} = $value; - $self->session->db->write("update settings set value=".$self->session->db->quote($value)." where name=".$self->session->db->quote($name)); + $self->session->db->write("update settings set value=? where name=?",[$value, $name]); } diff --git a/lib/WebGUI/Session/Url.pm b/lib/WebGUI/Session/Url.pm index fd10108bf..2e43bd3fb 100644 --- a/lib/WebGUI/Session/Url.pm +++ b/lib/WebGUI/Session/Url.pm @@ -189,8 +189,8 @@ sub getRefererUrl { return undef unless ($referer); my $url = $referer; my $gateway = $self->session->config->get("gateway"); - $url =~ s/htt\w+\:\/\/[A-Za-z0-9\.\-]+$gateway\/*(\S*)/$1/;; - if ($url eq $referer) { + $url =~ s{https?://[A-Za-z0-9\.-]+$gateway/*([^?]*)\??.*$}{$1}; + if ($url eq $referer) { ##s/// failed return undef; } else { return $url; diff --git a/t/Session/Setting.t b/t/Session/Setting.t index 6d778a2e1..84c7dd627 100644 --- a/t/Session/Setting.t +++ b/t/Session/Setting.t @@ -15,7 +15,7 @@ use lib "$FindBin::Bin/../lib"; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 4; # increment this value for each test you create +use Test::More tests => 6; # increment this value for each test you create my $session = WebGUI::Test->session; @@ -26,6 +26,9 @@ is($session->setting->get("test"), "XXX", "get()"); $session->setting->set("test","YYY"); my ($value) = $session->db->quickArray("select value from settings where name='test'"); is($value, 'YYY', "set()"); +is($session->setting->get("test"), 'YYY', 'set() also updates object cache'); $session->setting->remove("test"); my ($value) = $session->db->quickArray("select value from settings where name='test'"); is($value, undef, "delete()"); + +isa_ok($session->setting->session, 'WebGUI::Session', 'session method returns a session object'); diff --git a/t/Session/Url.t b/t/Session/Url.t index fb543e304..ba62589cd 100644 --- a/t/Session/Url.t +++ b/t/Session/Url.t @@ -15,8 +15,43 @@ use lib "$FindBin::Bin/../lib"; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 19; # increment this value for each test you create +my @getRefererUrlTests = ( + { + input => undef, + output => undef, + comment => 'getRerererUrl returns undef unless there is a referrer', + }, + { + input => 'http://www.domain.com/myUrl.html', + output => 'myUrl.html', + comment => 'getRefererUrl returns the url minus the gateway', + }, + { + input => 'http://www.domain.com/myUrl.html?op=switchAdminOn', + output => 'myUrl.html', + comment => 'getRefererUrl returns the url minus the gateway', + }, + { + input => 'https://www.site.com/myUrl.html', + output => 'myUrl.html', + comment => 'getRefererUrl handles SSL urls', + }, + { + input => 'itunes://www.site.com/myUrl.html', + output => undef, + comment => 'getRefererUrl only handles HTTP protocols', + }, + { + input => 'http://site/myUrl.html', + output => 'myUrl.html', + comment => 'getRefererUrl will also parse weird URLs', + }, +); + +use Test::More; +use Test::MockObject::Extends; use Test::MockObject; +plan tests => 21 + scalar(@getRefererUrlTests); my $session = WebGUI::Test->session; @@ -28,14 +63,12 @@ $session->setting->set('preventProxyCache', 0) if ($preventProxyCache); my $url = 'http://localhost.localdomain/foo'; my $url2; - $url2 = $session->url->append($url,'a=b'); is( $url2, $url.'?a=b', 'append first pair'); $url2 = $session->url->append($url2,'c=d'); is( $url2, $url.'?a=b;c=d', 'append second pair'); - $session->config->{_config}->{'gateway'} = '/'; is ( $session->config->get('gateway'), '/', 'Set gateway for downstream tests'); @@ -105,3 +138,18 @@ is ($session->url->page, '/path1/file1', 'page with no args returns getRequested $url2 = 'http://'.$session->config->get('sitename')->[0].'/path1/file1'; is ($session->url->page('',1), $url2, 'page, withFullUrl includes method and sitename'); + +##getReferrerUrl tests + +our %mockEnv = (HTTP_REFERER => undef); + +$session->{_env}->{_env} = \%mockEnv; + +$mockEnv{'HTTP_REFERER'} = 'test'; + +is($session->env->get('HTTP_REFERER'), 'test', 'testing MockObject'); + +foreach my $test (@getRefererUrlTests) { + $mockEnv{HTTP_REFERER} = $test->{input}; + is($session->url->getRefererUrl, $test->{output}, $test->{comment}); +}