Bug 24980: Only add timezone if passed a date-time
[koha.git] / t / db_dependent / Biblio.t
blob1d12d29b37cf7ea9c698789c3dfb4a1291a8a23d
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 => 13;
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 BEGIN {
33 use_ok('C4::Biblio');
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
38 my $dbh = C4::Context->dbh;
39 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
41 my $builder = t::lib::TestBuilder->new;
43 subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
44 plan tests => 25;
46 my @columns = qw(
47 tagfield tagsubfield liblibrarian libopac repeatable mandatory kohafield tab
48 authorised_value authtypecode value_builder isurl hidden frameworkcode
49 seealso link defaultvalue maxlength
52 # biblio.biblionumber must be mapped so this should return something
53 my $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber');
55 ok(defined $marc_subfield_structure, "There is a result");
56 is(ref $marc_subfield_structure, "HASH", "Result is a hashref");
57 foreach my $col (@columns) {
58 ok(exists $marc_subfield_structure->{$col}, "Hashref contains key '$col'");
60 is($marc_subfield_structure->{kohafield}, 'biblio.biblionumber', "Result is the good result");
61 like($marc_subfield_structure->{tagfield}, qr/^\d{3}$/, "tagfield is a valid tagfield");
63 # Add a test for list context (BZ 10306)
64 my @results = GetMarcSubfieldStructureFromKohaField('biblio.biblionumber');
65 is( @results, 1, 'We expect only one mapping' );
66 is_deeply( $results[0], $marc_subfield_structure,
67 'The first entry should be the same hashref as we had before' );
69 # foo.bar does not exist so this should return undef
70 $marc_subfield_structure = GetMarcSubfieldStructureFromKohaField('foo.bar');
71 is($marc_subfield_structure, undef, "invalid kohafield returns undef");
75 subtest "GetMarcSubfieldStructure" => sub {
76 plan tests => 5;
78 # Add multiple Koha to Marc mappings
79 Koha::MarcSubfieldStructures->search({ frameworkcode => '', tagfield => '399', tagsubfield => [ 'a', 'b' ] })->delete;
80 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'a', kohafield => "mytable.nicepages" })->store;
81 Koha::MarcSubfieldStructure->new({ frameworkcode => '', tagfield => '399', tagsubfield => 'b', kohafield => "mytable.nicepages" })->store;
82 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
83 my $structure = C4::Biblio::GetMarcSubfieldStructure('');
85 is( @{ $structure->{"mytable.nicepages"} }, 2,
86 'GetMarcSubfieldStructure should return two entries for nicepages' );
87 is( $structure->{"mytable.nicepages"}->[0]->{tagfield}, '399',
88 'Check tagfield for first entry' );
89 is( $structure->{"mytable.nicepages"}->[0]->{tagsubfield}, 'a',
90 'Check tagsubfield for first entry' );
91 is( $structure->{"mytable.nicepages"}->[1]->{tagfield}, '399',
92 'Check tagfield for second entry' );
93 is( $structure->{"mytable.nicepages"}->[1]->{tagsubfield}, 'b',
94 'Check tagsubfield for second entry' );
97 subtest "GetMarcFromKohaField" => sub {
98 plan tests => 8;
100 #NOTE: We are building on data from the previous subtest
101 # With: field 399 / mytable.nicepages
103 # Check call in list context for multiple mappings
104 my @retval = C4::Biblio::GetMarcFromKohaField('mytable.nicepages');
105 is( @retval, 4, 'Should return two tags and subfields' );
106 is( $retval[0], '399', 'Check first tag' );
107 is( $retval[1], 'a', 'Check first subfield' );
108 is( $retval[2], '399', 'Check second tag' );
109 is( $retval[3], 'b', 'Check second subfield' );
111 # Check same call in scalar context
112 is( C4::Biblio::GetMarcFromKohaField('mytable.nicepages'), '399',
113 'GetMarcFromKohaField returns first tag in scalar context' );
115 # Bug 19096 Default is authoritative
116 # If we add a new empty framework, we should still get the mappings
117 # from Default. CAUTION: This test passes intentionally the obsoleted
118 # framework parameter.
119 my $new_fw = t::lib::TestBuilder->new->build({source => 'BiblioFramework'});
120 @retval = C4::Biblio::GetMarcFromKohaField(
121 'mytable.nicepages', $new_fw->{frameworkcode},
123 is( @retval, 4, 'Still got two pairs of tags/subfields' );
124 is( $retval[0].$retval[1], '399a', 'Including 399a' );
127 # Mocking variables
128 my $biblio_module = new Test::MockModule('C4::Biblio');
129 $biblio_module->mock(
130 'GetMarcSubfieldStructure',
131 sub {
132 my ($self) = shift;
134 my ( $title_field, $title_subfield ) = get_title_field();
135 my ( $subtitle_field, $subtitle_subfield ) = get_subtitle_field();
136 my ( $medium_field, $medium_subfield ) = get_medium_field();
137 my ( $part_number_field, $part_number_subfield ) = get_part_number_field();
138 my ( $part_name_field, $part_name_subfield ) = get_part_name_field();
139 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
140 my ( $issn_field, $issn_subfield ) = get_issn_field();
141 my ( $biblionumber_field, $biblionumber_subfield ) = ( '999', 'c' );
142 my ( $biblioitemnumber_field, $biblioitemnumber_subfield ) = ( '999', '9' );
143 my ( $itemnumber_field, $itemnumber_subfield ) = get_itemnumber_field();
145 return {
146 'biblio.title' => [ { tagfield => $title_field, tagsubfield => $title_subfield } ],
147 'biblio.subtitle' => [ { tagfield => $subtitle_field, tagsubfield => $subtitle_subfield } ],
148 'biblio.medium' => [ { tagfield => $medium_field, tagsubfield => $medium_subfield } ],
149 'biblio.part_number' => [ { tagfield => $part_number_field, tagsubfield => $part_number_subfield } ],
150 'biblio.part_name' => [ { tagfield => $part_name_field, tagsubfield => $part_name_subfield } ],
151 'biblio.biblionumber' => [ { tagfield => $biblionumber_field, tagsubfield => $biblionumber_subfield } ],
152 'biblioitems.isbn' => [ { tagfield => $isbn_field, tagsubfield => $isbn_subfield } ],
153 'biblioitems.issn' => [ { tagfield => $issn_field, tagsubfield => $issn_subfield } ],
154 'biblioitems.biblioitemnumber' => [ { tagfield => $biblioitemnumber_field, tagsubfield => $biblioitemnumber_subfield } ],
155 'items.itemnumber' => [ { tagfield => $itemnumber_subfield, tagsubfield => $itemnumber_subfield } ],
160 my $currency = new Test::MockModule('Koha::Acquisition::Currencies');
161 $currency->mock(
162 'get_active',
163 sub {
164 return Koha::Acquisition::Currency->new(
165 { symbol => '$',
166 isocode => 'USD',
167 currency => 'USD',
168 active => 1,
174 sub run_tests {
176 my $marcflavour = shift;
177 t::lib::Mocks::mock_preference('marcflavour', $marcflavour);
179 my $isbn = '0590353403';
180 my $title = 'Foundation';
181 my $subtitle1 = 'Research';
182 my $subtitle2 = 'Conclusions';
183 my $medium = 'Medium';
184 my $part_number = '123';
185 my $part_name = 'First years';
187 # Generate a record with just the ISBN
188 my $marc_record = MARC::Record->new;
189 $marc_record->append_fields( create_isbn_field( $isbn, $marcflavour ) );
191 # Add the record to the DB
192 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
193 my $data = GetBiblioData( $biblionumber );
194 is( $data->{ isbn }, $isbn,
195 '(GetBiblioData) ISBN correctly retireved.');
196 is( $data->{ title }, undef,
197 '(GetBiblioData) Title field is empty in fresh biblio.');
199 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
200 my $marc = GetMarcBiblio({ biblionumber => $biblionumber });
201 is( $marc->subfield( $isbn_field, $isbn_subfield ), $isbn, );
203 # Add title
204 my $field = create_title_field( $title, $marcflavour );
205 $marc_record->append_fields( $field );
206 ModBiblio( $marc_record, $biblionumber ,'' );
207 $data = GetBiblioData( $biblionumber );
208 is( $data->{ title }, $title,
209 'ModBiblio correctly added the title field, and GetBiblioData.');
210 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after ModBiblio.');
211 $marc = GetMarcBiblio({ biblionumber => $biblionumber });
212 my ( $title_field, $title_subfield ) = get_title_field();
213 is( $marc->subfield( $title_field, $title_subfield ), $title, );
215 # Add other fields
216 $marc_record->append_fields( create_field( $subtitle1, $marcflavour, get_subtitle_field() ) );
217 $marc_record->append_fields( create_field( $subtitle2, $marcflavour, get_subtitle_field() ) );
218 $marc_record->append_fields( create_field( $medium, $marcflavour, get_medium_field() ) );
219 $marc_record->append_fields( create_field( $part_number, $marcflavour, get_part_number_field() ) );
220 $marc_record->append_fields( create_field( $part_name, $marcflavour, get_part_name_field() ) );
222 ModBiblio( $marc_record, $biblionumber ,'' );
223 $data = GetBiblioData( $biblionumber );
224 is( $data->{ title }, $title, '(ModBiblio) still there after adding other fields.' );
225 is( $data->{ isbn }, $isbn, '(ModBiblio) ISBN is still there after adding other fields.' );
227 is( $data->{ subtitle }, "$subtitle1 | $subtitle2", '(ModBiblio) subtitles correctly added and returned in GetBiblioData.' );
228 is( $data->{ medium }, $medium, '(ModBiblio) medium correctly added and returned in GetBiblioData.' );
229 is( $data->{ part_number }, $part_number, '(ModBiblio) part_number correctly added and returned in GetBiblioData.' );
230 is( $data->{ part_name }, $part_name, '(ModBiblio) part_name correctly added and returned in GetBiblioData.' );
232 my $biblioitem = Koha::Biblioitems->find( $biblioitemnumber );
233 is( $biblioitem->_result->biblio->title, $title, # Should be $biblioitem->biblio instead, but not needed elsewhere for now
234 'Do not know if this makes sense - compare result of previous two GetBiblioData tests.');
235 is( $biblioitem->isbn, $isbn,
236 'Second test checking it returns the correct isbn.');
238 my $success = 0;
239 $field = MARC::Field->new(
240 655, ' ', ' ',
241 'a' => 'Auction catalogs',
242 '9' => '1'
244 eval {
245 $marc_record->append_fields($field);
246 $success = ModBiblio($marc_record,$biblionumber,'');
247 } or do {
248 diag($@);
249 $success = 0;
251 ok($success, "ModBiblio handles authority-linked 655");
253 eval {
254 $field->delete_subfields('a');
255 $marc_record->append_fields($field);
256 $success = ModBiblio($marc_record,$biblionumber,'');
257 } or do {
258 diag($@);
259 $success = 0;
261 ok($success, "ModBiblio handles 655 with authority link but no heading");
263 eval {
264 $field->delete_subfields('9');
265 $marc_record->append_fields($field);
266 $success = ModBiblio($marc_record,$biblionumber,'');
267 } or do {
268 diag($@);
269 $success = 0;
271 ok($success, "ModBiblio handles 655 with no subfields");
273 ## Testing GetMarcISSN
274 my $issns;
275 $issns = GetMarcISSN( $marc_record, $marcflavour );
276 is( $issns->[0], undef,
277 'GetMarcISSN handles records without the ISSN field (list is empty)' );
278 is( scalar @$issns, 0,
279 'GetMarcISSN handles records without the ISSN field (count is 0)' );
280 # Add an ISSN field
281 my $issn = '1234-1234';
282 $field = create_issn_field( $issn, $marcflavour );
283 $marc_record->append_fields($field);
284 $issns = GetMarcISSN( $marc_record, $marcflavour );
285 is( $issns->[0], $issn,
286 'GetMarcISSN handles records with a single ISSN field (first element is correct)' );
287 is( scalar @$issns, 1,
288 'GetMARCISSN handles records with a single ISSN field (count is 1)');
289 # Add multiple ISSN field
290 my @more_issns = qw/1111-1111 2222-2222 3333-3333/;
291 foreach (@more_issns) {
292 $field = create_issn_field( $_, $marcflavour );
293 $marc_record->append_fields($field);
295 $issns = GetMarcISSN( $marc_record, $marcflavour );
296 is( scalar @$issns, 4,
297 'GetMARCISSN handles records with multiple ISSN fields (count correct)');
298 # Create an empty ISSN
299 $field = create_issn_field( "", $marcflavour );
300 $marc_record->append_fields($field);
301 $issns = GetMarcISSN( $marc_record, $marcflavour );
302 is( scalar @$issns, 4,
303 'GetMARCISSN skips empty ISSN fields (Bug 12674)');
305 ## Testing GetMarcControlnumber
306 my $controlnumber;
307 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
308 is( $controlnumber, '', 'GetMarcControlnumber handles records without 001' );
310 $field = MARC::Field->new( '001', '' );
311 $marc_record->append_fields($field);
312 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
313 is( $controlnumber, '', 'GetMarcControlnumber handles records with empty 001' );
315 $field = $marc_record->field('001');
316 $field->update('123456789X');
317 $controlnumber = GetMarcControlnumber( $marc_record, $marcflavour );
318 is( $controlnumber, '123456789X', 'GetMarcControlnumber handles records with 001' );
320 ## Testing GetMarcISBN
321 my $record_for_isbn = MARC::Record->new();
322 my $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
323 is( scalar @$isbns, 0, '(GetMarcISBN) The record contains no ISBN');
325 # We add one ISBN
326 $isbn_field = create_isbn_field( $isbn, $marcflavour );
327 $record_for_isbn->append_fields( $isbn_field );
328 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
329 is( scalar @$isbns, 1, '(GetMarcISBN) The record contains one ISBN');
330 is( $isbns->[0], $isbn, '(GetMarcISBN) The record contains our ISBN');
332 # We add 3 more ISBNs
333 $record_for_isbn = MARC::Record->new();
334 my @more_isbns = qw/1111111111 2222222222 3333333333 444444444/;
335 foreach (@more_isbns) {
336 $field = create_isbn_field( $_, $marcflavour );
337 $record_for_isbn->append_fields($field);
339 $isbns = GetMarcISBN( $record_for_isbn, $marcflavour );
340 is( scalar @$isbns, 4, '(GetMarcISBN) The record contains 4 ISBNs');
341 for my $i (0 .. $#more_isbns) {
342 is( $isbns->[$i], $more_isbns[$i],
343 "(GetMarcISBN) Correctly retrieves ISBN #". ($i + 1));
346 is( GetMarcPrice( $record_for_isbn, $marcflavour ), 100,
347 "GetMarcPrice returns the correct value");
348 my $newincbiblioitemnumber=$biblioitemnumber+1;
349 $dbh->do("UPDATE biblioitems SET biblioitemnumber = ? WHERE biblionumber = ?;", undef, $newincbiblioitemnumber, $biblionumber );
350 my $updatedrecord = GetMarcBiblio({
351 biblionumber => $biblionumber,
352 embed_items => 0 });
353 my $frameworkcode = GetFrameworkCode($biblionumber);
354 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
355 die qq{No biblioitemnumber tag for framework "$frameworkcode"} unless $biblioitem_tag;
356 my $biblioitemnumbertotest;
357 if ( $biblioitem_tag < 10 ) {
358 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->data();
359 } else {
360 $biblioitemnumbertotest = $updatedrecord->field($biblioitem_tag)->subfield($biblioitem_subfield);
362 is ($newincbiblioitemnumber, $biblioitemnumbertotest, 'Check newincbiblioitemnumber');
364 # test for GetMarcNotes
365 my $a1= GetMarcNotes( $marc_record, $marcflavour );
366 my $field2 = MARC::Field->new( $marcflavour eq 'UNIMARC'? 300: 555, 0, '', a=> 'Some text', u=> 'http://url-1.com', u=> 'nohttp://something_else' );
367 $marc_record->append_fields( $field2 );
368 my $a2= GetMarcNotes( $marc_record, $marcflavour );
369 is( ( $marcflavour eq 'UNIMARC' && @$a2 == @$a1 + 1 ) ||
370 ( $marcflavour ne 'UNIMARC' && @$a2 == @$a1 + 3 ), 1,
371 'Check the number of returned notes of GetMarcNotes' );
373 # test for GetMarcUrls
374 $marc_record->append_fields(
375 MARC::Field->new( '856', '', '', u => ' https://koha-community.org ' ),
376 MARC::Field->new( '856', '', '', u => 'koha-community.org' ),
378 my $marcurl = GetMarcUrls( $marc_record, $marcflavour );
379 is( @$marcurl, 2, 'GetMarcUrls returns two URLs' );
380 like( $marcurl->[0]->{MARCURL}, qr/^https/, 'GetMarcUrls did not stumble over a preceding space' );
381 ok( $marcflavour ne 'MARC21' || $marcurl->[1]->{MARCURL} =~ /^http:\/\//,
382 'GetMarcUrls prefixed a MARC21 URL with http://' );
384 # Automatic authority creation
385 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
386 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1);
387 my $authorities_mod = Test::MockModule->new( 'C4::AuthoritiesMarc' );
388 $authorities_mod->mock(
389 'SearchAuthorities',
390 sub {
391 my @results;
392 return \@results, 0;
395 $success = 0;
396 $field = create_author_field('Author Name');
397 eval {
398 $marc_record->append_fields($field);
399 $success = ModBiblio($marc_record,$biblionumber,'');
400 } or do {
401 diag($@);
402 $success = 0;
404 ok($success, "ModBiblio handles authority addition for author");
406 my ($author_field, $author_subfield, $author_relator_subfield) = get_author_field();
407 $field = $marc_record->field($author_field);
408 ok($field->subfield($author_subfield), "ModBiblio keeps $author_field$author_subfield intact");
410 my $authid = $field->subfield('9');
411 ok($authid, 'ModBiblio adds authority id');
413 use_ok('C4::AuthoritiesMarc');
414 my $auth_record = C4::AuthoritiesMarc::GetAuthority($authid);
415 ok($auth_record, 'Authority record successfully retrieved');
418 my ($auth_author_field, $auth_author_subfield) = get_auth_author_field();
419 $field = $auth_record->field($auth_author_field);
420 ok($field, "Authority record contains field $auth_author_field");
422 $field->subfield($auth_author_subfield),
423 'Author Name',
424 'Authority $auth_author_field$auth_author_subfield contains author name'
426 is($field->subfield($author_relator_subfield), undef, 'Authority does not contain relator subfield');
428 # Reset settings
429 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 0);
430 t::lib::Mocks::mock_preference('AutoCreateAuthorities', 0);
433 sub get_title_field {
434 my $marc_flavour = C4::Context->preference('marcflavour');
435 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'a' ) : ( '245', 'a' );
438 sub get_medium_field {
439 my $marc_flavour = C4::Context->preference('marcflavour');
440 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'b' ) : ( '245', 'h' );
443 sub get_subtitle_field {
444 my $marc_flavour = C4::Context->preference('marcflavour');
445 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'e' ) : ( '245', 'b' );
448 sub get_part_number_field {
449 my $marc_flavour = C4::Context->preference('marcflavour');
450 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'h' ) : ( '245', 'n' );
453 sub get_part_name_field {
454 my $marc_flavour = C4::Context->preference('marcflavour');
455 return ( $marc_flavour eq 'UNIMARC' ) ? ( '200', 'i' ) : ( '245', 'p' );
458 sub get_isbn_field {
459 my $marc_flavour = C4::Context->preference('marcflavour');
460 return ( $marc_flavour eq 'UNIMARC' ) ? ( '010', 'a' ) : ( '020', 'a' );
463 sub get_issn_field {
464 my $marc_flavour = C4::Context->preference('marcflavour');
465 return ( $marc_flavour eq 'UNIMARC' ) ? ( '011', 'a' ) : ( '022', 'a' );
468 sub get_itemnumber_field {
469 my $marc_flavour = C4::Context->preference('marcflavour');
470 return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' );
473 sub get_author_field {
474 my $marc_flavour = C4::Context->preference('marcflavour');
475 return ( $marc_flavour eq 'UNIMARC' ) ? ( '700', 'a', '4' ) : ( '100', 'a', 'e' );
478 sub get_auth_author_field {
479 my $marc_flavour = C4::Context->preference('marcflavour');
480 return ( $marc_flavour eq 'UNIMARC' ) ? ( '106', 'a' ) : ( '100', 'a' );
483 sub create_title_field {
484 my ( $title, $marcflavour ) = @_;
486 my ( $title_field, $title_subfield ) = get_title_field();
487 my $field = MARC::Field->new( $title_field, '', '', $title_subfield => $title );
489 return $field;
492 sub create_field {
493 my ( $content, $marcflavour, $field, $subfield ) = @_;
495 return MARC::Field->new( $field, '', '', $subfield => $content );
498 sub create_isbn_field {
499 my ( $isbn, $marcflavour ) = @_;
501 my ( $isbn_field, $isbn_subfield ) = get_isbn_field();
502 my $field = MARC::Field->new( $isbn_field, '', '', $isbn_subfield => $isbn );
504 # Add the price subfield
505 my $price_subfield = ( $marcflavour eq 'UNIMARC' ) ? 'd' : 'c';
506 $field->add_subfields( $price_subfield => '$100' );
508 return $field;
511 sub create_issn_field {
512 my ( $issn, $marcflavour ) = @_;
514 my ( $issn_field, $issn_subfield ) = get_issn_field();
515 my $field = MARC::Field->new( $issn_field, '', '', $issn_subfield => $issn );
517 return $field;
520 sub create_author_field {
521 my ( $author ) = @_;
523 my ( $author_field, $author_subfield, $author_relator_subfield ) = get_author_field();
524 my $field = MARC::Field->new(
525 $author_field, '', '',
526 $author_subfield => $author,
527 $author_relator_subfield => 'aut'
530 return $field;
533 subtest 'MARC21' => sub {
534 plan tests => 48;
535 run_tests('MARC21');
536 $schema->storage->txn_rollback;
537 $schema->storage->txn_begin;
540 subtest 'UNIMARC' => sub {
541 plan tests => 48;
543 # Mock the auth type data for UNIMARC
544 $dbh->do("UPDATE auth_types SET auth_tag_to_report = '106' WHERE auth_tag_to_report = '100'") or die $dbh->errstr;
546 run_tests('UNIMARC');
547 $schema->storage->txn_rollback;
548 $schema->storage->txn_begin;
551 subtest 'NORMARC' => sub {
552 plan tests => 48;
553 run_tests('NORMARC');
554 $schema->storage->txn_rollback;
555 $schema->storage->txn_begin;
558 subtest 'IsMarcStructureInternal' => sub {
559 plan tests => 9;
560 my $tagslib = GetMarcStructure();
561 my @internals;
562 for my $tag ( sort keys %$tagslib ) {
563 next unless $tag;
564 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
565 push @internals, $subfield if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
568 @internals = uniq @internals;
569 is( scalar(@internals), 7, 'expect 7 internals');
570 is( grep( /^lib$/, @internals ), 1, 'check lib' );
571 is( grep( /^tab$/, @internals ), 1, 'check tab' );
572 is( grep( /^mandatory$/, @internals ), 1, 'check mandatory' );
573 is( grep( /^repeatable$/, @internals ), 1, 'check repeatable' );
574 is( grep( /^important$/, @internals ), 1, 'check important' );
575 is( grep( /^a$/, @internals ), 0, 'no subfield a' );
576 is( grep( /^ind1_defaultvalue$/, @internals ), 1, 'check indicator 1 default value' );
577 is( grep( /^ind2_defaultvalue$/, @internals ), 1, 'check indicator 2 default value' );
580 subtest 'deletedbiblio_metadata' => sub {
581 plan tests => 2;
583 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
584 my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber );
585 C4::Biblio::DelBiblio( $biblionumber );
586 my ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio WHERE biblionumber=?|, undef, $biblionumber);
587 is( $moved, $biblionumber, 'Found in deletedbiblio' );
588 ( $moved ) = $dbh->selectrow_array(q|SELECT biblionumber FROM deletedbiblio_metadata WHERE biblionumber=?|, undef, $biblionumber);
589 is( $moved, $biblionumber, 'Found in deletedbiblio_metadata' );
592 subtest 'DelBiblio' => sub {
593 plan tests => 5;
595 my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio(MARC::Record->new, '');
596 my $deleted = C4::Biblio::DelBiblio( $biblionumber );
597 is( $deleted, undef, 'DelBiblio returns undef is the biblio has been deleted correctly - Must be 1 instead'); # FIXME We should return 1 instead!
599 $deleted = C4::Biblio::DelBiblio( $biblionumber );
600 is( $deleted, undef, 'DelBiblo should return undef is the record did not exist');
602 my $biblio = $builder->build_sample_biblio;
603 my $subscription = $builder->build_object(
605 class => 'Koha::Subscriptions',
606 value => { biblionumber => $biblio->biblionumber }
609 my $serial = $builder->build_object(
611 class => 'Koha::Serials',
612 value => {
613 biblionumber => $biblio->biblionumber,
614 subscriptionid => $subscription->subscriptionid
618 my $subscription_history = $builder->build_object(
620 class => 'Koha::Subscription::Histories',
621 value => {
622 biblionumber => $biblio->biblionumber,
623 subscriptionid => $subscription->subscriptionid
627 C4::Biblio::DelBiblio($biblio->biblionumber); # Or $biblio->delete
628 is( $subscription->get_from_storage, undef, 'subscription should be deleted on biblio deletion' );
629 is( $serial->get_from_storage, undef, 'serial should be deleted on biblio deletion' );
630 is( $subscription_history->get_from_storage, undef, 'subscription history should be deleted on biblio deletion' );
633 subtest 'MarcFieldForCreatorAndModifier' => sub {
634 plan tests => 8;
636 t::lib::Mocks::mock_preference('MarcFieldForCreatorId', '998$a');
637 t::lib::Mocks::mock_preference('MarcFieldForCreatorName', '998$b');
638 t::lib::Mocks::mock_preference('MarcFieldForModifierId', '998$c');
639 t::lib::Mocks::mock_preference('MarcFieldForModifierName', '998$d');
640 my $c4_context = Test::MockModule->new('C4::Context');
641 $c4_context->mock('userenv', sub { return { number => 123, firstname => 'John', surname => 'Doe'}; });
643 my $record = MARC::Record->new();
644 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
646 $record = GetMarcBiblio({biblionumber => $biblionumber});
647 is($record->subfield('998', 'a'), 123, '998$a = 123');
648 is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
649 is($record->subfield('998', 'c'), 123, '998$c = 123');
650 is($record->subfield('998', 'd'), 'John Doe', '998$d = John Doe');
652 $c4_context->mock('userenv', sub { return { number => 321, firstname => 'Jane', surname => 'Doe'}; });
653 C4::Biblio::ModBiblio($record, $biblionumber, '');
655 $record = GetMarcBiblio({biblionumber => $biblionumber});
656 is($record->subfield('998', 'a'), 123, '998$a = 123');
657 is($record->subfield('998', 'b'), 'John Doe', '998$b = John Doe');
658 is($record->subfield('998', 'c'), 321, '998$c = 321');
659 is($record->subfield('998', 'd'), 'Jane Doe', '998$d = Jane Doe');
662 subtest 'ModBiblio called from linker test' => sub {
663 plan tests => 2;
664 my $called = 0;
665 t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1);
666 my $biblio_mod = Test::MockModule->new( 'C4::Biblio' );
667 $biblio_mod->mock( 'LinkBibHeadingsToAuthorities', sub {
668 $called = 1;
670 my $record = MARC::Record->new();
671 my ($biblionumber) = C4::Biblio::AddBiblio($record,'');
672 C4::Biblio::ModBiblio($record,$biblionumber,'');
673 is($called,1,"We called to link bibs because not from linker");
674 $called = 0;
675 C4::Biblio::ModBiblio($record,$biblionumber,'',1);
676 is($called,0,"We didn't call to link bibs because from linker");
679 subtest "LinkBibHeadingsToAuthorities record generation tests" => sub {
680 plan tests => 6;
682 # Set up mocks to ensure authorities are generated
683 my $biblio_mod = Test::MockModule->new( 'C4::Linker::Default' );
684 $biblio_mod->mock( 'get_link', sub {
685 return (undef,undef);
687 # UNIMARC valid headings are built from the marc_subfield_structure for bibs and
688 # include all subfields as valid, testing with MARC21 should be sufficient for now
689 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
690 t::lib::Mocks::mock_preference('AutoCreateAuthorities', '1');
692 my $linker = C4::Linker::Default->new();
693 my $record = MARC::Record->new();
695 # Generate a record including all valid subfields and an invalid one 'e'
696 my $field = MARC::Field->new('650','','','a' => 'Beach city', 'b' => 'Weirdness', 'v' => 'Fiction', 'x' => 'Books', 'y' => '21st Century', 'z' => 'Fish Stew Pizza', 'e' => 'Depicted');
698 $record->append_fields($field);
699 my ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef);
701 is( $num_headings_changed, 1, 'We changed the one we passed' );
702 is_deeply( $results->{added},
703 {"Beach city Weirdness--Fiction--Books--21st Century--Fish Stew Pizza" => 1 },
704 "We added an authority record for the heading"
707 # Now we check the authority record itself
708 my $authority = GetAuthority( $record->subfield('650','9') );
709 is( $authority->field('150')->as_string(),
710 "Beach city Weirdness Fiction Books 21st Century Fish Stew Pizza",
711 "The generated record contains the correct subfields"
714 # Example series link with volume and punctuation
715 $record = MARC::Record->new();
716 $field = MARC::Field->new('800','','','a' => 'Tolkien, J. R. R.', 'q' => '(John Ronald Reuel),', 'd' => '1892-1973.', 't' => 'Lord of the rings ;', 'v' => '1');
717 $record->append_fields($field);
719 ( $num_headings_changed, $results ) = LinkBibHeadingsToAuthorities($linker, $record, "",undef);
721 is( $num_headings_changed, 1, 'We changed the one we passed' );
722 is_deeply( $results->{added},
723 {"Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings ;" => 1 },
724 "We added an authority record for the heading"
727 # Now we check the authority record itself
728 $authority = GetAuthority( $record->subfield('800','9') );
729 is( $authority->field('100')->as_string(),
730 "Tolkien, J. R. R. (John Ronald Reuel), 1892-1973. Lord of the rings",
731 "The generated record contains the correct subfields"
738 # Cleanup
739 Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
740 $schema->storage->txn_rollback;