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

(-)a/t/db_dependent/Holds/CancelReserves.t (-100 lines)
Lines 1-100 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use C4::Reserves;
6
use Koha::DateUtils;
7
8
use t::lib::Mocks;
9
use t::lib::TestBuilder;
10
11
use Test::More tests => 5;
12
13
use_ok('C4::Reserves');
14
15
my $schema  = Koha::Database->new->schema;
16
$schema->storage->txn_begin;
17
my $builder = t::lib::TestBuilder->new();
18
19
t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0);
20
# Waiting reservers could be canceled only if ExpireReservesMaxPickUpDelay is set to "allow", see bug 19260
21
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
22
23
my $today = dt_from_string();
24
my $reserve_reservedate = $today->clone;
25
$reserve_reservedate->subtract(days => 30);
26
27
my $reserve1_expirationdate = $today->clone;
28
$reserve1_expirationdate->add(days => 1);
29
30
# Reserve not expired
31
my $reserve1 = $builder->build({
32
    source => 'Reserve',
33
    value => {
34
        reservedate => $reserve_reservedate,
35
        expirationdate => $reserve1_expirationdate,
36
        cancellationdate => undef,
37
        priority => 0,
38
        found => 'W',
39
    },
40
});
41
42
CancelExpiredReserves();
43
my $r1 = Koha::Holds->find($reserve1->{reserve_id});
44
ok($r1, 'Reserve 1 should not be canceled.');
45
46
my $reserve2_expirationdate = $today->clone;
47
$reserve2_expirationdate->subtract(days => 1);
48
49
# Reserve expired
50
my $reserve2 = $builder->build({
51
    source => 'Reserve',
52
    value => {
53
        reservedate => $reserve_reservedate,
54
        expirationdate => $reserve2_expirationdate,
55
        cancellationdate => undef,
56
        priority => 0,
57
        found => 'W',
58
    },
59
});
60
61
CancelExpiredReserves();
62
my $r2 = Koha::Holds->find($reserve2->{reserve_id});
63
is($r2, undef,'reserve 2 should be canceled.');
64
65
# Reserve expired on holiday
66
my $reserve3 = $builder->build({
67
    source => 'Reserve',
68
    value => {
69
        reservedate => $reserve_reservedate,
70
        expirationdate => $reserve2_expirationdate,
71
        branchcode => 'LIB1',
72
        cancellationdate => undef,
73
        priority => 0,
74
        found => 'W',
75
    },
76
});
77
78
Koha::Caches->get_instance()->flush_all();
79
my $holiday = $builder->build({
80
    source => 'SpecialHoliday',
81
    value => {
82
        branchcode => 'LIB1',
83
        day => $today->day,
84
        month => $today->month,
85
        year => $today->year,
86
        title => 'My holiday',
87
        isexception => 0
88
    },
89
});
90
91
CancelExpiredReserves();
92
my $r3 = Koha::Holds->find($reserve3->{reserve_id});
93
ok($r3,'Reserve 3 should not be canceled.');
94
95
t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
96
CancelExpiredReserves();
97
$r3 = Koha::Holds->find($reserve3->{reserve_id});
98
is($r3, undef,'Reserve 3 should be canceled.');
99
100
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Reserves.t (-62 / +1 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 59;
20
use Test::More tests => 58;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 692-758 subtest '_koha_notify_reserve() tests' => sub { Link Here
692
692
693
};
693
};
694
694
695
subtest 'CancelExpiredReserves tests' => sub {
696
    plan tests => 2;
697
698
    $dbh->do('DELETE FROM reserves');
699
700
    my $category = $builder->build({ source => 'Category' });
701
    my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
702
703
    my $borrowernumber = AddMember(
704
        firstname =>  'my firstname',
705
        surname => 'my surname',
706
        categorycode => $category->{categorycode},
707
        branchcode => $branchcode,
708
    );
709
710
    my $resdate = dt_from_string->add( days => -20 );
711
    my $expdate = dt_from_string->add( days => -2 );
712
    my $notexpdate = dt_from_string->add( days => 2 );
713
714
    my $hold1 = Koha::Hold->new({
715
        branchcode => $branchcode,
716
        borrowernumber => $borrowernumber,
717
        biblionumber => $bibnum,
718
        priority => 1,
719
        reservedate => $resdate,
720
        expirationdate => $notexpdate,
721
        found => undef
722
    })->store;
723
724
    my $hold2 = Koha::Hold->new({
725
        branchcode => $branchcode,
726
        borrowernumber => $borrowernumber,
727
        biblionumber => $bibnum,
728
        priority => 2,
729
        reservedate => $resdate,
730
        expirationdate => $expdate,
731
        found => undef
732
    })->store;
733
734
    my $hold3 = Koha::Hold->new({
735
        branchcode => $branchcode,
736
        borrowernumber => $borrowernumber,
737
        biblionumber => $bibnum,
738
        itemnumber => $itemnumber,
739
        priority => 0,
740
        reservedate => $resdate,
741
        expirationdate => $expdate,
742
        found => 'W'
743
    })->store;
744
745
    t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 0 );
