2e2ac0fe74410616e1f89a24d241da733cd7aad6
[srvgit] / 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 $trigger = "Manual";
42     my ( $dotransfer, $messages ) =
43       C4::Circulation::transferbook({
44           from_branch => $item->homebranch,
45           to_branch => $library->{branchcode},
46           barcode => $badbc,
47           trigger => $trigger
48       });
49     is( $dotransfer, 0, "Can't transfer a bad barcode" );
50     is_deeply(
51         $messages,
52         { BadBarcode => $badbc },
53         "We got the expected barcode"
54     );
55 };
56
57 subtest 'field population tests' => sub {
58     plan tests => 6;
59
60     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
61     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
62
63     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
64
65     my $patron = $builder->build_object(
66         {
67             class => 'Koha::Patrons',
68             value => { branchcode => $library->branchcode }
69         }
70     );
71
72     my $item = $builder->build_sample_item(
73         {
74             library => $library->branchcode,
75         }
76     );
77
78     my $trigger = "Manual";
79     my ($dotransfer, $messages ) = transferbook({
80         from_branch => $item->homebranch,
81         to_branch => $library2->branchcode,
82         barcode => $item->barcode,
83         trigger => $trigger
84     });
85     is( $dotransfer, 1, 'Transfer succeeded' );
86     is_deeply(
87         $messages,
88         { 'WasTransfered' => $library2->branchcode },
89         "WasTransfered was set correctly"
90     );
91
92     my $transfers = Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef });
93     is( $transfers->count, 1, 'One transfer created');
94
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');
99 };
100
101 #FIXME:'UseBranchTransferLimits tests missing
102
103 subtest 'transfer already at destination' => sub {
104     plan tests => 5;
105
106     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
107     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
108
109     my $patron = $builder->build_object(
110         {
111             class => 'Koha::Patrons',
112             value => { branchcode => $library->branchcode }
113         }
114     );
115
116     my $item = $builder->build_sample_item(
117         {
118             library => $library->branchcode,
119         }
120     );
121
122     my ($dotransfer, $messages ) = transferbook({
123         from_branch => $item->homebranch,
124         to_branch => $library->branchcode,
125         barcode => $item->barcode,
126         trigger => "Manual"
127     });
128     is( $dotransfer, 0, 'Transfer of item failed when destination equals holding branch' );
129     is_deeply(
130         $messages,
131         { 'DestinationEqualsHolding' => 1 },
132         "We got the expected failure message: DestinationEqualsHolding"
133     );
134
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.
138     AddReserve({
139         branchcode     => $item->homebranch,
140         borrowernumber => $patron->borrowernumber,
141         biblionumber   => $item->biblionumber,
142         itemnumber     => $item->itemnumber,
143     });
144
145     ($dotransfer, $messages ) = transferbook({
146         from_branch => $item->homebranch,
147         to_branch => $library->branchcode,
148         barcode => $item->barcode,
149         trigger => "Manual"
150     });
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");
154 };
155
156 subtest 'transfer an issued item' => sub {
157     plan tests => 5;
158
159     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
160     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
161
162     my $patron = $builder->build_object(
163         {
164             class => 'Koha::Patrons',
165             value => { branchcode => $library->branchcode }
166         }
167     );
168
169     my $item = $builder->build_sample_item(
170         {
171             library => $library->branchcode,
172         }
173     );
174
175     my $dt_to = dt_from_string();
176     my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
177
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,
185         trigger => "Manual"
186     });
187     is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
188
189     # Reset issue
190     $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
191
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.
195     AddReserve({
196         branchcode     => $item->homebranch,
197         borrowernumber => $patron->borrowernumber,
198         biblionumber   => $item->biblionumber,
199         itemnumber     => $item->itemnumber,
200     });
201
202     ($dotransfer, $messages ) = transferbook({
203         from_branch => $item->homebranch,
204         to_branch => $library->branchcode,
205         barcode => $item->barcode,
206         trigger => "Manual"
207     });
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");
212 };
213
214 subtest 'ignore_reserves flag' => sub {
215     plan tests => 9;
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 } );
219
220     my $patron = $builder->build_object(
221         {
222             class => 'Koha::Patrons',
223             value => { branchcode => $library->branchcode }
224         }
225     );
226
227     my $item = $builder->build_sample_item(
228         {
229             library => $library->branchcode,
230         }
231     );
232
233     AddReserve({
234         branchcode     => $item->homebranch,
235         borrowernumber => $patron->borrowernumber,
236         biblionumber   => $item->biblionumber,
237         itemnumber     => $item->itemnumber,
238     });
239
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,
247         trigger => "Manual"
248     });
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");
252
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,
259         trigger => "Manual"
260     });
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");
264
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,
271         trigger => "Manual"
272     });
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");
276 };
277
278 subtest 'transferbook test from branch' => sub {
279     plan tests => 5;
280
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,
288         trigger => "Manual"
289     });
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,
297         trigger => "Manual"
298     });
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');
302
303 };