View | Details | Raw Unified | Return to bug 12499
Collapse All | Expand All

(-)a/t/db_dependent/Letters.t (-17 / +186 lines)
Lines 19-29 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 45;
23
23
24
use C4::Context;
24
use C4::Context;
25
use C4::Letters;
25
use C4::Letters;
26
use C4::Members;
26
use C4::Members;
27
use C4::Branch;
28
use t::lib::Mocks;
27
29
28
my $dbh = C4::Context->dbh;
30
my $dbh = C4::Context->dbh;
29
31
Lines 42-80 my $borrowernumber = AddMember( Link Here
42
    branchcode   => 'CPL',
44
    branchcode   => 'CPL',
43
);
45
);
44
46
47
48
# GetMessageTransportTypes
49
my $mtts = C4::Letters::GetMessageTransportTypes();
50
is( @$mtts, 0, 'GetMessageTransportTypes returns the correct number of message types' );
51
45
$dbh->do(q|
52
$dbh->do(q|
46
    INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
53
    INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
47
|);
54
|);
48
55
$mtts = C4::Letters::GetMessageTransportTypes();
49
my $mtts = C4::Letters::GetMessageTransportTypes();
50
is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
56
is_deeply( $mtts, ['email', 'phone', 'print', 'sms'], 'GetMessageTransportTypes returns all values' );
51
57
52
my $message_id = C4::Letters::EnqueueLetter({
58
59
# EnqueueLetter
60
is( C4::Letters::EnqueueLetter(), undef, 'EnqueueLetter without argument returns undef' );
61
62
my $my_message = {
53
    borrowernumber         => $borrowernumber,
63
    borrowernumber         => $borrowernumber,
54
    message_transport_type => 'sms',
64
    message_transport_type => 'sms',
55
    to_address             => 'to@example.com',
65
    to_address             => 'to@example.com',
56
    from_address           => 'from@example.com',
66
    from_address           => 'from@example.com',
57
    letter => {
67
};
58
        content      => 'a message',
68
my $message_id = C4::Letters::EnqueueLetter($my_message);
59
        title        => 'message title',
69
is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' );
60
        metadata     => 'metadata',
70
61
        code         => 'TEST_MESSAGE',
71
delete $my_message->{message_transport_type};
62
        content_type => 'text/plain',
72
$my_message->{letter} = {
63
    },
73
    content      => 'a message',
64
});
74
    title        => 'message title',
65
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);
66
ok(defined $message_id && $message_id > 0, 'new message successfully queued');
84
ok(defined $message_id && $message_id > 0, 'new message successfully queued');
67
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
68
my $messages_processed = C4::Letters::SendQueuedMessages();
102
my $messages_processed = C4::Letters::SendQueuedMessages();
69
is($messages_processed, 1, 'all queued messages processed');
103
is($messages_processed, 1, 'all queued messages processed');
70
104
71
my $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
105
$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber });
72
is(scalar(@$messages), 1, 'one message stored for the borrower');
73
74
is(
106
is(
75
    $messages->[0]->{status},
107
    $messages->[0]->{status},
76
    'failed',
108
    'failed',
77
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
109
    'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
78
);
110
);
79
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
80
$dbh->rollback;
250
$dbh->rollback;
81
- 

Return to bug 12499