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({
96 from_branch => $new_item_1->holdingbranch,
97 to_branch => $library_to->{branchcode},
98 barcode => $new_item_1->barcode,
101 $transfer = $new_item_1->get_transfer();
102 is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
104 is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
107 subtest 'holds' => sub {
110 my $biblio = $builder->build_sample_biblio();
111 my $item = $builder->build_sample_item({
112 biblionumber => $biblio->biblionumber,
115 is($item->holds->count, 0, "Nothing returned if no holds");
116 my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
117 my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
118 my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
120 is($item->holds()->count,3,"Three holds found");
121 is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
122 is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
123 is($item->holds({found => undef})->count, 0,"Nothing returned if no matching holds");
126 subtest 'biblio' => sub {
129 my $biblio = $retrieved_item_1->biblio;
130 is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
131 is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
134 subtest 'biblioitem' => sub {
137 my $biblioitem = $retrieved_item_1->biblioitem;
138 is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
139 is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
142 subtest 'checkout' => sub {
144 my $item = Koha::Items->find( $new_item_1->itemnumber );
146 my $checkout = $item->checkout;
147 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
150 my $patron = $builder->build({ source => 'Borrower' });
151 C4::Circulation::AddIssue( $patron, $item->barcode );
152 $checkout = $retrieved_item_1->checkout;
153 is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
154 is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
155 is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
158 C4::Circulation::AddReturn( $item->barcode );
160 # There is no more checkout on this item, making sure it will not return old checkouts
161 $checkout = $item->checkout;
162 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
165 subtest 'can_be_transferred' => sub {
168 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
169 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
171 my $biblio = $builder->build_sample_biblio();
172 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
173 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
174 my $item = $builder->build_sample_item({
175 biblionumber => $biblio->biblionumber,
176 homebranch => $library1->branchcode,
177 holdingbranch => $library1->branchcode,
181 is(Koha::Item::Transfer::Limits->search({
182 fromBranch => $library1->branchcode,
183 toBranch => $library2->branchcode,
184 })->count, 0, 'There are no transfer limits between libraries.');
185 ok($item->can_be_transferred({ to => $library2 }),
186 'Item can be transferred between libraries.');
188 my $limit = Koha::Item::Transfer::Limit->new({
189 fromBranch => $library1->branchcode,
190 toBranch => $library2->branchcode,
191 itemtype => $item->effective_itemtype,
193 is(Koha::Item::Transfer::Limits->search({
194 fromBranch => $library1->branchcode,
195 toBranch => $library2->branchcode,
196 })->count, 1, 'Given we have added a transfer limit,');
197 is($item->can_be_transferred({ to => $library2 }), 0,
198 'Item can no longer be transferred between libraries.');
199 is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
200 'We get the same result also if we pass the from-library parameter.');
203 $retrieved_item_1->delete;
204 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
206 $schema->storage->txn_rollback;