Bug 23695: Set holding branch to transferring branch when manually initiating a transfer
[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({
96         from_branch => $new_item_1->holdingbranch,
97         to_branch => $library_to->{branchcode},
98         barcode => $new_item_1->barcode,
99     });
100
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' );
103
104     is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
105 };
106
107 subtest 'holds' => sub {
108     plan tests => 5;
109
110     my $biblio = $builder->build_sample_biblio();
111     my $item   = $builder->build_sample_item({
112         biblionumber => $biblio->biblionumber,
113     });
114     $nb_of_items++;
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' }});
119
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");
124 };
125
126 subtest 'biblio' => sub {
127     plan tests => 2;
128
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' );
132 };
133
134 subtest 'biblioitem' => sub {
135     plan tests => 2;
136
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' );
140 };
141
142 subtest 'checkout' => sub {
143     plan tests => 5;
144     my $item = Koha::Items->find( $new_item_1->itemnumber );
145     # No checkout yet
146     my $checkout = $item->checkout;
147     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
148
149     # Add a checkout
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' );
156
157     # Do the return
158     C4::Circulation::AddReturn( $item->barcode );
159
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' );
163 };
164
165 subtest 'can_be_transferred' => sub {
166     plan tests => 5;
167
168     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
169     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
170
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,
178     });
179     $nb_of_items++;
180
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.');
187
188     my $limit = Koha::Item::Transfer::Limit->new({
189         fromBranch => $library1->branchcode,
190         toBranch => $library2->branchcode,
191         itemtype => $item->effective_itemtype,
192     })->store;
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.');
201 };
202
203 $retrieved_item_1->delete;
204 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
205
206 $schema->storage->txn_rollback;