From 9b05b534b947ab313c929192133a59d01cfd9e07 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 26 Oct 2021 15:16:54 +0200 Subject: [PATCH] Bug 11175: Add tests Add coverage for the message set by get_marc_components when the search fails. Also cleans a bit the weird return comparisons. Signed-off-by: Jonathan Druart --- Koha/Biblio.pm | 2 +- t/db_dependent/Koha/Biblio.t | 32 ++++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 2a1d67a993..29ba22d502 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -505,7 +505,7 @@ sub get_marc_components { if( $error || $@ ) { $error //= q{}; $error .= $@ if $@; - warn "Warning from simple_search_compat: $error"; + warn "Warning from simple_search_compat: '$error'"; $self->add_message( { type => 'error', diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 8b8f963588..218015bec6 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -18,6 +18,7 @@ use Modern::Perl; use Test::More tests => 18; +use Test::Warn; use C4::Biblio qw( AddBiblio ModBiblio ModBiblioMarc ); use Koha::Database; @@ -26,6 +27,7 @@ use Koha::Acquisition::Orders; use Koha::AuthorisedValueCategories; use Koha::AuthorisedValues; use Koha::MarcSubfieldStructures; +use Koha::Exceptions::Exception; use MARC::Field; use MARC::Record; @@ -509,7 +511,7 @@ subtest 'suggestions() tests' => sub { subtest 'get_marc_components() tests' => sub { - plan tests => 3; + plan tests => 5; $schema->storage->txn_begin; @@ -519,12 +521,12 @@ subtest 'get_marc_components() tests' => sub { my $search_mod = Test::MockModule->new( 'Koha::SearchEngine::Zebra::Search' ); $search_mod->mock( 'simple_search_compat', \&search_component_record2 ); - my @components = $host_biblio->get_marc_components; - is( ref(\@components), 'ARRAY', 'Return type is correct' ); + my $components = $host_biblio->get_marc_components; + is( ref($components), 'ARRAY', 'Return type is correct' ); is_deeply( - [@components], - [[]], + $components, + [], '->get_marc_components returns an empty ARRAY' ); @@ -534,11 +536,29 @@ subtest 'get_marc_components() tests' => sub { is_deeply( $host_biblio->get_marc_components, - [($component_record)], + [$component_record], '->get_marc_components returns the related component part record' ); $search_mod->unmock( 'simple_search_compat'); + $search_mod->mock( 'simple_search_compat', + sub { Koha::Exceptions::Exception->throw("error searching analytics") } + ); + warning_like { $components = $host_biblio->get_marc_components } + qr{^Warning from simple_search_compat: 'error searching analytics'}; + + is_deeply( + $host_biblio->messages, + [ + { + type => 'error', + message => 'component_search', + payload => "error searching analytics" + } + ] + ); + $search_mod->unmock( 'simple_search_compat'); + $schema->storage->txn_rollback; }; -- 2.20.1