xref: /dokuwiki/_test/tests/inc/mailer.test.php (revision 9ea4583661793f2f95c84aa39022dd8a64bf8821)
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>');
53c8d2e830SChristopher Smith        $mail->setHeader('from',"Thös ne\needs\x00serious cleaning\$§%.");
54c8d2e830SChristopher Smith        $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);
66d1612d99SAndreas 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;
119*9ea45836SChristopher Smith
120*9ea45836SChristopher Smith        $mailbody = 'A test mail in ASCII';
121cc92aca7SAndreas Gohr        $mail = new TestMailer();
122cc92aca7SAndreas Gohr        $mail->to('test@example.com');
123*9ea45836SChristopher Smith        $mail->setBody($mailbody);
124cc92aca7SAndreas Gohr
125cc92aca7SAndreas Gohr        $dump = $mail->dump();
126*9ea45836SChristopher Smith
127*9ea45836SChristopher Smith        // construct the expected mail body text - include the expected dokuwiki signature
128*9ea45836SChristopher Smith        $replacements = $mail->prop('replacements');
129*9ea45836SChristopher Smith        $expected_mail_body = chunk_split(base64_encode($mailbody.$replacements['text']['EMAILSIGNATURE']),72,MAILHEADER_EOL);
130*9ea45836SChristopher Smith
131cc92aca7SAndreas Gohr        $this->assertNotRegexp('/Content-Type: multipart/',$dump);
132cc92aca7SAndreas Gohr        $this->assertRegexp('#Content-Type: text/plain; charset=UTF-8#',$dump);
133*9ea45836SChristopher Smith        $this->assertRegexp('/'.preg_quote($expected_mail_body,'/').'/',$dump);
134cc92aca7SAndreas Gohr
135cc92aca7SAndreas Gohr        $conf['htmlmail'] = 1;
136cc92aca7SAndreas Gohr    }
137cc92aca7SAndreas Gohr
138cc92aca7SAndreas Gohr    function test_replacements(){
139cc92aca7SAndreas Gohr        $mail = new TestMailer();
140cc92aca7SAndreas Gohr
141cc92aca7SAndreas Gohr        $replacements = array( '@DATE@','@BROWSER@','@IPADDRESS@','@HOSTNAME@',
142cc92aca7SAndreas Gohr                               '@TITLE@','@DOKUWIKIURL@','@USER@','@NAME@','@MAIL@');
143cc92aca7SAndreas Gohr        $mail->setBody('A test mail in with replacements '.join(' ',$replacements));
144cc92aca7SAndreas Gohr
145cc92aca7SAndreas Gohr        $text = $mail->prop('text');
146cc92aca7SAndreas Gohr        $html = $mail->prop('html');
147cc92aca7SAndreas Gohr
148cc92aca7SAndreas Gohr        foreach($replacements as $repl){
149cc92aca7SAndreas Gohr            $this->assertNotRegexp("/$repl/",$text,"$repl replacement still in text");
150cc92aca7SAndreas Gohr            $this->assertNotRegexp("/$repl/",$html,"$repl replacement still in html");
151cc92aca7SAndreas Gohr        }
152cc92aca7SAndreas Gohr    }
153cc92aca7SAndreas Gohr
15491effd8dSDominik Eckelmann    /**
15591effd8dSDominik Eckelmann     * @see https://forum.dokuwiki.org/post/35822
15691effd8dSDominik Eckelmann     */
1571f61f312SDominik Eckelmann    function test_emptyBCCorCC() {
1581f61f312SDominik Eckelmann        $mail = new TestMailer();
1591f61f312SDominik Eckelmann        $headers = &$mail->propRef('headers');
1601f61f312SDominik Eckelmann        $headers['Bcc'] = '';
1611f61f312SDominik Eckelmann        $headers['Cc'] = '';
1621f61f312SDominik Eckelmann        $header = $mail->prepareHeaders();
1631f61f312SDominik Eckelmann        $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
1646be717dbSMichael Hamann        $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Cc found in headers.');
1656be717dbSMichael Hamann    }
1666be717dbSMichael Hamann
1676be717dbSMichael Hamann    function test_nullTOorCCorBCC() {
1686be717dbSMichael Hamann        $mail = new TestMailer();
1696be717dbSMichael Hamann        $headers = &$mail->propRef('headers');
1706be717dbSMichael Hamann        $headers['Bcc'] = NULL;
1716be717dbSMichael Hamann        $headers['Cc'] = NULL;
1726be717dbSMichael Hamann        $headers['To'] = NULL;
1736be717dbSMichael Hamann        $header = $mail->prepareHeaders();
1746be717dbSMichael Hamann        $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
1756be717dbSMichael Hamann        $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Cc found in headers.');
1766be717dbSMichael Hamann        $this->assertEquals(0, preg_match('/(^|\n)To: (\n|$)/', $header), 'To found in headers.');
1771f61f312SDominik Eckelmann    }
1780ee5ed1eSAndreas Gohr
1790ee5ed1eSAndreas Gohr    /**
1800ee5ed1eSAndreas Gohr     * @group internet
1810ee5ed1eSAndreas Gohr     */
1820ee5ed1eSAndreas Gohr    function test_lint(){
1830ee5ed1eSAndreas Gohr        // prepare a simple multipart message
1840ee5ed1eSAndreas Gohr        $mail = new TestMailer();
1850ee5ed1eSAndreas Gohr        $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
186d6e04b60SAndreas Gohr        $mail->from('Me <test@example.com>');
1870ee5ed1eSAndreas Gohr        $mail->subject('This is a töst');
1880ee5ed1eSAndreas Gohr        $mail->setBody('Hello Wörld,
1890ee5ed1eSAndreas Gohr
1900ee5ed1eSAndreas Gohr        please don\'t burn, okay?
1910ee5ed1eSAndreas Gohr        ');
192ec82d005SAndreas Gohr        $mail->attachContent('some test data', 'text/plain', 'a text.txt');
1930ee5ed1eSAndreas Gohr        $msg = $mail->dump();
1940ee5ed1eSAndreas Gohr        $msglines = explode("\n", $msg);
1950ee5ed1eSAndreas Gohr
196d6e04b60SAndreas Gohr        //echo $msg;
197d6e04b60SAndreas Gohr
1980ee5ed1eSAndreas Gohr        // ask message lint if it is okay
1990ee5ed1eSAndreas Gohr        $html = new HTTPClient();
2000ee5ed1eSAndreas Gohr        $results = $html->post('http://tools.ietf.org/tools/msglint/msglint', array('msg'=>$msg));
20195e6ded1SAndreas Gohr        if($results === false) {
20295e6ded1SAndreas Gohr            $this->markTestSkipped('no response from validator');
20395e6ded1SAndreas Gohr            return;
20495e6ded1SAndreas Gohr        }
2050ee5ed1eSAndreas Gohr
2060ee5ed1eSAndreas Gohr        // parse the result lines
2070ee5ed1eSAndreas Gohr        $lines = explode("\n", $results);
2080ee5ed1eSAndreas Gohr        $rows  = count($lines);
2090ee5ed1eSAndreas Gohr        $i=0;
2100ee5ed1eSAndreas Gohr        while(trim($lines[$i]) != '-----------' && $i<$rows) $i++; //skip preamble
2110ee5ed1eSAndreas Gohr        for($i=$i+1; $i<$rows; $i++){
2120ee5ed1eSAndreas Gohr            $line = trim($lines[$i]);
2130ee5ed1eSAndreas Gohr            if($line == '-----------') break; //skip appendix
2140ee5ed1eSAndreas Gohr
2150ee5ed1eSAndreas Gohr            // get possible continuation of the line
2160ee5ed1eSAndreas Gohr            while($lines[$i+1][0] == ' '){
2170ee5ed1eSAndreas Gohr                $line .= ' '.trim($lines[$i+1]);
2180ee5ed1eSAndreas Gohr                $i++;
2190ee5ed1eSAndreas Gohr            }
2200ee5ed1eSAndreas Gohr
2210ee5ed1eSAndreas Gohr            // check the line for errors
222ec82d005SAndreas Gohr            if(substr($line,0,5) == 'ERROR' || substr($line,0,7) == 'WARNING'){
223d6e04b60SAndreas Gohr                // ignore some errors
224d6e04b60SAndreas Gohr                if(strpos($line, "missing mandatory header 'return-path'")) continue; #set by MDA
225d6e04b60SAndreas Gohr                if(strpos($line, "bare newline in text body decoded")) continue; #seems to be false positive
226d6e04b60SAndreas Gohr
2270ee5ed1eSAndreas Gohr                // get the context in which the error occured
2280ee5ed1eSAndreas Gohr                $errorin = '';
2290ee5ed1eSAndreas Gohr                if(preg_match('/line (\d+)$/', $line, $m)){
2300ee5ed1eSAndreas Gohr                    $errorin .= "\n".$msglines[$m[1] - 1];
2310ee5ed1eSAndreas Gohr                }
2320ee5ed1eSAndreas Gohr                if(preg_match('/lines (\d+)-(\d+)$/', $line, $m)){
2330ee5ed1eSAndreas Gohr                    for($x=$m[1]-1; $x<$m[2]; $x++){
2340ee5ed1eSAndreas Gohr                        $errorin .= "\n".$msglines[$x];
2350ee5ed1eSAndreas Gohr                    }
2360ee5ed1eSAndreas Gohr                }
2370ee5ed1eSAndreas Gohr
2380ee5ed1eSAndreas Gohr                // raise the error
2390ee5ed1eSAndreas Gohr                throw new Exception($line.$errorin);
2400ee5ed1eSAndreas Gohr            }
2410ee5ed1eSAndreas Gohr        }
2420ee5ed1eSAndreas Gohr
2430ee5ed1eSAndreas Gohr    }
244cc92aca7SAndreas Gohr}
245cc92aca7SAndreas Gohr//Setup VIM: ex: et ts=4 :
246