]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Circulation/transferbook.t
Bug 24901: Add tests for transferbook
[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 => 4;
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
28 my $builder = t::lib::TestBuilder->new;
29
30 subtest 'transfer a non-existant item' => sub {
31     plan tests => 2;
32
33     my $library = $builder->build( { source => 'Branch' } );
34
35     #Transfert on unknown barcode
36     my $item  = $builder->build_sample_item();
37     my $badbc = $item->barcode;
38     $item->delete;
39
40     my ( $dotransfer, $messages ) =
41       C4::Circulation::transferbook( $library->{branchcode}, $badbc );
42     is( $dotransfer, 0, "Can't transfer a bad barcode" );
43     is_deeply(
44         $messages,
45         { BadBarcode => $badbc },
46         "We got the expected barcode"
47     );
48 };
49
50 #FIXME:'UseBranchTransferLimits tests missing
51
52 subtest 'transfer already at destination' => sub {
53     plan tests => 5;
54
55     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
56     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
57
58     my $patron = $builder->build_object(
59         {
60             class => 'Koha::Patrons',
61             value => { branchcode => $library->branchcode }
62         }
63     );
64
65     my $item = $builder->build_sample_item(
66         {
67             library => $library->branchcode,
68         }
69     );
70
71     my ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
72     is( $dotransfer, 0, 'Transfer of reserved item failed with ignore reserves: true' );
73     is_deeply(
74         $messages,
75         { 'DestinationEqualsHolding' => 1 },
76         "We got the expected failure message: DestinationEqualsHolding"
77     );
78
79     # We are making sure there is no regression, feel free to change the behavior if needed.
80     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
81     #   found will override all other measures that may prevent transfer and force a transfer.
82     AddReserve({
83         branchcode     => $item->homebranch,
84         borrowernumber => $patron->borrowernumber,
85         biblionumber   => $item->biblionumber,
86         itemnumber     => $item->itemnumber,
87     });
88
89     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
90     is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
91     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
92     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
93 };
94
95 subtest 'transfer an issued item' => sub {
96     plan tests => 5;
97
98     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
99     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
100
101     my $patron = $builder->build_object(
102         {
103             class => 'Koha::Patrons',
104             value => { branchcode => $library->branchcode }
105         }
106     );
107
108     my $item = $builder->build_sample_item(
109         {
110             library => $library->branchcode,
111         }
112     );
113
114     my $dt_to = dt_from_string();
115     my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
116
117     # We are making sure there is no regression, feel free to change the behavior if needed.
118     # * WasReturned does not seem like a variable that should contain a borrowernumber
119     # * Should we return even if the transfer did not happen? (same branches)
120     my ($dotransfer, $messages) = transferbook( $library->branchcode, $item->barcode );
121     is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
122
123     # Reset issue
124     $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
125
126     # We are making sure there is no regression, feel free to change the behavior if needed.
127     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
128     #   found will override all other measures that may prevent transfer and force a transfer.
129     AddReserve({
130         branchcode     => $item->homebranch,
131         borrowernumber => $patron->borrowernumber,
132         biblionumber   => $item->biblionumber,
133         itemnumber     => $item->itemnumber,
134     });
135
136     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
137     is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
138     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
139     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
140     is( $messages->{WasReturned}, $patron->borrowernumber, "We got the return info");
141 };
142
143 subtest 'ignore_reserves flag' => sub {
144     plan tests => 10;
145     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
146     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
147
148     my $patron = $builder->build_object(
149         {
150             class => 'Koha::Patrons',
151             value => { branchcode => $library->branchcode }
152         }
153     );
154
155     my $item = $builder->build_sample_item(
156         {
157             library => $library->branchcode,
158         }
159     );
160
161     AddReserve({
162         branchcode     => $item->homebranch,
163         borrowernumber => $patron->borrowernumber,
164         biblionumber   => $item->biblionumber,
165         itemnumber     => $item->itemnumber,
166     });
167
168     # We are making sure there is no regression, feel free to change the behavior if needed.
169     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
170     #   found will override all other measures that may prevent transfer and force a transfer.
171     my ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
172     is( $dotransfer, 1, 'Transfer of reserved item succeeded without ignore reserves' );
173     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
174     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
175
176     my $ignore_reserves = 0;
177     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode, $ignore_reserves );
178     is( $dotransfer, 1, 'Transfer of reserved item succeeded with ignore reserves: false' );
179     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
180     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
181
182     $ignore_reserves = 1;
183     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode, $ignore_reserves );
184     is( $dotransfer, 0, 'Transfer of reserved item failed with ignore reserves: true' );
185     is_deeply(
186         $messages,
187         { 'DestinationEqualsHolding' => 1 },
188         "We got the expected failure message: DestinationEqualsHolding"
189     );
190     isnt( $messages->{ResFound}->{ResFound}, 'Reserved', "We did not return that we found a reserve");
191     isnt( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We did not return the reserve info");
192 };