|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
|
| 5 |
use C4::Context; |
| 6 |
use C4::Items; |
| 7 |
use C4::Circulation; |
| 8 |
use Koha::IssuingRule; |
| 9 |
|
| 10 |
use Test::More tests => 4; |
| 11 |
|
| 12 |
use t::lib::TestBuilder; |
| 13 |
|
| 14 |
BEGIN { |
| 15 |
use FindBin; |
| 16 |
use lib $FindBin::Bin; |
| 17 |
use_ok('C4::Reserves'); |
| 18 |
} |
| 19 |
|
| 20 |
my $schema = Koha::Database->schema; |
| 21 |
$schema->storage->txn_begin; |
| 22 |
my $dbh = C4::Context->dbh; |
| 23 |
|
| 24 |
my $builder = t::lib::TestBuilder->new; |
| 25 |
|
| 26 |
my $library1 = $builder->build({ |
| 27 |
source => 'Branch', |
| 28 |
}); |
| 29 |
|
| 30 |
# Now, set a userenv |
| 31 |
C4::Context->_new_userenv('xxx'); |
| 32 |
C4::Context->set_userenv(0,0,0,'firstname','surname', $library1->{branchcode}, 'Midway Public Library', '', '', ''); |
| 33 |
|
| 34 |
my $bib_title = "Test Title"; |
| 35 |
|
| 36 |
my $borrower1 = $builder->build({ |
| 37 |
source => 'Borrower', |
| 38 |
value => { |
| 39 |
categorycode => 'S', |
| 40 |
branchcode => $library1->{branchcode}, |
| 41 |
dateexpiry => '3000-01-01', |
| 42 |
} |
| 43 |
}); |
| 44 |
|
| 45 |
my $borrower2 = $builder->build({ |
| 46 |
source => 'Borrower', |
| 47 |
value => { |
| 48 |
categorycode => 'S', |
| 49 |
branchcode => $library1->{branchcode}, |
| 50 |
dateexpiry => '3000-01-01', |
| 51 |
} |
| 52 |
}); |
| 53 |
|
| 54 |
# Test hold_fulfillment_policy |
| 55 |
my ( $itemtype ) = @{ $dbh->selectrow_arrayref("SELECT itemtype FROM itemtypes LIMIT 1") }; |
| 56 |
my $borrowernumber1 = $borrower1->{borrowernumber}; |
| 57 |
my $borrowernumber2 = $borrower2->{borrowernumber}; |
| 58 |
my $library_A = $library1->{branchcode}; |
| 59 |
|
| 60 |
$dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')"); |
| 61 |
|
| 62 |
my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'") |
| 63 |
or BAIL_OUT("Cannot find newly created biblio record"); |
| 64 |
|
| 65 |
$dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')"); |
| 66 |
|
| 67 |
my $biblioitemnumber = |
| 68 |
$dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber") |
| 69 |
or BAIL_OUT("Cannot find newly created biblioitems record"); |
| 70 |
|
| 71 |
$dbh->do(" |
| 72 |
INSERT INTO items (barcode, biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype) |
| 73 |
VALUES ('AllowHoldIf1', $biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype') |
| 74 |
"); |
| 75 |
|
| 76 |
my $itemnumber1 = |
| 77 |
$dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber") |
| 78 |
or BAIL_OUT("Cannot find newly created item"); |
| 79 |
|
| 80 |
my $item1 = GetItem( $itemnumber1 ); |
| 81 |
|
| 82 |
$dbh->do(" |
| 83 |
INSERT INTO items (barcode, biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype) |
| 84 |
VALUES ('AllowHoldIf2', $biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype') |
| 85 |
"); |
| 86 |
|
| 87 |
my $itemnumber2 = |
| 88 |
$dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber ORDER BY itemnumber DESC") |
| 89 |
or BAIL_OUT("Cannot find newly created item"); |
| 90 |
|
| 91 |
my $item2 = GetItem( $itemnumber2 ); |
| 92 |
|
| 93 |
$dbh->do("DELETE FROM issuingrules"); |
| 94 |
my $rule = Koha::IssuingRule->new( |
| 95 |
{ |
| 96 |
categorycode => '*', |
| 97 |
itemtype => '*', |
| 98 |
branchcode => '*', |
| 99 |
maxissueqty => 99, |
| 100 |
issuelength => 7, |
| 101 |
lengthunit => 8, |
| 102 |
reservesallowed => 99, |
| 103 |
onshelfholds => 2, |
| 104 |
} |
| 105 |
); |
| 106 |
$rule->store(); |
| 107 |
|
| 108 |
my $is = IsAvailableForItemLevelRequest( $item1, $borrower1); |
| 109 |
is( $is, 0, "Item cannot be held, 2 items available" ); |
| 110 |
|
| 111 |
AddIssue( $borrower2, $item1->{barcode} ); |
| 112 |
|
| 113 |
$is = IsAvailableForItemLevelRequest( $item1, $borrower1); |
| 114 |
is( $is, 0, "Item cannot be held, 1 item available" ); |
| 115 |
|
| 116 |
AddIssue( $borrower2, $item2->{barcode} ); |
| 117 |
|
| 118 |
$is = IsAvailableForItemLevelRequest( $item1, $borrower1); |
| 119 |
is( $is, 1, "Item can be held, no items available" ); |
| 120 |
|
| 121 |
# Cleanup |
| 122 |
$schema->storage->txn_rollback; |