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