Bug 13400: Untranslatable "Are you sure you want to delete this authority?"
[koha.git] / t / db_dependent / Letters.t
blob016c213ef4d23d033dac3f6edc1a0ac6d1f25e28
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) 2013 Equinox Software, Inc.
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.
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.
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>.
20 use Modern::Perl;
21 use Test::More tests => 58;
22 use Test::MockModule;
23 use Test::Warn;
25 use MARC::Record;
27 my %mail;
28 my $module = new Test::MockModule('Mail::Sendmail');
29 $module->mock(
30 'sendmail',
31 sub {
32 warn "Fake sendmail";
33 %mail = @_;
37 use_ok('C4::Context');
38 use_ok('C4::Members');
39 use_ok('C4::Branch');
40 use_ok('C4::Acquisition');
41 use_ok('C4::Biblio');
42 use_ok('C4::Bookseller');
43 use_ok('C4::Letters');
44 use t::lib::Mocks;
45 use Koha::DateUtils qw( dt_from_string output_pref );
46 use Koha::Acquisition::Order;
48 my $dbh = C4::Context->dbh;
50 # Start transaction
51 $dbh->{AutoCommit} = 0;
52 $dbh->{RaiseError} = 1;
54 $dbh->do(q|DELETE FROM letter|);
55 $dbh->do(q|DELETE FROM message_queue|);
56 $dbh->do(q|DELETE FROM message_transport_types|);
58 my $date = dt_from_string;
59 my $borrowernumber = AddMember(
60 firstname => 'Jane',
61 surname => 'Smith',
62 categorycode => 'PT',
63 branchcode => 'CPL',
64 dateofbirth => $date,
67 my $marc_record = MARC::Record->new;
68 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
70 # GetMessageTransportTypes
71 my $mtts = C4::Letters::GetMessageTransportTypes();
72 is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' );
74 $dbh->do(q|
75 INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
76 |);
77 $mtts = C4::Letters::GetMessageTransportTypes();
78 is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
81 # EnqueueLetter
82 is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' );
84 my $my_message = {
85 borrowernumber => $borrowernumber,
86 message_transport_type => 'sms',
87 to_address => 'to@example.com',
88 from_address => 'from@example.com',
90 my $message_id = C4::Letters::EnqueueLetter($my_message);
91 is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' );
93 delete $my_message->{message_transport_type};
94 $my_message->{letter} = {
95 content => 'a message',
96 title => 'message title',
97 metadata => 'metadata',
98 code => 'TEST_MESSAGE',
99 content_type => 'text/plain',
101 $message_id = C4::Letters::EnqueueLetter($my_message);
102 is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' );
104 $my_message->{message_transport_type} = 'sms';
105 $message_id = C4::Letters::EnqueueLetter($my_message);
106 ok(defined $message_id && $message_id > 0, 'new message successfully queued');
109 # GetQueuedMessages
110 my $messages = C4::Letters::GetQueuedMessages();
111 is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' );
113 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
114 is( @$messages, 1, 'one message stored for the borrower' );
115 is( $messages->[0]->{message_id}, $message_id, 'EnqueueLetter returns the message id correctly' );
116 is( $messages->[0]->{borrowernumber}, $borrowernumber, 'EnqueueLetter stores the borrower number correctly' );
117 is( $messages->[0]->{subject}, $my_message->{letter}->{title}, 'EnqueueLetter stores the subject correctly' );
118 is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' );
119 is( $messages->[0]->{message_transport_type}, $my_message->{message_transport_type}, 'EnqueueLetter stores the message type correctly' );
120 is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pending correctly' );
123 # SendQueuedMessages
124 my $messages_processed = C4::Letters::SendQueuedMessages();
125 is($messages_processed, 1, 'all queued messages processed');
127 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
129 $messages->[0]->{status},
130 'failed',
131 'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
134 # GetLetters
135 my $letters = C4::Letters::GetLetters();
136 is( @$letters, 0, 'GetLetters returns the correct number of letters' );
138 my $title = q|<<branches.branchname>> - <<status>>|;
139 my $content = q|Dear <<borrowers.firstname>> <<borrowers.surname>>,
140 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.
142 <<branches.branchname>>
143 <<branches.branchaddress1>>
144 URL: <<OPACBaseURL>>
146 The following item(s) is/are currently <<status>>:
148 <item> <<count>>. <<items.itemcallnumber>>, Barcode: <<items.barcode>> </item>
150 Thank-you for your prompt attention to this matter.
151 Don't forget your date of birth: <<borrowers.dateofbirth>>.
152 Look at this wonderful biblio timestamp: <<biblio.timestamp>>.
155 $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 );
156 $letters = C4::Letters::GetLetters();
157 is( @$letters, 1, 'GetLetters returns the correct number of letters' );
158 is( $letters->[0]->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
159 is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' );
160 is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' );
161 is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' );
164 # getletter
165 my $letter = C4::Letters::getletter('my module', 'my code', 'CPL', 'email');
166 is( $letter->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
167 is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' );
168 is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' );
169 is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' );
170 is( $letter->{is_html}, 1, 'GetLetters gets the boolean is_html correctly' );
171 is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
172 is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
173 is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
176 # addalert
177 my $type = 'my type';
178 my $externalid = 'my external id';
179 my $alert_id = C4::Letters::addalert($borrowernumber, $type, $externalid);
180 isnt( $alert_id, undef, 'addalert does not return undef' );
182 my $alerts = C4::Letters::getalert($borrowernumber);
183 is( @$alerts, 1, 'addalert adds an alert' );
184 is( $alerts->[0]->{alertid}, $alert_id, 'addalert returns the alert id correctly' );
185 is( $alerts->[0]->{type}, $type, 'addalert stores the type correctly' );
186 is( $alerts->[0]->{externalid}, $externalid, 'addalert stores the externalid correctly' );
189 # getalert
190 $alerts = C4::Letters::getalert($borrowernumber, $type);
191 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
192 $alerts = C4::Letters::getalert($borrowernumber, $type, $externalid);
193 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
194 $alerts = C4::Letters::getalert($borrowernumber, 'another type');
195 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
196 $alerts = C4::Letters::getalert($borrowernumber, $type, 'another external id');
197 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
200 # delalert
201 eval {
202 C4::Letters::delalert();
204 isnt( $@, undef, 'delalert without argument returns an error' );
205 $alerts = C4::Letters::getalert($borrowernumber);
206 is( @$alerts, 1, 'delalert without argument does not remove an alert' );
208 C4::Letters::delalert($alert_id);
209 $alerts = C4::Letters::getalert($borrowernumber);
210 is( @$alerts, 0, 'delalert removes an alert' );
213 # GetPreparedLetter
214 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
216 my $sms_content = 'This is a SMS for an <<status>>';
217 $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, $sms_content );
219 my $tables = {
220 borrowers => $borrowernumber,
221 branches => 'CPL',
222 biblio => $biblionumber,
224 my $substitute = {
225 status => 'overdue',
227 my $repeat = [
229 itemcallnumber => 'my callnumber1',
230 barcode => '1234',
233 itemcallnumber => 'my callnumber2',
234 barcode => '5678',
237 my $prepared_letter = GetPreparedLetter((
238 module => 'my module',
239 branchcode => 'CPL',
240 letter_code => 'my code',
241 tables => $tables,
242 substitute => $substitute,
243 repeat => $repeat,
245 my $branch = GetBranchDetail('CPL');
246 my $my_title_letter = qq|$branch->{branchname} - $substitute->{status}|;
247 my $my_content_letter = qq|Dear Jane Smith,
248 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.
250 $branch->{branchname}
251 $branch->{branchaddress1}
252 URL: http://thisisatest.com
254 The following item(s) is/are currently $substitute->{status}:
256 <item> 1. $repeat->[0]->{itemcallnumber}, Barcode: $repeat->[0]->{barcode} </item>
257 <item> 2. $repeat->[1]->{itemcallnumber}, Barcode: $repeat->[1]->{barcode} </item>
259 Thank-you for your prompt attention to this matter.
260 Don't forget your date of birth: | . output_pref({ dt => $date, dateonly => 1 }) . q|.
261 Look at this wonderful biblio timestamp: | . output_pref({ dt => $date }) . ".\n";
262 is( $prepared_letter->{title}, $my_title_letter, 'GetPreparedLetter returns the title correctly' );
263 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
265 $prepared_letter = GetPreparedLetter((
266 module => 'my module',
267 branchcode => 'CPL',
268 letter_code => 'my code',
269 tables => $tables,
270 substitute => $substitute,
271 repeat => $repeat,
272 message_transport_type => 'sms',
274 $my_content_letter = qq|This is a SMS for an $substitute->{status}|;
275 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
277 $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>');});
279 my $booksellerid = C4::Bookseller::AddBookseller(
281 name => "my vendor",
282 address1 => "bookseller's address",
283 phone => "0123456",
284 active => 1,
285 deliverytime => 5,
288 { name => 'John Smith', phone => '0123456x1', claimacquisition => 1 },
289 { name => 'Leo Tolstoy', phone => '0123456x2', claimissues => 1 },
292 my $basketno = NewBasket($booksellerid, 1);
294 my $budgetid = C4::Budgets::AddBudget({
295 budget_code => "budget_code_test_letters",
296 budget_name => "budget_name_test_letters",
299 my $bib = MARC::Record->new();
300 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
301 $bib->append_fields(
302 MARC::Field->new('200', ' ', ' ', a => 'Silence in the library'),
304 } else {
305 $bib->append_fields(
306 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
310 ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
311 my $order = Koha::Acquisition::Order->new(
313 basketno => $basketno,
314 quantity => 1,
315 biblionumber => $biblionumber,
316 budget_id => $budgetid,
318 )->insert;
319 my $ordernumber = $order->{ordernumber};
321 C4::Acquisition::CloseBasket( $basketno );
322 my $err;
323 warning_like {
324 $err = SendAlerts( 'claimacquisition', [ $ordernumber ], 'TESTACQCLAIM' ) }
325 qr/^Bookseller .* without emails at/,
326 "SendAlerts prints a warning";
327 is($err->{'error'}, 'no_email', "Trying to send an alert when there's no e-mail results in an error");
329 my $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
330 $bookseller->{'contacts'}->[0]->email('testemail@mydomain.com');
331 C4::Bookseller::ModBookseller($bookseller);
332 $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
334 warning_is {
335 $err = SendAlerts( 'claimacquisition', [ $ordernumber ], 'TESTACQCLAIM' ) }
336 "Fake sendmail",
337 "SendAlerts is using the mocked sendmail routine";
339 is($err, 1, "Successfully sent claim");
340 is($mail{'To'}, 'testemail@mydomain.com', "mailto correct in sent claim");
341 is($mail{'Message'}, 'my vendor|John Smith|Ordernumber ' . $ordernumber . ' (Silence in the library) (1 ordered)', 'Claim notice text constructed successfully');
343 $dbh->rollback;