Bug 20262: (QA follow-up) Update unit tests for changes to payin_amount
[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 qw( IsItemIssued AddIssue AddReturn );
24 use C4::Items;
25 use C4::Biblio qw( AddBiblio );
26 use Koha::Database;
27 use Koha::Items;
28 use Koha::Patrons;
29
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
32
33 use MARC::Record;
34
35 my $schema = Koha::Database->schema;
36 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new;
38
39 my $library = $builder->build({ source => 'Branch' });
40 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
41 my $patron_category = $builder->build({ source => 'Category' });
42
43 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
44
45 my $borrowernumber = Koha::Patron->new({
46     firstname =>  'my firstname',
47     surname => 'my surname',
48     categorycode => $patron_category->{categorycode},
49     branchcode => $library->{branchcode},
50 })->store->borrowernumber;
51
52 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
53 my $record = MARC::Record->new();
54 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
55
56 my $item = $builder->build_sample_item(
57     {
58         biblionumber     => $biblionumber,
59         library          => $library->{branchcode},
60         itype            => $itemtype
61     }
62 );
63
64 is ( IsItemIssued( $item->itemnumber ), 0, "item is not on loan at first" );
65
66 AddIssue($borrower, $item->barcode);
67 is ( IsItemIssued( $item->itemnumber ), 1, "item is now on loan" );
68
69 is(
70     @{$item->safe_delete->messages}[0]->message,
71     'book_on_loan',
72     'item that is on loan cannot be deleted',
73 );
74
75 AddReturn($item->barcode, $library->{branchcode});
76 is ( IsItemIssued( $item->itemnumber ), 0, "item has been returned" );
77
78 $item->discard_changes; # FIXME We should not need that
79                         # If we do not, $self->checkout in safe_to_delete will not know the item has been checked out
80 ok(
81     $item->safe_delete,
82     'item that is not on loan can be deleted',
83 );
84
85 $schema->storage->txn_rollback;
86