Koha/t/db_dependent/Members/GetUpcomingMembershipExpires.t
Marcel de Rooy 51be8ecd9d Bug 14834: Make membership_expiry cronjob more flexible
This patch adds three parameters to the cron job: -before and -after, and
-branch.

You can run the cronjob now in an adjusted frequency: say once a week with
before 6 or after 6 (not both together). If your pref is set to 14, running
before=6 will include expiries from 8 days to 14 days ahead. When you
use after=6, you would include 14 days to 20 days ahead, etc.

You could also rerun the job of yesterday by setting before=1 and after=-1;
this could help in case of problem recovery.

Obviously, the branch parameter can be used as a filter.

NOTE: Why are these parameters passed only via the command line?
Well, obviously the branch parameter is not suitable for a pref.
The before/after parameter allows you to handle expiry mails different from
the normal scheme or could be used in some sort of recovery. In those cases
it will be more practical to use a command line parameter than editing a
pref.

NOTE: The unit test has been adjusted for the above reasons, but I also
added some lines to let existing expires not interfere with the added
borrowers by an additional count and using the branchcode parameter.

Test plan:
[1] Run the adjusted unit test GetUpcomingMembershipExpires.t
[2] Set the expiry date for patron A to now+16 (with pref 14).
    Set the expiry date for patron B to now+11.
[3] Run the cronjob without range. You should not see A and B.
[4] Run the cronjob with before 3. You should see patron B.
[5] Run the cronjob with before 3 and after 2. You should see A and B.
[6] Repeat step 5 with a branchcode that does not exist. No patrons.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Work as described following test plan.
Test pass
No errors

New parameters work with one (-) or two(--) dashes, no problem
with that but convention suggest that 'long' options use two-dashes.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
2016-04-29 16:57:51 +00:00

136 lines
4.7 KiB
Perl

#!/usr/bin/perl
# Tests for C4::Members::GetUpcomingMembershipExpires
# This file is part of Koha.
#
# Copyright 2015 Biblibre
#
# 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::MockModule;
use Test::More tests => 6;
use C4::Members qw|GetUpcomingMembershipExpires|;
use Koha::Database;
use t::lib::TestBuilder;
use t::lib::Mocks qw( mock_preference );
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
my $date_time = new Test::MockModule('DateTime');
$date_time->mock(
'now', sub {
return DateTime->new(
year => 2015,
month => 6,
day => 15,
);
});
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
my $builder = t::lib::TestBuilder->new();
$builder->build({
source => 'Category',
value => {
categorycode => 'AD',
description => 'Adult',
enrolmentperiod => 18,
upperagelimit => 99,
category_type => 'A',
},
});
my $branch = $builder->build({
source => 'Branch',
value => {
branchname => 'My branch',
},
});
my $branchcode = $branch->{branchcode};
# before we add borrowers to this branch, add the expires we have now
# note that this pertains to the current mocked setting of the pref
# for this reason we add the new branchcode to most of the tests
my $expires = scalar @{ GetUpcomingMembershipExpires() };
$builder->build({
source => 'Borrower',
value => {
firstname => 'Vincent',
surname => 'Martin',
cardnumber => '80808081',
categorycode => 'AD',
branchcode => $branchcode,
dateexpiry => '2015-06-30'
},
});
$builder->build({
source => 'Borrower',
value => {
firstname => 'Claude',
surname => 'Dupont',
cardnumber => '80808082',
categorycode => 'AD',
branchcode => $branchcode,
dateexpiry => '2015-06-29'
},
});
$builder->build({
source => 'Borrower',
value => {
firstname => 'Gilles',
surname => 'Dupond',
cardnumber => '80808083',
categorycode => 'AD',
branchcode => $branchcode,
dateexpiry => '2015-07-02'
},
});
# Test without extra parameters
my $upcoming_mem_expires = GetUpcomingMembershipExpires();
is( scalar(@$upcoming_mem_expires), $expires + 1, 'Get upcoming membership expires should return one new borrower.' );
# Test with branch
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
is( @$upcoming_mem_expires==1 && $upcoming_mem_expires->[0]{surname} eq 'Martin',1 , 'Get upcoming membership expires should return borrower "Martin".' );
# Test MembershipExpiryDaysNotice == 0
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
# Test MembershipExpiryDaysNotice == undef
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
# Test the before parameter
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode, before => 1 });
# Expect 29/6 and 30/6
is( scalar(@$upcoming_mem_expires), 2, 'Expect two results for before==1');
# Test after parameter also
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode, before => 1, after => 2 });
# Expect 29/6, 30/6 and 2/7
is( scalar(@$upcoming_mem_expires), 3, 'Expect three results when adding after' );
# End
$schema->storage->txn_rollback;