32fd06044d3f39644c3a8be449d6134234daa7b7
[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::Items;
29 use Koha::Patrons;
30
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
33
34 use MARC::Record;
35
36 my $schema = Koha::Database->schema;
37 $schema->storage->txn_begin;
38 my $builder = t::lib::TestBuilder->new;
39
40 my $library = $builder->build({ source => 'Branch' });
41 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
42 my $patron_category = $builder->build({ source => 'Category' });
43
44 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
45
46 my $borrowernumber = Koha::Patron->new({
47     firstname =>  'my firstname',
48     surname => 'my surname',
49     categorycode => $patron_category->{categorycode},
50     branchcode => $library->{branchcode},
51 })->store->borrowernumber;
52
53 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
54 my $record = MARC::Record->new();
55 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
56
57 my $item = $builder->build_sample_item(
58     {
59         biblionumber     => $biblionumber,
60         library          => $library->{branchcode},
61         itype            => $itemtype
62     }
63 );
64
65 is ( IsItemIssued( $item->itemnumber ), 0, "item is not on loan at first" );
66
67 AddIssue($borrower, $item->barcode);
68 is ( IsItemIssued( $item->itemnumber ), 1, "item is now on loan" );
69
70 is(
71     DelItemCheck( $biblionumber, $item->itemnumber),
72     'book_on_loan',
73     'item that is on loan cannot be deleted',
74 );
75
76 AddReturn($item->barcode, $library->{branchcode});
77 is ( IsItemIssued( $item->itemnumber ), 0, "item has been returned" );
78
79 is(
80     DelItemCheck( $biblionumber, $item->itemnumber),
81     1,
82     'item that is not on loan can be deleted',
83 );
84
85 $schema->storage->txn_rollback;
86