From 5157f92531aa6181b31b115225499d5fd9c6912b Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 11 Feb 2019 16:04:59 -0300 Subject: [PATCH] Bug 21765: Regression tests Signed-off-by: Kyle M Hall --- .../Reserves/AutoUnsuspendReserves.t | 135 +++++++++++++++--- 1 file changed, 115 insertions(+), 20 deletions(-) diff --git a/t/db_dependent/Reserves/AutoUnsuspendReserves.t b/t/db_dependent/Reserves/AutoUnsuspendReserves.t index 9302da64da..d5703cc4e9 100644 --- a/t/db_dependent/Reserves/AutoUnsuspendReserves.t +++ b/t/db_dependent/Reserves/AutoUnsuspendReserves.t @@ -1,6 +1,22 @@ #!/usr/bin/perl +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + use Modern::Perl; + use Test::More tests => 1; use t::lib::Mocks; @@ -12,46 +28,125 @@ use Koha::DateUtils; use Koha::Holds; my $schema = Koha::Database->new->schema; -$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; subtest 'AutoUnsuspendReserves test' => sub { - plan tests => 2; - my $builder = t::lib::TestBuilder->new(); + plan tests => 4; - my $today = dt_from_string(); - my $today_date = output_pref({ dateformat => 'sql' }); - my $tomorrow_date = output_pref({ dt => $today->add(days=>1), dateformat=>'sql' }); + $schema->storage->txn_begin; - # Reserve not expired - my $reserve1 = $builder->build({ - source => 'Reserve', + my $today = dt_from_string(); + my $tomorrow = $today->clone->add(days=>1); + + # Not expired hold + my $hold_1 = $builder->build_object({ + class => 'Koha::Holds', value => { expirationdate => undef, cancellationdate => undef, priority => 5, found => undef, - suspend_until => $today_date, }, }); - # Reserve expired - my $reserve2 = $builder->build({ - source => 'Reserve', + + $hold_1->suspend_hold( $today ); + + # Expired hold + my $hold_2 = $builder->build_object({ + class => 'Koha::Holds', value => { expirationdate => undef, cancellationdate => undef, priority => 6, found => undef, - suspend_until => $tomorrow_date, }, }); + $hold_2->suspend_hold( $tomorrow ); + AutoUnsuspendReserves(); - my $r1 = Koha::Holds->find($reserve1->{reserve_id}); - my $r2 = Koha::Holds->find($reserve2->{reserve_id}); - ok(!defined($r1->suspend_until), 'Reserve suspended until today should be unsuspended.'); - ok(defined($r2->suspend_until), 'Reserve suspended after today should be suspended.'); -}; + # refresh + $hold_1->discard_changes; + $hold_2->discard_changes; + + ok(!$hold_1->is_suspended, 'Hold suspended until today should be unsuspended.'); + ok( $hold_2->is_suspended, 'Hold suspended after today should be suspended.'); + + subtest 'logging enabled' => sub { + + plan tests => 2; + + # Enable logging + t::lib::Mocks::mock_preference( 'HoldsLog', 1 ); + + my $hold_3 = $builder->build_object( + { class => 'Koha::Holds', + value => { + expirationdate => undef, + cancellationdate => undef, + priority => 5, + found => undef, + suspend_until => undef, + } + } + ); + $hold_3->suspend_hold( $today ); + + my $logs_count = $schema->resultset('ActionLog') + ->search( { module => 'HOLDS', action => 'RESUME' } )->count; + + AutoUnsuspendReserves(); -$schema->storage->txn_rollback; + $hold_3->discard_changes; + ok(!$hold_3->is_suspended, 'Hold suspended until today should be unsuspended.'); + + my $new_logs_count = $schema->resultset('ActionLog') + ->search( { module => 'HOLDS', action => 'RESUME' } )->count; + + is( $new_logs_count, + $logs_count + 1, + 'If logging is enabled, calling AutoUnsuspendReserves gets logged' + ); + }; + + subtest 'logging disabled' => sub { + + plan tests => 2; + + # Enable logging + t::lib::Mocks::mock_preference( 'HoldsLog', 0 ); + + my $hold_4 = $builder->build_object( + { class => 'Koha::Holds', + value => { + expirationdate => undef, + cancellationdate => undef, + priority => 5, + found => undef + } + } + ); + + my $logs_count = $schema->resultset('ActionLog') + ->search( { module => 'HOLDS', action => 'RESUME' } )->count; + + $hold_4->suspend_hold( $today ); + + AutoUnsuspendReserves(); + + $hold_4->discard_changes; + ok(!defined($hold_4->suspend_until), 'Hold suspended until today should be unsuspended.'); + + my $new_logs_count = $schema->resultset('ActionLog') + ->search( { module => 'HOLDS', action => 'RESUME' } )->count; + + is( $new_logs_count, + $logs_count, + 'If logging is not enabled, no logging from AutoUnsuspendReserves calls' + ); + }; + + $schema->storage->txn_rollback; +}; -- 2.17.2 (Apple Git-113)