3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 1;
23 use t::lib::TestBuilder;
25 use C4::Reserves qw( AutoUnsuspendReserves );
27 use Koha::DateUtils qw( dt_from_string );
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 subtest 'AutoUnsuspendReserves test' => sub {
37 $schema->storage->txn_begin;
39 my $today = dt_from_string();
40 my $tomorrow = $today->clone->add(days=>1);
43 my $hold_1 = $builder->build_object({
44 class => 'Koha::Holds',
46 expirationdate => undef,
47 cancellationdate => undef,
53 $hold_1->suspend_hold( $today );
56 my $hold_2 = $builder->build_object({
57 class => 'Koha::Holds',
59 expirationdate => undef,
60 cancellationdate => undef,
66 $hold_2->suspend_hold( $tomorrow );
68 AutoUnsuspendReserves();
71 $hold_1->discard_changes;
72 $hold_2->discard_changes;
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.');
77 subtest 'logging enabled' => sub {
82 t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
84 my $hold_3 = $builder->build_object(
85 { class => 'Koha::Holds',
87 expirationdate => undef,
88 cancellationdate => undef,
91 suspend_until => undef,
95 $hold_3->suspend_hold( $today );
97 my $logs_count = $schema->resultset('ActionLog')
98 ->search( { module => 'HOLDS', action => 'RESUME' } )->count;
100 AutoUnsuspendReserves();
102 $hold_3->discard_changes;
103 ok(!$hold_3->is_suspended, 'Hold suspended until today should be unsuspended.');
105 my $new_logs_count = $schema->resultset('ActionLog')
106 ->search( { module => 'HOLDS', action => 'RESUME' } )->count;
110 'If logging is enabled, calling AutoUnsuspendReserves gets logged'
114 subtest 'logging disabled' => sub {
119 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
121 my $hold_4 = $builder->build_object(
122 { class => 'Koha::Holds',
124 expirationdate => undef,
125 cancellationdate => undef,
132 my $logs_count = $schema->resultset('ActionLog')
133 ->search( { module => 'HOLDS', action => 'RESUME' } )->count;
135 $hold_4->suspend_hold( $today );
137 AutoUnsuspendReserves();
139 $hold_4->discard_changes;
140 ok(!defined($hold_4->suspend_until), 'Hold suspended until today should be unsuspended.');
142 my $new_logs_count = $schema->resultset('ActionLog')
143 ->search( { module => 'HOLDS', action => 'RESUME' } )->count;
147 'If logging is not enabled, no logging from AutoUnsuspendReserves calls'
151 $schema->storage->txn_rollback;