Bug 21754: Automatically clean up outstanding transfers on lost items
[koha.git] / t / db_dependent / Circulation / IsItemIssued.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 Test::MockModule;
22
23 use C4::Circulation;
24 use C4::Items;
25 use C4::Biblio;
26 use Koha::Database;
27 use Koha::DateUtils;
28 use Koha::Patrons;
29
30 use t::lib::TestBuilder;
31
32 use MARC::Record;
33
34 my $schema = Koha::Database->schema;
35 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
37
38 my $library = $builder->build({ source => 'Branch' });
39 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
40 my $patron_category = $builder->build({ source => 'Category' });
41
42 C4::Context->_new_userenv('DUMMY SESSION');
43 C4::Context->set_userenv(
44     undef, undef, undef, undef, undef,
45     $library->{branchcode},
46     $library->{branchname}
47 );
48
49
50
51 my $borrowernumber = Koha::Patron->new({
52     firstname =>  'my firstname',
53     surname => 'my surname',
54     categorycode => $patron_category->{categorycode},
55     branchcode => $library->{branchcode},
56 })->store->borrowernumber;
57
58 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
59 my $record = MARC::Record->new();
60 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
61
62 my ( undef, undef, $itemnumber ) = AddItem(
63     {   homebranch    => $library->{branchcode},
64         holdingbranch => $library->{branchcode},
65         barcode       => 'i_dont_exist',
66         itype         => $itemtype
67     },
68     $biblionumber
69 );
70
71 my $item = GetItem( $itemnumber );
72
73 is ( IsItemIssued( $item->{itemnumber} ), 0, "item is not on loan at first" );
74
75 AddIssue($borrower, 'i_dont_exist');
76 is ( IsItemIssued( $item->{itemnumber} ), 1, "item is now on loan" );
77
78 is(
79     DelItemCheck( $biblionumber, $itemnumber),
80     'book_on_loan',
81     'item that is on loan cannot be deleted',
82 );
83
84 AddReturn('i_dont_exist', $library->{branchcode});
85 is ( IsItemIssued( $item->{itemnumber} ), 0, "item has been returned" );
86
87 is(
88     DelItemCheck( $biblionumber, $itemnumber),
89     1,
90     'item that is not on loan can be deleted',
91 );
92
93 $schema->storage->txn_rollback;
94