89e511a7a3aaa1aabced71b932ded51d5b91e7dd
[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
22 use Test::More tests => 45;
23
24 use MARC::Record;
25 use C4::Biblio qw( AddBiblio );
26 use C4::Context;
27 use C4::Letters;
28 use C4::Members;
29 use C4::Branch;
30 use Koha::DateUtils qw( dt_from_string output_pref );
31 use t::lib::Mocks;
32
33 my $dbh = C4::Context->dbh;
34
35 # Start transaction
36 $dbh->{AutoCommit} = 0;
37 $dbh->{RaiseError} = 1;
38
39 $dbh->do(q|DELETE FROM letter|);
40 $dbh->do(q|DELETE FROM message_queue|);
41 $dbh->do(q|DELETE FROM message_transport_types|);
42
43 my $date = dt_from_string;
44 my $borrowernumber = AddMember(
45     firstname    => 'Jane',
46     surname      => 'Smith',
47     categorycode => 'PT',
48     branchcode   => 'CPL',
49     dateofbirth  => $date,
50 );
51
52 my $marc_record = MARC::Record->new;
53 my( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
54
55 # GetMessageTransportTypes
56 my $mtts = C4::Letters::GetMessageTransportTypes();
57 is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' );
58
59 $dbh->do(q|
60     INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
61 |);
62 $mtts = C4::Letters::GetMessageTransportTypes();
63 is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
64
65
66 # EnqueueLetter
67 is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' );
68
69 my $my_message = {
70     borrowernumber         => $borrowernumber,
71     message_transport_type => 'sms',
72     to_address             => 'to@example.com',
73     from_address           => 'from@example.com',
74 };
75 my $message_id = C4::Letters::EnqueueLetter($my_message);
76 is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' );
77
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',
85 };
86 $message_id = C4::Letters::EnqueueLetter($my_message);
87 is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' );
88
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');
92
93
94 # GetQueuedMessages
95 my $messages = C4::Letters::GetQueuedMessages();
96 is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' );
97
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' );
106
107
108 # SendQueuedMessages
109 my $messages_processed = C4::Letters::SendQueuedMessages();
110 is($messages_processed, 1, 'all queued messages processed');
111
112 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
113 is(
114     $messages->[0]->{status},
115     'failed',
116     'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
117 );
118
119
120 # GetLetters
121 my $letters = C4::Letters::GetLetters();
122 is( @$letters, 0, 'GetLetters returns the correct number of letters' );
123
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.
127
128 <<branches.branchname>>
129 <<branches.branchaddress1>>
130 URL: <<OPACBaseURL>>
131
132 The following item(s) is/are currently <<status>>:
133
134 <item> <<count>>. <<items.itemcallnumber>>, Barcode: <<items.barcode>> </item>
135
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>>.
139 |;
140
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' );
148
149
150 # getletter
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' );
160
161
162 # addalert
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' );
167
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' );
173
174
175 # getalert
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' );
184
185
186 # delalert
187 eval {
188     C4::Letters::delalert();
189 };
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' );
193
194 C4::Letters::delalert($alert_id);
195 $alerts = C4::Letters::getalert($borrowernumber);
196 is( @$alerts, 0, 'delalert removes an alert' );
197
198
199 # GetPreparedLetter
200 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
201
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 );
204
205 my $tables = {
206     borrowers => $borrowernumber,
207     branches => 'CPL',
208     biblio => $biblionumber,
209 };
210 my $substitute = {
211     status => 'overdue',
212 };
213 my $repeat = [
214     {
215         itemcallnumber => 'my callnumber1',
216         barcode        => '1234',
217     },
218     {
219         itemcallnumber => 'my callnumber2',
220         barcode        => '5678',
221     },
222 ];
223 my $prepared_letter = GetPreparedLetter((
224     module      => 'my module',
225     branchcode  => 'CPL',
226     letter_code => 'my code',
227     tables      => $tables,
228     substitute  => $substitute,
229     repeat      => $repeat,
230 ));
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.
235
236 $branch->{branchname}
237 $branch->{branchaddress1}
238 URL: http://thisisatest.com
239
240 The following item(s) is/are currently $substitute->{status}:
241
242 <item> 1. $repeat->[0]->{itemcallnumber}, Barcode: $repeat->[0]->{barcode} </item>
243 <item> 2. $repeat->[1]->{itemcallnumber}, Barcode: $repeat->[1]->{barcode} </item>
244
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' );
250
251 $prepared_letter = GetPreparedLetter((
252     module                 => 'my module',
253     branchcode             => 'CPL',
254     letter_code            => 'my code',
255     tables                 => $tables,
256     substitute             => $substitute,
257     repeat                 => $repeat,
258     message_transport_type => 'sms',
259 ));
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' );
262
263 $dbh->rollback;