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;
24 use C4::Circulation qw( transferbook AddIssue GetTransfers );
25 use C4::Reserves qw( AddReserve );
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 $trigger = "Manual";
42 my ( $dotransfer, $messages ) =
43 C4::Circulation::transferbook({
44 from_branch => $item->homebranch,
45 to_branch => $library->{branchcode},
49 is( $dotransfer, 0, "Can't transfer a bad barcode" );
52 { BadBarcode => $badbc },
53 "We got the expected barcode"
57 subtest 'field population tests' => sub {
60 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
61 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
63 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
65 my $patron = $builder->build_object(
67 class => 'Koha::Patrons',
68 value => { branchcode => $library->branchcode }
72 my $item = $builder->build_sample_item(
74 library => $library->branchcode,
78 my $trigger = "Manual";
79 my ($dotransfer, $messages ) = transferbook({
80 from_branch => $item->homebranch,
81 to_branch => $library2->branchcode,
82 barcode => $item->barcode,
85 is( $dotransfer, 1, 'Transfer succeeded' );
88 { 'WasTransfered' => $library2->branchcode },
89 "WasTransfered was set correctly"
92 my $transfers = Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef });
93 is( $transfers->count, 1, 'One transfer created');
95 my $transfer = $transfers->next;
96 is ($transfer->frombranch, $library->branchcode, 'frombranch set correctly');
97 is ($transfer->tobranch, $library2->branchcode, 'tobranch set correctly');
98 is ($transfer->reason, $trigger, 'reason set if passed');
101 #FIXME:'UseBranchTransferLimits tests missing
103 subtest 'transfer already at destination' => sub {
106 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
107 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
109 my $patron = $builder->build_object(
111 class => 'Koha::Patrons',
112 value => { branchcode => $library->branchcode }
116 my $item = $builder->build_sample_item(
118 library => $library->branchcode,
122 my ($dotransfer, $messages ) = transferbook({
123 from_branch => $item->homebranch,
124 to_branch => $library->branchcode,
125 barcode => $item->barcode,
128 is( $dotransfer, 0, 'Transfer of item failed when destination equals holding branch' );
131 { 'DestinationEqualsHolding' => 1 },
132 "We got the expected failure message: DestinationEqualsHolding"
135 # We are making sure there is no regression, feel free to change the behavior if needed.
136 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
137 # found will override all other measures that may prevent transfer and force a transfer.
139 branchcode => $item->homebranch,
140 borrowernumber => $patron->borrowernumber,
141 biblionumber => $item->biblionumber,
142 itemnumber => $item->itemnumber,
145 ($dotransfer, $messages ) = transferbook({
146 from_branch => $item->homebranch,
147 to_branch => $library->branchcode,
148 barcode => $item->barcode,
151 is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
152 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
153 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
156 subtest 'transfer an issued item' => sub {
159 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
160 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
162 my $patron = $builder->build_object(
164 class => 'Koha::Patrons',
165 value => { branchcode => $library->branchcode }
169 my $item = $builder->build_sample_item(
171 library => $library->branchcode,
175 my $dt_to = dt_from_string();
176 my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
178 # We are making sure there is no regression, feel free to change the behavior if needed.
179 # * WasReturned does not seem like a variable that should contain a borrowernumber
180 # * Should we return even if the transfer did not happen? (same branches)
181 my ($dotransfer, $messages) = transferbook({
182 from_branch => $item->homebranch,
183 to_branch => $library->branchcode,
184 barcode => $item->barcode,
187 is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
190 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
192 # We are making sure there is no regression, feel free to change the behavior if needed.
193 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
194 # found will override all other measures that may prevent transfer and force a transfer.
196 branchcode => $item->homebranch,
197 borrowernumber => $patron->borrowernumber,
198 biblionumber => $item->biblionumber,
199 itemnumber => $item->itemnumber,
202 ($dotransfer, $messages ) = transferbook({
203 from_branch => $item->homebranch,
204 to_branch => $library->branchcode,
205 barcode => $item->barcode,
208 is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
209 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
210 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
211 is( $messages->{WasReturned}, $patron->borrowernumber, "We got the return info");
214 subtest 'ignore_reserves flag' => sub {
216 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
217 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
218 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
220 my $patron = $builder->build_object(
222 class => 'Koha::Patrons',
223 value => { branchcode => $library->branchcode }
227 my $item = $builder->build_sample_item(
229 library => $library->branchcode,
234 branchcode => $item->homebranch,
235 borrowernumber => $patron->borrowernumber,
236 biblionumber => $item->biblionumber,
237 itemnumber => $item->itemnumber,
240 # We are making sure there is no regression, feel free to change the behavior if needed.
241 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
242 # found will override all other measures that may prevent transfer and force a transfer.
243 my ($dotransfer, $messages ) = transferbook({
244 from_branch => $item->homebranch,
245 to_branch => $library2->branchcode,
246 barcode => $item->barcode,
249 is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
250 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
251 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
253 my $ignore_reserves = 0;
254 ($dotransfer, $messages ) = transferbook({
255 from_branch => $item->homebranch,
256 to_branch => $library2->branchcode,
257 barcode => $item->barcode,
258 ignore_reserves => $ignore_reserves,
261 is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
262 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
263 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
265 $ignore_reserves = 1;
266 ($dotransfer, $messages ) = transferbook({
267 from_branch => $item->homebranch,
268 to_branch => $library2->branchcode,
269 barcode => $item->barcode,
270 ignore_reserves => $ignore_reserves,
273 is( $dotransfer, 1, 'Transfer of reserved item succeed with ignore reserves: true' );
274 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
275 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
278 subtest 'transferbook test from branch' => sub {
281 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
282 my $item = $builder->build_sample_item();
283 ok( $item->holdingbranch ne $library->branchcode && $item->homebranch ne $library->branchcode, "Item is not held or owned by library");
284 C4::Circulation::transferbook({
285 from_branch => $library->branchcode,
286 to_branch => $item->homebranch,
287 barcode => $item->barcode,
290 my ($datesent,$from_branch,$to_branch) = GetTransfers($item->itemnumber);
291 is( $from_branch, $library->branchcode, 'The transfer is initiated from the specified branch, not the items home or holdingbranch');
292 is( $to_branch, $item->homebranch, 'The transfer is initiated to the specified branch');
293 C4::Circulation::transferbook({
294 from_branch => $item->homebranch,
295 to_branch => $library->branchcode,
296 barcode => $item->barcode,
299 ($datesent,$from_branch,$to_branch) = GetTransfers($item->itemnumber);
300 is( $from_branch, $item->homebranch, 'The transfer is initiated from the specified branch');
301 is( $to_branch, $library->branchcode, 'The transfer is initiated to the specified branch');