Bug 12499: adding units tests for C4::Letters
[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 C4::Context;
25 use C4::Letters;
26 use C4::Members;
27 use C4::Branch;
28 use t::lib::Mocks;
29
30 my $dbh = C4::Context->dbh;
31
32 # Start transaction
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
35
36 $dbh->do(q|DELETE FROM letter|);
37 $dbh->do(q|DELETE FROM message_queue|);
38 $dbh->do(q|DELETE FROM message_transport_types|);
39
40 my $borrowernumber = AddMember(
41     firstname    => 'Jane',
42     surname      => 'Smith',
43     categorycode => 'PT',
44     branchcode   => 'CPL',
45 );
46
47
48 # GetMessageTransportTypes
49 my $mtts = C4::Letters::GetMessageTransportTypes();
50 is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' );
51
52 $dbh->do(q|
53     INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
54 |);
55 $mtts = C4::Letters::GetMessageTransportTypes();
56 is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
57
58
59 # EnqueueLetter
60 is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' );
61
62 my $my_message = {
63     borrowernumber         => $borrowernumber,
64     message_transport_type => 'sms',
65     to_address             => 'to@example.com',
66     from_address           => 'from@example.com',
67 };
68 my $message_id = C4::Letters::EnqueueLetter($my_message);
69 is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' );
70
71 delete $my_message->{message_transport_type};
72 $my_message->{letter} = {
73     content      => 'a message',
74     title        => 'message title',
75     metadata     => 'metadata',
76     code         => 'TEST_MESSAGE',
77     content_type => 'text/plain',
78 };
79 $message_id = C4::Letters::EnqueueLetter($my_message);
80 is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' );
81
82 $my_message->{message_transport_type} = 'sms';
83 $message_id = C4::Letters::EnqueueLetter($my_message);
84 ok(defined $message_id && $message_id > 0, 'new message successfully queued');
85
86
87 # GetQueuedMessages
88 my $messages = C4::Letters::GetQueuedMessages();
89 is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' );
90
91 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
92 is( @$messages, 1, 'one message stored for the borrower' );
93 is( $messages->[0]->{message_id}, $message_id, 'EnqueueLetter returns the message id correctly' );
94 is( $messages->[0]->{borrowernumber}, $borrowernumber, 'EnqueueLetter stores the borrower number correctly' );
95 is( $messages->[0]->{subject}, $my_message->{letter}->{title}, 'EnqueueLetter stores the subject correctly' );
96 is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' );
97 is( $messages->[0]->{message_transport_type}, $my_message->{message_transport_type}, 'EnqueueLetter stores the message type correctly' );
98 is( $messages->[0]->{status}, 'pending', 'EnqueueLetter stores the status pending correctly' );
99
100
101 # SendQueuedMessages
102 my $messages_processed = C4::Letters::SendQueuedMessages();
103 is($messages_processed, 1, 'all queued messages processed');
104
105 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
106 is(
107     $messages->[0]->{status},
108     'failed',
109     'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
110 );
111
112
113 # GetLetters
114 my $letters = C4::Letters::GetLetters();
115 is( @$letters, 0, 'GetLetters returns the correct number of letters' );
116
117 my $title = q|<<branches.branchname>> - <<status>>|;
118 my $content = q|Dear <<borrowers.firstname>> <<borrowers.surname>>,
119 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.
120
121 <<branches.branchname>>
122 <<branches.branchaddress1>>
123 URL: <<OPACBaseURL>>
124
125 The following item(s) is/are currently <<status>>:
126
127 <item> <<count>>. <<items.itemcallnumber>>, Barcode: <<items.barcode>> </item>
128
129 Thank-you for your prompt attention to this matter.|;
130
131 $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 );
132 $letters = C4::Letters::GetLetters();
133 is( @$letters, 1, 'GetLetters returns the correct number of letters' );
134 is( $letters->[0]->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
135 is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' );
136 is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' );
137 is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' );
138
139
140 # getletter
141 my $letter = C4::Letters::getletter('my module', 'my code', 'CPL', 'email');
142 is( $letter->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
143 is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' );
144 is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' );
145 is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' );
146 is( $letter->{is_html}, 1, 'GetLetters gets the boolean is_html correctly' );
147 is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
148 is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
149 is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
150
151
152 # addalert
153 my $type = 'my type';
154 my $externalid = 'my external id';
155 my $alert_id = C4::Letters::addalert($borrowernumber, $type, $externalid);
156 isnt( $alert_id, undef, 'addalert does not return undef' );
157
158 my $alerts = C4::Letters::getalert($borrowernumber);
159 is( @$alerts, 1, 'addalert adds an alert' );
160 is( $alerts->[0]->{alertid}, $alert_id, 'addalert returns the alert id correctly' );
161 is( $alerts->[0]->{type}, $type, 'addalert stores the type correctly' );
162 is( $alerts->[0]->{externalid}, $externalid, 'addalert stores the externalid correctly' );
163
164
165 # getalert
166 $alerts = C4::Letters::getalert($borrowernumber, $type);
167 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
168 $alerts = C4::Letters::getalert($borrowernumber, $type, $externalid);
169 is( @$alerts, 1, 'getalert returns the correct number of alerts' );
170 $alerts = C4::Letters::getalert($borrowernumber, 'another type');
171 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
172 $alerts = C4::Letters::getalert($borrowernumber, $type, 'another external id');
173 is( @$alerts, 0, 'getalert returns the correct number of alerts' );
174
175
176 # delalert
177 eval {
178     C4::Letters::delalert();
179 };
180 isnt( $@, undef, 'delalert without argument returns an error' );
181 $alerts = C4::Letters::getalert($borrowernumber);
182 is( @$alerts, 1, 'delalert without argument does not remove an alert' );
183
184 C4::Letters::delalert($alert_id);
185 $alerts = C4::Letters::getalert($borrowernumber);
186 is( @$alerts, 0, 'delalert removes an alert' );
187
188
189 # GetPreparedLetter
190 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
191
192 $content = 'This is a SMS for an <<status>>';
193 $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, $content );
194
195 my $tables = {
196     borrowers => $borrowernumber,
197     branches => 'CPL',
198 };
199 my $substitute = {
200     status => 'overdue',
201 };
202 my $repeat = [
203     {
204         itemcallnumber => 'my callnumber1',
205         barcode        => '1234',
206     },
207     {
208         itemcallnumber => 'my callnumber2',
209         barcode        => '5678',
210     },
211 ];
212 my $prepared_letter = GetPreparedLetter((
213     module      => 'my module',
214     branchcode  => 'CPL',
215     letter_code => 'my code',
216     tables      => $tables,
217     substitute  => $substitute,
218     repeat      => $repeat,
219 ));
220 my $branch = GetBranchDetail('CPL');
221 my $my_title_letter = qq|$branch->{branchname} - $substitute->{status}|;
222 my $my_content_letter = qq|Dear Jane Smith,
223 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.
224
225 $branch->{branchname}
226 $branch->{branchaddress1}
227 URL: http://thisisatest.com
228
229 The following item(s) is/are currently $substitute->{status}:
230
231 <item> 1. $repeat->[0]->{itemcallnumber}, Barcode: $repeat->[0]->{barcode} </item>
232 <item> 2. $repeat->[1]->{itemcallnumber}, Barcode: $repeat->[1]->{barcode} </item>
233
234 Thank-you for your prompt attention to this matter.|;
235 is( $prepared_letter->{title}, $my_title_letter, 'GetPreparedLetter returns the title correctly' );
236 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
237
238 $prepared_letter = GetPreparedLetter((
239     module                 => 'my module',
240     branchcode             => 'CPL',
241     letter_code            => 'my code',
242     tables                 => $tables,
243     substitute             => $substitute,
244     repeat                 => $repeat,
245     message_transport_type => 'sms',
246 ));
247 $my_content_letter = qq|This is a SMS for an $substitute->{status}|;
248 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
249
250 $dbh->rollback;