xref: /dokuwiki/_test/tests/inc/mailer.test.php (revision b6c97c7072748fa573d54a91c1be14e496fd9990)
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    }
18*b6c97c70SAndreas Gohr
19*b6c97c70SAndreas Gohr    public function cleanHeaders() {
20*b6c97c70SAndreas Gohr        parent::cleanHeaders();
21*b6c97c70SAndreas Gohr    }
22*b6c97c70SAndreas 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
75*b6c97c70SAndreas Gohr    function test_addresses(){
76*b6c97c70SAndreas Gohr        $mail = new TestMailer();
77*b6c97c70SAndreas Gohr
78*b6c97c70SAndreas Gohr        $mail->to('andi@splitbrain.org');
79*b6c97c70SAndreas Gohr        $mail->cleanHeaders();
80*b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
81*b6c97c70SAndreas Gohr        $this->assertEquals('andi@splitbrain.org', $headers['To']);
82*b6c97c70SAndreas Gohr
83*b6c97c70SAndreas Gohr        $mail->to('<andi@splitbrain.org>');
84*b6c97c70SAndreas Gohr        $mail->cleanHeaders();
85*b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
86*b6c97c70SAndreas Gohr        $this->assertEquals('andi@splitbrain.org', $headers['To']);
87*b6c97c70SAndreas Gohr
88*b6c97c70SAndreas Gohr        $mail->to('Andreas Gohr <andi@splitbrain.org>');
89*b6c97c70SAndreas Gohr        $mail->cleanHeaders();
90*b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
91*b6c97c70SAndreas Gohr        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>', $headers['To']);
92*b6c97c70SAndreas Gohr
93*b6c97c70SAndreas Gohr        $mail->to('Andreas Gohr <andi@splitbrain.org> , foo <foo@example.com>');
94*b6c97c70SAndreas Gohr        $mail->cleanHeaders();
95*b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
96*b6c97c70SAndreas Gohr        $this->assertEquals('Andreas Gohr <andi@splitbrain.org>, foo <foo@example.com>', $headers['To']);
97*b6c97c70SAndreas Gohr
98*b6c97c70SAndreas Gohr        $mail->to('Möp <moep@example.com> , foo <foo@example.com>');
99*b6c97c70SAndreas Gohr        $mail->cleanHeaders();
100*b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
101*b6c97c70SAndreas Gohr        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
102*b6c97c70SAndreas Gohr
103*b6c97c70SAndreas Gohr        $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>'));
104*b6c97c70SAndreas Gohr        $mail->cleanHeaders();
105*b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
106*b6c97c70SAndreas Gohr        $this->assertEquals('=?UTF-8?B?TcO2cA==?= <moep@example.com>, foo <foo@example.com>', $headers['To']);
107*b6c97c70SAndreas Gohr
108*b6c97c70SAndreas Gohr        $mail->to(array('Beet, L van <lvb@example.com>',' foo <foo@example.com>'));
109*b6c97c70SAndreas Gohr        $mail->cleanHeaders();
110*b6c97c70SAndreas Gohr        $headers = $mail->prop('headers');
111*b6c97c70SAndreas Gohr        $this->assertEquals('=?UTF-8?B?QmVldCwgTCB2YW4=?= <lvb@example.com>, foo <foo@example.com>', $headers['To']);
112*b6c97c70SAndreas Gohr
113*b6c97c70SAndreas Gohr
114*b6c97c70SAndreas Gohr    }
115*b6c97c70SAndreas 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    }
159cc92aca7SAndreas Gohr}
160cc92aca7SAndreas Gohr//Setup VIM: ex: et ts=4 :
161