Bug 22343: Add exec flag on .t files
[koha.git] / t / Koha / Email.t
blob1c46fcc28432c65d4882801558ded0873b78e6ae
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 2;
21 use Test::Exception;
23 use t::lib::Mocks;
25 use_ok('Koha::Email');
27 subtest 'create() tests' => sub {
29 plan tests => 23;
31 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
33 my $html_body = '<h1>Title</h1><p>Message</p>';
34 my $text_body = "#Title: Message";
36 my $email = Koha::Email->create(
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' },
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' );
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' );
68 $email = Koha::Email->create(
70 to => 'to@example.com',
71 cc => 'cc@example.com',
72 bcc => 'bcc@example.com',
73 text_body => $text_body,
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");
88 subtest 'exception cases' => sub {
90 plan tests => 16;
92 throws_ok
93 { Koha::Email->create({ from => 'not_an_email' }); }
94 'Koha::Exceptions::BadParameter',
95 'Exception thrown correctly';
97 is( "$@", q{Invalid 'from' parameter: not_an_email}, 'Exception message correct' );
99 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'not_an_email' );
101 throws_ok
102 { Koha::Email->create({ }); }
103 'Koha::Exceptions::BadParameter',
104 'Exception thrown correctly';
106 is( "$@", q{Invalid 'from' parameter: not_an_email}, 'Exception message correct' );
108 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'tomasito@mail.com' );
109 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
111 throws_ok
112 { Koha::Email->create({ to => 'not_an_email' }); }
113 'Koha::Exceptions::BadParameter',
114 'Exception thrown correctly';
116 is( "$@", q{Invalid 'to' parameter: not_an_email}, 'Exception message correct' );
118 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'not_an_email' );
120 throws_ok
121 { Koha::Email->create({ }); }
122 'Koha::Exceptions::BadParameter',
123 'Exception thrown correctly';
125 is( "$@", q{Invalid 'to' parameter: not_an_email}, 'Exception message correct' );
127 t::lib::Mocks::mock_preference( 'SendAllEmailsTo', undef );
129 throws_ok
130 { Koha::Email->create(
132 to => 'tomasito@mail.com',
133 reply_to => 'not_an_email'
135 ); }
136 'Koha::Exceptions::BadParameter',
137 'Exception thrown correctly';
139 is( "$@", q{Invalid 'reply_to' parameter: not_an_email}, 'Exception message correct' );
141 throws_ok
142 { Koha::Email->create(
144 to => 'tomasito@mail.com',
145 sender => 'not_an_email'
147 ); }
148 'Koha::Exceptions::BadParameter',
149 'Exception thrown correctly';
151 is( "$@", q{Invalid 'sender' parameter: not_an_email}, 'Exception message correct' );
153 throws_ok
154 { Koha::Email->create(
156 to => 'tomasito@mail.com',
157 cc => 'not_an_email'
159 ); }
160 'Koha::Exceptions::BadParameter',
161 'Exception thrown correctly';
163 is( "$@", q{Invalid 'cc' parameter: not_an_email}, 'Exception message correct' );
165 throws_ok
166 { Koha::Email->create(
168 to => 'tomasito@mail.com',
169 bcc => 'not_an_email'
171 ); }
172 'Koha::Exceptions::BadParameter',
173 'Exception thrown correctly';
175 is( "$@", q{Invalid 'bcc' parameter: not_an_email}, 'Exception message correct' );