576894d110b5d3f4d268140a84043ea763ba0fed
[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 => 5;
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( $library->{branchcode}, $badbc );
43     is( $dotransfer, 0, "Can't transfer a bad barcode" );
44     is_deeply(
45         $messages,
46         { BadBarcode => $badbc },
47         "We got the expected barcode"
48     );
49 };
50
51 subtest 'field population tests' => sub {
52     plan tests => 6;
53
54     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
55     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
56
57     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
58
59     my $patron = $builder->build_object(
60         {
61             class => 'Koha::Patrons',
62             value => { branchcode => $library->branchcode }
63         }
64     );
65
66     my $item = $builder->build_sample_item(
67         {
68             library => $library->branchcode,
69         }
70     );
71
72     my $trigger = "Manual";
73     my ($dotransfer, $messages ) = transferbook( $library2->branchcode, $item->barcode, undef, $trigger );
74     is( $dotransfer, 1, 'Transfer succeeded' );
75     is_deeply(
76         $messages,
77         { 'WasTransfered' => 1 },
78         "WasTransfered was set correctly"
79     );
80
81     my $transfers = Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef });
82     is( $transfers->count, 1, 'One transfer created');
83
84     my $transfer = $transfers->next;
85     is ($transfer->frombranch, $library->branchcode, 'frombranch set correctly');
86     is ($transfer->tobranch, $library2->branchcode, 'tobranch set correctly');
87     is ($transfer->reason, $trigger, 'reason set if passed');
88 };
89
90 #FIXME:'UseBranchTransferLimits tests missing
91
92 subtest 'transfer already at destination' => sub {
93     plan tests => 5;
94
95     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
96     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
97
98     my $patron = $builder->build_object(
99         {
100             class => 'Koha::Patrons',
101             value => { branchcode => $library->branchcode }
102         }
103     );
104
105     my $item = $builder->build_sample_item(
106         {
107             library => $library->branchcode,
108         }
109     );
110
111     my ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
112     is( $dotransfer, 0, 'Transfer of item failed when destination equals holding branch' );
113     is_deeply(
114         $messages,
115         { 'DestinationEqualsHolding' => 1 },
116         "We got the expected failure message: DestinationEqualsHolding"
117     );
118
119     # We are making sure there is no regression, feel free to change the behavior if needed.
120     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
121     #   found will override all other measures that may prevent transfer and force a transfer.
122     AddReserve({
123         branchcode     => $item->homebranch,
124         borrowernumber => $patron->borrowernumber,
125         biblionumber   => $item->biblionumber,
126         itemnumber     => $item->itemnumber,
127     });
128
129     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
130     is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
131     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
132     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
133 };
134
135 subtest 'transfer an issued item' => sub {
136     plan tests => 5;
137
138     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
139     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
140
141     my $patron = $builder->build_object(
142         {
143             class => 'Koha::Patrons',
144             value => { branchcode => $library->branchcode }
145         }
146     );
147
148     my $item = $builder->build_sample_item(
149         {
150             library => $library->branchcode,
151         }
152     );
153
154     my $dt_to = dt_from_string();
155     my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
156
157     # We are making sure there is no regression, feel free to change the behavior if needed.
158     # * WasReturned does not seem like a variable that should contain a borrowernumber
159     # * Should we return even if the transfer did not happen? (same branches)
160     my ($dotransfer, $messages) = transferbook( $library->branchcode, $item->barcode );
161     is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
162
163     # Reset issue
164     $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
165
166     # We are making sure there is no regression, feel free to change the behavior if needed.
167     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
168     #   found will override all other measures that may prevent transfer and force a transfer.
169     AddReserve({
170         branchcode     => $item->homebranch,
171         borrowernumber => $patron->borrowernumber,
172         biblionumber   => $item->biblionumber,
173         itemnumber     => $item->itemnumber,
174     });
175
176     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
177     is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
178     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
179     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
180     is( $messages->{WasReturned}, $patron->borrowernumber, "We got the return info");
181 };
182
183 subtest 'ignore_reserves flag' => sub {
184     plan tests => 10;
185     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
186     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
187
188     my $patron = $builder->build_object(
189         {
190             class => 'Koha::Patrons',
191             value => { branchcode => $library->branchcode }
192         }
193     );
194
195     my $item = $builder->build_sample_item(
196         {
197             library => $library->branchcode,
198         }
199     );
200
201     AddReserve({
202         branchcode     => $item->homebranch,
203         borrowernumber => $patron->borrowernumber,
204         biblionumber   => $item->biblionumber,
205         itemnumber     => $item->itemnumber,
206     });
207
208     # We are making sure there is no regression, feel free to change the behavior if needed.
209     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
210     #   found will override all other measures that may prevent transfer and force a transfer.
211     my ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
212     is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
213     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
214     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
215
216     my $ignore_reserves = 0;
217     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode, $ignore_reserves );
218     is( $dotransfer, 1, 'Transfer of reserved item succeeded with ignore reserves: false' );
219     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
220     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
221
222     $ignore_reserves = 1;
223     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode, $ignore_reserves );
224     is( $dotransfer, 0, 'Transfer of reserved item failed with ignore reserves: true' );
225     is_deeply(
226         $messages,
227         { 'DestinationEqualsHolding' => 1 },
228         "We got the expected failure message: DestinationEqualsHolding"
229     );
230     isnt( $messages->{ResFound}->{ResFound}, 'Reserved', "We did not return that we found a reserve");
231     isnt( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We did not return the reserve info");
232 };