From 47de926463c7f50cde887dfa36895a125a09b7c8 Mon Sep 17 00:00:00 2001 From: Yohann Dufour Date: Wed, 2 Jul 2014 09:13:44 +0200 Subject: [PATCH] Bug 12499: adding units tests for C4::Letters The tests for the subroutines : GetMessageTransportType, EnqueueLetter, SendQueuedMessages, GetQueuedMessages, GetLetters, getletter, addalert, getalert, delalert and GetPreparedLetter of the module C4::Letters have been added. Test plan: 1) Apply the patch 2) Execute the unit tests by launching : prove t/db_dependent/Letters.t 3) The result has to be a success without error or warning : t/db_dependent/Letters.t .. ok All tests successful. Files=1, Tests=45, 2 wallclock secs ( 0.04 usr 0.01 sys + 1.44 cusr 0.08 csys = 1.57 CPU) Result: PASS Signed-off-by: Bernardo Gonzalez Kriegel All test pass, no errors. Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Letters.t | 202 +++++++++++++++++++++++++++++++++++---- 1 file changed, 186 insertions(+), 16 deletions(-) diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 9f5c9ace19..bf0c1a1cd3 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -19,11 +19,13 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 45; use C4::Context; use C4::Letters; use C4::Members; +use C4::Branch; +use t::lib::Mocks; my $dbh = C4::Context->dbh; @@ -42,39 +44,207 @@ my $borrowernumber = AddMember( branchcode => 'CPL', ); + +# GetMessageTransportTypes +my $mtts = C4::Letters::GetMessageTransportTypes(); +is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' ); + $dbh->do(q| INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms') |); - -my $mtts = C4::Letters::GetMessageTransportTypes(); +$mtts = C4::Letters::GetMessageTransportTypes(); is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' ); -my $message_id = C4::Letters::EnqueueLetter({ + +# EnqueueLetter +is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' ); + +my $my_message = { borrowernumber => $borrowernumber, message_transport_type => 'sms', to_address => 'to@example.com', from_address => 'from@example.com', - letter => { - content => 'a message', - title => 'message title', - metadata => 'metadata', - code => 'TEST_MESSAGE', - content_type => 'text/plain', - }, -}); - +}; +my $message_id = C4::Letters::EnqueueLetter($my_message); +is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' ); + +delete $my_message->{message_transport_type}; +$my_message->{letter} = { + content => 'a message', + title => 'message title', + metadata => 'metadata', + code => 'TEST_MESSAGE', + content_type => 'text/plain', +}; +$message_id = C4::Letters::EnqueueLetter($my_message); +is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' ); + +$my_message->{message_transport_type} = 'sms'; +$message_id = C4::Letters::EnqueueLetter($my_message); ok(defined $message_id && $message_id > 0, 'new message successfully queued'); + +# GetQueuedMessages +my $messages = C4::Letters::GetQueuedMessages(); +is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' ); + +$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); +is( @$messages, 1, 'one message stored for the borrower' ); +is( $messages->[0]->{message_id}, $message_id, 'EnqueueLetter returns the message id correctly' ); +is( $messages->[0]->{borrowernumber}, $borrowernumber, 'EnqueueLetter stores the borrower number correctly' ); +is( $messages->[0]->{subject}, $my_message->{letter}->{title}, 'EnqueueLetter stores the subject correctly' ); +is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' ); +is( $messages->[0]->{message_transport_type}, $my_message->{message_transport_type}, 'EnqueueLetter stores the message type correctly' ); +is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pending correctly' ); + + +# SendQueuedMessages my $messages_processed = C4::Letters::SendQueuedMessages(); is($messages_processed, 1, 'all queued messages processed'); -my $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); -is(scalar(@$messages), 1, 'one message stored for the borrower'); - +$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); is( $messages->[0]->{status}, 'failed', 'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)' ); + +# GetLetters +my $letters = C4::Letters::GetLetters(); +is( @$letters, 0, 'GetLetters returns the correct number of letters' ); + +my $title = q|<> - <>|; +my $content = q|Dear <> <>, +According to our current records, you have items that are overdue.Your library does not charge late fines, but please return or renew them at the branch below as soon as possible. + +<> +<> +URL: <> + +The following item(s) is/are currently <>: + + <>. <>, Barcode: <> + +Thank-you for your prompt attention to this matter.|; + +$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('CPL','my module','my code','my name',1,?,?,'email')|, undef, $title, $content ); +$letters = C4::Letters::GetLetters(); +is( @$letters, 1, 'GetLetters returns the correct number of letters' ); +is( $letters->[0]->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' ); +is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' ); +is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' ); +is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' ); + + +# getletter +my $letter = C4::Letters::getletter('my module', 'my code', 'CPL', 'email'); +is( $letter->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' ); +is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' ); +is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' ); +is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' ); +is( $letter->{is_html}, 1, 'GetLetters gets the boolean is_html correctly' ); +is( $letter->{title}, $title, 'GetLetters gets the title correctly' ); +is( $letter->{content}, $content, 'GetLetters gets the content correctly' ); +is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' ); + + +# addalert +my $type = 'my type'; +my $externalid = 'my external id'; +my $alert_id = C4::Letters::addalert($borrowernumber, $type, $externalid); +isnt( $alert_id, undef, 'addalert does not return undef' ); + +my $alerts = C4::Letters::getalert($borrowernumber); +is( @$alerts, 1, 'addalert adds an alert' ); +is( $alerts->[0]->{alertid}, $alert_id, 'addalert returns the alert id correctly' ); +is( $alerts->[0]->{type}, $type, 'addalert stores the type correctly' ); +is( $alerts->[0]->{externalid}, $externalid, 'addalert stores the externalid correctly' ); + + +# getalert +$alerts = C4::Letters::getalert($borrowernumber, $type); +is( @$alerts, 1, 'getalert returns the correct number of alerts' ); +$alerts = C4::Letters::getalert($borrowernumber, $type, $externalid); +is( @$alerts, 1, 'getalert returns the correct number of alerts' ); +$alerts = C4::Letters::getalert($borrowernumber, 'another type'); +is( @$alerts, 0, 'getalert returns the correct number of alerts' ); +$alerts = C4::Letters::getalert($borrowernumber, $type, 'another external id'); +is( @$alerts, 0, 'getalert returns the correct number of alerts' ); + + +# delalert +eval { + C4::Letters::delalert(); +}; +isnt( $@, undef, 'delalert without argument returns an error' ); +$alerts = C4::Letters::getalert($borrowernumber); +is( @$alerts, 1, 'delalert without argument does not remove an alert' ); + +C4::Letters::delalert($alert_id); +$alerts = C4::Letters::getalert($borrowernumber); +is( @$alerts, 0, 'delalert removes an alert' ); + + +# GetPreparedLetter +t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com'); + +$content = 'This is a SMS for an <>'; +$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('CPL','my module','my code','my name',1,'my title',?,'sms')|, undef, $content ); + +my $tables = { + borrowers => $borrowernumber, + branches => 'CPL', +}; +my $substitute = { + status => 'overdue', +}; +my $repeat = [ + { + itemcallnumber => 'my callnumber1', + barcode => '1234', + }, + { + itemcallnumber => 'my callnumber2', + barcode => '5678', + }, +]; +my $prepared_letter = GetPreparedLetter(( + module => 'my module', + branchcode => 'CPL', + letter_code => 'my code', + tables => $tables, + substitute => $substitute, + repeat => $repeat, +)); +my $branch = GetBranchDetail('CPL'); +my $my_title_letter = qq|$branch->{branchname} - $substitute->{status}|; +my $my_content_letter = qq|Dear Jane Smith, +According to our current records, you have items that are overdue.Your library does not charge late fines, but please return or renew them at the branch below as soon as possible. + +$branch->{branchname} +$branch->{branchaddress1} +URL: http://thisisatest.com + +The following item(s) is/are currently $substitute->{status}: + + 1. $repeat->[0]->{itemcallnumber}, Barcode: $repeat->[0]->{barcode} + 2. $repeat->[1]->{itemcallnumber}, Barcode: $repeat->[1]->{barcode} + +Thank-you for your prompt attention to this matter.|; +is( $prepared_letter->{title}, $my_title_letter, 'GetPreparedLetter returns the title correctly' ); +is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' ); + +$prepared_letter = GetPreparedLetter(( + module => 'my module', + branchcode => 'CPL', + letter_code => 'my code', + tables => $tables, + substitute => $substitute, + repeat => $repeat, + message_transport_type => 'sms', +)); +$my_content_letter = qq|This is a SMS for an $substitute->{status}|; +is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' ); + $dbh->rollback; -- 2.39.5