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 => 11;
28 use Koha::Item::Transfer::Limits;
31 use Koha::DateUtils qw( dt_from_string );
33 use t::lib::TestBuilder;
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
40 my $dbh = C4::Context->dbh;
42 my $builder = t::lib::TestBuilder->new;
43 my $library = $builder->build( { source => 'Branch' } );
44 my $nb_of_items = Koha::Items->search->count;
45 my $biblio = $builder->build_sample_biblio();
46 my $new_item_1 = $builder->build_sample_item({
47 biblionumber => $biblio->biblionumber,
48 homebranch => $library->{branchcode},
49 holdingbranch => $library->{branchcode},
51 my $new_item_2 = $builder->build_sample_item({
52 biblionumber => $biblio->biblionumber,
53 homebranch => $library->{branchcode},
54 holdingbranch => $library->{branchcode},
58 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
60 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
61 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
63 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
64 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
66 subtest 'store' => sub {
68 my $biblio = $builder->build_sample_biblio;
69 my $today = dt_from_string->set( hour => 0, minute => 0, second => 0 );
70 my $item = Koha::Item->new(
72 homebranch => $library->{branchcode},
73 holdingbranch => $library->{branchcode},
74 biblionumber => $biblio->biblionumber,
80 is( t::lib::Dates::compare($item->replacementpricedate, $today), 0, 'replacementpricedate must have been set to today if not given');
81 is( t::lib::Dates::compare($item->datelastseen, $today), 0, 'datelastseen must have been set to today if not given');
82 is( $item->itype, $biblio->biblioitem->itemtype, 'items.itype must have been set to biblioitem.itemtype is not given');
83 is( $item->permanent_location, $item->location, 'permanent_location must have been set to location if not given' );
87 subtest 'get_transfer' => sub {
90 my $transfer = $new_item_1->get_transfer();
91 is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
93 my $library_to = $builder->build( { source => 'Branch' } );
95 C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
97 $transfer = $new_item_1->get_transfer();
98 is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
100 is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
103 subtest 'holds' => sub {
106 my $biblio = $builder->build_sample_biblio();
107 my $item = $builder->build_sample_item({
108 biblionumber => $biblio->biblionumber,
111 is($item->holds->count, 0, "Nothing returned if no holds");
112 my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
113 my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
114 my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
116 is($item->holds()->count,3,"Three holds found");
117 is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
118 is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
119 is($item->holds({found => undef})->count, 0,"Nothing returned if no matching holds");
122 subtest 'biblio' => sub {
125 my $biblio = $retrieved_item_1->biblio;
126 is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
127 is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
130 subtest 'biblioitem' => sub {
133 my $biblioitem = $retrieved_item_1->biblioitem;
134 is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
135 is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
138 subtest 'checkout' => sub {
140 my $item = Koha::Items->find( $new_item_1->itemnumber );
142 my $checkout = $item->checkout;
143 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
146 my $patron = $builder->build({ source => 'Borrower' });
147 C4::Circulation::AddIssue( $patron, $item->barcode );
148 $checkout = $retrieved_item_1->checkout;
149 is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
150 is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
151 is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
154 C4::Circulation::AddReturn( $item->barcode );
156 # There is no more checkout on this item, making sure it will not return old checkouts
157 $checkout = $item->checkout;
158 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
161 subtest 'can_be_transferred' => sub {
164 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
165 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
167 my $biblio = $builder->build_sample_biblio();
168 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
169 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
170 my $item = $builder->build_sample_item({
171 biblionumber => $biblio->biblionumber,
172 homebranch => $library1->branchcode,
173 holdingbranch => $library1->branchcode,
177 is(Koha::Item::Transfer::Limits->search({
178 fromBranch => $library1->branchcode,
179 toBranch => $library2->branchcode,
180 })->count, 0, 'There are no transfer limits between libraries.');
181 ok($item->can_be_transferred({ to => $library2 }),
182 'Item can be transferred between libraries.');
184 my $limit = Koha::Item::Transfer::Limit->new({
185 fromBranch => $library1->branchcode,
186 toBranch => $library2->branchcode,
187 itemtype => $item->effective_itemtype,
189 is(Koha::Item::Transfer::Limits->search({
190 fromBranch => $library1->branchcode,
191 toBranch => $library2->branchcode,
192 })->count, 1, 'Given we have added a transfer limit,');
193 is($item->can_be_transferred({ to => $library2 }), 0,
194 'Item can no longer be transferred between libraries.');
195 is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
196 'We get the same result also if we pass the from-library parameter.');
199 $retrieved_item_1->delete;
200 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
202 $schema->storage->txn_rollback;