xref: /dokuwiki/_test/tests/inc/mailer.test.php (revision 187b13eb2e4f044659ad056a038b7aed65033579)
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
20class mailer_test extends DokuWikiTest {
21
22
23    function test_userheader(){
24        $mail = new TestMailer();
25        $headers = $mail->prop('headers');
26        $this->assertArrayNotHasKey('X-Dokuwiki-User',$headers);
27
28        $_SERVER['REMOTE_USER'] = 'andi';
29        $mail = new TestMailer();
30        $headers = $mail->prop('headers');
31        $this->assertArrayHasKey('X-Dokuwiki-User',$headers);
32    }
33
34    function test_setHeader(){
35        $mail = new TestMailer();
36
37        // check existance of default headers
38        $headers = $mail->prop('headers');
39        $this->assertArrayHasKey('X-Mailer',$headers);
40        $this->assertArrayHasKey('X-Dokuwiki-Title',$headers);
41        $this->assertArrayHasKey('X-Dokuwiki-Server',$headers);
42        $this->assertArrayHasKey('X-Auto-Response-Suppress',$headers);
43        $this->assertArrayHasKey('List-Id',$headers);
44
45        // set a bunch of test headers
46        $mail->setHeader('test-header','bla');
47        $mail->setHeader('to','A valid ASCII name <test@example.com>');
48        $mail->setHeader('from',"Thös ne\needs\x00serious cleaning$§%.");
49        $mail->setHeader('bad',"Thös ne\needs\x00serious cleaning$§%.",false);
50        $mail->setHeader("weird\n*+\x00foo.-_@bar?",'now clean');
51
52        // are they set?
53        $headers = $mail->prop('headers');
54        $this->assertArrayHasKey('Test-Header',$headers);
55        $this->assertEquals('bla',$headers['Test-Header']);
56        $this->assertArrayHasKey('To',$headers);
57        $this->assertEquals('A valid ASCII name <test@example.com>',$headers['To']);
58        $this->assertArrayHasKey('From',$headers);
59        $this->assertEquals('Ths neeedsserious cleaning.',$headers['From']);
60        $this->assertArrayHasKey('Bad',$headers);
61        $this->assertEquals("Thös ne\needs\x00serious cleaning$§%.",$headers['Bad']);
62        $this->assertArrayHasKey('Weird+foo.-_@bar',$headers);
63
64        // unset a header again
65        $mail->setHeader('test-header','');
66        $headers = $mail->prop('headers');
67        $this->assertArrayNotHasKey('Test-Header',$headers);
68    }
69
70    function test_simplemail(){
71        global $conf;
72        $conf['htmlmail'] = 0;
73        $mail = new TestMailer();
74        $mail->to('test@example.com');
75        $mail->setBody('A test mail in ASCII');
76
77        $dump = $mail->dump();
78        $this->assertNotRegexp('/Content-Type: multipart/',$dump);
79        $this->assertRegexp('#Content-Type: text/plain; charset=UTF-8#',$dump);
80        $this->assertRegexp('/'.base64_encode('A test mail in ASCII').'/',$dump);
81
82        $conf['htmlmail'] = 1;
83    }
84
85    function test_replacements(){
86        $mail = new TestMailer();
87
88        $replacements = array( '@DATE@','@BROWSER@','@IPADDRESS@','@HOSTNAME@',
89                               '@TITLE@','@DOKUWIKIURL@','@USER@','@NAME@','@MAIL@');
90        $mail->setBody('A test mail in with replacements '.join(' ',$replacements));
91
92        $text = $mail->prop('text');
93        $html = $mail->prop('html');
94
95        foreach($replacements as $repl){
96            $this->assertNotRegexp("/$repl/",$text,"$repl replacement still in text");
97            $this->assertNotRegexp("/$repl/",$html,"$repl replacement still in html");
98        }
99    }
100
101    /**
102     * @see https://forum.dokuwiki.org/post/35822
103     */
104    function test_emptyBCCorCC() {
105        $mail = new TestMailer();
106        $headers = &$mail->propRef('headers');
107        $headers['Bcc'] = '';
108        $headers['Cc'] = '';
109        $header = $mail->prepareHeaders();
110        $this->assertEquals(0, preg_match('/(^|\n)Bcc: (\n|$)/', $header), 'Bcc found in headers.');
111        $this->assertEquals(0, preg_match('/(^|\n)Cc: (\n|$)/', $header), 'Bcc found in headers.');
112    }
113}
114//Setup VIM: ex: et ts=4 :
115