Bug 12499: adding units tests for C4::Letters
[koha.git] / t / db_dependent / Letters.t
blobbf0c1a1cd3e1c64c00e44d549833eeae86692c00
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;
22 use Test::More tests => 45;
24 use C4::Context;
25 use C4::Letters;
26 use C4::Members;
27 use C4::Branch;
28 use t::lib::Mocks;
30 my $dbh = C4::Context->dbh;
32 # Start transaction
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
36 $dbh->do(q|DELETE FROM letter|);
37 $dbh->do(q|DELETE FROM message_queue|);
38 $dbh->do(q|DELETE FROM message_transport_types|);
40 my $borrowernumber = AddMember(
41 firstname => 'Jane',
42 surname => 'Smith',
43 categorycode => 'PT',
44 branchcode => 'CPL',
48 # GetMessageTransportTypes
49 my $mtts = C4::Letters::GetMessageTransportTypes();
50 is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' );
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' );
59 # EnqueueLetter
60 is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' );
62 my $my_message = {
63 borrowernumber => $borrowernumber,
64 message_transport_type => 'sms',
65 to_address => 'to@example.com',
66 from_address => 'from@example.com',
68 my $message_id = C4::Letters::EnqueueLetter($my_message);
69 is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' );
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',
79 $message_id = C4::Letters::EnqueueLetter($my_message);
80 is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' );
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');
87 # GetQueuedMessages
88 my $messages = C4::Letters::GetQueuedMessages();
89 is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' );
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' );
101 # SendQueuedMessages
102 my $messages_processed = C4::Letters::SendQueuedMessages();
103 is($messages_processed, 1, 'all queued messages processed');
105 $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
107 $messages->[0]->{status},
108 'failed',
109 'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
113 # GetLetters
114 my $letters = C4::Letters::GetLetters();
115 is( @$letters, 0, 'GetLetters returns the correct number of letters' );
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.
121 <<branches.branchname>>
122 <<branches.branchaddress1>>
123 URL: <<OPACBaseURL>>
125 The following item(s) is/are currently <<status>>:
127 <item> <<count>>. <<items.itemcallnumber>>, Barcode: <<items.barcode>> </item>
129 Thank-you for your prompt attention to this matter.|;
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' );
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' );
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' );
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' );
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' );
176 # delalert
177 eval {
178 C4::Letters::delalert();
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' );
184 C4::Letters::delalert($alert_id);
185 $alerts = C4::Letters::getalert($borrowernumber);
186 is( @$alerts, 0, 'delalert removes an alert' );
189 # GetPreparedLetter
190 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
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 );
195 my $tables = {
196 borrowers => $borrowernumber,
197 branches => 'CPL',
199 my $substitute = {
200 status => 'overdue',
202 my $repeat = [
204 itemcallnumber => 'my callnumber1',
205 barcode => '1234',
208 itemcallnumber => 'my callnumber2',
209 barcode => '5678',
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,
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.
225 $branch->{branchname}
226 $branch->{branchaddress1}
227 URL: http://thisisatest.com
229 The following item(s) is/are currently $substitute->{status}:
231 <item> 1. $repeat->[0]->{itemcallnumber}, Barcode: $repeat->[0]->{barcode} </item>
232 <item> 2. $repeat->[1]->{itemcallnumber}, Barcode: $repeat->[1]->{barcode} </item>
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' );
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',
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' );
250 $dbh->rollback;