X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=t%2Fdb_dependent%2FLetters.t;h=d8e96a6a7ade32c6268b265899cba0bd38a97b7d;hb=8a0e21f0ecfd7e5bd135ad30e8955960b5d3549d;hp=7950750c308d57824e4f91a8d26a7457e9454aa2;hpb=b1e78d530a38d3edc04168d22671624589d6bbcc;p=koha.git diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 7950750c30..d8e96a6a7a 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 82; +use Test::More tests => 76; use Test::MockModule; use Test::Warn; @@ -43,9 +43,9 @@ use t::lib::Mocks; use t::lib::TestBuilder; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); -use Koha::Acquisition::Order; use Koha::Acquisition::Booksellers; use Koha::Acquisition::Bookseller::Contacts; +use Koha::Acquisition::Orders; use Koha::Libraries; use Koha::Notice::Templates; my $schema = Koha::Database->schema; @@ -109,6 +109,7 @@ $my_message->{letter} = { 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' ); @@ -175,22 +176,47 @@ Look at this wonderful biblio timestamp: <>. $dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,'my module','my code','my name',1,?,?,'email')|, undef, $library->{branchcode}, $title, $content ); $letters = C4::Letters::GetLetters(); is( @$letters, 1, 'GetLetters returns the correct number of letters' ); -is( $letters->[0]->{branchcode}, $library->{branchcode}, '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', $library->{branchcode}, 'email'); -is( $letter->{branchcode}, $library->{branchcode}, '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' ); +subtest 'getletter' => sub { + plan tests => 16; + t::lib::Mocks::mock_preference('IndependentBranches', 0); + my $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email'); + is( $letter->{branchcode}, $library->{branchcode}, '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' ); + + my $context = Test::MockModule->new('C4::Context'); + $context->mock( 'userenv', sub { + return { + flags => 1, + branch => "anotherlib" } + }); + + t::lib::Mocks::mock_preference('IndependentBranches', 1); + $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email'); + is( $letter->{branchcode}, $library->{branchcode}, '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' ); + + $context->unmock('userenv'); +}; + + # Regression test for Bug 14206 $dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('FFL','my module','my code','my name',1,?,?,'print')|, undef, $title, $content ); @@ -409,8 +435,8 @@ my $order = Koha::Acquisition::Order->new( biblionumber => $biblionumber, budget_id => $budgetid, } -)->insert; -my $ordernumber = $order->{ordernumber}; +)->store; +my $ordernumber = $order->ordernumber; C4::Acquisition::CloseBasket( $basketno ); my $err; @@ -606,7 +632,7 @@ subtest 'TranslateNotices' => sub { subtest 'SendQueuedMessages' => sub { - plan tests => 2; + plan tests => 3; t::lib::Mocks::mock_preference( 'SMSSendDriver', 'Email' ); my $patron = Koha::Patrons->find($borrowernumber); $dbh->do(q| @@ -625,6 +651,94 @@ subtest 'SendQueuedMessages' => sub { borrowernumber => $borrowernumber, status => 'sent' })->next()->to_address(); - is( $sms_message_address, '5555555555@kidclamp.rocks', 'SendQueuedMessages populates the to address correctly for SMS by email' ); + is( $sms_message_address, '5555555555@kidclamp.rocks', 'SendQueuedMessages populates the to address correctly for SMS by email when to_address not set' ); + $schema->resultset('MessageQueue')->search({borrowernumber => $borrowernumber,status => 'sent'})->delete(); #clear borrower queue + $my_message->{to_address} = 'fixme@kidclamp.iswrong'; + $message_id = C4::Letters::EnqueueLetter($my_message); + C4::Letters::SendQueuedMessages(); + $sms_message_address = $schema->resultset('MessageQueue')->search({ + borrowernumber => $borrowernumber, + status => 'sent' + })->next()->to_address(); + is( $sms_message_address, '5555555555@kidclamp.rocks', 'SendQueuedMessages populates the to address correctly for SMS by email when to_address is set incorrectly' ); + +}; + +subtest 'get_item_content' => sub { + plan tests => 2; + + t::lib::Mocks::mock_preference('dateformat', 'metric'); + t::lib::Mocks::mock_preference('timeformat', '24hr'); + my @items = ( + {date_due => '2041-01-01 12:34', title => 'a first title', barcode => 'a_first_barcode', author => 'a_first_author', itemnumber => 1 }, + {date_due => '2042-01-02 23:45', title => 'a second title', barcode => 'a_second_barcode', author => 'a_second_author', itemnumber => 2 }, + ); + my @item_content_fields = qw( date_due title barcode author itemnumber ); + + my $items_content; + for my $item ( @items ) { + $items_content .= C4::Letters::get_item_content( { item => $item, item_content_fields => \@item_content_fields } ); + } + + my $expected_items_content = < $item, item_content_fields => \@item_content_fields, dateonly => 1, } ); + } + + $expected_items_content = < 1)' ); +}; + +subtest 'Test limit parameter for SendQueuedMessages' => sub { + plan tests => 3; + + my $dbh = C4::Context->dbh; + + my $borrowernumber = AddMember( + firstname => 'Jane', + surname => 'Smith', + categorycode => $patron_category, + branchcode => $library->{branchcode}, + dateofbirth => $date, + smsalertnumber => undef, + ); + $dbh->do(q|DELETE FROM message_queue|); + $my_message = { + 'letter' => { + 'content' => 'a message', + 'metadata' => 'metadata', + 'code' => 'TEST_MESSAGE', + 'content_type' => 'text/plain', + 'title' => 'message title' + }, + 'borrowernumber' => $borrowernumber, + 'to_address' => undef, + 'message_transport_type' => 'sms', + 'from_address' => 'from@example.com' + }; + C4::Letters::EnqueueLetter($my_message); + C4::Letters::EnqueueLetter($my_message); + C4::Letters::EnqueueLetter($my_message); + C4::Letters::EnqueueLetter($my_message); + C4::Letters::EnqueueLetter($my_message); + my $messages_processed = C4::Letters::SendQueuedMessages( { limit => 1 } ); + is( $messages_processed, 1, + 'Processed 1 message with limit of 1 and 5 unprocessed messages' ); + $messages_processed = C4::Letters::SendQueuedMessages( { limit => 2 } ); + is( $messages_processed, 2, + 'Processed 2 message with limit of 2 and 4 unprocessed messages' ); + $messages_processed = C4::Letters::SendQueuedMessages( { limit => 3 } ); + is( $messages_processed, 2, + 'Processed 2 message with limit of 3 and 2 unprocessed messages' ); };