From 2843a7f2305f07558533c699ac332ac88b978f8c Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 2 Nov 2016 16:20:12 +0200 Subject: [PATCH] Bug 18595: Unit tests preparing for GetMessagingPreferences move This patch prepares removing GetMessagingPreferences by adding some useful unit tests for subroutines that the move targets. Reserves.t already covers print notice, but this patch adds tests for a set transport type. To test: 1. Run t/db_dependent/Circulation.t 2. Run t/db_dependent/Reserves.t 3. Observe test pass --- t/db_dependent/Circulation.t | 49 +++++++++++++++++++++++++++++++++++++++++++- t/db_dependent/Reserves.t | 24 ++++++++++------------ 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 9a5b821..d1bd888 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 95; +use Test::More tests => 96; use DateTime; @@ -29,11 +29,14 @@ use C4::Biblio; use C4::Items; use C4::Log; use C4::Members; +use C4::Message; use C4::Reserves; use C4::Overdues qw(UpdateFine CalcFine); use Koha::DateUtils; use Koha::Database; use Koha::IssuingRules; +use Koha::Patron::Message::Attributes; +use Koha::Patron::Message::Preferences; use Koha::Subscriptions; my $schema = Koha::Database->schema; @@ -1520,6 +1523,7 @@ subtest 'AddReturn + CumulativeRestrictionPeriods' => sub { } } ); + my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } ); my $item_2 = $builder->build( { @@ -1626,6 +1630,49 @@ subtest 'AddReturn + CumulativeRestrictionPeriods' => sub { is( $debarments->[0]->{expiration}, $expected_expiration ); }; +subtest 'SendCirculationAlert test' => sub { + plan tests => 4; + + my $library = $builder->build( { source => 'Branch' } ); + my $patron = $builder->build( { source => 'Borrower' } ); + my $attribute = Koha::Patron::Message::Attributes->find({ + message_name => 'Item_Checkout', + }); + Koha::Patron::Message::Preference->new({ + borrowernumber => $patron->{'borrowernumber'}, + message_attribute_id => $attribute->message_attribute_id, + days_in_advance => 0, + wants_digest => 0, + message_transport_types => ['email'], + })->store; + my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } ); + my $item_1 = $builder->build( + { source => 'Item', + value => { + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, + notforloan => 0, + itemlost => 0, + withdrawn => 0, + biblionumber => $biblioitem_1->{biblionumber} + } + } + ); + + my $old_message = C4::Message->find_last_message($patron, 'CHECKOUT', 'email'); + $old_message->{'message_id'} = 0 unless $old_message; + is(C4::Circulation::SendCirculationAlert({ + type => 'CHECKOUT', + item => $item_1, + borrower => $patron, + branch => $library->{'branchcode'}, + }), undef, "SendCirculationAlert called."); + my $new_message = C4::Message->find_last_message($patron, 'CHECKOUT', 'email'); + ok($old_message->{'message_id'} != $new_message->{'message_id'}, "New message has appeared."); + is($new_message->{'letter_code'}, 'CHECKOUT', "New message letter code is CHECKOUT."); + is($new_message->{'borrowernumber'}, $patron->{'borrowernumber'}, "New message is to our test patron."); +}; + sub set_userenv { my ( $library ) = @_; C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', ''); diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index 0d23816..af63da5 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -38,6 +38,7 @@ use Koha::Holds; use Koha::Libraries; use Koha::Notice::Templates; use Koha::Patron::Categories; +use Koha::Patron::Message::Preference; BEGIN { require_ok('C4::Reserves'); @@ -718,19 +719,6 @@ subtest '_koha_notify_reserve() tests' => sub { plan tests => 2; - my $wants_hold_and_email = { - wants_digest => '0', - transports => { - sms => 'HOLD', - email => 'HOLD', - }, - letter_code => 'HOLD' - }; - - my $mp = Test::MockModule->new( 'C4::Members::Messaging' ); - - $mp->mock("GetMessagingPreferences",$wants_hold_and_email); - $dbh->do('DELETE FROM letter'); my $email_hold_notice = $builder->build({ @@ -770,6 +758,16 @@ subtest '_koha_notify_reserve() tests' => sub { } }); + my $message_attr_id = Koha::Patron::Message::Attributes->find({ + message_name => 'Hold_Filled' + })->message_attribute_id; + Koha::Patron::Message::Preference->new({ + borrowernumber => $hold_borrower, + message_attribute_id => $message_attr_id, + message_transport_types => ['sms', 'email'], + wants_digest => 0, + })->store; + ModReserveAffect($hold->{itemnumber}, $hold->{borrowernumber}, 0); my $sms_message_address = $schema->resultset('MessageQueue')->search({ letter_code => 'HOLD', -- 2.7.4