Line 0
Link Here
|
|
|
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 => 2; |
21 |
use Test::Exception; |
22 |
|
23 |
use t::lib::Mocks; |
24 |
|
25 |
use_ok('Koha::Email'); |
26 |
|
27 |
subtest 'create() tests' => sub { |
28 |
|
29 |
plan tests => 23; |
30 |
|
31 |
t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef ); |
32 |
|
33 |
my $html_body = '<h1>Title</h1><p>Message</p>'; |
34 |
my $text_body = "#Title: Message"; |
35 |
|
36 |
my $email = Koha::Email->create( |
37 |
{ |
38 |
from => 'from@example.com', |
39 |
to => 'to@example.com', |
40 |
cc => 'cc@example.com', |
41 |
bcc => 'bcc@example.com', |
42 |
reply_to => 'reply_to@example.com', |
43 |
sender => 'sender@example.com', |
44 |
subject => 'Some subject', |
45 |
html_body => $html_body, |
46 |
body_params => { charset => 'iso-8859-1' }, |
47 |
} |
48 |
); |
49 |
|
50 |
is( $email->email->header('From'), 'from@example.com', 'Value set correctly' ); |
51 |
is( $email->email->header('To'), 'to@example.com', 'Value set correctly' ); |
52 |
is( $email->email->header('Cc'), 'cc@example.com', 'Value set correctly' ); |
53 |
is( $email->email->header('Bcc'), 'bcc@example.com', 'Value set correctly' ); |
54 |
is( $email->email->header('ReplyTo'), 'reply_to@example.com', 'Value set correctly' ); |
55 |
is( $email->email->header('Sender'), 'sender@example.com', 'Value set correctly' ); |
56 |
is( $email->email->header('Subject'), 'Some subject', 'Value set correctly' ); |
57 |
is( $email->email->header('X-Mailer'), 'Koha', 'Value set correctly' ); |
58 |
is( $email->email->body, $html_body, "Body set correctly" ); |
59 |
like( $email->email->content_type, qr|text/html|, "Content type set correctly"); |
60 |
like( $email->email->content_type, qr|charset="?iso-8859-1"?|, "Charset set correctly"); |
61 |
like( $email->email->header('Message-ID'), qr/\<.*@.*\>/, 'Value set correctly' ); |
62 |
|
63 |
t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'catchall@example.com' ); |
64 |
t::lib::Mocks::mock_preference( 'ReplytoDefault', 'replytodefault@example.com' ); |
65 |
t::lib::Mocks::mock_preference( 'ReturnpathDefault', 'returnpathdefault@example.com' ); |
66 |
t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'kohaadminemailaddress@example.com' ); |
67 |
|
68 |
$email = Koha::Email->create( |
69 |
{ |
70 |
to => 'to@example.com', |
71 |
cc => 'cc@example.com', |
72 |
bcc => 'bcc@example.com', |
73 |
text_body => $text_body, |
74 |
} |
75 |
); |
76 |
|
77 |
is( $email->email->header('From'), 'kohaadminemailaddress@example.com', 'KohaAdminEmailAddress is picked when no from passed' ); |
78 |
is( $email->email->header('To'), 'catchall@example.com', 'SendAllEmailsTo overloads any address' ); |
79 |
is( $email->email->header('Cc'), undef, 'SendAllEmailsTo overloads any address' ); |
80 |
is( $email->email->header('Bcc'), undef, 'SendAllEmailsTo overloads any address' ); |
81 |
is( $email->email->header('ReplyTo'), 'replytodefault@example.com', 'ReplytoDefault picked when replyto not passed' ); |
82 |
is( $email->email->header('Sender'), 'returnpathdefault@example.com', 'ReturnpathDefault picked when sender not passed' ); |
83 |
is( $email->email->header('Subject'), '', 'No subject passed, empty string' ); |
84 |
is( $email->email->body, $text_body, "Body set correctly" ); |
85 |
like( $email->email->content_type, qr|text/plain|, "Content type set correctly"); |
86 |
like( $email->email->content_type, qr|charset="?utf-8"?|, "Charset set correctly"); |
87 |
|
88 |
subtest 'exception cases' => sub { |
89 |
|
90 |
plan tests => 16; |
91 |
|
92 |
throws_ok |
93 |
{ Koha::Email->create({ from => 'not_an_email' }); } |
94 |
'Koha::Exceptions::BadParameter', |
95 |
'Exception thrown correctly'; |
96 |
|
97 |
is( "$@", q{Invalid 'from' parameter: not_an_email}, 'Exception message correct' ); |
98 |
|
99 |
t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'not_an_email' ); |
100 |
|
101 |
throws_ok |
102 |
{ Koha::Email->create({ }); } |
103 |
'Koha::Exceptions::BadParameter', |
104 |
'Exception thrown correctly'; |
105 |
|
106 |
is( "$@", q{Invalid 'from' parameter: not_an_email}, 'Exception message correct' ); |
107 |
|
108 |
t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'tomasito@mail.com' ); |
109 |
t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef ); |
110 |
|
111 |
throws_ok |
112 |
{ Koha::Email->create({ to => 'not_an_email' }); } |
113 |
'Koha::Exceptions::BadParameter', |
114 |
'Exception thrown correctly'; |
115 |
|
116 |
is( "$@", q{Invalid 'to' parameter: not_an_email}, 'Exception message correct' ); |
117 |
|
118 |
t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'not_an_email' ); |
119 |
|
120 |
throws_ok |
121 |
{ Koha::Email->create({ }); } |
122 |
'Koha::Exceptions::BadParameter', |
123 |
'Exception thrown correctly'; |
124 |
|
125 |
is( "$@", q{Invalid 'to' parameter: not_an_email}, 'Exception message correct' ); |
126 |
|
127 |
t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef ); |
128 |
|
129 |
throws_ok |
130 |
{ Koha::Email->create( |
131 |
{ |
132 |
to => 'tomasito@mail.com', |
133 |
reply_to => 'not_an_email' |
134 |
} |
135 |
); } |
136 |
'Koha::Exceptions::BadParameter', |
137 |
'Exception thrown correctly'; |
138 |
|
139 |
is( "$@", q{Invalid 'reply_to' parameter: not_an_email}, 'Exception message correct' ); |
140 |
|
141 |
throws_ok |
142 |
{ Koha::Email->create( |
143 |
{ |
144 |
to => 'tomasito@mail.com', |
145 |
sender => 'not_an_email' |
146 |
} |
147 |
); } |
148 |
'Koha::Exceptions::BadParameter', |
149 |
'Exception thrown correctly'; |
150 |
|
151 |
is( "$@", q{Invalid 'sender' parameter: not_an_email}, 'Exception message correct' ); |
152 |
|
153 |
throws_ok |
154 |
{ Koha::Email->create( |
155 |
{ |
156 |
to => 'tomasito@mail.com', |
157 |
cc => 'not_an_email' |
158 |
} |
159 |
); } |
160 |
'Koha::Exceptions::BadParameter', |
161 |
'Exception thrown correctly'; |
162 |
|
163 |
is( "$@", q{Invalid 'cc' parameter: not_an_email}, 'Exception message correct' ); |
164 |
|
165 |
throws_ok |
166 |
{ Koha::Email->create( |
167 |
{ |
168 |
to => 'tomasito@mail.com', |
169 |
bcc => 'not_an_email' |
170 |
} |
171 |
); } |
172 |
'Koha::Exceptions::BadParameter', |
173 |
'Exception thrown correctly'; |
174 |
|
175 |
is( "$@", q{Invalid 'bcc' parameter: not_an_email}, 'Exception message correct' ); |
176 |
}; |
177 |
}; |