From 68a900132e35384ef6447d99b9bb186b16ab2445 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 2 Aug 2022 11:54:59 +0000 Subject: [PATCH] Bug 29144: Add $contents->find_best_match, $library->opac_info Test plan: Run t/db_dependent/Koha/AdditionalContents.t Run t/db_dependent/Koha/Library.t Note: You may need to set ListOwnershipUponPatronDeletion to delete to fix a pending issue in patron->delete with lists. Signed-off-by: Marcel de Rooy Signed-off-by: Owen Leonard Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- Koha/AdditionalContents.pm | 35 +++++++++++++++++++++ Koha/Library.pm | 20 ++++++++++++ t/db_dependent/Koha/AdditionalContents.t | 29 ++++++++++++++++- t/db_dependent/Koha/Library.t | 40 +++++++++++++++++++++++- 4 files changed, 122 insertions(+), 2 deletions(-) diff --git a/Koha/AdditionalContents.pm b/Koha/AdditionalContents.pm index a33152b3ee..3ed9f42f41 100644 --- a/Koha/AdditionalContents.pm +++ b/Koha/AdditionalContents.pm @@ -110,6 +110,41 @@ sub search_for_display { return $self->SUPER::search({%$search_params, lang => 'default'}, { order_by => 'number'}); } +=head3 find_best_match + + Koha::AdditionalContents->find_best_match({ + category => , location => , lang => , library_id => + }); + + When choosing the best match, a match on lang and library is preferred. + Next a match on library and default lang. Then match on All libs and lang. + Finally a match with All libs and default lang. + +=cut + +sub find_best_match { + my ( $self, $params ) = @_; + my $library_id = $params->{library_id}; + my $lang = $params->{lang}; + + my $rs = $self->SUPER::search({ + category => $params->{category}, + location => $params->{location}, + lang => [ $lang, 'default' ], + branchcode => [ $library_id, undef ], + }); + + # Pick the best + my ( $alt1, $alt2, $alt3 ); + while( my $rec = $rs->next ) { + return $rec if $library_id && $rec->branchcode && $rec->branchcode eq $library_id && $lang && $rec->lang eq $lang; + $alt1 = $rec if !$alt1 && $library_id && $rec->branchcode && $rec->branchcode eq $library_id; + $alt2 = $rec if !$alt2 && $lang && $rec->lang eq $lang; + $alt3 = $rec if !$alt3; + } + return $alt1 // $alt2 // $alt3; +} + =head3 _type =cut diff --git a/Koha/Library.pm b/Koha/Library.pm index 48e125529b..20db2ca632 100644 --- a/Koha/Library.pm +++ b/Koha/Library.pm @@ -313,6 +313,26 @@ sub to_api_mapping { }; } +=head3 opac_info + + $library->opac_info({ lang => $lang }); + +Returns additional contents block OpacLibraryInfo for $lang or 'default'. + +Note: This replaces the former branches.opac_info column. + +=cut + +sub opac_info { + my ( $self, $params ) = @_; + return Koha::AdditionalContents->find_best_match({ + category => 'html_customizations', + location => 'OpacLibraryInfo', + lang => $params->{lang}, + library_id => $self->branchcode, + }); +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Koha/AdditionalContents.t b/t/db_dependent/Koha/AdditionalContents.t index bf515b1af1..67094cd48b 100755 --- a/t/db_dependent/Koha/AdditionalContents.t +++ b/t/db_dependent/Koha/AdditionalContents.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Test::Exception; use Koha::AdditionalContents; @@ -259,3 +259,30 @@ subtest '->search_for_display' => sub { $schema->storage->txn_rollback; }; + +subtest 'find_best_match' => sub { + plan tests => 3; + $schema->storage->txn_begin; + + my $library01 = $builder->build_object({ class => 'Koha::Libraries' }); + my $html01 = $builder->build_object({ + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef, lang => 'default' }, + }); + my $params = { category => 'html_customizations', location => 'test_best_match', lang => 'nl-NL' }; + is( Koha::AdditionalContents->find_best_match($params)->idnew, $html01->idnew, 'Found all branches, lang default' ); + + my $html02 = $builder->build_object({ + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef, lang => 'nl-NL' }, + }); + is( Koha::AdditionalContents->find_best_match($params)->idnew, $html02->idnew, 'Found all branches, lang nl-NL' ); + + $params->{ library_id } = $library01->id; + $html02->branchcode( $library01->id )->store; + is( Koha::AdditionalContents->find_best_match($params)->idnew, $html02->idnew, 'Found library01, lang nl-NL' ); + + # Note: find_best_match is tested further via $libary->opac_info; see t/db_dependent/Koha/Library.t + + $schema->storage->txn_rollback; +} diff --git a/t/db_dependent/Koha/Library.t b/t/db_dependent/Koha/Library.t index 23e88578b3..c6637ddfd9 100755 --- a/t/db_dependent/Koha/Library.t +++ b/t/db_dependent/Koha/Library.t @@ -19,9 +19,10 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Koha::Database; +use Koha::AdditionalContents; use Koha::SMTP::Servers; use t::lib::TestBuilder; @@ -93,3 +94,40 @@ subtest 'smtp_server() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'opac_info tests' => sub { + plan tests => 8; + $schema->storage->txn_begin; + my $library01 = $builder->build_object({ class => 'Koha::Libraries' }); + my $library02 = $builder->build_object({ class => 'Koha::Libraries' }); + + my $html01 = $builder->build_object({ + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, lang => 'default', content => '1', expirationdate => undef }, + }); + my $html02 = $builder->build_object({ + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, lang => 'default', content => '2', expirationdate => undef }, + }); + my $html03 = $builder->build_object({ + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, lang => 'nl-NL', content => '3', expirationdate => undef }, + }); + my $html04 = $builder->build_object({ + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, lang => 'fr-FR', content => '4', expirationdate => undef }, + }); + + # Start testing + is( $library01->opac_info->content, '2', 'specific library, default language' ); + is( $library01->opac_info({ lang => 'nl-NL' })->content, '3', 'specific library, specific language' ); + is( $library01->opac_info({ lang => 'nl-BE' })->content, '2', 'specific library, unknown language' ); + is( $library02->opac_info->content, '1', 'unknown library, default language' ); + is( $library02->opac_info({ lang => 'fr-FR' })->content, '4', 'unknown library, specific language' ); + is( $library02->opac_info({ lang => 'de-DE' })->content, '1', 'unknown library, unknown language' ); + $html01->delete; + is( $library02->opac_info, undef, 'unknown library, default language (after removing html01)' ); + is( $library02->opac_info({ lang => 'de-DE' }), undef, 'unknown library, unknown language (after removing html01)' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5