Bug 35248: Add unit tests for Koha::Bibilio->bookable_items
[koha.git] / t / db_dependent / Koha / Biblio.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 33;
21 use Test::Exception;
22 use Test::Warn;
23
24 use C4::Biblio qw( AddBiblio ModBiblio ModBiblioMarc );
25 use C4::Circulation qw( AddIssue AddReturn );
26
27 use Koha::Database;
28 use Koha::DateUtils qw( dt_from_string );
29 use Koha::Cache::Memory::Lite;
30 use Koha::Caches;
31 use Koha::Acquisition::Orders;
32 use Koha::AuthorisedValueCategories;
33 use Koha::AuthorisedValues;
34 use Koha::MarcSubfieldStructures;
35 use Koha::Exception;
36
37 use MARC::Field;
38 use MARC::Record;
39
40 use t::lib::TestBuilder;
41 use t::lib::Mocks;
42 use Test::MockModule;
43
44 BEGIN {
45     use_ok('Koha::Biblio');
46     use_ok('Koha::Biblios');
47 }
48
49 my $schema  = Koha::Database->new->schema;
50 my $builder = t::lib::TestBuilder->new;
51
52 subtest 'metadata() tests' => sub {
53
54     plan tests => 4;
55
56     $schema->storage->txn_begin;
57
58     my $title = 'Oranges and Peaches';
59
60     my $record = MARC::Record->new();
61     my $field = MARC::Field->new('245','','','a' => $title);
62     $record->append_fields( $field );
63     my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
64
65     my $biblio = Koha::Biblios->find( $biblionumber );
66     is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
67
68     my $metadata = $biblio->metadata;
69     is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
70
71     my $record2 = $metadata->record;
72     is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
73
74     is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
75
76     $schema->storage->txn_rollback;
77 };
78
79 subtest 'hidden_in_opac() tests' => sub {
80
81     plan tests => 6;
82
83     $schema->storage->txn_begin;
84
85     my $biblio = $builder->build_sample_biblio();
86     my $rules  = { withdrawn => [ 2 ] };
87
88     t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 0 );
89
90     ok(
91         !$biblio->hidden_in_opac({ rules => $rules }),
92         'Biblio not hidden if there is no item attached (!OpacHiddenItemsHidesRecord)'
93     );
94
95     t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 1 );
96
97     ok(
98         !$biblio->hidden_in_opac({ rules => $rules }),
99         'Biblio not hidden if there is no item attached (OpacHiddenItemsHidesRecord)'
100     );
101
102     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
103     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
104
105     $item_1->withdrawn( 1 )->store->discard_changes;
106     $item_2->withdrawn( 1 )->store->discard_changes;
107
108     ok( !$biblio->hidden_in_opac({ rules => $rules }), 'Biblio not hidden' );
109
110     $item_2->withdrawn( 2 )->store->discard_changes;
111     $biblio->discard_changes; # refresh
112
113     ok( !$biblio->hidden_in_opac({ rules => $rules }), 'Biblio not hidden' );
114
115     $item_1->withdrawn( 2 )->store->discard_changes;
116     $biblio->discard_changes; # refresh
117
118     ok( $biblio->hidden_in_opac({ rules => $rules }), 'Biblio hidden' );
119
120     t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 0 );
121     ok(
122         !$biblio->hidden_in_opac( { rules => $rules } ),
123         'Biblio hidden (!OpacHiddenItemsHidesRecord)'
124     );
125
126
127     $schema->storage->txn_rollback;
128 };
129
130 subtest 'items() tests' => sub {
131
132     plan tests => 3;
133
134     $schema->storage->txn_begin;
135
136     my $biblio = $builder->build_sample_biblio();
137
138     is( $biblio->items->count, 0, 'No items, count is 0' );
139
140     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
141     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
142
143     my $items = $biblio->items;
144     is( ref($items), 'Koha::Items', 'Returns a Koha::Items resultset' );
145     is( $items->count, 2, 'Two items in resultset' );
146
147     $schema->storage->txn_rollback;
148
149 };
150
151 subtest 'bookable_items() tests' => sub {
152     plan tests => 3;
153
154     $schema->storage->txn_begin;
155
156     my $biblio = $builder->build_sample_biblio();
157
158     # bookable items
159     my $bookable_item1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, bookable => 1 } );
160
161     # not bookable items
162     my $non_bookable_item1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, bookable => 0 } );
163     my $non_bookable_item2 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, bookable => 0 } );
164
165     is( ref( $biblio->bookable_items ), 'Koha::Items', "bookable_items returns a Koha::Items resultset" );
166     is( $biblio->bookable_items->count, 1,             "bookable_items returns the correct number of items" );
167     is(
168         $biblio->bookable_items->next->itemnumber, $bookable_item1->itemnumber,
169         "bookable_items returned the correct item"
170     );
171
172     $schema->storage->txn_rollback;
173 };
174
175 subtest 'get_coins and get_openurl' => sub {
176
177     plan tests => 4;
178
179     $schema->storage->txn_begin;
180
181     my $builder = t::lib::TestBuilder->new;
182     my $biblio = $builder->build_sample_biblio({
183             title => 'Title 1',
184             author => 'Author 1'
185         });
186     is(
187         $biblio->get_coins,
188         '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',
189         'GetCOinsBiblio returned right metadata'
190     );
191
192     my $record = MARC::Record->new();
193     $record->append_fields( MARC::Field->new('100','','','a' => 'Author 2'), MARC::Field->new('880','','','a' => 'Something') );
194     my ( $biblionumber ) = C4::Biblio::AddBiblio($record, '');
195     my $biblio_no_title = Koha::Biblios->find($biblionumber);
196     is(
197         $biblio_no_title->get_coins,
198         'ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.au=Author%202',
199         'GetCOinsBiblio returned right metadata if biblio does not have a title'
200     );
201
202     t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/");
203     is(
204         $biblio->get_openurl,
205         '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',
206         'Koha::Biblio->get_openurl returned right URL'
207     );
208
209     t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/?client_id=ci1");
210     is(
211         $biblio->get_openurl,
212         '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',
213         'Koha::Biblio->get_openurl returned right URL'
214     );
215
216     $schema->storage->txn_rollback;
217 };
218
219 subtest 'is_serial() tests' => sub {
220
221     plan tests => 3;
222
223     $schema->storage->txn_begin;
224
225     my $biblio = $builder->build_sample_biblio();
226
227     $biblio->serial( 1 )->store->discard_changes;
228     ok( $biblio->is_serial, 'Bibliographic record is serial' );
229
230     $biblio->serial( 0 )->store->discard_changes;
231     ok( !$biblio->is_serial, 'Bibliographic record is not serial' );
232
233     my $record = $biblio->metadata->record;
234     $record->leader('00142nas a22     7a 4500');
235     ModBiblio($record, $biblio->biblionumber );
236     $biblio = Koha::Biblios->find($biblio->biblionumber);
237
238     ok( $biblio->is_serial, 'Bibliographic record is serial' );
239
240     $schema->storage->txn_rollback;
241 };
242
243 subtest 'pickup_locations() tests' => sub {
244
245     plan tests => 11;
246
247     $schema->storage->txn_begin;
248
249     Koha::CirculationRules->search->delete;
250     Koha::CirculationRules->set_rules(
251         {
252             categorycode => undef,
253             itemtype     => undef,
254             branchcode   => undef,
255             rules        => {
256                 reservesallowed => 25,
257             }
258         }
259     );
260
261     my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
262     my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
263     my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
264
265     my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'zzz' } } );
266     my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'AAA' } } );
267     my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, branchname => 'FFF' } } );
268     my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'CCC' } } );
269     my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'eee' } } );
270     my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'BBB' } } );
271     my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchname => 'DDD' } } );
272     my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, branchname => 'GGG' } } );
273
274     our @branchcodes = map { $_->branchcode } ($library1, $library2, $library3, $library4, $library5, $library6, $library7, $library8);
275
276     Koha::CirculationRules->set_rules(
277         {
278             branchcode => $library1->branchcode,
279             itemtype   => undef,
280             rules => {
281                 holdallowed => 'from_home_library',
282                 hold_fulfillment_policy => 'any',
283                 returnbranch => 'any'
284             }
285         }
286     );
287
288     Koha::CirculationRules->set_rules(
289         {
290             branchcode => $library2->branchcode,
291             itemtype   => undef,
292             rules => {
293                 holdallowed => 'from_local_hold_group',
294                 hold_fulfillment_policy => 'holdgroup',
295                 returnbranch => 'any'
296             }
297         }
298     );
299
300     Koha::CirculationRules->set_rules(
301         {
302             branchcode => $library3->branchcode,
303             itemtype   => undef,
304             rules => {
305                 holdallowed => 'from_local_hold_group',
306                 hold_fulfillment_policy => 'patrongroup',
307                 returnbranch => 'any'
308             }
309         }
310     );
311
312     Koha::CirculationRules->set_rules(
313         {
314             branchcode => $library4->branchcode,
315             itemtype   => undef,
316             rules => {
317                 holdallowed => 'from_any_library',
318                 hold_fulfillment_policy => 'holdingbranch',
319                 returnbranch => 'any'
320             }
321         }
322     );
323
324     Koha::CirculationRules->set_rules(
325         {
326             branchcode => $library5->branchcode,
327             itemtype   => undef,
328             rules => {
329                 holdallowed => 'from_any_library',
330                 hold_fulfillment_policy => 'homebranch',
331                 returnbranch => 'any'
332             }
333         }
334     );
335
336     Koha::CirculationRules->set_rules(
337         {
338             branchcode => $library6->branchcode,
339             itemtype   => undef,
340             rules => {
341                 holdallowed => 'from_home_library',
342                 hold_fulfillment_policy => 'holdgroup',
343                 returnbranch => 'any'
344             }
345         }
346     );
347
348     Koha::CirculationRules->set_rules(
349         {
350             branchcode => $library7->branchcode,
351             itemtype   => undef,
352             rules => {
353                 holdallowed => 'from_local_hold_group',
354                 hold_fulfillment_policy => 'holdingbranch',
355                 returnbranch => 'any'
356             }
357         }
358     );
359
360
361     Koha::CirculationRules->set_rules(
362         {
363             branchcode => $library8->branchcode,
364             itemtype   => undef,
365             rules => {
366                 holdallowed => 'from_any_library',
367                 hold_fulfillment_policy => 'patrongroup',
368                 returnbranch => 'any'
369             }
370         }
371     );
372
373     my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
374     my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
375
376     my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
377     my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
378
379     my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
380     my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
381     my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
382     my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
383
384     my $biblio1  = $builder->build_sample_biblio({ title => '1' });
385     my $biblio2  = $builder->build_sample_biblio({ title => '2' });
386
387     throws_ok
388       { $biblio1->pickup_locations }
389       'Koha::Exceptions::MissingParameter',
390       'Exception thrown on missing parameter';
391
392     is( $@->parameter, 'patron', 'Exception param correctly set' );
393
394     my $item1_1  = $builder->build_sample_item({
395         biblionumber     => $biblio1->biblionumber,
396         homebranch       => $library1->branchcode,
397         holdingbranch    => $library2->branchcode,
398     })->store;
399
400     my $item1_3  = $builder->build_sample_item({
401         biblionumber     => $biblio1->biblionumber,
402         homebranch       => $library3->branchcode,
403         holdingbranch    => $library4->branchcode,
404     })->store;
405
406     my $item1_7  = $builder->build_sample_item({
407         biblionumber     => $biblio1->biblionumber,
408         homebranch       => $library7->branchcode,
409         holdingbranch    => $library4->branchcode,
410     })->store;
411
412     my $item2_2  = $builder->build_sample_item({
413         biblionumber     => $biblio2->biblionumber,
414         homebranch       => $library2->branchcode,
415         holdingbranch    => $library1->branchcode,
416     })->store;
417
418     my $item2_4  = $builder->build_sample_item({
419         biblionumber     => $biblio2->biblionumber,
420         homebranch       => $library4->branchcode,
421         holdingbranch    => $library3->branchcode,
422     })->store;
423
424     my $item2_6  = $builder->build_sample_item({
425         biblionumber     => $biblio2->biblionumber,
426         homebranch       => $library6->branchcode,
427         holdingbranch    => $library4->branchcode,
428     })->store;
429
430     my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'1', branchcode => $library1->branchcode } } );
431     my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
432
433     my $results = {
434         "ItemHomeLibrary-1-1" => 6,
435         "ItemHomeLibrary-1-8" => 1,
436         "ItemHomeLibrary-2-1" => 2,
437         "ItemHomeLibrary-2-8" => 0,
438         "PatronLibrary-1-1" => 6,
439         "PatronLibrary-1-8" => 3,
440         "PatronLibrary-2-1" => 0,
441         "PatronLibrary-2-8" => 3,
442     };
443
444     sub _doTest {
445         my ( $cbranch, $biblio, $patron, $results ) = @_;
446         t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
447
448         my @pl = map {
449             my $pickup_location = $_;
450             grep { $pickup_location->branchcode eq $_ } @branchcodes
451         } $biblio->pickup_locations( { patron => $patron } )->as_list;
452
453         ok(
454             scalar(@pl) == $results->{ $cbranch . '-'
455                   . $biblio->title . '-'
456                   . $patron->firstname },
457             'ReservesControlBranch: '
458               . $cbranch
459               . ', biblio'
460               . $biblio->title
461               . ', patron'
462               . $patron->firstname
463               . ' should return '
464               . $results->{ $cbranch . '-'
465                   . $biblio->title . '-'
466                   . $patron->firstname }
467               . ' but returns '
468               . scalar(@pl)
469         );
470     }
471
472     foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
473         my $cache = Koha::Cache::Memory::Lite->get_instance();
474         $cache->flush(); # needed since we change ReservesControlBranch
475         foreach my $biblio ($biblio1, $biblio2) {
476             foreach my $patron ($patron1, $patron8) {
477                 _doTest($cbranch, $biblio, $patron, $results);
478             }
479         }
480     }
481
482     my @pl_names = map { $_->branchname } $biblio1->pickup_locations( { patron => $patron1 } )->as_list;
483     my $pl_ori_str = join('|', @pl_names);
484     my $pl_sorted_str = join('|', sort { lc($a) cmp lc($b) } @pl_names);
485     ok(
486         $pl_ori_str eq $pl_sorted_str,
487         'Libraries must be sorted by name'
488     );
489     $schema->storage->txn_rollback;
490 };
491
492 subtest 'to_api() tests' => sub {
493
494     $schema->storage->txn_begin;
495
496     my $biblio = $builder->build_sample_biblio();
497     my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
498
499     my $biblioitem_api = $biblio->biblioitem->to_api;
500     my $biblio_api     = $biblio->to_api;
501
502     plan tests => (scalar keys %{ $biblioitem_api }) + 1;
503
504     foreach my $key ( keys %{ $biblioitem_api } ) {
505         is( $biblio_api->{$key}, $biblioitem_api->{$key}, "$key is added to the biblio object" );
506     }
507
508     $biblio_api = $biblio->to_api({ embed => { items => {} } });
509     is_deeply( $biblio_api->{items}, [ $item->to_api ], 'Item correctly embedded' );
510
511     $schema->storage->txn_rollback;
512 };
513
514 subtest 'bookings() tests' => sub {
515
516     plan tests => 3;
517
518     $schema->storage->txn_begin;
519
520     my $biblio = $builder->build_sample_biblio();
521
522     is( ref( $biblio->bookings ), 'Koha::Bookings', 'Return type is correct' );
523
524     is_deeply(
525         $biblio->bookings->unblessed,
526         [],
527         '->bookings returns an empty Koha::Bookings resultset'
528     );
529
530     my $booking = $builder->build_object(
531         {
532             class => 'Koha::Bookings',
533             value => { biblio_id => $biblio->biblionumber }
534         }
535     );
536
537     my $bookings = $biblio->bookings->unblessed;
538
539     is_deeply(
540         $biblio->bookings->unblessed,
541         [ $booking->unblessed ],
542         '->bookings returns the related Koha::Booking objects'
543     );
544
545     $schema->storage->txn_rollback;
546 };
547
548 subtest 'suggestions() tests' => sub {
549
550     plan tests => 3;
551
552     $schema->storage->txn_begin;
553
554     my $biblio     = $builder->build_sample_biblio();
555
556     is( ref($biblio->suggestions), 'Koha::Suggestions', 'Return type is correct' );
557
558     is_deeply(
559         $biblio->suggestions->unblessed,
560         [],
561         '->suggestions returns an empty Koha::Suggestions resultset'
562     );
563
564     my $suggestion = $builder->build_object(
565         {
566             class => 'Koha::Suggestions',
567             value => { biblionumber => $biblio->biblionumber }
568         }
569     );
570
571     my $suggestions = $biblio->suggestions->unblessed;
572
573     is_deeply(
574         $biblio->suggestions->unblessed,
575         [ $suggestion->unblessed ],
576         '->suggestions returns the related Koha::Suggestion objects'
577     );
578
579     $schema->storage->txn_rollback;
580 };
581
582 subtest 'get_marc_components() tests' => sub {
583
584     plan tests => 5;
585
586     $schema->storage->txn_begin;
587
588     my ($host_bibnum) = C4::Biblio::AddBiblio(host_record(), '');
589     my $host_biblio = Koha::Biblios->find($host_bibnum);
590     t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' );
591     my $search_mod = Test::MockModule->new( 'Koha::SearchEngine::Zebra::Search' );
592     $search_mod->mock( 'search_compat', \&search_component_record2 );
593
594     my $components = $host_biblio->get_marc_components;
595     is( ref($components), 'ARRAY', 'Return type is correct' );
596
597     is_deeply(
598         $components,
599         [],
600         '->get_marc_components returns an empty ARRAY'
601     );
602
603     $search_mod->unmock( 'search_compat');
604     $search_mod->mock( 'search_compat', \&search_component_record1 );
605     my $component_record = component_record1()->as_xml();
606
607     is_deeply(
608         $host_biblio->get_marc_components,
609         [$component_record],
610         '->get_marc_components returns the related component part record'
611     );
612     $search_mod->unmock( 'search_compat');
613
614     $search_mod->mock( 'search_compat',
615         sub { Koha::Exception->throw("error searching analytics") }
616     );
617     warning_like { $components = $host_biblio->get_marc_components }
618         qr{Warning from search_compat: .* 'error searching analytics'};
619
620     is_deeply(
621         $host_biblio->object_messages,
622         [
623             {
624                 type    => 'error',
625                 message => 'component_search',
626                 payload => "Exception 'Koha::Exception' thrown 'error searching analytics'\n"
627             }
628         ]
629     );
630     $search_mod->unmock( 'search_compat');
631
632     $schema->storage->txn_rollback;
633 };
634
635 subtest 'get_components_query' => sub {
636     plan tests => 12;
637
638     my $biblio = $builder->build_sample_biblio();
639     my $biblionumber = $biblio->biblionumber;
640     my $record = $biblio->metadata->record;
641
642     foreach my $engine ('Zebra','Elasticsearch'){
643         t::lib::Mocks::mock_preference( 'SearchEngine', $engine );
644
645         t::lib::Mocks::mock_preference( 'UseControlNumber', '0' );
646         t::lib::Mocks::mock_preference( 'ComponentSortField', 'author' );
647         t::lib::Mocks::mock_preference( 'ComponentSortOrder', 'za' );
648         my ( $comp_query, $comp_query_str, $comp_sort ) = $biblio->get_components_query;
649         is($comp_query_str, 'Host-item:("Some boring read")', "$engine: UseControlNumber disabled");
650         is($comp_sort, "author_za", "$engine: UseControlNumber disabled sort is correct");
651
652         t::lib::Mocks::mock_preference( 'UseControlNumber', '1' );
653         t::lib::Mocks::mock_preference( 'ComponentSortOrder', 'az' );
654         my $marc_001_field = MARC::Field->new('001', $biblionumber);
655         $record->append_fields($marc_001_field);
656         C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
657         $biblio = Koha::Biblios->find( $biblio->biblionumber);
658
659         ( $comp_query, $comp_query_str, $comp_sort ) = $biblio->get_components_query;
660         is($comp_query_str, "(rcn:\"$biblionumber\" AND (bib-level:a OR bib-level:b))", "$engine: UseControlNumber enabled without MarcOrgCode");
661         is($comp_sort, "author_az", "$engine: UseControlNumber enabled without MarcOrgCode sort is correct");
662
663         my $marc_003_field = MARC::Field->new('003', 'OSt');
664         $record->append_fields($marc_003_field);
665         C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
666         $biblio = Koha::Biblios->find( $biblio->biblionumber);
667
668         t::lib::Mocks::mock_preference( 'ComponentSortField', 'title' );
669         t::lib::Mocks::mock_preference( 'ComponentSortOrder', 'asc' );
670         ( $comp_query, $comp_query_str, $comp_sort ) = $biblio->get_components_query;
671         is($comp_query_str, "(((rcn:\"$biblionumber\" AND cni:\"OSt\") OR rcn:\"OSt $biblionumber\") AND (bib-level:a OR bib-level:b))", "$engine: UseControlNumber enabled with MarcOrgCode");
672         is($comp_sort, "title_asc", "$engine: UseControlNumber enabled with MarcOrgCode sort if correct");
673         $record->delete_field($marc_003_field);
674     }
675 };
676
677 subtest 'get_volumes_query' => sub {
678     plan tests => 3;
679
680     my $biblio       = $builder->build_sample_biblio();
681     my $biblionumber = $biblio->biblionumber;
682     my $record       = $biblio->metadata->record;
683
684     # Ensure our mocked record is captured as a set or monographic series
685     my $ldr = $record->leader();
686     substr( $ldr, 19, 1 ) = 'a';
687     $record->leader($ldr);
688     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
689     $biblio = Koha::Biblios->find( $biblio->biblionumber );
690
691     t::lib::Mocks::mock_preference( 'UseControlNumber', '0' );
692     is(
693         $biblio->get_volumes_query,
694         "(title-series,phr:(\"Some boring read\") OR Host-item,phr:(\"Some boring read\") NOT (bib-level:a OR bib-level:b))",
695         "UseControlNumber disabled"
696     );
697
698     t::lib::Mocks::mock_preference( 'UseControlNumber', '1' );
699     my $marc_001_field = MARC::Field->new( '001', $biblionumber );
700     $record->append_fields($marc_001_field);
701     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
702     $biblio = Koha::Biblios->find( $biblio->biblionumber );
703
704
705     is(
706         $biblio->get_volumes_query, "(rcn:$biblionumber NOT (bib-level:a OR bib-level:b))",
707         "UseControlNumber enabled without MarcOrgCode"
708     );
709
710     my $marc_003_field = MARC::Field->new( '003', 'OSt' );
711     $record->append_fields($marc_003_field);
712     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
713     $biblio = Koha::Biblios->find( $biblio->biblionumber );
714
715     is(
716         $biblio->get_volumes_query,
717         "(((rcn:$biblionumber AND cni:OSt) OR rcn:\"OSt $biblionumber\") NOT (bib-level:a OR bib-level:b))",
718         "UseControlNumber enabled with MarcOrgCode"
719     );
720 };
721
722 subtest 'orders() and active_orders() tests' => sub {
723
724     plan tests => 5;
725
726     $schema->storage->txn_begin;
727
728     my $biblio = $builder->build_sample_biblio();
729
730     my $orders        = $biblio->orders;
731     my $active_orders = $biblio->active_orders;
732
733     is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
734     is( $biblio->orders->count, $biblio->active_orders->count, '->orders->count returns the count for the resultset' );
735
736     # Add a couple orders
737     foreach (1..2) {
738         $builder->build_object(
739             {
740                 class => 'Koha::Acquisition::Orders',
741                 value => {
742                     biblionumber => $biblio->biblionumber,
743                     datecancellationprinted => '2019-12-31'
744                 }
745             }
746         );
747     }
748
749     $builder->build_object(
750         {
751             class => 'Koha::Acquisition::Orders',
752             value => {
753                 biblionumber => $biblio->biblionumber,
754                 datecancellationprinted => undef
755             }
756         }
757     );
758
759     $orders = $biblio->orders;
760     $active_orders = $biblio->active_orders;
761
762     is( ref($orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
763     is( ref($active_orders), 'Koha::Acquisition::Orders', 'Result type is correct' );
764     is( $orders->count, $active_orders->count + 2, '->active_orders->count returns the rigt count' );
765
766     $schema->storage->txn_rollback;
767 };
768
769 subtest 'tickets() tests' => sub {
770
771     plan tests => 4;
772
773     $schema->storage->txn_begin;
774
775     my $biblio = $builder->build_sample_biblio();
776     my $tickets = $biblio->tickets;
777     is( ref($tickets), 'Koha::Tickets', 'Koha::Biblio->tickets should return a Koha::Tickets object' );
778     is( $tickets->count, 0, 'Koha::Biblio->tickets should return a count of 0 when there are no related tickets' );
779
780     # Add two tickets
781     foreach (1..2) {
782         $builder->build_object(
783             {
784                 class => 'Koha::Tickets',
785                 value => { biblio_id => $biblio->biblionumber }
786             }
787         );
788     }
789
790     $tickets = $biblio->tickets;
791     is( ref($tickets), 'Koha::Tickets', 'Koha::Biblio->tickets should return a Koha::Tickets object' );
792     is( $tickets->count, 2, 'Koha::Biblio->tickets should return the correct number of tickets' );
793
794     $schema->storage->txn_rollback;
795 };
796
797 subtest 'subscriptions() tests' => sub {
798
799     plan tests => 4;
800
801     $schema->storage->txn_begin;
802
803     my $biblio = $builder->build_sample_biblio;
804
805     my $subscriptions = $biblio->subscriptions;
806     is( ref($subscriptions), 'Koha::Subscriptions',
807         'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
808     );
809     is( $subscriptions->count, 0, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
810
811     # Add two subscriptions
812     foreach (1..2) {
813         $builder->build_object(
814             {
815                 class => 'Koha::Subscriptions',
816                 value => { biblionumber => $biblio->biblionumber }
817             }
818         );
819     }
820
821     $subscriptions = $biblio->subscriptions;
822     is( ref($subscriptions), 'Koha::Subscriptions',
823         'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
824     );
825     is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
826
827     $schema->storage->txn_rollback;
828 };
829
830 subtest 'get_marc_notes() MARC21 tests' => sub {
831     plan tests => 14;
832
833     $schema->storage->txn_begin;
834
835     t::lib::Mocks::mock_preference( 'NotesToHide', '520' );
836
837     my $av = $builder->build_object( { class => 'Koha::AuthorisedValues' } );
838
839     my $biblio = $builder->build_sample_biblio;
840     my $record = $biblio->metadata->record;
841     $record->append_fields(
842         MARC::Field->new( '500', '', '', a => 'Note1' ),
843         MARC::Field->new( '505', '', '', a => 'Note2', u => 'http://someserver.com' ),
844         MARC::Field->new( '520', '', '', a => 'Note3 skipped' ),
845         MARC::Field->new( '541', '0', '', a => 'Note4 skipped on opac' ),
846         MARC::Field->new( '544', '', '', a => 'Note5' ),
847         MARC::Field->new( '590', '', '', a => $av->authorised_value ),
848         MARC::Field->new( '545', '', '', a => 'Invisible on OPAC' ),
849     );
850
851     my $mss = Koha::MarcSubfieldStructures->find({tagfield => "590", tagsubfield => "a", frameworkcode => $biblio->frameworkcode });
852     $mss->update({ authorised_value => $av->category });
853
854     $mss = Koha::MarcSubfieldStructures->find({tagfield => "545", tagsubfield => "a", frameworkcode => $biblio->frameworkcode });
855     $mss->update({ hidden => 1 });
856
857     my $cache = Koha::Caches->get_instance;
858     $cache->clear_from_cache("MarcStructure-0-");
859     $cache->clear_from_cache("MarcStructure-1-");
860     $cache->clear_from_cache("MarcSubfieldStructure-");
861     $cache->clear_from_cache("MarcCodedFields-");
862
863     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
864     $biblio = Koha::Biblios->find( $biblio->biblionumber);
865
866     my $notes = $biblio->get_marc_notes;
867     is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
868     is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
869     is( $notes->[2]->{marcnote}, 'http://someserver.com', 'URL separated' );
870     is( $notes->[3]->{marcnote}, 'Note4 skipped on opac',"Note shows if not opac (Hidden by Indicator)" );
871     is( $notes->[4]->{marcnote}, 'Note5', 'Fifth note' );
872     is( $notes->[5]->{marcnote}, $av->lib, 'Authorised value is correctly parsed to show description rather than code' );
873     is( $notes->[6]->{marcnote}, 'Invisible on OPAC', 'Note shows if not opac (Hidden by framework)' );
874     is( @$notes, 7, 'No more notes' );
875     $notes = $biblio->get_marc_notes({ opac => 1 });
876     is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
877     is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
878     is( $notes->[2]->{marcnote}, 'http://someserver.com', 'URL separated' );
879     is( $notes->[3]->{marcnote}, 'Note5', 'Fifth note shows after fourth skipped' );
880     is( $notes->[4]->{marcnote}, $av->lib_opac, 'Authorised value is correctly parsed for OPAC to show description rather than code' );
881     is( @$notes, 5, 'No more notes' );
882
883     $cache->clear_from_cache("MarcStructure-0-");
884     $cache->clear_from_cache("MarcStructure-1-");
885     $cache->clear_from_cache("MarcSubfieldStructure-");
886     $cache->clear_from_cache("MarcCodedFields-");
887
888     $schema->storage->txn_rollback;
889 };
890
891 subtest 'get_marc_notes() UNIMARC tests' => sub {
892     plan tests => 3;
893
894     $schema->storage->txn_begin;
895
896     t::lib::Mocks::mock_preference( 'NotesToHide', '310' );
897     t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
898
899     my $biblio = $builder->build_sample_biblio;
900     my $record = $biblio->metadata->record;
901     $record->append_fields(
902         MARC::Field->new( '300', '', '', a => 'Note1' ),
903         MARC::Field->new( '300', '', '', a => 'Note2' ),
904         MARC::Field->new( '310', '', '', a => 'Note3 skipped' ),
905     );
906     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
907     $biblio = Koha::Biblios->find( $biblio->biblionumber);
908     my $notes = $biblio->get_marc_notes({ marcflavour => 'UNIMARC' });
909     is( $notes->[0]->{marcnote}, 'Note1', 'First note' );
910     is( $notes->[1]->{marcnote}, 'Note2', 'Second note' );
911     is( @$notes, 2, 'No more notes' );
912
913     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
914     $schema->storage->txn_rollback;
915 };
916
917 subtest 'host_items() tests' => sub {
918     plan tests => 7;
919
920     $schema->storage->txn_begin;
921
922     my $biblio = $builder->build_sample_biblio( { frameworkcode => '' } );
923
924     t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 1 );
925     my $host_items = $biblio->host_items;
926     is( ref($host_items),   'Koha::Items' );
927     is( $host_items->count, 0 );
928
929     my $item_1 =
930       $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
931     my $host_item_1 = $builder->build_sample_item;
932     my $host_item_2 = $builder->build_sample_item;
933
934     my $record = $biblio->metadata->record;
935     $record->append_fields(
936         MARC::Field->new(
937             '773', '', '',
938             9 => $host_item_1->itemnumber,
939             9 => $host_item_2->itemnumber
940         ),
941     );
942     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
943     $biblio = $biblio->get_from_storage;
944     $host_items = $biblio->host_items;
945     is( $host_items->count, 2 );
946     is_deeply( [ $host_items->get_column('itemnumber') ],
947         [ $host_item_1->itemnumber, $host_item_2->itemnumber ] );
948
949     t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 0 );
950     $host_items = $biblio->host_items;
951     is( ref($host_items),   'Koha::Items' );
952     is( $host_items->count, 0 );
953
954     subtest 'test host_items param in items()' => sub {
955         plan tests => 4;
956
957         my $items = $biblio->items;
958         is( $items->count, 1, "Without host_items param we only get the items on the biblio");
959         $items = $biblio->items({ host_items => 1 });
960         is( $items->count, 3, "With param host_items we get the biblio items plus analytics");
961         is( ref($items), 'Koha::Items', "We correctly get an Items object");
962         is_deeply( [ $items->get_column('itemnumber') ],
963             [ $item_1->itemnumber, $host_item_1->itemnumber, $host_item_2->itemnumber ] );
964     };
965
966     $schema->storage->txn_rollback;
967 };
968
969 subtest 'article_requests() tests' => sub {
970
971     plan tests => 3;
972
973     $schema->storage->txn_begin;
974
975     my $item   = $builder->build_sample_item;
976     my $biblio = $item->biblio;
977
978     my $article_requests = $biblio->article_requests;
979     is( ref($article_requests), 'Koha::ArticleRequests',
980         'In scalar context, type is correct' );
981     is( $article_requests->count, 0, 'No article requests' );
982
983     foreach my $i ( 0 .. 3 ) {
984
985         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
986
987         Koha::ArticleRequest->new(
988             {
989                 borrowernumber => $patron->id,
990                 biblionumber   => $biblio->id,
991                 itemnumber     => $item->id,
992                 title          => $biblio->title,
993             }
994         )->request;
995     }
996
997     $article_requests = $biblio->article_requests;
998     is( $article_requests->count, 4, '4 article requests' );
999
1000     $schema->storage->txn_rollback;
1001 };
1002
1003 subtest 'current_checkouts() and old_checkouts() tests' => sub {
1004
1005     plan tests => 4;
1006
1007     $schema->storage->txn_begin;
1008
1009     my $library = $builder->build_object({ class => 'Koha::Libraries' });
1010
1011     my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
1012     my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
1013
1014     my $item_1 = $builder->build_sample_item;
1015     my $item_2 = $builder->build_sample_item({ biblionumber => $item_1->biblionumber });
1016
1017     t::lib::Mocks::mock_userenv({ branchcode => $library->id });
1018
1019     AddIssue( $patron_1, $item_1->barcode );
1020     AddIssue( $patron_1, $item_2->barcode );
1021
1022     AddReturn( $item_1->barcode );
1023     AddIssue( $patron_2, $item_1->barcode );
1024
1025     my $biblio = $item_1->biblio;
1026     my $current_checkouts = $biblio->current_checkouts;
1027     my $old_checkouts = $biblio->old_checkouts;
1028
1029     is( ref($current_checkouts), 'Koha::Checkouts', 'Type is correct' );
1030     is( ref($old_checkouts), 'Koha::Old::Checkouts', 'Type is correct' );
1031
1032     is( $current_checkouts->count, 2, 'Count is correct for current checkouts' );
1033     is( $old_checkouts->count, 1, 'Count is correct for old checkouts' );
1034
1035     $schema->storage->txn_rollback;
1036 };
1037
1038 subtest 'get_marc_contributors() tests' => sub {
1039
1040     plan tests => 2;
1041
1042     $schema->storage->txn_begin;
1043
1044     my $biblio = $builder->build_sample_biblio({ author => 'Main author' });
1045     my $record = $biblio->metadata->record;
1046
1047     # add author information
1048     my $field = MARC::Field->new('700','1','','a' => 'Jefferson, Thomas');
1049     $record->append_fields($field);
1050     $field = MARC::Field->new('701','1','','d' => 'Secondary author 2');
1051     $record->append_fields($field);
1052
1053     # get record
1054     C4::Biblio::ModBiblio( $record, $biblio->biblionumber );
1055     $biblio = Koha::Biblios->find( $biblio->biblionumber );
1056
1057     is( @{$biblio->get_marc_authors}, 3, 'get_marc_authors retrieves correct number of author subfields' );
1058     is( @{$biblio->get_marc_contributors}, 2, 'get_marc_contributors retrieves correct number of author subfields' );
1059     $schema->storage->txn_rollback;
1060 };
1061
1062 subtest 'Recalls tests' => sub {
1063
1064     plan tests => 13;
1065
1066     $schema->storage->txn_begin;
1067
1068     my $item1 = $builder->build_sample_item;
1069     my $biblio = $item1->biblio;
1070     my $branchcode = $item1->holdingbranch;
1071     my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1072     my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1073     my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1074     my $item2 = $builder->build_object({ class => 'Koha::Items', value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype } });
1075     t::lib::Mocks::mock_userenv({ patron => $patron1 });
1076
1077     my $recall1 = Koha::Recall->new(
1078         {   patron_id         => $patron1->borrowernumber,
1079             created_date      => \'NOW()',
1080             biblio_id         => $biblio->biblionumber,
1081             pickup_library_id => $branchcode,
1082             item_id           => $item1->itemnumber,
1083             expiration_date   => undef,
1084             item_level        => 1
1085         }
1086     )->store;
1087     my $recall2 = Koha::Recall->new(
1088         {   patron_id         => $patron2->borrowernumber,
1089             created_date      => \'NOW()',
1090             biblio_id         => $biblio->biblionumber,
1091             pickup_library_id => $branchcode,
1092             item_id           => undef,
1093             expiration_date   => undef,
1094             item_level        => 0
1095         }
1096     )->store;
1097     my $recall3 = Koha::Recall->new(
1098         {   patron_id         => $patron3->borrowernumber,
1099             created_date      => \'NOW()',
1100             biblio_id         => $biblio->biblionumber,
1101             pickup_library_id => $branchcode,
1102             item_id           => $item1->itemnumber,
1103             expiration_date   => undef,
1104             item_level        => 1
1105         }
1106     )->store;
1107
1108     my $recalls = $biblio->recalls;
1109     is( $recalls->count, 3, 'Correctly get number of recalls for biblio' );
1110
1111     $recall1->set_cancelled;
1112     $recall2->set_expired({ interface => 'COMMANDLINE' });
1113
1114     is( $recalls->count, 3, 'Correctly get number of recalls for biblio' );
1115     is( $recalls->filter_by_current->count, 1, 'Correctly get number of active recalls for biblio' );
1116
1117     t::lib::Mocks::mock_preference('UseRecalls', 0);
1118     is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1119
1120     t::lib::Mocks::mock_preference("UseRecalls", 1);
1121     $item1->update({ notforloan => 1 });
1122     is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with no available items" );
1123
1124     $item1->update({ notforloan => 0 });
1125     Koha::CirculationRules->set_rules({
1126         branchcode => $branchcode,
1127         categorycode => $patron1->categorycode,
1128         itemtype => $item1->effective_itemtype,
1129         rules => {
1130             recalls_allowed => 0,
1131             recalls_per_record => 1,
1132             on_shelf_recalls => 'all',
1133         },
1134     });
1135     is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1136
1137     Koha::CirculationRules->set_rules({
1138         branchcode => $branchcode,
1139         categorycode => $patron1->categorycode,
1140         itemtype => $item1->effective_itemtype,
1141         rules => {
1142             recalls_allowed => 1,
1143             recalls_per_record => 1,
1144             on_shelf_recalls => 'all',
1145         },
1146     });
1147     is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1148     is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1149
1150     $recall1->set_cancelled;
1151     C4::Circulation::AddIssue( $patron1, $item2->barcode );
1152     is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1153
1154     is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1155
1156     Koha::CirculationRules->set_rules({
1157         branchcode => $branchcode,
1158         categorycode => $patron1->categorycode,
1159         itemtype => $item1->effective_itemtype,
1160         rules => {
1161             recalls_allowed => 1,
1162             recalls_per_record => 1,
1163             on_shelf_recalls => 'any',
1164         },
1165     });
1166     C4::Circulation::AddReturn( $item2->barcode, $branchcode );
1167     is( $biblio->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1168
1169     $recall2->set_cancelled;
1170     C4::Circulation::AddIssue( $patron2, $item2->barcode );
1171     C4::Circulation::AddIssue( $patron2, $item1->barcode );
1172     is( $biblio->can_be_recalled({ patron => $patron1 }), 2, "Can recall two items" );
1173
1174     $item1->update({ withdrawn => 1 });
1175     is( $biblio->can_be_recalled({ patron => $patron1 }), 1, "Can recall one item" );
1176
1177     $schema->storage->txn_rollback;
1178 };
1179
1180 subtest 'ill_requests() tests' => sub {
1181
1182     plan tests => 3;
1183
1184     $schema->storage->txn_begin;
1185
1186     my $biblio = $builder->build_sample_biblio;
1187
1188     my $rs = $biblio->ill_requests;
1189     is( ref($rs), 'Koha::Illrequests' );
1190     is( $rs->count, 0, 'No linked requests' );
1191
1192     foreach ( 1..10 ) {
1193         $builder->build_object(
1194             {
1195                 class => 'Koha::Illrequests',
1196                 value => { biblio_id => $biblio->id }
1197             }
1198         );
1199     }
1200
1201     is( $biblio->ill_requests->count, 10, 'Linked requests are present' );
1202
1203     $schema->storage->txn_rollback;
1204 };
1205
1206 subtest 'item_groups() tests' => sub {
1207
1208     plan tests => 6;
1209
1210     $schema->storage->txn_begin;
1211
1212     my $biblio = $builder->build_sample_biblio();
1213
1214     my @item_groups = $biblio->item_groups->as_list;
1215     is( scalar(@item_groups), 0, 'Got zero item groups');
1216
1217     my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
1218
1219     @item_groups = $biblio->item_groups->as_list;
1220     is( scalar(@item_groups), 1, 'Got one item group');
1221     is( $item_groups[0]->id, $item_group_1->id, 'Got correct item group');
1222
1223     my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
1224
1225     @item_groups = $biblio->item_groups->as_list;
1226     is( scalar(@item_groups), 2, 'Got two item groups');
1227     is( $item_groups[0]->id, $item_group_1->id, 'Got correct item group 1');
1228     is( $item_groups[1]->id, $item_group_2->id, 'Got correct item group 2');
1229
1230     $schema->storage->txn_rollback;
1231 };
1232
1233 subtest 'normalized_isbn' => sub {
1234     plan tests => 1;
1235
1236     # We will move the tests from GetNormalizedISBN here when it will get replaced
1237     my $biblio = $builder->build_sample_biblio();
1238     $biblio->biblioitem->set( { isbn => '9781250067128 | 125006712X' } )->store;
1239     is(
1240         $biblio->normalized_isbn, C4::Koha::GetNormalizedISBN( $biblio->biblioitem->isbn ),
1241         'normalized_isbn is a wrapper around C4::Koha::GetNormalizedISBN'
1242     );
1243 };
1244
1245 subtest 'normalized_upc' => sub {
1246     plan tests => 1;
1247
1248     # We will move the tests from GetNormalizedUPC here when it will get replaced
1249     # Note that only a single test exist and it's not really meaningful...
1250     my $biblio = $builder->build_sample_biblio();
1251     is(
1252         $biblio->normalized_upc, C4::Koha::GetNormalizedUPC( $biblio->metadata->record ),
1253         'normalized_upc is a wrapper around C4::Koha::GetNormalizedUPC'
1254     );
1255 };
1256
1257 subtest 'normalized_oclc' => sub {
1258     plan tests => 1;
1259
1260     # We will move the tests from GetNormalizedOCLC here when it will get replaced
1261     # Note that only a single test exist and it's not really meaningful...
1262     my $biblio = $builder->build_sample_biblio();
1263     is(
1264         $biblio->normalized_oclc, C4::Koha::GetNormalizedOCLCNumber( $biblio->metadata->record ),
1265         'normalized_oclc is a wrapper around C4::Koha::GetNormalizedOCLCNumber'
1266     );
1267 };
1268
1269 subtest 'ratings' => sub {
1270     plan tests => 1;
1271     # See t/db_dependent/Koha/Ratings.t
1272     ok(1);
1273 };
1274
1275 subtest 'opac_summary_html' => sub {
1276
1277     plan tests => 2;
1278
1279     my $author = 'my author';
1280     my $title  = 'my title';
1281     my $isbn   = '9781250067128 | 125006712X';
1282     my $biblio = $builder->build_sample_biblio( { author => $author, title => $title } );
1283     $biblio->biblioitem->set( { isbn => '9781250067128 | 125006712X' } )->store;
1284
1285     t::lib::Mocks::mock_preference( 'OPACMySummaryHTML', '' );
1286     is( $biblio->opac_summary_html, '', 'opac_summary_html returns empty string if pref is off' );
1287
1288     t::lib::Mocks::mock_preference(
1289         'OPACMySummaryHTML',
1290         'Replace {AUTHOR}, {TITLE}, {ISBN} AND {BIBLIONUMBER} please'
1291     );
1292     is(
1293         $biblio->opac_summary_html,
1294         sprintf( 'Replace %s, %s, %s AND %s please', $author, $title, $biblio->normalized_isbn, $biblio->biblionumber ),
1295         'opac_summary_html replaces the different patterns'
1296     );
1297 };
1298
1299 sub component_record1 {
1300     my $marc = MARC::Record->new;
1301     $marc->append_fields(
1302         MARC::Field->new( '001', '3456' ),
1303         MARC::Field->new( '245', '', '', a => 'Some title 1' ),
1304         MARC::Field->new( '773', '', '', w => '(FIRST)1234' ),
1305     );
1306     return $marc;
1307 }
1308 sub search_component_record1 {
1309     my @results = ( component_record1()->as_xml() );
1310     return ( undef, { biblioserver => { RECORDS => \@results, hits => 1 } }, 1 );
1311 }
1312
1313 sub search_component_record2 {
1314     my @results;
1315     return ( undef, { biblioserver => { RECORDS => \@results, hits => 0 } }, 0 );
1316 }
1317
1318 sub host_record {
1319     my $marc = MARC::Record->new;
1320     $marc->append_fields(
1321         MARC::Field->new( '001', '1234' ),
1322         MARC::Field->new( '003', 'FIRST' ),
1323         MARC::Field->new( '245', '', '', a => 'Some title' ),
1324     );
1325     return $marc;
1326 }
1327
1328 subtest 'check_booking tests' => sub {
1329     plan tests => 4;
1330
1331     $schema->storage->txn_begin;
1332
1333     my $biblio = $builder->build_sample_biblio();
1334     my @items;
1335     for ( 0 .. 2 ) {
1336         my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, bookable => 1 } );
1337         push @items, $item;
1338     }
1339
1340     my $can_book = $biblio->check_booking(
1341         {
1342             start_date => dt_from_string(),
1343             end_date   => dt_from_string()->add( days => 7 )
1344         }
1345     );
1346
1347     is(
1348         $can_book, 1,
1349         "True returned from Koha::Biblio->check_booking if there are no bookings"
1350     );
1351
1352     my $start_1 = dt_from_string()->subtract( days => 7 );
1353     my $end_1   = dt_from_string()->subtract( days => 1 );
1354     my $start_2 = dt_from_string();
1355     my $end_2   = dt_from_string()->add( days => 7 );
1356
1357     # Past bookings
1358     my @bookings;
1359     for my $item (@items) {
1360
1361         my $booking = $builder->build_object(
1362             {
1363                 class => 'Koha::Bookings',
1364                 value => {
1365                     biblio_id  => $biblio->biblionumber,
1366                     item_id    => $item->itemnumber,
1367                     start_date => $start_1,
1368                     end_date   => $end_1
1369                 }
1370             }
1371         );
1372         push @bookings, $booking;
1373     }
1374
1375     $can_book = $biblio->check_booking(
1376         {
1377             start_date => dt_from_string(),
1378             end_date   => dt_from_string()->add( days => 7 ),
1379         }
1380     );
1381
1382     is(
1383         $can_book,
1384         1,
1385         "Koha::Biblio->check_booking returns true when we all existing bookings are in the past"
1386     );
1387
1388     # Current bookings
1389     my @current_bookings;
1390     for my $item (@items) {
1391         my $booking = $builder->build_object(
1392             {
1393                 class => 'Koha::Bookings',
1394                 value => {
1395                     biblio_id  => $biblio->biblionumber,
1396                     item_id    => $item->itemnumber,
1397                     start_date => $start_2,
1398                     end_date   => $end_2
1399                 }
1400             }
1401         );
1402         push @current_bookings, $booking;
1403     }
1404
1405     $can_book = $biblio->check_booking(
1406         {
1407             start_date => dt_from_string(),
1408             end_date   => dt_from_string()->add( days => 7 ),
1409         }
1410     );
1411     is(
1412         $can_book,
1413         0,
1414         "Koha::Biblio->check_booking returns false if the booking would conflict with existing bookings"
1415     );
1416
1417     $can_book = $biblio->check_booking(
1418         {
1419             start_date => dt_from_string(),
1420             end_date   => dt_from_string()->add( days => 7 ),
1421             booking_id => $current_bookings[0]->booking_id
1422         }
1423     );
1424     is(
1425         $can_book,
1426         1,
1427         "Koha::Biblio->check_booking returns true if we pass the booking_id of one of the bookings that we would conflict with"
1428     );
1429
1430     $schema->storage->txn_rollback;
1431 };