Bug 22284: (follow-up) Squash multiple follow-ups
[koha.git] / t / db_dependent / Koha / Biblios.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 6;
23 use Test::Exception;
24 use MARC::Field;
25
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Reserves;
29
30 use Koha::DateUtils qw( dt_from_string output_pref );
31 use Koha::Biblios;
32 use Koha::Patrons;
33 use Koha::Subscriptions;
34 use t::lib::TestBuilder;
35 use t::lib::Mocks;
36
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39
40 my $dbh     = C4::Context->dbh;
41
42 my $builder = t::lib::TestBuilder->new;
43 my $patron = $builder->build( { source => 'Borrower' } );
44 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
45
46 my $biblio = Koha::Biblio->new()->store();
47
48 my $biblioitem = $schema->resultset('Biblioitem')->new(
49     {
50         biblionumber => $biblio->id
51     }
52 )->insert();
53
54 subtest 'store' => sub {
55     plan tests => 1;
56     is(
57         Koha::Biblios->find( $biblio->biblionumber )->datecreated,
58         output_pref(
59             { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
60         ),
61         "datecreated must be set to today if not passed to the constructor"
62     );
63 };
64
65 subtest 'holds + current_holds' => sub {
66     plan tests => 5;
67     C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber );
68     my $holds = $biblio->holds;
69     is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
70     is( $holds->count, 1, '->holds should only return 1 hold' );
71     is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
72     $holds->delete;
73
74     # Add a hold in the future
75     C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber, undef, undef, dt_from_string->add( days => 2 ) );
76     $holds = $biblio->holds;
77     is( $holds->count, 1, '->holds should return future holds' );
78     $holds = $biblio->current_holds;
79     is( $holds->count, 0, '->current_holds should not return future holds' );
80     $holds->delete;
81
82 };
83
84 subtest 'subscriptions' => sub {
85     plan tests => 2;
86     $builder->build(
87         { source => 'Subscription', value => { biblionumber => $biblio->id } }
88     );
89     $builder->build(
90         { source => 'Subscription', value => { biblionumber => $biblio->id } }
91     );
92     my $biblio        = Koha::Biblios->find( $biblio->id );
93     my $subscriptions = $biblio->subscriptions;
94     is( ref($subscriptions), 'Koha::Subscriptions',
95         'Koha::Biblio->subscriptions should return a Koha::Subscriptions object'
96     );
97     is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
98 };
99
100 subtest 'waiting_or_in_transit' => sub {
101     plan tests => 4;
102     my $biblio = $builder->build( { source => 'Biblio' } );
103     my $item = $builder->build({
104         source => 'Item',
105         value => {
106             biblionumber => $biblio->{biblionumber}
107         }
108     });
109     my $reserve = $builder->build({
110         source => 'Reserve',
111         value => {
112             biblionumber => $biblio->{biblionumber},
113             found => undef
114         }
115     });
116
117     $reserve = Koha::Holds->find($reserve->{reserve_id});
118     $biblio = Koha::Biblios->find($biblio->{biblionumber});
119
120     is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
121
122     $reserve->found('W')->store;
123     is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
124
125     $reserve->found('T')->store;
126     is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
127
128     my $transfer = $builder->build({
129         source => 'Branchtransfer',
130         value => {
131             itemnumber => $item->{itemnumber},
132             datearrived => undef
133         }
134     });
135     my $t = Koha::Database->new()->schema()->resultset( 'Branchtransfer' )->find($transfer->{branchtransfer_id});
136     $reserve->found(undef)->store;
137     is($biblio->has_items_waiting_or_intransit, 1, 'Item has transfer');
138 };
139
140 subtest 'can_be_transferred' => sub {
141     plan tests => 8;
142
143     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
144     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
145
146     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
147     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
148     my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
149     my $biblio = $builder->build_sample_biblio({ itemtype => 'ONLY1' });
150     my ($item_bibnum, $item_bibitemnum, $itemnumber)
151         = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library1->branchcode }, $biblio->biblionumber);
152     my $item  = Koha::Items->find($itemnumber);
153
154     is(Koha::Item::Transfer::Limits->search({
155         fromBranch => $library1->branchcode,
156         toBranch => $library2->branchcode,
157     })->count, 0, 'There are no transfer limits between libraries.');
158     ok($biblio->can_be_transferred({ to => $library2 }),
159         'Some items of this biblio can be transferred between libraries.');
160
161     my $limit = Koha::Item::Transfer::Limit->new({
162         fromBranch => $library1->branchcode,
163         toBranch => $library2->branchcode,
164         itemtype => $item->effective_itemtype,
165     })->store;
166     is(Koha::Item::Transfer::Limits->search({
167         fromBranch => $library1->branchcode,
168         toBranch => $library2->branchcode,
169     })->count, 1, 'Given we have added a transfer limit that applies for all '
170         .'of this biblio\s items,');
171     is($biblio->can_be_transferred({ to => $library2 }), 0,
172         'None of the items of biblio can no longer be transferred between '
173         .'libraries.');
174     is($biblio->can_be_transferred({ to => $library2, from => $library1 }), 0,
175          'We get the same result also if we pass the from-library parameter.');
176     $item->holdingbranch($library2->branchcode)->store;
177     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given one of the '
178          .'items is already located at to-library, then the transfer is possible.');
179     $item->holdingbranch($library1->branchcode)->store;
180     my ($item_bibnum2, $item_bibitemnum2, $itemnumber2)
181         = AddItem({ homebranch => $library1->branchcode, holdingbranch => $library3->branchcode }, $biblio->biblionumber);
182     my $item2  = Koha::Items->find($itemnumber2);
183     is($biblio->can_be_transferred({ to => $library2 }), 1, 'Given we added '
184         .'another item that should have no transfer limits applying on, then '
185         .'the transfer is possible.');
186     $item2->holdingbranch($library1->branchcode)->store;
187     is($biblio->can_be_transferred({ to => $library2 }), 0, 'Given all of items'
188         .' of the biblio are from same, transfer limited library, then transfer'
189         .' is not possible.');
190 };
191
192 subtest 'custom_cover_image_url' => sub {
193     plan tests => 3;
194
195     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
196
197     my $isbn       = '0553573403 | 9780553573404 (pbk.).png';
198     my $issn       = 'my_issn';
199     my $marc_record = MARC::Record->new;
200     my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, '');
201
202     my $biblio = Koha::Biblios->find( $biblionumber );
203     my $biblioitem = $biblio->biblioitem->set(
204         { isbn => $isbn, issn => $issn });
205     is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" );
206
207     my $marc_024a = '710347104926';
208     $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) );
209     C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber );
210
211     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' );
212     is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" );
213
214     t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' );
215     my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn);
216     is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" );
217 };
218
219 $schema->storage->txn_rollback;
220
221
222 subtest 'pickup_locations' => sub {
223     plan tests => 8;
224
225     $schema->storage->txn_begin;
226
227     # Cleanup database
228     Koha::Holds->search->delete;
229     Koha::Patrons->search->delete;
230     Koha::Items->search->delete;
231     Koha::Libraries->search->delete;
232     $dbh->do('DELETE FROM issues');
233     $dbh->do('DELETE FROM issuingrules');
234     $dbh->do(
235         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
236         VALUES (?, ?, ?, ?)},
237         {},
238         '*', '*', '*', 25
239     );
240     $dbh->do('DELETE FROM circulation_rules');
241
242     my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
243     my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
244     my $root3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
245
246     my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
247     my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
248     my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
249     my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
250     my $library5 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
251     my $library6 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
252     my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
253     my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
254
255     Koha::CirculationRules->set_rules(
256         {
257             branchcode => $library1->branchcode,
258             itemtype   => undef,
259             categorycode => undef,
260             rules => {
261                 holdallowed => 1,
262                 hold_fulfillment_policy => 'any',
263                 returnbranch => 'any'
264             }
265         }
266     );
267
268     Koha::CirculationRules->set_rules(
269         {
270             branchcode => $library2->branchcode,
271             itemtype   => undef,
272             categorycode => undef,
273             rules => {
274                 holdallowed => 3,
275                 hold_fulfillment_policy => 'holdgroup',
276                 returnbranch => 'any'
277             }
278         }
279     );
280
281     Koha::CirculationRules->set_rules(
282         {
283             branchcode => $library3->branchcode,
284             itemtype   => undef,
285             categorycode => undef,
286             rules => {
287                 holdallowed => 3,
288                 hold_fulfillment_policy => 'patrongroup',
289                 returnbranch => 'any'
290             }
291         }
292     );
293
294     Koha::CirculationRules->set_rules(
295         {
296             branchcode => $library4->branchcode,
297             itemtype   => undef,
298             categorycode => undef,
299             rules => {
300                 holdallowed => 2,
301                 hold_fulfillment_policy => 'holdingbranch',
302                 returnbranch => 'any'
303             }
304         }
305     );
306
307     Koha::CirculationRules->set_rules(
308         {
309             branchcode => $library5->branchcode,
310             itemtype   => undef,
311             categorycode => undef,
312             rules => {
313                 holdallowed => 2,
314                 hold_fulfillment_policy => 'homebranch',
315                 returnbranch => 'any'
316             }
317         }
318     );
319
320     Koha::CirculationRules->set_rules(
321         {
322             branchcode => $library6->branchcode,
323             itemtype   => undef,
324             categorycode => undef,
325             rules => {
326                 holdallowed => 1,
327                 hold_fulfillment_policy => 'holdgroup',
328                 returnbranch => 'any'
329             }
330         }
331     );
332
333     Koha::CirculationRules->set_rules(
334         {
335             branchcode => $library7->branchcode,
336             itemtype   => undef,
337             categorycode => undef,
338             rules => {
339                 holdallowed => 3,
340                 hold_fulfillment_policy => 'holdingbranch',
341                 returnbranch => 'any'
342             }
343         }
344     );
345
346
347     Koha::CirculationRules->set_rules(
348         {
349             branchcode => $library8->branchcode,
350             itemtype   => undef,
351             categorycode => undef,
352             rules => {
353                 holdallowed => 2,
354                 hold_fulfillment_policy => 'patrongroup',
355                 returnbranch => 'any'
356             }
357         }
358     );
359
360     my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
361     my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
362
363     my $group2_3 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
364     my $group2_4 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
365
366     my $group3_5 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library5->branchcode } } );
367     my $group3_6 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library6->branchcode } } );
368     my $group3_7 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library7->branchcode } } );
369     my $group3_8 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root3->id, branchcode => $library8->branchcode } } );
370
371     my $biblio1  = $builder->build_object( { class => 'Koha::Biblios', value => {title => '1'} } );
372     my $biblioitem1 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio1->biblionumber } } );
373     my $biblio2  = $builder->build_object( { class => 'Koha::Biblios', value => {title => '2'} } );
374     my $biblioitem2 = $builder->build_object( { class => 'Koha::Biblioitems', value => { biblionumber => $biblio2->biblionumber } } );
375
376     my $item1_1  = Koha::Item->new({
377         biblionumber     => $biblio1->biblionumber,
378         biblioitemnumber => $biblioitem1->biblioitemnumber,
379         homebranch       => $library1->branchcode,
380         holdingbranch    => $library2->branchcode,
381         itype            => 'test',
382         barcode          => "item11barcode",
383     })->store;
384
385     my $item1_3  = Koha::Item->new({
386         biblionumber     => $biblio1->biblionumber,
387         biblioitemnumber => $biblioitem1->biblioitemnumber,
388         homebranch       => $library3->branchcode,
389         holdingbranch    => $library4->branchcode,
390         itype            => 'test',
391         barcode          => "item13barcode",
392     })->store;
393
394     my $item1_7  = Koha::Item->new({
395         biblionumber     => $biblio1->biblionumber,
396         biblioitemnumber => $biblioitem1->biblioitemnumber,
397         homebranch       => $library7->branchcode,
398         holdingbranch    => $library4->branchcode,
399         itype            => 'test',
400         barcode          => "item17barcode",
401     })->store;
402
403     my $item2_2  = Koha::Item->new({
404         biblionumber     => $biblio2->biblionumber,
405         biblioitemnumber => $biblioitem2->biblioitemnumber,
406         homebranch       => $library2->branchcode,
407         holdingbranch    => $library1->branchcode,
408         itype            => 'test',
409         barcode          => "item22barcode",
410     })->store;
411
412     my $item2_4  = Koha::Item->new({
413         biblionumber     => $biblio2->biblionumber,
414         biblioitemnumber => $biblioitem2->biblioitemnumber,
415         homebranch       => $library4->branchcode,
416         holdingbranch    => $library3->branchcode,
417         itype            => 'test',
418         barcode          => "item23barcode",
419     })->store;
420
421     my $item2_6  = Koha::Item->new({
422         biblionumber     => $biblio2->biblionumber,
423         biblioitemnumber => $biblioitem2->biblioitemnumber,
424         homebranch       => $library6->branchcode,
425         holdingbranch    => $library4->branchcode,
426         itype            => 'test',
427         barcode          => "item26barcode",
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 = $biblio->pickup_locations( { patron => $patron} );
449
450         ok(scalar(@pl) == $results->{$cbranch.'-'.$biblio->title.'-'.$patron->firstname}, 'ReservesControlBranch: '.$cbranch.', biblio'.$biblio->title.', patron'.$patron->firstname.' should return '.$results->{$cbranch.'-'.$biblio->title.'-'.$patron->firstname}.' but returns '.scalar(@pl));
451     }
452
453     foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
454         foreach my $biblio ($biblio1, $biblio2) {
455             foreach my $patron ($patron1, $patron8) {
456                 _doTest($cbranch, $biblio, $patron, $results);
457             }
458         }
459     }
460
461     $schema->storage->txn_rollback;
462 };