Bug 18624: Regression test for 'any' vs 'all'
[koha.git] / t / db_dependent / Koha / Authorities.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 6;
23 use MARC::Field;
24 use MARC::File::XML;
25 use MARC::Record;
26 use Test::Deep;
27 use Test::MockModule;
28 use Test::MockObject;
29 use Test::Warn;
30
31 use C4::Context;
32 use Koha::Authority;
33 use Koha::Authorities;
34 use Koha::Authority::MergeRequest;
35 use Koha::Authority::Type;
36 use Koha::Authority::Types;
37 use Koha::Database;
38
39 use t::lib::Mocks;
40 use t::lib::TestBuilder;
41
42 BEGIN {
43     #TODO Helpful as long as we have issues here
44     my $mock = Test::MockObject->new();
45     $mock->fake_module( 'Catmandu::Store::ElasticSearch' );
46 }
47
48 my $schema = Koha::Database->new->schema;
49 $schema->storage->txn_begin;
50
51 # Globals
52 our $search_compat_pars;
53 our $builder              = t::lib::TestBuilder->new;
54
55 my $nb_of_authorities     = Koha::Authorities->search->count;
56 my $nb_of_authority_types = Koha::Authority::Types->search->count;
57 my $new_authority_type_1  = Koha::Authority::Type->new(
58     {   authtypecode       => 'my_ac_1',
59         authtypetext       => 'my authority type text 1',
60         auth_tag_to_report => '100',
61         summary            => 'my summary for authority 1',
62     }
63 )->store;
64 my $new_authority_1 = Koha::Authority->new( { authtypecode => $new_authority_type_1->authtypecode, } )->store;
65 my $new_authority_2 = Koha::Authority->new( { authtypecode => $new_authority_type_1->authtypecode, } )->store;
66
67 is( Koha::Authority::Types->search->count, $nb_of_authority_types + 1, 'The authority type should have been added' );
68 is( Koha::Authorities->search->count,      $nb_of_authorities + 2,     'The 2 authorities should have been added' );
69
70 $new_authority_1->delete;
71 is( Koha::Authorities->search->count, $nb_of_authorities + 1, 'Delete should have deleted the authority' );
72
73 subtest 'New merge request, method oldmarc' => sub {
74     plan tests => 4;
75
76     my $marc = MARC::Record->new;
77     $marc->append_fields(
78         MARC::Field->new( '100', '', '', a => 'a', b => 'b_findme' ),
79         MARC::Field->new( '200', '', '', a => 'aa' ),
80     );
81     my $req = Koha::Authority::MergeRequest->new({
82         authid => $new_authority_2->authid,
83         reportxml => 'Should be discarded',
84     });
85     is( $req->reportxml, undef, 'Reportxml is undef without oldrecord' );
86
87     $req = Koha::Authority::MergeRequest->new({
88         authid => $new_authority_2->authid,
89         oldrecord => $marc,
90     });
91     like( $req->reportxml, qr/b_findme/, 'Reportxml initialized' );
92
93     # Check if oldmarc is a MARC::Record and has one or two fields
94     is( ref( $req->oldmarc ), 'MARC::Record', 'Check oldmarc method' );
95     if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
96         is( scalar $req->oldmarc->fields, 2, 'UNIMARC contains two fields' );
97     } else {
98         is( scalar $req->oldmarc->fields, 1, 'MARC21 contains one field' );
99     }
100 };
101
102 subtest 'Testing reporting_tag_xml in MergeRequest' => sub {
103     plan tests => 2;
104
105     my $record = MARC::Record->new;
106     $record->append_fields(
107         MARC::Field->new( '024', '', '', a => 'aaa' ),
108         MARC::Field->new( '110', '', '', a => 'Best author' ),
109         MARC::Field->new( '234', '', '', a => 'Just a field' ),
110     );
111     my $xml = Koha::Authority::MergeRequest->reporting_tag_xml({
112         record => $record, tag => '100',
113     });
114     is( $xml, undef, 'Expected no result for wrong tag' );
115     $xml = Koha::Authority::MergeRequest->reporting_tag_xml({
116         record => $record, tag => '110',
117     });
118     my $newrecord = MARC::Record->new_from_xml(
119         $xml, 'UTF-8',
120         C4::Context->preference('marcflavour') eq 'UNIMARC' ?
121         'UNIMARCAUTH' :
122         'MARC21',
123     );
124     cmp_deeply( $record->field('110')->subfields,
125         $newrecord->field('110')->subfields,
126         'Compare reporting tag in both records',
127     );
128 };
129
130 subtest 'Trivial tests for get_count_usage and linked_biblionumbers' => sub {
131     plan tests => 5;
132
133     # NOTE: We are not testing $searcher->simple_search_compat here. Suppose
134     # that should be done in t/db../Koha/SearchEngine?
135     # So we're just testing the 'wrapper' here.
136
137     my ( $mods, $koha_fields );
138     t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
139     $mods->{zebra} = Test::MockModule->new( 'Koha::SearchEngine::Zebra::Search' );
140     $mods->{elastic} = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch::Search' );
141     $mods->{biblio} = Test::MockModule->new( 'C4::Biblio' );
142     $mods->{zebra}->mock( 'simple_search_compat', \&simple_search_compat );
143     $mods->{elastic}->mock( 'simple_search_compat', \&simple_search_compat );
144     $mods->{biblio}->mock( 'GetMarcFromKohaField', sub { return @$koha_fields; });
145
146     my $auth1 = $builder->build({ source => 'AuthHeader' });
147     $auth1 = Koha::Authorities->find( $auth1->{authid} );
148
149     # Test error condition
150     my $count;
151     $search_compat_pars = [ 0, 'some_error' ];
152     warning_like { $count = $auth1->get_usage_count }
153         qr/some_error/, 'Catch warn of simple_search_compat';
154     is( $count, undef, 'Undef returned when error encountered' );
155
156     # Simple test with some results; one result discarded in the 2nd test
157     $search_compat_pars = [ 1 ];
158     $koha_fields = [ '001', '' ];
159     is(  $auth1->get_usage_count, 3, 'Three results expected (Zebra)' );
160     cmp_deeply( [ $auth1->linked_biblionumbers ], [ 1001, 3003 ],
161         'linked_biblionumbers should ignore record without biblionumber' );
162
163     # And a simple test with Elastic
164     t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
165     cmp_deeply( [ $auth1->linked_biblionumbers ], [ 2001 ],
166         'linked_biblionumbers with Elasticsearch' );
167 };
168
169 sub simple_search_compat {
170     if( $search_compat_pars->[0] == 0 ) {
171         return ( $search_compat_pars->[1], [], 0 );
172     } elsif( $search_compat_pars->[0] == 1 ) {
173         my $records = C4::Context->preference('SearchEngine') eq 'Zebra'
174             ? few_marcxml_records()
175             : few_marc_records();
176         return ( undef, $records, scalar @$records );
177     }
178 }
179
180 sub few_marcxml_records {
181     return [
182 q|<?xml version="1.0" encoding="UTF-8"?>
183 <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
184     <controlfield tag="001">1001</controlfield>
185     <datafield tag="110" ind1=" " ind2=" ">
186         <subfield code="9">102</subfield>
187         <subfield code="a">My Corporation</subfield>
188     </datafield>
189 </record>|,
190 q|<?xml version="1.0" encoding="UTF-8"?>
191 <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
192     <!-- No biblionumber here -->
193     <datafield tag="610" ind1=" " ind2=" ">
194         <subfield code="9">112</subfield>
195         <subfield code="a">Another Corporation</subfield>
196     </datafield>
197 </record>|,
198 q|<?xml version="1.0" encoding="UTF-8"?>
199 <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
200     <controlfield tag="001">3003</controlfield>
201     <datafield tag="110" ind1=" " ind2=" ">
202         <subfield code="9">102</subfield>
203         <subfield code="a">My Corporation</subfield>
204     </datafield>
205 </record>|
206     ];
207 }
208
209 sub few_marc_records {
210     my $marc = MARC::Record->new;
211     $marc->append_fields(
212         MARC::Field->new( '001', '2001' ),
213         MARC::Field->new( '245', '', '', a => 'Title' ),
214     );
215     return [ $marc ];
216 }
217
218 $schema->storage->txn_rollback;