10 use Test::More tests => 6;
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({
31 my $itemtype = $builder->build({
33 value => { notforloan => 0 }
36 t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
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 my $borrower3 = $builder->build({
58 branchcode => $library2->{branchcode},
59 dateexpiry => '3000-01-01',
63 my $borrowernumber1 = $borrower1->{borrowernumber};
64 my $borrowernumber2 = $borrower2->{borrowernumber};
65 my $library_A = $library1->{branchcode};
66 my $library_B = $library2->{branchcode};
68 my $biblio = $builder->build_sample_biblio({itemtype=>$itemtype});
69 my $biblionumber = $biblio->biblionumber;
70 my $item1 = $builder->build_sample_item({
71 biblionumber=>$biblionumber,
73 homebranch => $library_A,
74 holdingbranch => $library_A
76 my $item2 = $builder->build_sample_item({
77 biblionumber=>$biblionumber,
79 homebranch => $library_A,
80 holdingbranch => $library_A
83 # Test hold_fulfillment_policy
87 my $rule = Koha::IssuingRule->new(
90 itemtype => $itemtype,
94 reservesallowed => 99,
100 my $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
101 is( $is, 0, "Item cannot be held, 2 items available" );
103 my $issue1 = AddIssue( $borrower2, $item1->{barcode} );
105 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
106 is( $is, 0, "Item cannot be held, 1 item available" );
108 AddIssue( $borrower2, $item2->{barcode} );
110 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
111 is( $is, 1, "Item can be held, no items available" );
113 AddReturn( $item1->{barcode} );
115 { # Remove the issue for the first patron, and modify the branch for item1
116 subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
119 my $hold_allowed_from_home_library = 1;
120 my $hold_allowed_from_any_libraries = 2;
121 my $sth_delete_rules = $dbh->prepare(q|DELETE FROM default_circ_rules|);
122 my $sth_insert_rule = $dbh->prepare(q|INSERT INTO default_circ_rules(singleton, holdallowed, hold_fulfillment_policy, returnbranch) VALUES ('singleton', ?, 'any', 'homebranch');|);
123 my $sth_insert_branch_rule = $dbh->prepare(q|INSERT INTO default_branch_circ_rules(branchcode, holdallowed, hold_fulfillment_policy, returnbranch) VALUES (?, ?, 'any', 'homebranch');|);
125 subtest 'Item is available at a different library' => sub {
128 $item1 = Koha::Items->find( $item1->{itemnumber} );
129 $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
130 $item1 = $item1->unblessed;
132 #One shelf holds is 'If all unavailable'/2
133 #Item 1 homebranch library B is available
134 #Item 2 homebranch library A is checked out
135 #Borrower1 is from library A
138 $sth_delete_rules->execute;
139 $sth_insert_rule->execute( $hold_allowed_from_home_library );
141 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
142 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
143 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" );
144 $is = IsAvailableForItemLevelRequest( $item1, $borrower2);
145 is( $is, 1, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at home library, holdable = one available => the hold is not allowed at item level" );
146 $sth_insert_branch_rule->execute( $library_B, $hold_allowed_from_any_libraries );
147 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
148 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
149 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, One item is available at different library, holdable = one available => the hold is not allowed at item level" );
151 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
152 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
153 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" );
154 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
155 $sth_insert_branch_rule->execute( $library_A, $hold_allowed_from_any_libraries );
156 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
157 is( $is, 0, "Hold allowed from home library + ReservesControlBranch=PatronLibrary, One item is available at different library, holdable = one available => the hold is not 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 $item1 = Koha::Items->find( $item1->{itemnumber} );
178 $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
179 $item1 = $item1->unblessed;
181 #One shelf holds is 'If all unavailable'/2
182 #Item 1 homebranch library A is available
183 #Item 2 homebranch library A is checked out
184 #Borrower1 is from library A
185 #CircControl has no effect - same rule for all branches as set at line 96
186 #ReservesControlBranch is not checked in these subs we are testing?
189 $sth_delete_rules->execute;
190 $sth_insert_rule->execute( $hold_allowed_from_home_library );
192 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
193 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
194 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" );
196 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
197 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
198 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" );
202 $sth_delete_rules->execute;
203 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
205 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
206 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
207 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" );
209 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
210 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
211 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 my $itemtype2 = $builder->build({
218 source => 'Itemtype',
219 value => { notforloan => 0 }
221 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
223 my $hold = $builder->build({
226 itemnumber => $item3->itemnumber,
231 $rule = Koha::IssuingRule->new(
234 itemtype => $itemtype2,
238 reservesallowed => 99,
244 $is = IsAvailableForItemLevelRequest( $item3->unblessed, $borrower1);
245 is( $is, 1, "Item can be held, items in transit are not available" );
248 $schema->storage->txn_rollback;