Bug 14834: Make membership_expiry cronjob more flexible
[koha.git] / t / db_dependent / Members / GetUpcomingMembershipExpires.t
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 t::lib::TestBuilder;
30 use t::lib::Mocks qw( mock_preference );
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $date_time = new Test::MockModule('DateTime');
36 $date_time->mock(
37     'now', sub {
38         return DateTime->new(
39             year      => 2015,
40             month     => 6,
41             day       => 15,
42         );
43 });
44
45 t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
46
47 my $builder = t::lib::TestBuilder->new();
48 $builder->build({
49     source => 'Category',
50     value  => {
51         categorycode            => 'AD',
52         description             => 'Adult',
53         enrolmentperiod         => 18,
54         upperagelimit           => 99,
55         category_type           => 'A',
56     },
57 });
58
59 my $branch = $builder->build({
60     source => 'Branch',
61     value  => {
62         branchname              => 'My branch',
63     },
64 });
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() };
70
71 $builder->build({
72     source => 'Borrower',
73     value  => {
74         firstname               => 'Vincent',
75         surname                 => 'Martin',
76         cardnumber              => '80808081',
77         categorycode            => 'AD',
78         branchcode              => $branchcode,
79         dateexpiry              => '2015-06-30'
80     },
81 });
82
83 $builder->build({
84     source => 'Borrower',
85     value  => {
86         firstname               => 'Claude',
87         surname                 => 'Dupont',
88         cardnumber              => '80808082',
89         categorycode            => 'AD',
90         branchcode              => $branchcode,
91         dateexpiry              => '2015-06-29'
92     },
93 });
94
95 $builder->build({
96     source => 'Borrower',
97     value  => {
98         firstname               => 'Gilles',
99         surname                 => 'Dupond',
100         cardnumber              => '80808083',
101         categorycode            => 'AD',
102         branchcode              => $branchcode,
103         dateexpiry              => '2015-07-02'
104     },
105 });
106
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.' );
110
111 # Test with branch
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".' );
114
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.' );
119
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.' );
124
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' );
134
135 # End
136 $schema->storage->txn_rollback;