From 4bbd35da92c9dd8469eb9a74d87556a9e3379366 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Tue, 1 Apr 2014 17:18:22 +1300 Subject: [PATCH] Bug 11998 - add test case, fix issue found Adds a test case with improved coverage of the caching functionality. Fixes a bug that was uncovered by writing the test case. Signed-off-by: Brendan Gallagher --- C4/Context.pm | 12 ++++++++---- t/db_dependent/sysprefs.t | 13 ++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 87f48fa..8b024e6 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -521,8 +521,8 @@ sub ModZebrations { =head2 _cache_preference($variable, [$value]) - _cache_preferences('opacusercss', $css); - my $val = _cach_preferences('opacusercss'); + _cache_preference('opacusercss', $css); + my $val = _cache_preference('opacusercss'); An interface into the syspref caching. One argument to get a value, two to set it. The single argument version will return C if the cache doesn't @@ -542,6 +542,7 @@ sub _cache_preference { my $memcached = memcached(); my $memcached_prefix = "_syspref_"; if ( scalar(@_) == 1 ) { + # This means we want to fetch from the cache my ($var) = @_; if ($memcached) { my $val = $memcached->get( $memcached_prefix . lc($var) ); @@ -557,6 +558,7 @@ sub _cache_preference { } } elsif ( scalar(@_) == 2 ) { + # Set something in the cache my ( $var, $val ) = @_; if ($memcached) { if ( !defined($val) ) { @@ -573,6 +575,8 @@ sub _cache_preference { } $syspref_cache{ lc $var } = [ time, $val ]; } + } else { + confess "Invalid arguments to _cache_preference: " . join(', ', @_); } } @@ -741,9 +745,9 @@ sub delete_preference { my $sth = $dbh->prepare("DELETE FROM systempreferences WHERE variable=?"); my $res = $sth->execute($var); if ($res) { - C4::Context->set_preference($var, undef); + _cache_preference($var, undef); logaction( 'SYSTEMPREFERENCE', 'DELETE', undef, - $var . " | " . ( $var // 'undefined' ) ); + $var . " | " . ( $val // 'undefined' ) ); return 1; } warn "Unable to delete syspref: " . $sth->errstr; diff --git a/t/db_dependent/sysprefs.t b/t/db_dependent/sysprefs.t index ca88740..a2ce98e 100755 --- a/t/db_dependent/sysprefs.t +++ b/t/db_dependent/sysprefs.t @@ -19,7 +19,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 6; use C4::Context; # Start transaction @@ -43,4 +43,15 @@ is(C4::Context->preference('opacheader'), 'system preference value overridden from environment' ); +C4::Context->set_preference('testpreference', 'abc'); +C4::Context->delete_preference('testpreference'); +is(C4::Context->preference('testpreference'), undef, 'deleting preferences'); + +C4::Context->set_preference('testpreference', 'def'); +# Delete from the database, it should still be in cache +$dbh->do("DELETE FROM systempreferences WHERE variable='testpreference'"); +is(C4::Context->preference('testpreference'), 'def', 'caching preferences'); +C4::Context->clear_syspref_cache(); +is(C4::Context->preference('testpreference'), undef, 'clearing preference cache'); + $dbh->rollback; -- 2.1.0