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 => 14;
22
use Test::More tests => 15;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use C4::Members;
25
use C4::Members;
Lines 368-373 subtest 'add_enrolment_fee_if_needed' => sub { Link Here
368
    $patron->delete;
368
    $patron->delete;
369
};
369
};
370
370
371
subtest 'search_upcoming_membership_expires' => sub {
372
    plan tests => 9;
373
374
    my $expiry_days = 15;
375
    t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', $expiry_days );
376
    my $nb_of_days_before = 1;
377
    my $nb_of_days_after = 2;
378
379
    my $builder = t::lib::TestBuilder->new();
380
381
    my $library = $builder->build({ source => 'Branch' });
382
383
    # before we add borrowers to this branch, add the expires we have now
384
    # note that this pertains to the current mocked setting of the pref
385
    # for this reason we add the new branchcode to most of the tests
386
    my $nb_of_expires = Koha::Patrons->search_upcoming_membership_expires->count;
387
388
    my $patron_1 = $builder->build({
389
        source => 'Borrower',
390
        value  => {
391
            branchcode              => $library->{branchcode},
392
            dateexpiry              => dt_from_string->add( days => $expiry_days )
393
        },
394
    });
395
396
    my $patron_2 = $builder->build({
397
        source => 'Borrower',
398
        value  => {
399
            branchcode              => $library->{branchcode},
400
            dateexpiry              => dt_from_string->add( days => $expiry_days - $nb_of_days_before )
401
        },
402
    });
403
404
    my $patron_3 = $builder->build({
405
        source => 'Borrower',
406
        value  => {
407
            branchcode              => $library->{branchcode},
408
            dateexpiry              => dt_from_string->add( days => $expiry_days + $nb_of_days_after )
409
        },
410
    });
411
412
    # Test without extra parameters
413
    my $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires();
414
    is( $upcoming_mem_expires->count, $nb_of_expires + 1, 'Get upcoming membership expires should return one new borrower.' );
415
416
    # Test with branch
417
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
418
    is( $upcoming_mem_expires->count, 1, );
419
    my $expired = $upcoming_mem_expires->next;
420
    is( $expired->surname, $patron_1->{surname}, 'Get upcoming membership expires should return the correct patron.' );
421
    is( $expired->library->branchemail, $library->{branchemail}, 'Get upcoming membership expires should return the correct patron.' );
422
    is( $expired->branchcode, $patron_1->{branchcode}, 'Get upcoming membership expires should return the correct patron.' );
423
424
    t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
425
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
426
    is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
427
428
    # Test MembershipExpiryDaysNotice == undef
429
    t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
430
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode} });
431
    is( $upcoming_mem_expires->count, 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
432
433
    # Test the before parameter
434
    t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
435
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before });
436
    # Expect 29/6 and 30/6
437
    is( $upcoming_mem_expires->count, 2, 'Expect two results for before==1');
438
    # Test after parameter also
439
    $upcoming_mem_expires = Koha::Patrons->search_upcoming_membership_expires({ 'me.branchcode' => $library->{branchcode}, before => $nb_of_days_before, after => $nb_of_days_after });
440
    # Expect 29/6, 30/6 and 2/7
441
    is( $upcoming_mem_expires->count, 3, 'Expect three results when adding after' );
442
    Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
443
};
444
371
$retrieved_patron_1->delete;
445
$retrieved_patron_1->delete;
372
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
446
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
373
447
(-)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