Bug 17642: Add find_by_koha_field
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 16 Nov 2016 15:08:59 +0000 (15:08 +0000)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 18 Nov 2016 15:51:59 +0000 (15:51 +0000)
When we call search_by_koha_field with an authorised_value, we actually
expect only 1 value

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/AuthorisedValues.pm
t/db_dependent/AuthorisedValues.t

index 2a31699..55bf85d 100644 (file)
@@ -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(
index 4871712..bde3e2d 100644 (file)
@@ -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, );
     };
 };