View | Details | Raw Unified | Return to bug 25375
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch.pm (-11 / +13 lines)
Lines 776-792 sub marc_records_to_documents { Link Here
776
        if ($self->index eq $BIBLIOS_INDEX) {
776
        if ($self->index eq $BIBLIOS_INDEX) {
777
            my ($tag, $code) = C4::Biblio::GetMarcFromKohaField('biblio.biblionumber');
777
            my ($tag, $code) = C4::Biblio::GetMarcFromKohaField('biblio.biblionumber');
778
            my $field = $record->field($tag);
778
            my $field = $record->field($tag);
779
            my $biblionumber = $field->is_control_field ? $field->data : $field->subfield($code);
779
            if ($field) {
780
            my $avail_items = Koha::Items->search({
780
                my $biblionumber = $field->is_control_field ? $field->data : $field->subfield($code);
781
                biblionumber => $biblionumber,
781
                my $avail_items = Koha::Items->search({
782
                onloan       => undef,
782
                    biblionumber => $biblionumber,
783
                notforloan   => 0,
783
                    onloan       => undef,
784
                withdrawn    => 0,
784
                    notforloan   => 0,
785
                itemlost     => 0,
785
                    withdrawn    => 0,
786
                damaged      => 0
786
                    itemlost     => 0,
787
            })->count;
787
                    damaged      => 0
788
788
                })->count;
789
            $record_document->{available} = $avail_items ? \1 : \0;
789
790
                $record_document->{available} = $avail_items ? \1 : \0;
791
            }
790
        }
792
        }
791
793
792
        push @record_documents, $record_document;
794
        push @record_documents, $record_document;
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch.t (-2 / +60 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 7;
20
use Test::More tests => 8;
21
use Test::Exception;
21
use Test::Exception;
22
22
23
use t::lib::Mocks;
23
use t::lib::Mocks;
Lines 30-35 use Try::Tiny; Link Here
30
use List::Util qw( any );
30
use List::Util qw( any );
31
31
32
use C4::AuthoritiesMarc qw( AddAuthority );
32
use C4::AuthoritiesMarc qw( AddAuthority );
33
use C4::Biblio;
33
34
34
use Koha::SearchEngine::Elasticsearch;
35
use Koha::SearchEngine::Elasticsearch;
35
use Koha::SearchEngine::Elasticsearch::Search;
36
use Koha::SearchEngine::Elasticsearch::Search;
Lines 929-932 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents with Inclu Link Here
929
    is_deeply($docs->[0]->{subject__sort}, ['Foo'], 'subject__sort should not include "See from"');
930
    is_deeply($docs->[0]->{subject__sort}, ['Foo'], 'subject__sort should not include "See from"');
930
};
931
};
931
932
933
subtest 'marc_records_to_documents should set the "available" field' => sub {
934
    plan tests => 8;
935
936
    t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
937
    my $dbh = C4::Context->dbh;
938
939
    my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
940
    $se->noop('_foreach_mapping');
941
942
    my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX });
943
944
    # sort_fields will call this and use the actual db values unless we call it first
945
    $see->get_elasticsearch_mappings();
946
947
    my $marc_record_1 = MARC::Record->new();
948
    $marc_record_1->leader('     cam  22      a 4500');
949
    $marc_record_1->append_fields(
950
        MARC::Field->new('245', '', '', a => 'Title'),
951
    );
952
    my ($biblionumber) = C4::Biblio::AddBiblio($marc_record_1, '', { defer_marc_save => 1 });
953
954
    my $docs = $see->marc_records_to_documents([$marc_record_1]);
955
    is_deeply($docs->[0]->{available}, \0, 'a biblio without items is not available');
956
957
    my $item = Koha::Item->new({
958
        biblionumber => $biblionumber,
959
    })->store();
960
961
    $docs = $see->marc_records_to_documents([$marc_record_1]);
962
    is_deeply($docs->[0]->{available}, \1, 'a biblio with one item that has no particular status is available');
963
964
    $item->notforloan(1)->store();
965
    $docs = $see->marc_records_to_documents([$marc_record_1]);
966
    is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is "notforloan" is not available');
967
968
    $item->set({ notforloan => 0, onloan => '2022-03-03' })->store();
969
    $docs = $see->marc_records_to_documents([$marc_record_1]);
970
    is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is on loan is not available');
971
972
    $item->set({ onloan => undef, withdrawn => 1 })->store();
973
    $docs = $see->marc_records_to_documents([$marc_record_1]);
974
    is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is withdrawn is not available');
975
976
    $item->set({ withdrawn => 0, itemlost => 1 })->store();
977
    $docs = $see->marc_records_to_documents([$marc_record_1]);
978
    is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is lost is not available');
979
980
    $item->set({ itemlost => 0, damaged => 1 })->store();
981
    $docs = $see->marc_records_to_documents([$marc_record_1]);
982
    is_deeply($docs->[0]->{available}, \0, 'a biblio with one item that is damaged is not available');
983
984
    my $item2 = Koha::Item->new({
985
        biblionumber => $biblionumber,
986
    })->store();
987
    $docs = $see->marc_records_to_documents([$marc_record_1]);
988
    is_deeply($docs->[0]->{available}, \1, 'a biblio with at least one item that has no particular status is available');
989
};
990
932
$schema->storage->txn_rollback;
991
$schema->storage->txn_rollback;
933
- 

Return to bug 25375