Koha/t/db_dependent/Patron/Borrower_Discharge.t
David Gustafsson f726558510
Bug 32496: Reduce unnecessary unblessings of objects in Circulation.pm
Refactor the most performance critical subroutines in Circulation.pm
to take objects instead of unblessed ones to reduce unnecessary
unblessings and generally clean up the code.

To test:

1) Ensure the following tests all pass:
  t/db_dependent/Circulation.t
  t/db_dependent/Circulation/CalcDateDue.t
  t/db_dependent/Circulation/CheckIfIssuedToPatron.t
  t/db_dependent/Circulation/GetPendingOnSiteCheckouts.t
  t/db_dependent/Circulation/GetTopIssues.t
  t/db_dependent/Circulation/IsItemIssued.t
  t/db_dependent/Circulation/MarkIssueReturned.t
  t/db_dependent/Circulation/ReturnClaims.t
  t/db_dependent/Circulation/Returns.t
  t/db_dependent/Circulation/SwitchOnSiteCheckouts.t
  t/db_dependent/Circulation/TooMany.t
  t/db_dependent/Circulation/dateexpiry.t
  t/db_dependent/Circulation/issue.t
  t/db_dependent/Circulation/maxsuspensiondays.t
  t/db_dependent/Circulation/transferbook.t
  t/db_dependent/Circulation_holdsqueue.t
  t/db_dependent/DecreaseLoanHighHolds.t
  t/db_dependent/Holds/DisallowHoldIfItemsAvailable.t
  t/db_dependent/Holds/RevertWaitingStatus.t
  t/db_dependent/ILSDI_Services.t
  t/db_dependent/Illrequests.t
  t/db_dependent/Koha/Account/Line.t
  t/db_dependent/Koha/Biblio.t
  t/db_dependent/Koha/Items.t
  t/db_dependent/Koha/Object.t
  t/db_dependent/Koha/Patrons.t
  t/db_dependent/Koha/Pseudonymization.t
  t/db_dependent/Koha/Template/Plugin/CirculationRules.t
  t/db_dependent/Letters/TemplateToolkit.t
  t/db_dependent/Members/GetAllIssues.t
  t/db_dependent/Members/IssueSlip.t
  t/db_dependent/Patron/Borrower_Discharge.t
  t/db_dependent/Patron/Borrower_PrevCheckout.t
  t/db_dependent/SIP/ILS.t
  t/db_dependent/Holds.t
  t/db_dependent/Holds/LocalHoldsPriority.t
  t/db_dependent/Holds/HoldFulfillmentPolicy.t
  t/db_dependent/Holds/HoldItemtypeLimit.t
  t/db_dependent/Reserves/GetReserveFee.t
  t/db_dependent/api/v1/return_claims.t
  t/db_dependent/api/v1/biblios.t
  t/db_dependent/api/v1/checkouts.t
  t/db_dependent/Reserves.t
  t/db_dependent/HoldsQueue.t
  t/db_dependent/selenium/regressions.t
  t/db_dependent/Koha/Plugins/Circulation_hooks.t
  t/db_dependent/Koha/Plugins/Recall_hooks.t
  t/db_dependent/Koha/Recalls.t
  t/db_dependent/Koha/Recall.t
  t/db_dependent/Circulation/_CalculateAndUpdateFine.t

Sponsored-by: Gothenburg University Library

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2023-09-22 10:52:39 -03:00

177 lines
7.7 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 23;
use Test::MockModule;
use Test::Warn;
use IPC::Cmd qw(can_run);
use MARC::Record;
use C4::Circulation qw( AddIssue AddReturn );
use C4::Biblio qw( AddBiblio );
use C4::Context;
use Koha::Patrons;
use Koha::Patron::Discharge;
use Koha::Database;
use t::lib::TestBuilder;
use t::lib::Mocks;
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
my $builder = t::lib::TestBuilder->new;
my $dbh = C4::Context->dbh;
$dbh->do(q|DELETE FROM discharges|);
my $library = $builder->build({ source => 'Branch' });
my $another_library = $builder->build({ source => 'Branch' });
my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
C4::Context->_new_userenv('xxx');
my $patron = $builder->build_object({
class => 'Koha::Patrons',
value => {
branchcode => $library->{branchcode},
flags => 1, # superlibrarian
}
});
t::lib::Mocks::mock_userenv({ patron => $patron });
my $patron2 = $builder->build_object({
class => 'Koha::Patrons',
value => {
branchcode => $library->{branchcode},
}
});
my $patron3 = $builder->build_object({
class => 'Koha::Patrons',
value => {
branchcode => $another_library->{branchcode},
flags => undef,
}
});
# Discharge not possible with issues
my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
my $barcode = 'BARCODE42';
$builder->build_sample_item(
{
biblionumber => $biblionumber,
library => $library->{branchcode},
barcode => $barcode,
itype => $itemtype
}
);
AddIssue( $patron, $barcode );
is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }), 0, 'A patron with issues cannot be discharged' );
is( Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber }), undef, 'No request done if patron has issues' );
is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->borrowernumber }), undef, 'No discharge done if patron has issues' );
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' );
is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' );
AddReturn( $barcode );
# Discharge possible without issue
is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }), 1, 'A patron without issues can be discharged' );
is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
# Verify that the user is not discharged anymore if the restriction has been lifted
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->borrowernumber } );
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->borrowernumber } );
is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 1, 'The patron has been discharged' );
is( Koha::Patrons->find( $patron->borrowernumber )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
is( scalar( Koha::Patron::Discharge::get_validated ), 3, 'There are 3 validated discharges' );
is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->borrowernumber } ) ), 1, 'There is 1 validated discharge for a given patron' );
is( scalar( Koha::Patron::Discharge::get_validated( { branchcode => $library->{branchcode} } ) ), 2, 'There is 2 validated discharges for a given branchcode' ); # This is not used in the code yet
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->borrowernumber, 'type' => 'DISCHARGE' } );
ok( !Koha::Patrons->find( $patron->borrowernumber )->is_debarred, 'The debarment has been lifted' );
ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 'The patron is not discharged after the restriction has been lifted' );
# Verify that the discharge works multiple times
Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber });
is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)');
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
SKIP: {
skip "Skipping because weasyprint is not installed",
5 unless can_run('weasyprint');
isnt(
Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ),
undef,
"Temporary PDF generated."
);
my $mocked_ipc = Test::MockModule->new('IPC::Cmd');
$mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
my $result;
warning_is
{ $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
'Some error',
'Failed call to run() prints the generated error';
is( $result, undef, 'undef returned if failed run' );
$mocked_ipc->mock( 'can_run', undef );
warning_is
{ $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
'weasyprint not found!',
'Expected failure because of missing weasyprint';
is( $result, undef, 'undef returned if missing weasyprint' );
}
# FIXME Should be a Koha::Object object
is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
subtest 'search_limited' => sub {
plan tests => 4;
$dbh->do(q|DELETE FROM discharges|);
my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
# $patron and $patron2 are from the same library, $patron3 from another one
# Logged in user is $patron, superlibrarian
t::lib::Mocks::mock_userenv({ patron => $patron });
Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store();
Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron3->branchcode })->store();
Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber });
Koha::Patron::Discharge::request({ borrowernumber => $patron2->borrowernumber });
Koha::Patron::Discharge::request({ borrowernumber => $patron3->borrowernumber });
is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' );
is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
# With patron 3 logged in, only discharges from their group are visible
t::lib::Mocks::mock_userenv({ patron => $patron3 });
is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
};
$schema->storage->txn_rollback;