|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2017 R-Bit Technology, s.r.o. |
| 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 => 5; |
| 23 |
use t::lib::TestBuilder; |
| 24 |
use t::lib::Mocks; |
| 25 |
|
| 26 |
use C4::Reserves qw( GetBorrowersToSatisfyHold ); |
| 27 |
use Koha::Database; |
| 28 |
use Koha::Holds; |
| 29 |
use Data::Dumper; |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
$schema->storage->txn_begin; |
| 33 |
|
| 34 |
my $builder = t::lib::TestBuilder->new(); |
| 35 |
my $libraryA = $builder->build( |
| 36 |
{ |
| 37 |
source => 'Branch', |
| 38 |
} |
| 39 |
); |
| 40 |
|
| 41 |
my $libraryB = $builder->build( |
| 42 |
{ |
| 43 |
source => 'Branch', |
| 44 |
} |
| 45 |
); |
| 46 |
|
| 47 |
my $holdRequestorInA = $builder->build( |
| 48 |
{ |
| 49 |
source => 'Borrower', |
| 50 |
value => { |
| 51 |
branchcode => $libraryA->{branchcode}, |
| 52 |
cardnumber => '001', |
| 53 |
}, |
| 54 |
} |
| 55 |
); |
| 56 |
|
| 57 |
my $patronInA = $builder->build( |
| 58 |
{ |
| 59 |
source => 'Borrower', |
| 60 |
value => { |
| 61 |
branchcode => $libraryA->{branchcode}, |
| 62 |
cardnumber => '002', |
| 63 |
}, |
| 64 |
} |
| 65 |
); |
| 66 |
|
| 67 |
my $patronInB = $builder->build( |
| 68 |
{ |
| 69 |
source => 'Borrower', |
| 70 |
value => { |
| 71 |
branchcode => $libraryB->{branchcode}, |
| 72 |
cardnumber => '003', |
| 73 |
}, |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
my $biblio = $builder->build( |
| 78 |
{ |
| 79 |
source => 'Biblio', |
| 80 |
value => { |
| 81 |
title => 'Title 1', |
| 82 |
}, |
| 83 |
} |
| 84 |
); |
| 85 |
my $itemType = $builder->build( |
| 86 |
{ |
| 87 |
source => 'Itemtype', |
| 88 |
} |
| 89 |
); |
| 90 |
my $itemInA = $builder->build( |
| 91 |
{ |
| 92 |
source => 'Item', |
| 93 |
value => { |
| 94 |
biblionumber => $biblio->{biblionumber}, |
| 95 |
homebranch => $libraryA->{branchcode}, |
| 96 |
holdingbranch => $libraryA->{branchcode}, |
| 97 |
itype => $itemType->{itemtype}, |
| 98 |
}, |
| 99 |
} |
| 100 |
); |
| 101 |
my $itemInB = $builder->build( |
| 102 |
{ |
| 103 |
source => 'Item', |
| 104 |
value => { |
| 105 |
biblionumber => $biblio->{biblionumber}, |
| 106 |
homebranch => $libraryB->{branchcode}, |
| 107 |
holdingbranch => $libraryB->{branchcode}, |
| 108 |
itype => $itemType->{itemtype}, |
| 109 |
}, |
| 110 |
} |
| 111 |
); |
| 112 |
|
| 113 |
my $issueOfItemInA = $builder->build( |
| 114 |
{ |
| 115 |
source => 'Issue', |
| 116 |
value => { |
| 117 |
borrowernumber => $patronInB->{borrowernumber}, |
| 118 |
itemnumber => $itemInA->{itemnumber}, |
| 119 |
}, |
| 120 |
} |
| 121 |
); |
| 122 |
my $issueOfItemInB = $builder->build( |
| 123 |
{ |
| 124 |
source => 'Issue', |
| 125 |
value => { |
| 126 |
borrowernumber => $patronInA->{borrowernumber}, |
| 127 |
itemnumber => $itemInB->{itemnumber}, |
| 128 |
}, |
| 129 |
} |
| 130 |
); |
| 131 |
|
| 132 |
t::lib::Mocks::mock_preference('NotifyToReturnItemWhenHoldIsPlaced', 1); # Assuming the notification is allowed |
| 133 |
|
| 134 |
my $hold = Koha::Hold->new( |
| 135 |
{ |
| 136 |
borrowernumber => $holdRequestorInA->{borrowernumber}, |
| 137 |
biblionumber => $biblio->{biblionumber}, |
| 138 |
reservedate => '2017-01-01', |
| 139 |
branchcode => $holdRequestorInA->{branchcode}, |
| 140 |
priority => 1, |
| 141 |
reservenotes => 'dummy text', |
| 142 |
#itemnumber => $itemInA->{itemnumber}, |
| 143 |
waitingdate => '2099-01-01', |
| 144 |
expirationdate => '2099-01-01', |
| 145 |
itemtype => $itemInA->{itype}, |
| 146 |
} |
| 147 |
)->store(); |
| 148 |
|
| 149 |
my @borrowers; |
| 150 |
|
| 151 |
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'AnyLibrary'); # Assuming items from any library |
| 152 |
@borrowers = GetBorrowersToSatisfyHold($hold); |
| 153 |
is(scalar(grep {defined $_} @borrowers), 2, "2 borrowers with requested item found in any library"); |
| 154 |
|
| 155 |
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'RequestorLibrary'); # Assuming items from requestor library |
| 156 |
@borrowers = GetBorrowersToSatisfyHold($hold); |
| 157 |
is(scalar(grep {defined $_} @borrowers), 1, "1 item found in requestor library"); |
| 158 |
is($borrowers[0]->{cardnumber}, "002", " ...and it is in 002's hands"); |
| 159 |
|
| 160 |
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'ItemHomeLibrary'); # Assuming items from the same library as requested item |
| 161 |
@borrowers = GetBorrowersToSatisfyHold($hold); |
| 162 |
is(scalar(grep {defined $_} @borrowers), 1, "1 item found in the same library as the requested item"); |
| 163 |
is($borrowers[0]->{cardnumber}, "003", " ...and it is in 003's hands"); |
| 164 |
|
| 165 |
$schema->storage->txn_rollback; |
| 166 |
|
| 167 |
1; |