3 # Tests for C4::Members::GetUpcomingMembershipExpires
5 # This file is part of Koha.
7 # Copyright 2015 Biblibre
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.
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.
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>.
25 use Test::More tests => 6;
27 use C4::Members qw|GetUpcomingMembershipExpires|;
29 use t::lib::TestBuilder;
30 use t::lib::Mocks qw( mock_preference );
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $date_time = new Test::MockModule('DateTime');
45 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
47 my $builder = t::lib::TestBuilder->new();
52 description => 'Adult',
53 enrolmentperiod => 18,
59 my $branch = $builder->build({
62 branchname => 'My branch',
65 my $branchcode = $branch->{branchcode};
66 # before we add borrowers to this branch, add the expires we have now
67 # note that this pertains to the current mocked setting of the pref
68 # for this reason we add the new branchcode to most of the tests
69 my $expires = scalar @{ GetUpcomingMembershipExpires() };
74 firstname => 'Vincent',
76 cardnumber => '80808081',
78 branchcode => $branchcode,
79 dateexpiry => '2015-06-30'
86 firstname => 'Claude',
88 cardnumber => '80808082',
90 branchcode => $branchcode,
91 dateexpiry => '2015-06-29'
98 firstname => 'Gilles',
100 cardnumber => '80808083',
101 categorycode => 'AD',
102 branchcode => $branchcode,
103 dateexpiry => '2015-07-02'
107 # Test without extra parameters
108 my $upcoming_mem_expires = GetUpcomingMembershipExpires();
109 is( scalar(@$upcoming_mem_expires), $expires + 1, 'Get upcoming membership expires should return one new borrower.' );
112 $upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
113 is( @$upcoming_mem_expires==1 && $upcoming_mem_expires->[0]{surname} eq 'Martin',1 , 'Get upcoming membership expires should return borrower "Martin".' );
115 # Test MembershipExpiryDaysNotice == 0
116 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
117 $upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
118 is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
120 # Test MembershipExpiryDaysNotice == undef
121 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
122 $upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
123 is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
125 # Test the before parameter
126 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
127 $upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode, before => 1 });
128 # Expect 29/6 and 30/6
129 is( scalar(@$upcoming_mem_expires), 2, 'Expect two results for before==1');
130 # Test after parameter also
131 $upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode, before => 1, after => 2 });
132 # Expect 29/6, 30/6 and 2/7
133 is( scalar(@$upcoming_mem_expires), 3, 'Expect three results when adding after' );
136 $schema->storage->txn_rollback;