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 => 15;
23 use List::MoreUtils qw( uniq );
26 use t::lib::Mocks qw( mock_preference );
27 use t::lib::TestBuilder;
31 use Koha::MarcSubfieldStructures;
33 use C4::Linker::Default;
39 my $schema = Koha::Database->new->schema;
40 $schema->storage->txn_begin;
41 my $dbh = C4::Context->dbh;
42 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
44 my $builder = t::lib::TestBuilder->new;
46 subtest 'AddBiblio' => sub {
49 my $marcflavour = 'MARC21';
50 t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour );
51 my $record = MARC::Record->new();
53 my ( $f, $sf ) = GetMarcFromKohaField('biblioitems.lccn');
54 my $lccn_field = MARC::Field->new( $f, ' ', ' ',
55 $sf => 'ThisisgoingtobetoomanycharactersfortheLCCNfield' );
56 $record->append_fields($lccn_field);
58 my $nb_biblios = Koha::Biblios->count;
59 my ( $biblionumber, $biblioitemnumber );
60 warnings_like { ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' ) }
61 [ qr/Data too long for column 'lccn'/, qr/Data too long for column 'lccn'/ ],
62 "expected warnings when adding too long LCCN";
63 is( $biblionumber, undef,
64 'AddBiblio returns undef for biblionumber if something went wrong' );
65 is( $biblioitemnumber, undef,
66 'AddBiblio returns undef for biblioitemnumber if something went wrong'
68 is( Koha::Biblios->count, $nb_biblios,
69 'No biblio should have been added if something went wrong' );
71 t::lib::Mocks::mock_preference( 'BiblioAddsAuthorities', $marcflavour );
72 t::lib::Mocks::mock_preference( 'AutoCreateAuthorities', $marcflavour );
74 my $mock_biblio = Test::MockModule->new("C4::Biblio");
75 $mock_biblio->mock( BiblioAutoLink => sub {
77 my $frameworkcode = shift;
78 warn "My biblionumber is ".$record->subfield('999','c')." and my frameworkcode is $frameworkcode";
80 warning_like { $builder->build_sample_biblio(); }
81 qr/My biblionumber is \d+ and my frameworkcode is /, "The biblionumber is correctly passed to BiblioAutoLink";
85 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
89 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
90 authorised_value authtypecode value_builder isurl hidden frameworkcode
91 seealso link defaultvalue maxlength
94 # biblio.biblionumber must be mapped so this should return something
95 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber');
97 ok(defined $marc_subfield_structure, "There is a result");
98 is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
99 foreach my $col (@columns) {
100 ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
102 is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
103 like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
105 # Add a test for list context (BZ 10306)
106 my @results = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber');
107 is( @results, 1, 'We expect only one mapping' );
108 is_deeply( $results[0], $marc_subfield_structure,
109 'The first entry should be the same hashref as we had before' );
111 # foo.bar does not exist so this should return undef
112 $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar');
113 is($marc_subfield_structure, undef, "invalid kohafield returns undef");
117 subtest "GetMarcSubfieldStructure" => sub {
120 # Add multiple Koha to Marc mappings
121 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '399', tagsubfield => [ 'a', 'b' ] })->delete;
122 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'a', kohafield => "mytable.nicepages" })->store;
123 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'b', kohafield => "mytable.nicepages" })->store;
124 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
125 my $structure = C4::Biblio::GetMarcSubfieldStructure('');
127 is( @{ $structure->{"mytable.nicepages"} }, 2,
128 'GetMarcSubfieldStructure should return two entries for nicepages' );
129 is( $structure->{"mytable.nicepages"}->[0]->{tagfield}, '399',
130 'Check tagfield for first entry' );
131 is( $structure->{"mytable.nicepages"}->[0]->{tagsubfield}, 'a',
132 'Check tagsubfield for first entry' );
133 is( $structure->{"mytable.nicepages"}->[1]->{tagfield}, '399',
134 'Check tagfield for second entry' );
135 is( $structure->{"mytable.nicepages"}->[1]->{tagsubfield}, 'b',
136 'Check tagsubfield for second entry' );
139 subtest "GetMarcFromKohaField" => sub {
142 #NOTE: We are building on data from the previous subtest
143 # With: field 399 / mytable.nicepages
145 # Check call in list context for multiple mappings
146 my @retval = C4::Biblio::GetMarcFromKohaField('mytable.nicepages');
147 is( @retval, 4, 'Should return two tags and subfields' );
148 is( $retval[0], '399', 'Check first tag' );
149 is( $retval[1], 'a', 'Check first subfield' );
150 is( $retval[2], '399', 'Check second tag' );
151 is( $retval[3], 'b', 'Check second subfield' );
153 # Check same call in scalar context
154 is( C4::Biblio::GetMarcFromKohaField('mytable.nicepages'), '399',
155 'GetMarcFromKohaField returns first tag in scalar context' );
157 # Bug 19096 Default is authoritative
158 # If we add a new empty framework, we should still get the mappings
159 # from Default. CAUTION: This test passes intentionally the obsoleted
160 # framework parameter.
161 my $new_fw = t::lib::TestBuilder->new->build({source => 'BiblioFramework'});
162 @retval = C4::Biblio::GetMarcFromKohaField(
163 'mytable.nicepages', $new_fw->{frameworkcode},
165 is( @retval, 4, 'Still got two pairs of tags/subfields' );
166 is( $retval[0].$retval[1], '399a', 'Including 399a' );
169 subtest "Authority creation with default linker" => sub {
171 # Automatic authority creation
172 t::lib::Mocks::mock_preference('LinkerModule', 'Default');
173 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
174 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
175 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
176 my $linker = C4::Linker::Default->new({});
177 my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
178 $authorities_mod->mock(
181 my $results = [{ authid => 'original' },{ authid => 'duplicate' }];
185 my $marc_record = MARC::Record->new();
186 my $field = MARC::Field->new(655, ' ', ' ','a' => 'Magical realism');
187 $marc_record->append_fields( $field );
188 my ($num_changed,$results) = LinkBibHeadingsToAuthorities($linker, $marc_record, "",undef);
189 is( $num_changed, 0, "We shouldn't link or create a new record");
190 ok( !defined $results->{added}, "If we have multiple matches, we shouldn't create a new record");
196 my $biblio_module = Test::MockModule->new('C4::Biblio');
197 $biblio_module->mock(
198 'GetMarcSubfieldStructure',
202 my ( $title_field, $title_subfield ) = get_title_field();
203 my ( $subtitle_field, $subtitle_subfield ) = get_subtitle_field();
204 my ( $medium_field, $medium_subfield ) = get_medium_field();
205 my ( $part_number_field, $part_number_subfield ) = get_part_number_field();
206 my ( $part_name_field, $part_name_subfield ) = get_part_name_field();
207 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
208 my ( $issn_field, $issn_subfield ) = get_issn_field();
209 my ( $biblionumber_field, $biblionumber_subfield ) = ( '999', 'c' );
210 my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
211 my ( $itemnumber_field, $itemnumber_subfield ) = get_itemnumber_field();
214 'biblio.title' => [ { tagfield => $title_field, tagsubfield => $title_subfield } ],
215 'biblio.subtitle' => [ { tagfield => $subtitle_field, tagsubfield => $subtitle_subfield } ],
216 'biblio.medium' => [ { tagfield => $medium_field, tagsubfield => $medium_subfield } ],
217 'biblio.part_number' => [ { tagfield => $part_number_field, tagsubfield => $part_number_subfield } ],
218 'biblio.part_name' => [ { tagfield => $part_name_field, tagsubfield => $part_name_subfield } ],
219 'biblio.biblionumber' => [ { tagfield => $biblionumber_field, tagsubfield => $biblionumber_subfield } ],
220 'biblioitems.isbn' => [ { tagfield => $isbn_field, tagsubfield => $isbn_subfield } ],
221 'biblioitems.issn' => [ { tagfield => $issn_field, tagsubfield => $issn_subfield } ],
222 'biblioitems.biblioitemnumber' => [ { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield } ],
223 'items.itemnumber' => [ { tagfield => $itemnumber_subfield, tagsubfield => $itemnumber_subfield } ],
228 my $currency = Test::MockModule->new('Koha::Acquisition::Currencies');
232 return Koha::Acquisition::Currency->new(
244 my $marcflavour = shift;
245 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
247 my $isbn = '0590353403';
248 my $title = 'Foundation';
249 my $subtitle1 = 'Research';
250 my $subtitle2 = 'Conclusions';
251 my $medium = 'Medium';
252 my $part_number = '123';
253 my $part_name = 'First years';
255 # Generate a record with just the ISBN
256 my $marc_record = MARC::Record->new;
257 $marc_record->append_fields( create_isbn_field( $isbn, $marcflavour ) );
259 # Add the record to the DB
260 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
261 my $data = GetBiblioData( $biblionumber );
262 is( $data->{ isbn }, $isbn,
263 '(GetBiblioData) ISBN correctly retireved.');
264 is( $data->{ title }, undef,
265 '(GetBiblioData) Title field is empty in fresh biblio.');
267 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
268 my $marc = GetMarcBiblio({ biblionumber => $biblionumber });
269 is( $marc->subfield( $isbn_field, $isbn_subfield ), $isbn, );
272 my $field = create_title_field( $title, $marcflavour );
273 $marc_record->append_fields( $field );
274 ModBiblio( $marc_record, $biblionumber ,'' );
275 $data = GetBiblioData( $biblionumber );
276 is( $data->{ title }, $title,
277 'ModBiblio correctly added the title field, and GetBiblioData.');
278 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
279 $marc = GetMarcBiblio({ biblionumber => $biblionumber });
280 my ( $title_field, $title_subfield ) = get_title_field();
281 is( $marc->subfield( $title_field, $title_subfield ), $title, );
284 $marc_record->append_fields( create_field( $subtitle1, $marcflavour, get_subtitle_field() ) );
285 $marc_record->append_fields( create_field( $subtitle2, $marcflavour, get_subtitle_field() ) );
286 $marc_record->append_fields( create_field( $medium, $marcflavour, get_medium_field() ) );
287 $marc_record->append_fields( create_field( $part_number, $marcflavour, get_part_number_field() ) );
288 $marc_record->append_fields( create_field( $part_name, $marcflavour, get_part_name_field() ) );
290 ModBiblio( $marc_record, $biblionumber ,'' );
291 $data = GetBiblioData( $biblionumber );
292 is( $data->{ title }, $title, '(ModBiblio) still there after adding other fields.' );
293 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after adding other fields.' );
295 is( $data->{ subtitle }, "$subtitle1 | $subtitle2", '(ModBiblio) subtitles correctly added and returned in GetBiblioData.' );
296 is( $data->{ medium }, $medium, '(ModBiblio) medium correctly added and returned in GetBiblioData.' );
297 is( $data->{ part_number }, $part_number, '(ModBiblio) part_number correctly added and returned in GetBiblioData.' );
298 is( $data->{ part_name }, $part_name, '(ModBiblio) part_name correctly added and returned in GetBiblioData.' );
300 my $biblioitem = Koha::Biblioitems->find( $biblioitemnumber );
301 is( $biblioitem->_result->biblio->title, $title, # Should be $biblioitem->biblio instead, but not needed elsewhere for now
302 'Do not know if this makes sense - compare result of previous two GetBiblioData tests.');
303 is( $biblioitem->isbn, $isbn,
304 'Second test checking it returns the correct isbn.');
307 $field = MARC::Field->new(
309 'a' => 'Auction catalogs',
313 $marc_record->append_fields($field);
314 $success = ModBiblio($marc_record,$biblionumber,'');
319 ok($success, "ModBiblio handles authority-linked 655");
322 $field->delete_subfields('a');
323 $marc_record->append_fields($field);
324 $success = ModBiblio($marc_record,$biblionumber,'');
329 ok($success, "ModBiblio handles 655 with authority link but no heading");
332 $field->delete_subfields('9');
333 $marc_record->append_fields($field);
334 $success = ModBiblio($marc_record,$biblionumber,'');
339 ok($success, "ModBiblio handles 655 with no subfields");
341 ## Testing GetMarcISSN
343 $issns = GetMarcISSN( $marc_record, $marcflavour );
344 is( $issns->[0], undef,
345 'GetMarcISSN handles records without the ISSN field (list is empty)' );
346 is( scalar @$issns, 0,
347 'GetMarcISSN handles records without the ISSN field (count is 0)' );
349 my $issn = '1234-1234';
350 $field = create_issn_field( $issn, $marcflavour );
351 $marc_record->append_fields($field);
352 $issns = GetMarcISSN( $marc_record, $marcflavour );
353 is( $issns->[0], $issn,
354 'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
355 is( scalar @$issns, 1,
356 'GetMARCISSN handles records with a single ISSN field (count is 1)');
357 # Add multiple ISSN field
358 my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
359 foreach (@more_issns) {
360 $field = create_issn_field( $_, $marcflavour );
361 $marc_record->append_fields($field);
363 $issns = GetMarcISSN( $marc_record, $marcflavour );
364 is( scalar @$issns, 4,
365 'GetMARCISSN handles records with multiple ISSN fields (count correct)');
366 # Create an empty ISSN
367 $field = create_issn_field( "", $marcflavour );
368 $marc_record->append_fields($field);
369 $issns = GetMarcISSN( $marc_record, $marcflavour );
370 is( scalar @$issns, 4,
371 'GetMARCISSN skips empty ISSN fields (Bug 12674)');
373 ## Testing GetMarcControlnumber
375 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
376 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
378 $field = MARC::Field->new( '001', '' );
379 $marc_record->append_fields($field);
380 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
381 is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
383 $field = $marc_record->field('001');
384 $field->update('123456789X');
385 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
386 is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
388 ## Testing GetMarcISBN
389 my $record_for_isbn = MARC::Record->new();
390 my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
391 is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
394 $isbn_field = create_isbn_field( $isbn, $marcflavour );
395 $record_for_isbn->append_fields( $isbn_field );
396 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
397 is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
398 is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
400 # We add 3 more ISBNs
401 $record_for_isbn = MARC::Record->new();
402 my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
403 foreach (@more_isbns) {
404 $field = create_isbn_field( $_, $marcflavour );
405 $record_for_isbn->append_fields($field);
407 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
408 is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
409 for my $i (0 .. $#more_isbns) {
410 is( $isbns->[$i], $more_isbns[$i],
411 "(GetMarcISBN) Correctly retrieves ISBN #". ($i + 1));
414 is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
415 "GetMarcPrice returns the correct value");
416 my $newincbiblioitemnumber=$biblioitemnumber+1;
417 $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
418 my $updatedrecord = GetMarcBiblio({
419 biblionumber => $biblionumber,
421 my $frameworkcode = GetFrameworkCode($biblionumber);
422 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
423 die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
424 my $biblioitemnumbertotest;
425 if ( $biblioitem_tag < 10 ) {
426 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
428 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
430 is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
432 # test for GetMarcNotes
433 my $a1= GetMarcNotes( $marc_record, $marcflavour );
434 my $field2 = MARC::Field->new( $marcflavour eq 'UNIMARC'? 300: 555, 0, '', a=> 'Some text', u=> 'http://url-1.com', u=> 'nohttp://something_else' );
435 $marc_record->append_fields( $field2 );
436 my $a2= GetMarcNotes( $marc_record, $marcflavour );
437 is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
438 ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
439 'Check the number of returned notes of GetMarcNotes' );
441 # test for GetMarcUrls
442 $marc_record->append_fields(
443 MARC::Field->new( '856', '', '', u => ' https://koha-community.org ' ),
444 MARC::Field->new( '856', '', '', u => 'koha-community.org' ),
446 my $marcurl = GetMarcUrls( $marc_record, $marcflavour );
447 is( @$marcurl, 2, 'GetMarcUrls returns two URLs' );
448 like( $marcurl->[0]->{MARCURL}, qr/^https/, 'GetMarcUrls did not stumble over a preceding space' );
449 ok( $marcflavour ne 'MARC21' || $marcurl->[1]->{MARCURL} =~ /^http:\/\//,
450 'GetMarcUrls prefixed a MARC21 URL with http://' );
452 # Automatic authority creation
453 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
454 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
455 my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
456 $authorities_mod->mock(
464 $field = create_author_field('Author Name');
466 $marc_record->append_fields($field);
467 $success = ModBiblio($marc_record,$biblionumber,'');
472 ok($success, "ModBiblio handles authority addition for author");
474 my ($author_field, $author_subfield, $author_relator_subfield) = get_author_field();
475 $field = $marc_record->field($author_field);
476 ok($field->subfield($author_subfield), "ModBiblio keeps $author_field$author_subfield intact");
478 my $authid = $field->subfield('9');
479 ok($authid, 'ModBiblio adds authority id');
481 use_ok('C4::AuthoritiesMarc');
482 my $auth_record = C4::AuthoritiesMarc::GetAuthority($authid);
483 ok($auth_record, 'Authority record successfully retrieved');
486 my ($auth_author_field, $auth_author_subfield) = get_auth_author_field();
487 $field = $auth_record->field($auth_author_field);
488 ok($field, "Authority record contains field $auth_author_field");
490 $field->subfield($auth_author_subfield),
492 'Authority $auth_author_field$auth_author_subfield contains author name'
494 is($field->subfield($author_relator_subfield), undef, 'Authority does not contain relator subfield');
497 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 0);
498 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 0);
501 sub get_title_field {
502 my $marc_flavour = C4::Context->preference('marcflavour');
503 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
506 sub get_medium_field {
507 my $marc_flavour = C4::Context->preference('marcflavour');
508 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'b' ) : ( '245', 'h' );
511 sub get_subtitle_field {
512 my $marc_flavour = C4::Context->preference('marcflavour');
513 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'e' ) : ( '245', 'b' );
516 sub get_part_number_field {
517 my $marc_flavour = C4::Context->preference('marcflavour');
518 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'h' ) : ( '245', 'n' );
521 sub get_part_name_field {
522 my $marc_flavour = C4::Context->preference('marcflavour');
523 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'i' ) : ( '245', 'p' );
527 my $marc_flavour = C4::Context->preference('marcflavour');
528 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
532 my $marc_flavour = C4::Context->preference('marcflavour');
533 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
536 sub get_itemnumber_field {
537 my $marc_flavour = C4::Context->preference('marcflavour');
538 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
541 sub get_author_field {
542 my $marc_flavour = C4::Context->preference('marcflavour');
543 return ( $marc_flavour eq 'UNIMARC' ) ? ( '700', 'a', '4' ) : ( '100', 'a', 'e' );
546 sub get_auth_author_field {
547 my $marc_flavour = C4::Context->preference('marcflavour');
548 return ( $marc_flavour eq 'UNIMARC' ) ? ( '106', 'a' ) : ( '100', 'a' );
551 sub create_title_field {
552 my ( $title, $marcflavour ) = @_;
554 my ( $title_field, $title_subfield ) = get_title_field();
555 my $field = MARC::Field->new( $title_field, '', '', $title_subfield => $title );
561 my ( $content, $marcflavour, $field, $subfield ) = @_;
563 return MARC::Field->new( $field, '', '', $subfield => $content );
566 sub create_isbn_field {
567 my ( $isbn, $marcflavour ) = @_;
569 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
570 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
572 # Add the price subfield
573 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
574 $field->add_subfields( $price_subfield => '$100' );
579 sub create_issn_field {
580 my ( $issn, $marcflavour ) = @_;
582 my ( $issn_field, $issn_subfield ) = get_issn_field();
583 my $field = MARC::Field->new( $issn_field, '', '', $issn_subfield => $issn );
588 sub create_author_field {
591 my ( $author_field, $author_subfield, $author_relator_subfield ) = get_author_field();
592 my $field = MARC::Field->new(
593 $author_field, '', '',
594 $author_subfield => $author,
595 $author_relator_subfield => 'aut'
601 subtest 'MARC21' => sub {
604 $schema->storage->txn_rollback;
605 $schema->storage->txn_begin;
608 subtest 'UNIMARC' => sub {
611 # Mock the auth type data for UNIMARC
612 $dbh->do("UPDATE auth_types SET auth_tag_to_report = '106' WHERE auth_tag_to_report = '100'") or die $dbh->errstr;
614 run_tests('UNIMARC');
615 $schema->storage->txn_rollback;
616 $schema->storage->txn_begin;
619 subtest 'NORMARC' => sub {
621 run_tests('NORMARC');
622 $schema->storage->txn_rollback;
623 $schema->storage->txn_begin;
626 subtest 'IsMarcStructureInternal' => sub {
628 my $tagslib = GetMarcStructure();
630 for my $tag ( sort keys %$tagslib ) {
632 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
633 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
636 @internals = uniq @internals;
637 is( scalar(@internals), 7, 'expect 7 internals');
638 is( grep( /^lib$/, @internals ), 1, 'check lib' );
639 is( grep( /^tab$/, @internals ), 1, 'check tab' );
640 is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
641 is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
642 is( grep( /^important$/, @internals ), 1, 'check important' );
643 is( grep( /^a$/, @internals ), 0, 'no subfield a' );
644 is( grep( /^ind1_defaultvalue$/, @internals ), 1, 'check indicator 1 default value' );
645 is( grep( /^ind2_defaultvalue$/, @internals ), 1, 'check indicator 2 default value' );
648 subtest 'deletedbiblio_metadata' => sub {
651 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
652 my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber );
653 C4::Biblio::DelBiblio( $biblionumber );
654 my ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio WHERE biblionumber=?|, undef, $biblionumber);
655 is( $moved, $biblionumber, 'Found in deletedbiblio' );
656 ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio_metadata WHERE biblionumber=?|, undef, $biblionumber);
657 is( $moved, $biblionumber, 'Found in deletedbiblio_metadata' );
660 subtest 'DelBiblio' => sub {
663 my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio(MARC::Record->new, '');
664 my $deleted = C4::Biblio::DelBiblio( $biblionumber );
665 is( $deleted, undef, 'DelBiblio returns undef is the biblio has been deleted correctly - Must be 1 instead'); # FIXME We should return 1 instead!
667 $deleted = C4::Biblio::DelBiblio( $biblionumber );
668 is( $deleted, undef, 'DelBiblo should return undef is the record did not exist');
670 my $biblio = $builder->build_sample_biblio;
671 my $subscription = $builder->build_object(
673 class => 'Koha::Subscriptions',
674 value => { biblionumber => $biblio->biblionumber }
677 my $serial = $builder->build_object(
679 class => 'Koha::Serials',
681 biblionumber => $biblio->biblionumber,
682 subscriptionid => $subscription->subscriptionid
686 my $subscription_history = $builder->build_object(
688 class => 'Koha::Subscription::Histories',
690 biblionumber => $biblio->biblionumber,
691 subscriptionid => $subscription->subscriptionid
695 C4::Biblio::DelBiblio($biblio->biblionumber); # Or $biblio->delete
696 is( $subscription->get_from_storage, undef, 'subscription should be deleted on biblio deletion' );
697 is( $serial->get_from_storage, undef, 'serial should be deleted on biblio deletion' );
698 is( $subscription_history->get_from_storage, undef, 'subscription history should be deleted on biblio deletion' );
701 subtest 'MarcFieldForCreatorAndModifier' => sub {
704 t::lib::Mocks::mock_preference('MarcFieldForCreatorId', '998$a');
705 t::lib::Mocks::mock_preference('MarcFieldForCreatorName', '998$b');
706 t::lib::Mocks::mock_preference('MarcFieldForModifierId', '998$c');
707 t::lib::Mocks::mock_preference('MarcFieldForModifierName', '998$d');
708 my $c4_context = Test::MockModule->new('C4::Context');
709 $c4_context->mock('userenv', sub { return { number => 123, firstname => 'John', surname => 'Doe'}; });
711 my $record = MARC::Record->new();
712 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
714 $record = GetMarcBiblio({biblionumber => $biblionumber});
715 is($record->subfield('998', 'a'), 123, '998$a = 123');
716 is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
717 is($record->subfield('998', 'c'), 123, '998$c = 123');
718 is($record->subfield('998', 'd'), 'John Doe', '998$d = John Doe');
720 $c4_context->mock('userenv', sub { return { number => 321, firstname => 'Jane', surname => 'Doe'}; });
721 C4::Biblio::ModBiblio($record, $biblionumber, '');
723 $record = GetMarcBiblio({biblionumber => $biblionumber});
724 is($record->subfield('998', 'a'), 123, '998$a = 123');
725 is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
726 is($record->subfield('998', 'c'), 321, '998$c = 321');
727 is($record->subfield('998', 'd'), 'Jane Doe', '998$d = Jane Doe');
730 subtest 'ModBiblio called from linker test' => sub {
733 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
734 my $biblio_mod = Test::MockModule->new( 'C4::Biblio' );
735 $biblio_mod->mock( 'LinkBibHeadingsToAuthorities', sub {
738 my $record = MARC::Record->new();
739 my ($biblionumber) = C4::Biblio::AddBiblio($record,'');
740 C4::Biblio::ModBiblio($record,$biblionumber,'');
741 is($called,1,"We called to link bibs because not from linker");
743 C4::Biblio::ModBiblio($record,$biblionumber,'',1);
744 is($called,0,"We didn't call to link bibs because from linker");
747 subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
750 # Set up mocks to ensure authorities are generated
751 my $biblio_mod = Test::MockModule->new( 'C4::Linker::Default' );
752 $biblio_mod->mock( 'get_link', sub {
753 return (undef,undef);
755 # UNIMARC valid headings are built from the marc_subfield_structure for bibs and
756 # include all subfields as valid, testing with MARC21 should be sufficient for now
757 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
758 t::lib::Mocks::mock_preference('AutoCreateAuthorities', '1');
760 my $linker = C4::Linker::Default->new();
761 my $record = MARC::Record->new();
763 # Generate a record including all valid subfields and an invalid one 'e'
764 my $field = MARC::Field->new('650','','','a' => 'Beach city', 'b' => 'Weirdness', 'v' => 'Fiction', 'x' => 'Books', 'y' => '21st Century', 'z' => 'Fish Stew Pizza', 'e' => 'Depicted');
766 $record->append_fields($field);
767 my ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef);
769 is( $num_headings_changed, 1, 'We changed the one we passed' );
770 is_deeply( $results->{added},
771 {"Beach city Weirdness--Fiction--Books--21st Century--Fish Stew Pizza" => 1 },
772 "We added an authority record for the heading"
775 # Now we check the authority record itself
776 my $authority = GetAuthority( $record->subfield('650','9') );
777 is( $authority->field('150')->as_string(),
778 "Beach city Weirdness Fiction Books 21st Century Fish Stew Pizza",
779 "The generated record contains the correct subfields"
782 # Example series link with volume and punctuation
783 $record = MARC::Record->new();
784 $field = MARC::Field->new('800','','','a' => 'Tolkien, J. R. R.', 'q' => '(John Ronald Reuel),', 'd' => '1892-1973.', 't' => 'Lord of the rings ;', 'v' => '1');
785 $record->append_fields($field);
787 ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef);
789 is( $num_headings_changed, 1, 'We changed the one we passed' );
790 is_deeply( $results->{added},
791 {"Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings ;" => 1 },
792 "We added an authority record for the heading"
795 # Now we check the authority record itself
796 $authority = GetAuthority( $record->subfield('800','9') );
797 is( $authority->field('100')->as_string(),
798 "Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings",
799 "The generated record contains the correct subfields"
804 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
805 $schema->storage->txn_rollback;