From 74530f8761f394e133ba0424bc3af339af7f1230 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 16 Nov 2016 17:23:23 +0100 Subject: [PATCH] Bug 17642: Add get_description_by_koha_field Most of the time we just need the descriptions (lib or opac_description), so let's add a new method for that and cache the descriptions in L1. Ideally we should cache it in L2 as well, but the AV code is not robust enough to allow that Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy --- Koha/AuthorisedValues.pm | 20 ++++++++++++++++++++ t/db_dependent/AuthorisedValues.t | 31 +++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index 55bf85d..ad68904 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -25,6 +25,7 @@ use Koha::Database; use Koha::AuthorisedValue; use Koha::MarcSubfieldStructures; +use Koha::Cache::Memory::Lite; use base qw(Koha::Objects); @@ -119,6 +120,25 @@ sub find_by_koha_field { return $av->count ? $av->next : undef; } +sub get_description_by_koha_field { + my ( $self, $params ) = @_; + my $frameworkcode = $params->{frameworkcode} || ''; + my $kohafield = $params->{kohafield}; + my $authorised_value = $params->{authorised_value}; + + return {} unless defined $authorised_value; + + my $memory_cache = Koha::Cache::Memory::Lite->get_instance; + my $cache_key = "Av_descriptions:$frameworkcode:$kohafield:$authorised_value"; + my $cached = $memory_cache->get_from_cache($cache_key); + return $cached if $cached; + + my $av = $self->find_by_koha_field($params); + my $descriptions = { lib => $av->lib, opac_description => $av->opac_description }; + $memory_cache->set_in_cache( $cache_key, $descriptions ); + return $descriptions; +} + sub categories { my ( $self ) = @_; my $rs = $self->_resultset->search( diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index bde3e2d..7295d41 100644 --- a/t/db_dependent/AuthorisedValues.t +++ b/t/db_dependent/AuthorisedValues.t @@ -118,8 +118,8 @@ is( @categories, 3, 'There should have 2 categories inserted' ); is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' ); is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' ); -subtest 'search_by_*_field + find_by_koha_field' => sub { - plan tests => 3; +subtest 'search_by_*_field + find_by_koha_field + search_for_descriptions' => sub { + plan tests => 4; my $loc_cat = Koha::AuthorisedValueCategories->find('LOC'); $loc_cat->delete if $loc_cat; my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } ); @@ -173,4 +173,31 @@ subtest 'search_by_*_field + find_by_koha_field' => sub { $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } ); is( $av, undef, ); }; + subtest 'get_description_by_koha_field' => sub { + plan tests => 3; + my $descriptions; + + # Test authorised_value = 0 + $descriptions = Koha::AuthorisedValues->get_description_by_koha_field( + { kohafield => 'items.restricted', authorised_value => 0 } ); + is_deeply( $descriptions, + { lib => $av_0->lib, opac_description => $av_0->lib_opac }, + ); + + # Test authorised_value = "" + $descriptions = Koha::AuthorisedValues->get_description_by_koha_field( + { kohafield => 'items.restricted', authorised_value => '' } ); + is_deeply( + $descriptions, + { + lib => $av_empty_string->lib, + opac_description => $av_empty_string->lib_opac + }, + ); + + # Test authorised_value = undef => we do not want to retrieve anything + $descriptions = Koha::AuthorisedValues->get_description_by_koha_field( + { kohafield => 'items.restricted', authorised_value => undef } ); + is_deeply( $descriptions, {}, ) ; # This could be arguable, we could return undef instead + }; }; -- 2.1.4