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 |
branchcode => $library1->{branchcode}, |
40 |
dateexpiry => '3000-01-01', |
41 |
} |
42 |
}); |
43 |
|
44 |
my $borrower2 = $builder->build({ |
45 |
source => 'Borrower', |
46 |
value => { |
47 |
branchcode => $library1->{branchcode}, |
48 |
dateexpiry => '3000-01-01', |
49 |
} |
50 |
}); |
51 |
|
52 |
# Test hold_fulfillment_policy |
53 |
my ( $itemtype ) = @{ $dbh->selectrow_arrayref("SELECT itemtype FROM itemtypes LIMIT 1") }; |
54 |
my $borrowernumber1 = $borrower1->{borrowernumber}; |
55 |
my $borrowernumber2 = $borrower2->{borrowernumber}; |
56 |
my $library_A = $library1->{branchcode}; |
57 |
|
58 |
$dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')"); |
59 |
|
60 |
my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'") |
61 |
or BAIL_OUT("Cannot find newly created biblio record"); |
62 |
|
63 |
$dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')"); |
64 |
|
65 |
my $biblioitemnumber = |
66 |
$dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber") |
67 |
or BAIL_OUT("Cannot find newly created biblioitems record"); |
68 |
|
69 |
$dbh->do(" |
70 |
INSERT INTO items (barcode, biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype) |
71 |
VALUES ('AllowHoldIf1', $biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype') |
72 |
"); |
73 |
|
74 |
my $itemnumber1 = |
75 |
$dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber") |
76 |
or BAIL_OUT("Cannot find newly created item"); |
77 |
|
78 |
my $item1 = GetItem( $itemnumber1 ); |
79 |
|
80 |
$dbh->do(" |
81 |
INSERT INTO items (barcode, biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype) |
82 |
VALUES ('AllowHoldIf2', $biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype') |
83 |
"); |
84 |
|
85 |
my $itemnumber2 = |
86 |
$dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber ORDER BY itemnumber DESC") |
87 |
or BAIL_OUT("Cannot find newly created item"); |
88 |
|
89 |
my $item2 = GetItem( $itemnumber2 ); |
90 |
|
91 |
$dbh->do("DELETE FROM issuingrules"); |
92 |
my $rule = Koha::IssuingRule->new( |
93 |
{ |
94 |
categorycode => '*', |
95 |
itemtype => '*', |
96 |
branchcode => '*', |
97 |
maxissueqty => 99, |
98 |
issuelength => 7, |
99 |
lengthunit => 8, |
100 |
reservesallowed => 99, |
101 |
onshelfholds => 2, |
102 |
} |
103 |
); |
104 |
$rule->store(); |
105 |
|
106 |
my $is = IsAvailableForItemLevelRequest( $item1, $borrower1); |
107 |
is( $is, 0, "Item cannot be held, 2 items available" ); |
108 |
|
109 |
AddIssue( $borrower2, $item1->{barcode} ); |
110 |
|
111 |
$is = IsAvailableForItemLevelRequest( $item1, $borrower1); |
112 |
is( $is, 0, "Item cannot be held, 1 item available" ); |
113 |
|
114 |
AddIssue( $borrower2, $item2->{barcode} ); |
115 |
|
116 |
$is = IsAvailableForItemLevelRequest( $item1, $borrower1); |
117 |
is( $is, 1, "Item can be held, no items available" ); |
118 |
|
119 |
# Cleanup |
120 |
$schema->storage->txn_rollback; |