3 # Copyright 2016 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 6;
30 use Koha::DateUtils qw( dt_from_string output_pref );
33 use Koha::Subscriptions;
34 use t::lib::TestBuilder;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
40 my $dbh = C4::Context->dbh;
42 my $builder = t::lib::TestBuilder->new;
43 my $patron = $builder->build( { source => 'Borrower' } );
44 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
46 my $biblio = Koha::Biblio->new()->store();
48 my $biblioitem = $schema->resultset('Biblioitem')->new(
50 biblionumber => $biblio->id
54 subtest 'store' => sub {
57 Koha::Biblios->find( $biblio->biblionumber )->datecreated,
59 { dt => dt_from_string, dateformat => 'iso', dateonly => 1 }
61 "datecreated must be set to today if not passed to the constructor"
65 subtest 'holds + current_holds' => sub {
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' );
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' );
84 subtest 'subscriptions' => sub {
87 { source => 'Subscription', value => { biblionumber => $biblio->id } }
90 { source => 'Subscription', value => { biblionumber => $biblio->id } }
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'
97 is( $subscriptions->count, 2, 'Koha::Biblio->subscriptions should return the correct number of subscriptions');
100 subtest 'waiting_or_in_transit' => sub {
102 my $biblio = $builder->build( { source => 'Biblio' } );
103 my $item = $builder->build({
106 biblionumber => $biblio->{biblionumber}
109 my $reserve = $builder->build({
112 biblionumber => $biblio->{biblionumber},
117 $reserve = Koha::Holds->find($reserve->{reserve_id});
118 $biblio = Koha::Biblios->find($biblio->{biblionumber});
120 is($biblio->has_items_waiting_or_intransit, 0, 'Item is neither waiting nor in transit');
122 $reserve->found('W')->store;
123 is($biblio->has_items_waiting_or_intransit, 1, 'Item is waiting');
125 $reserve->found('T')->store;
126 is($biblio->has_items_waiting_or_intransit, 1, 'Item is in transit');
128 my $transfer = $builder->build({
129 source => 'Branchtransfer',
131 itemnumber => $item->{itemnumber},
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');
140 subtest 'can_be_transferred' => sub {
143 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
144 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
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);
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.');
161 my $limit = Koha::Item::Transfer::Limit->new({
162 fromBranch => $library1->branchcode,
163 toBranch => $library2->branchcode,
164 itemtype => $item->effective_itemtype,
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 '
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.');
192 subtest 'custom_cover_image_url' => sub {
195 t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' );
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, '');
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" );
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 );
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" );
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" );
219 $schema->storage->txn_rollback;
222 subtest 'pickup_locations' => sub {
225 $schema->storage->txn_begin;
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');
235 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
236 VALUES (?, ?, ?, ?)},
240 $dbh->do('DELETE FROM circulation_rules');
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 } } );
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 } } );
255 Koha::CirculationRules->set_rules(
257 branchcode => $library1->branchcode,
259 categorycode => undef,
262 hold_fulfillment_policy => 'any',
263 returnbranch => 'any'
268 Koha::CirculationRules->set_rules(
270 branchcode => $library2->branchcode,
272 categorycode => undef,
275 hold_fulfillment_policy => 'holdgroup',
276 returnbranch => 'any'
281 Koha::CirculationRules->set_rules(
283 branchcode => $library3->branchcode,
285 categorycode => undef,
288 hold_fulfillment_policy => 'patrongroup',
289 returnbranch => 'any'
294 Koha::CirculationRules->set_rules(
296 branchcode => $library4->branchcode,
298 categorycode => undef,
301 hold_fulfillment_policy => 'holdingbranch',
302 returnbranch => 'any'
307 Koha::CirculationRules->set_rules(
309 branchcode => $library5->branchcode,
311 categorycode => undef,
314 hold_fulfillment_policy => 'homebranch',
315 returnbranch => 'any'
320 Koha::CirculationRules->set_rules(
322 branchcode => $library6->branchcode,
324 categorycode => undef,
327 hold_fulfillment_policy => 'holdgroup',
328 returnbranch => 'any'
333 Koha::CirculationRules->set_rules(
335 branchcode => $library7->branchcode,
337 categorycode => undef,
340 hold_fulfillment_policy => 'holdingbranch',
341 returnbranch => 'any'
347 Koha::CirculationRules->set_rules(
349 branchcode => $library8->branchcode,
351 categorycode => undef,
354 hold_fulfillment_policy => 'patrongroup',
355 returnbranch => 'any'
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 } } );
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 } } );
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 } } );
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 } } );
376 my $item1_1 = Koha::Item->new({
377 biblionumber => $biblio1->biblionumber,
378 biblioitemnumber => $biblioitem1->biblioitemnumber,
379 homebranch => $library1->branchcode,
380 holdingbranch => $library2->branchcode,
382 barcode => "item11barcode",
385 my $item1_3 = Koha::Item->new({
386 biblionumber => $biblio1->biblionumber,
387 biblioitemnumber => $biblioitem1->biblioitemnumber,
388 homebranch => $library3->branchcode,
389 holdingbranch => $library4->branchcode,
391 barcode => "item13barcode",
394 my $item1_7 = Koha::Item->new({
395 biblionumber => $biblio1->biblionumber,
396 biblioitemnumber => $biblioitem1->biblioitemnumber,
397 homebranch => $library7->branchcode,
398 holdingbranch => $library4->branchcode,
400 barcode => "item17barcode",
403 my $item2_2 = Koha::Item->new({
404 biblionumber => $biblio2->biblionumber,
405 biblioitemnumber => $biblioitem2->biblioitemnumber,
406 homebranch => $library2->branchcode,
407 holdingbranch => $library1->branchcode,
409 barcode => "item22barcode",
412 my $item2_4 = Koha::Item->new({
413 biblionumber => $biblio2->biblionumber,
414 biblioitemnumber => $biblioitem2->biblioitemnumber,
415 homebranch => $library4->branchcode,
416 holdingbranch => $library3->branchcode,
418 barcode => "item23barcode",
421 my $item2_6 = Koha::Item->new({
422 biblionumber => $biblio2->biblionumber,
423 biblioitemnumber => $biblioitem2->biblioitemnumber,
424 homebranch => $library6->branchcode,
425 holdingbranch => $library4->branchcode,
427 barcode => "item26barcode",
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 } } );
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,
445 my ( $cbranch, $biblio, $patron, $results ) = @_;
446 t::lib::Mocks::mock_preference('ReservesControlBranch', $cbranch);
448 my @pl = $biblio->pickup_locations( { patron => $patron} );
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));
453 foreach my $cbranch ('ItemHomeLibrary','PatronLibrary') {
454 foreach my $biblio ($biblio1, $biblio2) {
455 foreach my $patron ($patron1, $patron8) {
456 _doTest($cbranch, $biblio, $patron, $results);
461 $schema->storage->txn_rollback;