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;
29 use Koha::Item::Transfer::Limits;
32 use Koha::DateUtils qw( dt_from_string );
34 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
41 my $dbh = C4::Context->dbh;
43 my $builder = t::lib::TestBuilder->new;
44 my $library = $builder->build( { source => 'Branch' } );
45 my $nb_of_items = Koha::Items->search->count;
46 my $biblio = $builder->build_sample_biblio();
47 my $new_item_1 = $builder->build_sample_item({
48 biblionumber => $biblio->biblionumber,
49 homebranch => $library->{branchcode},
50 holdingbranch => $library->{branchcode},
52 my $new_item_2 = $builder->build_sample_item({
53 biblionumber => $biblio->biblionumber,
54 homebranch => $library->{branchcode},
55 holdingbranch => $library->{branchcode},
59 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
61 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
62 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
64 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
65 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
67 subtest 'store' => sub {
70 my $biblio = $builder->build_sample_biblio;
71 my $today = dt_from_string->set( hour => 0, minute => 0, second => 0 );
72 my $item = Koha::Item->new(
74 homebranch => $library->{branchcode},
75 holdingbranch => $library->{branchcode},
76 biblionumber => $biblio->biblionumber,
82 is( t::lib::Dates::compare($item->replacementpricedate, $today), 0, 'replacementpricedate must have been set to today if not given');
83 is( t::lib::Dates::compare($item->datelastseen, $today), 0, 'datelastseen must have been set to today if not given');
84 is( $item->itype, $biblio->biblioitem->itemtype, 'items.itype must have been set to biblioitem.itemtype is not given');
85 is( $item->permanent_location, $item->location, 'permanent_location must have been set to location if not given' );
88 subtest '*_on updates' => sub {
91 # Once the '_on' value is set (triggered by the related field turning from false to true)
92 # it should not be re-set for any changes outside of the related field being 'unset'.
94 my @fields = qw( itemlost withdrawn damaged );
95 my $today = dt_from_string();
96 my $yesterday = $today->clone()->subtract( days => 1 );
98 for my $field ( @fields ) {
99 my $item = $builder->build_sample_item(
102 itemlost_on => undef,
104 withdrawn_on => undef,
109 my $field_on = $field . '_on';
111 # Set field for the first time
112 Time::Fake->offset( $yesterday->epoch );
113 $item->$field(1)->store;
114 $item->get_from_storage;
115 is($item->$field_on, DateTime::Format::MySQL->format_datetime($yesterday), $field_on . " was set upon first truthy setting");
117 # Update the field to a new 'true' value
118 Time::Fake->offset( $today->epoch );
119 $item->$field(2)->store;
120 $item->get_from_storage;
121 is($item->$field_on, DateTime::Format::MySQL->format_datetime($yesterday), $field_on . " was not updated upon second truthy setting");
123 # Update the field to a new 'false' value
124 $item->$field(0)->store;
125 $item->get_from_storage;
126 is($item->$field_on, undef, $field_on . " was unset upon untruthy setting");
134 subtest 'get_transfer' => sub {
137 my $transfer = $new_item_1->get_transfer();
138 is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
140 my $library_to = $builder->build( { source => 'Branch' } );
142 C4::Circulation::transferbook({
143 from_branch => $new_item_1->holdingbranch,
144 to_branch => $library_to->{branchcode},
145 barcode => $new_item_1->barcode,
148 $transfer = $new_item_1->get_transfer();
149 is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
151 is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
154 subtest 'holds' => sub {
157 my $biblio = $builder->build_sample_biblio();
158 my $item = $builder->build_sample_item({
159 biblionumber => $biblio->biblionumber,
161 is($item->holds->count, 0, "Nothing returned if no holds");
162 my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
163 my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
164 my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
166 is($item->holds()->count,3,"Three holds found");
167 is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
168 is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
169 is($item->holds({found => undef})->count, 0,"Nothing returned if no matching holds");
172 subtest 'biblio' => sub {
175 my $biblio = $retrieved_item_1->biblio;
176 is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
177 is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
180 subtest 'biblioitem' => sub {
183 my $biblioitem = $retrieved_item_1->biblioitem;
184 is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
185 is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
188 subtest 'checkout' => sub {
190 my $item = Koha::Items->find( $new_item_1->itemnumber );
192 my $checkout = $item->checkout;
193 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
196 my $patron = $builder->build({ source => 'Borrower' });
197 C4::Circulation::AddIssue( $patron, $item->barcode );
198 $checkout = $retrieved_item_1->checkout;
199 is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
200 is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
201 is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
204 C4::Circulation::AddReturn( $item->barcode );
206 # There is no more checkout on this item, making sure it will not return old checkouts
207 $checkout = $item->checkout;
208 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
211 subtest 'can_be_transferred' => sub {
214 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
215 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
217 my $biblio = $builder->build_sample_biblio();
218 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
219 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
220 my $item = $builder->build_sample_item({
221 biblionumber => $biblio->biblionumber,
222 homebranch => $library1->branchcode,
223 holdingbranch => $library1->branchcode,
226 is(Koha::Item::Transfer::Limits->search({
227 fromBranch => $library1->branchcode,
228 toBranch => $library2->branchcode,
229 })->count, 0, 'There are no transfer limits between libraries.');
230 ok($item->can_be_transferred({ to => $library2 }),
231 'Item can be transferred between libraries.');
233 my $limit = Koha::Item::Transfer::Limit->new({
234 fromBranch => $library1->branchcode,
235 toBranch => $library2->branchcode,
236 itemtype => $item->effective_itemtype,
238 is(Koha::Item::Transfer::Limits->search({
239 fromBranch => $library1->branchcode,
240 toBranch => $library2->branchcode,
241 })->count, 1, 'Given we have added a transfer limit,');
242 is($item->can_be_transferred({ to => $library2 }), 0,
243 'Item can no longer be transferred between libraries.');
244 is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
245 'We get the same result also if we pass the from-library parameter.');
248 # Reset nb_of_items prior to testing delete
249 $nb_of_items = Koha::Items->search->count;
252 $retrieved_item_1->delete;
253 is( Koha::Items->search->count, $nb_of_items - 1, 'Delete should have deleted the item' );
255 $schema->storage->txn_rollback;