View | Details | Raw Unified | Return to bug 17569
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Patrons.t (-1 / +75 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 12;
22
use Test::More tests => 13;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use C4::Members;
25
use C4::Members;
Lines 343-348 subtest 'add_enrolment_fee_if_needed' => sub { Link Here
343
    $patron->delete;
343
    $patron->delete;
344
};
344
};
345
345
346
subtest 'search_upcoming_membership_expires' => sub {
347
    plan tests => 9;
348
349
    my $expiry_days = 15;
350
    t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days );
351
    my $nb_of_days_before = 1;
352
    my $nb_of_days_after = 2;
353
354
    my $builder = t::lib::TestBuilder->new();
355
356
    my $library = $builder->build({ source => 'Branch' });
357
358
    # before we add borrowers to this branch, add the expires we have now
359
    # note that this pertains to the current mocked setting of the pref
360
    # for this reason we add the new branchcode to most of the tests
361
    my $nb_of_expires = Koha::Patrons->search_upcoming_membership_expires->count;
362
363
    my $patron_1 = $builder->build({
364
        source => 'Borrower',
365
        value  => {
366
            branchcode              => $library->{branchcode},
367
            dateexpiry              => dt_from_string->add( days => $expiry_days )
368
        },
369
    });
370
371
    my $patron_2 = $builder->build({
372
        source => 'Borrower',
373
        value  => {
374
            branchcode              => $library->{branchcode},
375
            dateexpiry              => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
376
        },
377
    });
378
379
    my $patron_3 = $builder->build({
380
        source => 'Borrower',
381
        value  => {
382
            branchcode              => $library->{branchcode},
383
            dateexpiry              => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
384
        },
385
    });
386
387
    # Test without extra parameters
388
    my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires();
389
    is( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
390
391
    # Test with branch
392
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
393
    is( $upcoming_mem_expires->count, 1, );
394
    my $expired = $upcoming_mem_expires->next;
395
    is( $expired->surname, $patron_1->{surname}, 'Get upcoming membership expires should return the correct patron.' );
396
    is( $expired->library->branchemail, $library->{branchemail}, 'Get upcoming membership expires should return the correct patron.' );
397
    is( $expired->branchcode, $patron_1->{branchcode}, 'Get upcoming membership expires should return the correct patron.' );
398
399
    t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
400
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
401
    is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
402
403
    # Test MembershipExpiryDaysNotice == undef
404
    t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
405
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
406
    is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
407
408
    # Test the before parameter
409
    t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
410
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before });
411
    # Expect 29/6 and 30/6
412
    is( $upcoming_mem_expires->count, 2, 'Expect two results for before==1');
413
    # Test after parameter also
414
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after });
415
    # Expect 29/6, 30/6 and 2/7
416
    is( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
417
    Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
418
};
419
346
$retrieved_patron_1->delete;
420
$retrieved_patron_1->delete;
347
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
421
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
348
422
(-)a/t/db_dependent/Members/GetUpcomingMembershipExpires.t (-104 lines)
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
- 

Return to bug 17569