Bug 18072: Add Koha::Item->can_be_transferred
[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 => 9;
23
24 use C4::Circulation;
25 use Koha::Item;
26 use Koha::Item::Transfer::Limits;
27 use Koha::Items;
28 use Koha::Database;
29
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35
36 my $builder     = t::lib::TestBuilder->new;
37 my $biblioitem  = $builder->build( { source => 'Biblioitem' } );
38 my $library     = $builder->build( { source => 'Branch' } );
39 my $nb_of_items = Koha::Items->search->count;
40 my $new_item_1  = Koha::Item->new(
41     {   biblionumber     => $biblioitem->{biblionumber},
42         biblioitemnumber => $biblioitem->{biblioitemnumber},
43         homebranch       => $library->{branchcode},
44         holdingbranch    => $library->{branchcode},
45         barcode          => "a_barcode_for_t",
46         itype            => 'BK',
47     }
48 )->store;
49 my $new_item_2 = Koha::Item->new(
50     {   biblionumber     => $biblioitem->{biblionumber},
51         biblioitemnumber => $biblioitem->{biblioitemnumber},
52         homebranch       => $library->{branchcode},
53         holdingbranch    => $library->{branchcode},
54         barcode          => "another_bc_for_t",
55         itype            => 'BK',
56     }
57 )->store;
58
59 C4::Context->_new_userenv('xxx');
60 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
61
62 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
63 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
64
65 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
66 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
67
68 subtest 'get_transfer' => sub {
69     plan tests => 3;
70
71     my $transfer = $new_item_1->get_transfer();
72     is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
73
74     my $library_to = $builder->build( { source => 'Branch' } );
75
76     C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
77
78     $transfer = $new_item_1->get_transfer();
79     is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
80
81     is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
82 };
83
84 subtest 'biblio' => sub {
85     plan tests => 2;
86
87     my $biblio = $retrieved_item_1->biblio;
88     is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
89     is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
90 };
91
92 subtest 'biblioitem' => sub {
93     plan tests => 2;
94
95     my $biblioitem = $retrieved_item_1->biblioitem;
96     is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
97     is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
98 };
99
100 subtest 'checkout' => sub {
101     plan tests => 5;
102     my $item = Koha::Items->find( $new_item_1->itemnumber );
103     # No checkout yet
104     my $checkout = $item->checkout;
105     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
106
107     # Add a checkout
108     my $patron = $builder->build({ source => 'Borrower' });
109     C4::Circulation::AddIssue( $patron, $item->barcode );
110     $checkout = $retrieved_item_1->checkout;
111     is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
112     is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
113     is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
114
115     # Do the return
116     C4::Circulation::AddReturn( $item->barcode );
117
118     # There is no more checkout on this item, making sure it will not return old checkouts
119     $checkout = $item->checkout;
120     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
121 };
122
123 subtest 'can_be_transferred' => sub {
124     plan tests => 8;
125
126     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
127     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
128
129     my $library1 = $builder->build( { source => 'Branch' } )->{branchcode};
130     my $library2 = $builder->build( { source => 'Branch' } )->{branchcode};
131     my $item  = Koha::Item->new({
132         biblionumber     => $biblioitem->{biblionumber},
133         biblioitemnumber => $biblioitem->{biblioitemnumber},
134         homebranch       => $library1,
135         holdingbranch    => $library1,
136         itype            => 'test',
137         barcode          => "newbarcode",
138     })->store;
139     $nb_of_items++;
140
141     is(Koha::Item::Transfer::Limits->search({
142         fromBranch => $library1,
143         toBranch => $library2,
144     })->count, 0, 'There are no transfer limits between libraries.');
145     ok($item->can_be_transferred($library2),
146        'Item can be transferred between libraries.');
147
148     my $limit = Koha::Item::Transfer::Limit->new({
149         fromBranch => $library1,
150         toBranch => $library2,
151         itemtype => $item->effective_itemtype,
152     })->store;
153     is(Koha::Item::Transfer::Limits->search({
154         fromBranch => $library1,
155         toBranch => $library2,
156     })->count, 1, 'Given we have added a transfer limit,');
157     is($item->can_be_transferred($library2), 0,
158        'Item can no longer be transferred between libraries.');
159     is($item->can_be_transferred($library2, $library1), 0,
160        'We get the same result also if we pass the from-library parameter.');
161     eval { $item->can_be_transferred(); };
162     is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when no library given.');
163     eval { $item->can_be_transferred('heaven'); };
164     is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.');
165     eval { $item->can_be_transferred($library2, 'hell'); };
166     is(ref($@), 'Koha::Exceptions::Library::NotFound', 'Exception thrown when invalid library is given.');
167 };
168
169 $retrieved_item_1->delete;
170 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
171
172 $schema->storage->txn_rollback;
173