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

(-)a/t/db_dependent/Holds/CancelReserves.t (-98 lines)
Lines 1-98 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
21
my $today = dt_from_string();
22
my $reserve_reservedate = $today->clone;
23
$reserve_reservedate->subtract(days => 30);
24
25
my $reserve1_expirationdate = $today->clone;
26
$reserve1_expirationdate->add(days => 1);
27
28
# Reserve not expired
29
my $reserve1 = $builder->build({
30
    source => 'Reserve',
31
    value => {
32
        reservedate => $reserve_reservedate,
33
        expirationdate => $reserve1_expirationdate,
34
        cancellationdate => undef,
35
        priority => 0,
36
        found => 'W',
37
    },
38
});
39
40
CancelExpiredReserves();
41
my $r1 = Koha::Holds->find($reserve1->{reserve_id});
42
ok($r1, 'Reserve 1 should not be canceled.');
43
44
my $reserve2_expirationdate = $today->clone;
45
$reserve2_expirationdate->subtract(days => 1);
46
47
# Reserve expired
48
my $reserve2 = $builder->build({
49
    source => 'Reserve',
50
    value => {
51
        reservedate => $reserve_reservedate,
52
        expirationdate => $reserve2_expirationdate,
53
        cancellationdate => undef,
54
        priority => 0,
55
        found => 'W',
56
    },
57
});
58
59
CancelExpiredReserves();
60
my $r2 = Koha::Holds->find($reserve2->{reserve_id});
61
is($r2, undef,'reserve 2 should be canceled.');
62
63
# Reserve expired on holiday
64
my $reserve3 = $builder->build({
65
    source => 'Reserve',
66
    value => {
67
        reservedate => $reserve_reservedate,
68
        expirationdate => $reserve2_expirationdate,
69
        branchcode => 'LIB1',
70
        cancellationdate => undef,
71
        priority => 0,
72
        found => 'W',
73
    },
74
});
75
76
Koha::Caches->get_instance()->flush_all();
77
my $holiday = $builder->build({
78
    source => 'SpecialHoliday',
79
    value => {
80
        branchcode => 'LIB1',
81
        day => $today->day,
82
        month => $today->month,
83
        year => $today->year,
84
        title => 'My holiday',
85
        isexception => 0
86
    },
87
});
88
89
CancelExpiredReserves();
90
my $r3 = Koha::Holds->find($reserve3->{reserve_id});
91
ok($r3,'Reserve 3 should not be canceled.');
92
93
t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
94
CancelExpiredReserves();
95
$r3 = Koha::Holds->find($reserve3->{reserve_id});
96
is($r3, undef,'Reserve 3 should be canceled.');
97
98
$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 / +163 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
25
    my $today = dt_from_string();
26
    my $reserve_reservedate = $today->clone;
27
    $reserve_reservedate->subtract(days => 30);
28
29
    my $reserve1_expirationdate = $today->clone;
30
    $reserve1_expirationdate->add(days => 1);
31
32
    # Reserve not expired
33
    my $reserve1 = $builder->build({
34
        source => 'Reserve',
35
        value => {
36
            reservedate => $reserve_reservedate,
37
            expirationdate => $reserve1_expirationdate,
38
            cancellationdate => undef,
39
            priority => 0,
40
            found => 'W',
41
        },
42
    });
43
44
    CancelExpiredReserves();
45
    my $r1 = Koha::Holds->find($reserve1->{reserve_id});
46
    ok($r1, 'Reserve 1 should not be canceled.');
47
48
    my $reserve2_expirationdate = $today->clone;
49
    $reserve2_expirationdate->subtract(days => 1);
50
51
    # Reserve expired
52
    my $reserve2 = $builder->build({
53
        source => 'Reserve',
54
        value => {
55
            reservedate => $reserve_reservedate,
56
            expirationdate => $reserve2_expirationdate,
57
            cancellationdate => undef,
58
            priority => 0,
59
            found => 'W',
60
        },
61
    });
