Bug 18501: (QA follow-up) Add test of *_on functionality
[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 use Time::Fake;
25
26 use C4::Circulation;
27 use C4::Context;
28 use Koha::Item;
29 use Koha::Item::Transfer::Limits;
30 use Koha::Items;
31 use Koha::Database;
32 use Koha::DateUtils qw( dt_from_string );
33
34 use t::lib::TestBuilder;
35 use t::lib::Mocks;
36 use t::lib::Dates;
37
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40
41 my $dbh     = C4::Context->dbh;
42
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},
51 });
52 my $new_item_2   = $builder->build_sample_item({
53     biblionumber => $biblio->biblionumber,
54     homebranch       => $library->{branchcode},
55     holdingbranch    => $library->{branchcode},
56 });
57
58
59 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
60
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' );
63
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' );
66
67 subtest 'store' => sub {
68     plan tests => 5;
69
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(
73         {
74             homebranch    => $library->{branchcode},
75             holdingbranch => $library->{branchcode},
76             biblionumber  => $biblio->biblionumber,
77             location      => 'my_loc',
78         }
79     )->store
80     ->get_from_storage;
81
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' );
86     $item->delete;
87
88     subtest '*_on updates' => sub {
89         plan tests => 9;
90
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'.
93
94         my @fields = qw( itemlost withdrawn damaged );
95         my $today = dt_from_string();
96         my $yesterday = $today->clone()->subtract( days => 1 );
97
98         for my $field ( @fields ) {
99             my $item = $builder->build_sample_item(
100                 {
101                     itemlost     => 0,
102                     itemlost_on  => undef,
103                     withdrawn    => 0,
104                     withdrawn_on => undef,
105                     damaged      => 0,
106                     damaged_on   => undef
107                 }
108             );
109             my $field_on = $field . '_on';
110
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");
116
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");
122
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");
127
128             Time::Fake->reset;
129         }
130     };
131
132 };
133
134 subtest 'get_transfer' => sub {
135     plan tests => 3;
136
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' );
139
140     my $library_to = $builder->build( { source => 'Branch' } );
141
142     C4::Circulation::transferbook({
143         from_branch => $new_item_1->holdingbranch,
144         to_branch => $library_to->{branchcode},
145         barcode => $new_item_1->barcode,
146     });
147
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' );
150
151     is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
152 };
153
154 subtest 'holds' => sub {
155     plan tests => 5;
156
157     my $biblio = $builder->build_sample_biblio();
158     my $item   = $builder->build_sample_item({
159         biblionumber => $biblio->biblionumber,
160     });
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' }});
165
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");
170 };
171
172 subtest 'biblio' => sub {
173     plan tests => 2;
174
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' );
178 };
179
180 subtest 'biblioitem' => sub {
181     plan tests => 2;
182
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' );
186 };
187
188 subtest 'checkout' => sub {
189     plan tests => 5;
190     my $item = Koha::Items->find( $new_item_1->itemnumber );
191     # No checkout yet
192     my $checkout = $item->checkout;
193     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
194
195     # Add a checkout
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' );
202
203     # Do the return
204     C4::Circulation::AddReturn( $item->barcode );
205
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' );
209 };
210
211 subtest 'can_be_transferred' => sub {
212     plan tests => 5;
213
214     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
215     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
216
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,
224     });
225
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.');
232
233     my $limit = Koha::Item::Transfer::Limit->new({
234         fromBranch => $library1->branchcode,
235         toBranch => $library2->branchcode,
236         itemtype => $item->effective_itemtype,
237     })->store;
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.');
246 };
247
248 # Reset nb_of_items prior to testing delete
249 $nb_of_items = Koha::Items->search->count;
250
251 # Test delete
252 $retrieved_item_1->delete;
253 is( Koha::Items->search->count, $nb_of_items - 1, 'Delete should have deleted the item' );
254
255 $schema->storage->txn_rollback;