Bug 29234: Further clean Z3950 Tests
[koha-ffzg.git] / t / db_dependent / Koha / Biblio / host_record.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21 use Data::Dumper qw/Dumper/;
22 use MARC::Field;
23 use MARC::Record;
24 use Test::MockModule;
25 use Test::MockObject;
26
27 use t::lib::TestBuilder;
28 use t::lib::Mocks;
29 use Koha::Database;
30 use Koha::Biblios;
31 use C4::Biblio;
32
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36 our $builder = t::lib::TestBuilder->new;
37
38 subtest 'get_marc_host' => sub {
39     plan tests => 18;
40
41     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
42     t::lib::Mocks::mock_preference( 'MARCOrgCode', 'xyz' );
43
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;
48     my $results = [];
49
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; } );
60
61     # Case 1: Search engine does not return any results on controlnumber
62     is( $bib1->get_marc_host, undef, 'Empty MARC record' );
63     $marc->append_fields(
64         MARC::Field->new( '773', '', '', g => 'relpart', w => '(xyz)123' ),
65     );
66     is( $bib1->get_marc_host, undef, '773 looks fine, but no search results' );
67
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' );
76
77     t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 1 );
78     # no $w
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' );
84
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' );
89
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' );
95
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");
102
103     $marc->field('773')->delete_subfield( code => 't' ); # restore
104
105     # Add second 773
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' );
109     # Replace orgcode
110     ($marc->field('773'))[1]->update( w => '(abc)345' );
111     is( $bib1->get_marc_host, undef, 'No results for two 773s' );
112     # Test no_items flag
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' );
118     # Test list context
119     my @temp = $bib1->get_marc_host;
120     is( $temp[1], 'relpart2', 'Return $g in list context' );
121
122     # Case 3: Search engine returns more results
123     $results = [ 1, 2 ];
124     is( $bib1->get_marc_host, undef, 'get_marc_host returns undef for non-unique control number' );
125 };
126
127 sub mocked_search {
128     my $results = shift;
129     return ( undef, $results, scalar @$results );
130 }
131
132 $schema->storage->txn_rollback();