Bug 23049: Update existing code to use debit_type
[koha.git] / t / db_dependent / Holds / HoldFulfillmentPolicy.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use Koha::CirculationRules;
7
8 use Test::More tests => 11;
9
10 use t::lib::TestBuilder;
11 use t::lib::Mocks;
12 use Koha::Holds;
13
14 BEGIN {
15     use_ok('C4::Reserves');
16 }
17
18 my $schema = Koha::Database->schema;
19 $schema->storage->txn_begin;
20 my $dbh = C4::Context->dbh;
21
22 my $builder = t::lib::TestBuilder->new;
23
24 my $library1 = $builder->build({
25     source => 'Branch',
26 });
27 my $library2 = $builder->build({
28     source => 'Branch',
29 });
30 my $library3 = $builder->build({
31     source => 'Branch',
32 });
33 my $itemtype = $builder->build({ source => 'Item' })->{itype};
34
35 my $bib_title = "Test Title";
36
37 my $borrower = $builder->build({
38     source => 'Borrower',
39     value => {
40         branchcode => $library1->{branchcode},
41     }
42 });
43
44 # Test hold_fulfillment_policy
45 my $borrowernumber = $borrower->{borrowernumber};
46 my $library_A = $library1->{branchcode};
47 my $library_B = $library2->{branchcode};
48 my $library_C = $library3->{branchcode};
49 $dbh->do("DELETE FROM transport_cost");
50 $dbh->do("DELETE FROM tmp_holdsqueue");
51 $dbh->do("DELETE FROM hold_fill_targets");
52 $dbh->do("DELETE FROM circulation_rules");
53
54 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
55
56 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
57   or BAIL_OUT("Cannot find newly created biblio record");
58
59 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
60
61 my $biblioitemnumber =
62   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
63   or BAIL_OUT("Cannot find newly created biblioitems record");
64
65 $dbh->do("
66     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
67     VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
68 ");
69
70 my $itemnumber =
71   $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
72   or BAIL_OUT("Cannot find newly created item");
73
74 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
75 $dbh->do("DELETE FROM circulation_rules");
76 Koha::CirculationRules->set_rules(
77     {
78         categorycode => undef,
79         branchcode   => undef,
80         itemtype     => undef,
81         rules        => {
82             holdallowed             => 2,
83             hold_fulfillment_policy => 'homebranch',
84         }
85     }
86 );
87
88 # Home branch matches pickup branch
89 my $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
90 my ( $status ) = CheckReserves($itemnumber);
91 is( $status, 'Reserved', "Hold where pickup branch matches home branch targeted" );
92 Koha::Holds->find( $reserve_id )->cancel;
93
94 # Holding branch matches pickup branch
95 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
96 ( $status ) = CheckReserves($itemnumber);
97 is($status, q{}, "Hold where pickup ne home, pickup eq home not targeted" );
98 Koha::Holds->find( $reserve_id )->cancel;
99
100 # Neither branch matches pickup branch
101 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
102 ( $status ) = CheckReserves($itemnumber);
103 is( $status, q{}, "Hold where pickup ne home, pickup ne holding not targeted" );
104 Koha::Holds->find( $reserve_id )->cancel;
105
106 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
107 $dbh->do("DELETE FROM circulation_rules");
108 Koha::CirculationRules->set_rules(
109     {
110         categorycode => undef,
111         branchcode   => undef,
112         itemtype     => undef,
113         rules        => {
114             holdallowed             => 2,
115             hold_fulfillment_policy => 'holdingbranch',
116         }
117     }
118 );
119
120 # Home branch matches pickup branch
121 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
122 ( $status ) = CheckReserves($itemnumber);
123 is( $status, q{}, "Hold where pickup eq home, pickup ne holding not targeted" );
124 Koha::Holds->find( $reserve_id )->cancel;
125
126 # Holding branch matches pickup branch
127 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
128 ( $status ) = CheckReserves($itemnumber);
129 is( $status, 'Reserved', "Hold where pickup ne home, pickup eq holding targeted" );
130 Koha::Holds->find( $reserve_id )->cancel;
131
132 # Neither branch matches pickup branch
133 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
134 ( $status ) = CheckReserves($itemnumber);
135 is( $status, q{}, "Hold where pickup ne home, pickup ne holding not targeted" );
136 Koha::Holds->find( $reserve_id )->cancel;
137
138 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
139 $dbh->do("DELETE FROM circulation_rules");
140 Koha::CirculationRules->set_rules(
141     {
142         categorycode => undef,
143         branchcode   => undef,
144         itemtype     => undef,
145         rules        => {
146             holdallowed             => 2,
147             hold_fulfillment_policy => 'any',
148         }
149     }
150 );
151
152 # Home branch matches pickup branch
153 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
154 ( $status ) = CheckReserves($itemnumber);
155 is( $status, 'Reserved', "Hold where pickup eq home, pickup ne holding targeted" );
156 Koha::Holds->find( $reserve_id )->cancel;
157
158 # Holding branch matches pickup branch
159 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
160 ( $status ) = CheckReserves($itemnumber);
161 is( $status, 'Reserved', "Hold where pickup ne home, pickup eq holding targeted" );
162 Koha::Holds->find( $reserve_id )->cancel;
163
164 # Neither branch matches pickup branch
165 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
166 ( $status ) = CheckReserves($itemnumber);
167 is( $status, 'Reserved', "Hold where pickup ne home, pickup ne holding targeted" );
168 Koha::Holds->find( $reserve_id )->cancel;
169
170 # Test enforement of branch transfer limits
171 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits',  '1' );
172 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
173 Koha::Holds->search()->delete();
174 my ($item) = Koha::Biblios->find($biblionumber)->items->as_list;
175 my $limit = Koha::Item::Transfer::Limit->new(
176     {
177         toBranch   => $library_C,
178         fromBranch => $item->holdingbranch,
179         itemtype   => $item->effective_itemtype,
180     }
181 )->store();
182 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
183 ($status) = CheckReserves($itemnumber);
184 is( $status, '',  "No hold where branch transfer is not allowed" );
185 Koha::Holds->find($reserve_id)->cancel;