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>.
22 use Test::More tests => 45;
25 use C4::Biblio qw( AddBiblio );
30 use Koha::DateUtils qw( dt_from_string output_pref );
33 my $dbh = C4::Context->dbh;
36 $dbh->{AutoCommit} = 0;
37 $dbh->{RaiseError} = 1;
39 $dbh->do(q|DELETE FROM letter|);
40 $dbh->do(q|DELETE FROM message_queue|);
41 $dbh->do(q|DELETE FROM message_transport_types|);
43 my $date = dt_from_string;
44 my $borrowernumber = AddMember(
52 my $marc_record = MARC::Record->new;
53 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
55 # GetMessageTransportTypes
56 my $mtts = C4::Letters::GetMessageTransportTypes();
57 is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' );
60 INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
62 $mtts = C4::Letters::GetMessageTransportTypes();
63 is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
67 is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' );
70 borrowernumber => $borrowernumber,
71 message_transport_type => 'sms',
72 to_address => 'to@example.com',
73 from_address => 'from@example.com',
75 my $message_id = C4::Letters::EnqueueLetter($my_message);
76 is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' );
78 delete $my_message->{message_transport_type};
79 $my_message->{letter} = {
80 content => 'a message',
81 title => 'message title',
82 metadata => 'metadata',
83 code => 'TEST_MESSAGE',
84 content_type => 'text/plain',
86 $message_id = C4::Letters::EnqueueLetter($my_message);
87 is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' );
89 $my_message->{message_transport_type} = 'sms';
90 $message_id = C4::Letters::EnqueueLetter($my_message);
91 ok(defined $message_id && $message_id > 0, 'new message successfully queued');
95 my $messages = C4::Letters::GetQueuedMessages();
96 is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' );
98 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
99 is( @$messages, 1, 'one message stored for the borrower' );
100 is( $messages->[0]->{message_id}, $message_id, 'EnqueueLetter returns the message id correctly' );
101 is( $messages->[0]->{borrowernumber}, $borrowernumber, 'EnqueueLetter stores the borrower number correctly' );
102 is( $messages->[0]->{subject}, $my_message->{letter}->{title}, 'EnqueueLetter stores the subject correctly' );
103 is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' );
104 is( $messages->[0]->{message_transport_type}, $my_message->{message_transport_type}, 'EnqueueLetter stores the message type correctly' );
105 is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pending correctly' );
109 my $messages_processed = C4::Letters::SendQueuedMessages();
110 is($messages_processed, 1, 'all queued messages processed');
112 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
114 $messages->[0]->{status},
116 'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
121 my $letters = C4::Letters::GetLetters();
122 is( @$letters, 0, 'GetLetters returns the correct number of letters' );
124 my $title = q|<<branches.branchname>> - <<status>>|;
125 my $content = q|Dear <<borrowers.firstname>> <<borrowers.surname>>,
126 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.
128 <<branches.branchname>>
129 <<branches.branchaddress1>>
132 The following item(s) is/are currently <<status>>:
134 <item> <<count>>. <<items.itemcallnumber>>, Barcode: <<items.barcode>> </item>
136 Thank-you for your prompt attention to this matter.
137 Don't forget your date of birth: <<borrowers.dateofbirth>>.
138 Look at this wonderful biblio timestamp: <<biblio.timestamp>>.
141 $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 );
142 $letters = C4::Letters::GetLetters();
143 is( @$letters, 1, 'GetLetters returns the correct number of letters' );
144 is( $letters->[0]->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
145 is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' );
146 is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' );
147 is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' );
151 my $letter = C4::Letters::getletter('my module', 'my code', 'CPL', 'email');
152 is( $letter->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
153 is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' );
154 is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' );
155 is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' );
156 is( $letter->{is_html}, 1, 'GetLetters gets the boolean is_html correctly' );
157 is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
158 is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
159 is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
163 my $type = 'my type';
164 my $externalid = 'my external id';
165 my $alert_id = C4::Letters::addalert($borrowernumber, $type, $externalid);
166 isnt( $alert_id, undef, 'addalert does not return undef' );
168 my $alerts = C4::Letters::getalert($borrowernumber);
169 is( @$alerts, 1, 'addalert adds an alert' );
170 is( $alerts->[0]->{alertid}, $alert_id, 'addalert returns the alert id correctly' );
171 is( $alerts->[0]->{type}, $type, 'addalert stores the type correctly' );
172 is( $alerts->[0]->{externalid}, $externalid, 'addalert stores the externalid correctly' );
176 $alerts = C4::Letters::getalert($borrowernumber, $type);
177 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
178 $alerts = C4::Letters::getalert($borrowernumber, $type, $externalid);
179 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
180 $alerts = C4::Letters::getalert($borrowernumber, 'another type');
181 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
182 $alerts = C4::Letters::getalert($borrowernumber, $type, 'another external id');
183 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
188 C4::Letters::delalert();
190 isnt( $@, undef, 'delalert without argument returns an error' );
191 $alerts = C4::Letters::getalert($borrowernumber);
192 is( @$alerts, 1, 'delalert without argument does not remove an alert' );
194 C4::Letters::delalert($alert_id);
195 $alerts = C4::Letters::getalert($borrowernumber);
196 is( @$alerts, 0, 'delalert removes an alert' );
200 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
202 my $sms_content = 'This is a SMS for an <<status>>';
203 $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 );
206 borrowers => $borrowernumber,
208 biblio => $biblionumber,
215 itemcallnumber => 'my callnumber1',
219 itemcallnumber => 'my callnumber2',
223 my $prepared_letter = GetPreparedLetter((
224 module => 'my module',
226 letter_code => 'my code',
228 substitute => $substitute,
231 my $branch = GetBranchDetail('CPL');
232 my $my_title_letter = qq|$branch->{branchname} - $substitute->{status}|;
233 my $my_content_letter = qq|Dear Jane Smith,
234 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.
236 $branch->{branchname}
237 $branch->{branchaddress1}
238 URL: http://thisisatest.com
240 The following item(s) is/are currently $substitute->{status}:
242 <item> 1. $repeat->[0]->{itemcallnumber}, Barcode: $repeat->[0]->{barcode} </item>
243 <item> 2. $repeat->[1]->{itemcallnumber}, Barcode: $repeat->[1]->{barcode} </item>
245 Thank-you for your prompt attention to this matter.
246 Don't forget your date of birth: | . output_pref({ dt => $date, dateonly => 1 }) . q|.
247 Look at this wonderful biblio timestamp: | . output_pref({ dt => $date }) . ".\n";
248 is( $prepared_letter->{title}, $my_title_letter, 'GetPreparedLetter returns the title correctly' );
249 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
251 $prepared_letter = GetPreparedLetter((
252 module => 'my module',
254 letter_code => 'my code',
256 substitute => $substitute,
258 message_transport_type => 'sms',
260 $my_content_letter = qq|This is a SMS for an $substitute->{status}|;
261 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );