From 17297f4fe657b58b9148104c51c66b4237d6db05 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Fri, 20 Nov 2009 11:33:49 -0800 Subject: [PATCH] Fix cache setting and cleaning up issues in the Snippet. Fixes bug #11259 --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Asset/Snippet.pm | 35 +++++++++++++++++++++++++++++++---- t/Asset/Snippet.t | 19 +++++++++++++++++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index bc35c272b..b3333356b 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -4,6 +4,7 @@ - fixed #11249: Recaptcha https bug - fixed #11200: Navigation in AssetProxy cached in browser - fixed #11143: cancel button + - fixed #11259: Snippet content never cached 7.8.5 - added the EMS submission subsystem diff --git a/lib/WebGUI/Asset/Snippet.pm b/lib/WebGUI/Asset/Snippet.pm index f93698dc3..1b8314837 100644 --- a/lib/WebGUI/Asset/Snippet.pm +++ b/lib/WebGUI/Asset/Snippet.pm @@ -176,6 +176,30 @@ sub exportGetUrlAsPath { #------------------------------------------------------------------- +=head2 getCache ( $calledAsWebMethod ) + +Overrides the base method to handle Snippet specific caching. + +=head3 $calledAsWebMethod + +If this is true, then change the cache key. + +=cut + +sub getCache { + my $self = shift; + my $calledAsWebMethod = shift; + my $session = $self->session; + my $cacheKey = "view_".$calledAsWebMethod.'_'.$self->getId; + if ($session->env->sslRequest) { + $cacheKey .= '_ssl'; + } + my $cache = WebGUI::Cache->new($session, $cacheKey); + return $cache; +} + +#------------------------------------------------------------------- + =head2 getToolbar ( ) Returns a toolbar with a set of icons that hyperlink to functions that delete, edit, promote, demote, cut, and copy. @@ -253,6 +277,8 @@ sub purgeCache { WebGUI::Cache->new($self->session,"view__".$self->getId)->delete; WebGUI::Cache->new($self->session,"view_1_".$self->getId)->delete; + WebGUI::Cache->new($self->session,"view__".$self->getId."_ssl")->delete; + WebGUI::Cache->new($self->session,"view_1_".$self->getId."_ssl")->delete; $self->SUPER::purgeCache(); } @@ -270,16 +296,16 @@ toolbar if in adminMode. =cut sub view { - my $self = shift; + my $self = shift; my $calledAsWebMethod = shift; - my $session = $self->session; + my $session = $self->session; my $versionTag = WebGUI::VersionTag->getWorking($session, 1); my $noCache = $session->var->isAdminOn || $self->get("cacheTimeout") <= 10 || ($versionTag && $versionTag->getId eq $self->get("tagId")); unless ($noCache) { - my $cache = $self->getCache; + my $cache = $self->getCache($calledAsWebMethod); my $out = $cache->get if defined $cache; return $out if $out; } @@ -293,7 +319,8 @@ sub view { } WebGUI::Macro::process($session,\$output); unless ($noCache) { - WebGUI::Cache->new($session,"view_".$calledAsWebMethod."_".$self->getId)->set($output,$self->get("cacheTimeout")); + my $cache = $self->getCache($calledAsWebMethod); + $cache->set($output,$self->get("cacheTimeout")); } return $output; } diff --git a/t/Asset/Snippet.t b/t/Asset/Snippet.t index e76527b81..d56a21510 100644 --- a/t/Asset/Snippet.t +++ b/t/Asset/Snippet.t @@ -16,14 +16,14 @@ use lib "$FindBin::Bin/../lib"; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 16; # increment this value for each test you create +use Test::More tests => 18; # increment this value for each test you create use WebGUI::Asset::Snippet; my $session = WebGUI::Test->session; my $node = WebGUI::Asset->getImportNode($session); my $versionTag = WebGUI::VersionTag->getWorking($session); $versionTag->set({name=>"Snippet Test"}); -WebGUI::Test->tagsToRollback($versionTag); +addToCleanup($versionTag); my $snippet = $node->addChild({className=>'WebGUI::Asset::Snippet'}); # Test for a sane object type @@ -86,6 +86,21 @@ is($snippet->view(), 'WebGUI', 'Interpolating macros in works with template in t my $empty = $node->addChild( { className => 'WebGUI::Asset::Snippet', } ); is($empty->www_view, 'empty', 'www_view: snippet with no content returns "empty"'); +#---------------------------------------------------------------------- +#Check caching + +##Set up the snippet to do caching +$snippet->update({ + cacheTimeout => 100, + snippet => 'Cache test: ^#;', +}); + +$versionTag->commit; + +is $snippet->view, 'Cache test: 1', 'validate snippet content and set cache'; +$session->user({userId => 3}); +is $snippet->view(1), 'Cache test: 3', 'receive uncached content since view was passed the webMethod flag'; + TODO: { local $TODO = "Tests to make later"; ok(0, 'Test indexContent method');