Bug 18309: Add UNIMARC field 214 and its subfields
[koha.git] / t / db_dependent / Holds / DisallowHoldIfItemsAvailable.t
blob755721d824d0620a56b5274a2cda061cd793aefe
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use C4::Context;
6 use C4::Circulation;
7 use C4::Items;
8 use Koha::IssuingRule;
9 use Koha::Items;
10 use Test::More tests => 6;
12 use t::lib::TestBuilder;
13 use t::lib::Mocks;
15 BEGIN {
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({
26 source => 'Branch',
27 });
28 my $library2 = $builder->build({
29 source => 'Branch',
30 });
31 my $itemtype = $builder->build({
32 source => 'Itemtype',
33 value => { notforloan => 0 }
34 })->{itemtype};
36 t::lib::Mocks::mock_userenv({ branchcode => $library1->{branchcode} });
39 my $patron1 = $builder->build_object({
40 class => 'Koha::Patrons',
41 value => {
42 branchcode => $library1->{branchcode},
43 dateexpiry => '3000-01-01',
45 });
46 my $borrower1 = $patron1->unblessed;
48 my $patron2 = $builder->build_object({
49 class => 'Koha::Patrons',
50 value => {
51 branchcode => $library1->{branchcode},
52 dateexpiry => '3000-01-01',
54 });
56 my $patron3 = $builder->build_object({
57 class => 'Koha::Patrons',
58 value => {
59 branchcode => $library2->{branchcode},
60 dateexpiry => '3000-01-01',
62 });
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,
71 itype=>$itemtype,
72 homebranch => $library_A,
73 holdingbranch => $library_A
74 });
75 my $item2 = $builder->build_sample_item({
76 biblionumber=>$biblionumber,
77 itype=>$itemtype,
78 homebranch => $library_A,
79 holdingbranch => $library_A
80 });
82 # Test hold_fulfillment_policy
83 my $rule = Koha::IssuingRule->new(
85 categorycode => '*',
86 itemtype => $itemtype,
87 branchcode => '*',
88 issuelength => 7,
89 lengthunit => 8,
90 reservesallowed => 99,
91 onshelfholds => 2,
94 $rule->store();
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 {
113 plan tests => 2;
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 {
119 plan tests => 7;
121 $item1->set({homebranch => $library_B, holdingbranch => $library_B })->store;
122 #Scenario is:
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 {
164 plan tests => 4;
166 $item1->set({homebranch => $library_A, holdingbranch => $library_A })->store;
167 #Scenario is:
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 }
205 })->{itemtype};
206 my $item3 = $builder->build_sample_item({ itype => $itemtype2 });
208 my $hold = $builder->build({
209 source => 'Reserve',
210 value =>{
211 itemnumber => $item3->itemnumber,
212 found => 'T'
216 $rule = Koha::IssuingRule->new(
218 categorycode => '*',
219 itemtype => $itemtype2,
220 branchcode => '*',
221 issuelength => 7,
222 lengthunit => 8,
223 reservesallowed => 99,
224 onshelfholds => 0,
227 $rule->store();
229 $is = IsAvailableForItemLevelRequest( $item3, $patron1);
230 is( $is, 1, "Item can be held, items in transit are not available" );
232 # Cleanup
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,
241 itemtype => undef,
242 rules => {
243 holdallowed => $holdallowed,
244 hold_fulfillment_policy => 'any',
245 returnbranch => 'homebranch',