Bug 25898: Prohibit indirect object notation
[koha.git] / t / db_dependent / Biblio.t
blob21d2c0710571f6b6e670550cc2f6a2a808d1eeb2
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 14;
21 use Test::MockModule;
22 use List::MoreUtils qw( uniq );
23 use MARC::Record;
25 use t::lib::Mocks qw( mock_preference );
26 use t::lib::TestBuilder;
28 use Koha::Database;
29 use Koha::Caches;
30 use Koha::MarcSubfieldStructures;
32 use C4::Linker::Default;
34 BEGIN {
35 use_ok('C4::Biblio');
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40 my $dbh = C4::Context->dbh;
41 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
43 my $builder = t::lib::TestBuilder->new;
45 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
46 plan tests => 25;
48 my @columns = qw(
49 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
50 authorised_value authtypecode value_builder isurl hidden frameworkcode
51 seealso link defaultvalue maxlength
54 # biblio.biblionumber must be mapped so this should return something
55 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber');
57 ok(defined $marc_subfield_structure, "There is a result");
58 is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
59 foreach my $col (@columns) {
60 ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
62 is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
63 like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
65 # Add a test for list context (BZ 10306)
66 my @results = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber');
67 is( @results, 1, 'We expect only one mapping' );
68 is_deeply( $results[0], $marc_subfield_structure,
69 'The first entry should be the same hashref as we had before' );
71 # foo.bar does not exist so this should return undef
72 $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar');
73 is($marc_subfield_structure, undef, "invalid kohafield returns undef");
77 subtest "GetMarcSubfieldStructure" => sub {
78 plan tests => 5;
80 # Add multiple Koha to Marc mappings
81 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '399', tagsubfield => [ 'a', 'b' ] })->delete;
82 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'a', kohafield => "mytable.nicepages" })->store;
83 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'b', kohafield => "mytable.nicepages" })->store;
84 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
85 my $structure = C4::Biblio::GetMarcSubfieldStructure('');
87 is( @{ $structure->{"mytable.nicepages"} }, 2,
88 'GetMarcSubfieldStructure should return two entries for nicepages' );
89 is( $structure->{"mytable.nicepages"}->[0]->{tagfield}, '399',
90 'Check tagfield for first entry' );
91 is( $structure->{"mytable.nicepages"}->[0]->{tagsubfield}, 'a',
92 'Check tagsubfield for first entry' );
93 is( $structure->{"mytable.nicepages"}->[1]->{tagfield}, '399',
94 'Check tagfield for second entry' );
95 is( $structure->{"mytable.nicepages"}->[1]->{tagsubfield}, 'b',
96 'Check tagsubfield for second entry' );
99 subtest "GetMarcFromKohaField" => sub {
100 plan tests => 8;
102 #NOTE: We are building on data from the previous subtest
103 # With: field 399 / mytable.nicepages
105 # Check call in list context for multiple mappings
106 my @retval = C4::Biblio::GetMarcFromKohaField('mytable.nicepages');
107 is( @retval, 4, 'Should return two tags and subfields' );
108 is( $retval[0], '399', 'Check first tag' );
109 is( $retval[1], 'a', 'Check first subfield' );
110 is( $retval[2], '399', 'Check second tag' );
111 is( $retval[3], 'b', 'Check second subfield' );
113 # Check same call in scalar context
114 is( C4::Biblio::GetMarcFromKohaField('mytable.nicepages'), '399',
115 'GetMarcFromKohaField returns first tag in scalar context' );
117 # Bug 19096 Default is authoritative
118 # If we add a new empty framework, we should still get the mappings
119 # from Default. CAUTION: This test passes intentionally the obsoleted
120 # framework parameter.
121 my $new_fw = t::lib::TestBuilder->new->build({source => 'BiblioFramework'});
122 @retval = C4::Biblio::GetMarcFromKohaField(
123 'mytable.nicepages', $new_fw->{frameworkcode},
125 is( @retval, 4, 'Still got two pairs of tags/subfields' );
126 is( $retval[0].$retval[1], '399a', 'Including 399a' );
129 subtest "Authority creation with default linker" => sub {
130 plan tests => 2;
131 # Automatic authority creation
132 t::lib::Mocks::mock_preference('LinkerModule', 'Default');
133 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
134 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
135 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
136 my $linker = C4::Linker::Default->new({});
137 my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
138 $authorities_mod->mock(
139 'authorities',
140 sub {
141 my $results = [{ authid => 'original' },{ authid => 'duplicate' }];
142 return $results;
145 my $marc_record = MARC::Record->new();
146 my $field = MARC::Field->new(655, ' ', ' ','a' => 'Magical realism');
147 $marc_record->append_fields( $field );
148 my ($num_changed,$results) = LinkBibHeadingsToAuthorities($linker, $marc_record, "",undef);
149 is( $num_changed, 0, "We shouldn't link or create a new record");
150 ok( !defined $results->{added}, "If we have multiple matches, we shouldn't create a new record");
155 # Mocking variables
156 my $biblio_module = Test::MockModule->new('C4::Biblio');
157 $biblio_module->mock(
158 'GetMarcSubfieldStructure',
159 sub {
160 my ($self) = shift;
162 my ( $title_field, $title_subfield ) = get_title_field();
163 my ( $subtitle_field, $subtitle_subfield ) = get_subtitle_field();
164 my ( $medium_field, $medium_subfield ) = get_medium_field();
165 my ( $part_number_field, $part_number_subfield ) = get_part_number_field();
166 my ( $part_name_field, $part_name_subfield ) = get_part_name_field();
167 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
168 my ( $issn_field, $issn_subfield ) = get_issn_field();
169 my ( $biblionumber_field, $biblionumber_subfield ) = ( '999', 'c' );
170 my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
171 my ( $itemnumber_field, $itemnumber_subfield ) = get_itemnumber_field();
173 return {
174 'biblio.title' => [ { tagfield => $title_field, tagsubfield => $title_subfield } ],
175 'biblio.subtitle' => [ { tagfield => $subtitle_field, tagsubfield => $subtitle_subfield } ],
176 'biblio.medium' => [ { tagfield => $medium_field, tagsubfield => $medium_subfield } ],
177 'biblio.part_number' => [ { tagfield => $part_number_field, tagsubfield => $part_number_subfield } ],
178 'biblio.part_name' => [ { tagfield => $part_name_field, tagsubfield => $part_name_subfield } ],
179 'biblio.biblionumber' => [ { tagfield => $biblionumber_field, tagsubfield => $biblionumber_subfield } ],
180 'biblioitems.isbn' => [ { tagfield => $isbn_field, tagsubfield => $isbn_subfield } ],
181 'biblioitems.issn' => [ { tagfield => $issn_field, tagsubfield => $issn_subfield } ],
182 'biblioitems.biblioitemnumber' => [ { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield } ],
183 'items.itemnumber' => [ { tagfield => $itemnumber_subfield, tagsubfield => $itemnumber_subfield } ],
188 my $currency = Test::MockModule->new('Koha::Acquisition::Currencies');
189 $currency->mock(
190 'get_active',
191 sub {
192 return Koha::Acquisition::Currency->new(
193 { symbol => '$',
194 isocode => 'USD',
195 currency => 'USD',
196 active => 1,
202 sub run_tests {
204 my $marcflavour = shift;
205 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
207 my $isbn = '0590353403';
208 my $title = 'Foundation';
209 my $subtitle1 = 'Research';
210 my $subtitle2 = 'Conclusions';
211 my $medium = 'Medium';
212 my $part_number = '123';
213 my $part_name = 'First years';
215 # Generate a record with just the ISBN
216 my $marc_record = MARC::Record->new;
217 $marc_record->append_fields( create_isbn_field( $isbn, $marcflavour ) );
219 # Add the record to the DB
220 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
221 my $data = GetBiblioData( $biblionumber );
222 is( $data->{ isbn }, $isbn,
223 '(GetBiblioData) ISBN correctly retireved.');
224 is( $data->{ title }, undef,
225 '(GetBiblioData) Title field is empty in fresh biblio.');
227 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
228 my $marc = GetMarcBiblio({ biblionumber => $biblionumber });
229 is( $marc->subfield( $isbn_field, $isbn_subfield ), $isbn, );
231 # Add title
232 my $field = create_title_field( $title, $marcflavour );
233 $marc_record->append_fields( $field );
234 ModBiblio( $marc_record, $biblionumber ,'' );
235 $data = GetBiblioData( $biblionumber );
236 is( $data->{ title }, $title,
237 'ModBiblio correctly added the title field, and GetBiblioData.');
238 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
239 $marc = GetMarcBiblio({ biblionumber => $biblionumber });
240 my ( $title_field, $title_subfield ) = get_title_field();
241 is( $marc->subfield( $title_field, $title_subfield ), $title, );
243 # Add other fields
244 $marc_record->append_fields( create_field( $subtitle1, $marcflavour, get_subtitle_field() ) );
245 $marc_record->append_fields( create_field( $subtitle2, $marcflavour, get_subtitle_field() ) );
246 $marc_record->append_fields( create_field( $medium, $marcflavour, get_medium_field() ) );
247 $marc_record->append_fields( create_field( $part_number, $marcflavour, get_part_number_field() ) );
248 $marc_record->append_fields( create_field( $part_name, $marcflavour, get_part_name_field() ) );
250 ModBiblio( $marc_record, $biblionumber ,'' );
251 $data = GetBiblioData( $biblionumber );
252 is( $data->{ title }, $title, '(ModBiblio) still there after adding other fields.' );
253 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after adding other fields.' );
255 is( $data->{ subtitle }, "$subtitle1 | $subtitle2", '(ModBiblio) subtitles correctly added and returned in GetBiblioData.' );
256 is( $data->{ medium }, $medium, '(ModBiblio) medium correctly added and returned in GetBiblioData.' );
257 is( $data->{ part_number }, $part_number, '(ModBiblio) part_number correctly added and returned in GetBiblioData.' );
258 is( $data->{ part_name }, $part_name, '(ModBiblio) part_name correctly added and returned in GetBiblioData.' );
260 my $biblioitem = Koha::Biblioitems->find( $biblioitemnumber );
261 is( $biblioitem->_result->biblio->title, $title, # Should be $biblioitem->biblio instead, but not needed elsewhere for now
262 'Do not know if this makes sense - compare result of previous two GetBiblioData tests.');
263 is( $biblioitem->isbn, $isbn,
264 'Second test checking it returns the correct isbn.');
266 my $success = 0;
267 $field = MARC::Field->new(
268 655, ' ', ' ',
269 'a' => 'Auction catalogs',
270 '9' => '1'
272 eval {
273 $marc_record->append_fields($field);
274 $success = ModBiblio($marc_record,$biblionumber,'');
275 } or do {
276 diag($@);
277 $success = 0;
279 ok($success, "ModBiblio handles authority-linked 655");
281 eval {
282 $field->delete_subfields('a');
283 $marc_record->append_fields($field);
284 $success = ModBiblio($marc_record,$biblionumber,'');
285 } or do {
286 diag($@);
287 $success = 0;
289 ok($success, "ModBiblio handles 655 with authority link but no heading");
291 eval {
292 $field->delete_subfields('9');
293 $marc_record->append_fields($field);
294 $success = ModBiblio($marc_record,$biblionumber,'');
295 } or do {
296 diag($@);
297 $success = 0;
299 ok($success, "ModBiblio handles 655 with no subfields");
301 ## Testing GetMarcISSN
302 my $issns;
303 $issns = GetMarcISSN( $marc_record, $marcflavour );
304 is( $issns->[0], undef,
305 'GetMarcISSN handles records without the ISSN field (list is empty)' );
306 is( scalar @$issns, 0,
307 'GetMarcISSN handles records without the ISSN field (count is 0)' );
308 # Add an ISSN field
309 my $issn = '1234-1234';
310 $field = create_issn_field( $issn, $marcflavour );
311 $marc_record->append_fields($field);
312 $issns = GetMarcISSN( $marc_record, $marcflavour );
313 is( $issns->[0], $issn,
314 'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
315 is( scalar @$issns, 1,
316 'GetMARCISSN handles records with a single ISSN field (count is 1)');
317 # Add multiple ISSN field
318 my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
319 foreach (@more_issns) {
320 $field = create_issn_field( $_, $marcflavour );
321 $marc_record->append_fields($field);
323 $issns = GetMarcISSN( $marc_record, $marcflavour );
324 is( scalar @$issns, 4,
325 'GetMARCISSN handles records with multiple ISSN fields (count correct)');
326 # Create an empty ISSN
327 $field = create_issn_field( "", $marcflavour );
328 $marc_record->append_fields($field);
329 $issns = GetMarcISSN( $marc_record, $marcflavour );
330 is( scalar @$issns, 4,
331 'GetMARCISSN skips empty ISSN fields (Bug 12674)');
333 ## Testing GetMarcControlnumber
334 my $controlnumber;
335 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
336 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
338 $field = MARC::Field->new( '001', '' );
339 $marc_record->append_fields($field);
340 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
341 is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
343 $field = $marc_record->field('001');
344 $field->update('123456789X');
345 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
346 is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
348 ## Testing GetMarcISBN
349 my $record_for_isbn = MARC::Record->new();
350 my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
351 is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
353 # We add one ISBN
354 $isbn_field = create_isbn_field( $isbn, $marcflavour );
355 $record_for_isbn->append_fields( $isbn_field );
356 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
357 is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
358 is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
360 # We add 3 more ISBNs
361 $record_for_isbn = MARC::Record->new();
362 my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
363 foreach (@more_isbns) {
364 $field = create_isbn_field( $_, $marcflavour );
365 $record_for_isbn->append_fields($field);
367 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
368 is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
369 for my $i (0 .. $#more_isbns) {
370 is( $isbns->[$i], $more_isbns[$i],
371 "(GetMarcISBN) Correctly retrieves ISBN #". ($i + 1));
374 is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
375 "GetMarcPrice returns the correct value");
376 my $newincbiblioitemnumber=$biblioitemnumber+1;
377 $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
378 my $updatedrecord = GetMarcBiblio({
379 biblionumber => $biblionumber,
380 embed_items => 0 });
381 my $frameworkcode = GetFrameworkCode($biblionumber);
382 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
383 die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
384 my $biblioitemnumbertotest;
385 if ( $biblioitem_tag < 10 ) {
386 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
387 } else {
388 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
390 is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
392 # test for GetMarcNotes
393 my $a1= GetMarcNotes( $marc_record, $marcflavour );
394 my $field2 = MARC::Field->new( $marcflavour eq 'UNIMARC'? 300: 555, 0, '', a=> 'Some text', u=> 'http://url-1.com', u=> 'nohttp://something_else' );
395 $marc_record->append_fields( $field2 );
396 my $a2= GetMarcNotes( $marc_record, $marcflavour );
397 is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
398 ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
399 'Check the number of returned notes of GetMarcNotes' );
401 # test for GetMarcUrls
402 $marc_record->append_fields(
403 MARC::Field->new( '856', '', '', u => ' https://koha-community.org ' ),
404 MARC::Field->new( '856', '', '', u => 'koha-community.org' ),
406 my $marcurl = GetMarcUrls( $marc_record, $marcflavour );
407 is( @$marcurl, 2, 'GetMarcUrls returns two URLs' );
408 like( $marcurl->[0]->{MARCURL}, qr/^https/, 'GetMarcUrls did not stumble over a preceding space' );
409 ok( $marcflavour ne 'MARC21' || $marcurl->[1]->{MARCURL} =~ /^http:\/\//,
410 'GetMarcUrls prefixed a MARC21 URL with http://' );
412 # Automatic authority creation
413 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
414 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
415 my $authorities_mod = Test::MockModule->new( 'C4::Heading' );
416 $authorities_mod->mock(
417 'authorities',
418 sub {
419 my @results;
420 return \@results;
423 $success = 0;
424 $field = create_author_field('Author Name');
425 eval {
426 $marc_record->append_fields($field);
427 $success = ModBiblio($marc_record,$biblionumber,'');
428 } or do {
429 diag($@);
430 $success = 0;
432 ok($success, "ModBiblio handles authority addition for author");
434 my ($author_field, $author_subfield, $author_relator_subfield) = get_author_field();
435 $field = $marc_record->field($author_field);
436 ok($field->subfield($author_subfield), "ModBiblio keeps $author_field$author_subfield intact");
438 my $authid = $field->subfield('9');
439 ok($authid, 'ModBiblio adds authority id');
441 use_ok('C4::AuthoritiesMarc');
442 my $auth_record = C4::AuthoritiesMarc::GetAuthority($authid);
443 ok($auth_record, 'Authority record successfully retrieved');
446 my ($auth_author_field, $auth_author_subfield) = get_auth_author_field();
447 $field = $auth_record->field($auth_author_field);
448 ok($field, "Authority record contains field $auth_author_field");
450 $field->subfield($auth_author_subfield),
451 'Author Name',
452 'Authority $auth_author_field$auth_author_subfield contains author name'
454 is($field->subfield($author_relator_subfield), undef, 'Authority does not contain relator subfield');
456 # Reset settings
457 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 0);
458 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 0);
461 sub get_title_field {
462 my $marc_flavour = C4::Context->preference('marcflavour');
463 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
466 sub get_medium_field {
467 my $marc_flavour = C4::Context->preference('marcflavour');
468 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'b' ) : ( '245', 'h' );
471 sub get_subtitle_field {
472 my $marc_flavour = C4::Context->preference('marcflavour');
473 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'e' ) : ( '245', 'b' );
476 sub get_part_number_field {
477 my $marc_flavour = C4::Context->preference('marcflavour');
478 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'h' ) : ( '245', 'n' );
481 sub get_part_name_field {
482 my $marc_flavour = C4::Context->preference('marcflavour');
483 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'i' ) : ( '245', 'p' );
486 sub get_isbn_field {
487 my $marc_flavour = C4::Context->preference('marcflavour');
488 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
491 sub get_issn_field {
492 my $marc_flavour = C4::Context->preference('marcflavour');
493 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
496 sub get_itemnumber_field {
497 my $marc_flavour = C4::Context->preference('marcflavour');
498 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
501 sub get_author_field {
502 my $marc_flavour = C4::Context->preference('marcflavour');
503 return ( $marc_flavour eq 'UNIMARC' ) ? ( '700', 'a', '4' ) : ( '100', 'a', 'e' );
506 sub get_auth_author_field {
507 my $marc_flavour = C4::Context->preference('marcflavour');
508 return ( $marc_flavour eq 'UNIMARC' ) ? ( '106', 'a' ) : ( '100', 'a' );
511 sub create_title_field {
512 my ( $title, $marcflavour ) = @_;
514 my ( $title_field, $title_subfield ) = get_title_field();
515 my $field = MARC::Field->new( $title_field, '', '', $title_subfield => $title );
517 return $field;
520 sub create_field {
521 my ( $content, $marcflavour, $field, $subfield ) = @_;
523 return MARC::Field->new( $field, '', '', $subfield => $content );
526 sub create_isbn_field {
527 my ( $isbn, $marcflavour ) = @_;
529 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
530 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
532 # Add the price subfield
533 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
534 $field->add_subfields( $price_subfield => '$100' );
536 return $field;
539 sub create_issn_field {
540 my ( $issn, $marcflavour ) = @_;
542 my ( $issn_field, $issn_subfield ) = get_issn_field();
543 my $field = MARC::Field->new( $issn_field, '', '', $issn_subfield => $issn );
545 return $field;
548 sub create_author_field {
549 my ( $author ) = @_;
551 my ( $author_field, $author_subfield, $author_relator_subfield ) = get_author_field();
552 my $field = MARC::Field->new(
553 $author_field, '', '',
554 $author_subfield => $author,
555 $author_relator_subfield => 'aut'
558 return $field;
561 subtest 'MARC21' => sub {
562 plan tests => 48;
563 run_tests('MARC21');
564 $schema->storage->txn_rollback;
565 $schema->storage->txn_begin;
568 subtest 'UNIMARC' => sub {
569 plan tests => 48;
571 # Mock the auth type data for UNIMARC
572 $dbh->do("UPDATE auth_types SET auth_tag_to_report = '106' WHERE auth_tag_to_report = '100'") or die $dbh->errstr;
574 run_tests('UNIMARC');
575 $schema->storage->txn_rollback;
576 $schema->storage->txn_begin;
579 subtest 'NORMARC' => sub {
580 plan tests => 48;
581 run_tests('NORMARC');
582 $schema->storage->txn_rollback;
583 $schema->storage->txn_begin;
586 subtest 'IsMarcStructureInternal' => sub {
587 plan tests => 9;
588 my $tagslib = GetMarcStructure();
589 my @internals;
590 for my $tag ( sort keys %$tagslib ) {
591 next unless $tag;
592 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
593 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
596 @internals = uniq @internals;
597 is( scalar(@internals), 7, 'expect 7 internals');
598 is( grep( /^lib$/, @internals ), 1, 'check lib' );
599 is( grep( /^tab$/, @internals ), 1, 'check tab' );
600 is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
601 is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
602 is( grep( /^important$/, @internals ), 1, 'check important' );
603 is( grep( /^a$/, @internals ), 0, 'no subfield a' );
604 is( grep( /^ind1_defaultvalue$/, @internals ), 1, 'check indicator 1 default value' );
605 is( grep( /^ind2_defaultvalue$/, @internals ), 1, 'check indicator 2 default value' );
608 subtest 'deletedbiblio_metadata' => sub {
609 plan tests => 2;
611 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
612 my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber );
613 C4::Biblio::DelBiblio( $biblionumber );
614 my ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio WHERE biblionumber=?|, undef, $biblionumber);
615 is( $moved, $biblionumber, 'Found in deletedbiblio' );
616 ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio_metadata WHERE biblionumber=?|, undef, $biblionumber);
617 is( $moved, $biblionumber, 'Found in deletedbiblio_metadata' );
620 subtest 'DelBiblio' => sub {
621 plan tests => 5;
623 my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio(MARC::Record->new, '');
624 my $deleted = C4::Biblio::DelBiblio( $biblionumber );
625 is( $deleted, undef, 'DelBiblio returns undef is the biblio has been deleted correctly - Must be 1 instead'); # FIXME We should return 1 instead!
627 $deleted = C4::Biblio::DelBiblio( $biblionumber );
628 is( $deleted, undef, 'DelBiblo should return undef is the record did not exist');
630 my $biblio = $builder->build_sample_biblio;
631 my $subscription = $builder->build_object(
633 class => 'Koha::Subscriptions',
634 value => { biblionumber => $biblio->biblionumber }
637 my $serial = $builder->build_object(
639 class => 'Koha::Serials',
640 value => {
641 biblionumber => $biblio->biblionumber,
642 subscriptionid => $subscription->subscriptionid
646 my $subscription_history = $builder->build_object(
648 class => 'Koha::Subscription::Histories',
649 value => {
650 biblionumber => $biblio->biblionumber,
651 subscriptionid => $subscription->subscriptionid
655 C4::Biblio::DelBiblio($biblio->biblionumber); # Or $biblio->delete
656 is( $subscription->get_from_storage, undef, 'subscription should be deleted on biblio deletion' );
657 is( $serial->get_from_storage, undef, 'serial should be deleted on biblio deletion' );
658 is( $subscription_history->get_from_storage, undef, 'subscription history should be deleted on biblio deletion' );
661 subtest 'MarcFieldForCreatorAndModifier' => sub {
662 plan tests => 8;
664 t::lib::Mocks::mock_preference('MarcFieldForCreatorId', '998$a');
665 t::lib::Mocks::mock_preference('MarcFieldForCreatorName', '998$b');
666 t::lib::Mocks::mock_preference('MarcFieldForModifierId', '998$c');
667 t::lib::Mocks::mock_preference('MarcFieldForModifierName', '998$d');
668 my $c4_context = Test::MockModule->new('C4::Context');
669 $c4_context->mock('userenv', sub { return { number => 123, firstname => 'John', surname => 'Doe'}; });
671 my $record = MARC::Record->new();
672 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
674 $record = GetMarcBiblio({biblionumber => $biblionumber});
675 is($record->subfield('998', 'a'), 123, '998$a = 123');
676 is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
677 is($record->subfield('998', 'c'), 123, '998$c = 123');
678 is($record->subfield('998', 'd'), 'John Doe', '998$d = John Doe');
680 $c4_context->mock('userenv', sub { return { number => 321, firstname => 'Jane', surname => 'Doe'}; });
681 C4::Biblio::ModBiblio($record, $biblionumber, '');
683 $record = GetMarcBiblio({biblionumber => $biblionumber});
684 is($record->subfield('998', 'a'), 123, '998$a = 123');
685 is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
686 is($record->subfield('998', 'c'), 321, '998$c = 321');
687 is($record->subfield('998', 'd'), 'Jane Doe', '998$d = Jane Doe');
690 subtest 'ModBiblio called from linker test' => sub {
691 plan tests => 2;
692 my $called = 0;
693 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
694 my $biblio_mod = Test::MockModule->new( 'C4::Biblio' );
695 $biblio_mod->mock( 'LinkBibHeadingsToAuthorities', sub {
696 $called = 1;
698 my $record = MARC::Record->new();
699 my ($biblionumber) = C4::Biblio::AddBiblio($record,'');
700 C4::Biblio::ModBiblio($record,$biblionumber,'');
701 is($called,1,"We called to link bibs because not from linker");
702 $called = 0;
703 C4::Biblio::ModBiblio($record,$biblionumber,'',1);
704 is($called,0,"We didn't call to link bibs because from linker");
707 subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
708 plan tests => 6;
710 # Set up mocks to ensure authorities are generated
711 my $biblio_mod = Test::MockModule->new( 'C4::Linker::Default' );
712 $biblio_mod->mock( 'get_link', sub {
713 return (undef,undef);
715 # UNIMARC valid headings are built from the marc_subfield_structure for bibs and
716 # include all subfields as valid, testing with MARC21 should be sufficient for now
717 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
718 t::lib::Mocks::mock_preference('AutoCreateAuthorities', '1');
720 my $linker = C4::Linker::Default->new();
721 my $record = MARC::Record->new();
723 # Generate a record including all valid subfields and an invalid one 'e'
724 my $field = MARC::Field->new('650','','','a' => 'Beach city', 'b' => 'Weirdness', 'v' => 'Fiction', 'x' => 'Books', 'y' => '21st Century', 'z' => 'Fish Stew Pizza', 'e' => 'Depicted');
726 $record->append_fields($field);
727 my ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef);
729 is( $num_headings_changed, 1, 'We changed the one we passed' );
730 is_deeply( $results->{added},
731 {"Beach city Weirdness--Fiction--Books--21st Century--Fish Stew Pizza" => 1 },
732 "We added an authority record for the heading"
735 # Now we check the authority record itself
736 my $authority = GetAuthority( $record->subfield('650','9') );
737 is( $authority->field('150')->as_string(),
738 "Beach city Weirdness Fiction Books 21st Century Fish Stew Pizza",
739 "The generated record contains the correct subfields"
742 # Example series link with volume and punctuation
743 $record = MARC::Record->new();
744 $field = MARC::Field->new('800','','','a' => 'Tolkien, J. R. R.', 'q' => '(John Ronald Reuel),', 'd' => '1892-1973.', 't' => 'Lord of the rings ;', 'v' => '1');
745 $record->append_fields($field);
747 ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef);
749 is( $num_headings_changed, 1, 'We changed the one we passed' );
750 is_deeply( $results->{added},
751 {"Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings ;" => 1 },
752 "We added an authority record for the heading"
755 # Now we check the authority record itself
756 $authority = GetAuthority( $record->subfield('800','9') );
757 is( $authority->field('100')->as_string(),
758 "Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings",
759 "The generated record contains the correct subfields"
766 # Cleanup
767 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
768 $schema->storage->txn_rollback;