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 => 14;
22 use C4::Biblio qw( AddBiblio ModBiblio );
24 use Koha::Acquisition::Orders;
25 use Koha::AuthorisedValueCategories;
26 use Koha::AuthorisedValues;
27 use Koha::MarcSubfieldStructures;
29 use t::lib::TestBuilder;
33 use_ok('Koha::Biblio');
34 use_ok('Koha::Biblios');
37 my $schema = Koha::Database->new->schema;
38 my $builder = t::lib::TestBuilder->new;
40 subtest 'metadata() tests' => sub {
44 $schema->storage->txn_begin;
46 my $title = 'Oranges and Peaches';
48 my $record = MARC::Record->new();
49 my $field = MARC::Field->new('245','','','a' => $title);
50 $record->append_fields( $field );
51 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
53 my $biblio = Koha::Biblios->find( $biblionumber );
54 is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
56 my $metadata = $biblio->metadata;
57 is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
59 my $record2 = $metadata->record;
60 is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
62 is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
64 $schema->storage->txn_rollback;
67 subtest 'hidden_in_opac() tests' => sub {
71 $schema->storage->txn_begin;
73 my $biblio = $builder->build_sample_biblio();
74 my $rules = { withdrawn => [ 2 ] };
76 t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 0 );
79 !$biblio->hidden_in_opac({ rules => $rules }),
80 'Biblio not hidden if there is no item attached (!OpacHiddenItemsHidesRecord)'
83 t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 1 );
86 !$biblio->hidden_in_opac({ rules => $rules }),
87 'Biblio not hidden if there is no item attached (OpacHiddenItemsHidesRecord)'
90 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
91 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
93 $item_1->withdrawn( 1 )->store->discard_changes;
94 $item_2->withdrawn( 1 )->store->discard_changes;
96 ok( !$biblio->hidden_in_opac({ rules => $rules }), 'Biblio not hidden' );
98 $item_2->withdrawn( 2 )->store->discard_changes;
99 $biblio->discard_changes; # refresh
101 ok( !$biblio->hidden_in_opac({ rules => $rules }), 'Biblio not hidden' );
103 $item_1->withdrawn( 2 )->store->discard_changes;
104 $biblio->discard_changes; # refresh
106 ok( $biblio->hidden_in_opac({ rules => $rules }), 'Biblio hidden' );
108 t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 0 );
110 !$biblio->hidden_in_opac( { rules => $rules } ),
111 'Biblio hidden (!OpacHiddenItemsHidesRecord)'
115 $schema->storage->txn_rollback;
118 subtest 'items() tests' => sub {
122 $schema->storage->txn_begin;
124 my $biblio = $builder->build_sample_biblio();
126 is( $biblio->items->count, 0, 'No items, count is 0' );
128 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
129 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
131 my $items = $biblio->items;
132 is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
133 is( $items->count, 2, 'Two items in resultset' );
135 my @items = $biblio->items->as_list;
136 is( scalar @items, 2, 'Same result, but in list context' );
138 $schema->storage->txn_rollback;
142 subtest 'get_coins and get_openurl' => sub {
146 $schema->storage->txn_begin;
148 my $builder = t::lib::TestBuilder->new;
149 my $biblio = $builder->build_sample_biblio({
155 'ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Title%201&rft.au=Author%201',
156 'GetCOinsBiblio returned right metadata'
159 my $record = MARC::Record->new();
160 $record->append_fields( MARC::Field->new('100','','','a' => 'Author 2'), MARC::Field->new('880','','','a' => 'Something') );
161 my ( $biblionumber ) = C4::Biblio::AddBiblio($record, '');
162 my $biblio_no_title = Koha::Biblios->find($biblionumber);
164 $biblio_no_title->get_coins,
165 'ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.au=Author%202',
166 'GetCOinsBiblio returned right metadata if biblio does not have a title'
169 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
171 $biblio->get_openurl,
172 'https://koha.example.com/?ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Title%201&rft.au=Author%201',
173 'Koha::Biblio->get_openurl returned right URL'
176 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
178 $biblio->get_openurl,
179 'https://koha.example.com/?client_id=ci1&ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=Title%201&rft.au=Author%201',
180 'Koha::Biblio->get_openurl returned right URL'
183 $schema->storage->txn_rollback;
186 subtest 'is_serial() tests' => sub {
190 $schema->storage->txn_begin;
192 my $biblio = $builder->build_sample_biblio();
194 $biblio->serial( 1 )->store->discard_changes;
195 ok( $biblio->is_serial, 'Bibliographic record is serial' );
197 $biblio->serial( 0 )->store->discard_changes;
198 ok( !$biblio->is_serial, 'Bibliographic record is not serial' );
200 my $record = $biblio->metadata->record;
201 $record->leader('00142nas a22 7a 4500');
202 ModBiblio($record, $biblio->biblionumber );
203 $biblio = Koha::Biblios->find($biblio->biblionumber);
205 ok( $biblio->is_serial, 'Bibliographic record is serial' );
207 $schema->storage->txn_rollback;
210 subtest 'pickup_locations' => sub {
213 $schema->storage->txn_begin;
215 Koha::CirculationRules->search->delete;
216 Koha::CirculationRules->set_rules(
218 categorycode => undef,
222 reservesallowed => 25,
227 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
228 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
229 my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
231 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'zzz' } } );
232 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'AAA' } } );
233 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, branchname => 'FFF' } } );
234 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'CCC' } } );
235 my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'eee' } } );
236 my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'BBB' } } );
237 my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'DDD' } } );
238 my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, branchname => 'GGG' } } );
240 our @branchcodes = map { $_->branchcode } ($library1, $library2, $library3, $library4, $library5, $library6, $library7, $library8);
242 Koha::CirculationRules->set_rules(
244 branchcode => $library1->branchcode,
247 holdallowed => 'from_home_library',
248 hold_fulfillment_policy => 'any',
249 returnbranch => 'any'
254 Koha::CirculationRules->set_rules(
256 branchcode => $library2->branchcode,
259 holdallowed => 'from_local_hold_group',
260 hold_fulfillment_policy => 'holdgroup',
261 returnbranch => 'any'
266 Koha::CirculationRules->set_rules(
268 branchcode => $library3->branchcode,
271 holdallowed => 'from_local_hold_group',
272 hold_fulfillment_policy => 'patrongroup',
273 returnbranch => 'any'
278 Koha::CirculationRules->set_rules(
280 branchcode => $library4->branchcode,
283 holdallowed => 'from_any_library',
284 hold_fulfillment_policy => 'holdingbranch',
285 returnbranch => 'any'
290 Koha::CirculationRules->set_rules(
292 branchcode => $library5->branchcode,
295 holdallowed => 'from_any_library',
296 hold_fulfillment_policy => 'homebranch',
297 returnbranch => 'any'
302 Koha::CirculationRules->set_rules(
304 branchcode => $library6->branchcode,
307 holdallowed => 'from_home_library',
308 hold_fulfillment_policy => 'holdgroup',
309 returnbranch => 'any'
314 Koha::CirculationRules->set_rules(
316 branchcode => $library7->branchcode,
319 holdallowed => 'from_local_hold_group',
320 hold_fulfillment_policy => 'holdingbranch',
321 returnbranch => 'any'
327 Koha::CirculationRules->set_rules(
329 branchcode => $library8->branchcode,
332 holdallowed => 'from_any_library',
333 hold_fulfillment_policy => 'patrongroup',
334 returnbranch => 'any'
339 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
340 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
342 my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
343 my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
345 my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
346 my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
347 my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
348 my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
350 my $biblio1 = $builder->build_sample_biblio({ title => '1' });
351 my $biblio2 = $builder->build_sample_biblio({ title => '2' });
353 my $item1_1 = $builder->build_sample_item({
354 biblionumber => $biblio1->biblionumber,
355 homebranch => $library1->branchcode,
356 holdingbranch => $library2->branchcode,
359 my $item1_3 = $builder->build_sample_item({
360 biblionumber => $biblio1->biblionumber,
361 homebranch => $library3->branchcode,
362 holdingbranch => $library4->branchcode,
365 my $item1_7 = $builder->build_sample_item({
366 biblionumber => $biblio1->biblionumber,
367 homebranch => $library7->branchcode,
368 holdingbranch => $library4->branchcode,
371 my $item2_2 = $builder->build_sample_item({
372 biblionumber => $biblio2->biblionumber,
373 homebranch => $library2->branchcode,
374 holdingbranch => $library1->branchcode,
377 my $item2_4 = $builder->build_sample_item({
378 biblionumber => $biblio2->biblionumber,
379 homebranch => $library4->branchcode,
380 holdingbranch => $library3->branchcode,
383 my $item2_6 = $builder->build_sample_item({
384 biblionumber => $biblio2->biblionumber,
385 homebranch => $library6->branchcode,
386 holdingbranch => $library4->branchcode,
389 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'1', branchcode => $library1->branchcode } } );
390 my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
393 "ItemHomeLibrary-1-1" => 6,
394 "ItemHomeLibrary-1-8" => 1,
395 "ItemHomeLibrary-2-1" => 2,
396 "ItemHomeLibrary-2-8" => 0,
397 "PatronLibrary-1-1" => 6,
398 "PatronLibrary-1-8" => 3,
399 "PatronLibrary-2-1" => 0,
400 "PatronLibrary-2-8" => 3,
404 my ( $cbranch, $biblio, $patron, $results ) = @_;
405 t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
408 my $pickup_location = $_;
409 grep { $pickup_location->branchcode eq $_ } @branchcodes
410 } $biblio->pickup_locations( { patron => $patron } )->as_list;
413 scalar(@pl) == $results->{ $cbranch . '-'
414 . $biblio->title . '-'
415 . $patron->firstname },
416 'ReservesControlBranch: '
423 . $results->{ $cbranch . '-'
424 . $biblio->title . '-'
425 . $patron->firstname }
431 foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
432 foreach my $biblio ($biblio1, $biblio2) {
433 foreach my $patron ($patron1, $patron8) {
434 _doTest($cbranch, $biblio, $patron, $results);
439 my @pl_names = map { $_->branchname } $biblio1->pickup_locations( { patron => $patron1 } )->as_list;
440 my $pl_ori_str = join('|', @pl_names);
441 my $pl_sorted_str = join('|', sort { lc($a) cmp lc($b) } @pl_names);
443 $pl_ori_str eq $pl_sorted_str,
444 'Libraries must be sorted by name'
446 $schema->storage->txn_rollback;
449 subtest 'to_api() tests' => sub {
451 $schema->storage->txn_begin;
453 my $biblio = $builder->build_sample_biblio();
454 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
456 my $biblioitem_api = $biblio->biblioitem->to_api;
457 my $biblio_api = $biblio->to_api;
459 plan tests => (scalar keys %{ $biblioitem_api }) + 1;
461 foreach my $key ( keys %{ $biblioitem_api } ) {
462 is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" );
465 $biblio_api = $biblio->to_api({ embed => { items => {} } });
466 is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' );
468 $schema->storage->txn_rollback;
471 subtest 'suggestions() tests' => sub {
475 $schema->storage->txn_begin;
477 my $biblio = $builder->build_sample_biblio();
479 is( ref($biblio->suggestions), 'Koha::Suggestions', 'Return type is correct' );
482 $biblio->suggestions->unblessed,
484 '->suggestions returns an empty Koha::Suggestions resultset'
487 my $suggestion = $builder->build_object(
489 class => 'Koha::Suggestions',
490 value => { biblionumber => $biblio->biblionumber }
494 my $suggestions = $biblio->suggestions->unblessed;
497 $biblio->suggestions->unblessed,
498 [ $suggestion->unblessed ],
499 '->suggestions returns the related Koha::Suggestion objects'
502 $schema->storage->txn_rollback;
505 subtest 'orders() and active_orders() tests' => sub {
509 $schema->storage->txn_begin;
511 my $biblio = $builder->build_sample_biblio();
513 my $orders = $biblio->orders;
514 my $active_orders = $biblio->active_orders;
516 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
517 is( $biblio->orders->count, $biblio->active_orders->count, '->orders->count returns the count for the resultset' );
519 # Add a couple orders
521 $builder->build_object(
523 class => 'Koha::Acquisition::Orders',
525 biblionumber => $biblio->biblionumber,
526 datecancellationprinted => '2019-12-31'
532 $builder->build_object(
534 class => 'Koha::Acquisition::Orders',
536 biblionumber => $biblio->biblionumber,
537 datecancellationprinted => undef
542 $orders = $biblio->orders;
543 $active_orders = $biblio->active_orders;
545 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
546 is( ref($active_orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
547 is( $orders->count, $active_orders->count + 2, '->active_orders->count returns the rigt count' );
549 $schema->storage->txn_rollback;
552 subtest 'subscriptions() tests' => sub {
556 $schema->storage->txn_begin;
558 my $biblio = $builder->build_sample_biblio;
560 my $subscriptions = $biblio->subscriptions;
561 is( ref($subscriptions), 'Koha::Subscriptions',
562 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
564 is( $subscriptions->count, 0, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
566 # Add two subscriptions
568 $builder->build_object(
570 class => 'Koha::Subscriptions',
571 value => { biblionumber => $biblio->biblionumber }
576 $subscriptions = $biblio->subscriptions;
577 is( ref($subscriptions), 'Koha::Subscriptions',
578 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
580 is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
582 $schema->storage->txn_rollback;
585 subtest 'get_marc_notes() MARC21 tests' => sub {
588 $schema->storage->txn_begin;
590 t::lib::Mocks::mock_preference( 'NotesToHide', '520' );
592 my $biblio = $builder->build_sample_biblio;
593 my $record = $biblio->metadata->record;
594 $record->append_fields(
595 MARC::Field->new( '500', '', '', a => 'Note1' ),
596 MARC::Field->new( '505', '', '', a => 'Note2', u => 'http://someserver.com' ),
597 MARC::Field->new( '520', '', '', a => 'Note3 skipped' ),
598 MARC::Field->new( '541', '0', '', a => 'Note4 skipped on opac' ),
599 MARC::Field->new( '541', '', '', a => 'Note5' ),
600 MARC::Field->new( '590', '', '', a => 'CODE' ),
603 Koha::AuthorisedValueCategory->new({ category_name => 'TEST' })->store;
604 Koha::AuthorisedValue->new({ category => 'TEST', authorised_value => 'CODE', lib => 'Description should show', lib_opac => 'Description should show OPAC' })->store;
605 my $mss = Koha::MarcSubfieldStructures->find({tagfield => "590", tagsubfield => "a", frameworkcode => $biblio->frameworkcode });
606 $mss->update({ authorised_value => "TEST" });
608 C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
609 $biblio = Koha::Biblios->find( $biblio->biblionumber);
611 my $notes = $biblio->get_marc_notes({ marcflavour => 'MARC21' });
612 is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
613 is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
614 is( $notes->[2]->{marcnote}, 'http://someserver.com', 'URL separated' );
615 is( $notes->[3]->{marcnote}, 'Note4 skipped on opac',"Not shows if not opac" );
616 is( $notes->[4]->{marcnote}, 'Note5', 'Fifth note' );
617 is( $notes->[5]->{marcnote}, 'Description should show', 'Authorised value is correctly parsed to show description rather than code' );
618 is( @$notes, 6, 'No more notes' );
619 $notes = $biblio->get_marc_notes({ marcflavour => 'MARC21', opac => 1 });
620 is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
621 is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
622 is( $notes->[2]->{marcnote}, 'http://someserver.com', 'URL separated' );
623 is( $notes->[3]->{marcnote}, 'Note5', 'Fifth note shows after fourth skipped' );
624 is( $notes->[4]->{marcnote}, 'Description should show OPAC', 'Authorised value is correctly parsed for OPAC to show description rather than code' );
625 is( @$notes, 5, 'No more notes' );
627 $schema->storage->txn_rollback;
630 subtest 'get_marc_notes() UNIMARC tests' => sub {
633 $schema->storage->txn_begin;
635 t::lib::Mocks::mock_preference( 'NotesToHide', '310' );
637 my $biblio = $builder->build_sample_biblio;
638 my $record = $biblio->metadata->record;
639 $record->append_fields(
640 MARC::Field->new( '300', '', '', a => 'Note1' ),
641 MARC::Field->new( '300', '', '', a => 'Note2' ),
642 MARC::Field->new( '310', '', '', a => 'Note3 skipped' ),
644 C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
645 $biblio = Koha::Biblios->find( $biblio->biblionumber);
646 my $notes = $biblio->get_marc_notes({ marcflavour => 'UNIMARC' });
647 is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
648 is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
649 is( @$notes, 2, 'No more notes' );
651 $schema->storage->txn_rollback;