xref: /dokuwiki/_test/tests/inc/mailer.test.php (revision 98548fef01f6eb5c04e2ab0aae2c46019fc2b448)
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('andi@splitbrain.org,foo@example.com');
108        $mail->cleanHeaders();
109        $headers = $mail->prop('headers');
110        $this->assertEquals('andi@splitbrain.org, foo@example.com', $headers['To']);
111
112        $mail->to('Andreas Gohr <andi@splitbrain.org> , foo <foo@example.com>');
113        $mail->cleanHeaders();
114        $headers = $mail->prop('headers');
115        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>, foo <foo@example.com>', $headers['To']);
116
117        $mail->to('"Foo, Dr." <foo@example.com> , foo <foo@example.com>');
118        $mail->cleanHeaders();
119        $headers = $mail->prop('headers');
120        $this->assertEquals('=?UTF-8?B?IkZvbywgRHIuIg==?= <foo@example.com>, foo <foo@example.com>', $headers['To']);
121
122        $mail->to('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('Möp <moep@example.com> ',' foo <foo@example.com>'));
128        $mail->cleanHeaders();
129        $headers = $mail->prop('headers');
130        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
131
132        $mail->to(array('Beet, L van <lvb@example.com>',' foo <foo@example.com>'));
133        $mail->cleanHeaders();
134        $headers = $mail->prop('headers');
135        $this->assertEquals('=?UTF-8?B?QmVldCwgTCB2YW4=?= <lvb@example.com>, foo <foo@example.com>', $headers['To']);
136
137
138    }
139
140    function test_simplemail(){
141        global $conf;
142        $conf['htmlmail'] = 0;
143
144        $mailbody = 'A test mail in ASCII';
145        $mail = new TestMailer();
146        $mail->to('test@example.com');
147        $mail->setBody($mailbody);
148
149        $dump = $mail->dump();
150
151        // construct the expected mail body text - include the expected dokuwiki signature
152        $replacements = $mail->prop('replacements');
153        $expected_mail_body = chunk_split(base64_encode($mailbody.$replacements['text']['EMAILSIGNATURE']),72,MAILHEADER_EOL);
154
155        $this->assertNotRegexp('/Content-Type: multipart/',$dump);
156        $this->assertRegexp('#Content-Type: text/plain; charset=UTF-8#',$dump);
157        $this->assertRegexp('/'.preg_quote($expected_mail_body,'/').'/',$dump);
158
159        $conf['htmlmail'] = 1;
160    }
161
162    function test_replacements(){
163        $mail = new TestMailer();
164
165        $replacements = array( '@DATE@','@BROWSER@','@IPADDRESS@','@HOSTNAME@','@EMAILSIGNATURE@',
166                               '@TITLE@','@DOKUWIKIURL@','@USER@','@NAME@','@MAIL@');
167        $mail->setBody('A test mail in with replacements '.join(' ',$replacements));
168
169        $text = $mail->prop('text');
170        $html = $mail->prop('html');
171
172        foreach($replacements as $repl){
173            $this->assertNotRegexp("/$repl/",$text,"$repl replacement still in text");
174            $this->assertNotRegexp("/$repl/",$html,"$repl replacement still in html");
175        }
176    }
177
178    /**
179     * @see https://forum.dokuwiki.org/post/35822
180     */
181    function test_emptyBCCorCC() {
182        $mail = new TestMailer();
183        $headers = &$mail->propRef('headers');
184        $headers['Bcc'] = '';
185        $headers['Cc'] = '';
186        $header = $mail->prepareHeaders();
187        $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
188        $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Cc found in headers.');
189    }
190
191    function test_nullTOorCCorBCC() {
192        $mail = new TestMailer();
193        $headers = &$mail->propRef('headers');
194        $headers['Bcc'] = NULL;
195        $headers['Cc'] = NULL;
196        $headers['To'] = NULL;
197        $header = $mail->prepareHeaders();
198        $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
199        $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Cc found in headers.');
200        $this->assertEquals(0, preg_match('/(^|\n)To: (\n|$)/', $header), 'To found in headers.');
201    }
202
203    /**
204     * @group internet
205     */
206    function test_lint(){
207        // prepare a simple multipart message
208        $mail = new TestMailer();
209        $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
210        $mail->from('Me <test@example.com>');
211        $mail->subject('This is a töst');
212        $mail->setBody('Hello Wörld,
213
214        please don\'t burn, okay?
215        ');
216        $mail->attachContent('some test data', 'text/plain', 'a text.txt');
217        $msg = $mail->dump();
218        $msglines = explode("\n", $msg);
219
220        //echo $msg;
221
222        // ask message lint if it is okay
223        $html = new HTTPClient();
224        $results = $html->post('https://tools.ietf.org/tools/msglint/msglint', array('msg'=>$msg));
225        if($results === false) {
226            $this->markTestSkipped('no response from validator');
227            return;
228        }
229
230        // parse the result lines
231        $lines = explode("\n", $results);
232        $rows  = count($lines);
233        $i=0;
234        while(trim($lines[$i]) != '-----------' && $i<$rows) $i++; //skip preamble
235        for($i=$i+1; $i<$rows; $i++){
236            $line = trim($lines[$i]);
237            if($line == '-----------') break; //skip appendix
238
239            // get possible continuation of the line
240            while($lines[$i+1][0] == ' '){
241                $line .= ' '.trim($lines[$i+1]);
242                $i++;
243            }
244
245            // check the line for errors
246            if(substr($line,0,5) == 'ERROR' || substr($line,0,7) == 'WARNING'){
247                // ignore some errors
248                if(strpos($line, "missing mandatory header 'return-path'")) continue; #set by MDA
249                if(strpos($line, "bare newline in text body decoded")) continue; #we don't send mail bodies as CRLF, yet
250                if(strpos($line, "last decoded line too long")) continue; #we don't send mail bodies as CRLF, yet
251
252                // get the context in which the error occured
253                $errorin = '';
254                if(preg_match('/line (\d+)$/', $line, $m)){
255                    $errorin .= "\n".$msglines[$m[1] - 1];
256                }
257                if(preg_match('/lines (\d+)-(\d+)$/', $line, $m)){
258                    for($x=$m[1]-1; $x<$m[2]; $x++){
259                        $errorin .= "\n".$msglines[$x];
260                    }
261                }
262
263                // raise the error
264                throw new Exception($line.$errorin);
265            }
266        }
267
268        $this->assertTrue(true); // avoid being marked as risky for having no assertion
269    }
270
271    function test_simplemailsignature() {
272        global $conf;
273        $conf['htmlmail'] = 0;
274
275        $mailbody = 'A test mail in ASCII';
276        $signature = "\n-- \n" . 'This mail was generated by DokuWiki at' . "\n" . DOKU_URL . "\n";
277        $mail = new TestMailer();
278        $mail->to('test@example.com');
279        $mail->setBody($mailbody);
280
281        $dump = $mail->dump();
282
283        // construct the expected mail body text - include the expected dokuwiki signature
284        $expected_mail_body = chunk_split(base64_encode($mailbody . $signature), 72, MAILHEADER_EOL);
285        $this->assertRegexp('/' . preg_quote($expected_mail_body, '/') . '/', $dump);
286
287        $conf['htmlmail'] = 1;
288    }
289
290    function test_htmlmailsignature() {
291        $mailbody_text = 'A test mail in ASCII :)';
292        $mailbody_html = 'A test mail in <strong>html</strong>';
293        $htmlmsg_expected = '<html>
294<head>
295    <title>My Test Wiki</title>
296    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
297</head>
298<body>
299
300A test mail in <strong>html</strong>
301
302<br /><hr />
303<small>This mail was generated by DokuWiki at<br /><a href="' . DOKU_URL . '">' . DOKU_URL . '</a></small>
304</body>
305</html>
306';
307
308        $mail = new TestMailer();
309        $mail->to('test@example.com');
310        $mail->setBody($mailbody_text, null, null, $mailbody_html);
311
312        $dump = $mail->dump();
313
314        // construct the expected mail body text - include the expected dokuwiki signature
315        $expected_mail_body = chunk_split(base64_encode($htmlmsg_expected), 72, MAILHEADER_EOL);
316
317        $this->assertRegexp('/Content-Type: multipart/', $dump);
318        $this->assertRegexp('#Content-Type: text/plain; charset=UTF-8#', $dump);
319        $this->assertRegexp('/' . preg_quote($expected_mail_body, '/') . '/', $dump);
320
321    }
322
323    function test_htmlmailsignaturecustom() {
324        global $lang;
325        $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>';
326
327        $mailbody_text = 'A test mail in ASCII :)';
328        $mailbody_html = 'A test mail in <strong>html</strong>';
329        $htmlmsg_expected = '<html>
330<head>
331    <title>My Test Wiki</title>
332    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
333</head>
334<body>
335
336A test mail in <strong>html</strong>
337
338<br /><hr />
339<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>
340</body>
341</html>
342';
343
344        $mail = new TestMailer();
345        $mail->to('test@example.com');
346        $mail->setBody($mailbody_text, null, null, $mailbody_html);
347
348        $dump = $mail->dump();
349
350        // construct the expected mail body text - include the expected dokuwiki signature
351        $replacements = $mail->prop('replacements');
352        $expected_mail_body = chunk_split(base64_encode($htmlmsg_expected), 72, MAILHEADER_EOL);
353
354        $this->assertRegexp('/' . preg_quote($expected_mail_body, '/') . '/', $dump);
355
356    }
357
358    function test_getCleanName() {
359        $mail = new TestMailer();
360        $name = $mail->getCleanName('Foo Bar');
361        $this->assertEquals('Foo Bar', $name);
362        $name = $mail->getCleanName('Foo, Bar');
363        $this->assertEquals('"Foo, Bar"', $name);
364        $name = $mail->getCleanName('Foo" Bar');
365        $this->assertEquals('"Foo\" Bar"', $name);
366    }
367}
368//Setup VIM: ex: et ts=4 :
369