Bug 23591: Add some useful methods to Koha::Suggestions
[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 => 11;
23 use Test::Exception;
24
25 use C4::Circulation;
26 use C4::Context;
27 use Koha::Item;
28 use Koha::Item::Transfer::Limits;
29 use Koha::Items;
30 use Koha::Database;
31 use Koha::DateUtils qw( dt_from_string );
32
33 use t::lib::TestBuilder;
34 use t::lib::Mocks;
35 use t::lib::Dates;
36
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39
40 my $dbh     = C4::Context->dbh;
41
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},
50 });
51 my $new_item_2   = $builder->build_sample_item({
52     biblionumber => $biblio->biblionumber,
53     homebranch       => $library->{branchcode},
54     holdingbranch    => $library->{branchcode},
55 });
56
57
58 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
59
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' );
62
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' );
65
66 subtest 'store' => sub {
67     plan tests => 4;
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(
71         {
72             homebranch    => $library->{branchcode},
73             holdingbranch => $library->{branchcode},
74             biblionumber  => $biblio->biblionumber,
75             location      => 'my_loc',
76         }
77     )->store
78     ->get_from_storage;
79
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' );
84     $item->delete;
85 };
86
87 subtest 'get_transfer' => sub {
88     plan tests => 3;
89
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' );
92
93     my $library_to = $builder->build( { source => 'Branch' } );
94
95     C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
96
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' );
99
100     is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
101 };
102
103 subtest 'holds' => sub {
104     plan tests => 5;
105
106     my $biblio = $builder->build_sample_biblio();
107     my $item   = $builder->build_sample_item({
108         biblionumber => $biblio->biblionumber,
109     });
110     $nb_of_items++;
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' }});
115
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");
120 };
121
122 subtest 'biblio' => sub {
123     plan tests => 2;
124
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' );
128 };
129
130 subtest 'biblioitem' => sub {
131     plan tests => 2;
132
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' );
136 };
137
138 subtest 'checkout' => sub {
139     plan tests => 5;
140     my $item = Koha::Items->find( $new_item_1->itemnumber );
141     # No checkout yet
142     my $checkout = $item->checkout;
143     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
144
145     # Add a checkout
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' );
152
153     # Do the return
154     C4::Circulation::AddReturn( $item->barcode );
155
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' );
159 };
160
161 subtest 'can_be_transferred' => sub {
162     plan tests => 5;
163
164     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
165     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
166
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,
174     });
175     $nb_of_items++;
176
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.');
183
184     my $limit = Koha::Item::Transfer::Limit->new({
185         fromBranch => $library1->branchcode,
186         toBranch => $library2->branchcode,
187         itemtype => $item->effective_itemtype,
188     })->store;
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.');
197 };
198
199 $retrieved_item_1->delete;
200 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
201
202 $schema->storage->txn_rollback;