3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 6;
23 use List::MoreUtils qw( uniq );
25 use t::lib::Mocks qw( mock_preference );
31 my $dbh = C4::Context->dbh;
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
36 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
40 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
41 authorised_value authtypecode value_builder isurl hidden frameworkcode
42 seealso link defaultvalue maxlength
45 # biblio.biblionumber must be mapped so this should return something
46 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber', '');
48 ok(defined $marc_subfield_structure, "There is a result");
49 is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
50 foreach my $col (@columns) {
51 ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
53 is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
54 like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
56 # foo.bar does not exist so this should return undef
57 $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar', '');
58 is($marc_subfield_structure, undef, "invalid kohafield returns undef");
63 my $biblio_module = new Test::MockModule('C4::Biblio');
65 'GetMarcSubfieldStructure',
69 if ( C4::Context->preference('marcflavour') eq 'MARC21'
70 || C4::Context->preference('marcflavour') eq 'NORMARC' ) {
73 'biblio.title' => { tagfield => '245', tagsubfield => 'a' },
74 'biblio.biblionumber' => { tagfield => '999', tagsubfield => 'c' },
75 'biblioitems.isbn' => { tagfield => '020', tagsubfield => 'a' },
76 'biblioitems.issn' => { tagfield => '022', tagsubfield => 'a' },
77 'biblioitems.biblioitemnumber' => { tagfield => '999', tagsubfield => 'd' },
78 'items.itemnumber' => { tagfield => '952', tagsubfield => '9' },
80 } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
83 'biblio.title' => { tagfield => '200', tagsubfield => 'a' },
84 'biblio.biblionumber' => { tagfield => '999', tagsubfield => 'c' },
85 'biblioitems.isbn' => { tagfield => '010', tagsubfield => 'a' },
86 'biblioitems.issn' => { tagfield => '011', tagsubfield => 'a' },
87 'biblioitems.biblioitemnumber' => { tagfield => '090', tagsubfield => 'a' },
88 'items.itemnumber' => { tagfield => '995', tagsubfield => '9' },
94 my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
98 return Koha::Acquisition::Currency->new(
110 my $marcflavour = shift;
111 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
113 my $isbn = '0590353403';
114 my $title = 'Foundation';
116 # Generate a record with just the ISBN
117 my $marc_record = MARC::Record->new;
118 my $isbn_field = create_isbn_field( $isbn, $marcflavour );
119 $marc_record->append_fields( $isbn_field );
121 # Add the record to the DB
122 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
123 my $data = GetBiblioData( $biblionumber );
124 is( $data->{ isbn }, $isbn,
125 '(GetBiblioData) ISBN correctly retireved.');
126 is( $data->{ title }, undef,
127 '(GetBiblioData) Title field is empty in fresh biblio.');
130 my $field = create_title_field( $title, $marcflavour );
131 $marc_record->append_fields( $field );
132 ModBiblio( $marc_record, $biblionumber ,'' );
133 $data = GetBiblioData( $biblionumber );
134 is( $data->{ title }, $title,
135 'ModBiblio correctly added the title field, and GetBiblioData.');
136 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
138 my $itemdata = GetBiblioItemData( $biblioitemnumber );
139 is( $itemdata->{ title }, $title,
140 'First test of GetBiblioItemData to get same result of previous two GetBiblioData tests.');
141 is( $itemdata->{ isbn }, $isbn,
142 'Second test checking it returns the correct isbn.');
145 $field = MARC::Field->new(
147 'a' => 'Auction catalogs',
151 $marc_record->append_fields($field);
152 $success = ModBiblio($marc_record,$biblionumber,'');
157 ok($success, "ModBiblio handles authority-linked 655");
160 $field->delete_subfields('a');
161 $marc_record->append_fields($field);
162 $success = ModBiblio($marc_record,$biblionumber,'');
167 ok($success, "ModBiblio handles 655 with authority link but no heading");
170 $field->delete_subfields('9');
171 $marc_record->append_fields($field);
172 $success = ModBiblio($marc_record,$biblionumber,'');
177 ok($success, "ModBiblio handles 655 with no subfields");
179 ## Testing GetMarcISSN
181 $issns = GetMarcISSN( $marc_record, $marcflavour );
182 is( $issns->[0], undef,
183 'GetMarcISSN handles records without the ISSN field (list is empty)' );
184 is( scalar @$issns, 0,
185 'GetMarcISSN handles records without the ISSN field (count is 0)' );
187 my $issn = '1234-1234';
188 $field = create_issn_field( $issn, $marcflavour );
189 $marc_record->append_fields($field);
190 $issns = GetMarcISSN( $marc_record, $marcflavour );
191 is( $issns->[0], $issn,
192 'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
193 is( scalar @$issns, 1,
194 'GetMARCISSN handles records with a single ISSN field (count is 1)');
195 # Add multiple ISSN field
196 my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
197 foreach (@more_issns) {
198 $field = create_issn_field( $_, $marcflavour );
199 $marc_record->append_fields($field);
201 $issns = GetMarcISSN( $marc_record, $marcflavour );
202 is( scalar @$issns, 4,
203 'GetMARCISSN handles records with multiple ISSN fields (count correct)');
204 # Create an empty ISSN
205 $field = create_issn_field( "", $marcflavour );
206 $marc_record->append_fields($field);
207 $issns = GetMarcISSN( $marc_record, $marcflavour );
208 is( scalar @$issns, 4,
209 'GetMARCISSN skips empty ISSN fields (Bug 12674)');
211 ## Testing GetMarcControlnumber
213 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
214 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
216 $field = MARC::Field->new( '001', '' );
217 $marc_record->append_fields($field);
218 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
219 is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
221 $field = $marc_record->field('001');
222 $field->update('123456789X');
223 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
224 is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
226 ## Testing GetMarcISBN
227 my $record_for_isbn = MARC::Record->new();
228 my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
229 is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
232 $isbn_field = create_isbn_field( $isbn, $marcflavour );
233 $record_for_isbn->append_fields( $isbn_field );
234 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
235 is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
236 is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
238 # We add 3 more ISBNs
239 $record_for_isbn = MARC::Record->new();
240 my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
241 foreach (@more_isbns) {
242 $field = create_isbn_field( $_, $marcflavour );
243 $record_for_isbn->append_fields($field);
245 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
246 is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
247 for my $i (0 .. $#more_isbns) {
248 is( $isbns->[$i], $more_isbns[$i],
249 "(GetMarcISBN) Corretly retrieves ISBN #". ($i + 1));
252 is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
253 "GetMarcPrice returns the correct value");
254 my $newincbiblioitemnumber=$biblioitemnumber+1;
255 $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
256 my $updatedrecord = GetMarcBiblio($biblionumber, 0);
257 my $frameworkcode = GetFrameworkCode($biblionumber);
258 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
259 die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
260 my $biblioitemnumbertotest;
261 if ( $biblioitem_tag < 10 ) {
262 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
264 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
266 is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
268 # test for GetMarcNotes
269 my $a1= GetMarcNotes( $marc_record, $marcflavour );
270 my $field2 = MARC::Field->new( $marcflavour eq 'UNIMARC'? 300: 555, 0, '', a=> 'Some text', u=> 'http://url-1.com', u=> 'nohttp://something_else' );
271 $marc_record->append_fields( $field2 );
272 my $a2= GetMarcNotes( $marc_record, $marcflavour );
273 is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
274 ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
275 'Check the number of returned notes of GetMarcNotes' );
278 sub create_title_field {
279 my ( $title, $marcflavour ) = @_;
281 my $title_field = ( $marcflavour eq 'UNIMARC' ) ? '200' : '245';
282 my $field = MARC::Field->new( $title_field,'','','a' => $title);
287 sub create_isbn_field {
288 my ( $isbn, $marcflavour ) = @_;
290 my $isbn_field = ( $marcflavour eq 'UNIMARC' ) ? '010' : '020';
291 my $field = MARC::Field->new( $isbn_field,'','','a' => $isbn);
292 # Add the price subfield
293 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c' ;
294 $field->add_subfields( $price_subfield => '$100' );
299 sub create_issn_field {
300 my ( $issn, $marcflavour ) = @_;
302 my $issn_field = ( $marcflavour eq 'UNIMARC' ) ? '011' : '022';
303 my $field = MARC::Field->new( $issn_field,'','','a' => $issn);
308 subtest 'MARC21' => sub {
314 subtest 'UNIMARC' => sub {
316 run_tests('UNIMARC');
320 subtest 'NORMARC' => sub {
322 run_tests('NORMARC');
326 subtest 'IsMarcStructureInternal' => sub {
328 my $tagslib = GetMarcStructure();
330 for my $tag ( sort keys %$tagslib ) {
332 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
333 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
336 @internals = uniq @internals;
337 is( scalar(@internals), 4, 'expect four internals');
338 is( grep( /^lib$/, @internals ), 1, 'check lib' );
339 is( grep( /^tab$/, @internals ), 1, 'check tab' );
340 is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
341 is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
342 is( grep( /^a$/, @internals ), 0, 'no subfield a' );