From 864aa5805dfb68dde762796d6cacd2f7959d15c5 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 16 Nov 2016 15:08:59 +0000 Subject: [PATCH] Bug 17642: Add find_by_koha_field When we call search_by_koha_field with an authorised_value, we actually expect only 1 value Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Signed-off-by: Kyle M Hall --- Koha/AuthorisedValues.pm | 20 ++++++++++++++++++-- t/db_dependent/AuthorisedValues.t | 25 +++++++++++++------------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index 2a31699c5c..55bf85dac5 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -87,7 +87,6 @@ sub search_by_koha_field { my $frameworkcode = $params->{frameworkcode} || ''; my $kohafield = $params->{kohafield}; my $category = $params->{category}; - #my $authorised_value = $params->{authorised_value}; return unless $kohafield; @@ -95,7 +94,6 @@ sub search_by_koha_field { { 'marc_subfield_structures.frameworkcode' => $frameworkcode, 'marc_subfield_structures.kohafield' => $kohafield, ( defined $category ? ( category_name => $category ) : () ), - ( exists $params->{authorised_value} ? ( 'me.authorised_value' => $params->{authorised_value} ) : () ), }, { join => { category => 'marc_subfield_structures' }, distinct => 1, @@ -103,6 +101,24 @@ sub search_by_koha_field { ); } +sub find_by_koha_field { + my ( $self, $params ) = @_; + my $frameworkcode = $params->{frameworkcode} || ''; + my $kohafield = $params->{kohafield}; + my $authorised_value = $params->{authorised_value}; + + my $av = $self->SUPER::search( + { 'marc_subfield_structures.frameworkcode' => $frameworkcode, + 'marc_subfield_structures.kohafield' => $kohafield, + 'me.authorised_value' => $authorised_value, + }, + { join => { category => 'marc_subfield_structures' }, + distinct => 1, + } + ); + return $av->count ? $av->next : undef; +} + sub categories { my ( $self ) = @_; my $rs = $self->_resultset->search( diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index 48717121ae..bde3e2d98b 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' => sub { - plan tests => 2; +subtest 'search_by_*_field + find_by_koha_field' => sub { + plan tests => 3; my $loc_cat = Koha::AuthorisedValueCategories->find('LOC'); $loc_cat->delete if $loc_cat; my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } ); @@ -151,7 +151,7 @@ subtest 'search_by_*_field' => sub { is( $avs->next->authorised_value, 'location_1', ); }; subtest 'search_by_koha_field' => sub { - plan tests => 8; + plan tests => 3; my $avs; $avs = Koha::AuthorisedValues->search_by_koha_field(); is ( $avs, undef ); @@ -159,17 +159,18 @@ subtest 'search_by_*_field' => sub { is( $avs->count, 3, ); is( $avs->next->authorised_value, 'location_1', ); + }; + subtest 'find_by_koha_field' => sub { + plan tests => 3; # Test authorised_value = 0 - $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.restricted', authorised_value => 0 } ); - is( $avs->count, 1, ); - is( $avs->next->lib, $av_0->lib, ); + my $av; + $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => 0 } ); + is( $av->lib, $av_0->lib, ); # Test authorised_value = "" - $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.restricted', authorised_value => '' } ); - is( $avs->count, 1, ); - is( $avs->next->lib, $av_empty_string->lib, ); + $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => '' } ); + is( $av->lib, $av_empty_string->lib, ); # Test authorised_value = undef => we do not want to retrieve anything - $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } ); - is( $avs->count, 0, ); - + $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } ); + is( $av, undef, ); }; }; -- 2.39.5