Bug 33237: (QA follow-up) Clarify tests and Mock
[koha.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 qw( transferbook AddIssue );
25 use C4::Reserves qw( AddReserve );
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Item::Transfers;
28
29 my $builder = t::lib::TestBuilder->new;
30 my $schema = Koha::Database->new->schema;
31
32 $schema->storage->txn_begin;
33
34 subtest 'transfer a non-existant item' => sub {
35     plan tests => 2;
36
37     my $library = $builder->build( { source => 'Branch' } );
38
39     #Transfert on unknown barcode
40     my $item  = $builder->build_sample_item();
41     my $badbc = $item->barcode;
42     $item->delete;
43
44     my $trigger = "Manual";
45     my ( $dotransfer, $messages ) =
46       C4::Circulation::transferbook({
47           from_branch => $item->homebranch,
48           to_branch => $library->{branchcode},
49           barcode => $badbc,
50           trigger => $trigger
51       });
52     is( $dotransfer, 0, "Can't transfer a bad barcode" );
53     is_deeply(
54         $messages,
55         { BadBarcode => $badbc },
56         "We got the expected barcode"
57     );
58 };
59
60 subtest 'field population tests' => sub {
61     plan tests => 6;
62
63     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
64     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
65
66     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
67
68     my $patron = $builder->build_object(
69         {
70             class => 'Koha::Patrons',
71             value => { branchcode => $library->branchcode }
72         }
73     );
74
75     my $item = $builder->build_sample_item(
76         {
77             library => $library->branchcode,
78         }
79     );
80
81     my $trigger = "Manual";
82     my ($dotransfer, $messages ) = transferbook({
83         from_branch => $item->homebranch,
84         to_branch => $library2->branchcode,
85         barcode => $item->barcode,
86         trigger => $trigger
87     });
88     is( $dotransfer, 1, 'Transfer succeeded' );
89     is_deeply(
90         $messages,
91         { 'WasTransfered' => $library2->branchcode },
92         "WasTransfered was set correctly"
93     );
94
95     my $transfers = Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef });
96     is( $transfers->count, 1, 'One transfer created');
97
98     my $transfer = $transfers->next;
99     is ($transfer->frombranch, $library->branchcode, 'frombranch set correctly');
100     is ($transfer->tobranch, $library2->branchcode, 'tobranch set correctly');
101     is ($transfer->reason, $trigger, 'reason set if passed');
102 };
103
104 #FIXME:'UseBranchTransferLimits tests missing
105
106 subtest 'transfer already at destination' => sub {
107     plan tests => 9;
108
109     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
110     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
111
112     my $patron = $builder->build_object(
113         {
114             class => 'Koha::Patrons',
115             value => { branchcode => $library->branchcode }
116         }
117     );
118
119     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' })->store->itemtype;
120     my $item = $builder->build_sample_item(
121         {
122             library => $library->branchcode,
123             itype => $itemtype
124         }
125     );
126
127     my ($dotransfer, $messages ) = transferbook({
128         from_branch => $item->homebranch,
129         to_branch => $library->branchcode,
130         barcode => $item->barcode,
131         trigger => "Manual"
132     });
133     is( $dotransfer, 0, 'Transfer of item failed when destination equals holding branch' );
134     is_deeply(
135         $messages,
136         { 'DestinationEqualsHolding' => 1 },
137         "We got the expected failure message: DestinationEqualsHolding"
138     );
139
140     # We are making sure there is no regression, feel free to change the behavior if needed.
141     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
142     #   found will override all other measures that may prevent transfer and force a transfer.
143     AddReserve({
144         branchcode     => $item->homebranch,
145         borrowernumber => $patron->borrowernumber,
146         biblionumber   => $item->biblionumber,
147         itemnumber     => $item->itemnumber,
148     });
149
150     ($dotransfer, $messages ) = transferbook({
151         from_branch => $item->homebranch,
152         to_branch => $library->branchcode,
153         barcode => $item->barcode,
154         trigger => "Manual"
155     });
156     is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
157     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
158     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
159
160     # recalls
161     t::lib::Mocks::mock_preference('UseRecalls', 1);
162     my $recall = Koha::Recall->new(
163         {   biblio_id         => $item->biblionumber,
164             item_id           => $item->itemnumber,
165             item_level        => 1,
166             patron_id         => $patron->borrowernumber,
167             pickup_library_id => $library->branchcode,
168         }
169     )->store;
170     ( $recall, $dotransfer, $messages ) = $recall->start_transfer;
171     is( $dotransfer, 0, 'Do not transfer recalled item, it has already arrived' );
172     is( $messages->{RecallPlacedAtHoldingBranch}, 1, "We found the recall");
173
174     $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' })->store->itemtype;
175     my $item2 = $builder->build_object({ class => 'Koha::Items', value => { itype => $itemtype } }); # this item will have a different holding branch to the pickup branch
176     $recall = Koha::Recall->new(
177         {   biblio_id         => $item2->biblionumber,
178             item_id           => $item2->itemnumber,
179             item_level        => 1,
180             patron_id         => $patron->borrowernumber,
181             pickup_library_id => $library->branchcode,
182         }
183     )->store;
184     ( $recall, $dotransfer, $messages ) = $recall->start_transfer;
185     is( $dotransfer, 1, 'Transfer of recalled item succeeded' );
186     is( $messages->{RecallFound}->id, $recall->id, "We found the recall");
187 };
188
189 subtest 'transfer an issued item' => sub {
190     plan tests => 5;
191
192     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
193     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
194
195     my $patron = $builder->build_object(
196         {
197             class => 'Koha::Patrons',
198             value => { branchcode => $library->branchcode }
199         }
200     );
201
202     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' })->store->itemtype;
203     my $item = $builder->build_sample_item(
204         {
205             library => $library->branchcode,
206             itype => $itemtype
207         }
208     );
209
210     my $dt_to = dt_from_string();
211     my $issue = AddIssue( $patron, $item->barcode, $dt_to );
212
213     # We are making sure there is no regression, feel free to change the behavior if needed.
214     # * WasReturned does not seem like a variable that should contain a borrowernumber
215     # * Should we return even if the transfer did not happen? (same branches)
216     my ($dotransfer, $messages) = transferbook({
217         from_branch => $item->homebranch,
218         to_branch => $library->branchcode,
219         barcode => $item->barcode,
220         trigger => "Manual"
221     });
222     is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
223
224     # Reset issue
225     $issue = AddIssue( $patron, $item->barcode, $dt_to );
226
227     # We are making sure there is no regression, feel free to change the behavior if needed.
228     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
229     #   found will override all other measures that may prevent transfer and force a transfer.
230     AddReserve({
231         branchcode     => $item->homebranch,
232         borrowernumber => $patron->borrowernumber,
233         biblionumber   => $item->biblionumber,
234         itemnumber     => $item->itemnumber,
235     });
236
237     ($dotransfer, $messages ) = transferbook({
238         from_branch => $item->homebranch,
239         to_branch => $library->branchcode,
240         barcode => $item->barcode,
241         trigger => "Manual"
242     });
243     is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
244     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
245     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
246     is( $messages->{WasReturned}, $patron->borrowernumber, "We got the return info");
247 };
248
249 subtest 'ignore_reserves flag' => sub {
250     plan tests => 9;
251     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
252     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
253     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
254
255     my $patron = $builder->build_object(
256         {
257             class => 'Koha::Patrons',
258             value => { branchcode => $library->branchcode }
259         }
260     );
261
262     my $item = $builder->build_sample_item(
263         {
264             library => $library->branchcode,
265         }
266     );
267
268     AddReserve({
269         branchcode     => $item->homebranch,
270         borrowernumber => $patron->borrowernumber,
271         biblionumber   => $item->biblionumber,
272         itemnumber     => $item->itemnumber,
273     });
274
275     # We are making sure there is no regression, feel free to change the behavior if needed.
276     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
277     #   found will override all other measures that may prevent transfer and force a transfer.
278     my ($dotransfer, $messages ) = transferbook({
279         from_branch => $item->homebranch,
280         to_branch => $library2->branchcode,
281         barcode => $item->barcode,
282         trigger => "Manual"
283     });
284     is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
285     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
286     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
287
288     my $ignore_reserves = 0;
289     ($dotransfer, $messages ) = transferbook({
290         from_branch => $item->homebranch,
291         to_branch => $library2->branchcode,
292         barcode => $item->barcode,
293         ignore_reserves => $ignore_reserves,
294         trigger => "Manual"
295     });
296     is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
297     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
298     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
299
300     $ignore_reserves = 1;
301     ($dotransfer, $messages ) = transferbook({
302         from_branch => $item->homebranch,
303         to_branch => $library2->branchcode,
304         barcode => $item->barcode,
305         ignore_reserves => $ignore_reserves,
306         trigger => "Manual"
307     });
308     is( $dotransfer, 1, 'Transfer of reserved item succeed with ignore reserves: true' );
309     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
310     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
311 };
312
313 subtest 'transferbook test from branch' => sub {
314     plan tests => 5;
315
316     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
317     my $item = $builder->build_sample_item();
318     ok( $item->holdingbranch ne $library->branchcode && $item->homebranch ne $library->branchcode, "Item is not held or owned by library");
319     C4::Circulation::transferbook({
320         from_branch => $library->branchcode,
321         to_branch => $item->homebranch,
322         barcode   => $item->barcode,
323         trigger => "Manual"
324     });
325     my $transfer = $item->get_transfer;
326     is( $transfer->frombranch, $library->branchcode, 'The transfer is initiated from the specified branch, not the items home or holdingbranch');
327     is( $transfer->tobranch, $item->homebranch, 'The transfer is initiated to the specified branch');
328     C4::Circulation::transferbook({
329         from_branch => $item->homebranch,
330         to_branch => $library->branchcode,
331         barcode   => $item->barcode,
332         trigger => "Manual"
333     });
334     $transfer = $item->get_transfer;
335     is( $transfer->frombranch, $item->homebranch, 'The transfer is initiated from the specified branch');
336     is( $transfer->tobranch, $library->branchcode, 'The transfer is initiated to the specified branch');
337
338 };
339 $schema->storage->txn_rollback;