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 => 11;
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() and items_count() 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' );
111 is( $biblio->items_count, $items->count, 'items_count returns the expected value' );
113 my @items = $biblio->items->as_list;
114 is( scalar @items, 2, 'Same result, but in list context' );
116 $schema->storage->txn_rollback;
120 subtest 'get_coins and get_openurl' => sub {
124 $schema->storage->txn_begin;
126 my $builder = t::lib::TestBuilder->new;
127 my $biblio = $builder->build_sample_biblio({
134 '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',
135 'GetCOinsBiblio returned right metadata'
138 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
140 $biblio->get_openurl,
141 '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',
142 'Koha::Biblio->get_openurl returned right URL'
145 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
147 $biblio->get_openurl,
148 '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',
149 'Koha::Biblio->get_openurl returned right URL'
152 $schema->storage->txn_rollback;
155 subtest 'is_serial() tests' => sub {
159 $schema->storage->txn_begin;
161 my $biblio = $builder->build_sample_biblio();
163 $biblio->serial( 1 )->store->discard_changes;
164 ok( $biblio->is_serial, 'Bibliographic record is serial' );
166 $biblio->serial( 0 )->store->discard_changes;
167 ok( !$biblio->is_serial, 'Bibliographic record is not serial' );
169 my $record = $biblio->metadata->record;
170 $record->leader('00142nas a22 7a 4500');
171 ModBiblio($record, $biblio->biblionumber );
172 $biblio = Koha::Biblios->find($biblio->biblionumber);
174 ok( $biblio->is_serial, 'Bibliographic record is serial' );
176 $schema->storage->txn_rollback;
179 subtest 'pickup_locations' => sub {
182 $schema->storage->txn_begin;
184 my $dbh = C4::Context->dbh;
187 Koha::Holds->search->delete;
188 Koha::Patrons->search->delete;
189 Koha::Items->search->delete;
190 Koha::Libraries->search->delete;
191 $dbh->do('DELETE FROM issues');
192 $dbh->do('DELETE FROM issuingrules');
194 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
195 VALUES (?, ?, ?, ?)},
199 $dbh->do('DELETE FROM circulation_rules');
201 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
202 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
203 my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
205 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
206 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
207 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
208 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
209 my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
210 my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
211 my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
212 my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
214 Koha::CirculationRules->set_rules(
216 branchcode => $library1->branchcode,
218 categorycode => undef,
221 hold_fulfillment_policy => 'any',
222 returnbranch => 'any'
227 Koha::CirculationRules->set_rules(
229 branchcode => $library2->branchcode,
231 categorycode => undef,
234 hold_fulfillment_policy => 'holdgroup',
235 returnbranch => 'any'
240 Koha::CirculationRules->set_rules(
242 branchcode => $library3->branchcode,
244 categorycode => undef,
247 hold_fulfillment_policy => 'patrongroup',
248 returnbranch => 'any'
253 Koha::CirculationRules->set_rules(
255 branchcode => $library4->branchcode,
257 categorycode => undef,
260 hold_fulfillment_policy => 'holdingbranch',
261 returnbranch => 'any'
266 Koha::CirculationRules->set_rules(
268 branchcode => $library5->branchcode,
270 categorycode => undef,
273 hold_fulfillment_policy => 'homebranch',
274 returnbranch => 'any'
279 Koha::CirculationRules->set_rules(
281 branchcode => $library6->branchcode,
283 categorycode => undef,
286 hold_fulfillment_policy => 'holdgroup',
287 returnbranch => 'any'
292 Koha::CirculationRules->set_rules(
294 branchcode => $library7->branchcode,
296 categorycode => undef,
299 hold_fulfillment_policy => 'holdingbranch',
300 returnbranch => 'any'
306 Koha::CirculationRules->set_rules(
308 branchcode => $library8->branchcode,
310 categorycode => undef,
313 hold_fulfillment_policy => 'patrongroup',
314 returnbranch => 'any'
319 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
320 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
322 my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
323 my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
325 my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
326 my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
327 my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
328 my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
330 my $biblio1 = $builder->build_object( { class => 'Koha::Biblios', value => {title => '1'} } );
331 my $biblioitem1 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio1->biblionumber } } );
332 my $biblio2 = $builder->build_object( { class => 'Koha::Biblios', value => {title => '2'} } );
333 my $biblioitem2 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio2->biblionumber } } );
335 my $item1_1 = Koha::Item->new({
336 biblionumber => $biblio1->biblionumber,
337 biblioitemnumber => $biblioitem1->biblioitemnumber,
338 homebranch => $library1->branchcode,
339 holdingbranch => $library2->branchcode,
341 barcode => "item11barcode",
344 my $item1_3 = Koha::Item->new({
345 biblionumber => $biblio1->biblionumber,
346 biblioitemnumber => $biblioitem1->biblioitemnumber,
347 homebranch => $library3->branchcode,
348 holdingbranch => $library4->branchcode,
350 barcode => "item13barcode",
353 my $item1_7 = Koha::Item->new({
354 biblionumber => $biblio1->biblionumber,
355 biblioitemnumber => $biblioitem1->biblioitemnumber,
356 homebranch => $library7->branchcode,
357 holdingbranch => $library4->branchcode,
359 barcode => "item17barcode",
362 my $item2_2 = Koha::Item->new({
363 biblionumber => $biblio2->biblionumber,
364 biblioitemnumber => $biblioitem2->biblioitemnumber,
365 homebranch => $library2->branchcode,
366 holdingbranch => $library1->branchcode,
368 barcode => "item22barcode",
371 my $item2_4 = Koha::Item->new({
372 biblionumber => $biblio2->biblionumber,
373 biblioitemnumber => $biblioitem2->biblioitemnumber,
374 homebranch => $library4->branchcode,
375 holdingbranch => $library3->branchcode,
377 barcode => "item23barcode",
380 my $item2_6 = Koha::Item->new({
381 biblionumber => $biblio2->biblionumber,
382 biblioitemnumber => $biblioitem2->biblioitemnumber,
383 homebranch => $library6->branchcode,
384 holdingbranch => $library4->branchcode,
386 barcode => "item26barcode",
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);
407 my @pl = $biblio->pickup_locations( { patron => $patron} );
409 foreach my $pickup_location (@pl) {
410 is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
414 scalar(@pl) == $results->{ $cbranch . '-'
415 . $biblio->title . '-'
416 . $patron->firstname },
417 'ReservesControlBranch: '
424 . $results->{ $cbranch . '-'
425 . $biblio->title . '-'
426 . $patron->firstname }
432 foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
433 foreach my $biblio ($biblio1, $biblio2) {
434 foreach my $patron ($patron1, $patron8) {
435 _doTest($cbranch, $biblio, $patron, $results);
440 $schema->storage->txn_rollback;
443 subtest 'to_api() tests' => sub {
445 $schema->storage->txn_begin;
447 my $biblio = $builder->build_sample_biblio();
448 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
450 my $biblioitem_api = $biblio->biblioitem->to_api;
451 my $biblio_api = $biblio->to_api;
453 plan tests => (scalar keys %{ $biblioitem_api }) + 1;
455 foreach my $key ( keys %{ $biblioitem_api } ) {
456 is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" );
459 $biblio_api = $biblio->to_api({ embed => { items => {} } });
460 is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' );
462 $schema->storage->txn_rollback;
465 subtest 'suggestions() tests' => sub {
469 $schema->storage->txn_begin;
471 my $biblio = $builder->build_sample_biblio();
473 is( ref($biblio->suggestions), 'Koha::Suggestions', 'Return type is correct' );
476 $biblio->suggestions->unblessed,
478 '->suggestions returns an empty Koha::Suggestions resultset'
481 my $suggestion = $builder->build_object(
483 class => 'Koha::Suggestions',
484 value => { biblionumber => $biblio->biblionumber }
488 my $suggestions = $biblio->suggestions->unblessed;
491 $biblio->suggestions->unblessed,
492 [ $suggestion->unblessed ],
493 '->suggestions returns the related Koha::Suggestion objects'
496 $schema->storage->txn_rollback;
499 subtest 'orders() and active_orders_count() tests' => sub {
503 $schema->storage->txn_begin;
505 my $biblio = $builder->build_sample_biblio();
507 my $orders = $biblio->orders;
508 my $active_orders_count = $biblio->active_orders_count;
510 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
511 is( $orders->count, $active_orders_count, '->orders_count returns the count for the resultset' );
513 # Add a couple orders
515 $builder->build_object(
517 class => 'Koha::Acquisition::Orders',
519 biblionumber => $biblio->biblionumber,
520 datecancellationprinted => '2019-12-31'
526 $builder->build_object(
528 class => 'Koha::Acquisition::Orders',
530 biblionumber => $biblio->biblionumber,
531 datecancellationprinted => undef
536 $orders = $biblio->orders;
537 $active_orders_count = $biblio->active_orders_count;
539 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
540 is( $orders->count, $active_orders_count + 2, '->active_orders_count returns the rigt count' );
542 $schema->storage->txn_rollback;