d5703cc4e9e86c3e6f7719ddabc3892a98ef0ddc
[srvgit] / t / db_dependent / Reserves / AutoUnsuspendReserves.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21
22 use t::lib::Mocks;
23 use t::lib::TestBuilder;
24
25 use C4::Reserves;
26 use Koha::Database;
27 use Koha::DateUtils;
28 use Koha::Holds;
29
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 subtest 'AutoUnsuspendReserves test' => sub {
34
35     plan tests => 4;
36
37     $schema->storage->txn_begin;
38
39     my $today    = dt_from_string();
40     my $tomorrow = $today->clone->add(days=>1);
41
42     # Not expired hold
43     my $hold_1 = $builder->build_object({
44         class => 'Koha::Holds',
45         value => {
46             expirationdate => undef,
47             cancellationdate => undef,
48             priority => 5,
49             found => undef,
50         },
51     });
52
53     $hold_1->suspend_hold( $today );
54
55     # Expired hold
56     my $hold_2 = $builder->build_object({
57         class => 'Koha::Holds',
58         value => {
59             expirationdate => undef,
60             cancellationdate => undef,
61             priority => 6,
62             found => undef,
63         },
64     });
65
66     $hold_2->suspend_hold( $tomorrow );
67
68     AutoUnsuspendReserves();
69
70     # refresh
71     $hold_1->discard_changes;
72     $hold_2->discard_changes;
73
74     ok(!$hold_1->is_suspended, 'Hold suspended until today should be unsuspended.');
75     ok( $hold_2->is_suspended, 'Hold suspended after today should be suspended.');
76
77     subtest 'logging enabled' => sub {
78
79         plan tests => 2;
80
81         # Enable logging
82         t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
83
84         my $hold_3 = $builder->build_object(
85             {   class => 'Koha::Holds',
86                 value => {
87                     expirationdate   => undef,
88                     cancellationdate => undef,
89                     priority         => 5,
90                     found            => undef,
91                     suspend_until    => undef,
92                 }
93             }
94         );
95         $hold_3->suspend_hold( $today );
96
97         my $logs_count = $schema->resultset('ActionLog')
98             ->search( { module => 'HOLDS', action => 'RESUME' } )->count;
99
100         AutoUnsuspendReserves();
101
102         $hold_3->discard_changes;
103         ok(!$hold_3->is_suspended, 'Hold suspended until today should be unsuspended.');
104
105         my $new_logs_count = $schema->resultset('ActionLog')
106             ->search( { module => 'HOLDS', action => 'RESUME' } )->count;
107
108         is( $new_logs_count,
109             $logs_count + 1,
110             'If logging is enabled, calling AutoUnsuspendReserves gets logged'
111         );
112     };
113
114     subtest 'logging disabled' => sub {
115
116         plan tests => 2;
117
118         # Enable logging
119         t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
120
121         my $hold_4 = $builder->build_object(
122             {   class => 'Koha::Holds',
123                 value => {
124                     expirationdate   => undef,
125                     cancellationdate => undef,
126                     priority         => 5,
127                     found            => undef
128                 }
129             }
130         );
131
132         my $logs_count = $schema->resultset('ActionLog')
133             ->search( { module => 'HOLDS', action => 'RESUME' } )->count;
134
135         $hold_4->suspend_hold( $today );
136
137         AutoUnsuspendReserves();
138
139         $hold_4->discard_changes;
140         ok(!defined($hold_4->suspend_until), 'Hold suspended until today should be unsuspended.');
141
142         my $new_logs_count = $schema->resultset('ActionLog')
143             ->search( { module => 'HOLDS', action => 'RESUME' } )->count;
144
145         is( $new_logs_count,
146             $logs_count,
147             'If logging is not enabled, no logging from AutoUnsuspendReserves calls'
148         );
149     };
150
151     $schema->storage->txn_rollback;
152 };