Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / Koha / Email.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 5;
21
22 use Test::MockModule;
23 use Test::Exception;
24
25 use t::lib::Mocks;
26
27 use_ok('Koha::Email');
28
29 subtest 'create() tests' => sub {
30
31     plan tests => 25;
32
33     t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
34
35     my $html_body = '<h1>Title</h1><p>Message</p>';
36     my $text_body = "#Title: Message";
37
38     my $email = Koha::Email->create(
39         {
40             from        => 'Fróm <from@example.com>',
41             to          => 'Tö <to@example.com>',
42             cc          => 'cc@example.com',
43             bcc         => 'bcc@example.com',
44             reply_to    => 'reply_to@example.com',
45             sender      => 'sender@example.com',
46             subject     => 'Some subject',
47             html_body   => $html_body,
48             body_params => { charset => 'iso-8859-1' },
49         }
50     );
51
52     is( $email->email->header('From'), 'Fróm <from@example.com>', 'Value set correctly' );
53     is( $email->email->header('To'), 'Tö <to@example.com>', 'Value set correctly' );
54     is( $email->email->header('Cc'), 'cc@example.com', 'Value set correctly' );
55     is( $email->email->header('Bcc'), 'bcc@example.com', 'Value set correctly' );
56     is( $email->email->header('Reply-To'), 'reply_to@example.com', 'Value set correctly' );
57     is( $email->email->header('Sender'), 'sender@example.com', 'Value set correctly' );
58     is( $email->email->header('Subject'), 'Some subject', 'Value set correctly' );
59     is( $email->email->header('X-Mailer'), 'Koha', 'Value set correctly' );
60     is( $email->email->body, $html_body, "Body set correctly" );
61     like( $email->email->content_type, qr|text/html|, "Content type set correctly");
62     like( $email->email->content_type, qr|charset="?iso-8859-1"?|, "Charset set correctly");
63     like( $email->email->header('Message-ID'), qr/\<.*@.*\>/, 'Value set correctly' );
64
65     $email = Koha::Email->create(
66         {
67             from        => 'from@8.8.8.8',
68             to          => 'to@example.com',
69             bcc         => 'root@localhost',
70         }
71     );
72
73     is( $email->email->header('Bcc'), 'root@localhost', 'Non-FQDN (@localhost) supported' );
74     is( $email->email->header('From'), 'from@8.8.8.8', 'IPs supported' );
75
76     t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'catchall@example.com' );
77     t::lib::Mocks::mock_preference( 'ReplytoDefault', 'replytodefault@example.com' );
78     t::lib::Mocks::mock_preference( 'ReturnpathDefault', 'returnpathdefault@example.com' );
79     t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'kohaadminemailaddress@example.com' );
80
81     $email = Koha::Email->create(
82         {
83             to        => 'to@example.com',
84             cc        => 'cc@example.com',
85             bcc       => 'bcc@example.com',
86             text_body => $text_body,
87         }
88     );
89
90     is( $email->email->header('From'), 'kohaadminemailaddress@example.com', 'KohaAdminEmailAddress is picked when no from passed' );
91     is( $email->email->header('To'), 'catchall@example.com', 'SendAllEmailsTo overloads any address' );
92     is( $email->email->header('Cc'), undef, 'SendAllEmailsTo overloads any address' );
93     is( $email->email->header('Bcc'), undef, 'SendAllEmailsTo overloads any address' );
94     is( $email->email->header('Reply-To'), 'replytodefault@example.com', 'ReplytoDefault picked when replyto not passed' );
95     is( $email->email->header('Sender'), 'returnpathdefault@example.com', 'ReturnpathDefault picked when sender not passed' );
96     is( $email->email->header('Subject'), '', 'No subject passed, empty string' );
97     is( $email->email->body, $text_body, "Body set correctly" );
98     like( $email->email->content_type, qr|text/plain|, "Content type set correctly");
99     like( $email->email->content_type, qr|charset="?utf-8"?|, "Charset set correctly");
100
101     subtest 'exception cases' => sub {
102
103         plan tests => 16;
104
105         throws_ok
106             { Koha::Email->create({ from => 'not_an_email' }); }
107             'Koha::Exceptions::BadParameter',
108             'Exception thrown correctly';
109
110         like( "$@", qr/Invalid 'from' parameter: not_an_email/, 'Exception message correct' );
111
112         t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'not_an_email' );
113
114         throws_ok
115             { Koha::Email->create({  }); }
116             'Koha::Exceptions::BadParameter',
117             'Exception thrown correctly';
118
119         like( "$@", qr/Invalid 'from' parameter: not_an_email/, 'Exception message correct' );
120
121         t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'tomasito@mail.com' );
122         t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
123
124         throws_ok
125             { Koha::Email->create({ to => 'not_an_email' }); }
126             'Koha::Exceptions::BadParameter',
127             'Exception thrown correctly';
128
129         like( "$@", qr/Invalid 'to' parameter: not_an_email/, 'Exception message correct' );
130
131         t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'not_an_email' );
132
133         throws_ok
134             { Koha::Email->create({  }); }
135             'Koha::Exceptions::BadParameter',
136             'Exception thrown correctly';
137
138         like( "$@", qr/Invalid 'to' parameter: not_an_email/, 'Exception message correct' );
139
140         t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
141
142         throws_ok
143             { Koha::Email->create(
144                 {
145                     to       => 'tomasito@mail.com',
146                     reply_to => 'not_an_email'
147                 }
148               ); }
149             'Koha::Exceptions::BadParameter',
150             'Exception thrown correctly';
151
152         like( "$@", qr/Invalid 'reply_to' parameter: not_an_email/, 'Exception message correct' );
153
154         throws_ok
155             { Koha::Email->create(
156                 {
157                     to     => 'tomasito@mail.com',
158                     sender => 'not_an_email'
159                 }
160               ); }
161             'Koha::Exceptions::BadParameter',
162             'Exception thrown correctly';
163
164         like( "$@", qr/Invalid 'sender' parameter: not_an_email/, 'Exception message correct' );
165
166         throws_ok
167             { Koha::Email->create(
168                 {
169                     to => 'tomasito@mail.com',
170                     cc => 'not_an_email'
171                 }
172               ); }
173             'Koha::Exceptions::BadParameter',
174             'Exception thrown correctly';
175
176         like( "$@", qr/Invalid 'cc' parameter: not_an_email/, 'Exception message correct' );
177
178         throws_ok
179             { Koha::Email->create(
180                 {
181                     to  => 'tomasito@mail.com',
182                     bcc => 'not_an_email'
183                 }
184               ); }
185             'Koha::Exceptions::BadParameter',
186             'Exception thrown correctly';
187
188         like( "$@", qr/Invalid 'bcc' parameter: not_an_email/, 'Exception message correct' );
189     };
190 };
191
192 subtest 'send_or_die() tests' => sub {
193
194     plan tests => 7;
195
196     my $email;
197     my $args;
198
199     my $transport = "Hi there!";
200
201     my $mocked_email_simple = Test::MockModule->new('Email::Sender::Simple');
202     $mocked_email_simple->mock(
203         'send',
204         sub {
205             my @params = @_;
206             $email = $params[1];
207             $args  = $params[2];
208             return;
209         }
210     );
211
212     my $html_body = '<h1>Title</h1><p>Message</p>';
213     my $THE_email = Koha::Email->create(
214         {
215             from      => 'from@example.com',
216             to        => 'to@example.com',
217             cc        => 'cc@example.com',
218             reply_to  => 'reply_to@example.com',
219             sender    => 'sender@example.com',
220             html_body => $html_body
221         }
222     );
223
224     my @bcc = ( 'bcc_1@example.com', 'bcc_2@example.com' );
225
226     $THE_email->bcc(@bcc);
227
228     is(
229         $THE_email->email->header_str('Bcc'),
230         join( ', ', @bcc ),
231         'Bcc header set correctly'
232     );
233
234     $THE_email->send_or_die(
235         { transport => $transport, to => ['tomasito@mail.com'], from => 'returns@example.com' } );
236     is_deeply( $args->{to}, ['tomasito@mail.com'],
237         'If explicitly passed, "to" is preserved' );
238     is( $args->{from}, 'returns@example.com', 'If explicitly pass, "from" is preserved');
239
240     $THE_email->send_or_die( { transport => $transport } );
241     my @to = sort @{ $args->{to} };
242     is_deeply(
243         [@to],
244         [
245             'bcc_1@example.com', 'bcc_2@example.com',
246             'cc@example.com',    'to@example.com',
247         ],
248         'If "to" is not explicitly passed, extract recipients from headers'
249     );
250     is( $email->header_str('Bcc'), undef, 'The Bcc header is unset' );
251     my $from = $args->{from};
252     is( $from, 'sender@example.com', 'If "from" is not explicitly passed, extract from Sender header' );
253     is( $email->header_str('Sender'), undef, 'The Sender header is unset' );
254 };
255
256 subtest 'is_valid' => sub {
257     plan tests => 8;
258
259     is(Koha::Email->is_valid('Fróm <from@example.com>'), 1);
260     is(Koha::Email->is_valid('from@example.com'), 1);
261     is(Koha::Email->is_valid('<from@example.com>'), 1);
262     is(Koha::Email->is_valid('root@localhost'), 1); # See bug 28017
263
264     is(Koha::Email->is_valid('<from@fróm.com>'), 0); # "In accordance with RFC 822 and its descendants, this module demands that email addresses be ASCII only"
265     isnt(Koha::Email->is_valid('@example.com'), 1);
266     isnt(Koha::Email->is_valid('example.com'), 1);
267     isnt(Koha::Email->is_valid('from'), 1);
268 };
269
270 subtest 'new_from_string() tests' => sub {
271
272     plan tests => 1;
273
274     my $html_body = '<h1>Title</h1><p>Message</p>';
275     my $email_1 = Koha::Email->create(
276         {
277             from        => 'Fróm <from@example.com>',
278             to          => 'Tö <to@example.com>',
279             cc          => 'cc@example.com',
280             bcc         => 'bcc@example.com',
281             reply_to    => 'reply_to@example.com',
282             sender      => 'sender@example.com',
283             subject     => 'Some subject',
284             html_body   => $html_body,
285             body_params => { charset => 'iso-8859-1' },
286         }
287     );
288
289     my $string = $email_1->as_string;
290     my $email_2 = Koha::Email->new_from_string( $string );
291
292     is( $email_1->as_string, $email_2->as_string, 'Emails match' );
293 };