3c5fd72708dc6e2ef52941ea963a8fe0ec004243
[koha.git] / t / db_dependent / Holds / DisallowHoldIfItemsAvailable.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use C4::Items;
7 use C4::Circulation;
8 use Koha::IssuingRule;
9
10 use Test::More tests => 5;
11
12 use t::lib::TestBuilder;
13 use t::lib::Mocks;
14
15 BEGIN {
16     use_ok('C4::Reserves');
17 }
18
19 my $schema = Koha::Database->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
22
23 my $builder = t::lib::TestBuilder->new;
24
25 my $library1 = $builder->build({
26     source => 'Branch',
27 });
28 my $library2 = $builder->build({
29     source => 'Branch',
30 });
31
32
33 # Now, set a userenv
34 C4::Context->_new_userenv('xxx');
35 C4::Context->set_userenv(0,0,0,'firstname','surname', $library1->{branchcode}, 'Midway Public Library', '', '', '');
36
37 my $bib_title = "Test Title";
38
39 my $borrower1 = $builder->build({
40     source => 'Borrower',
41     value => {
42         branchcode => $library1->{branchcode},
43         dateexpiry => '3000-01-01',
44     }
45 });
46
47 my $borrower2 = $builder->build({
48     source => 'Borrower',
49     value => {
50         branchcode => $library1->{branchcode},
51         dateexpiry => '3000-01-01',
52     }
53 });
54
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};
61
62 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
63
64 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
65   or BAIL_OUT("Cannot find newly created biblio record");
66
67 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
68
69 my $biblioitemnumber =
70   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
71   or BAIL_OUT("Cannot find newly created biblioitems record");
72
73 $dbh->do("
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')
76 ");
77
78 my $itemnumber1 =
79   $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
80   or BAIL_OUT("Cannot find newly created item");
81
82 my $item1 = GetItem( $itemnumber1 );
83
84 $dbh->do("
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')
87 ");
88
89 my $itemnumber2 =
90   $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber ORDER BY itemnumber DESC")
91   or BAIL_OUT("Cannot find newly created item");
92
93 my $item2 = GetItem( $itemnumber2 );
94
95 $dbh->do("DELETE FROM issuingrules");
96 my $rule = Koha::IssuingRule->new(
97     {
98         categorycode => '*',
99         itemtype     => '*',
100         branchcode   => '*',
101         maxissueqty  => 99,
102         issuelength  => 7,
103         lengthunit   => 8,
104         reservesallowed => 99,
105         onshelfholds => 2,
106     }
107 );
108 $rule->store();
109
110 my $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
111 is( $is, 0, "Item cannot be held, 2 items available" );
112
113 my $issue1 = AddIssue( $borrower2, $item1->{barcode} );
114
115 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
116 is( $is, 0, "Item cannot be held, 1 item available" );
117
118 AddIssue( $borrower2, $item2->{barcode} );
119
120 $is = IsAvailableForItemLevelRequest( $item1, $borrower1);
121 is( $is, 1, "Item can be held, no items available" );
122
123 AddReturn( $item1->{barcode} );
124
125 { # Remove the issue for the first patron, and modify the branch for item1
126     subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
127         plan tests => 2;
128
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');|);
133
134         subtest 'Item is available at a different library' => sub {
135             plan tests => 4;
136
137             Koha::Items->find( $item1->{itemnumber} )->set({homebranch => $library_B, holdingbranch => $library_B })->store;
138             $item1 = GetItem( $itemnumber1 );
139             #Scenario is:
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
146
147             {
148                 $sth_delete_rules->execute;
149                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
150
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" );
154
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" );
158             }
159
160             {
161                 $sth_delete_rules->execute;
162                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
163
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" );
167
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" );
171             }
172         };
173
174         subtest 'Item is available at the same library' => sub {
175             plan tests => 4;
176
177             Koha::Items->find( $item1->{itemnumber} )->set({homebranch => $library_A, holdingbranch => $library_A })->store;
178             $item1 = GetItem( $itemnumber1 );
179             #Scenario is:
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?
186
187             {
188                 $sth_delete_rules->execute;
189                 $sth_insert_rule->execute( $hold_allowed_from_home_library );
190
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" );
194
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" );
198             }
199
200             {
201                 $sth_delete_rules->execute;
202                 $sth_insert_rule->execute( $hold_allowed_from_any_libraries );
203
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" );
207
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" );
211             }
212         };
213     };
214 }
215
216 # Cleanup
217 $schema->storage->txn_rollback;