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