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>.
21 use Test::More tests => 56;
27 my $module = new Test::MockModule('Mail::Sendmail');
36 use_ok('C4::Context');
37 use_ok('C4::Members');
39 use_ok('C4::Acquisition');
41 use_ok('C4::Bookseller');
42 use_ok('C4::Letters');
44 use Koha::DateUtils qw( dt_from_string output_pref );
46 my $dbh = C4::Context->dbh;
49 $dbh->{AutoCommit} = 0;
50 $dbh->{RaiseError} = 1;
52 $dbh->do(q|DELETE FROM letter|);
53 $dbh->do(q|DELETE FROM message_queue|);
54 $dbh->do(q|DELETE FROM message_transport_types|);
56 my $date = dt_from_string;
57 my $borrowernumber = AddMember(
65 my $marc_record = MARC::Record->new;
66 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
68 # GetMessageTransportTypes
69 my $mtts = C4::Letters::GetMessageTransportTypes();
70 is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' );
73 INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
75 $mtts = C4::Letters::GetMessageTransportTypes();
76 is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
80 is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' );
83 borrowernumber => $borrowernumber,
84 message_transport_type => 'sms',
85 to_address => 'to@example.com',
86 from_address => 'from@example.com',
88 my $message_id = C4::Letters::EnqueueLetter($my_message);
89 is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' );
91 delete $my_message->{message_transport_type};
92 $my_message->{letter} = {
93 content => 'a message',
94 title => 'message title',
95 metadata => 'metadata',
96 code => 'TEST_MESSAGE',
97 content_type => 'text/plain',
99 $message_id = C4::Letters::EnqueueLetter($my_message);
100 is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' );
102 $my_message->{message_transport_type} = 'sms';
103 $message_id = C4::Letters::EnqueueLetter($my_message);
104 ok(defined $message_id && $message_id > 0, 'new message successfully queued');
108 my $messages = C4::Letters::GetQueuedMessages();
109 is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' );
111 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
112 is( @$messages, 1, 'one message stored for the borrower' );
113 is( $messages->[0]->{message_id}, $message_id, 'EnqueueLetter returns the message id correctly' );
114 is( $messages->[0]->{borrowernumber}, $borrowernumber, 'EnqueueLetter stores the borrower number correctly' );
115 is( $messages->[0]->{subject}, $my_message->{letter}->{title}, 'EnqueueLetter stores the subject correctly' );
116 is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' );
117 is( $messages->[0]->{message_transport_type}, $my_message->{message_transport_type}, 'EnqueueLetter stores the message type correctly' );
118 is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pending correctly' );
122 my $messages_processed = C4::Letters::SendQueuedMessages();
123 is($messages_processed, 1, 'all queued messages processed');
125 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
127 $messages->[0]->{status},
129 'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
133 my $letters = C4::Letters::GetLetters();
134 is( @$letters, 0, 'GetLetters returns the correct number of letters' );
136 my $title = q|<<branches.branchname>> - <<status>>|;
137 my $content = q|Dear <<borrowers.firstname>> <<borrowers.surname>>,
138 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.
140 <<branches.branchname>>
141 <<branches.branchaddress1>>
144 The following item(s) is/are currently <<status>>:
146 <item> <<count>>. <<items.itemcallnumber>>, Barcode: <<items.barcode>> </item>
148 Thank-you for your prompt attention to this matter.
149 Don't forget your date of birth: <<borrowers.dateofbirth>>.
150 Look at this wonderful biblio timestamp: <<biblio.timestamp>>.
153 $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 );
154 $letters = C4::Letters::GetLetters();
155 is( @$letters, 1, 'GetLetters returns the correct number of letters' );
156 is( $letters->[0]->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
157 is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' );
158 is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' );
159 is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' );
163 my $letter = C4::Letters::getletter('my module', 'my code', 'CPL', 'email');
164 is( $letter->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
165 is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' );
166 is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' );
167 is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' );
168 is( $letter->{is_html}, 1, 'GetLetters gets the boolean is_html correctly' );
169 is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
170 is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
171 is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
175 my $type = 'my type';
176 my $externalid = 'my external id';
177 my $alert_id = C4::Letters::addalert($borrowernumber, $type, $externalid);
178 isnt( $alert_id, undef, 'addalert does not return undef' );
180 my $alerts = C4::Letters::getalert($borrowernumber);
181 is( @$alerts, 1, 'addalert adds an alert' );
182 is( $alerts->[0]->{alertid}, $alert_id, 'addalert returns the alert id correctly' );
183 is( $alerts->[0]->{type}, $type, 'addalert stores the type correctly' );
184 is( $alerts->[0]->{externalid}, $externalid, 'addalert stores the externalid correctly' );
188 $alerts = C4::Letters::getalert($borrowernumber, $type);
189 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
190 $alerts = C4::Letters::getalert($borrowernumber, $type, $externalid);
191 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
192 $alerts = C4::Letters::getalert($borrowernumber, 'another type');
193 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
194 $alerts = C4::Letters::getalert($borrowernumber, $type, 'another external id');
195 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
200 C4::Letters::delalert();
202 isnt( $@, undef, 'delalert without argument returns an error' );
203 $alerts = C4::Letters::getalert($borrowernumber);
204 is( @$alerts, 1, 'delalert without argument does not remove an alert' );
206 C4::Letters::delalert($alert_id);
207 $alerts = C4::Letters::getalert($borrowernumber);
208 is( @$alerts, 0, 'delalert removes an alert' );
212 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
214 my $sms_content = 'This is a SMS for an <<status>>';
215 $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 );
218 borrowers => $borrowernumber,
220 biblio => $biblionumber,
227 itemcallnumber => 'my callnumber1',
231 itemcallnumber => 'my callnumber2',
235 my $prepared_letter = GetPreparedLetter((
236 module => 'my module',
238 letter_code => 'my code',
240 substitute => $substitute,
243 my $branch = GetBranchDetail('CPL');
244 my $my_title_letter = qq|$branch->{branchname} - $substitute->{status}|;
245 my $my_content_letter = qq|Dear Jane Smith,
246 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.
248 $branch->{branchname}
249 $branch->{branchaddress1}
250 URL: http://thisisatest.com
252 The following item(s) is/are currently $substitute->{status}:
254 <item> 1. $repeat->[0]->{itemcallnumber}, Barcode: $repeat->[0]->{barcode} </item>
255 <item> 2. $repeat->[1]->{itemcallnumber}, Barcode: $repeat->[1]->{barcode} </item>
257 Thank-you for your prompt attention to this matter.
258 Don't forget your date of birth: | . output_pref({ dt => $date, dateonly => 1 }) . q|.
259 Look at this wonderful biblio timestamp: | . output_pref({ dt => $date }) . ".\n";
260 is( $prepared_letter->{title}, $my_title_letter, 'GetPreparedLetter returns the title correctly' );
261 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
263 $prepared_letter = GetPreparedLetter((
264 module => 'my module',
266 letter_code => 'my code',
268 substitute => $substitute,
270 message_transport_type => 'sms',
272 $my_content_letter = qq|This is a SMS for an $substitute->{status}|;
273 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
275 $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>');});
277 my $booksellerid = C4::Bookseller::AddBookseller(
280 address1 => "bookseller's address",
286 { name => 'John Smith', phone => '0123456x1', claimacquisition => 1 },
287 { name => 'Leo Tolstoy', phone => '0123456x2', claimissues => 1 },
290 my $basketno = NewBasket($booksellerid, 1);
292 my $budgetid = C4::Budgets::AddBudget({
293 budget_code => "budget_code_test_letters",
294 budget_name => "budget_name_test_letters",
298 my $bib = MARC::Record->new();
299 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
301 MARC::Field->new('200', ' ', ' ', a => 'Silence in the library'),
305 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
309 ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
310 ( undef, $ordernumber ) = C4::Acquisition::NewOrder(
312 basketno => $basketno,
314 biblionumber => $biblionumber,
315 budget_id => $budgetid,
319 C4::Acquisition::CloseBasket( $basketno );
322 warn "This test may issue a warning. Please ignore it.\n";
323 $err = SendAlerts( 'claimacquisition', [ $ordernumber ], 'TESTACQCLAIM' );
325 is($err->{'error'}, 'no_email', "Trying to send an alert when there's no e-mail results in an error");
327 my $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
328 $bookseller->{'contacts'}->[0]->email('testemail@mydomain.com');
329 C4::Bookseller::ModBookseller($bookseller);
330 $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
333 $err = SendAlerts( 'claimacquisition', [ $ordernumber ], 'TESTACQCLAIM' );
335 is($err, 1, "Successfully sent claim");
336 is($mail{'To'}, 'testemail@mydomain.com');
337 is($mail{'Message'}, 'my vendor|John Smith|<order>Ordernumber ' . $ordernumber . ' (Silence in the library) (1 ordered)</order>', 'Claim notice text constructed successfully');