Bug 24467: (QA follow-up) Clarify tests wording for ->count methods
[koha.git] / t / db_dependent / Koha / Biblio.t
blob2abacff51a5a55d748fdbe023a221454e06bc125
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 => 3;
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'
132 $biblio->get_coins,
133 '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',
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&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=Title%201&amp;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&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',
148 'Koha::Biblio->get_openurl returned right URL'
151 $schema->storage->txn_rollback;
154 subtest 'is_serial() tests' => sub {
156 plan tests => 3;
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 {
179 plan tests => 29;
181 $schema->storage->txn_begin;
183 my $dbh = C4::Context->dbh;
185 # Cleanup database
186 Koha::Holds->search->delete;
187 Koha::Patrons->search->delete;
188 Koha::Items->search->delete;
189 Koha::Libraries->search->delete;
190 $dbh->do('DELETE FROM issues');
191 $dbh->do('DELETE FROM issuingrules');
192 $dbh->do(
193 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
194 VALUES (?, ?, ?, ?)},
196 '*', '*', '*', 25
198 $dbh->do('DELETE FROM circulation_rules');
200 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
201 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
202 my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
204 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
205 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
206 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
207 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
208 my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
209 my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
210 my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
211 my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
213 Koha::CirculationRules->set_rules(
215 branchcode => $library1->branchcode,
216 itemtype => undef,
217 categorycode => undef,
218 rules => {
219 holdallowed => 1,
220 hold_fulfillment_policy => 'any',
221 returnbranch => 'any'
226 Koha::CirculationRules->set_rules(
228 branchcode => $library2->branchcode,
229 itemtype => undef,
230 categorycode => undef,
231 rules => {
232 holdallowed => 3,
233 hold_fulfillment_policy => 'holdgroup',
234 returnbranch => 'any'
239 Koha::CirculationRules->set_rules(
241 branchcode => $library3->branchcode,
242 itemtype => undef,
243 categorycode => undef,
244 rules => {
245 holdallowed => 3,
246 hold_fulfillment_policy => 'patrongroup',
247 returnbranch => 'any'
252 Koha::CirculationRules->set_rules(
254 branchcode => $library4->branchcode,
255 itemtype => undef,
256 categorycode => undef,
257 rules => {
258 holdallowed => 2,
259 hold_fulfillment_policy => 'holdingbranch',
260 returnbranch => 'any'
265 Koha::CirculationRules->set_rules(
267 branchcode => $library5->branchcode,
268 itemtype => undef,
269 categorycode => undef,
270 rules => {
271 holdallowed => 2,
272 hold_fulfillment_policy => 'homebranch',
273 returnbranch => 'any'
278 Koha::CirculationRules->set_rules(
280 branchcode => $library6->branchcode,
281 itemtype => undef,
282 categorycode => undef,
283 rules => {
284 holdallowed => 1,
285 hold_fulfillment_policy => 'holdgroup',
286 returnbranch => 'any'
291 Koha::CirculationRules->set_rules(
293 branchcode => $library7->branchcode,
294 itemtype => undef,
295 categorycode => undef,
296 rules => {
297 holdallowed => 3,
298 hold_fulfillment_policy => 'holdingbranch',
299 returnbranch => 'any'
305 Koha::CirculationRules->set_rules(
307 branchcode => $library8->branchcode,
308 itemtype => undef,
309 categorycode => undef,
310 rules => {
311 holdallowed => 2,
312 hold_fulfillment_policy => 'patrongroup',
313 returnbranch => 'any'
318 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
319 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
321 my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
322 my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
324 my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
325 my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
326 my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
327 my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
329 my $biblio1 = $builder->build_object( { class => 'Koha::Biblios', value => {title => '1'} } );
330 my $biblioitem1 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio1->biblionumber } } );
331 my $biblio2 = $builder->build_object( { class => 'Koha::Biblios', value => {title => '2'} } );
332 my $biblioitem2 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio2->biblionumber } } );
334 my $item1_1 = Koha::Item->new({
335 biblionumber => $biblio1->biblionumber,
336 biblioitemnumber => $biblioitem1->biblioitemnumber,
337 homebranch => $library1->branchcode,
338 holdingbranch => $library2->branchcode,
339 itype => 'test',
340 barcode => "item11barcode",
341 })->store;
343 my $item1_3 = Koha::Item->new({
344 biblionumber => $biblio1->biblionumber,
345 biblioitemnumber => $biblioitem1->biblioitemnumber,
346 homebranch => $library3->branchcode,
347 holdingbranch => $library4->branchcode,
348 itype => 'test',
349 barcode => "item13barcode",
350 })->store;
352 my $item1_7 = Koha::Item->new({
353 biblionumber => $biblio1->biblionumber,
354 biblioitemnumber => $biblioitem1->biblioitemnumber,
355 homebranch => $library7->branchcode,
356 holdingbranch => $library4->branchcode,
357 itype => 'test',
358 barcode => "item17barcode",
359 })->store;
361 my $item2_2 = Koha::Item->new({
362 biblionumber => $biblio2->biblionumber,
363 biblioitemnumber => $biblioitem2->biblioitemnumber,
364 homebranch => $library2->branchcode,
365 holdingbranch => $library1->branchcode,
366 itype => 'test',
367 barcode => "item22barcode",
368 })->store;
370 my $item2_4 = Koha::Item->new({
371 biblionumber => $biblio2->biblionumber,
372 biblioitemnumber => $biblioitem2->biblioitemnumber,
373 homebranch => $library4->branchcode,
374 holdingbranch => $library3->branchcode,
375 itype => 'test',
376 barcode => "item23barcode",
377 })->store;
379 my $item2_6 = Koha::Item->new({
380 biblionumber => $biblio2->biblionumber,
381 biblioitemnumber => $biblioitem2->biblioitemnumber,
382 homebranch => $library6->branchcode,
383 holdingbranch => $library4->branchcode,
384 itype => 'test',
385 barcode => "item26barcode",
386 })->store;
388 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'1', branchcode => $library1->branchcode } } );
389 my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
391 my $results = {
392 "ItemHomeLibrary-1-1" => 6,
393 "ItemHomeLibrary-1-8" => 1,
394 "ItemHomeLibrary-2-1" => 2,
395 "ItemHomeLibrary-2-8" => 0,
396 "PatronLibrary-1-1" => 6,
397 "PatronLibrary-1-8" => 3,
398 "PatronLibrary-2-1" => 0,
399 "PatronLibrary-2-8" => 3,
402 sub _doTest {
403 my ( $cbranch, $biblio, $patron, $results ) = @_;
404 t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
406 my @pl = $biblio->pickup_locations( { patron => $patron} );
408 foreach my $pickup_location (@pl) {
409 is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
413 scalar(@pl) == $results->{ $cbranch . '-'
414 . $biblio->title . '-'
415 . $patron->firstname },
416 'ReservesControlBranch: '
417 . $cbranch
418 . ', biblio'
419 . $biblio->title
420 . ', patron'
421 . $patron->firstname
422 . ' should return '
423 . $results->{ $cbranch . '-'
424 . $biblio->title . '-'
425 . $patron->firstname }
426 . ' but returns '
427 . scalar(@pl)
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 $schema->storage->txn_rollback;
442 subtest 'to_api() tests' => sub {
444 $schema->storage->txn_begin;
446 my $biblio = $builder->build_sample_biblio();
447 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
449 my $biblioitem_api = $biblio->biblioitem->to_api;
450 my $biblio_api = $biblio->to_api;
452 plan tests => (scalar keys %{ $biblioitem_api }) + 1;
454 foreach my $key ( keys %{ $biblioitem_api } ) {
455 is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" );
458 $biblio_api = $biblio->to_api({ embed => { items => {} } });
459 is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' );
461 $schema->storage->txn_rollback;
464 subtest 'suggestions() tests' => sub {
466 plan tests => 3;
468 $schema->storage->txn_begin;
470 my $biblio = $builder->build_sample_biblio();
472 is( ref($biblio->suggestions), 'Koha::Suggestions', 'Return type is correct' );
474 is_deeply(
475 $biblio->suggestions->unblessed,
477 '->suggestions returns an empty Koha::Suggestions resultset'
480 my $suggestion = $builder->build_object(
482 class => 'Koha::Suggestions',
483 value => { biblionumber => $biblio->biblionumber }
487 my $suggestions = $biblio->suggestions->unblessed;
489 is_deeply(
490 $biblio->suggestions->unblessed,
491 [ $suggestion->unblessed ],
492 '->suggestions returns the related Koha::Suggestion objects'
495 $schema->storage->txn_rollback;
498 subtest 'orders() and active_orders() tests' => sub {
500 plan tests => 5;
502 $schema->storage->txn_begin;
504 my $biblio = $builder->build_sample_biblio();
506 my $orders = $biblio->orders;
507 my $active_orders = $biblio->active_orders;
509 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
510 is( $biblio->orders->count, $biblio->active_orders->count, '->orders->count returns the count for the resultset' );
512 # Add a couple orders
513 foreach (1..2) {
514 $builder->build_object(
516 class => 'Koha::Acquisition::Orders',
517 value => {
518 biblionumber => $biblio->biblionumber,
519 datecancellationprinted => '2019-12-31'
525 $builder->build_object(
527 class => 'Koha::Acquisition::Orders',
528 value => {
529 biblionumber => $biblio->biblionumber,
530 datecancellationprinted => undef
535 $orders = $biblio->orders;
536 $active_orders = $biblio->active_orders;
538 is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
539 is( ref($active_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;
545 subtest 'subscriptions() tests' => sub {
547 plan tests => 4;
549 $schema->storage->txn_begin;
551 my $biblio = $builder->build_sample_biblio;
553 my $subscriptions = $biblio->subscriptions;
554 is( ref($subscriptions), 'Koha::Subscriptions',
555 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
557 is( $subscriptions->count, 0, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
559 # Add two subscriptions
560 foreach (1..2) {
561 $builder->build_object(
563 class => 'Koha::Subscriptions',
564 value => { biblionumber => $biblio->biblionumber }
569 $subscriptions = $biblio->subscriptions;
570 is( ref($subscriptions), 'Koha::Subscriptions',
571 'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
573 is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
575 $schema->storage->txn_rollback;