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 => 16;
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 qw( get_link );
36 use_ok('C4::Biblio', qw( AddBiblio GetMarcFromKohaField BiblioAutoLink GetMarcSubfieldStructure GetMarcSubfieldStructureFromKohaField LinkBibHeadingsToAuthorities GetBiblioData ModBiblio GetMarcISSN GetMarcControlnumber GetMarcISBN GetMarcPrice GetFrameworkCode GetMarcUrls IsMarcStructureInternal GetMarcStructure GetXmlBiblio DelBiblio ));
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( 'AutoLinkBiblios', $marcflavour );
72 t::lib::Mocks::mock_preference( 'AutoCreateAuthorities', $marcflavour );
73 t::lib::Mocks::mock_preference( 'autoControlNumber', "OFF" );
75 my $mock_biblio = Test::MockModule->new("C4::Biblio");
76 $mock_biblio->mock( BiblioAutoLink => sub {
78 my $frameworkcode = shift;
79 warn "My biblionumber is ".$record->subfield('999','c')." and my frameworkcode is $frameworkcode";
81 warning_like { $builder->build_sample_biblio(); }
82 qr/My biblionumber is \d+ and my frameworkcode is /, "The biblionumber is correctly passed to BiblioAutoLink";
86 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
90 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
91 authorised_value authtypecode value_builder isurl hidden frameworkcode
92 seealso link defaultvalue maxlength
95 # biblio.biblionumber must be mapped so this should return something
96 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber');
98 ok(defined $marc_subfield_structure, "There is a result");
99 is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
100 foreach my $col (@columns) {
101 ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
103 is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
104 like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
106 # Add a test for list context (BZ 10306)
107 my @results = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber');
108 is( @results, 1, 'We expect only one mapping' );
109 is_deeply( $results[0], $marc_subfield_structure,
110 'The first entry should be the same hashref as we had before' );
112 # foo.bar does not exist so this should return undef
113 $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar');
114 is($marc_subfield_structure, undef, "invalid kohafield returns undef");
118 subtest "GetMarcSubfieldStructure" => sub {
121 # Add multiple Koha to Marc mappings
122 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '399', tagsubfield => [ 'a', 'b' ] })->delete;
123 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'a', kohafield => "mytable.nicepages" })->store;
124 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'b', kohafield => "mytable.nicepages" })->store;
125 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
126 my $structure = C4::Biblio::GetMarcSubfieldStructure('');
128 is( @{ $structure->{"mytable.nicepages"} }, 2,
129 'GetMarcSubfieldStructure should return two entries for nicepages' );
130 is( $structure->{"mytable.nicepages"}->[0]->{tagfield}, '399',
131 'Check tagfield for first entry' );
132 is( $structure->{"mytable.nicepages"}->[0]->{tagsubfield}, 'a',
133 'Check tagsubfield for first entry' );
134 is( $structure->{"mytable.nicepages"}->[1]->{tagfield}, '399',
135 'Check tagfield for second entry' );
136 is( $structure->{"mytable.nicepages"}->[1]->{tagsubfield}, 'b',
137 'Check tagsubfield for second entry' );
140 subtest "GetMarcFromKohaField" => sub {
143 #NOTE: We are building on data from the previous subtest
144 # With: field 399 / mytable.nicepages
146 # Check call in list context for multiple mappings
147 my @retval = C4::Biblio::GetMarcFromKohaField('mytable.nicepages');
148 is( @retval, 4, 'Should return two tags and subfields' );
149 is( $retval[0], '399', 'Check first tag' );
150 is( $retval[1], 'a', 'Check first subfield' );
151 is( $retval[2], '399', 'Check second tag' );
152 is( $retval[3], 'b', 'Check second subfield' );
154 # Check same call in scalar context
155 is( C4::Biblio::GetMarcFromKohaField('mytable.nicepages'), '399',
156 'GetMarcFromKohaField returns first tag in scalar context' );
158 # Bug 19096 Default is authoritative
159 # If we add a new empty framework, we should still get the mappings
160 # from Default. CAUTION: This test passes intentionally the obsoleted
161 # framework parameter.
162 my $new_fw = t::lib::TestBuilder->new->build({source => 'BiblioFramework'});
163 @retval = C4::Biblio::GetMarcFromKohaField(
164 'mytable.nicepages', $new_fw->{frameworkcode},
166 is( @retval, 4, 'Still got two pairs of tags/subfields' );
167 is( $retval[0].$retval[1], '399a', 'Including 399a' );
170 subtest "Authority creation with default linker" => sub {
172 # Automatic authority creation
173 t::lib::Mocks::mock_preference('LinkerModule', 'Default');
174 t::lib::Mocks::mock_preference('AutoLinkBiblios', 1);
175 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
176 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
177 my $linker = C4::Linker::Default->new({});
178 my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
179 $authorities_mod->mock(
182 my $results = [{ authid => 'original' },{ authid => 'duplicate' }];
186 my $marc_record = MARC::Record->new();
187 my $field = MARC::Field->new(655, ' ', ' ','a' => 'Magical realism');
188 $marc_record->append_fields( $field );
189 my ($num_changed,$results) = LinkBibHeadingsToAuthorities($linker, $marc_record, "",undef);
190 is( $num_changed, 0, "We shouldn't link or create a new record");
191 ok( !defined $results->{added}, "If we have multiple matches, we shouldn't create a new record");
193 ($num_changed,$results) = LinkBibHeadingsToAuthorities($linker, $marc_record, "",undef);
194 is( $num_changed, 0, "We shouldn't link or create a new record using cached result");
195 ok( !defined $results->{added}, "If we have multiple matches, we shouldn't create a new record on second instance");
201 my $biblio_module = Test::MockModule->new('C4::Biblio');
202 $biblio_module->mock(
203 'GetMarcSubfieldStructure',
207 my ( $title_field, $title_subfield ) = get_title_field();
208 my ( $subtitle_field, $subtitle_subfield ) = get_subtitle_field();
209 my ( $medium_field, $medium_subfield ) = get_medium_field();
210 my ( $part_number_field, $part_number_subfield ) = get_part_number_field();
211 my ( $part_name_field, $part_name_subfield ) = get_part_name_field();
212 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
213 my ( $issn_field, $issn_subfield ) = get_issn_field();
214 my ( $biblionumber_field, $biblionumber_subfield ) = ( '999', 'c' );
215 my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
216 my ( $itemnumber_field, $itemnumber_subfield ) = get_itemnumber_field();
219 'biblio.title' => [ { tagfield => $title_field, tagsubfield => $title_subfield } ],
220 'biblio.subtitle' => [ { tagfield => $subtitle_field, tagsubfield => $subtitle_subfield } ],
221 'biblio.medium' => [ { tagfield => $medium_field, tagsubfield => $medium_subfield } ],
222 'biblio.part_number' => [ { tagfield => $part_number_field, tagsubfield => $part_number_subfield } ],
223 'biblio.part_name' => [ { tagfield => $part_name_field, tagsubfield => $part_name_subfield } ],
224 'biblio.biblionumber' => [ { tagfield => $biblionumber_field, tagsubfield => $biblionumber_subfield } ],
225 'biblioitems.isbn' => [ { tagfield => $isbn_field, tagsubfield => $isbn_subfield } ],
226 'biblioitems.issn' => [ { tagfield => $issn_field, tagsubfield => $issn_subfield } ],
227 'biblioitems.biblioitemnumber' => [ { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield } ],
228 'items.itemnumber' => [ { tagfield => $itemnumber_subfield, tagsubfield => $itemnumber_subfield } ],
233 my $currency = Test::MockModule->new('Koha::Acquisition::Currencies');
237 return Koha::Acquisition::Currency->new(
249 my $marcflavour = shift;
250 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
251 # Authority tests don't interact well with Elasticsearch at the moment due to the fact that there's currently no way to
252 # roll back ES index changes.
253 t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
254 t::lib::Mocks::mock_preference('autoControlNumber', 'OFF');
256 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
258 my $isbn = '0590353403';
259 my $title = 'Foundation';
260 my $subtitle1 = 'Research';
261 my $subtitle2 = 'Conclusions';
262 my $medium = 'Medium';
263 my $part_number = '123';
264 my $part_name = 'First years';
266 # Generate a record with just the ISBN
267 my $marc_record = MARC::Record->new;
268 $marc_record->append_fields( create_isbn_field( $isbn, $marcflavour ) );
270 # Add the record to the DB
271 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
272 my $data = GetBiblioData( $biblionumber );
273 is( $data->{ isbn }, $isbn,
274 '(GetBiblioData) ISBN correctly retireved.');
275 is( $data->{ title }, undef,
276 '(GetBiblioData) Title field is empty in fresh biblio.');
278 my $biblio = Koha::Biblios->find($biblionumber);
280 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
281 my $marc = $biblio->metadata->record;
282 is( $marc->subfield( $isbn_field, $isbn_subfield ), $isbn, );
285 my $field = create_title_field( $title, $marcflavour );
286 $marc_record->append_fields( $field );
287 ModBiblio( $marc_record, $biblionumber ,'' );
288 $data = GetBiblioData( $biblionumber );
289 is( $data->{ title }, $title,
290 'ModBiblio correctly added the title field, and GetBiblioData.');
291 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
292 $marc = $biblio->get_from_storage->metadata->record;
293 my ( $title_field, $title_subfield ) = get_title_field();
294 is( $marc->subfield( $title_field, $title_subfield ), $title, );
297 $marc_record->append_fields( create_field( $subtitle1, $marcflavour, get_subtitle_field() ) );
298 $marc_record->append_fields( create_field( $subtitle2, $marcflavour, get_subtitle_field() ) );
299 $marc_record->append_fields( create_field( $medium, $marcflavour, get_medium_field() ) );
300 $marc_record->append_fields( create_field( $part_number, $marcflavour, get_part_number_field() ) );
301 $marc_record->append_fields( create_field( $part_name, $marcflavour, get_part_name_field() ) );
303 ModBiblio( $marc_record, $biblionumber ,'' );
304 $data = GetBiblioData( $biblionumber );
305 is( $data->{ title }, $title, '(ModBiblio) still there after adding other fields.' );
306 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after adding other fields.' );
308 is( $data->{ subtitle }, "$subtitle1 | $subtitle2", '(ModBiblio) subtitles correctly added and returned in GetBiblioData.' );
309 is( $data->{ medium }, $medium, '(ModBiblio) medium correctly added and returned in GetBiblioData.' );
310 is( $data->{ part_number }, $part_number, '(ModBiblio) part_number correctly added and returned in GetBiblioData.' );
311 is( $data->{ part_name }, $part_name, '(ModBiblio) part_name correctly added and returned in GetBiblioData.' );
313 my $biblioitem = Koha::Biblioitems->find( $biblioitemnumber );
314 is( $biblioitem->_result->biblio->title, $title, # Should be $biblioitem->biblio instead, but not needed elsewhere for now
315 'Do not know if this makes sense - compare result of previous two GetBiblioData tests.');
316 is( $biblioitem->isbn, $isbn,
317 'Second test checking it returns the correct isbn.');
320 $field = MARC::Field->new(
322 'a' => 'Auction catalogs',
326 $marc_record->append_fields($field);
327 $success = ModBiblio($marc_record,$biblionumber,'');
332 ok($success, "ModBiblio handles authority-linked 655");
335 $field->delete_subfields('a');
336 $marc_record->append_fields($field);
337 $success = ModBiblio($marc_record,$biblionumber,'');
342 ok($success, "ModBiblio handles 655 with authority link but no heading");
345 $field->delete_subfields('9');
346 $marc_record->append_fields($field);
347 $success = ModBiblio($marc_record,$biblionumber,'');
352 ok($success, "ModBiblio handles 655 with no subfields");
354 ## Testing GetMarcISSN
356 $issns = GetMarcISSN( $marc_record, $marcflavour );
357 is( $issns->[0], undef,
358 'GetMarcISSN handles records without the ISSN field (list is empty)' );
359 is( scalar @$issns, 0,
360 'GetMarcISSN handles records without the ISSN field (count is 0)' );
362 my $issn = '1234-1234';
363 $field = create_issn_field( $issn, $marcflavour );
364 $marc_record->append_fields($field);
365 $issns = GetMarcISSN( $marc_record, $marcflavour );
366 is( $issns->[0], $issn,
367 'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
368 is( scalar @$issns, 1,
369 'GetMARCISSN handles records with a single ISSN field (count is 1)');
370 # Add multiple ISSN field
371 my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
372 foreach (@more_issns) {
373 $field = create_issn_field( $_, $marcflavour );
374 $marc_record->append_fields($field);
376 $issns = GetMarcISSN( $marc_record, $marcflavour );
377 is( scalar @$issns, 4,
378 'GetMARCISSN handles records with multiple ISSN fields (count correct)');
379 # Create an empty ISSN
380 $field = create_issn_field( "", $marcflavour );
381 $marc_record->append_fields($field);
382 $issns = GetMarcISSN( $marc_record, $marcflavour );
383 is( scalar @$issns, 4,
384 'GetMARCISSN skips empty ISSN fields (Bug 12674)');
386 ## Testing GetMarcControlnumber
388 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
389 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
391 $field = MARC::Field->new( '001', '' );
392 $marc_record->append_fields($field);
393 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
394 is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
396 $field = $marc_record->field('001');
397 $field->update('123456789X');
398 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
399 is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
401 ## Testing GetMarcISBN
402 my $record_for_isbn = MARC::Record->new();
403 my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
404 is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
407 $isbn_field = create_isbn_field( $isbn, $marcflavour );
408 $record_for_isbn->append_fields( $isbn_field );
409 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
410 is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
411 is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
413 # We add 3 more ISBNs
414 $record_for_isbn = MARC::Record->new();
415 my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
416 foreach (@more_isbns) {
417 $field = create_isbn_field( $_, $marcflavour );
418 $record_for_isbn->append_fields($field);
420 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
421 is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
422 for my $i (0 .. $#more_isbns) {
423 is( $isbns->[$i], $more_isbns[$i],
424 "(GetMarcISBN) Correctly retrieves ISBN #". ($i + 1));
427 is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
428 "GetMarcPrice returns the correct value");
429 my $frameworkcode = GetFrameworkCode($biblionumber);
430 my $updatedrecord = $biblio->metadata->record;
431 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
432 die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
433 my $biblioitemnumbertotest;
434 if ( $biblioitem_tag < 10 ) {
435 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
437 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
440 # test for GetMarcUrls
441 $marc_record->append_fields(
442 MARC::Field->new( '856', '', '', u => ' https://koha-community.org ' ),
443 MARC::Field->new( '856', '', '', u => 'koha-community.org' ),
445 my $marcurl = GetMarcUrls( $marc_record, $marcflavour );
446 is( @$marcurl, 2, 'GetMarcUrls returns two URLs' );
447 like( $marcurl->[0]->{MARCURL}, qr/^https/, 'GetMarcUrls did not stumble over a preceding space' );
448 ok( $marcflavour ne 'MARC21' || $marcurl->[1]->{MARCURL} =~ /^http:\/\//,
449 'GetMarcUrls prefixed a MARC21 URL with http://' );
451 # Automatic authority creation
452 t::lib::Mocks::mock_preference('AutoLinkBiblios', 1);
453 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
454 my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
455 $authorities_mod->mock(
463 $field = create_author_field('Author Name');
465 $marc_record->append_fields($field);
466 $success = ModBiblio($marc_record,$biblionumber,'');
471 ok($success, "ModBiblio handles authority addition for author");
473 my ($author_field, $author_subfield, $author_relator_subfield) = get_author_field();
474 $field = $marc_record->field($author_field);
475 ok($field->subfield($author_subfield), "ModBiblio keeps $author_field$author_subfield intact");
477 my $authid = $field->subfield('9');
478 ok($authid, 'ModBiblio adds authority id');
480 use_ok('C4::AuthoritiesMarc', qw( GetAuthority ));
481 my $auth_record = C4::AuthoritiesMarc::GetAuthority($authid);
482 ok($auth_record, 'Authority record successfully retrieved');
485 my ($auth_author_field, $auth_author_subfield) = get_auth_author_field();
486 $field = $auth_record->field($auth_author_field);
487 ok($field, "Authority record contains field $auth_author_field");
489 $field->subfield($auth_author_subfield),
491 'Authority $auth_author_field$auth_author_subfield contains author name'
493 is($field->subfield($author_relator_subfield), undef, 'Authority does not contain relator subfield');
496 t::lib::Mocks::mock_preference('AutoLinkBiblios', 0);
497 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 0);
500 sub get_title_field {
501 my $marc_flavour = C4::Context->preference('marcflavour');
502 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
505 sub get_medium_field {
506 my $marc_flavour = C4::Context->preference('marcflavour');
507 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'b' ) : ( '245', 'h' );
510 sub get_subtitle_field {
511 my $marc_flavour = C4::Context->preference('marcflavour');
512 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'e' ) : ( '245', 'b' );
515 sub get_part_number_field {
516 my $marc_flavour = C4::Context->preference('marcflavour');
517 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'h' ) : ( '245', 'n' );
520 sub get_part_name_field {
521 my $marc_flavour = C4::Context->preference('marcflavour');
522 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'i' ) : ( '245', 'p' );
526 my $marc_flavour = C4::Context->preference('marcflavour');
527 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
531 my $marc_flavour = C4::Context->preference('marcflavour');
532 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
535 sub get_itemnumber_field {
536 my $marc_flavour = C4::Context->preference('marcflavour');
537 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
540 sub get_author_field {
541 my $marc_flavour = C4::Context->preference('marcflavour');
542 return ( $marc_flavour eq 'UNIMARC' ) ? ( '700', 'a', '4' ) : ( '100', 'a', 'e' );
545 sub get_auth_author_field {
546 my $marc_flavour = C4::Context->preference('marcflavour');
547 return ( $marc_flavour eq 'UNIMARC' ) ? ( '106', 'a' ) : ( '100', 'a' );
550 sub create_title_field {
551 my ( $title, $marcflavour ) = @_;
553 my ( $title_field, $title_subfield ) = get_title_field();
554 my $field = MARC::Field->new( $title_field, '', '', $title_subfield => $title );
560 my ( $content, $marcflavour, $field, $subfield ) = @_;
562 return MARC::Field->new( $field, '', '', $subfield => $content );
565 sub create_isbn_field {
566 my ( $isbn, $marcflavour ) = @_;
568 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
569 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
571 # Add the price subfield
572 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
573 $field->add_subfields( $price_subfield => '$100' );
578 sub create_issn_field {
579 my ( $issn, $marcflavour ) = @_;
581 my ( $issn_field, $issn_subfield ) = get_issn_field();
582 my $field = MARC::Field->new( $issn_field, '', '', $issn_subfield => $issn );
587 sub create_author_field {
590 my ( $author_field, $author_subfield, $author_relator_subfield ) = get_author_field();
591 my $field = MARC::Field->new(
592 $author_field, '', '',
593 $author_subfield => $author,
594 $author_relator_subfield => 'aut'
600 subtest 'MARC21' => sub {
603 $schema->storage->txn_rollback;
604 $schema->storage->txn_begin;
607 subtest 'UNIMARC' => sub {
610 # Mock the auth type data for UNIMARC
611 $dbh->do("UPDATE auth_types SET auth_tag_to_report = '106' WHERE auth_tag_to_report = '100'") or die $dbh->errstr;
613 run_tests('UNIMARC');
614 $schema->storage->txn_rollback;
615 $schema->storage->txn_begin;
618 subtest 'IsMarcStructureInternal' => sub {
620 my $tagslib = GetMarcStructure();
622 for my $tag ( sort keys %$tagslib ) {
624 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
625 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
628 @internals = uniq @internals;
629 is( scalar(@internals), 7, 'expect 7 internals');
630 is( grep( /^lib$/, @internals ), 1, 'check lib' );
631 is( grep( /^tab$/, @internals ), 1, 'check tab' );
632 is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
633 is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
634 is( grep( /^important$/, @internals ), 1, 'check important' );
635 is( grep( /^a$/, @internals ), 0, 'no subfield a' );
636 is( grep( /^ind1_defaultvalue$/, @internals ), 1, 'check indicator 1 default value' );
637 is( grep( /^ind2_defaultvalue$/, @internals ), 1, 'check indicator 2 default value' );
640 subtest 'deletedbiblio_metadata' => sub {
643 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
645 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
646 my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber );
647 C4::Biblio::DelBiblio( $biblionumber );
648 my ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio WHERE biblionumber=?|, undef, $biblionumber);
649 is( $moved, $biblionumber, 'Found in deletedbiblio' );
650 ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio_metadata WHERE biblionumber=?|, undef, $biblionumber);
651 is( $moved, $biblionumber, 'Found in deletedbiblio_metadata' );
654 subtest 'DelBiblio' => sub {
658 t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
660 my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio(MARC::Record->new, '');
661 my $deleted = C4::Biblio::DelBiblio( $biblionumber );
662 is( $deleted, undef, 'DelBiblio returns undef is the biblio has been deleted correctly - Must be 1 instead'); # FIXME We should return 1 instead!
664 $deleted = C4::Biblio::DelBiblio( $biblionumber );
665 is( $deleted, undef, 'DelBiblo should return undef is the record did not exist');
667 my $biblio = $builder->build_sample_biblio;
668 my $subscription = $builder->build_object(
670 class => 'Koha::Subscriptions',
671 value => { biblionumber => $biblio->biblionumber }
674 my $serial = $builder->build_object(
676 class => 'Koha::Serials',
678 biblionumber => $biblio->biblionumber,
679 subscriptionid => $subscription->subscriptionid
683 my $subscription_history = $builder->build_object(
685 class => 'Koha::Subscription::Histories',
687 biblionumber => $biblio->biblionumber,
688 subscriptionid => $subscription->subscriptionid
693 my $order_basket = $builder->build( { source => 'Aqbasket' } );
696 biblionumber => $biblio->biblionumber,
697 basketno => $order_basket->{basketno},
699 my $order = $builder->build_object(
700 { class => 'Koha::Acquisition::Orders', value => $orderinfo } );
702 C4::Biblio::DelBiblio($biblio->biblionumber); # Or $biblio->delete
703 is( $subscription->get_from_storage, undef, 'subscription should be deleted on biblio deletion' );
704 is( $serial->get_from_storage, undef, 'serial should be deleted on biblio deletion' );
705 is( $subscription_history->get_from_storage, undef, 'subscription history should be deleted on biblio deletion' );
706 is( $order->get_from_storage->deleted_biblionumber, $biblio->biblionumber, 'biblionumber of order has been moved to deleted_biblionumber column' );
709 subtest 'MarcFieldForCreatorAndModifier' => sub {
712 t::lib::Mocks::mock_preference('MarcFieldForCreatorId', '998$a');
713 t::lib::Mocks::mock_preference('MarcFieldForCreatorName', '998$b');
714 t::lib::Mocks::mock_preference('MarcFieldForModifierId', '998$c');
715 t::lib::Mocks::mock_preference('MarcFieldForModifierName', '998$d');
716 my $c4_context = Test::MockModule->new('C4::Context');
717 $c4_context->mock('userenv', sub { return { number => 123, firstname => 'John', surname => 'Doe'}; });
719 my $record = MARC::Record->new();
720 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
722 my $biblio = Koha::Biblios->find($biblionumber);
723 $record = $biblio->metadata->record;
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'), 123, '998$c = 123');
727 is($record->subfield('998', 'd'), 'John Doe', '998$d = John Doe');
729 $c4_context->mock('userenv', sub { return { number => 321, firstname => 'Jane', surname => 'Doe'}; });
730 C4::Biblio::ModBiblio($record, $biblionumber, '');
732 $record = $biblio->get_from_storage->metadata->record;
733 is($record->subfield('998', 'a'), 123, '998$a = 123');
734 is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
735 is($record->subfield('998', 'c'), 321, '998$c = 321');
736 is($record->subfield('998', 'd'), 'Jane Doe', '998$d = Jane Doe');
739 subtest 'ModBiblio called from linker test' => sub {
742 t::lib::Mocks::mock_preference('AutoLinkBiblios', 1);
743 my $biblio_mod = Test::MockModule->new( 'C4::Biblio' );
744 $biblio_mod->mock( 'LinkBibHeadingsToAuthorities', sub {
747 my $record = MARC::Record->new();
748 my ($biblionumber) = C4::Biblio::AddBiblio($record,'');
749 C4::Biblio::ModBiblio($record,$biblionumber,'');
750 is($called,1,"We called to link bibs because not from linker");
752 C4::Biblio::ModBiblio($record,$biblionumber,'',{ disable_autolink => 1 });
753 is($called,0,"We didn't call to link bibs because from linker");
756 subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
759 # Set up mocks to ensure authorities are generated
760 my $biblio_mod = Test::MockModule->new( 'C4::Linker::Default' );
761 $biblio_mod->mock( 'get_link', sub {
762 return (undef,undef);
764 # UNIMARC valid headings are built from the marc_subfield_structure for bibs and
765 # include all subfields as valid, testing with MARC21 should be sufficient for now
766 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
767 t::lib::Mocks::mock_preference('AutoCreateAuthorities', '1');
769 my $linker = C4::Linker::Default->new();
770 my $biblio = $builder->build_sample_biblio();
771 my $record = $biblio->metadata->record;
773 # Generate a record including all valid subfields and an invalid one 'e'
774 my $field = MARC::Field->new('650','','','a' => 'Beach city', 'b' => 'Weirdness', 'v' => 'Fiction', 'x' => 'Books', 'y' => '21st Century', 'z' => 'Fish Stew Pizza', 'e' => 'Depicted');
776 $record->append_fields($field);
777 my ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef,650);
779 is( $num_headings_changed, 1, 'We changed the one we passed' );
780 is_deeply( $results->{added},
781 {"Beach city Weirdness--Fiction--Books--21st Century--Fish Stew Pizza" => 1 },
782 "We added an authority record for the heading"
785 # Now we check the authority record itself
786 my $authority = GetAuthority( $record->subfield('650','9') );
787 is( $authority->field('150')->as_string(),
788 "Beach city Weirdness Fiction Books 21st Century Fish Stew Pizza",
789 "The generated record contains the correct subfields"
792 #Add test for this case using verbose
793 $record->field('650')->delete_subfield('9');
794 ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef, 650, 1);
795 is( $num_headings_changed, 1, 'We changed the one we passed' );
796 is( $results->{details}->[0]->{status}, 'CREATED', "We added an authority record for the heading using verbose");
798 # Now we check the authority record itself
799 $authority = GetAuthority($results->{details}->[0]->{authid});
801 is( $authority->field('150')->as_string(),
802 "Beach city Weirdness Fiction Books 21st Century Fish Stew Pizza",
803 "The generated record contains the correct subfields when using verbose"
806 # Example series link with volume and punctuation
807 $field = MARC::Field->new('800','','','a' => 'Tolkien, J. R. R.', 'q' => '(John Ronald Reuel),', 'd' => '1892-1973.', 't' => 'Lord of the rings ;', 'v' => '1');
808 $record->append_fields($field);
810 ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef, 800);
812 is( $num_headings_changed, 1, 'We changed the one we passed' );
813 is_deeply( $results->{added},
814 {"Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings ;" => 1 },
815 "We added an authority record for the heading"
818 # Now we check the authority record itself
819 $authority = GetAuthority( $record->subfield('800','9') );
820 is( $authority->field('100')->as_string(),
821 "Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings",
822 "The generated record contains the correct subfields"
825 # The same example With verbose
826 $record->field('800')->delete_subfield('9');
827 ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef, 800, 1);
828 is( $num_headings_changed, 1, 'We changed the one we passed' );
829 is( $results->{details}->[0]->{status}, 'CREATED', "We added an authority record for the heading using verbose");
831 # Now we check the authority record itself
832 $authority = GetAuthority($results->{details}->[0]->{authid});
833 is( $authority->field('100')->as_string(),
834 "Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings",
835 "The generated record contains the correct subfields"
839 subtest 'autoControlNumber tests' => sub {
843 t::lib::Mocks::mock_preference('autoControlNumber', 'OFF');
845 my $record = MARC::Record->new();
846 my ($biblio_id) = C4::Biblio::AddBiblio($record, '');
847 my $biblio = Koha::Biblios->find($biblio_id);
849 $record = $biblio->metadata->record;
850 is($record->field('001'), undef, '001 not set when pref is off');
852 t::lib::Mocks::mock_preference('autoControlNumber', 'biblionumber');
853 C4::Biblio::ModBiblio($record, $biblio_id, "", { skip_record_index => 1, disable_autolink => 1 });
854 $biblio->discard_changes;
855 $record = $biblio->metadata->record;
856 is($record->field('001')->as_string(), $biblio_id, '001 set to biblionumber when pref set and field is blank');
858 $record->field('001')->update('Not biblionumber');
859 C4::Biblio::ModBiblio($record, $biblio_id, "", { skip_record_index => 1, disable_autolink => 1 });
860 $biblio->discard_changes;
861 $record = $biblio->metadata->record;
862 is($record->field('001')->as_string(), 'Not biblionumber', '001 not set to biblionumber when pref set and field exists');
866 subtest 'record test' => sub {
869 my $marc_record = MARC::Record->new;
870 $marc_record->append_fields( create_isbn_field( '0590353403', 'MARC21' ) );
872 my ($biblionumber) = C4::Biblio::AddBiblio( $marc_record, '' );
874 my $biblio = Koha::Biblios->find($biblionumber);
876 is( $biblio->record->as_formatted,
877 $biblio->metadata->record->as_formatted );
883 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
884 $schema->storage->txn_rollback;