Bug 9016: Create a message for each transport type.
[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 => 5;
23
24 use C4::Context;
25 use C4::Letters;
26 use C4::Members;
27
28 my $dbh = C4::Context->dbh;
29
30 # Start transaction
31 $dbh->{AutoCommit} = 0;
32 $dbh->{RaiseError} = 1;
33
34 $dbh->do(q|DELETE FROM letter|);
35 $dbh->do(q|DELETE FROM message_queue|);
36 $dbh->do(q|DELETE FROM message_transport_types|);
37
38 my $borrowernumber = AddMember(
39     firstname    => 'Jane',
40     surname      => 'Smith',
41     categorycode => 'PT',
42     branchcode   => 'CPL',
43 );
44
45 $dbh->do(q|
46     INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
47 |);
48
49 my $mtts = C4::Letters::GetMessageTransportTypes();
50 is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
51
52 my $message_id = C4::Letters::EnqueueLetter({
53     borrowernumber         => $borrowernumber,
54     message_transport_type => 'sms',
55     to_address             => 'to@example.com',
56     from_address           => 'from@example.com',
57     letter => {
58         content      => 'a message',
59         title        => 'message title',
60         metadata     => 'metadata',
61         code         => 'TEST_MESSAGE',
62         content_type => 'text/plain',
63     },
64 });
65
66 ok(defined $message_id && $message_id > 0, 'new message successfully queued');
67
68 my $messages_processed = C4::Letters::SendQueuedMessages();
69 is($messages_processed, 1, 'all queued messages processed');
70
71 my $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
72 is(scalar(@$messages), 1, 'one message stored for the borrower');
73
74 is(
75     $messages->[0]->{status},
76     'failed',
77     'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
78 );
79
80 $dbh->rollback;