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() tests' => sub {
99 $schema->storage->txn_begin;
101 my $biblio = $builder->build_sample_biblio();
102 my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
103 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
105 my $items = $biblio->items;
106 is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
107 is( $items->count, 2, 'Two items in resultset' );
109 my @items = $biblio->items->as_list;
110 is( scalar @items, 2, 'Same result, but in list context' );
112 $schema->storage->txn_rollback;
116 subtest 'get_coins and get_openurl' => sub {
120 $schema->storage->txn_begin;
122 my $builder = t::lib::TestBuilder->new;
123 my $biblio = $builder->build_sample_biblio({
130 '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',
131 'GetCOinsBiblio returned right metadata'
134 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
136 $biblio->get_openurl,
137 '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',
138 'Koha::Biblio->get_openurl returned right URL'
141 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
143 $biblio->get_openurl,
144 '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',
145 'Koha::Biblio->get_openurl returned right URL'
148 $schema->storage->txn_rollback;
151 subtest 'is_serial() tests' => sub {
155 $schema->storage->txn_begin;
157 my $biblio = $builder->build_sample_biblio();
159 $biblio->serial( 1 )->store->discard_changes;
160 ok( $biblio->is_serial, 'Bibliographic record is serial' );
162 $biblio->serial( 0 )->store->discard_changes;
163 ok( !$biblio->is_serial, 'Bibliographic record is not serial' );
165 my $record = $biblio->metadata->record;
166 $record->leader('00142nas a22 7a 4500');
167 ModBiblio($record, $biblio->biblionumber );
168 $biblio = Koha::Biblios->find($biblio->biblionumber);
170 ok( $biblio->is_serial, 'Bibliographic record is serial' );
172 $schema->storage->txn_rollback;
175 subtest 'pickup_locations' => sub {
178 $schema->storage->txn_begin;
180 my $dbh = C4::Context->dbh;
183 Koha::Holds->search->delete;
184 Koha::Patrons->search->delete;
185 Koha::Items->search->delete;
186 Koha::Libraries->search->delete;
187 $dbh->do('DELETE FROM issues');
188 $dbh->do('DELETE FROM issuingrules');
190 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
191 VALUES (?, ?, ?, ?)},
195 $dbh->do('DELETE FROM circulation_rules');
197 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
198 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
199 my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
201 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
202 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
203 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
204 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
205 my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
206 my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
207 my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
208 my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
210 Koha::CirculationRules->set_rules(
212 branchcode => $library1->branchcode,
214 categorycode => undef,
217 hold_fulfillment_policy => 'any',
218 returnbranch => 'any'
223 Koha::CirculationRules->set_rules(
225 branchcode => $library2->branchcode,
227 categorycode => undef,
230 hold_fulfillment_policy => 'holdgroup',
231 returnbranch => 'any'
236 Koha::CirculationRules->set_rules(
238 branchcode => $library3->branchcode,
240 categorycode => undef,
243 hold_fulfillment_policy => 'patrongroup',
244 returnbranch => 'any'
249 Koha::CirculationRules->set_rules(
251 branchcode => $library4->branchcode,
253 categorycode => undef,
256 hold_fulfillment_policy => 'holdingbranch',
257 returnbranch => 'any'
262 Koha::CirculationRules->set_rules(
264 branchcode => $library5->branchcode,
266 categorycode => undef,
269 hold_fulfillment_policy => 'homebranch',
270 returnbranch => 'any'
275 Koha::CirculationRules->set_rules(
277 branchcode => $library6->branchcode,
279 categorycode => undef,
282 hold_fulfillment_policy => 'holdgroup',
283 returnbranch => 'any'
288 Koha::CirculationRules->set_rules(
290 branchcode => $library7->branchcode,
292 categorycode => undef,
295 hold_fulfillment_policy => 'holdingbranch',
296 returnbranch => 'any'
302 Koha::CirculationRules->set_rules(
304 branchcode => $library8->branchcode,
306 categorycode => undef,
309 hold_fulfillment_policy => 'patrongroup',
310 returnbranch => 'any'
315 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
316 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
318 my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
319 my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
321 my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
322 my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
323 my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
324 my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
326 my $biblio1 = $builder->build_object( { class => 'Koha::Biblios', value => {title => '1'} } );
327 my $biblioitem1 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio1->biblionumber } } );
328 my $biblio2 = $builder->build_object( { class => 'Koha::Biblios', value => {title => '2'} } );
329 my $biblioitem2 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio2->biblionumber } } );
331 my $item1_1 = Koha::Item->new({
332 biblionumber => $biblio1->biblionumber,
333 biblioitemnumber => $biblioitem1->biblioitemnumber,
334 homebranch => $library1->branchcode,
335 holdingbranch => $library2->branchcode,
337 barcode => "item11barcode",
340 my $item1_3 = Koha::Item->new({
341 biblionumber => $biblio1->biblionumber,
342 biblioitemnumber => $biblioitem1->biblioitemnumber,
343 homebranch => $library3->branchcode,
344 holdingbranch => $library4->branchcode,
346 barcode => "item13barcode",
349 my $item1_7 = Koha::Item->new({
350 biblionumber => $biblio1->biblionumber,
351 biblioitemnumber => $biblioitem1->biblioitemnumber,
352 homebranch => $library7->branchcode,
353 holdingbranch => $library4->branchcode,
355 barcode => "item17barcode",
358 my $item2_2 = Koha::Item->new({
359 biblionumber => $biblio2->biblionumber,
360 biblioitemnumber => $biblioitem2->biblioitemnumber,
361 homebranch => $library2->branchcode,
362 holdingbranch => $library1->branchcode,
364 barcode => "item22barcode",
367 my $item2_4 = Koha::Item->new({
368 biblionumber => $biblio2->biblionumber,
369 biblioitemnumber => $biblioitem2->biblioitemnumber,
370 homebranch => $library4->branchcode,
371 holdingbranch => $library3->branchcode,
373 barcode => "item23barcode",
376 my $item2_6 = Koha::Item->new({
377 biblionumber => $biblio2->biblionumber,
378 biblioitemnumber => $biblioitem2->biblioitemnumber,
379 homebranch => $library6->branchcode,
380 holdingbranch => $library4->branchcode,
382 barcode => "item26barcode",
385 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'1', branchcode => $library1->branchcode } } );
386 my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
389 "ItemHomeLibrary-1-1" => 6,
390 "ItemHomeLibrary-1-8" => 1,
391 "ItemHomeLibrary-2-1" => 2,
392 "ItemHomeLibrary-2-8" => 0,
393 "PatronLibrary-1-1" => 6,
394 "PatronLibrary-1-8" => 3,
395 "PatronLibrary-2-1" => 0,
396 "PatronLibrary-2-8" => 3,
400 my ( $cbranch, $biblio, $patron, $results ) = @_;
401 t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
403 my @pl = $biblio->pickup_locations( { patron => $patron} );
405 foreach my $pickup_location (@pl) {
406 is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
410 scalar(@pl) == $results->{ $cbranch . '-'
411 . $biblio->title . '-'
412 . $patron->firstname },
413 'ReservesControlBranch: '
420 . $results->{ $cbranch . '-'
421 . $biblio->title . '-'
422 . $patron->firstname }
428 foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
429 foreach my $biblio ($biblio1, $biblio2) {
430 foreach my $patron ($patron1, $patron8) {
431 _doTest($cbranch, $biblio, $patron, $results);
436 $schema->storage->txn_rollback;
439 subtest 'to_api() tests' => sub {
441 $schema->storage->txn_begin;
443 my $biblio = $builder->build_sample_biblio();
444 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
446 my $biblioitem_api = $biblio->biblioitem->to_api;
447 my $biblio_api = $biblio->to_api;
449 plan tests => (scalar keys %{ $biblioitem_api }) + 1;
451 foreach my $key ( keys %{ $biblioitem_api } ) {
452 is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" );
455 $biblio_api = $biblio->to_api({ embed => { items => {} } });
456 is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' );
458 $schema->storage->txn_rollback;
461 subtest 'suggestions() tests' => sub {
465 $schema->storage->txn_begin;
467 my $biblio = $builder->build_sample_biblio();
469 is( ref($biblio->suggestions), 'Koha::Suggestions', 'Return type is correct' );
472 $biblio->suggestions->unblessed,
474 '->suggestions returns an empty Koha::Suggestions resultset'
477 my $suggestion = $builder->build_object(
479 class => 'Koha::Suggestions',
480 value => { biblionumber => $biblio->biblionumber }
484 my $suggestions = $biblio->suggestions->unblessed;
487 $biblio->suggestions->unblessed,
488 [ $suggestion->unblessed ],
489 '->suggestions returns the related Koha::Suggestion objects'
492 $schema->storage->txn_rollback;
495 subtest 'orders() and active_orders_count() tests' => sub {
499 $schema->storage->txn_begin;
501 my $biblio = $builder->build_sample_biblio();
503 my $orders = $biblio->orders;
504 my $active_orders_count = $biblio->active_orders_count;
506 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
507 is( $orders->count, $active_orders_count, '->orders_count returns the count for the resultset' );
509 # Add a couple orders
511 $builder->build_object(
513 class => 'Koha::Acquisition::Orders',
515 biblionumber => $biblio->biblionumber,
516 datecancellationprinted => '2019-12-31'
522 $builder->build_object(
524 class => 'Koha::Acquisition::Orders',
526 biblionumber => $biblio->biblionumber,
527 datecancellationprinted => undef
532 $orders = $biblio->orders;
533 $active_orders_count = $biblio->active_orders_count;
535 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
536 is( $orders->count, $active_orders_count + 2, '->active_orders_count returns the rigt count' );
538 $schema->storage->txn_rollback;