Koha/t/db_dependent/Circulation/transfers.t
Jonathan Druart 9d6d641d1f Bug 17600: Standardize our EXPORT_OK
On bug 17591 we discovered that there was something weird going on with
the way we export and use subroutines/modules.
This patch tries to standardize our EXPORT to use EXPORT_OK only.

That way we will need to explicitely define the subroutine we want to
use from a module.

This patch is a squashed version of:
Bug 17600: After export.pl
Bug 17600: After perlimport
Bug 17600: Manual changes
Bug 17600: Other manual changes after second perlimports run
Bug 17600: Fix tests

And a lot of other manual changes.

export.pl is a dirty script that can be found on bug 17600.

"perlimport" is:
git clone https://github.com/oalders/App-perlimports.git
cd App-perlimports/
cpanm --installdeps .
export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib"
find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \;

The ideas of this patch are to:
* use EXPORT_OK instead of EXPORT
* perltidy the EXPORT_OK list
* remove '&' before the subroutine names
* remove some uneeded use statements
* explicitely import the subroutines we need within the controllers or
modules

Note that the private subroutines (starting with _) should not be
exported (and not used from outside of the module except from tests).

EXPORT vs EXPORT_OK (from
https://www.thegeekstuff.com/2010/06/perl-exporter-examples/)
"""
Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members.

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.
"""

If this patch caused a conflict with a patch you wrote prior to its
push:
* Make sure you are not reintroducing a "use" statement that has been
removed
* "$subroutine" is not exported by the C4::$MODULE module
means that you need to add the subroutine to the @EXPORT_OK list
* Bareword "$subroutine" not allowed while "strict subs"
means that you didn't imported the subroutine from the module:
  - use $MODULE qw( $subroutine list );
You can also use the fully qualified namespace: C4::$MODULE::$subroutine

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-07-16 08:58:47 +02:00

255 lines
8.4 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 C4::Context;
use C4::Circulation qw( CreateBranchTransferLimit DeleteBranchTransferLimits GetTransfers GetTransfersFromTo TransferSlip );
use C4::Biblio qw( AddBiblio );
use C4::Items qw( ModItemTransfer );
use Koha::Database;
use Koha::DateUtils;
use DateTime::Duration;
use Koha::Item::Transfers;
use t::lib::TestBuilder;
use Test::More tests => 22;
use Test::Deep;
BEGIN {
use_ok('C4::Circulation', qw( CreateBranchTransferLimit DeleteBranchTransferLimits GetTransfers GetTransfersFromTo TransferSlip ));
}
can_ok(
'C4::Circulation',
qw(
CreateBranchTransferLimit
DeleteBranchTransferLimits
GetTransfers
GetTransfersFromTo
)
);
my $schema = Koha::Database->schema;
$schema->storage->txn_begin;
my $builder = t::lib::TestBuilder->new;
my $dbh = C4::Context->dbh;
$dbh->do(q|DELETE FROM issues|);
$dbh->do(q|DELETE FROM borrowers|);
$dbh->do(q|DELETE FROM items|);
$dbh->do(q|DELETE FROM branches|);
$dbh->do(q|DELETE FROM branch_transfer_limits|);
$dbh->do(q|DELETE FROM branchtransfers|);
## Create sample datas
# Add branches
my $branchcode_1 = $builder->build( { source => 'Branch', } )->{branchcode};
my $branchcode_2 = $builder->build( { source => 'Branch', } )->{branchcode};
# Add itemtype
my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
#Add biblio and items
my $record = MARC::Record->new();
$record->append_fields(
MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
my $item_id1 = Koha::Item->new(
{
biblionumber => $biblionumber,
barcode => 1,
itemcallnumber => 'callnumber1',
homebranch => $branchcode_1,
holdingbranch => $branchcode_1,
itype => $itemtype
},
)->store->itemnumber;
my $item_id2 = Koha::Item->new(
{
biblionumber => $biblionumber,
barcode => 2,
itemcallnumber => 'callnumber2',
homebranch => $branchcode_1,
holdingbranch => $branchcode_1,
itype => $itemtype
},
)->store->itemnumber;
my $item_id3 = Koha::Item->new(
{
biblionumber => $biblionumber,
barcode => 3,
itemcallnumber => 'callnumber3',
homebranch => $branchcode_1,
holdingbranch => $branchcode_1,
itype => $itemtype
},
)->store->itemnumber;
my $item_id4 = Koha::Item->new(
{
biblionumber => $biblionumber,
barcode => 4,
itemcallnumber => 'callnumber4',
homebranch => $branchcode_1,
holdingbranch => $branchcode_1,
itype => $itemtype
},
)->store->itemnumber;
#Add transfers
my $trigger = 'Manual';
ModItemTransfer(
$item_id1,
$branchcode_1,
$branchcode_2,
$trigger
);
my $item_obj = Koha::Items->find({ itemnumber => $item_id1 });
is( $item_obj->holdingbranch, $branchcode_1, "Item should be held at branch that initiates transfer");
ModItemTransfer(
$item_id2,
$branchcode_1,
$branchcode_2,
$trigger
);
# Add an "unsent" transfer for tests
ModItemTransfer(
$item_id3,
$branchcode_1,
$branchcode_2,
$trigger
);
my $transfer_requested = Koha::Item::Transfers->search( { itemnumber => $item_id3 }, { rows => 1 })->single;
$transfer_requested->set({ daterequested => dt_from_string, datesent => undef })->store;
# Add a "cancelled" transfer for tests
ModItemTransfer(
$item_id4,
$branchcode_1,
$branchcode_2,
$trigger
);
my $transfer_cancelled = Koha::Item::Transfers->search( { itemnumber => $item_id4 }, { rows => 1 })->single;
$transfer_cancelled->set( { daterequested => dt_from_string, datesent => undef, datecancelled => dt_from_string } )->store;
#Begin Tests
#Test CreateBranchTransferLimit
is(
CreateBranchTransferLimit(
$branchcode_2,
$branchcode_1, 'CODE'
),
1,
"A Branch TransferLimit has been added"
);
is(CreateBranchTransferLimit(),undef,
"Without parameters CreateBranchTransferLimit returns undef");
is(CreateBranchTransferLimit($branchcode_2),undef,
"With only tobranch CreateBranchTransferLimit returns undef");
is(CreateBranchTransferLimit(undef,$branchcode_2),undef,
"With only frombranch CreateBranchTransferLimit returns undef");
#FIXME: Currently, we can add a transferlimit even to nonexistent branches because in the database,
#branch_transfer_limits.toBranch and branch_transfer_limits.fromBranch aren't foreign keys
#is(CreateBranchTransferLimit(-1,-1,'CODE'),0,"With wrong CreateBranchTransferLimit returns 0 - No transfertlimit added");
#Test GetTransfers
my @transfers = GetTransfers($item_id1);
cmp_deeply(
\@transfers,
[ re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), $branchcode_1, $branchcode_2, re('[0-9]*'), re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), 'Manual' ],
"Transfers of the item1"
); #NOTE: Only the first transfer is returned
@transfers = GetTransfers;
is_deeply( \@transfers, [],
"GetTransfers without params returns an empty array" );
@transfers = GetTransfers(-1);
is_deeply( \@transfers, [],
"GetTransfers with a wrong item id returns an empty array" );
#Test GetTransfersFromTo
my @transferfrom1to2 = GetTransfersFromTo( $branchcode_1,
$branchcode_2 );
cmp_deeply(
\@transferfrom1to2,
[
{
branchtransfer_id => re('[0-9]*'),
itemnumber => $item_id1,
datesent => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
frombranch => $branchcode_1
},
{
branchtransfer_id => re('[0-9]*'),
itemnumber => $item_id2,
datesent => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
frombranch => $branchcode_1
}
],
"Item1 and Item2 has been transferred from branch1 to branch2"
);
my @transferto = GetTransfersFromTo( undef, $branchcode_2 );
is_deeply( \@transferto, [],
"GetTransfersfromTo without frombranch returns an empty array" );
my @transferfrom = GetTransfersFromTo( $branchcode_1 );
is_deeply( \@transferfrom, [],
"GetTransfersfromTo without tobranch returns an empty array" );
@transferfrom = GetTransfersFromTo();
is_deeply( \@transferfrom, [],
"GetTransfersfromTo without params returns an empty array" );
#Test DeleteBranchTransferLimits
is(
C4::Circulation::DeleteBranchTransferLimits( $branchcode_1 ),
1,
"A Branch TransferLimit has been deleted"
);
is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
is(C4::Circulation::DeleteBranchTransferLimits('B'),'0E0',"With a wrong id DeleteBranchTransferLimit returns 0E0");
#Test TransferSlip
is( C4::Circulation::TransferSlip($branchcode_1, undef, 5, $branchcode_2),
undef, "No tranferslip if invalid or undef itemnumber or barcode" );
is( C4::Circulation::TransferSlip($branchcode_1, $item_id1, 1, $branchcode_2)->{'code'},
'TRANSFERSLIP', "Get a transferslip on valid itemnumber and/or barcode" );
cmp_deeply(
C4::Circulation::TransferSlip($branchcode_1, $item_id1, undef, $branchcode_2),
C4::Circulation::TransferSlip($branchcode_1, undef, 1, $branchcode_2),
"Barcode and itemnumber for same item both generate same TransferSlip"
);
$dbh->do("DELETE FROM branchtransfers");
ModItemTransfer(
$item_id1,
$branchcode_1,
$branchcode_2,
$trigger
);
my $transfer = Koha::Item::Transfers->search()->next();
ModItemTransfer(
$item_id1,
$branchcode_1,
$branchcode_2,
$trigger
);
$transfer->{_result}->discard_changes;
ok( $transfer->datecancelled, 'Date cancelled is set when new transfer is initiated' );
is( $transfer->cancellation_reason, "Manual", 'Cancellation reason is set correctly when new transfer is initiated' );
$schema->storage->txn_rollback;