Bug 28772: Make validate_secret return 1|0
[koha.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 => 11;
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 $marc = MARC::Record->new;
47     my $results = [];
48
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; } );
59
60     # Case 1: Search engine does not return any results on controlnumber
61     is( $bib1->get_marc_host, undef, 'Empty MARC record' );
62     $marc->append_fields(
63         MARC::Field->new( '773', '', '', g => 'relpart', w => '(xyz)123' ),
64     );
65     is( $bib1->get_marc_host, undef, '773 looks fine, but no search results' );
66
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' );
75     # Add second 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' );
79     # Replace orgcode
80     ($marc->field('773'))[1]->update( w => '(abc)345' );
81     is( $bib1->get_marc_host, undef, 'No results for two 773s' );
82     # Test no_items flag
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' );
88     # Test list context
89     my @temp = $bib1->get_marc_host;
90     is( $temp[1], 'relpart2', 'Return $g in list context' );
91
92     # Case 3: Search engine returns more results
93     $results = [ 1, 2 ];
94     is( $bib1->get_marc_host, undef, 'get_marc_host returns undef for non-unique control number' );
95 };
96
97 sub mocked_search {
98     my $results = shift;
99     return ( undef, $results, scalar @$results );
100 }
101
102 $schema->storage->txn_rollback();