3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 6;
21 use t::lib::TestBuilder;
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Item::Transfers;
29 my $builder = t::lib::TestBuilder->new;
31 subtest 'transfer a non-existant item' => sub {
34 my $library = $builder->build( { source => 'Branch' } );
36 #Transfert on unknown barcode
37 my $item = $builder->build_sample_item();
38 my $badbc = $item->barcode;
41 my ( $dotransfer, $messages ) =
42 C4::Circulation::transferbook({
43 from_branch => $item->homebranch,
44 to_branch => $library->{branchcode},
47 is( $dotransfer, 0, "Can't transfer a bad barcode" );
50 { BadBarcode => $badbc },
51 "We got the expected barcode"
55 subtest 'field population tests' => sub {
58 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
59 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
61 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
63 my $patron = $builder->build_object(
65 class => 'Koha::Patrons',
66 value => { branchcode => $library->branchcode }
70 my $item = $builder->build_sample_item(
72 library => $library->branchcode,
76 my $trigger = "Manual";
77 my ($dotransfer, $messages ) = transferbook({
78 from_branch => $item->homebranch,
79 to_branch => $library2->branchcode,
80 barcode => $item->barcode,
83 is( $dotransfer, 1, 'Transfer succeeded' );
86 { 'WasTransfered' => 1 },
87 "WasTransfered was set correctly"
90 my $transfers = Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef });
91 is( $transfers->count, 1, 'One transfer created');
93 my $transfer = $transfers->next;
94 is ($transfer->frombranch, $library->branchcode, 'frombranch set correctly');
95 is ($transfer->tobranch, $library2->branchcode, 'tobranch set correctly');
96 is ($transfer->reason, $trigger, 'reason set if passed');
99 #FIXME:'UseBranchTransferLimits tests missing
101 subtest 'transfer already at destination' => sub {
104 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
105 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
107 my $patron = $builder->build_object(
109 class => 'Koha::Patrons',
110 value => { branchcode => $library->branchcode }
114 my $item = $builder->build_sample_item(
116 library => $library->branchcode,
120 my ($dotransfer, $messages ) = transferbook({
121 from_branch => $item->homebranch,
122 to_branch => $library->branchcode,
123 barcode => $item->barcode
125 is( $dotransfer, 0, 'Transfer of item failed when destination equals holding branch' );
128 { 'DestinationEqualsHolding' => 1 },
129 "We got the expected failure message: DestinationEqualsHolding"
132 # We are making sure there is no regression, feel free to change the behavior if needed.
133 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
134 # found will override all other measures that may prevent transfer and force a transfer.
136 branchcode => $item->homebranch,
137 borrowernumber => $patron->borrowernumber,
138 biblionumber => $item->biblionumber,
139 itemnumber => $item->itemnumber,
142 ($dotransfer, $messages ) = transferbook({
143 from_branch => $item->homebranch,
144 to_branch => $library->branchcode,
145 barcode => $item->barcode
147 is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
148 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
149 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
152 subtest 'transfer an issued item' => sub {
155 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
156 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
158 my $patron = $builder->build_object(
160 class => 'Koha::Patrons',
161 value => { branchcode => $library->branchcode }
165 my $item = $builder->build_sample_item(
167 library => $library->branchcode,
171 my $dt_to = dt_from_string();
172 my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
174 # We are making sure there is no regression, feel free to change the behavior if needed.
175 # * WasReturned does not seem like a variable that should contain a borrowernumber
176 # * Should we return even if the transfer did not happen? (same branches)
177 my ($dotransfer, $messages) = transferbook({
178 from_branch => $item->homebranch,
179 to_branch => $library->branchcode,
180 barcode => $item->barcode
182 is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
185 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
187 # We are making sure there is no regression, feel free to change the behavior if needed.
188 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
189 # found will override all other measures that may prevent transfer and force a transfer.
191 branchcode => $item->homebranch,
192 borrowernumber => $patron->borrowernumber,
193 biblionumber => $item->biblionumber,
194 itemnumber => $item->itemnumber,
197 ($dotransfer, $messages ) = transferbook({
198 from_branch => $item->homebranch,
199 to_branch => $library->branchcode,
200 barcode => $item->barcode
202 is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
203 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
204 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
205 is( $messages->{WasReturned}, $patron->borrowernumber, "We got the return info");
208 subtest 'ignore_reserves flag' => sub {
210 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
211 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
213 my $patron = $builder->build_object(
215 class => 'Koha::Patrons',
216 value => { branchcode => $library->branchcode }
220 my $item = $builder->build_sample_item(
222 library => $library->branchcode,
227 branchcode => $item->homebranch,
228 borrowernumber => $patron->borrowernumber,
229 biblionumber => $item->biblionumber,
230 itemnumber => $item->itemnumber,
233 # We are making sure there is no regression, feel free to change the behavior if needed.
234 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
235 # found will override all other measures that may prevent transfer and force a transfer.
236 my ($dotransfer, $messages ) = transferbook({
237 from_branch => $item->homebranch,
238 to_branch => $library->branchcode,
239 barcode => $item->barcode
241 is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
242 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
243 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
245 my $ignore_reserves = 0;
246 ($dotransfer, $messages ) = transferbook({
247 from_branch => $item->homebranch,
248 to_branch => $library->branchcode,
249 barcode => $item->barcode,
250 ignore_reserves => $ignore_reserves
252 is( $dotransfer, 1, 'Transfer of reserved item succeeded with ignore reserves: false' );
253 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
254 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
256 $ignore_reserves = 1;
257 ($dotransfer, $messages ) = transferbook({
258 from_branch => $item->homebranch,
259 to_branch => $library->branchcode,
260 barcode => $item->barcode,
261 ignore_reserves => $ignore_reserves
263 is( $dotransfer, 0, 'Transfer of reserved item failed with ignore reserves: true' );
266 { 'DestinationEqualsHolding' => 1 },
267 "We got the expected failure message: DestinationEqualsHolding"
269 isnt( $messages->{ResFound}->{ResFound}, 'Reserved', "We did not return that we found a reserve");
270 isnt( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We did not return the reserve info");
273 subtest 'transferbook test from branch' => sub {
276 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
277 my $item = $builder->build_sample_item();
278 ok( $item->holdingbranch ne $library->branchcode && $item->homebranch ne $library->branchcode, "Item is not held or owned by library");
279 C4::Circulation::transferbook({
280 from_branch => $library->branchcode,
281 to_branch => $item->homebranch,
282 barcode => $item->barcode,
284 my ($datesent,$from_branch,$to_branch) = GetTransfers($item->itemnumber);
285 is( $from_branch, $library->branchcode, 'The transfer is initiated from the specified branch, not the items home or holdingbranch');
286 is( $to_branch, $item->homebranch, 'The transfer is initiated to the specified branch');
287 C4::Circulation::transferbook({
288 from_branch => $item->homebranch,
289 to_branch => $library->branchcode,
290 barcode => $item->barcode,
292 ($datesent,$from_branch,$to_branch) = GetTransfers($item->itemnumber);
293 is( $from_branch, $item->homebranch, 'The transfer is initiated from the specified branch');
294 is( $to_branch, $library->branchcode, 'The transfer is initiated to the specified branch');