e37324f147b01813d789867a5e1723428a526e66
[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 => 2;
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 'transfer an issued item' => sub {
51     plan tests => 3;
52
53     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
54     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
55
56     my $patron = $builder->build_object(
57         {
58             class => 'Koha::Patrons',
59             value => { branchcode => $library->branchcode }
60         }
61     );
62
63     my $item = $builder->build_sample_item(
64         {
65             library => $library->branchcode,
66         }
67     );
68
69     my $dt_to = dt_from_string();
70     my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
71
72     # We are making sure there is no regression, feel free to change the behavior if needed.
73     # * WasReturned does not seem like a variable that should contain a borrowernumber
74     # * Should we return even if the transfer did not happen? (same branches)
75     my ($dotransfer, $messages) = transferbook( $library->branchcode, $item->barcode );
76     is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
77
78     AddReserve({
79         branchcode     => $item->homebranch,
80         borrowernumber => $patron->borrowernumber,
81         biblionumber   => $item->biblionumber,
82         itemnumber     => $item->itemnumber,
83     });
84     ($dotransfer, $messages ) = transferbook( $library->branchcode, $item->barcode );
85     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
86     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
87 };