3 # Copyright 2016 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 10;
27 use Koha::Item::Transfer::Limits;
31 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new;
38 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
39 my $library = $builder->build( { source => 'Branch' } );
40 my $nb_of_items = Koha::Items->search->count;
41 my $new_item_1 = Koha::Item->new(
42 { biblionumber => $biblioitem->{biblionumber},
43 biblioitemnumber => $biblioitem->{biblioitemnumber},
44 homebranch => $library->{branchcode},
45 holdingbranch => $library->{branchcode},
46 barcode => "a_barcode_for_t",
50 my $new_item_2 = Koha::Item->new(
51 { biblionumber => $biblioitem->{biblionumber},
52 biblioitemnumber => $biblioitem->{biblioitemnumber},
53 homebranch => $library->{branchcode},
54 holdingbranch => $library->{branchcode},
55 barcode => "another_bc_for_t",
60 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
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' );
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' );
68 subtest 'get_transfer' => sub {
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' );
74 my $library_to = $builder->build( { source => 'Branch' } );
76 C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
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' );
81 is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
84 subtest 'holds' => sub {
87 my $biblio = $builder->build_sample_biblio();
88 my $item = $builder->build_sample_item({
89 biblionumber => $biblio->biblionumber,
92 is($item->holds(), undef, "Nothing returned if no holds");
93 my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
94 my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
95 my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
97 is($item->holds()->count,3,"Three holds found");
98 is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
99 is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
100 is($item->holds({found => undef}),undef,"Nothing returned if no matching holds");
103 subtest 'biblio' => sub {
106 my $biblio = $retrieved_item_1->biblio;
107 is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
108 is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
111 subtest 'biblioitem' => sub {
114 my $biblioitem = $retrieved_item_1->biblioitem;
115 is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
116 is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
119 subtest 'checkout' => sub {
121 my $item = Koha::Items->find( $new_item_1->itemnumber );
123 my $checkout = $item->checkout;
124 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
127 my $patron = $builder->build({ source => 'Borrower' });
128 C4::Circulation::AddIssue( $patron, $item->barcode );
129 $checkout = $retrieved_item_1->checkout;
130 is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
131 is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
132 is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
135 C4::Circulation::AddReturn( $item->barcode );
137 # There is no more checkout on this item, making sure it will not return old checkouts
138 $checkout = $item->checkout;
139 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
142 subtest 'can_be_transferred' => sub {
145 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
146 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
148 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
149 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
150 my $item = Koha::Item->new({
151 biblionumber => $biblioitem->{biblionumber},
152 biblioitemnumber => $biblioitem->{biblioitemnumber},
153 homebranch => $library1->branchcode,
154 holdingbranch => $library1->branchcode,
156 barcode => "newbarcode",
160 is(Koha::Item::Transfer::Limits->search({
161 fromBranch => $library1->branchcode,
162 toBranch => $library2->branchcode,
163 })->count, 0, 'There are no transfer limits between libraries.');
164 ok($item->can_be_transferred({ to => $library2 }),
165 'Item can be transferred between libraries.');
167 my $limit = Koha::Item::Transfer::Limit->new({
168 fromBranch => $library1->branchcode,
169 toBranch => $library2->branchcode,
170 itemtype => $item->effective_itemtype,
172 is(Koha::Item::Transfer::Limits->search({
173 fromBranch => $library1->branchcode,
174 toBranch => $library2->branchcode,
175 })->count, 1, 'Given we have added a transfer limit,');
176 is($item->can_be_transferred({ to => $library2 }), 0,
177 'Item can no longer be transferred between libraries.');
178 is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
179 'We get the same result also if we pass the from-library parameter.');
182 $retrieved_item_1->delete;
183 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
185 $schema->storage->txn_rollback;