10 use Test::More tests => 5;
12 use t::lib::TestBuilder;
16 use_ok('C4::Reserves');
19 my $schema = Koha::Database->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
23 my $builder = t::lib::TestBuilder->new;
25 my $library1 = $builder->build({
28 my $library2 = $builder->build({
34 C4::Context->_new_userenv('xxx');
35 C4::Context->set_userenv(0,0,0,'firstname','surname', $library1->{branchcode}, 'Midway Public Library', '', '', '');
37 my $bib_title = "Test Title";
39 my $borrower1 = $builder->build({
42 branchcode => $library1->{branchcode},
43 dateexpiry => '3000-01-01',
47 my $borrower2 = $builder->build({
50 branchcode => $library1->{branchcode},
51 dateexpiry => '3000-01-01',
55 # Test hold_fulfillment_policy
56 my ( $itemtype ) = @{ $dbh->selectrow_arrayref("SELECT itemtype FROM itemtypes LIMIT 1") };
57 my $borrowernumber1 = $borrower1->{borrowernumber};
58 my $borrowernumber2 = $borrower2->{borrowernumber};
59 my $library_A = $library1->{branchcode};
60 my $library_B = $library2->{branchcode};
62 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
64 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
65 or BAIL_OUT("Cannot find newly created biblio record");
67 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
69 my $biblioitemnumber =
70 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
71 or BAIL_OUT("Cannot find newly created biblioitems record");
74 INSERT INTO items (barcode, biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
75 VALUES ('AllowHoldIf1', $biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
79 $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
80 or BAIL_OUT("Cannot find newly created item");
82 my $item1 = GetItem( $itemnumber1 );
85 INSERT INTO items (barcode, biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
86 VALUES ('AllowHoldIf2', $biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype')
90 $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber ORDER BY itemnumber DESC")
91 or BAIL_OUT("Cannot find newly created item");
93 my $item2 = GetItem( $itemnumber2 );
95 $dbh->do("DELETE FROM issuingrules");
96 my $rule = Koha::IssuingRule->new(
104 reservesallowed => 99,
110 my $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
111 is( $is, 0, "Item cannot be held, 2 items available" );
113 my $issue1 = AddIssue( $borrower2, $item1->{barcode} );
115 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
116 is( $is, 0, "Item cannot be held, 1 item available" );
118 AddIssue( $borrower2, $item2->{barcode} );
120 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
121 is( $is, 1, "Item can be held, no items available" );
123 AddReturn( $item1->{barcode} );
125 { # Remove the issue for the first patron, and modify the branch for item1
126 subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
129 my $hold_allowed_from_home_library = 1;
130 my $hold_allowed_from_any_libraries = 2;
131 my $sth_delete_rules = $dbh->prepare(q|DELETE FROM default_circ_rules|);
132 my $sth_insert_rule = $dbh->prepare(q|INSERT INTO default_circ_rules(singleton, maxissueqty, maxonsiteissueqty, holdallowed, hold_fulfillment_policy, returnbranch) VALUES ('singleton', NULL, NULL, ?, 'any', 'homebranch');|);
134 subtest 'Item is available at a different library' => sub {
137 Koha::Items->find( $item1->{itemnumber} )->set({homebranch => $library_B, holdingbranch => $library_B })->store;
138 $item1 = GetItem( $itemnumber1 );
140 #One shelf holds is 'If all unavailable'/2
141 #Item 1 homebranch library B is available
142 #Item 2 homebranch library A is checked out
143 #Borrower1 is from library A
144 #CircControl has no effect - same rule for all branches as set at line 96
145 #FIXME: ReservesControlBranch is not checked in these subs we are testing
148 $sth_delete_rules->execute;
149 $sth_insert_rule->execute( $hold_allowed_from_home_library );
151 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
152 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
153 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at different library, not holdable = none available => the hold is allowed at item level" );
155 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
156 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
157 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at different library, not holdable = none available => the hold is allowed at item level" );
161 $sth_delete_rules->execute;
162 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
164 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
165 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
166 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level" );
168 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
169 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
170 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=PatronLibrary, One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level" );
174 subtest 'Item is available at the same library' => sub {
177 Koha::Items->find( $item1->{itemnumber} )->set({homebranch => $library_A, holdingbranch => $library_A })->store;
178 $item1 = GetItem( $itemnumber1 );
180 #One shelf holds is 'If all unavailable'/2
181 #Item 1 homebranch library A is available
182 #Item 2 homebranch library A is checked out
183 #Borrower1 is from library A
184 #CircControl has no effect - same rule for all branches as set at line 96
185 #ReservesControlBranch is not checked in these subs we are testing?
188 $sth_delete_rules->execute;
189 $sth_insert_rule->execute( $hold_allowed_from_home_library );
191 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
192 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
193 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
195 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
196 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
197 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
201 $sth_delete_rules->execute;
202 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
204 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
205 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
206 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
208 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
209 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
210 is( $is, 0, "Hold allowed from any library + ReservesControlBranch=PatronLibrary, One item is available at the same library, holdable = 1 available => the hold is not allowed at item level" );
217 $schema->storage->txn_rollback;