xref: /dokuwiki/_test/tests/inc/mailer.test.php (revision 0ee5ed1e998c1e67c4a0b7687977c4e2e0f29494)
1cc92aca7SAndreas Gohr<?php
2cc92aca7SAndreas Gohr
3cc92aca7SAndreas Gohr/**
4cc92aca7SAndreas Gohr * Extends the mailer class to expose internal variables for testing
5cc92aca7SAndreas Gohr */
6cc92aca7SAndreas Gohrclass TestMailer extends Mailer {
7cc92aca7SAndreas Gohr    public function prop($name){
8cc92aca7SAndreas Gohr        return $this->$name;
9cc92aca7SAndreas Gohr    }
101f61f312SDominik Eckelmann
111f61f312SDominik Eckelmann    public function &propRef($name) {
121f61f312SDominik Eckelmann        return $this->$name;
131f61f312SDominik Eckelmann    }
141f61f312SDominik Eckelmann
151f61f312SDominik Eckelmann    public function prepareHeaders() {
161f61f312SDominik Eckelmann        return parent::prepareHeaders();
171f61f312SDominik Eckelmann    }
18b6c97c70SAndreas Gohr
19b6c97c70SAndreas Gohr    public function cleanHeaders() {
20b6c97c70SAndreas Gohr        parent::cleanHeaders();
21b6c97c70SAndreas Gohr    }
22b6c97c70SAndreas Gohr
23cc92aca7SAndreas Gohr}
24cc92aca7SAndreas Gohr
25cc92aca7SAndreas Gohrclass mailer_test extends DokuWikiTest {
26cc92aca7SAndreas Gohr
27cc92aca7SAndreas Gohr
28cc92aca7SAndreas Gohr    function test_userheader(){
29cc92aca7SAndreas Gohr        $mail = new TestMailer();
30cc92aca7SAndreas Gohr        $headers = $mail->prop('headers');
31cc92aca7SAndreas Gohr        $this->assertArrayNotHasKey('X-Dokuwiki-User',$headers);
32cc92aca7SAndreas Gohr
33cc92aca7SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'andi';
34cc92aca7SAndreas Gohr        $mail = new TestMailer();
35cc92aca7SAndreas Gohr        $headers = $mail->prop('headers');
36cc92aca7SAndreas Gohr        $this->assertArrayHasKey('X-Dokuwiki-User',$headers);
37cc92aca7SAndreas Gohr    }
38cc92aca7SAndreas Gohr
39cc92aca7SAndreas Gohr    function test_setHeader(){
40cc92aca7SAndreas Gohr        $mail = new TestMailer();
41cc92aca7SAndreas Gohr
42cc92aca7SAndreas Gohr        // check existance of default headers
43cc92aca7SAndreas Gohr        $headers = $mail->prop('headers');
44cc92aca7SAndreas Gohr        $this->assertArrayHasKey('X-Mailer',$headers);
45cc92aca7SAndreas Gohr        $this->assertArrayHasKey('X-Dokuwiki-Title',$headers);
46cc92aca7SAndreas Gohr        $this->assertArrayHasKey('X-Dokuwiki-Server',$headers);
47cc92aca7SAndreas Gohr        $this->assertArrayHasKey('X-Auto-Response-Suppress',$headers);
48cc92aca7SAndreas Gohr        $this->assertArrayHasKey('List-Id',$headers);
49cc92aca7SAndreas Gohr
50cc92aca7SAndreas Gohr        // set a bunch of test headers
51cc92aca7SAndreas Gohr        $mail->setHeader('test-header','bla');
52cc92aca7SAndreas Gohr        $mail->setHeader('to','A valid ASCII name <test@example.com>');
53cc92aca7SAndreas Gohr        $mail->setHeader('from',"Thös ne\needs\x00serious cleaning$§%.");
54cc92aca7SAndreas Gohr        $mail->setHeader('bad',"Thös ne\needs\x00serious cleaning$§%.",false);
55cc92aca7SAndreas Gohr        $mail->setHeader("weird\n*+\x00foo.-_@bar?",'now clean');
56cc92aca7SAndreas Gohr
57cc92aca7SAndreas Gohr        // are they set?
58cc92aca7SAndreas Gohr        $headers = $mail->prop('headers');
59cc92aca7SAndreas Gohr        $this->assertArrayHasKey('Test-Header',$headers);
60cc92aca7SAndreas Gohr        $this->assertEquals('bla',$headers['Test-Header']);
61cc92aca7SAndreas Gohr        $this->assertArrayHasKey('To',$headers);
62cc92aca7SAndreas Gohr        $this->assertEquals('A valid ASCII name <test@example.com>',$headers['To']);
63cc92aca7SAndreas Gohr        $this->assertArrayHasKey('From',$headers);
64cc92aca7SAndreas Gohr        $this->assertEquals('Ths neeedsserious cleaning.',$headers['From']);
65cc92aca7SAndreas Gohr        $this->assertArrayHasKey('Bad',$headers);
66cc92aca7SAndreas Gohr        $this->assertEquals("Thös ne\needs\x00serious cleaning$§%.",$headers['Bad']);
67cc92aca7SAndreas Gohr        $this->assertArrayHasKey('Weird+foo.-_@bar',$headers);
68cc92aca7SAndreas Gohr
69cc92aca7SAndreas Gohr        // unset a header again
70cc92aca7SAndreas Gohr        $mail->setHeader('test-header','');
71cc92aca7SAndreas Gohr        $headers = $mail->prop('headers');
72cc92aca7SAndreas Gohr        $this->assertArrayNotHasKey('Test-Header',$headers);
73cc92aca7SAndreas Gohr    }
74cc92aca7SAndreas Gohr
75b6c97c70SAndreas Gohr    function test_addresses(){
76b6c97c70SAndreas Gohr        $mail = new TestMailer();
77b6c97c70SAndreas Gohr
78b6c97c70SAndreas Gohr        $mail->to('andi@splitbrain.org');
79b6c97c70SAndreas Gohr        $mail->cleanHeaders();
80b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
81b6c97c70SAndreas Gohr        $this->assertEquals('andi@splitbrain.org', $headers['To']);
82b6c97c70SAndreas Gohr
83b6c97c70SAndreas Gohr        $mail->to('<andi@splitbrain.org>');
84b6c97c70SAndreas Gohr        $mail->cleanHeaders();
85b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
86b6c97c70SAndreas Gohr        $this->assertEquals('andi@splitbrain.org', $headers['To']);
87b6c97c70SAndreas Gohr
88b6c97c70SAndreas Gohr        $mail->to('Andreas Gohr <andi@splitbrain.org>');
89b6c97c70SAndreas Gohr        $mail->cleanHeaders();
90b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
91b6c97c70SAndreas Gohr        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>', $headers['To']);
92b6c97c70SAndreas Gohr
93b6c97c70SAndreas Gohr        $mail->to('Andreas Gohr <andi@splitbrain.org> , foo <foo@example.com>');
94b6c97c70SAndreas Gohr        $mail->cleanHeaders();
95b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
96b6c97c70SAndreas Gohr        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>, foo <foo@example.com>', $headers['To']);
97b6c97c70SAndreas Gohr
98b6c97c70SAndreas Gohr        $mail->to('Möp <moep@example.com> , foo <foo@example.com>');
99b6c97c70SAndreas Gohr        $mail->cleanHeaders();
100b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
101b6c97c70SAndreas Gohr        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
102b6c97c70SAndreas Gohr
103b6c97c70SAndreas Gohr        $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
104b6c97c70SAndreas Gohr        $mail->cleanHeaders();
105b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
106b6c97c70SAndreas Gohr        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
107b6c97c70SAndreas Gohr
108b6c97c70SAndreas Gohr        $mail->to(array('Beet, L van <lvb@example.com>',' foo <foo@example.com>'));
109b6c97c70SAndreas Gohr        $mail->cleanHeaders();
110b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
111b6c97c70SAndreas Gohr        $this->assertEquals('=?UTF-8?B?QmVldCwgTCB2YW4=?= <lvb@example.com>, foo <foo@example.com>', $headers['To']);
112b6c97c70SAndreas Gohr
113b6c97c70SAndreas Gohr
114b6c97c70SAndreas Gohr    }
115b6c97c70SAndreas Gohr
116cc92aca7SAndreas Gohr    function test_simplemail(){
117cc92aca7SAndreas Gohr        global $conf;
118cc92aca7SAndreas Gohr        $conf['htmlmail'] = 0;
119cc92aca7SAndreas Gohr        $mail = new TestMailer();
120cc92aca7SAndreas Gohr        $mail->to('test@example.com');
121cc92aca7SAndreas Gohr        $mail->setBody('A test mail in ASCII');
122cc92aca7SAndreas Gohr
123cc92aca7SAndreas Gohr        $dump = $mail->dump();
124cc92aca7SAndreas Gohr        $this->assertNotRegexp('/Content-Type: multipart/',$dump);
125cc92aca7SAndreas Gohr        $this->assertRegexp('#Content-Type: text/plain; charset=UTF-8#',$dump);
126cc92aca7SAndreas Gohr        $this->assertRegexp('/'.base64_encode('A test mail in ASCII').'/',$dump);
127cc92aca7SAndreas Gohr
128cc92aca7SAndreas Gohr        $conf['htmlmail'] = 1;
129cc92aca7SAndreas Gohr    }
130cc92aca7SAndreas Gohr
131cc92aca7SAndreas Gohr    function test_replacements(){
132cc92aca7SAndreas Gohr        $mail = new TestMailer();
133cc92aca7SAndreas Gohr
134cc92aca7SAndreas Gohr        $replacements = array( '@DATE@','@BROWSER@','@IPADDRESS@','@HOSTNAME@',
135cc92aca7SAndreas Gohr                               '@TITLE@','@DOKUWIKIURL@','@USER@','@NAME@','@MAIL@');
136cc92aca7SAndreas Gohr        $mail->setBody('A test mail in with replacements '.join(' ',$replacements));
137cc92aca7SAndreas Gohr
138cc92aca7SAndreas Gohr        $text = $mail->prop('text');
139cc92aca7SAndreas Gohr        $html = $mail->prop('html');
140cc92aca7SAndreas Gohr
141cc92aca7SAndreas Gohr        foreach($replacements as $repl){
142cc92aca7SAndreas Gohr            $this->assertNotRegexp("/$repl/",$text,"$repl replacement still in text");
143cc92aca7SAndreas Gohr            $this->assertNotRegexp("/$repl/",$html,"$repl replacement still in html");
144cc92aca7SAndreas Gohr        }
145cc92aca7SAndreas Gohr    }
146cc92aca7SAndreas Gohr
14791effd8dSDominik Eckelmann    /**
14891effd8dSDominik Eckelmann     * @see https://forum.dokuwiki.org/post/35822
14991effd8dSDominik Eckelmann     */
1501f61f312SDominik Eckelmann    function test_emptyBCCorCC() {
1511f61f312SDominik Eckelmann        $mail = new TestMailer();
1521f61f312SDominik Eckelmann        $headers = &$mail->propRef('headers');
1531f61f312SDominik Eckelmann        $headers['Bcc'] = '';
1541f61f312SDominik Eckelmann        $headers['Cc'] = '';
1551f61f312SDominik Eckelmann        $header = $mail->prepareHeaders();
1561f61f312SDominik Eckelmann        $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
1571f61f312SDominik Eckelmann        $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Bcc found in headers.');
1581f61f312SDominik Eckelmann    }
159*0ee5ed1eSAndreas Gohr
160*0ee5ed1eSAndreas Gohr    /**
161*0ee5ed1eSAndreas Gohr     * @group internet
162*0ee5ed1eSAndreas Gohr     */
163*0ee5ed1eSAndreas Gohr    function test_lint(){
164*0ee5ed1eSAndreas Gohr        // prepare a simple multipart message
165*0ee5ed1eSAndreas Gohr        $mail = new TestMailer();
166*0ee5ed1eSAndreas Gohr        $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
167*0ee5ed1eSAndreas Gohr        $mail->subject('This is a töst');
168*0ee5ed1eSAndreas Gohr        $mail->setBody('Hello Wörld,
169*0ee5ed1eSAndreas Gohr
170*0ee5ed1eSAndreas Gohr        please don\'t burn, okay?
171*0ee5ed1eSAndreas Gohr        ');
172*0ee5ed1eSAndreas Gohr        $mail->attachContent('some test data', 'text/plain', 'text.txt');
173*0ee5ed1eSAndreas Gohr        $msg = $mail->dump();
174*0ee5ed1eSAndreas Gohr        $msglines = explode("\n", $msg);
175*0ee5ed1eSAndreas Gohr
176*0ee5ed1eSAndreas Gohr        // ask message lint if it is okay
177*0ee5ed1eSAndreas Gohr        $html = new HTTPClient();
178*0ee5ed1eSAndreas Gohr        $results = $html->post('http://tools.ietf.org/tools/msglint/msglint', array('msg'=>$msg));
179*0ee5ed1eSAndreas Gohr        $this->assertTrue($results !== false);
180*0ee5ed1eSAndreas Gohr
181*0ee5ed1eSAndreas Gohr        // parse the result lines
182*0ee5ed1eSAndreas Gohr        $lines = explode("\n", $results);
183*0ee5ed1eSAndreas Gohr        $rows  = count($lines);
184*0ee5ed1eSAndreas Gohr        $i=0;
185*0ee5ed1eSAndreas Gohr        while(trim($lines[$i]) != '-----------' && $i<$rows) $i++; //skip preamble
186*0ee5ed1eSAndreas Gohr        for($i=$i+1; $i<$rows; $i++){
187*0ee5ed1eSAndreas Gohr            $line = trim($lines[$i]);
188*0ee5ed1eSAndreas Gohr            if($line == '-----------') break; //skip appendix
189*0ee5ed1eSAndreas Gohr
190*0ee5ed1eSAndreas Gohr            // get possible continuation of the line
191*0ee5ed1eSAndreas Gohr            while($lines[$i+1][0] == ' '){
192*0ee5ed1eSAndreas Gohr                $line .= ' '.trim($lines[$i+1]);
193*0ee5ed1eSAndreas Gohr                $i++;
194*0ee5ed1eSAndreas Gohr            }
195*0ee5ed1eSAndreas Gohr
196*0ee5ed1eSAndreas Gohr            // check the line for errors
197*0ee5ed1eSAndreas Gohr            if(substr($line,0,5) == 'ERROR'){
198*0ee5ed1eSAndreas Gohr                // get the context in which the error occured
199*0ee5ed1eSAndreas Gohr                $errorin = '';
200*0ee5ed1eSAndreas Gohr                if(preg_match('/line (\d+)$/', $line, $m)){
201*0ee5ed1eSAndreas Gohr                    $errorin .= "\n".$msglines[$m[1] - 1];
202*0ee5ed1eSAndreas Gohr                }
203*0ee5ed1eSAndreas Gohr                if(preg_match('/lines (\d+)-(\d+)$/', $line, $m)){
204*0ee5ed1eSAndreas Gohr                    for($x=$m[1]-1; $x<$m[2]; $x++){
205*0ee5ed1eSAndreas Gohr                        $errorin .= "\n".$msglines[$x];
206*0ee5ed1eSAndreas Gohr                    }
207*0ee5ed1eSAndreas Gohr                }
208*0ee5ed1eSAndreas Gohr
209*0ee5ed1eSAndreas Gohr                // raise the error
210*0ee5ed1eSAndreas Gohr                throw new Exception($line.$errorin);
211*0ee5ed1eSAndreas Gohr            }
212*0ee5ed1eSAndreas Gohr        }
213*0ee5ed1eSAndreas Gohr
214*0ee5ed1eSAndreas Gohr    }
215cc92aca7SAndreas Gohr}
216cc92aca7SAndreas Gohr//Setup VIM: ex: et ts=4 :
217