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