62
63
    CancelExpiredReserves();
64
    my $r2 = Koha::Holds->find($reserve2->{reserve_id});
65
    is($r2, undef,'reserve 2 should be canceled.');
66
67
    # Reserve expired on holiday
68
    my $reserve3 = $builder->build({
69
        source => 'Reserve',
70
        value => {
71
            reservedate => $reserve_reservedate,
72
            expirationdate => $reserve2_expirationdate,
73
            branchcode => 'LIB1',
74
            cancellationdate => undef,
75
            priority => 0,
76
            found => 'W',
77
        },
78
    });
79
80
    Koha::Caches->get_instance()->flush_all();
81
    my $holiday = $builder->build({
82
        source => 'SpecialHoliday',
83
        value => {
84
            branchcode => 'LIB1',
85
            day => $today->day,
86
            month => $today->month,
87
            year => $today->year,
88
            title => 'My holiday',
89
            isexception => 0
90
        },
91
    });
92
93
    CancelExpiredReserves();
94
    my $r3 = Koha::Holds->find($reserve3->{reserve_id});
95
    ok($r3,'Reserve 3 should not be canceled.');
96
97
    t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
98
    CancelExpiredReserves();
99
    $r3 = Koha::Holds->find($reserve3->{reserve_id});
100
    is($r3, undef,'Reserve 3 should be canceled.');
101
};
102
103
subtest 'Test handling of waiting reserves by CancelExpiredReserves' => sub {
104
    plan tests => 2;
105
106
    Koha::Holds->delete;
107
108
    my $builder = t::lib::TestBuilder->new();
109
    my $category = $builder->build({ source => 'Category' });
110
    my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
111
    my $biblio = $builder->build({ source => 'Biblio' });
112
    my $bibnum = $biblio->{biblionumber};
113
    my $item = $builder->build({ source => 'Item', value => { biblionumber => $bibnum }});
114
    my $itemnumber = $item->{itemnumber};
115
    my $borrowernumber = $builder->build({ source => 'Borrower', value => { categorycode => $category->{categorycode}, branchcode => $branchcode }})->{borrowernumber};
116
117
    my $resdate = dt_from_string->add( days => -20 );
118
    my $expdate = dt_from_string->add( days => -2 );
119
    my $notexpdate = dt_from_string->add( days => 2 );
120
121
    my $hold1 = Koha::Hold->new({
122
        branchcode => $branchcode,
123
        borrowernumber => $borrowernumber,
124
        biblionumber => $bibnum,
125
        priority => 1,
126
        reservedate => $resdate,
127
        expirationdate => $notexpdate,
128
        found => undef,
129
    })->store;
130
131
    my $hold2 = Koha::Hold->new({
132
        branchcode => $branchcode,
133
        borrowernumber => $borrowernumber,
134
        biblionumber => $bibnum,
135
        priority => 2,
136
        reservedate => $resdate,
137
        expirationdate => $expdate,
138
        found => undef,
139
    })->store;
140
141
    my $hold3 = Koha::Hold->new({
142
        branchcode => $branchcode,
143
        borrowernumber => $borrowernumber,
144
        biblionumber => $bibnum,
145
        itemnumber => $itemnumber,
146
        priority => 0,
147
        reservedate => $resdate,
148
        expirationdate => $expdate,
149
        found => 'W',
150
    })->store;
151
152
    t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 0 );
153
    CancelExpiredReserves();
154
    my $count1 = Koha::Holds->search->count;
155
    is( $count1, 2, 'Only the non-waiting expired holds should be cancelled');
156
157
    t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 );
158
    CancelExpiredReserves();
159
    my $count2 = Koha::Holds->search->count;
160
    is( $count2, 1, 'Also the waiting expired hold should be cancelled now');
161
};
162
163
$schema->storage->txn_rollback;

Return to bug 19260