From ab23ce3eb18467a13a31bfc92b350da305671291 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 25 Nov 2020 11:22:24 -0300 Subject: [PATCH] Bug 26282: Unit tests This patch adds tests for the behaviour change. It tests if letter processing takes place when notify_patron is passed. It tests all combinations. Bonus: it tweaks the POD to acknowledge this new parameter. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Hold.t => FAIL: Tests fail! 3. Apply the rest of the patches 4. Repeat 2 => SUCCESS: Tests pass! No warnings! 5. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Josef Moravec --- t/db_dependent/Koha/Hold.t | 96 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t index bdf4f499d2..445132e739 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -19,8 +19,11 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; +use Test::MockModule; +use Test::Warn; +use t::lib::Mocks; use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; @@ -48,3 +51,94 @@ subtest 'patron() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'cancel() tests' => sub { + + plan tests => 12; + + $schema->storage->txn_begin; + + my $get_prepared_letter_called; + + # Mock GetPreparedLetter so it raises a warning we can look for + # and returns undef, so no call to EnqueueLetter happens + my $mocked_letter = Test::MockModule->new("C4::Letters"); + $mocked_letter->mock( 'GetPreparedLetter', sub { + $get_prepared_letter_called = 1; + return; + }); + + my $hold = $builder->build_object( + { + class => 'Koha::Holds', + value => { + cancellationdate => undef, + priority => 1, + cancellation_reason => undef, + } + } + ); + + # leave this things out of the test + t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge', 0 ); + t::lib::Mocks::mock_preference( 'HoldsLog', 0 ); + + $hold = $builder->build_object( + { + class => 'Koha::Holds', + value => { + cancellationdate => undef, + priority => 1, + cancellation_reason => undef, + } + } + ); + + $get_prepared_letter_called = 0; + $hold->cancel({ cancellation_reason => 'Some reason' }); + ok( !$get_prepared_letter_called, 'GetPreparedLetter not called' ); + + isnt( $hold->cancellationdate, undef, 'cancellationdate gets set to the passed value' ); + is( $hold->priority, 0, 'priority gets set to 0' ); + is( $hold->cancellation_reason, 'Some reason', 'cancellation_reason is set to the passed value' ); + + $hold = $builder->build_object( + { + class => 'Koha::Holds', + value => { + cancellationdate => undef, + priority => 1, + cancellation_reason => undef, + } + } + ); + + $get_prepared_letter_called = 0; + $hold->cancel({ cancellation_reason => 'Some reason', notify_patron => 1 }); + ok( $get_prepared_letter_called, 'GetPreparedLetter called if notify_patron and cancellation_reason passed' ); + + isnt( $hold->cancellationdate, undef, 'cancellationdate gets set to the passed value' ); + is( $hold->priority, 0, 'priority gets set to 0' ); + is( $hold->cancellation_reason, 'Some reason', 'cancellation_reason is set to the passed value' ); + + $hold = $builder->build_object( + { + class => 'Koha::Holds', + value => { + cancellationdate => undef, + priority => 1, + cancellation_reason => undef, + } + } + ); + + $get_prepared_letter_called = 0; + $hold->cancel({ notify_patron => 1 }); + ok( $get_prepared_letter_called, 'GetPreparedLetter called if notify_patron passed' ); + + isnt( $hold->cancellationdate, undef, 'cancellationdate gets set to the passed value' ); + is( $hold->priority, 0, 'priority gets set to 0' ); + is( $hold->cancellation_reason, undef, 'cancellation_reason not passed' ); + + $schema->storage->txn_rollback; +}; -- 2.11.0