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 $patron1 = $builder->build_object({
40 class => 'Koha::Patrons',
42 branchcode => $library1->{branchcode},
43 dateexpiry => '3000-01-01',
46 my $borrower1 = $patron1->unblessed;
48 my $patron2 = $builder->build_object({
49 class => 'Koha::Patrons',
51 branchcode => $library1->{branchcode},
52 dateexpiry => '3000-01-01',
56 my $patron3 = $builder->build_object({
57 class => 'Koha::Patrons',
59 branchcode => $library2->{branchcode},
60 dateexpiry => '3000-01-01',
64 my $library_A = $library1->{branchcode};
65 my $library_B = $library2->{branchcode};
67 my $biblio = $builder->build_sample_biblio({itemtype=>$itemtype});
68 my $biblionumber = $biblio->biblionumber;
69 my $item1 = $builder->build_sample_item({
70 biblionumber=>$biblionumber,
72 homebranch => $library_A,
73 holdingbranch => $library_A
75 my $item2 = $builder->build_sample_item({
76 biblionumber=>$biblionumber,
78 homebranch => $library_A,
79 holdingbranch => $library_A
82 # Test hold_fulfillment_policy
83 my $rule = Koha::IssuingRule->new(
86 itemtype => $itemtype,
90 reservesallowed => 99,
96 my $is = IsAvailableForItemLevelRequest( $item1, $patron1);
97 is( $is, 0, "Item cannot be held, 2 items available" );
99 my $issue1 = AddIssue( $patron2->unblessed, $item1->barcode );
101 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
102 is( $is, 0, "Item cannot be held, 1 item available" );
104 AddIssue( $patron2->unblessed, $item2->barcode );
106 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
107 is( $is, 1, "Item can be held, no items available" );
109 AddReturn( $item1->barcode );
111 { # Remove the issue for the first patron, and modify the branch for item1
112 subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
115 my $hold_allowed_from_home_library = 1;
116 my $hold_allowed_from_any_libraries = 2;
118 subtest 'Item is available at a different library' => sub {
121 $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
123 #One shelf holds is 'If all unavailable'/2
124 #Item 1 homebranch library B is available
125 #Item 2 homebranch library A is checked out
126 #Borrower1 is from library A
129 set_holdallowed_rule( $hold_allowed_from_home_library );
131 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
132 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
133 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" );
134 $is = IsAvailableForItemLevelRequest( $item1, $patron2);
135 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" );
136 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_B );
137 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
138 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
139 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" );
141 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
142 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
143 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" );
144 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
145 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_A );
146 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
147 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" );
151 set_holdallowed_rule( $hold_allowed_from_any_libraries );
153 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
154 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
155 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" );
157 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
158 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
159 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" );
163 subtest 'Item is available at the same library' => sub {
166 $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
168 #One shelf holds is 'If all unavailable'/2
169 #Item 1 homebranch library A is available
170 #Item 2 homebranch library A is checked out
171 #Borrower1 is from library A
172 #CircControl has no effect - same rule for all branches as set at line 96
173 #ReservesControlBranch is not checked in these subs we are testing?
176 set_holdallowed_rule( $hold_allowed_from_home_library );
178 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
179 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
180 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" );
182 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
183 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
184 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" );
188 set_holdallowed_rule( $hold_allowed_from_any_libraries );
190 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
191 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
192 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" );
194 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
195 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
196 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" );
202 my $itemtype2 = $builder->build({
203 source => 'Itemtype',
204 value => { notforloan => 0 }
206 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
208 my $hold = $builder->build({
211 itemnumber => $item3->itemnumber,
216 $rule = Koha::IssuingRule->new(
219 itemtype => $itemtype2,
223 reservesallowed => 99,
229 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
230 is( $is, 1, "Item can be held, items in transit are not available" );
233 $schema->storage->txn_rollback;
235 sub set_holdallowed_rule {
236 my ( $holdallowed, $branchcode ) = @_;
237 Koha::CirculationRules->set_rules(
239 branchcode => $branchcode || undef,
240 categorycode => undef,
243 holdallowed => $holdallowed,
244 hold_fulfillment_policy => 'any',
245 returnbranch => 'homebranch',