Bug 10748: Add tests
[koha.git] / t / db_dependent / Circulation / Returns.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 5;
21 use Test::MockModule;
22 use Test::Warn;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Biblio;
28 use C4::Circulation;
29 use C4::Items;
30 use C4::Members;
31 use Koha::Database;
32 use Koha::Account::Lines;
33 use Koha::DateUtils;
34 use Koha::Items;
35 use Koha::Patrons;
36
37 use MARC::Record;
38 use MARC::Field;
39
40 # Mock userenv, used by AddIssue
41 my $branch;
42 my $context = Test::MockModule->new('C4::Context');
43 $context->mock( 'userenv', sub {
44     return { branch => $branch }
45 });
46
47 my $schema = Koha::Database->schema;
48 $schema->storage->txn_begin;
49
50 my $builder = t::lib::TestBuilder->new();
51 Koha::IssuingRules->search->delete;
52 my $rule = Koha::IssuingRule->new(
53     {
54         categorycode => '*',
55         itemtype     => '*',
56         branchcode   => '*',
57         maxissueqty  => 99,
58         issuelength  => 1,
59     }
60 );
61 $rule->store();
62
63 subtest "InProcessingToShelvingCart tests" => sub {
64
65     plan tests => 2;
66
67     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
68     my $permanent_location = 'TEST';
69     my $location           = 'PROC';
70
71     # Create a biblio record with biblio-level itemtype
72     my $record = MARC::Record->new();
73     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
74     my $built_item = $builder->build({
75         source => 'Item',
76         value  => {
77             biblionumber  => $biblionumber,
78             homebranch    => $branch,
79             holdingbranch => $branch,
80             location      => $location,
81             permanent_location => $permanent_location
82         }
83     });
84     my $barcode = $built_item->{ barcode };
85     my $itemnumber = $built_item->{ itemnumber };
86     my $item;
87
88     t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 );
89     AddReturn( $barcode, $branch );
90     $item = GetItem( $itemnumber );
91     is( $item->{location}, 'CART',
92         "InProcessingToShelvingCart functions as intended" );
93
94     $item->{location} = $location;
95     ModItem( $item, undef, $itemnumber );
96
97     t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 );
98     AddReturn( $barcode, $branch );
99     $item = GetItem( $itemnumber );
100     is( $item->{location}, $permanent_location,
101         "InProcessingToShelvingCart functions as intended" );
102 };
103
104
105 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
106
107     plan tests => 4;
108
109     # Set item-level item types
110     t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
111
112     # Make sure logging is enabled
113     t::lib::Mocks::mock_preference( "IssueLog", 1 );
114     t::lib::Mocks::mock_preference( "ReturnLog", 1 );
115
116     # Create an itemtype for biblio-level item type
117     my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
118     # Create an itemtype for item-level item type
119     my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
120     # Create a branch
121     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
122     # Create a borrower
123     my $borrowernumber = $builder->build({
124         source => 'Borrower',
125         value => { branchcode => $branch }
126     })->{ borrowernumber };
127     # Look for the defined MARC field for biblio-level itemtype
128     my $rs = $schema->resultset('MarcSubfieldStructure')->search({
129         frameworkcode => '',
130         kohafield     => 'biblioitems.itemtype'
131     });
132     my $tagfield    = $rs->first->tagfield;
133     my $tagsubfield = $rs->first->tagsubfield;
134
135     # Create a biblio record with biblio-level itemtype
136     my $record = MARC::Record->new();
137     $record->append_fields(
138         MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
139     );
140     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
141     my $item_with_itemtype = $builder->build(
142         {
143             source => 'Item',
144             value  => {
145                 biblionumber     => $biblionumber,
146                 biblioitemnumber => $biblioitemnumber,
147                 homebranch       => $branch,
148                 holdingbranch    => $branch,
149                 itype            => $ilevel_itemtype
150             }
151         }
152     );
153     my $item_without_itemtype = $builder->build(
154         {
155             source => 'Item',
156             value  => {
157                 biblionumber     => $biblionumber,
158                 biblioitemnumber => $biblioitemnumber,
159                 homebranch       => $branch,
160                 holdingbranch    => $branch,
161                 itype            => undef
162             }
163         }
164     );
165
166     my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
167     AddIssue( $borrower, $item_with_itemtype->{ barcode } );
168     AddReturn( $item_with_itemtype->{ barcode }, $branch );
169     # Test item-level itemtype was recorded on the 'statistics' table
170     my $stat = $schema->resultset('Statistic')->search({
171         branch     => $branch,
172         type       => 'return',
173         itemnumber => $item_with_itemtype->{ itemnumber }
174     }, { order_by => { -asc => 'datetime' } })->next();
175
176     is( $stat->itemtype, $ilevel_itemtype,
177         "item-level itype recorded on statistics for return");
178     warning_like { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
179                  [qr/^item-level_itypes set but no itemtype set for item/,
180                  qr/^item-level_itypes set but no itemtype set for item/],
181                  'Item without itemtype set raises warning on AddIssue';
182     warning_like { AddReturn( $item_without_itemtype->{ barcode }, $branch ) }
183                  qr/^item-level_itypes set but no itemtype set for item/,
184                  'Item without itemtype set raises warning on AddReturn';
185     # Test biblio-level itemtype was recorded on the 'statistics' table
186     $stat = $schema->resultset('Statistic')->search({
187         branch     => $branch,
188         type       => 'return',
189         itemnumber => $item_without_itemtype->{ itemnumber }
190     }, { order_by => { -asc => 'datetime' } })->next();
191
192     is( $stat->itemtype, $blevel_itemtype,
193         "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
194
195 };
196
197 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
198
199     plan tests => 2;
200
201     # Make sure logging is enabled
202     t::lib::Mocks::mock_preference( "IssueLog", 1 );
203     t::lib::Mocks::mock_preference( "ReturnLog", 1 );
204
205     # Set biblio level item types
206     t::lib::Mocks::mock_preference( "item-level_itypes", 0 );
207
208     # Create an itemtype for biblio-level item type
209     my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
210     # Create an itemtype for item-level item type
211     my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
212     # Create a branch
213     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
214     # Create a borrower
215     my $borrowernumber = $builder->build({
216         source => 'Borrower',
217         value => { branchcode => $branch }
218     })->{ borrowernumber };
219     # Look for the defined MARC field for biblio-level itemtype
220     my $rs = $schema->resultset('MarcSubfieldStructure')->search({
221         frameworkcode => '',
222         kohafield     => 'biblioitems.itemtype'
223     });
224     my $tagfield    = $rs->first->tagfield;
225     my $tagsubfield = $rs->first->tagsubfield;
226
227     # Create a biblio record with biblio-level itemtype
228     my $record = MARC::Record->new();
229     $record->append_fields(
230         MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
231     );
232     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
233     my $item_with_itemtype = $builder->build({
234         source => 'Item',
235         value  => {
236             biblionumber  => $biblionumber,
237             biblioitemnumber => $biblioitemnumber,
238             homebranch    => $branch,
239             holdingbranch => $branch,
240             itype         => $ilevel_itemtype
241         }
242     });
243     my $item_without_itemtype = $builder->build({
244         source => 'Item',
245         value  => {
246             biblionumber  => $biblionumber,
247             biblioitemnumber => $biblioitemnumber,
248             homebranch    => $branch,
249             holdingbranch => $branch,
250             itype         => undef
251         }
252     });
253
254     my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
255
256     AddIssue( $borrower, $item_with_itemtype->{ barcode } );
257     AddReturn( $item_with_itemtype->{ barcode }, $branch );
258     # Test item-level itemtype was recorded on the 'statistics' table
259     my $stat = $schema->resultset('Statistic')->search({
260         branch     => $branch,
261         type       => 'return',
262         itemnumber => $item_with_itemtype->{ itemnumber }
263     }, { order_by => { -asc => 'datetime' } })->next();
264
265     is( $stat->itemtype, $blevel_itemtype,
266         "biblio-level itype recorded on statistics for return");
267
268     AddIssue( $borrower, $item_without_itemtype->{ barcode } );
269     AddReturn( $item_without_itemtype->{ barcode }, $branch );
270     # Test biblio-level itemtype was recorded on the 'statistics' table
271     $stat = $schema->resultset('Statistic')->search({
272         branch     => $branch,
273         type       => 'return',
274         itemnumber => $item_without_itemtype->{ itemnumber }
275     }, { order_by => { -asc => 'datetime' } })->next();
276
277     is( $stat->itemtype, $blevel_itemtype,
278         "biblio-level itype recorded on statistics for return");
279 };
280
281 subtest 'Handle ids duplication' => sub {
282     plan tests => 6;
283
284     t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
285     t::lib::Mocks::mock_preference( 'CalculateFinesOnReturn', 1 );
286     t::lib::Mocks::mock_preference( 'finesMode', 'production' );
287     Koha::IssuingRules->search->update({ chargeperiod => 1, fine => 1, firstremind => 1, });
288
289     my $biblio = $builder->build( { source => 'Biblio' } );
290     my $itemtype = $builder->build( { source => 'Itemtype', value => { rentalcharge => 5 } } );
291     my $item = $builder->build(
292         {
293             source => 'Item',
294             value  => {
295                 biblionumber => $biblio->{biblionumber},
296                 notforloan => 0,
297                 itemlost   => 0,
298                 withdrawn  => 0,
299                 itype      => $itemtype->{itemtype},
300             }
301         }
302     );
303     my $patron = $builder->build({source => 'Borrower'});
304     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
305
306     my $original_checkout = AddIssue( $patron->unblessed, $item->{barcode}, dt_from_string->subtract( days => 50 ) );
307
308     my $issue_id = $original_checkout->issue_id;
309     # Create an existing entry in old_issue
310     $builder->build({ source => 'OldIssue', value => { issue_id => $issue_id } });
311
312     my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
313
314     my ($doreturn, $messages, $new_checkout, $borrower);
315     warning_like {
316         ( $doreturn, $messages, $new_checkout, $borrower ) =
317           AddReturn( $item->{barcode}, undef, undef, undef, dt_from_string );
318     }
319     [
320         qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
321         { carped => qr{The checkin for the following issue failed.*DBIx::Class::Storage::DBI::_dbh_execute.*} }
322     ],
323     'DBD should have raised an error about dup primary key';
324
325     is( $doreturn, 0, 'Return should not have been done' );
326     is( $messages->{WasReturned}, 0, 'messages should have the WasReturned flag set to 0' );
327     is( $messages->{DataCorrupted}, 1, 'messages should have the DataCorrupted flag set to 1' );
328
329     my $account_lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber, issue_id => $issue_id });
330     is( $account_lines->count, 0, 'No account lines should exist for this issue_id, patron should not have been charged' );
331
332     is( Koha::Checkouts->find( $issue_id )->issue_id, $issue_id, 'The issues entry should not have been removed' );
333 };
334
335 subtest 'BlockReturnOfLostItems' => sub {
336     plan tests => 3;
337     my $biblio = $builder->build_object( { class => 'Koha::Biblios' } );
338     my $item = $builder->build_object(
339         {
340             class  => 'Koha::Items',
341             value  => {
342                 biblionumber => $biblio->biblionumber,
343                 notforloan => 0,
344                 itemlost   => 0,
345                 withdrawn  => 0,
346         }
347     }
348     );
349     my $patron = $builder->build_object({class => 'Koha::Patrons'});
350     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
351
352     # Mark the item as lost
353     ModItem({itemlost => 1}, $biblio->biblionumber, $item->itemnumber);
354
355     t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 1);
356     my ( $doreturn, $messages, $issue ) = AddReturn($item->barcode);
357     is( $doreturn, 0, "With BlockReturnOfLostItems, a checkin of a lost item should be blocked");
358     is( $messages->{WasLost}, 1, "... and the WasLost flag should be set");
359
360     t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0);
361     ( $doreturn, $messages, $issue ) = AddReturn($item->barcode);
362     is( $doreturn, 1, "Without BlockReturnOfLostItems, a checkin of a lost item should not be blocked");
363 };