Bug 23805: Update 'Pay' to 'PAYMENT' for consistency
[koha.git] / t / db_dependent / Koha / Items.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 10;
23 use Test::Exception;
24
25 use C4::Circulation;
26 use Koha::Item;
27 use Koha::Item::Transfer::Limits;
28 use Koha::Items;
29 use Koha::Database;
30
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 my $builder     = t::lib::TestBuilder->new;
38 my $library     = $builder->build( { source => 'Branch' } );
39 my $nb_of_items = Koha::Items->search->count;
40 my $biblio      = $builder->build_sample_biblio();
41 my $new_item_1   = $builder->build_sample_item({
42     biblionumber => $biblio->biblionumber,
43     homebranch       => $library->{branchcode},
44     holdingbranch    => $library->{branchcode},
45 });
46 my $new_item_2   = $builder->build_sample_item({
47     biblionumber => $biblio->biblionumber,
48     homebranch       => $library->{branchcode},
49     holdingbranch    => $library->{branchcode},
50 });
51
52
53 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
54
55 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
56 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
57
58 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
59 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
60
61 subtest 'get_transfer' => sub {
62     plan tests => 3;
63
64     my $transfer = $new_item_1->get_transfer();
65     is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
66
67     my $library_to = $builder->build( { source => 'Branch' } );
68
69     C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
70
71     $transfer = $new_item_1->get_transfer();
72     is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
73
74     is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
75 };
76
77 subtest 'holds' => sub {
78     plan tests => 5;
79
80     my $biblio = $builder->build_sample_biblio();
81     my $item   = $builder->build_sample_item({
82         biblionumber => $biblio->biblionumber,
83     });
84     $nb_of_items++;
85     is($item->holds(), undef, "Nothing returned if no holds");
86     my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
87     my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
88     my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
89
90     is($item->holds()->count,3,"Three holds found");
91     is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
92     is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
93     is($item->holds({found => undef}),undef,"Nothing returned if no matching holds");
94 };
95
96 subtest 'biblio' => sub {
97     plan tests => 2;
98
99     my $biblio = $retrieved_item_1->biblio;
100     is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
101     is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
102 };
103
104 subtest 'biblioitem' => sub {
105     plan tests => 2;
106
107     my $biblioitem = $retrieved_item_1->biblioitem;
108     is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
109     is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
110 };
111
112 subtest 'checkout' => sub {
113     plan tests => 5;
114     my $item = Koha::Items->find( $new_item_1->itemnumber );
115     # No checkout yet
116     my $checkout = $item->checkout;
117     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
118
119     # Add a checkout
120     my $patron = $builder->build({ source => 'Borrower' });
121     C4::Circulation::AddIssue( $patron, $item->barcode );
122     $checkout = $retrieved_item_1->checkout;
123     is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
124     is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
125     is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
126
127     # Do the return
128     C4::Circulation::AddReturn( $item->barcode );
129
130     # There is no more checkout on this item, making sure it will not return old checkouts
131     $checkout = $item->checkout;
132     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
133 };
134
135 subtest 'can_be_transferred' => sub {
136     plan tests => 5;
137
138     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
139     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
140
141     my $biblio   = $builder->build_sample_biblio();
142     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
143     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
144     my $item  = $builder->build_sample_item({
145         biblionumber     => $biblio->biblionumber,
146         homebranch       => $library1->branchcode,
147         holdingbranch    => $library1->branchcode,
148     });
149     $nb_of_items++;
150
151     is(Koha::Item::Transfer::Limits->search({
152         fromBranch => $library1->branchcode,
153         toBranch => $library2->branchcode,
154     })->count, 0, 'There are no transfer limits between libraries.');
155     ok($item->can_be_transferred({ to => $library2 }),
156        'Item can be transferred between libraries.');
157
158     my $limit = Koha::Item::Transfer::Limit->new({
159         fromBranch => $library1->branchcode,
160         toBranch => $library2->branchcode,
161         itemtype => $item->effective_itemtype,
162     })->store;
163     is(Koha::Item::Transfer::Limits->search({
164         fromBranch => $library1->branchcode,
165         toBranch => $library2->branchcode,
166     })->count, 1, 'Given we have added a transfer limit,');
167     is($item->can_be_transferred({ to => $library2 }), 0,
168        'Item can no longer be transferred between libraries.');
169     is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
170        'We get the same result also if we pass the from-library parameter.');
171 };
172
173 $retrieved_item_1->delete;
174 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
175
176 $schema->storage->txn_rollback;
177