From f48c5bbb8b10ada5e10c63040a88693079b815f2 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 3 Mar 2022 11:18:25 +0100 Subject: [PATCH] Bug 25375: Add tests for the "available" ES field MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Nick Clemens Signed-off-by: Joonas Kylmälä Signed-off-by: Tomas Cohen Arazi --- Koha/SearchEngine/Elasticsearch.pm | 24 ++++---- .../Koha/SearchEngine/Elasticsearch.t | 61 ++++++++++++++++++- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index 20462ccd03..b2273c5ace 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -776,17 +776,19 @@ sub marc_records_to_documents { if ($self->index eq $BIBLIOS_INDEX) { my ($tag, $code) = C4::Biblio::GetMarcFromKohaField('biblio.biblionumber'); my $field = $record->field($tag); - my $biblionumber = $field->is_control_field ? $field->data : $field->subfield($code); - my $avail_items = Koha::Items->search({ - biblionumber => $biblionumber, - onloan => undef, - notforloan => 0, - withdrawn => 0, - itemlost => 0, - damaged => 0 - })->count; - - $record_document->{available} = $avail_items ? \1 : \0; + if ($field) { + my $biblionumber = $field->is_control_field ? $field->data : $field->subfield($code); + my $avail_items = Koha::Items->search({ + biblionumber => $biblionumber, + onloan => undef, + notforloan => 0, + withdrawn => 0, + itemlost => 0, + damaged => 0 + })->count; + + $record_document->{available} = $avail_items ? \1 : \0; + } } push @record_documents, $record_document; diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch.t index 0509328135..d0a11d924d 100755 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 8; use Test::Exception; use t::lib::Mocks; @@ -30,6 +30,7 @@ use Try::Tiny; use List::Util qw( any ); use C4::AuthoritiesMarc qw( AddAuthority ); +use C4::Biblio; use Koha::SearchEngine::Elasticsearch; use Koha::SearchEngine::Elasticsearch::Search; @@ -972,4 +973,62 @@ subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents with Inclu is_deeply($docs->[0]->{subject__sort}, ['Foo'], 'subject__sort should not include "See from"'); }; +subtest 'marc_records_to_documents should set the "available" field' => sub { + plan tests => 8; + + t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); + my $dbh = C4::Context->dbh; + + my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch'); + $se->noop('_foreach_mapping'); + + my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }); + + # sort_fields will call this and use the actual db values unless we call it first + $see->get_elasticsearch_mappings(); + + my $marc_record_1 = MARC::Record->new(); + $marc_record_1->leader(' cam 22 a 4500'); + $marc_record_1->append_fields( + MARC::Field->new('245', '', '', a => 'Title'), + ); + my ($biblionumber) = C4::Biblio::AddBiblio($marc_record_1, '', { defer_marc_save => 1 }); + + my $docs = $see->marc_records_to_documents([$marc_record_1]); + is_deeply($docs->[0]->{available}, \0, 'a biblio without items is not available'); + + my $item = Koha::Item->new({ + biblionumber => $biblionumber, + })->store(); + + $docs = $see->marc_records_to_documents([$marc_record_1]); + is_deeply($docs->[0]->{available}, \1, 'a biblio with one item that has no particular status is available'); + + $item->notforloan(1)->store(); + $docs = $see->marc_records_to_documents([$marc_record_1]); + is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is "notforloan" is not available'); + + $item->set({ notforloan => 0, onloan => '2022-03-03' })->store(); + $docs = $see->marc_records_to_documents([$marc_record_1]); + is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is on loan is not available'); + + $item->set({ onloan => undef, withdrawn => 1 })->store(); + $docs = $see->marc_records_to_documents([$marc_record_1]); + is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is withdrawn is not available'); + + $item->set({ withdrawn => 0, itemlost => 1 })->store(); + $docs = $see->marc_records_to_documents([$marc_record_1]); + is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is lost is not available'); + + $item->set({ itemlost => 0, damaged => 1 })->store(); + $docs = $see->marc_records_to_documents([$marc_record_1]); + is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is damaged is not available'); + + my $item2 = Koha::Item->new({ + biblionumber => $biblionumber, + })->store(); + $docs = $see->marc_records_to_documents([$marc_record_1]); + is_deeply($docs->[0]->{available}, \1, 'a biblio with at least one item that has no particular status is available'); +}; + $schema->storage->txn_rollback; -- 2.39.5