3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 5;
25 use t::lib::TestBuilder;
32 use Koha::Account::Lines;
40 # Mock userenv, used by AddIssue
42 my $context = Test::MockModule->new('C4::Context');
43 $context->mock( 'userenv', sub {
44 return { branch => $branch }
47 my $schema = Koha::Database->schema;
48 $schema->storage->txn_begin;
50 my $builder = t::lib::TestBuilder->new();
51 Koha::IssuingRules->search->delete;
52 my $rule = Koha::IssuingRule->new(
63 subtest "InProcessingToShelvingCart tests" => sub {
67 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
68 my $permanent_location = 'TEST';
69 my $location = 'PROC';
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({
77 biblionumber => $biblionumber,
78 homebranch => $branch,
79 holdingbranch => $branch,
80 location => $location,
81 permanent_location => $permanent_location
84 my $barcode = $built_item->{ barcode };
85 my $itemnumber = $built_item->{ itemnumber };
88 t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 );
89 AddReturn( $barcode, $branch );
90 $item = Koha::Items->find( $itemnumber );
91 is( $item->location, 'CART',
92 "InProcessingToShelvingCart functions as intended" );
94 ModItem( {location => $location}, undef, $itemnumber );
96 t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 );
97 AddReturn( $barcode, $branch );
98 $item = Koha::Items->find( $itemnumber );
99 is( $item->location, $permanent_location,
100 "InProcessingToShelvingCart functions as intended" );
104 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
108 # Set item-level item types
109 t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
111 # Make sure logging is enabled
112 t::lib::Mocks::mock_preference( "IssueLog", 1 );
113 t::lib::Mocks::mock_preference( "ReturnLog", 1 );
115 # Create an itemtype for biblio-level item type
116 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
117 # Create an itemtype for item-level item type
118 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
120 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
122 my $borrowernumber = $builder->build({
123 source => 'Borrower',
124 value => { branchcode => $branch }
125 })->{ borrowernumber };
126 # Look for the defined MARC field for biblio-level itemtype
127 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
129 kohafield => 'biblioitems.itemtype'
131 my $tagfield = $rs->first->tagfield;
132 my $tagsubfield = $rs->first->tagsubfield;
134 # Create a biblio record with biblio-level itemtype
135 my $record = MARC::Record->new();
136 $record->append_fields(
137 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
139 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
140 my $item_with_itemtype = $builder->build(
144 biblionumber => $biblionumber,
145 biblioitemnumber => $biblioitemnumber,
146 homebranch => $branch,
147 holdingbranch => $branch,
148 itype => $ilevel_itemtype
152 my $item_without_itemtype = $builder->build(
156 biblionumber => $biblionumber,
157 biblioitemnumber => $biblioitemnumber,
158 homebranch => $branch,
159 holdingbranch => $branch,
165 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
166 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
167 AddReturn( $item_with_itemtype->{ barcode }, $branch );
168 # Test item-level itemtype was recorded on the 'statistics' table
169 my $stat = $schema->resultset('Statistic')->search({
172 itemnumber => $item_with_itemtype->{ itemnumber }
173 }, { order_by => { -asc => 'datetime' } })->next();
175 is( $stat->itemtype, $ilevel_itemtype,
176 "item-level itype recorded on statistics for return");
177 warning_like { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
178 [qr/^item-level_itypes set but no itemtype set for item/,
179 qr/^item-level_itypes set but no itemtype set for item/],
180 'Item without itemtype set raises warning on AddIssue';
181 warning_like { AddReturn( $item_without_itemtype->{ barcode }, $branch ) }
182 qr/^item-level_itypes set but no itemtype set for item/,
183 'Item without itemtype set raises warning on AddReturn';
184 # Test biblio-level itemtype was recorded on the 'statistics' table
185 $stat = $schema->resultset('Statistic')->search({
188 itemnumber => $item_without_itemtype->{ itemnumber }
189 }, { order_by => { -asc => 'datetime' } })->next();
191 is( $stat->itemtype, $blevel_itemtype,
192 "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
196 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
200 # Make sure logging is enabled
201 t::lib::Mocks::mock_preference( "IssueLog", 1 );
202 t::lib::Mocks::mock_preference( "ReturnLog", 1 );
204 # Set biblio level item types
205 t::lib::Mocks::mock_preference( "item-level_itypes", 0 );
207 # Create an itemtype for biblio-level item type
208 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
209 # Create an itemtype for item-level item type
210 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
212 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
214 my $borrowernumber = $builder->build({
215 source => 'Borrower',
216 value => { branchcode => $branch }
217 })->{ borrowernumber };
218 # Look for the defined MARC field for biblio-level itemtype
219 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
221 kohafield => 'biblioitems.itemtype'
223 my $tagfield = $rs->first->tagfield;
224 my $tagsubfield = $rs->first->tagsubfield;
226 # Create a biblio record with biblio-level itemtype
227 my $record = MARC::Record->new();
228 $record->append_fields(
229 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
231 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
232 my $item_with_itemtype = $builder->build({
235 biblionumber => $biblionumber,
236 biblioitemnumber => $biblioitemnumber,
237 homebranch => $branch,
238 holdingbranch => $branch,
239 itype => $ilevel_itemtype
242 my $item_without_itemtype = $builder->build({
245 biblionumber => $biblionumber,
246 biblioitemnumber => $biblioitemnumber,
247 homebranch => $branch,
248 holdingbranch => $branch,
253 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
255 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
256 AddReturn( $item_with_itemtype->{ barcode }, $branch );
257 # Test item-level itemtype was recorded on the 'statistics' table
258 my $stat = $schema->resultset('Statistic')->search({
261 itemnumber => $item_with_itemtype->{ itemnumber }
262 }, { order_by => { -asc => 'datetime' } })->next();
264 is( $stat->itemtype, $blevel_itemtype,
265 "biblio-level itype recorded on statistics for return");
267 AddIssue( $borrower, $item_without_itemtype->{ barcode } );
268 AddReturn( $item_without_itemtype->{ barcode }, $branch );
269 # Test biblio-level itemtype was recorded on the 'statistics' table
270 $stat = $schema->resultset('Statistic')->search({
273 itemnumber => $item_without_itemtype->{ itemnumber }
274 }, { order_by => { -asc => 'datetime' } })->next();
276 is( $stat->itemtype, $blevel_itemtype,
277 "biblio-level itype recorded on statistics for return");
280 subtest 'Handle ids duplication' => sub {
283 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
284 t::lib::Mocks::mock_preference( 'CalculateFinesOnReturn', 1 );
285 t::lib::Mocks::mock_preference( 'finesMode', 'production' );
286 Koha::IssuingRules->search->update({ chargeperiod => 1, fine => 1, firstremind => 1, });
288 my $biblio = $builder->build( { source => 'Biblio' } );
289 my $itemtype = $builder->build( { source => 'Itemtype', value => { rentalcharge => 5 } } );
290 my $item = $builder->build(
294 biblionumber => $biblio->{biblionumber},
298 itype => $itemtype->{itemtype},
302 my $patron = $builder->build({source => 'Borrower'});
303 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
305 my $original_checkout = AddIssue( $patron->unblessed, $item->{barcode}, dt_from_string->subtract( days => 50 ) );
306 my $issue_id = $original_checkout->issue_id;
307 my $account_lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber, issue_id => $issue_id });
308 is( $account_lines->count, 1, '1 account line should exist for this issue_id' );
309 is( $account_lines->next->description, 'Rental', 'patron has been charged the rentalcharge' );
310 $account_lines->delete;
312 # Create an existing entry in old_issue
313 $builder->build({ source => 'OldIssue', value => { issue_id => $issue_id } });
315 my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
317 my ($doreturn, $messages, $new_checkout, $borrower);
319 ( $doreturn, $messages, $new_checkout, $borrower ) =
320 AddReturn( $item->{barcode}, undef, undef, undef, dt_from_string );
323 qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
324 { carped => qr{The checkin for the following issue failed.*Duplicate ID.*} }
326 'DBD should have raised an error about dup primary key';
328 is( $doreturn, 0, 'Return should not have been done' );
329 is( $messages->{WasReturned}, 0, 'messages should have the WasReturned flag set to 0' );
330 is( $messages->{DataCorrupted}, 1, 'messages should have the DataCorrupted flag set to 1' );
332 $account_lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber, issue_id => $issue_id });
333 is( $account_lines->count, 0, 'No account lines should exist for this issue_id, patron should not have been charged' );
335 is( Koha::Checkouts->find( $issue_id )->issue_id, $issue_id, 'The issues entry should not have been removed' );
338 subtest 'BlockReturnOfLostItems' => sub {
340 my $biblio = $builder->build_object( { class => 'Koha::Biblios' } );
341 my $item = $builder->build_object(
343 class => 'Koha::Items',
345 biblionumber => $biblio->biblionumber,
352 my $patron = $builder->build_object({class => 'Koha::Patrons'});
353 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
355 # Mark the item as lost
356 ModItem({itemlost => 1}, $biblio->biblionumber, $item->itemnumber);
358 t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 1);
359 my ( $doreturn, $messages, $issue ) = AddReturn($item->barcode);
360 is( $doreturn, 0, "With BlockReturnOfLostItems, a checkin of a lost item should be blocked");
361 is( $messages->{WasLost}, 1, "... and the WasLost flag should be set");
363 $item->discard_changes;
364 is( $item->itemlost, 1, "Item remains lost" );
366 t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0);
367 ( $doreturn, $messages, $issue ) = AddReturn($item->barcode);
368 is( $doreturn, 1, "Without BlockReturnOfLostItems, a checkin of a lost item should not be blocked");