Jonathan Druart
9d6d641d1f
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>
265 lines
9.1 KiB
Perl
Executable file
265 lines
9.1 KiB
Perl
Executable file
#!/usr/bin/perl
|
|
|
|
use Modern::Perl;
|
|
use C4::Context;
|
|
use DateTime;
|
|
use Koha::Database;
|
|
use Koha::DateUtils;
|
|
use Koha::CirculationRules;
|
|
use Koha::Library;
|
|
|
|
use t::lib::TestBuilder;
|
|
|
|
use Test::More tests => 9;
|
|
|
|
BEGIN {
|
|
use_ok('C4::Circulation', qw( GetHardDueDate GetLoanLength ));
|
|
}
|
|
can_ok(
|
|
'C4::Circulation',
|
|
qw(
|
|
GetHardDueDate
|
|
GetLoanLength
|
|
)
|
|
);
|
|
|
|
my $schema = Koha::Database->new->schema;
|
|
$schema->storage->txn_begin;
|
|
my $dbh = C4::Context->dbh;
|
|
|
|
$dbh->do(q|DELETE FROM issues|);
|
|
$dbh->do(q|DELETE FROM items|);
|
|
$dbh->do(q|DELETE FROM borrowers|);
|
|
$dbh->do(q|DELETE FROM edifact_ean|);
|
|
$dbh->do(q|DELETE FROM branches|);
|
|
$dbh->do(q|DELETE FROM categories|);
|
|
$dbh->do(q|DELETE FROM circulation_rules|);
|
|
|
|
#Add sample datas
|
|
|
|
#Add branch and category
|
|
my $samplebranch1 = {
|
|
branchcode => 'SAB1',
|
|
branchname => 'Sample Branch',
|
|
branchaddress1 => 'sample adr1',
|
|
branchaddress2 => 'sample adr2',
|
|
branchaddress3 => 'sample adr3',
|
|
branchzip => 'sample zip',
|
|
branchcity => 'sample city',
|
|
branchstate => 'sample state',
|
|
branchcountry => 'sample country',
|
|
branchphone => 'sample phone',
|
|
branchfax => 'sample fax',
|
|
branchemail => 'sample email',
|
|
branchurl => 'sample url',
|
|
branchip => 'sample ip',
|
|
opac_info => 'sample opac',
|
|
};
|
|
my $samplebranch2 = {
|
|
branchcode => 'SAB2',
|
|
branchname => 'Sample Branch2',
|
|
branchaddress1 => 'sample adr1_2',
|
|
branchaddress2 => 'sample adr2_2',
|
|
branchaddress3 => 'sample adr3_2',
|
|
branchzip => 'sample zip2',
|
|
branchcity => 'sample city2',
|
|
branchstate => 'sample state2',
|
|
branchcountry => 'sample country2',
|
|
branchphone => 'sample phone2',
|
|
branchfax => 'sample fax2',
|
|
branchemail => 'sample email2',
|
|
branchurl => 'sample url2',
|
|
branchip => 'sample ip2',
|
|
opac_info => 'sample opac2',
|
|
};
|
|
Koha::Library->new($samplebranch1)->store;
|
|
Koha::Library->new($samplebranch2)->store;
|
|
|
|
my $samplecat = {
|
|
categorycode => 'CAT1',
|
|
description => 'Description1',
|
|
enrolmentperiod => undef,
|
|
enrolmentperioddate => undef,
|
|
dateofbirthrequired => undef,
|
|
finetype => undef,
|
|
bulk => undef,
|
|
enrolmentfee => undef,
|
|
overduenoticerequired => undef,
|
|
issuelimit => undef,
|
|
reservefee => undef,
|
|
hidelostitems => 0,
|
|
category_type => 'A',
|
|
};
|
|
my $query =
|
|
"INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
|
|
$dbh->do(
|
|
$query, {},
|
|
$samplecat->{categorycode}, $samplecat->{description},
|
|
$samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
|
|
$samplecat->{dateofbirthrequired}, $samplecat->{finetype},
|
|
$samplecat->{bulk}, $samplecat->{enrolmentfee},
|
|
$samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
|
|
$samplecat->{reservefee}, $samplecat->{hidelostitems},
|
|
$samplecat->{category_type}
|
|
);
|
|
|
|
my $builder = t::lib::TestBuilder->new;
|
|
my $sampleitemtype1 = $builder->build({ source => 'Itemtype' })->{itemtype};
|
|
my $sampleitemtype2 = $builder->build({ source => 'Itemtype' })->{itemtype};
|
|
|
|
#Begin Tests
|
|
|
|
my $default = {
|
|
issuelength => 0,
|
|
renewalperiod => 0,
|
|
lengthunit => 'days'
|
|
};
|
|
|
|
#Test get_effective_rules
|
|
my $sampleissuingrule1 = {
|
|
branchcode => $samplebranch1->{branchcode},
|
|
categorycode => $samplecat->{categorycode},
|
|
itemtype => $sampleitemtype1,
|
|
rules => {
|
|
finedays => 0,
|
|
lengthunit => 'days',
|
|
renewalperiod => 5,
|
|
norenewalbefore => 6,
|
|
auto_renew => 0,
|
|
issuelength => 5,
|
|
chargeperiod => 0,
|
|
chargeperiod_charge_at => 0,
|
|
rentaldiscount => 2,
|
|
reservesallowed => 0,
|
|
hardduedate => '2013-01-01',
|
|
fine => 0,
|
|
hardduedatecompare => 5,
|
|
overduefinescap => 0,
|
|
renewalsallowed => 0,
|
|
firstremind => 0,
|
|
maxsuspensiondays => 0,
|
|
onshelfholds => 0,
|
|
opacitemholds => 'N',
|
|
cap_fine_to_replacement_price => 0,
|
|
holds_per_record => 1,
|
|
article_requests => 'yes',
|
|
no_auto_renewal_after => undef,
|
|
no_auto_renewal_after_hard_limit => undef,
|
|
suspension_chargeperiod => 1,
|
|
holds_per_day => undef,
|
|
}
|
|
};
|
|
my $sampleissuingrule2 = {
|
|
branchcode => $samplebranch2->{branchcode},
|
|
categorycode => $samplecat->{categorycode},
|
|
itemtype => $sampleitemtype1,
|
|
rules => {
|
|
renewalsallowed => 0,
|
|
renewalperiod => 2,
|
|
norenewalbefore => 7,
|
|
auto_renew => 0,
|
|
reservesallowed => 0,
|
|
issuelength => 2,
|
|
lengthunit => 'days',
|
|
hardduedate => 2,
|
|
hardduedatecompare => undef,
|
|
fine => undef,
|
|
finedays => undef,
|
|
firstremind => undef,
|
|
chargeperiod => undef,
|
|
chargeperiod_charge_at => 0,
|
|
rentaldiscount => 2.00,
|
|
overduefinescap => undef,
|
|
maxsuspensiondays => 0,
|
|
onshelfholds => 1,
|
|
opacitemholds => 'Y',
|
|
cap_fine_to_replacement_price => 0,
|
|
holds_per_record => 1,
|
|
article_requests => 'yes',
|
|
}
|
|
};
|
|
my $sampleissuingrule3 = {
|
|
branchcode => $samplebranch1->{branchcode},
|
|
categorycode => $samplecat->{categorycode},
|
|
itemtype => $sampleitemtype2,
|
|
rules => {
|
|
renewalsallowed => 0,
|
|
renewalperiod => 3,
|
|
norenewalbefore => 8,
|
|
auto_renew => 0,
|
|
reservesallowed => 0,
|
|
issuelength => 3,
|
|
lengthunit => 'days',
|
|
hardduedate => 3,
|
|
hardduedatecompare => undef,
|
|
fine => undef,
|
|
finedays => undef,
|
|
firstremind => undef,
|
|
chargeperiod => undef,
|
|
chargeperiod_charge_at => 0,
|
|
rentaldiscount => 3.00,
|
|
overduefinescap => undef,
|
|
maxsuspensiondays => 0,
|
|
onshelfholds => 1,
|
|
opacitemholds => 'F',
|
|
cap_fine_to_replacement_price => 0,
|
|
holds_per_record => 1,
|
|
article_requests => 'yes',
|
|
}
|
|
};
|
|
|
|
Koha::CirculationRules->set_rules( $sampleissuingrule1 );
|
|
Koha::CirculationRules->set_rules( $sampleissuingrule2 );
|
|
Koha::CirculationRules->set_rules( $sampleissuingrule3 );
|
|
|
|
#Test GetLoanLength
|
|
is_deeply(
|
|
C4::Circulation::GetLoanLength(
|
|
$samplecat->{categorycode},
|
|
$sampleitemtype1, $samplebranch1->{branchcode}
|
|
),
|
|
{ issuelength => 5, lengthunit => 'days', renewalperiod => 5 },
|
|
"GetLoanLength"
|
|
);
|
|
|
|
is_deeply(
|
|
C4::Circulation::GetLoanLength(),
|
|
$default,
|
|
"Without parameters, GetLoanLength returns hardcoded values"
|
|
);
|
|
is_deeply(
|
|
C4::Circulation::GetLoanLength( -1, -1 ),
|
|
$default,
|
|
"With wrong parameters, GetLoanLength returns hardcoded values"
|
|
);
|
|
is_deeply(
|
|
C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
|
|
$default,
|
|
"With only one parameter, GetLoanLength returns hardcoded values"
|
|
); #NOTE : is that really what is expected?
|
|
is_deeply(
|
|
C4::Circulation::GetLoanLength( $samplecat->{categorycode}, $sampleitemtype1 ),
|
|
$default,
|
|
"With only two parameters, GetLoanLength returns hardcoded values"
|
|
); #NOTE : is that really what is expected?
|
|
is_deeply(
|
|
C4::Circulation::GetLoanLength( $samplecat->{categorycode}, $sampleitemtype1, $samplebranch1->{branchcode} ),
|
|
{
|
|
issuelength => 5,
|
|
renewalperiod => 5,
|
|
lengthunit => 'days',
|
|
},
|
|
"With the correct number of parameters, GetLoanLength returns the expected values"
|
|
);
|
|
|
|
#Test GetHardDueDate
|
|
my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
|
|
$sampleitemtype1, $samplebranch1->{branchcode} );
|
|
is_deeply(
|
|
\@hardduedate,
|
|
[
|
|
dt_from_string( $sampleissuingrule1->{rules}->{hardduedate}, 'iso' ),
|
|
$sampleissuingrule1->{rules}->{hardduedatecompare}
|
|
],
|
|
"GetHardDueDate returns the duedate and the duedatecompare"
|
|
);
|