Bug 17762: Add the lang parameter to C4::Letters::getletter
[koha.git] / t / db_dependent / Letters.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2013 Equinox Software, Inc.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use Test::More tests => 82;
22 use Test::MockModule;
23 use Test::Warn;
24
25 use MARC::Record;
26
27 my %mail;
28 my $module = new Test::MockModule('Mail::Sendmail');
29 $module->mock(
30     'sendmail',
31     sub {
32         warn "Fake sendmail";
33         %mail = @_;
34     }
35 );
36
37 use_ok('C4::Context');
38 use_ok('C4::Members');
39 use_ok('C4::Acquisition');
40 use_ok('C4::Biblio');
41 use_ok('C4::Letters');
42 use t::lib::Mocks;
43 use t::lib::TestBuilder;
44 use Koha::Database;
45 use Koha::DateUtils qw( dt_from_string output_pref );
46 use Koha::Acquisition::Order;
47 use Koha::Acquisition::Booksellers;
48 use Koha::Acquisition::Bookseller::Contacts;
49 use Koha::Libraries;
50 use Koha::Notice::Templates;
51 my $schema = Koha::Database->schema;
52 $schema->storage->txn_begin();
53
54 my $builder = t::lib::TestBuilder->new;
55 my $dbh = C4::Context->dbh;
56 $dbh->{RaiseError} = 1;
57
58 $dbh->do(q|DELETE FROM letter|);
59 $dbh->do(q|DELETE FROM message_queue|);
60 $dbh->do(q|DELETE FROM message_transport_types|);
61
62 my $library = $builder->build({
63     source => 'Branch',
64 });
65 my $patron_category = $builder->build({ source => 'Category' })->{categorycode};
66 my $date = dt_from_string;
67 my $borrowernumber = AddMember(
68     firstname    => 'Jane',
69     surname      => 'Smith',
70     categorycode => $patron_category,
71     branchcode   => $library->{branchcode},
72     dateofbirth  => $date,
73 );
74
75 my $marc_record = MARC::Record->new;
76 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
77
78 # GetMessageTransportTypes
79 my $mtts = C4::Letters::GetMessageTransportTypes();
80 is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' );
81
82 $dbh->do(q|
83     INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
84 |);
85 $mtts = C4::Letters::GetMessageTransportTypes();
86 is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
87
88
89 # EnqueueLetter
90 is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' );
91
92 my $my_message = {
93     borrowernumber         => $borrowernumber,
94     message_transport_type => 'sms',
95     to_address             => 'to@example.com',
96     from_address           => 'from@example.com',
97 };
98 my $message_id = C4::Letters::EnqueueLetter($my_message);
99 is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' );
100
101 delete $my_message->{message_transport_type};
102 $my_message->{letter} = {
103     content      => 'a message',
104     title        => 'message title',
105     metadata     => 'metadata',
106     code         => 'TEST_MESSAGE',
107     content_type => 'text/plain',
108 };
109 $message_id = C4::Letters::EnqueueLetter($my_message);
110 is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' );
111
112 $my_message->{message_transport_type} = 'sms';
113 $message_id = C4::Letters::EnqueueLetter($my_message);
114 ok(defined $message_id && $message_id > 0, 'new message successfully queued');
115
116
117 # GetQueuedMessages
118 my $messages = C4::Letters::GetQueuedMessages();
119 is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' );
120
121 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
122 is( @$messages, 1, 'one message stored for the borrower' );
123 is( $messages->[0]->{message_id}, $message_id, 'EnqueueLetter returns the message id correctly' );
124 is( $messages->[0]->{borrowernumber}, $borrowernumber, 'EnqueueLetter stores the borrower number correctly' );
125 is( $messages->[0]->{subject}, $my_message->{letter}->{title}, 'EnqueueLetter stores the subject correctly' );
126 is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' );
127 is( $messages->[0]->{message_transport_type}, $my_message->{message_transport_type}, 'EnqueueLetter stores the message type correctly' );
128 is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pending correctly' );
129
130
131 # SendQueuedMessages
132 my $messages_processed = C4::Letters::SendQueuedMessages();
133 is($messages_processed, 1, 'all queued messages processed');
134
135 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
136 is(
137     $messages->[0]->{status},
138     'failed',
139     'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
140 );
141
142 # ResendMessage
143 my $resent = C4::Letters::ResendMessage($messages->[0]->{message_id});
144 my $message = C4::Letters::GetMessage( $messages->[0]->{message_id});
145 is( $resent, 1, 'The message should have been resent' );
146 is($message->{status},'pending', 'ResendMessage sets status to pending correctly (bug 12426)');
147 $resent = C4::Letters::ResendMessage($messages->[0]->{message_id});
148 is( $resent, 0, 'The message should not have been resent again' );
149 $resent = C4::Letters::ResendMessage();
150 is( $resent, undef, 'ResendMessage should return undef if not message_id given' );
151
152 # GetLetters
153 my $letters = C4::Letters::GetLetters();
154 is( @$letters, 0, 'GetLetters returns the correct number of letters' );
155
156 my $title = q|<<branches.branchname>> - <<status>>|;
157 my $content = q{Dear <<borrowers.firstname>> <<borrowers.surname>>,
158 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.
159
160 <<branches.branchname>>
161 <<branches.branchaddress1>>
162 URL: <<OPACBaseURL>>
163
164 The following item(s) is/are currently <<status>>:
165
166 <item> <<count>>. <<items.itemcallnumber>>, Barcode: <<items.barcode>> </item>
167
168 Thank-you for your prompt attention to this matter.
169 Don't forget your date of birth: <<borrowers.dateofbirth>>.
170 Look at this wonderful biblio timestamp: <<biblio.timestamp>>.
171 };
172
173 $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 );
174 $letters = C4::Letters::GetLetters();
175 is( @$letters, 1, 'GetLetters returns the correct number of letters' );
176 is( $letters->[0]->{branchcode}, $library->{branchcode}, 'GetLetters gets the branch code correctly' );
177 is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' );
178 is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' );
179 is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' );
180
181
182 # getletter
183 my $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email');
184 is( $letter->{branchcode}, $library->{branchcode}, 'GetLetters gets the branch code correctly' );
185 is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' );
186 is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' );
187 is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' );
188 is( $letter->{is_html}, 1, 'GetLetters gets the boolean is_html correctly' );
189 is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
190 is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
191 is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
192
193 # Regression test for Bug 14206
194 $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 );
195 my $letter14206_a = C4::Letters::getletter('my module', 'my code', 'FFL' );
196 is( $letter14206_a->{message_transport_type}, 'print', 'Bug 14206 - message_transport_type not passed, correct mtt detected' );
197 my $letter14206_b = C4::Letters::getletter('my module', 'my code', 'FFL', 'print');
198 is( $letter14206_b->{message_transport_type}, 'print', 'Bug 14206 - message_transport_type passed, correct mtt detected'  );
199
200 # test for overdue_notices.pl
201 my $overdue_rules = {
202     letter1         => 'my code',
203 };
204 my $i = 1;
205 my $branchcode = 'FFL';
206 my $letter14206_c = C4::Letters::getletter('my module', $overdue_rules->{"letter$i"}, $branchcode);
207 is( $letter14206_c->{message_transport_type}, 'print', 'Bug 14206 - correct mtt detected for call from overdue_notices.pl' );
208
209 # addalert
210 my $type = 'my type';
211 my $externalid = 'my external id';
212 my $alert_id = C4::Letters::addalert($borrowernumber, $type, $externalid);
213 isnt( $alert_id, undef, 'addalert does not return undef' );
214
215
216 # getalert
217 my $alerts = C4::Letters::getalert();
218 is( @$alerts, 1, 'getalert should not fail without parameter' );
219 $alerts = C4::Letters::getalert($borrowernumber);
220 is( @$alerts, 1, 'addalert adds an alert' );
221 is( $alerts->[0]->{alertid}, $alert_id, 'addalert returns the alert id correctly' );
222 is( $alerts->[0]->{type}, $type, 'addalert stores the type correctly' );
223 is( $alerts->[0]->{externalid}, $externalid, 'addalert stores the externalid correctly' );
224
225 $alerts = C4::Letters::getalert($borrowernumber, $type);
226 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
227 $alerts = C4::Letters::getalert($borrowernumber, $type, $externalid);
228 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
229 $alerts = C4::Letters::getalert($borrowernumber, 'another type');
230 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
231 $alerts = C4::Letters::getalert($borrowernumber, $type, 'another external id');
232 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
233
234
235 # delalert
236 eval {
237     C4::Letters::delalert();
238 };
239 isnt( $@, undef, 'delalert without argument returns an error' );
240 $alerts = C4::Letters::getalert($borrowernumber);
241 is( @$alerts, 1, 'delalert without argument does not remove an alert' );
242
243 C4::Letters::delalert($alert_id);
244 $alerts = C4::Letters::getalert($borrowernumber);
245 is( @$alerts, 0, 'delalert removes an alert' );
246
247
248 # GetPreparedLetter
249 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
250
251 my $sms_content = 'This is a SMS for an <<status>>';
252 $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,'my title',?,'sms')|, undef, $library->{branchcode}, $sms_content );
253
254 my $tables = {
255     borrowers => $borrowernumber,
256     branches => $library->{branchcode},
257     biblio => $biblionumber,
258 };
259 my $substitute = {
260     status => 'overdue',
261 };
262 my $repeat = [
263     {
264         itemcallnumber => 'my callnumber1',
265         barcode        => '1234',
266     },
267     {
268         itemcallnumber => 'my callnumber2',
269         barcode        => '5678',
270     },
271 ];
272 my $prepared_letter = GetPreparedLetter((
273     module      => 'my module',
274     branchcode  => $library->{branchcode},
275     letter_code => 'my code',
276     tables      => $tables,
277     substitute  => $substitute,
278     repeat      => $repeat,
279 ));
280 my $retrieved_library = Koha::Libraries->find($library->{branchcode});
281 my $my_title_letter = $retrieved_library->branchname . qq| - $substitute->{status}|;
282 my $my_content_letter = qq|Dear Jane Smith,
283 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.
284
285 |.$retrieved_library->branchname.qq|
286 |.$retrieved_library->branchaddress1.qq|
287 URL: http://thisisatest.com
288
289 The following item(s) is/are currently $substitute->{status}:
290
291 <item> 1. $repeat->[0]->{itemcallnumber}, Barcode: $repeat->[0]->{barcode} </item>
292 <item> 2. $repeat->[1]->{itemcallnumber}, Barcode: $repeat->[1]->{barcode} </item>
293
294 Thank-you for your prompt attention to this matter.
295 Don't forget your date of birth: | . output_pref({ dt => $date, dateonly => 1 }) . q|.
296 Look at this wonderful biblio timestamp: | . output_pref({ dt => $date }) . ".\n";
297
298 is( $prepared_letter->{title}, $my_title_letter, 'GetPreparedLetter returns the title correctly' );
299 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
300
301 $prepared_letter = GetPreparedLetter((
302     module                 => 'my module',
303     branchcode             => $library->{branchcode},
304     letter_code            => 'my code',
305     tables                 => $tables,
306     substitute             => $substitute,
307     repeat                 => $repeat,
308     message_transport_type => 'sms',
309 ));
310 $my_content_letter = qq|This is a SMS for an $substitute->{status}|;
311 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
312
313 $dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('test_date','TEST_DATE','Test dates','A title with a timestamp: <<biblio.timestamp>>','This one only contains the date: <<biblio.timestamp | dateonly>>.');});
314 $prepared_letter = GetPreparedLetter((
315     module                 => 'test_date',
316     branchcode             => '',
317     letter_code            => 'test_date',
318     tables                 => $tables,
319     substitute             => $substitute,
320     repeat                 => $repeat,
321 ));
322 is( $prepared_letter->{content}, q|This one only contains the date: | . output_pref({ dt => $date, dateonly => 1 }) . q|.|, 'dateonly test 1' );
323
324 $dbh->do(q{UPDATE letter SET content = 'And also this one:<<timestamp | dateonly>>.' WHERE code = 'test_date';});
325 $prepared_letter = GetPreparedLetter((
326     module                 => 'test_date',
327     branchcode             => '',
328     letter_code            => 'test_date',
329     tables                 => $tables,
330     substitute             => $substitute,
331     repeat                 => $repeat,
332 ));
333 is( $prepared_letter->{content}, q|And also this one:| . output_pref({ dt => $date, dateonly => 1 }) . q|.|, 'dateonly test 2' );
334
335 $dbh->do(q{UPDATE letter SET content = 'And also this one:<<timestamp|dateonly >>.' WHERE code = 'test_date';});
336 $prepared_letter = GetPreparedLetter((
337     module                 => 'test_date',
338     branchcode             => '',
339     letter_code            => 'test_date',
340     tables                 => $tables,
341     substitute             => $substitute,
342     repeat                 => $repeat,
343 ));
344 is( $prepared_letter->{content}, q|And also this one:| . output_pref({ dt => $date, dateonly => 1 }) . q|.|, 'dateonly test 3' );
345
346 t::lib::Mocks::mock_preference( 'TimeFormat', '12hr' );
347 my $yesterday_night = $date->clone->add( days => -1 )->set_hour(22);
348 $dbh->do(q|UPDATE biblio SET timestamp = ? WHERE biblionumber = ?|, undef, $yesterday_night, $biblionumber );
349 $dbh->do(q{UPDATE letter SET content = 'And also this one:<<timestamp>>.' WHERE code = 'test_date';});
350 $prepared_letter = GetPreparedLetter((
351     module                 => 'test_date',
352     branchcode             => '',
353     letter_code            => 'test_date',
354     tables                 => $tables,
355     substitute             => $substitute,
356     repeat                 => $repeat,
357 ));
358 is( $prepared_letter->{content}, q|And also this one:| . output_pref({ dt => $yesterday_night }) . q|.|, 'dateonly test 3' );
359
360 $dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('claimacquisition','TESTACQCLAIM','Acquisition Claim','Item Not Received','<<aqbooksellers.name>>|<<aqcontacts.name>>|<order>Ordernumber <<aqorders.ordernumber>> (<<biblio.title>>) (<<aqorders.quantity>> ordered)</order>');});
361 $dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('orderacquisition','TESTACQORDER','Acquisition Order','Order','<<aqbooksellers.name>>|<<aqcontacts.name>>|<order>Ordernumber <<aqorders.ordernumber>> (<<biblio.title>>) (<<aqorders.quantity>> ordered)</order>');});
362
363 # Test that _parseletter doesn't modify its parameters bug 15429
364 {
365     my $values = { dateexpiry => '2015-12-13', };
366     C4::Letters::_parseletter($prepared_letter, 'borrowers', $values);
367     is( $values->{dateexpiry}, '2015-12-13', "_parseletter doesn't modify its parameters" );
368 }
369
370 my $bookseller = Koha::Acquisition::Bookseller->new(
371     {
372         name => "my vendor",
373         address1 => "bookseller's address",
374         phone => "0123456",
375         active => 1,
376         deliverytime => 5,
377     }
378 )->store;
379 my $booksellerid = $bookseller->id;
380
381 Koha::Acquisition::Bookseller::Contact->new( { name => 'John Smith',  phone => '0123456x1', claimacquisition => 1, orderacquisition => 1, booksellerid => $booksellerid } )->store;
382 Koha::Acquisition::Bookseller::Contact->new( { name => 'Leo Tolstoy', phone => '0123456x2', claimissues      => 1, booksellerid => $booksellerid } )->store;
383 my $basketno = NewBasket($booksellerid, 1);
384
385 my $budgetid = C4::Budgets::AddBudget({
386     budget_code => "budget_code_test_letters",
387     budget_name => "budget_name_test_letters",
388 });
389
390 my $bib = MARC::Record->new();
391 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
392     $bib->append_fields(
393         MARC::Field->new('200', ' ', ' ', a => 'Silence in the library'),
394     );
395 } else {
396     $bib->append_fields(
397         MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
398     );
399 }
400
401 ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
402 my $order = Koha::Acquisition::Order->new(
403     {
404         basketno => $basketno,
405         quantity => 1,
406         biblionumber => $biblionumber,
407         budget_id => $budgetid,
408     }
409 )->insert;
410 my $ordernumber = $order->{ordernumber};
411
412 C4::Acquisition::CloseBasket( $basketno );
413 my $err;
414 warning_like {
415     $err = SendAlerts( 'claimacquisition', [ $ordernumber ], 'TESTACQCLAIM' ) }
416     qr/^Bookseller .* without emails at/,
417     "SendAlerts prints a warning";
418 is($err->{'error'}, 'no_email', "Trying to send an alert when there's no e-mail results in an error");
419
420 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
421 $bookseller->contacts->next->email('testemail@mydomain.com')->store;
422
423 # Ensure that the preference 'LetterLog' is set to logging
424 t::lib::Mocks::mock_preference( 'LetterLog', 'on' );
425
426 # SendAlerts needs branchemail or KohaAdminEmailAddress as sender
427 C4::Context->_new_userenv('DUMMY');
428 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'My Library', 0, '', '');
429 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'library@domain.com' );
430
431 {
432 warning_is {
433     $err = SendAlerts( 'orderacquisition', $basketno , 'TESTACQORDER' ) }
434     "Fake sendmail",
435     "SendAlerts is using the mocked sendmail routine (orderacquisition)";
436 is($err, 1, "Successfully sent order.");
437 is($mail{'To'}, 'testemail@mydomain.com', "mailto correct in sent order");
438 is($mail{'Message'}, 'my vendor|John Smith|Ordernumber ' . $ordernumber . ' (Silence in the library) (1 ordered)', 'Order notice text constructed successfully');
439
440 $dbh->do(q{DELETE FROM letter WHERE code = 'TESTACQORDER';});
441 warning_like {
442     $err = SendAlerts( 'orderacquisition', $basketno , 'TESTACQORDER' ) }
443     qr/No orderacquisition TESTACQORDER letter transported by email/,
444     "GetPreparedLetter warns about missing notice template";
445 is($err->{'error'}, 'no_letter', "No TESTACQORDER letter was defined.");
446 }
447
448 subtest 'GetPreparedLetter' => sub {
449     plan tests => 4;
450
451     Koha::Notice::Template->new(
452         {
453             module                 => 'test',
454             code                   => 'test',
455             branchcode             => '',
456             message_transport_type => 'email'
457         }
458     )->store;
459     my $letter;
460     warning_like {
461         $letter = C4::Letters::GetPreparedLetter(
462             module      => 'test',
463             letter_code => 'test',
464         );
465     }
466     qr{^ERROR: nothing to substitute},
467 'GetPreparedLetter should warn if tables, substiture and repeat are not set';
468     is( $letter, undef,
469 'No letter should be returned by GetPreparedLetter if something went wrong'
470     );
471
472     warning_like {
473         $letter = C4::Letters::GetPreparedLetter(
474             module      => 'test',
475             letter_code => 'test',
476             substitute  => {}
477         );
478     }
479     qr{^ERROR: nothing to substitute},
480 'GetPreparedLetter should warn if tables, substiture and repeat are not set, even if the key is passed';
481     is( $letter, undef,
482 'No letter should be returned by GetPreparedLetter if something went wrong'
483     );
484
485 };
486
487 {
488 warning_is {
489     $err = SendAlerts( 'claimacquisition', [ $ordernumber ], 'TESTACQCLAIM' ) }
490     "Fake sendmail",
491     "SendAlerts is using the mocked sendmail routine";
492
493 is($err, 1, "Successfully sent claim");
494 is($mail{'To'}, 'testemail@mydomain.com', "mailto correct in sent claim");
495 is($mail{'Message'}, 'my vendor|John Smith|Ordernumber ' . $ordernumber . ' (Silence in the library) (1 ordered)', 'Claim notice text constructed successfully');
496 }
497
498 {
499 use C4::Serials;
500
501 my $notes = 'notes';
502 my $internalnotes = 'intnotes';
503 $dbh->do(q|UPDATE subscription_numberpatterns SET numberingmethod='No. {X}' WHERE id=1|);
504 my $subscriptionid = NewSubscription(
505      undef,      "",     undef, undef, undef, $biblionumber,
506     '2013-01-01', 1, undef, undef,  undef,
507     undef,      undef,  undef, undef, undef, undef,
508     1,          $notes,undef, '2013-01-01', undef, 1,
509     undef,       undef,  0,    $internalnotes,  0,
510     undef, undef, 0,          undef,         '2013-12-31', 0
511 );
512 $dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('serial','RLIST','Serial issue notification','Serial issue notification','<<biblio.title>>,<<subscription.subscriptionid>>,<<serial.serialseq>>');});
513 my ($serials_count, @serials) = GetSerials($subscriptionid);
514 my $serial = $serials[0];
515
516 my $borrowernumber = AddMember(
517     firstname    => 'John',
518     surname      => 'Smith',
519     categorycode => $patron_category,
520     branchcode   => $library->{branchcode},
521     dateofbirth  => $date,
522     email        => 'john.smith@test.de',
523 );
524 my $alert_id = C4::Letters::addalert($borrowernumber, 'issue', $subscriptionid);
525
526
527 my $err2;
528 warning_is {
529 $err2 = SendAlerts( 'issue', $serial->{serialid}, 'RLIST' ) }
530     "Fake sendmail",
531     "SendAlerts is using the mocked sendmail routine";
532 is($err2, 1, "Successfully sent serial notification");
533 is($mail{'To'}, 'john.smith@test.de', "mailto correct in sent serial notification");
534 is($mail{'Message'}, 'Silence in the library,'.$subscriptionid.',No. 0', 'Serial notification text constructed successfully');
535 }
536
537
538 subtest 'TranslateNotices' => sub {
539     plan tests => 3;
540     $dbh->do(
541         q|
542         INSERT INTO letter (module, code, branchcode, name, title, content, message_transport_type, lang) VALUES
543         ('test', 'code', '', 'test', 'a test', 'just a test', 'email', 'default'),
544         ('test', 'code', '', 'test', 'una prueba', 'solo una prueba', 'email', 'es-ES');
545     | );
546     my $substitute = {};
547     my $letter = C4::Letters::GetPreparedLetter(
548             module                 => 'test',
549             letter_code            => 'code',
550             message_transport_type => 'email',
551             substitute             => $substitute,
552     );
553     is(
554         $letter->{title},
555         'a test',
556         'GetPreparedLetter should return the default one if the lang parameter is not provided'
557     );
558
559     $letter = C4::Letters::GetPreparedLetter(
560             module                 => 'test',
561             letter_code            => 'code',
562             message_transport_type => 'email',
563             substitute             => $substitute,
564             lang                   => 'es-ES',
565     );
566     is( $letter->{title}, 'una prueba',
567         'GetPreparedLetter should return the required notice if it exists' );
568
569     $letter = C4::Letters::GetPreparedLetter(
570             module                 => 'test',
571             letter_code            => 'code',
572             message_transport_type => 'email',
573             substitute             => $substitute,
574             lang                   => 'fr-FR',
575     );
576     is(
577         $letter->{title},
578         'a test',
579         'GetPreparedLetter should return the default notice if the one required does not exist'
580     );
581 };
582
583 subtest 'SendQueuedMessages' => sub {
584     plan tests => 1;
585     t::lib::Mocks::mock_preference( 'SMSSendDriver', 'Email' );
586     my $patron = Koha::Patrons->find($borrowernumber);
587     $dbh->do(q|
588         INSERT INTO message_queue(borrowernumber, subject, content, message_transport_type, status, letter_code)
589         VALUES (?, 'subject', 'content', 'sms', 'pending', 'just_a_code')
590         |, undef, $borrowernumber
591     );
592     eval { C4::Letters::SendQueuedMessages(); };
593     is( $@, '', 'SendQueuedMessages should not explode if the patron does not have a sms provider set' );
594 };