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 => 5;
21 use t::lib::TestBuilder;
26 use Koha::DateUtils qw( dt_from_string );
28 my $builder = t::lib::TestBuilder->new;
30 subtest 'transfer a non-existant item' => sub {
33 my $library = $builder->build( { source => 'Branch' } );
35 #Transfert on unknown barcode
36 my $item = $builder->build_sample_item();
37 my $badbc = $item->barcode;
40 my ( $dotransfer, $messages ) =
41 C4::Circulation::transferbook( $library->{branchcode}, $badbc );
42 is( $dotransfer, 0, "Can't transfer a bad barcode" );
45 { BadBarcode => $badbc },
46 "We got the expected barcode"
50 #subtest 'UseBranchTransferLimits tests' => sub {
54 subtest 'transfer already at destination' => sub {
57 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
58 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
60 my $patron = $builder->build_object(
62 class => 'Koha::Patrons',
63 value => { branchcode => $library->branchcode }
67 my $item = $builder->build_sample_item(
69 library => $library->branchcode,
73 my ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
74 is( $dotransfer, 0, 'Transfer of reserved item failed with ignore reserves: true' );
77 { 'DestinationEqualsHolding' => 1 },
78 "We got the expected failure message: DestinationEqualsHolding"
81 # We are making sure there is no regression, feel free to change the behavior if needed.
82 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
83 # found will override all other measures that may prevent transfer and force a transfer.
85 branchcode => $item->homebranch,
86 borrowernumber => $patron->borrowernumber,
87 biblionumber => $item->biblionumber,
88 itemnumber => $item->itemnumber,
91 ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
92 is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
93 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
94 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
97 subtest 'transfer an issued item' => sub {
100 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
101 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
103 my $patron = $builder->build_object(
105 class => 'Koha::Patrons',
106 value => { branchcode => $library->branchcode }
110 my $item = $builder->build_sample_item(
112 library => $library->branchcode,
116 my $dt_to = dt_from_string();
117 my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
119 # We are making sure there is no regression, feel free to change the behavior if needed.
120 # * WasReturned does not seem like a variable that should contain a borrowernumber
121 # * Should we return even if the transfer did not happen? (same branches)
122 my ($dotransfer, $messages) = transferbook( $library->branchcode, $item->barcode );
123 is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
126 $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
128 # We are making sure there is no regression, feel free to change the behavior if needed.
129 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
130 # found will override all other measures that may prevent transfer and force a transfer.
132 branchcode => $item->homebranch,
133 borrowernumber => $patron->borrowernumber,
134 biblionumber => $item->biblionumber,
135 itemnumber => $item->itemnumber,
138 ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
139 is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
140 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
141 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
142 is( $messages->{WasReturned}, $patron->borrowernumber, "We got the return info");
145 subtest 'ignore_reserves flag' => sub {
147 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
148 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
150 my $patron = $builder->build_object(
152 class => 'Koha::Patrons',
153 value => { branchcode => $library->branchcode }
157 my $item = $builder->build_sample_item(
159 library => $library->branchcode,
164 branchcode => $item->homebranch,
165 borrowernumber => $patron->borrowernumber,
166 biblionumber => $item->biblionumber,
167 itemnumber => $item->itemnumber,
170 # We are making sure there is no regression, feel free to change the behavior if needed.
171 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
172 # found will override all other measures that may prevent transfer and force a transfer.
173 my ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
174 is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
175 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
176 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
178 my $ignore_reserves = 0;
179 ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode, $ignore_reserves );
180 is( $dotransfer, 1, 'Transfer of reserved item succeeded with ignore reserves: false' );
181 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
182 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
184 $ignore_reserves = 1;
185 ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode, $ignore_reserves );
186 is( $dotransfer, 0, 'Transfer of reserved item failed with ignore reserves: true' );
189 { 'DestinationEqualsHolding' => 1 },
190 "We got the expected failure message: DestinationEqualsHolding"
192 isnt( $messages->{ResFound}->{ResFound}, 'Reserved', "We did not return that we found a reserve");
193 isnt( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We did not return the reserve info");