Bug 15081: (followup) Make test files using TestBuilder handle their transactions
[koha.git] / t / db_dependent / Members / GetUpcomingMembershipExpires.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2015 Biblibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 5;
23 use Test::MockModule;
24 use t::lib::TestBuilder;
25 use t::lib::Mocks qw( mock_preference );
26
27 use C4::Members;
28 use Koha::Database;
29
30 BEGIN {
31     use_ok('C4::Members');
32 }
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 my $date_time = new Test::MockModule('DateTime');
38 $date_time->mock(
39     'now', sub {
40         return DateTime->new(
41             year      => 2015,
42             month     => 6,
43             day       => 15,
44         );
45
46 });
47
48 t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', 15);
49
50 my $builder = t::lib::TestBuilder->new();
51 $builder->build({
52     source => 'Category',
53     value  => {
54         categorycode            => 'AD',
55         description             => 'Adult',
56         enrolmentperiod         => 18,
57         upperagelimit           => 99,
58         category_type           => 'A',
59     },
60 });
61
62 $builder->build({
63     source => 'Branch',
64     value  => {
65         branchcode              => 'CR',
66         branchname              => 'My branch',
67     },
68 });
69
70 $builder->build({
71     source => 'Borrower',
72     value  => {
73         firstname               => 'Vincent',
74         surname                 => 'Martin',
75         cardnumber              => '80808081',
76         categorycode            => 'AD',
77         branchcode              => 'CR',
78         dateexpiry              => '2015-06-30'
79     },
80 });
81
82 $builder->build({
83     source => 'Borrower',
84     value  => {
85         firstname               => 'Claude',
86         surname                 => 'Dupont',
87         cardnumber              => '80808082',
88         categorycode            => 'AD',
89         branchcode              => 'CR',
90         dateexpiry              => '2015-06-29'
91     },
92 });
93
94 $builder->build({
95     source => 'Borrower',
96     value  => {
97         firstname               => 'Gilles',
98         surname                 => 'Dupond',
99         cardnumber              => '80808083',
100         categorycode            => 'AD',
101         branchcode              => 'CR',
102         dateexpiry              => '2015-07-02'
103     },
104 });
105
106 my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
107 is(scalar(@$upcoming_mem_expires), 1, 'Get upcoming membership expires should return 1 borrower.');
108
109 is($upcoming_mem_expires->[0]{surname}, 'Martin', 'Get upcoming membership expires should return borrower "Martin".');
110
111 # Test GetUpcomingMembershipExpires() with MembershipExpiryDaysNotice == 0
112 t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', 0);
113
114 $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
115 is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with 0 MembershipExpiryDaysNotice should return 0.');
116
117 # Test GetUpcomingMembershipExpires() with MembershipExpiryDaysNotice == undef
118 t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', undef);
119
120 $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
121 is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should return 0.');
122
123 $schema->storage->txn_rollback;
124
125 1;