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 $item1 = $builder->build_object({ class => 'Koha::Items', value => { biblionumber => $bib2->biblionumber } });
47 my $marc = MARC::Record->new;
50 # Lets mock! Simulate search engine response and biblio metadata call.
51 my $metadata = Test::MockObject->new;
52 $metadata->mock( 'record', sub { return $marc; } );
53 my $meta_mod = Test::MockModule->new( 'Koha::Biblio' );
54 $meta_mod->mock( 'metadata', sub { return $metadata; } );
55 my $engine = Test::MockObject->new;
56 $engine->mock( 'simple_search_compat', sub { return ( undef, $results, scalar @$results ); } );
57 $engine->mock( 'extract_biblionumber', sub { return $results->[0]; } );
58 my $search_mod = Test::MockModule->new( 'Koha::SearchEngine::Search' );
59 $search_mod->mock( 'new', sub { return $engine; } );
61 # Case 1: Search engine does not return any results on controlnumber
62 is( $bib1->get_marc_host, undef, 'Empty MARC record' );
64 MARC::Field->new( '773', '', '', g => 'relpart', w => '(xyz)123' ),
66 is( $bib1->get_marc_host, undef, '773 looks fine, but no search results' );
68 # Case 2: Search engine returns (at maximum) one result
69 $results = [ $bib1->biblionumber ]; # will be found because 773w is in shape
70 my $host = $bib1->get_marc_host;
71 is( ref( $host ), 'Koha::Biblio', 'Correct object returned' );
72 is( $host->biblionumber, $bib1->biblionumber, 'Check biblionumber' );
73 $marc->field('773')->update( w => '(xyz) bad data' ); # causes no results
74 $host = $bib1->get_marc_host;
75 is( $bib1->get_marc_host, undef, 'No results for bad 773' );
77 t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 1 );
79 $marc->field('773')->update( t => 'title' );
80 $marc->field('773')->delete_subfield( code => 'w' );
81 $marc->field('773')->update( '0' => $bib2->biblionumber );
82 $host = $bib1->get_marc_host;
83 is( $host->biblionumber, $bib2->biblionumber, 'Found host biblio using 773$0 biblionumber' );
85 $marc->field('773')->delete_subfield( code => '0' );
86 $marc->field('773')->update( '9' => $item1->itemnumber );
87 $host = $bib1->get_marc_host;
88 is( $host->biblionumber, $bib2->biblionumber, 'Found host item using 773$9 itemnumber' );
90 $marc->field('773')->delete_subfield( code => '9' );
91 my ( $relatedparts, $info );
92 ( $host, $relatedparts, $info ) = $bib1->get_marc_host;
93 is( $host, undef, 'No Koha Biblio object returned with no $w' );
94 is( $info, "title, relpart", '773$atg returned when no $w' );
96 my $host_only = $bib1->get_marc_host_only;
97 is_deeply( $host_only, $host, "Host only retrieved successfully" );
98 my $relatedparts_only = $bib1->get_marc_relatedparts_only;
99 is_deeply( $relatedparts_only, $relatedparts, "Related parts only retrieved successfully" );
100 my $hostinfo_only = $bib1->get_marc_hostinfo_only;
101 is_deeply( $hostinfo_only, $info, "Host info only retrieved successfully");
103 $marc->field('773')->delete_subfield( code => 't' ); # restore
106 $marc->append_fields( MARC::Field->new( '773', '', '', g => 'relpart2', w => '234' ) );
107 $host = $bib1->get_marc_host;
108 is( $host->biblionumber, $bib1->biblionumber, 'Result triggered by second 773' );
110 ($marc->field('773'))[1]->update( w => '(abc)345' );
111 is( $bib1->get_marc_host, undef, 'No results for two 773s' );
113 ($marc->field('773'))[1]->update( w => '234' ); # restore
114 $host = $bib1->get_marc_host({ no_items => 1 });
115 is( $host->biblionumber, $bib1->biblionumber, 'Record found with no_items' );
116 $builder->build({ source => 'Item', value => { biblionumber => $bib1->biblionumber } });
117 is( $bib1->get_marc_host({ no_items => 1 }), undef, 'Record not found with no_items flag after adding one item' );
119 my @temp = $bib1->get_marc_host;
120 is( $temp[1], 'relpart2', 'Return $g in list context' );
122 # Case 3: Search engine returns more results
124 is( $bib1->get_marc_host, undef, 'get_marc_host returns undef for non-unique control number' );
129 return ( undef, $results, scalar @$results );
132 $schema->storage->txn_rollback();