|
Lines 1-103
Link Here
|
| 1 |
#!/usr/bin/perl |
|
|
| 2 |
|
| 3 |
# Tests for C4::Members::GetUpcomingMembershipExpires |
| 4 |
|
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Copyright 2015 Biblibre |
| 8 |
# |
| 9 |
# Koha is free software; you can redistribute it and/or modify it |
| 10 |
# under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation; either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# Koha is distributed in the hope that it will be useful, but |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU General Public License |
| 20 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 21 |
|
| 22 |
use Modern::Perl; |
| 23 |
|
| 24 |
use Test::MockModule; |
| 25 |
use Test::More tests => 6; |
| 26 |
|
| 27 |
use C4::Members qw|GetUpcomingMembershipExpires|; |
| 28 |
use Koha::Database; |
| 29 |
use Koha::DateUtils; |
| 30 |
use t::lib::TestBuilder; |
| 31 |
use t::lib::Mocks qw( mock_preference ); |
| 32 |
|
| 33 |
my $schema = Koha::Database->new->schema; |
| 34 |
$schema->storage->txn_begin; |
| 35 |
|
| 36 |
my $expiry_days = 15; |
| 37 |
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days ); |
| 38 |
my $nb_of_days_before = 1; |
| 39 |
my $nb_of_days_after = 2; |
| 40 |
|
| 41 |
my $builder = t::lib::TestBuilder->new(); |
| 42 |
|
| 43 |
my $library = $builder->build({ source => 'Branch' }); |
| 44 |
|
| 45 |
# before we add borrowers to this branch, add the expires we have now |
| 46 |
# note that this pertains to the current mocked setting of the pref |
| 47 |
# for this reason we add the new branchcode to most of the tests |
| 48 |
my $expires = scalar @{ GetUpcomingMembershipExpires() }; |
| 49 |
|
| 50 |
my $patron_1 = $builder->build({ |
| 51 |
source => 'Borrower', |
| 52 |
value => { |
| 53 |
branchcode => $library->{branchcode}, |
| 54 |
dateexpiry => dt_from_string->add( days => $expiry_days ) |
| 55 |
}, |
| 56 |
}); |
| 57 |
|
| 58 |
my $patron_2 = $builder->build({ |
| 59 |
source => 'Borrower', |
| 60 |
value => { |
| 61 |
branchcode => $library->{branchcode}, |
| 62 |
dateexpiry => dt_from_string->add( days => $expiry_days - $nb_of_days_before ) |
| 63 |
}, |
| 64 |
}); |
| 65 |
|
| 66 |
my $patron_3 = $builder->build({ |
| 67 |
source => 'Borrower', |
| 68 |
value => { |
| 69 |
branchcode => $library->{branchcode}, |
| 70 |
dateexpiry => dt_from_string->add( days => $expiry_days + $nb_of_days_after ) |
| 71 |
}, |
| 72 |
}); |
| 73 |
|
| 74 |
# Test without extra parameters |
| 75 |
my $upcoming_mem_expires = GetUpcomingMembershipExpires(); |
| 76 |
is( scalar(@$upcoming_mem_expires), $expires + 1, 'Get upcoming membership expires should return one new borrower.' ); |
| 77 |
|
| 78 |
# Test with branch |
| 79 |
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $library->{branchcode} }); |
| 80 |
is( @$upcoming_mem_expires==1 && $upcoming_mem_expires->[0]{surname} eq $patron_1->{surname},1 , 'Get upcoming membership expires should return the correct patron.' ); |
| 81 |
|
| 82 |
# Test MembershipExpiryDaysNotice == 0 |
| 83 |
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 ); |
| 84 |
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $library->{branchcode} }); |
| 85 |
is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' ); |
| 86 |
|
| 87 |
# Test MembershipExpiryDaysNotice == undef |
| 88 |
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef ); |
| 89 |
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $library->{branchcode} }); |
| 90 |
is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' ); |
| 91 |
|
| 92 |
# Test the before parameter |
| 93 |
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 ); |
| 94 |
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $library->{branchcode}, before => $nb_of_days_before }); |
| 95 |
# Expect 29/6 and 30/6 |
| 96 |
is( scalar(@$upcoming_mem_expires), 2, 'Expect two results for before==1'); |
| 97 |
# Test after parameter also |
| 98 |
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after }); |
| 99 |
# Expect 29/6, 30/6 and 2/7 |
| 100 |
is( scalar(@$upcoming_mem_expires), 3, 'Expect three results when adding after' ); |
| 101 |
|
| 102 |
# End |
| 103 |
$schema->storage->txn_rollback; |
| 104 |
- |