3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test
::More tests
=> 7;
32 use C4
::AuthoritiesMarc
;
34 use Koha
::Authority
::ControlledIndicators
;
35 use Koha
::Authorities
;
36 use Koha
::Authority
::MergeRequest
;
37 use Koha
::Authority
::Type
;
38 use Koha
::Authority
::Types
;
42 use t
::lib
::TestBuilder
;
44 my $schema = Koha
::Database
->new->schema;
45 $schema->storage->txn_begin;
48 our $search_compat_pars;
49 our $builder = t
::lib
::TestBuilder
->new;
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',
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;
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' );
66 $new_authority_1->delete;
67 is
( Koha
::Authorities
->search->count, $nb_of_authorities + 1, 'Delete should have deleted the authority' );
69 subtest
'New merge request, method oldmarc' => sub {
72 my $marc = MARC
::Record
->new;
74 MARC
::Field
->new( '100', '', '', a
=> 'a', b
=> 'b_findme' ),
75 MARC
::Field
->new( '200', '', '', a
=> 'aa' ),
77 my $req = Koha
::Authority
::MergeRequest
->new({
78 authid
=> $new_authority_2->authid,
79 reportxml
=> 'Should be discarded',
81 is
( $req->reportxml, undef, 'Reportxml is undef without oldrecord' );
83 $req = Koha
::Authority
::MergeRequest
->new({
84 authid
=> $new_authority_2->authid,
87 like
( $req->reportxml, qr/b_findme/, 'Reportxml initialized' );
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' );
94 is
( scalar $req->oldmarc->fields, 1, 'MARC21 contains one field' );
98 subtest
'Testing reporting_tag_xml in MergeRequest' => sub {
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' ),
107 my $xml = Koha
::Authority
::MergeRequest
->reporting_tag_xml({
108 record
=> $record, tag
=> '100',
110 is
( $xml, undef, 'Expected no result for wrong tag' );
111 $xml = Koha
::Authority
::MergeRequest
->reporting_tag_xml({
112 record
=> $record, tag
=> '110',
114 my $newrecord = MARC
::Record
->new_from_xml(
116 C4
::Context
->preference('marcflavour') eq 'UNIMARC' ?
120 cmp_deeply
( $record->field('110')->subfields,
121 $newrecord->field('110')->subfields,
122 'Compare reporting tag in both records',
126 subtest
'Trivial tests for get_usage_count and linked_biblionumbers' => sub {
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.
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; });
142 my $auth1 = $builder->build({ source
=> 'AuthHeader' });
143 $auth1 = Koha
::Authorities
->find( $auth1->{authid
} );
145 # Test error condition
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' );
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' );
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');
166 subtest
'Simple test for controlled_indicators' => sub {
169 # NOTE: See more detailed tests in t/Koha/Authority/ControlledIndicators.t
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' );
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' );
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' );
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 );
207 sub few_marcxml_records
{
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
>
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
>
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
>
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' ),
245 $schema->storage->txn_rollback;