61318d5c27b9ec86f1f8467c3a10e8ece13bd427
[koha-ffzg.git] / t / db_dependent / Circulation / transferbook.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 6;
21 use t::lib::TestBuilder;
22 use t::lib::Mocks;
23
24 use C4::Circulation;
25 use C4::Reserves;
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Item::Transfers;
28
29 my $builder = t::lib::TestBuilder->new;
30
31 subtest 'transfer a non-existant item' => sub {
32     plan tests => 2;
33
34     my $library = $builder->build( { source => 'Branch' } );
35
36     #Transfert on unknown barcode
37     my $item  = $builder->build_sample_item();
38     my $badbc = $item->barcode;
39     $item->delete;
40
41     my ( $dotransfer, $messages ) =
42       C4::Circulation::transferbook({
43           from_branch => $item->homebranch,
44           to_branch => $library->{branchcode},
45           barcode => $badbc
46       });
47     is( $dotransfer, 0, "Can't transfer a bad barcode" );
48     is_deeply(
49         $messages,
50         { BadBarcode => $badbc },
51         "We got the expected barcode"
52     );
53 };
54
55 subtest 'field population tests' => sub {
56     plan tests => 6;
57
58     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
59     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
60
61     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
62
63     my $patron = $builder->build_object(
64         {
65             class => 'Koha::Patrons',
66             value => { branchcode => $library->branchcode }
67         }
68     );
69
70     my $item = $builder->build_sample_item(
71         {
72             library => $library->branchcode,
73         }
74     );
75
76     my $trigger = "Manual";
77     my ($dotransfer, $messages ) = transferbook({
78         from_branch => $item->homebranch,
79         to_branch => $library2->branchcode,
80         barcode => $item->barcode,
81         trigger => $trigger
82     });
83     is( $dotransfer, 1, 'Transfer succeeded' );
84     is_deeply(
85         $messages,
86         { 'WasTransfered' => 1 },
87         "WasTransfered was set correctly"
88     );
89
90     my $transfers = Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef });
91     is( $transfers->count, 1, 'One transfer created');
92
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');
97 };
98
99 #FIXME:'UseBranchTransferLimits tests missing
100
101 subtest 'transfer already at destination' => sub {
102     plan tests => 5;
103
104     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
105     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
106
107     my $patron = $builder->build_object(
108         {
109             class => 'Koha::Patrons',
110             value => { branchcode => $library->branchcode }
111         }
112     );
113
114     my $item = $builder->build_sample_item(
115         {
116             library => $library->branchcode,
117         }
118     );
119
120     my ($dotransfer, $messages ) = transferbook({
121         from_branch => $item->homebranch,
122         to_branch => $library->branchcode,
123         barcode => $item->barcode
124     });
125     is( $dotransfer, 0, 'Transfer of item failed when destination equals holding branch' );
126     is_deeply(
127         $messages,
128         { 'DestinationEqualsHolding' => 1 },
129         "We got the expected failure message: DestinationEqualsHolding"
130     );
131
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.
135     AddReserve({
136         branchcode     => $item->homebranch,
137         borrowernumber => $patron->borrowernumber,
138         biblionumber   => $item->biblionumber,
139         itemnumber     => $item->itemnumber,
140     });
141
142     ($dotransfer, $messages ) = transferbook({
143         from_branch => $item->homebranch,
144         to_branch => $library->branchcode,
145         barcode => $item->barcode
146     });
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");
150 };
151
152 subtest 'transfer an issued item' => sub {
153     plan tests => 5;
154
155     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
156     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
157
158     my $patron = $builder->build_object(
159         {
160             class => 'Koha::Patrons',
161             value => { branchcode => $library->branchcode }
162         }
163     );
164
165     my $item = $builder->build_sample_item(
166         {
167             library => $library->branchcode,
168         }
169     );
170
171     my $dt_to = dt_from_string();
172     my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
173
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
181     });
182     is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
183
184     # Reset issue
185     $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
186
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.
190     AddReserve({
191         branchcode     => $item->homebranch,
192         borrowernumber => $patron->borrowernumber,
193         biblionumber   => $item->biblionumber,
194         itemnumber     => $item->itemnumber,
195     });
196
197     ($dotransfer, $messages ) = transferbook({
198         from_branch => $item->homebranch,
199         to_branch => $library->branchcode,
200         barcode => $item->barcode
201     });
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");
206 };
207
208 subtest 'ignore_reserves flag' => sub {
209     plan tests => 10;
210     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
211     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
212
213     my $patron = $builder->build_object(
214         {
215             class => 'Koha::Patrons',
216             value => { branchcode => $library->branchcode }
217         }
218     );
219
220     my $item = $builder->build_sample_item(
221         {
222             library => $library->branchcode,
223         }
224     );
225
226     AddReserve({
227         branchcode     => $item->homebranch,
228         borrowernumber => $patron->borrowernumber,
229         biblionumber   => $item->biblionumber,
230         itemnumber     => $item->itemnumber,
231     });
232
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
240     });
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");
244
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
251     });
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");
255
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
262     });
263     is( $dotransfer, 0, 'Transfer of reserved item failed with ignore reserves: true' );
264     is_deeply(
265         $messages,
266         { 'DestinationEqualsHolding' => 1 },
267         "We got the expected failure message: DestinationEqualsHolding"
268     );
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");
271 };
272
273 subtest 'transferbook test from branch' => sub {
274     plan tests => 5;
275
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,
283     });
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,
291     });
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');
295
296 };