From 4b8e6139d16fba7f18fc6c981d2e7baafac7731d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 19 Apr 2017 16:14:37 -0300 Subject: [PATCH] Bug 18433: Use existing pattern for find_by_koha_field --- Koha/AuthorisedValueCategories.pm | 17 ++++++---- t/db_dependent/Koha/AuthorisedValueCategories.t | 44 ++++++++++++++----------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/Koha/AuthorisedValueCategories.pm b/Koha/AuthorisedValueCategories.pm index 767cb92..3a1dae0 100644 --- a/Koha/AuthorisedValueCategories.pm +++ b/Koha/AuthorisedValueCategories.pm @@ -49,13 +49,16 @@ sub find_by_koha_field { $frameworkcode //= ''; - my ($subfield) = Koha::MarcSubfieldStructures->search({ - frameworkcode => $frameworkcode, - kohafield => $kohafield, - authorised_value => { not => undef }, - }); - - return $subfield ? $class->find($subfield->authorised_value) : undef; + my $avc = $class->SUPER::search( + { 'marc_subfield_structures.frameworkcode' => $frameworkcode, + 'marc_subfield_structures.kohafield' => $kohafield, + 'marc_subfield_structures.authorised_value' => { not => undef }, + }, + { join => 'marc_subfield_structures', + distinct => 1, + } + ); + return $avc->count ? $avc->next : undef; } =head3 type diff --git a/t/db_dependent/Koha/AuthorisedValueCategories.t b/t/db_dependent/Koha/AuthorisedValueCategories.t index 99735ac..ce00b3d 100755 --- a/t/db_dependent/Koha/AuthorisedValueCategories.t +++ b/t/db_dependent/Koha/AuthorisedValueCategories.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 2; use Koha::MarcSubfieldStructures; use Koha::Database; @@ -33,27 +33,33 @@ BEGIN { my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; -my $category = Koha::AuthorisedValueCategories->find_or_create({ - category_name => 'TEST', -}); +subtest '->find_by_koha_field' => sub { + plan tests => 3; + my $category = Koha::AuthorisedValueCategories->find_or_create({ + category_name => 'TEST', + }); -Koha::MarcSubfieldStructures->search({ - frameworkcode => '', - kohafield => 'items.notforloan', -})->delete(); + Koha::MarcSubfieldStructures->search({ + frameworkcode => '', + kohafield => 'items.notforloan', + })->delete(); -my $subfield = Koha::MarcSubfieldStructures->find_or_create({ - frameworkcode => '', - tagfield => 999, - tagsubfield => 9, -}); -$subfield->authorised_value($category->category_name) - ->kohafield('items.notforloan') - ->store(); + my $avc = Koha::AuthorisedValueCategories->find_by_koha_field('items.notforloan'); + is($avc, undef, '->find_by_koha_field should return undef if no category is linked to this field'); -my $result = Koha::AuthorisedValueCategories->find_by_koha_field('items.notforloan'); + my $subfield = Koha::MarcSubfieldStructures->find_or_create({ + frameworkcode => '', + tagfield => 999, + tagsubfield => 9, + }); + $subfield->authorised_value($category->category_name) + ->kohafield('items.notforloan') + ->store(); -isa_ok($result, 'Koha::AuthorisedValueCategory', 'Result found'); -is($result->category_name, $category->category_name, 'Result is correct'); + $avc = Koha::AuthorisedValueCategories->find_by_koha_field('items.notforloan'); + + isa_ok($avc, 'Koha::AuthorisedValueCategory', '->find_by_koha_field should return a Koha::AuthorisedValueCategory'); + is($avc->category_name, $avc->category_name, '->find_by_koha_field should return the correct category'); +}; $schema->storage->txn_rollback; -- 2.9.3