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;
117 my $sth_delete_rules = $dbh->prepare(q|DELETE FROM default_circ_rules|);
118 my $sth_insert_rule = $dbh->prepare(q|INSERT INTO default_circ_rules(singleton, holdallowed, hold_fulfillment_policy, returnbranch) VALUES ('singleton', ?, 'any', 'homebranch');|);
119 my $sth_insert_branch_rule = $dbh->prepare(q|INSERT INTO default_branch_circ_rules(branchcode, holdallowed, hold_fulfillment_policy, returnbranch) VALUES (?, ?, 'any', 'homebranch');|);
121 subtest 'Item is available at a different library' => sub {
124 $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
126 #One shelf holds is 'If all unavailable'/2
127 #Item 1 homebranch library B is available
128 #Item 2 homebranch library A is checked out
129 #Borrower1 is from library A
132 $sth_delete_rules->execute;
133 $sth_insert_rule->execute( $hold_allowed_from_home_library );
135 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
136 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
137 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" );
138 $is = IsAvailableForItemLevelRequest( $item1, $patron2);
139 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" );
140 $sth_insert_branch_rule->execute( $library_B, $hold_allowed_from_any_libraries );
141 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
142 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
143 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" );
145 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
146 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
147 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" );
148 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
149 $sth_insert_branch_rule->execute( $library_A, $hold_allowed_from_any_libraries );
150 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
151 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" );
155 $sth_delete_rules->execute;
156 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
158 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
159 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
160 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" );
162 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
163 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
164 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" );
168 subtest 'Item is available at the same library' => sub {
171 $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
173 #One shelf holds is 'If all unavailable'/2
174 #Item 1 homebranch library A is available
175 #Item 2 homebranch library A is checked out
176 #Borrower1 is from library A
177 #CircControl has no effect - same rule for all branches as set at line 96
178 #ReservesControlBranch is not checked in these subs we are testing?
181 $sth_delete_rules->execute;
182 $sth_insert_rule->execute( $hold_allowed_from_home_library );
184 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
185 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
186 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" );
188 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
189 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
190 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" );
194 $sth_delete_rules->execute;
195 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
197 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
198 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
199 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" );
201 t::lib::Mocks::mock_preference('ReservesControlBranch', 'PatronLibrary');
202 $is = IsAvailableForItemLevelRequest( $item1, $patron1);
203 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" );
209 my $itemtype2 = $builder->build({
210 source => 'Itemtype',
211 value => { notforloan => 0 }
213 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
215 my $hold = $builder->build({
218 itemnumber => $item3->itemnumber,
223 $rule = Koha::IssuingRule->new(
226 itemtype => $itemtype2,
230 reservesallowed => 99,
236 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
237 is( $is, 1, "Item can be held, items in transit are not available" );
240 $schema->storage->txn_rollback;