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 => 12;
24 use Koha::Acquisition::Orders;
26 use t::lib::TestBuilder;
30 use_ok('Koha::Biblio');
31 use_ok('Koha::Biblios');
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'metadata() tests' => sub {
41 $schema->storage->txn_begin;
43 my $title = 'Oranges and Peaches';
45 my $record = MARC::Record->new();
46 my $field = MARC::Field->new('245','','','a' => $title);
47 $record->append_fields( $field );
48 my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
50 my $biblio = Koha::Biblios->find( $biblionumber );
51 is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
53 my $metadata = $biblio->metadata;
54 is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
56 my $record2 = $metadata->record;
57 is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
59 is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
61 $schema->storage->txn_rollback;
64 subtest 'hidden_in_opac() tests' => sub {
68 $schema->storage->txn_begin;
70 my $biblio = $builder->build_sample_biblio();
72 ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden if there is no item attached' );
74 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
75 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
77 $item_1->withdrawn( 1 )->store->discard_changes;
78 $item_2->withdrawn( 1 )->store->discard_changes;
80 ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
82 $item_2->withdrawn( 2 )->store->discard_changes;
83 $biblio->discard_changes; # refresh
85 ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
87 $item_1->withdrawn( 2 )->store->discard_changes;
88 $biblio->discard_changes; # refresh
90 ok( $biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio hidden' );
92 $schema->storage->txn_rollback;
95 subtest 'items() tests' => sub {
99 $schema->storage->txn_begin;
101 my $biblio = $builder->build_sample_biblio();
103 is( $biblio->items->count, 0, 'No items, count is 0' );
105 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
106 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
108 my $items = $biblio->items;
109 is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
110 is( $items->count, 2, 'Two items in resultset' );
112 my @items = $biblio->items->as_list;
113 is( scalar @items, 2, 'Same result, but in list context' );
115 $schema->storage->txn_rollback;
119 subtest 'get_coins and get_openurl' => sub {
123 $schema->storage->txn_begin;
125 my $builder = t::lib::TestBuilder->new;
126 my $biblio = $builder->build_sample_biblio({
133 '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',
134 'GetCOinsBiblio returned right metadata'
137 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
139 $biblio->get_openurl,
140 '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',
141 'Koha::Biblio->get_openurl returned right URL'
144 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
146 $biblio->get_openurl,
147 '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',
148 'Koha::Biblio->get_openurl returned right URL'
151 $schema->storage->txn_rollback;
154 subtest 'is_serial() tests' => sub {
158 $schema->storage->txn_begin;
160 my $biblio = $builder->build_sample_biblio();
162 $biblio->serial( 1 )->store->discard_changes;
163 ok( $biblio->is_serial, 'Bibliographic record is serial' );
165 $biblio->serial( 0 )->store->discard_changes;
166 ok( !$biblio->is_serial, 'Bibliographic record is not serial' );
168 my $record = $biblio->metadata->record;
169 $record->leader('00142nas a22 7a 4500');
170 ModBiblio($record, $biblio->biblionumber );
171 $biblio = Koha::Biblios->find($biblio->biblionumber);
173 ok( $biblio->is_serial, 'Bibliographic record is serial' );
175 $schema->storage->txn_rollback;
178 subtest 'pickup_locations' => sub {
181 $schema->storage->txn_begin;
183 my $dbh = C4::Context->dbh;
186 Koha::Holds->search->delete;
187 Koha::Patrons->search->delete;
188 Koha::Items->search->delete;
189 Koha::Libraries->search->delete;
190 Koha::CirculationRules->search->delete;
191 $dbh->do('DELETE FROM issues');
192 Koha::CirculationRules->set_rules(
194 categorycode => undef,
198 reservesallowed => 25,
203 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
204 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
205 my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
207 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
208 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
209 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
210 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
211 my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
212 my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
213 my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
214 my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
216 Koha::CirculationRules->set_rules(
218 branchcode => $library1->branchcode,
222 hold_fulfillment_policy => 'any',
223 returnbranch => 'any'
228 Koha::CirculationRules->set_rules(
230 branchcode => $library2->branchcode,
234 hold_fulfillment_policy => 'holdgroup',
235 returnbranch => 'any'
240 Koha::CirculationRules->set_rules(
242 branchcode => $library3->branchcode,
246 hold_fulfillment_policy => 'patrongroup',
247 returnbranch => 'any'
252 Koha::CirculationRules->set_rules(
254 branchcode => $library4->branchcode,
258 hold_fulfillment_policy => 'holdingbranch',
259 returnbranch => 'any'
264 Koha::CirculationRules->set_rules(
266 branchcode => $library5->branchcode,
270 hold_fulfillment_policy => 'homebranch',
271 returnbranch => 'any'
276 Koha::CirculationRules->set_rules(
278 branchcode => $library6->branchcode,
282 hold_fulfillment_policy => 'holdgroup',
283 returnbranch => 'any'
288 Koha::CirculationRules->set_rules(
290 branchcode => $library7->branchcode,
294 hold_fulfillment_policy => 'holdingbranch',
295 returnbranch => 'any'
301 Koha::CirculationRules->set_rules(
303 branchcode => $library8->branchcode,
307 hold_fulfillment_policy => 'patrongroup',
308 returnbranch => 'any'
313 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
314 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
316 my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
317 my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
319 my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
320 my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
321 my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
322 my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
324 my $biblio1 = $builder->build_sample_biblio({ title => '1' });
325 my $biblio2 = $builder->build_sample_biblio({ title => '2' });
327 my $item1_1 = $builder->build_sample_item({
328 biblionumber => $biblio1->biblionumber,
329 homebranch => $library1->branchcode,
330 holdingbranch => $library2->branchcode,
333 my $item1_3 = $builder->build_sample_item({
334 biblionumber => $biblio1->biblionumber,
335 homebranch => $library3->branchcode,
336 holdingbranch => $library4->branchcode,
339 my $item1_7 = $builder->build_sample_item({
340 biblionumber => $biblio1->biblionumber,
341 homebranch => $library7->branchcode,
342 holdingbranch => $library4->branchcode,
345 my $item2_2 = $builder->build_sample_item({
346 biblionumber => $biblio2->biblionumber,
347 homebranch => $library2->branchcode,
348 holdingbranch => $library1->branchcode,
351 my $item2_4 = $builder->build_sample_item({
352 biblionumber => $biblio2->biblionumber,
353 homebranch => $library4->branchcode,
354 holdingbranch => $library3->branchcode,
357 my $item2_6 = $builder->build_sample_item({
358 biblionumber => $biblio2->biblionumber,
359 homebranch => $library6->branchcode,
360 holdingbranch => $library4->branchcode,
363 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'1', branchcode => $library1->branchcode } } );
364 my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
367 "ItemHomeLibrary-1-1" => 6,
368 "ItemHomeLibrary-1-8" => 1,
369 "ItemHomeLibrary-2-1" => 2,
370 "ItemHomeLibrary-2-8" => 0,
371 "PatronLibrary-1-1" => 6,
372 "PatronLibrary-1-8" => 3,
373 "PatronLibrary-2-1" => 0,
374 "PatronLibrary-2-8" => 3,
378 my ( $cbranch, $biblio, $patron, $results ) = @_;
379 t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
381 my @pl = @{ $biblio->pickup_locations( { patron => $patron} ) };
383 foreach my $pickup_location (@pl) {
384 is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
388 scalar(@pl) == $results->{ $cbranch . '-'
389 . $biblio->title . '-'
390 . $patron->firstname },
391 'ReservesControlBranch: '
398 . $results->{ $cbranch . '-'
399 . $biblio->title . '-'
400 . $patron->firstname }
406 foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
407 foreach my $biblio ($biblio1, $biblio2) {
408 foreach my $patron ($patron1, $patron8) {
409 _doTest($cbranch, $biblio, $patron, $results);
414 $schema->storage->txn_rollback;
417 subtest 'to_api() tests' => sub {
419 $schema->storage->txn_begin;
421 my $biblio = $builder->build_sample_biblio();
422 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
424 my $biblioitem_api = $biblio->biblioitem->to_api;
425 my $biblio_api = $biblio->to_api;
427 plan tests => (scalar keys %{ $biblioitem_api }) + 1;
429 foreach my $key ( keys %{ $biblioitem_api } ) {
430 is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" );
433 $biblio_api = $biblio->to_api({ embed => { items => {} } });
434 is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' );
436 $schema->storage->txn_rollback;
439 subtest 'suggestions() tests' => sub {
443 $schema->storage->txn_begin;
445 my $biblio = $builder->build_sample_biblio();
447 is( ref($biblio->suggestions), 'Koha::Suggestions', 'Return type is correct' );
450 $biblio->suggestions->unblessed,
452 '->suggestions returns an empty Koha::Suggestions resultset'
455 my $suggestion = $builder->build_object(
457 class => 'Koha::Suggestions',
458 value => { biblionumber => $biblio->biblionumber }
462 my $suggestions = $biblio->suggestions->unblessed;
465 $biblio->suggestions->unblessed,
466 [ $suggestion->unblessed ],
467 '->suggestions returns the related Koha::Suggestion objects'
470 $schema->storage->txn_rollback;
473 subtest 'orders() and active_orders() tests' => sub {
477 $schema->storage->txn_begin;
479 my $biblio = $builder->build_sample_biblio();
481 my $orders = $biblio->orders;
482 my $active_orders = $biblio->active_orders;
484 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
485 is( $biblio->orders->count, $biblio->active_orders->count, '->orders->count returns the count for the resultset' );
487 # Add a couple orders
489 $builder->build_object(
491 class => 'Koha::Acquisition::Orders',
493 biblionumber => $biblio->biblionumber,
494 datecancellationprinted => '2019-12-31'
500 $builder->build_object(
502 class => 'Koha::Acquisition::Orders',
504 biblionumber => $biblio->biblionumber,
505 datecancellationprinted => undef
510 $orders = $biblio->orders;
511 $active_orders = $biblio->active_orders;
513 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
514 is( ref($active_orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
515 is( $orders->count, $active_orders->count + 2, '->active_orders->count returns the rigt count' );
517 $schema->storage->txn_rollback;
520 subtest 'subscriptions() tests' => sub {
524 $schema->storage->txn_begin;
526 my $biblio = $builder->build_sample_biblio;
528 my $subscriptions = $biblio->subscriptions;
529 is( ref($subscriptions), 'Koha::Subscriptions',
530 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
532 is( $subscriptions->count, 0, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
534 # Add two subscriptions
536 $builder->build_object(
538 class => 'Koha::Subscriptions',
539 value => { biblionumber => $biblio->biblionumber }
544 $subscriptions = $biblio->subscriptions;
545 is( ref($subscriptions), 'Koha::Subscriptions',
546 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
548 is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
550 $schema->storage->txn_rollback;