3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 1;
21 use Data::Dumper qw/Dumper/;
27 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36 our $builder = t::lib::TestBuilder->new;
38 subtest 'get_marc_host' => sub {
41 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
42 t::lib::Mocks::mock_preference( 'MARCOrgCode', 'xyz' );
44 my $bib1 = $builder->build_object({ class => 'Koha::Biblios' });
45 my $bib2 = $builder->build_object({ class => 'Koha::Biblios' });
46 my $marc = MARC::Record->new;
49 # Lets mock! Simulate search engine response and biblio metadata call.
50 my $metadata = Test::MockObject->new;
51 $metadata->mock( 'record', sub { return $marc; } );
52 my $meta_mod = Test::MockModule->new( 'Koha::Biblio' );
53 $meta_mod->mock( 'metadata', sub { return $metadata; } );
54 my $engine = Test::MockObject->new;
55 $engine->mock( 'simple_search_compat', sub { return ( undef, $results, scalar @$results ); } );
56 $engine->mock( 'extract_biblionumber', sub { return $results->[0]; } );
57 my $search_mod = Test::MockModule->new( 'Koha::SearchEngine::Search' );
58 $search_mod->mock( 'new', sub { return $engine; } );
60 # Case 1: Search engine does not return any results on controlnumber
61 is( $bib1->get_marc_host, undef, 'Empty MARC record' );
63 MARC::Field->new( '773', '', '', g => 'relpart', w => '(xyz)123' ),
65 is( $bib1->get_marc_host, undef, '773 looks fine, but no search results' );
67 # Case 2: Search engine returns (at maximum) one result
68 $results = [ $bib1->biblionumber ]; # will be found because 773w is in shape
69 my $host = $bib1->get_marc_host;
70 is( ref( $host ), 'Koha::Biblio', 'Correct object returned' );
71 is( $host->biblionumber, $bib1->biblionumber, 'Check biblionumber' );
72 $marc->field('773')->update( w => '(xyz) bad data' ); # causes no results
73 $host = $bib1->get_marc_host;
74 is( $bib1->get_marc_host, undef, 'No results for bad 773' );
76 $marc->append_fields( MARC::Field->new( '773', '', '', g => 'relpart2', w => '234' ) );
77 $host = $bib1->get_marc_host;
78 is( $host->biblionumber, $bib1->biblionumber, 'Result triggered by second 773' );
80 ($marc->field('773'))[1]->update( w => '(abc)345' );
81 is( $bib1->get_marc_host, undef, 'No results for two 773s' );
83 ($marc->field('773'))[1]->update( w => '234' ); # restore
84 $host = $bib1->get_marc_host({ no_items => 1 });
85 is( $host->biblionumber, $bib1->biblionumber, 'Record found with no_items' );
86 $builder->build({ source => 'Item', value => { biblionumber => $bib1->biblionumber } });
87 is( $bib1->get_marc_host({ no_items => 1 }), undef, 'Record not found with no_items flag after adding one item' );
89 my @temp = $bib1->get_marc_host;
90 is( $temp[1], 'relpart2', 'Return $g in list context' );
92 # Case 3: Search engine returns more results
94 is( $bib1->get_marc_host, undef, 'get_marc_host returns undef for non-unique control number' );
99 return ( undef, $results, scalar @$results );
102 $schema->storage->txn_rollback();