Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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 => 7;
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 C4::AuthoritiesMarc qw( merge AddAuthority );
33 use Koha::Authority;
34 use Koha::Authority::ControlledIndicators;
35 use Koha::Authorities;
36 use Koha::Authority::MergeRequest;
37 use Koha::Authority::Type;
38 use Koha::Authority::Types;
39 use Koha::Database;
40
41 use t::lib::Mocks;
42 use t::lib::TestBuilder;
43
44 my $schema = Koha::Database->new->schema;
45 $schema->storage->txn_begin;
46
47 # Globals
48 our $search_compat_pars;
49 our $builder              = t::lib::TestBuilder->new;
50
51 my $nb_of_authorities     = Koha::Authorities->search->count;
52 my $nb_of_authority_types = Koha::Authority::Types->search->count;
53 my $new_authority_type_1  = Koha::Authority::Type->new(
54     {   authtypecode       => 'my_ac_1',
55         authtypetext       => 'my authority type text 1',
56         auth_tag_to_report => '100',
57         summary            => 'my summary for authority 1',
58     }
59 )->store;
60 my $new_authority_1 = Koha::Authority->new( { authtypecode => $new_authority_type_1->authtypecode, marcxml => '' } )->store;
61 my $new_authority_2 = Koha::Authority->new( { authtypecode => $new_authority_type_1->authtypecode, marcxml => '' } )->store;
62
63 is( Koha::Authority::Types->search->count, $nb_of_authority_types + 1, 'The authority type should have been added' );
64 is( Koha::Authorities->search->count,      $nb_of_authorities + 2,     'The 2 authorities should have been added' );
65
66 $new_authority_1->delete;
67 is( Koha::Authorities->search->count, $nb_of_authorities + 1, 'Delete should have deleted the authority' );
68
69 subtest 'New merge request, method oldmarc' => sub {
70     plan tests => 4;
71
72     my $marc = MARC::Record->new;
73     $marc->append_fields(
74         MARC::Field->new( '100', '', '', a => 'a', b => 'b_findme' ),
75         MARC::Field->new( '200', '', '', a => 'aa' ),
76     );
77     my $req = Koha::Authority::MergeRequest->new({
78         authid => $new_authority_2->authid,
79         reportxml => 'Should be discarded',
80     });
81     is( $req->reportxml, undef, 'Reportxml is undef without oldrecord' );
82
83     $req = Koha::Authority::MergeRequest->new({
84         authid => $new_authority_2->authid,
85         oldrecord => $marc,
86     });
87     like( $req->reportxml, qr/b_findme/, 'Reportxml initialized' );
88
89     # Check if oldmarc is a MARC::Record and has one or two fields
90     is( ref( $req->oldmarc ), 'MARC::Record', 'Check oldmarc method' );
91     if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
92         is( scalar $req->oldmarc->fields, 2, 'UNIMARC contains two fields' );
93     } else {
94         is( scalar $req->oldmarc->fields, 1, 'MARC21 contains one field' );
95     }
96 };
97
98 subtest 'Testing reporting_tag_xml in MergeRequest' => sub {
99     plan tests => 2;
100
101     my $record = MARC::Record->new;
102     $record->append_fields(
103         MARC::Field->new( '024', '', '', a => 'aaa' ),
104         MARC::Field->new( '110', '', '', a => 'Best author' ),
105         MARC::Field->new( '234', '', '', a => 'Just a field' ),
106     );
107     my $xml = Koha::Authority::MergeRequest->reporting_tag_xml({
108         record => $record, tag => '100',
109     });
110     is( $xml, undef, 'Expected no result for wrong tag' );
111     $xml = Koha::Authority::MergeRequest->reporting_tag_xml({
112         record => $record, tag => '110',
113     });
114     my $newrecord = MARC::Record->new_from_xml(
115         $xml, 'UTF-8',
116         C4::Context->preference('marcflavour') eq 'UNIMARC' ?
117         'UNIMARCAUTH' :
118         'MARC21',
119     );
120     cmp_deeply( $record->field('110')->subfields,
121         $newrecord->field('110')->subfields,
122         'Compare reporting tag in both records',
123     );
124 };
125
126 subtest 'Trivial tests for get_usage_count and linked_biblionumbers' => sub {
127     plan tests => 5;
128
129     # NOTE: We are not testing $searcher->simple_search_compat here. Suppose
130     # that should be done in t/db../Koha/SearchEngine?
131     # So we're just testing the 'wrapper' here.
132
133     my ( $mods, $koha_fields );
134     t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
135     $mods->{zebra} = Test::MockModule->new( 'Koha::SearchEngine::Zebra::Search' );
136     $mods->{elastic} = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch::Search' );
137     $mods->{biblio} = Test::MockModule->new( 'C4::Biblio' );
138     $mods->{zebra}->mock( 'simple_search_compat', \&simple_search_compat );
139     $mods->{elastic}->mock( 'simple_search_compat', \&simple_search_compat );
140     $mods->{biblio}->mock( 'GetMarcFromKohaField', sub { return @$koha_fields; });
141
142     my $auth1 = $builder->build({ source => 'AuthHeader' });
143     $auth1 = Koha::Authorities->find( $auth1->{authid} );
144
145     # Test error condition
146     my $count;
147     $search_compat_pars = [ 0, 'some_error' ];
148     warning_like { $count = $auth1->get_usage_count }
149         qr/some_error/, 'Catch warn of simple_search_compat';
150     is( $count, undef, 'Undef returned when error encountered' );
151
152     # Simple test with some results; one result discarded in the 2nd test
153     $search_compat_pars = [ 1 ];
154     $koha_fields = [ '001', '' ];
155     is(  $auth1->get_usage_count, 3, 'Three results expected (Zebra)' );
156     cmp_deeply( [ $auth1->linked_biblionumbers ], [ 1001, 3003 ],
157         'linked_biblionumbers should ignore record without biblionumber' );
158
159     # And a simple test with Elastic
160     t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
161     cmp_deeply( [ $auth1->linked_biblionumbers ], [ 2001 ],
162         'linked_biblionumbers with Elasticsearch' );
163     t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
164 };
165
166 subtest 'Simple test for controlled_indicators' => sub {
167     plan tests => 4;
168
169     # NOTE: See more detailed tests in t/Koha/Authority/ControlledIndicators.t
170
171     # Mock pref so that authority indicators are swapped for marc21/unimarc
172     # The biblio tag is actually made irrelevant here
173     t::lib::Mocks::mock_preference('AuthorityControlledIndicators', q|marc21,*,ind1:auth2,ind2:auth1
174 unimarc,*,ind1:auth2,ind2:auth1|);
175     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
176
177     my $record = MARC::Record->new;
178     $record->append_fields( MARC::Field->new( '100', '1', '2', a => 'Name' ) );
179     my $type = $builder->build({ source => 'AuthType', value => { auth_tag_to_report => '100'} });
180     my $authid = C4::AuthoritiesMarc::AddAuthority( $record, undef, $type->{authtypecode} );
181     my $auth = Koha::Authorities->find( $authid );
182     is( $auth->controlled_indicators({ biblio_tag => '123' })->{ind1}, '2', 'MARC21: Swapped ind2' );
183     is( $auth->controlled_indicators({ biblio_tag => '234' })->{ind2}, '1', 'MARC21: Swapped ind1' );
184
185     # try UNIMARC too
186     t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
187     $record = MARC::Record->new;
188     $record->append_fields( MARC::Field->new( '210', '1', '2', a => 'Name' ) );
189     $type = $builder->build({ source => 'AuthType', value => { auth_tag_to_report => '210'} });
190     $authid = C4::AuthoritiesMarc::AddAuthority( $record, undef, $type->{authtypecode} );
191     $auth = Koha::Authorities->find( $authid );
192     is( $auth->controlled_indicators({ biblio_tag => '345' })->{ind1}, '2', 'UNIMARC: Swapped ind2' );
193     is( $auth->controlled_indicators({ biblio_tag => '456' })->{ind2}, '1', 'UNIMARC: Swapped ind1' );
194 };
195
196 sub simple_search_compat {
197     if( $search_compat_pars->[0] == 0 ) {
198         return ( $search_compat_pars->[1], [], 0 );
199     } elsif( $search_compat_pars->[0] == 1 ) {
200         my $records = C4::Context->preference('SearchEngine') eq 'Zebra'
201             ? few_marcxml_records()
202             : few_marc_records();
203         return ( undef, $records, scalar @$records );
204     }
205 }
206
207 sub few_marcxml_records {
208     return [
209 q|<?xml version="1.0" encoding="UTF-8"?>
210 <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">
211     <controlfield tag="001">1001</controlfield>
212     <datafield tag="110" ind1=" " ind2=" ">
213         <subfield code="9">102</subfield>
214         <subfield code="a">My Corporation</subfield>
215     </datafield>
216 </record>|,
217 q|<?xml version="1.0" encoding="UTF-8"?>
218 <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">
219     <!-- No biblionumber here -->
220     <datafield tag="610" ind1=" " ind2=" ">
221         <subfield code="9">112</subfield>
222         <subfield code="a">Another Corporation</subfield>
223     </datafield>
224 </record>|,
225 q|<?xml version="1.0" encoding="UTF-8"?>
226 <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">
227     <controlfield tag="001">3003</controlfield>
228     <datafield tag="110" ind1=" " ind2=" ">
229         <subfield code="9">102</subfield>
230         <subfield code="a">My Corporation</subfield>
231     </datafield>
232 </record>|
233     ];
234 }
235
236 sub few_marc_records {
237     my $marc = MARC::Record->new;
238     $marc->append_fields(
239         MARC::Field->new( '001', '2001' ),
240         MARC::Field->new( '245', '', '', a => 'Title' ),
241     );
242     return [ $marc ];
243 }
244
245 $schema->storage->txn_rollback;