Bug 19382: Adjust comment in test
[koha.git] / t / db_dependent / Koha / Biblio.t
blobcf359edf325a8e53030a5f6b013a3738713abbb7
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 => 12;
22 use C4::Biblio;
23 use Koha::Database;
24 use Koha::Acquisition::Orders;
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
29 BEGIN {
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 {
39 plan tests => 4;
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 {
66 plan tests => 4;
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 {
97 plan tests => 4;
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 {
121 plan tests => 4;
123 $schema->storage->txn_begin;
125 my $builder = t::lib::TestBuilder->new;
126 my $biblio = $builder->build_sample_biblio({
127 title => 'Title 1',
128 author => 'Author 1'
131 $biblio->get_coins,
132 'ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=Title%201&amp;rft.au=Author%201',
133 'GetCOinsBiblio returned right metadata'
136 my $record = MARC::Record->new();
137 $record->append_fields( MARC::Field->new('100','','','a' => 'Author 2'), MARC::Field->new('880','','','a' => 'Something') );
138 my ( $biblionumber ) = C4::Biblio::AddBiblio($record, '');
139 my $biblio_no_title = Koha::Biblios->find($biblionumber);
141 $biblio_no_title->get_coins,
142 'ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.au=Author%202',
143 'GetCOinsBiblio returned right metadata if biblio does not have a title'
146 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
148 $biblio->get_openurl,
149 'https://koha.example.com/?ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=Title%201&amp;rft.au=Author%201',
150 'Koha::Biblio->get_openurl returned right URL'
153 t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
155 $biblio->get_openurl,
156 'https://koha.example.com/?client_id=ci1&amp;ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=Title%201&amp;rft.au=Author%201',
157 'Koha::Biblio->get_openurl returned right URL'
160 $schema->storage->txn_rollback;
163 subtest 'is_serial() tests' => sub {
165 plan tests => 3;
167 $schema->storage->txn_begin;
169 my $biblio = $builder->build_sample_biblio();
171 $biblio->serial( 1 )->store->discard_changes;
172 ok( $biblio->is_serial, 'Bibliographic record is serial' );
174 $biblio->serial( 0 )->store->discard_changes;
175 ok( !$biblio->is_serial, 'Bibliographic record is not serial' );
177 my $record = $biblio->metadata->record;
178 $record->leader('00142nas a22 7a 4500');
179 ModBiblio($record, $biblio->biblionumber );
180 $biblio = Koha::Biblios->find($biblio->biblionumber);
182 ok( $biblio->is_serial, 'Bibliographic record is serial' );
184 $schema->storage->txn_rollback;
187 subtest 'pickup_locations' => sub {
188 plan tests => 29;
190 $schema->storage->txn_begin;
192 my $dbh = C4::Context->dbh;
194 # Cleanup database
195 Koha::Holds->search->delete;
196 Koha::Patrons->search->delete;
197 Koha::Items->search->delete;
198 Koha::Libraries->search->delete;
199 Koha::CirculationRules->search->delete;
200 $dbh->do('DELETE FROM issues');
201 Koha::CirculationRules->set_rules(
203 categorycode => undef,
204 itemtype => undef,
205 branchcode => undef,
206 rules => {
207 reservesallowed => 25,
212 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
213 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
214 my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
216 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
217 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
218 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
219 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
220 my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
221 my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
222 my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
223 my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
225 Koha::CirculationRules->set_rules(
227 branchcode => $library1->branchcode,
228 itemtype => undef,
229 rules => {
230 holdallowed => 1,
231 hold_fulfillment_policy => 'any',
232 returnbranch => 'any'
237 Koha::CirculationRules->set_rules(
239 branchcode => $library2->branchcode,
240 itemtype => undef,
241 rules => {
242 holdallowed => 3,
243 hold_fulfillment_policy => 'holdgroup',
244 returnbranch => 'any'
249 Koha::CirculationRules->set_rules(
251 branchcode => $library3->branchcode,
252 itemtype => undef,
253 rules => {
254 holdallowed => 3,
255 hold_fulfillment_policy => 'patrongroup',
256 returnbranch => 'any'
261 Koha::CirculationRules->set_rules(
263 branchcode => $library4->branchcode,
264 itemtype => undef,
265 rules => {
266 holdallowed => 2,
267 hold_fulfillment_policy => 'holdingbranch',
268 returnbranch => 'any'
273 Koha::CirculationRules->set_rules(
275 branchcode => $library5->branchcode,
276 itemtype => undef,
277 rules => {
278 holdallowed => 2,
279 hold_fulfillment_policy => 'homebranch',
280 returnbranch => 'any'
285 Koha::CirculationRules->set_rules(
287 branchcode => $library6->branchcode,
288 itemtype => undef,
289 rules => {
290 holdallowed => 1,
291 hold_fulfillment_policy => 'holdgroup',
292 returnbranch => 'any'
297 Koha::CirculationRules->set_rules(
299 branchcode => $library7->branchcode,
300 itemtype => undef,
301 rules => {
302 holdallowed => 3,
303 hold_fulfillment_policy => 'holdingbranch',
304 returnbranch => 'any'
310 Koha::CirculationRules->set_rules(
312 branchcode => $library8->branchcode,
313 itemtype => undef,
314 rules => {
315 holdallowed => 2,
316 hold_fulfillment_policy => 'patrongroup',
317 returnbranch => 'any'
322 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
323 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
325 my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
326 my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
328 my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
329 my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
330 my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
331 my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
333 my $biblio1 = $builder->build_sample_biblio({ title => '1' });
334 my $biblio2 = $builder->build_sample_biblio({ title => '2' });
336 my $item1_1 = $builder->build_sample_item({
337 biblionumber => $biblio1->biblionumber,
338 homebranch => $library1->branchcode,
339 holdingbranch => $library2->branchcode,
340 })->store;
342 my $item1_3 = $builder->build_sample_item({
343 biblionumber => $biblio1->biblionumber,
344 homebranch => $library3->branchcode,
345 holdingbranch => $library4->branchcode,
346 })->store;
348 my $item1_7 = $builder->build_sample_item({
349 biblionumber => $biblio1->biblionumber,
350 homebranch => $library7->branchcode,
351 holdingbranch => $library4->branchcode,
352 })->store;
354 my $item2_2 = $builder->build_sample_item({
355 biblionumber => $biblio2->biblionumber,
356 homebranch => $library2->branchcode,
357 holdingbranch => $library1->branchcode,
358 })->store;
360 my $item2_4 = $builder->build_sample_item({
361 biblionumber => $biblio2->biblionumber,
362 homebranch => $library4->branchcode,
363 holdingbranch => $library3->branchcode,
364 })->store;
366 my $item2_6 = $builder->build_sample_item({
367 biblionumber => $biblio2->biblionumber,
368 homebranch => $library6->branchcode,
369 holdingbranch => $library4->branchcode,
370 })->store;
372 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'1', branchcode => $library1->branchcode } } );
373 my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
375 my $results = {
376 "ItemHomeLibrary-1-1" => 6,
377 "ItemHomeLibrary-1-8" => 1,
378 "ItemHomeLibrary-2-1" => 2,
379 "ItemHomeLibrary-2-8" => 0,
380 "PatronLibrary-1-1" => 6,
381 "PatronLibrary-1-8" => 3,
382 "PatronLibrary-2-1" => 0,
383 "PatronLibrary-2-8" => 3,
386 sub _doTest {
387 my ( $cbranch, $biblio, $patron, $results ) = @_;
388 t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
390 my @pl = @{ $biblio->pickup_locations( { patron => $patron} ) };
392 foreach my $pickup_location (@pl) {
393 is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
397 scalar(@pl) == $results->{ $cbranch . '-'
398 . $biblio->title . '-'
399 . $patron->firstname },
400 'ReservesControlBranch: '
401 . $cbranch
402 . ', biblio'
403 . $biblio->title
404 . ', patron'
405 . $patron->firstname
406 . ' should return '
407 . $results->{ $cbranch . '-'
408 . $biblio->title . '-'
409 . $patron->firstname }
410 . ' but returns '
411 . scalar(@pl)
415 foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
416 foreach my $biblio ($biblio1, $biblio2) {
417 foreach my $patron ($patron1, $patron8) {
418 _doTest($cbranch, $biblio, $patron, $results);
423 $schema->storage->txn_rollback;
426 subtest 'to_api() tests' => sub {
428 $schema->storage->txn_begin;
430 my $biblio = $builder->build_sample_biblio();
431 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
433 my $biblioitem_api = $biblio->biblioitem->to_api;
434 my $biblio_api = $biblio->to_api;
436 plan tests => (scalar keys %{ $biblioitem_api }) + 1;
438 foreach my $key ( keys %{ $biblioitem_api } ) {
439 is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" );
442 $biblio_api = $biblio->to_api({ embed => { items => {} } });
443 is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' );
445 $schema->storage->txn_rollback;
448 subtest 'suggestions() tests' => sub {
450 plan tests => 3;
452 $schema->storage->txn_begin;
454 my $biblio = $builder->build_sample_biblio();
456 is( ref($biblio->suggestions), 'Koha::Suggestions', 'Return type is correct' );
458 is_deeply(
459 $biblio->suggestions->unblessed,
461 '->suggestions returns an empty Koha::Suggestions resultset'
464 my $suggestion = $builder->build_object(
466 class => 'Koha::Suggestions',
467 value => { biblionumber => $biblio->biblionumber }
471 my $suggestions = $biblio->suggestions->unblessed;
473 is_deeply(
474 $biblio->suggestions->unblessed,
475 [ $suggestion->unblessed ],
476 '->suggestions returns the related Koha::Suggestion objects'
479 $schema->storage->txn_rollback;
482 subtest 'orders() and active_orders() tests' => sub {
484 plan tests => 5;
486 $schema->storage->txn_begin;
488 my $biblio = $builder->build_sample_biblio();
490 my $orders = $biblio->orders;
491 my $active_orders = $biblio->active_orders;
493 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
494 is( $biblio->orders->count, $biblio->active_orders->count, '->orders->count returns the count for the resultset' );
496 # Add a couple orders
497 foreach (1..2) {
498 $builder->build_object(
500 class => 'Koha::Acquisition::Orders',
501 value => {
502 biblionumber => $biblio->biblionumber,
503 datecancellationprinted => '2019-12-31'
509 $builder->build_object(
511 class => 'Koha::Acquisition::Orders',
512 value => {
513 biblionumber => $biblio->biblionumber,
514 datecancellationprinted => undef
519 $orders = $biblio->orders;
520 $active_orders = $biblio->active_orders;
522 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
523 is( ref($active_orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
524 is( $orders->count, $active_orders->count + 2, '->active_orders->count returns the rigt count' );
526 $schema->storage->txn_rollback;
529 subtest 'subscriptions() tests' => sub {
531 plan tests => 4;
533 $schema->storage->txn_begin;
535 my $biblio = $builder->build_sample_biblio;
537 my $subscriptions = $biblio->subscriptions;
538 is( ref($subscriptions), 'Koha::Subscriptions',
539 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
541 is( $subscriptions->count, 0, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
543 # Add two subscriptions
544 foreach (1..2) {
545 $builder->build_object(
547 class => 'Koha::Subscriptions',
548 value => { biblionumber => $biblio->biblionumber }
553 $subscriptions = $biblio->subscriptions;
554 is( ref($subscriptions), 'Koha::Subscriptions',
555 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
557 is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
559 $schema->storage->txn_rollback;