xref: /dokuwiki/_test/tests/inc/mailer.test.php (revision e8f1a63ff0c620e44b441d9a27d2aa66bf7202a9)
1<?php
2
3/**
4 * Extends the mailer class to expose internal variables for testing
5 */
6class TestMailer extends Mailer {
7    public function prop($name){
8        return $this->$name;
9    }
10
11    public function &propRef($name) {
12        return $this->$name;
13    }
14
15    public function prepareHeaders() {
16        return parent::prepareHeaders();
17    }
18
19    public function cleanHeaders() {
20        parent::cleanHeaders();
21    }
22
23}
24
25class mailer_test extends DokuWikiTest {
26
27
28    function test_userheader(){
29        $mail = new TestMailer();
30        $headers = $mail->prop('headers');
31        $this->assertArrayNotHasKey('X-Dokuwiki-User',$headers);
32
33        $_SERVER['REMOTE_USER'] = 'andi';
34        $mail = new TestMailer();
35        $headers = $mail->prop('headers');
36        $this->assertArrayHasKey('X-Dokuwiki-User',$headers);
37    }
38
39    function test_setHeader(){
40        $mail = new TestMailer();
41
42        // check existance of default headers
43        $headers = $mail->prop('headers');
44        $this->assertArrayHasKey('X-Mailer',$headers);
45        $this->assertArrayHasKey('X-Dokuwiki-Title',$headers);
46        $this->assertArrayHasKey('X-Dokuwiki-Server',$headers);
47        $this->assertArrayHasKey('X-Auto-Response-Suppress',$headers);
48        $this->assertArrayHasKey('List-Id',$headers);
49
50        // set a bunch of test headers
51        $mail->setHeader('test-header','bla');
52        $mail->setHeader('to','A valid ASCII name <test@example.com>');
53        $mail->setHeader('from',"Thös ne\needs\x00serious cleaning\$§%.");
54        $mail->setHeader('bad',"Thös ne\needs\x00serious cleaning\$§%.",false);
55        $mail->setHeader("weird\n*+\x00foo.-_@bar?",'now clean');
56
57        // are they set?
58        $headers = $mail->prop('headers');
59        $this->assertArrayHasKey('Test-Header',$headers);
60        $this->assertEquals('bla',$headers['Test-Header']);
61        $this->assertArrayHasKey('To',$headers);
62        $this->assertEquals('A valid ASCII name <test@example.com>',$headers['To']);
63        $this->assertArrayHasKey('From',$headers);
64        $this->assertEquals('Ths neeedsserious cleaning.',$headers['From']);
65        $this->assertArrayHasKey('Bad',$headers);
66        $this->assertEquals("Thös ne\needs\x00serious cleaning\$§%.",$headers['Bad']);
67        $this->assertArrayHasKey('Weird+foo.-_@bar',$headers);
68
69        // unset a header again
70        $mail->setHeader('test-header','');
71        $headers = $mail->prop('headers');
72        $this->assertArrayNotHasKey('Test-Header',$headers);
73    }
74
75    function test_addresses(){
76        $mail = new TestMailer();
77
78        $mail->to('andi@splitbrain.org');
79        $mail->cleanHeaders();
80        $headers = $mail->prop('headers');
81        $this->assertEquals('andi@splitbrain.org', $headers['To']);
82
83        $mail->to('<andi@splitbrain.org>');
84        $mail->cleanHeaders();
85        $headers = $mail->prop('headers');
86        $this->assertEquals('andi@splitbrain.org', $headers['To']);
87
88        $mail->to('Andreas Gohr <andi@splitbrain.org>');
89        $mail->cleanHeaders();
90        $headers = $mail->prop('headers');
91        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>', $headers['To']);
92
93        $mail->to('Andreas Gohr <andi@splitbrain.org> , foo <foo@example.com>');
94        $mail->cleanHeaders();
95        $headers = $mail->prop('headers');
96        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>, foo <foo@example.com>', $headers['To']);
97
98        $mail->to('Möp <moep@example.com> , foo <foo@example.com>');
99        $mail->cleanHeaders();
100        $headers = $mail->prop('headers');
101        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
102
103        $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
104        $mail->cleanHeaders();
105        $headers = $mail->prop('headers');
106        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
107
108        $mail->to(array('Beet, L van <lvb@example.com>',' foo <foo@example.com>'));
109        $mail->cleanHeaders();
110        $headers = $mail->prop('headers');
111        $this->assertEquals('=?UTF-8?B?QmVldCwgTCB2YW4=?= <lvb@example.com>, foo <foo@example.com>', $headers['To']);
112
113
114    }
115
116    function test_simplemail(){
117        global $conf;
118        $conf['htmlmail'] = 0;
119
120        $mailbody = 'A test mail in ASCII';
121        $mail = new TestMailer();
122        $mail->to('test@example.com');
123        $mail->setBody($mailbody);
124
125        $dump = $mail->dump();
126
127        // construct the expected mail body text - include the expected dokuwiki signature
128        $replacements = $mail->prop('replacements');
129        $expected_mail_body = chunk_split(base64_encode($mailbody.$replacements['text']['EMAILSIGNATURE']),72,MAILHEADER_EOL);
130
131        $this->assertNotRegexp('/Content-Type: multipart/',$dump);
132        $this->assertRegexp('#Content-Type: text/plain; charset=UTF-8#',$dump);
133        $this->assertRegexp('/'.preg_quote($expected_mail_body,'/').'/',$dump);
134
135        $conf['htmlmail'] = 1;
136    }
137
138    function test_replacements(){
139        $mail = new TestMailer();
140
141        $replacements = array( '@DATE@','@BROWSER@','@IPADDRESS@','@HOSTNAME@','@EMAILSIGNATURE@',
142                               '@TITLE@','@DOKUWIKIURL@','@USER@','@NAME@','@MAIL@');
143        $mail->setBody('A test mail in with replacements '.join(' ',$replacements));
144
145        $text = $mail->prop('text');
146        $html = $mail->prop('html');
147
148        foreach($replacements as $repl){
149            $this->assertNotRegexp("/$repl/",$text,"$repl replacement still in text");
150            $this->assertNotRegexp("/$repl/",$html,"$repl replacement still in html");
151        }
152    }
153
154    /**
155     * @see https://forum.dokuwiki.org/post/35822
156     */
157    function test_emptyBCCorCC() {
158        $mail = new TestMailer();
159        $headers = &$mail->propRef('headers');
160        $headers['Bcc'] = '';
161        $headers['Cc'] = '';
162        $header = $mail->prepareHeaders();
163        $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
164        $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Cc found in headers.');
165    }
166
167    function test_nullTOorCCorBCC() {
168        $mail = new TestMailer();
169        $headers = &$mail->propRef('headers');
170        $headers['Bcc'] = NULL;
171        $headers['Cc'] = NULL;
172        $headers['To'] = NULL;
173        $header = $mail->prepareHeaders();
174        $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
175        $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Cc found in headers.');
176        $this->assertEquals(0, preg_match('/(^|\n)To: (\n|$)/', $header), 'To found in headers.');
177    }
178
179    /**
180     * @group internet
181     */
182    function test_lint(){
183        // prepare a simple multipart message
184        $mail = new TestMailer();
185        $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
186        $mail->from('Me <test@example.com>');
187        $mail->subject('This is a töst');
188        $mail->setBody('Hello Wörld,
189
190        please don\'t burn, okay?
191        ');
192        $mail->attachContent('some test data', 'text/plain', 'a text.txt');
193        $msg = $mail->dump();
194        $msglines = explode("\n", $msg);
195
196        //echo $msg;
197
198        // ask message lint if it is okay
199        $html = new HTTPClient();
200        $results = $html->post('https://tools.ietf.org/tools/msglint/msglint', array('msg'=>$msg));
201        if($results === false) {
202            $this->markTestSkipped('no response from validator');
203            return;
204        }
205
206        // parse the result lines
207        $lines = explode("\n", $results);
208        $rows  = count($lines);
209        $i=0;
210        while(trim($lines[$i]) != '-----------' && $i<$rows) $i++; //skip preamble
211        for($i=$i+1; $i<$rows; $i++){
212            $line = trim($lines[$i]);
213            if($line == '-----------') break; //skip appendix
214
215            // get possible continuation of the line
216            while($lines[$i+1][0] == ' '){
217                $line .= ' '.trim($lines[$i+1]);
218                $i++;
219            }
220
221            // check the line for errors
222            if(substr($line,0,5) == 'ERROR' || substr($line,0,7) == 'WARNING'){
223                // ignore some errors
224                if(strpos($line, "missing mandatory header 'return-path'")) continue; #set by MDA
225                if(strpos($line, "bare newline in text body decoded")) continue; #we don't send mail bodies as CRLF, yet
226                if(strpos($line, "last decoded line too long")) continue; #we don't send mail bodies as CRLF, yet
227
228                // get the context in which the error occured
229                $errorin = '';
230                if(preg_match('/line (\d+)$/', $line, $m)){
231                    $errorin .= "\n".$msglines[$m[1] - 1];
232                }
233                if(preg_match('/lines (\d+)-(\d+)$/', $line, $m)){
234                    for($x=$m[1]-1; $x<$m[2]; $x++){
235                        $errorin .= "\n".$msglines[$x];
236                    }
237                }
238
239                // raise the error
240                throw new Exception($line.$errorin);
241            }
242        }
243
244    }
245
246    function test_simplemailsignature() {
247        global $conf;
248        $conf['htmlmail'] = 0;
249
250        $mailbody = 'A test mail in ASCII';
251        $signature = "\n-- \n" . 'This mail was generated by DokuWiki at' . "\n" . DOKU_URL . "\n";
252        $mail = new TestMailer();
253        $mail->to('test@example.com');
254        $mail->setBody($mailbody);
255
256        $dump = $mail->dump();
257
258        // construct the expected mail body text - include the expected dokuwiki signature
259        $expected_mail_body = chunk_split(base64_encode($mailbody . $signature), 72, MAILHEADER_EOL);
260        $this->assertRegexp('/' . preg_quote($expected_mail_body, '/') . '/', $dump);
261
262        $conf['htmlmail'] = 1;
263    }
264
265    function test_htmlmailsignature() {
266        $mailbody_text = 'A test mail in ASCII :)';
267        $mailbody_html = 'A test mail in <strong>html</strong>';
268        $htmlmsg_expected = '<html>
269<head>
270    <title>My Test Wiki</title>
271    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
272</head>
273<body>
274
275A test mail in <strong>html</strong>
276
277<br /><hr />
278<small>This mail was generated by DokuWiki at<br /><a href="' . DOKU_URL . '">' . DOKU_URL . '</a></small>
279</body>
280</html>
281';
282
283        $mail = new TestMailer();
284        $mail->to('test@example.com');
285        $mail->setBody($mailbody_text, null, null, $mailbody_html);
286
287        $dump = $mail->dump();
288
289        // construct the expected mail body text - include the expected dokuwiki signature
290        $expected_mail_body = chunk_split(base64_encode($htmlmsg_expected), 72, MAILHEADER_EOL);
291
292        $this->assertRegexp('/Content-Type: multipart/', $dump);
293        $this->assertRegexp('#Content-Type: text/plain; charset=UTF-8#', $dump);
294        $this->assertRegexp('/' . preg_quote($expected_mail_body, '/') . '/', $dump);
295
296    }
297
298    function test_htmlmailsignaturecustom() {
299        global $lang;
300        $lang['email_signature_html'] = 'Official message from your DokuWiki @DOKUWIKIURL@<br />Created by wonderful mail class <a href="https://www.dokuwiki.org/devel:mail">https://www.dokuwiki.org/devel:mail</a>';
301
302        $mailbody_text = 'A test mail in ASCII :)';
303        $mailbody_html = 'A test mail in <strong>html</strong>';
304        $htmlmsg_expected = '<html>
305<head>
306    <title>My Test Wiki</title>
307    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
308</head>
309<body>
310
311A test mail in <strong>html</strong>
312
313<br /><hr />
314<small>Official message from your DokuWiki <a href="' . DOKU_URL . '">' . DOKU_URL . '</a><br />Created by wonderful mail class <a href="https://www.dokuwiki.org/devel:mail">https://www.dokuwiki.org/devel:mail</a></small>
315</body>
316</html>
317';
318
319        $mail = new TestMailer();
320        $mail->to('test@example.com');
321        $mail->setBody($mailbody_text, null, null, $mailbody_html);
322
323        $dump = $mail->dump();
324
325        // construct the expected mail body text - include the expected dokuwiki signature
326        $replacements = $mail->prop('replacements');
327        $expected_mail_body = chunk_split(base64_encode($htmlmsg_expected), 72, MAILHEADER_EOL);
328
329        $this->assertRegexp('/' . preg_quote($expected_mail_body, '/') . '/', $dump);
330
331    }
332}
333//Setup VIM: ex: et ts=4 :
334