746
    CancelExpiredReserves();
747
    my $count1 = Koha::Holds->search->count;
748
    is( $count1, 2, 'Only the non-waiting expired holds should be cancelled');
749
750
    t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 );
751
    CancelExpiredReserves();
752
    my $count2 = Koha::Holds->search->count;
753
    is( $count2, 1, 'Also the waiting expired hold should be cancelled now');
754
};
755
756
sub count_hold_print_messages {
695
sub count_hold_print_messages {
757
    my $message_count = $dbh->selectall_arrayref(q{
696
    my $message_count = $dbh->selectall_arrayref(q{
758
        SELECT COUNT(*)
697
        SELECT COUNT(*)
(-)a/t/db_dependent/Reserves/CancelExpiredReserves.t (-1 / +166 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More tests => 2;
5
6
use t::lib::Mocks;
7
use t::lib::TestBuilder;
8
9
use C4::Members;
10
use C4::Reserves;
11
use Koha::Database;
12
use Koha::DateUtils;
13
use Koha::Holds;
14
15
my $schema = Koha::Database->new->schema;
16
$schema->storage->txn_begin;
17
18
subtest 'CancelExpiredReserves tests incl. holidays' => sub {
19
    plan tests => 4;
20
21
    my $builder = t::lib::TestBuilder->new();
22
23
    t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0);
24
    # Waiting reservers could be canceled only if ExpireReservesMaxPickUpDelay is set to "allow", see bug 19260
25
    t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
26
27
28
    my $today = dt_from_string();
29
    my $reserve_reservedate = $today->clone;
30
    $reserve_reservedate->subtract(days => 30);
31
32
    my $reserve1_expirationdate = $today->clone;
33
    $reserve1_expirationdate->add(days => 1);
34
35
    # Reserve not expired
36
    my $reserve1 = $builder->build({
37
        source => 'Reserve',
38
        value => {
39
            reservedate => $reserve_reservedate,
40
            expirationdate => $reserve1_expirationdate,
41
            cancellationdate => undef,
42
            priority => 0,
43
            found => 'W',
44
        },
45
    });
46
47
    CancelExpiredReserves();
48
    my $r1 = Koha::Holds->find($reserve1->{reserve_id});
49
    ok($r1, 'Reserve 1 should not be canceled.');
50
51
    my $reserve2_expirationdate = $today->clone;
52
    $reserve2_expirationdate->subtract(days => 1);
53
54
    # Reserve expired
55
    my $reserve2 = $builder->build({
56
        source => 'Reserve',
57
        value => {
58
            reservedate => $reserve_reservedate,
59
            expirationdate => $reserve2_expirationdate,
60
            cancellationdate => undef,
61
            priority => 0,
62
            found => 'W',
63
        },
64
    });
65
66
    CancelExpiredReserves();
67
    my $r2 = Koha::Holds->find($reserve2->{reserve_id});
68
    is($r2, undef,'reserve 2 should be canceled.');
69
70
    # Reserve expired on holiday
71
    my $reserve3 = $builder->build({
72
        source => 'Reserve',
73
        value => {
74
            reservedate => $reserve_reservedate,
75
            expirationdate => $reserve2_expirationdate,
76
            branchcode => 'LIB1',
77
            cancellationdate => undef,
78
            priority => 0,
79
            found => 'W',
80
        },
81
    });
82
83
    Koha::Caches->get_instance()->flush_all();
84
    my $holiday = $builder->build({
85
        source => 'SpecialHoliday',
86
        value => {
87
            branchcode => 'LIB1',
88
            day => $today->day,
89
            month => $today->month,
90
            year => $today->year,
91
            title => 'My holiday',
92
            isexception => 0
93
        },
94
    });
95
96
    CancelExpiredReserves();
97
    my $r3 = Koha::Holds->find($reserve3->{reserve_id});
98
    ok($r3,'Reserve 3 should not be canceled.');
99
100
    t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
101
    CancelExpiredReserves();
102
    $r3 = Koha::Holds->find($reserve3->{reserve_id});
103
    is($r3, undef,'Reserve 3 should be canceled.');
104
};
105
106
subtest 'Test handling of waiting reserves by CancelExpiredReserves' => sub {
107
    plan tests => 2;
108
109
    Koha::Holds->delete;
110
111
    my $builder = t::lib::TestBuilder->new();
112
    my $category = $builder->build({ source => 'Category' });
113
    my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
114
    my $biblio = $builder->build({ source => 'Biblio' });
115
    my $bibnum = $biblio->{biblionumber};
116
    my $item = $builder->build({ source => 'Item', value => { biblionumber => $bibnum }});
117
    my $itemnumber = $item->{itemnumber};
118
    my $borrowernumber = $builder->build({ source => 'Borrower', value => { categorycode => $category->{categorycode}, branchcode => $branchcode }})->{borrowernumber};
119
120
    my $resdate = dt_from_string->add( days => -20 );
121
    my $expdate = dt_from_string->add( days => -2 );
122
    my $notexpdate = dt_from_string->add( days => 2 );
123
124
    my $hold1 = Koha::Hold->new({
125
        branchcode => $branchcode,
126
        borrowernumber => $borrowernumber,
127
        biblionumber => $bibnum,
128
        priority => 1,
129
        reservedate => $resdate,
130
        expirationdate => $notexpdate,
131
        found => undef,
132
    })->store;
133
134
    my $hold2 = Koha::Hold->new({
135
        branchcode => $branchcode,
136
        borrowernumber => $borrowernumber,
137
        biblionumber => $bibnum,
138
        priority => 2,
139
        reservedate => $resdate,
140
        expirationdate => $expdate,
141
        found => undef,
142
    })->store;
143
144
    my $hold3 = Koha::Hold->new({
145
        branchcode => $branchcode,
146
        borrowernumber => $borrowernumber,
147
        biblionumber => $bibnum,
148
        itemnumber => $itemnumber,
149
        priority => 0,
150
        reservedate => $resdate,
151
        expirationdate => $expdate,
152
        found => 'W',
153
    })->store;
154
155
    t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 0 );
156
    CancelExpiredReserves();
157
    my $count1 = Koha::Holds->search->count;
158
    is( $count1, 2, 'Only the non-waiting expired holds should be cancelled');
159
160
    t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 );
161
    CancelExpiredReserves();
162
    my $count2 = Koha::Holds->search->count;
163
    is( $count2, 1, 'Also the waiting expired hold should be cancelled now');
164
};
165
166
$schema->storage->txn_rollback;

Return to bug 19437