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 } 1590ee5ed1eSAndreas Gohr 1600ee5ed1eSAndreas Gohr /** 1610ee5ed1eSAndreas Gohr * @group internet 1620ee5ed1eSAndreas Gohr */ 1630ee5ed1eSAndreas Gohr function test_lint(){ 1640ee5ed1eSAndreas Gohr // prepare a simple multipart message 1650ee5ed1eSAndreas Gohr $mail = new TestMailer(); 1660ee5ed1eSAndreas Gohr $mail->to(array('Möp <moep@example.com> ',' foo <foo@example.com>')); 167*d6e04b60SAndreas Gohr $mail->from('Me <test@example.com>'); 1680ee5ed1eSAndreas Gohr $mail->subject('This is a töst'); 1690ee5ed1eSAndreas Gohr $mail->setBody('Hello Wörld, 1700ee5ed1eSAndreas Gohr 1710ee5ed1eSAndreas Gohr please don\'t burn, okay? 1720ee5ed1eSAndreas Gohr '); 1730ee5ed1eSAndreas Gohr $mail->attachContent('some test data', 'text/plain', 'text.txt'); 1740ee5ed1eSAndreas Gohr $msg = $mail->dump(); 1750ee5ed1eSAndreas Gohr $msglines = explode("\n", $msg); 1760ee5ed1eSAndreas Gohr 177*d6e04b60SAndreas Gohr //echo $msg; 178*d6e04b60SAndreas Gohr 1790ee5ed1eSAndreas Gohr // ask message lint if it is okay 1800ee5ed1eSAndreas Gohr $html = new HTTPClient(); 1810ee5ed1eSAndreas Gohr $results = $html->post('http://tools.ietf.org/tools/msglint/msglint', array('msg'=>$msg)); 1820ee5ed1eSAndreas Gohr $this->assertTrue($results !== false); 1830ee5ed1eSAndreas Gohr 1840ee5ed1eSAndreas Gohr // parse the result lines 1850ee5ed1eSAndreas Gohr $lines = explode("\n", $results); 1860ee5ed1eSAndreas Gohr $rows = count($lines); 1870ee5ed1eSAndreas Gohr $i=0; 1880ee5ed1eSAndreas Gohr while(trim($lines[$i]) != '-----------' && $i<$rows) $i++; //skip preamble 1890ee5ed1eSAndreas Gohr for($i=$i+1; $i<$rows; $i++){ 1900ee5ed1eSAndreas Gohr $line = trim($lines[$i]); 1910ee5ed1eSAndreas Gohr if($line == '-----------') break; //skip appendix 1920ee5ed1eSAndreas Gohr 1930ee5ed1eSAndreas Gohr // get possible continuation of the line 1940ee5ed1eSAndreas Gohr while($lines[$i+1][0] == ' '){ 1950ee5ed1eSAndreas Gohr $line .= ' '.trim($lines[$i+1]); 1960ee5ed1eSAndreas Gohr $i++; 1970ee5ed1eSAndreas Gohr } 1980ee5ed1eSAndreas Gohr 1990ee5ed1eSAndreas Gohr // check the line for errors 2000ee5ed1eSAndreas Gohr if(substr($line,0,5) == 'ERROR'){ 201*d6e04b60SAndreas Gohr // ignore some errors 202*d6e04b60SAndreas Gohr if(strpos($line, "missing mandatory header 'return-path'")) continue; #set by MDA 203*d6e04b60SAndreas Gohr if(strpos($line, "bare newline in text body decoded")) continue; #seems to be false positive 204*d6e04b60SAndreas Gohr 2050ee5ed1eSAndreas Gohr // get the context in which the error occured 2060ee5ed1eSAndreas Gohr $errorin = ''; 2070ee5ed1eSAndreas Gohr if(preg_match('/line (\d+)$/', $line, $m)){ 2080ee5ed1eSAndreas Gohr $errorin .= "\n".$msglines[$m[1] - 1]; 2090ee5ed1eSAndreas Gohr } 2100ee5ed1eSAndreas Gohr if(preg_match('/lines (\d+)-(\d+)$/', $line, $m)){ 2110ee5ed1eSAndreas Gohr for($x=$m[1]-1; $x<$m[2]; $x++){ 2120ee5ed1eSAndreas Gohr $errorin .= "\n".$msglines[$x]; 2130ee5ed1eSAndreas Gohr } 2140ee5ed1eSAndreas Gohr } 2150ee5ed1eSAndreas Gohr 2160ee5ed1eSAndreas Gohr // raise the error 2170ee5ed1eSAndreas Gohr throw new Exception($line.$errorin); 2180ee5ed1eSAndreas Gohr } 2190ee5ed1eSAndreas Gohr } 2200ee5ed1eSAndreas Gohr 2210ee5ed1eSAndreas Gohr } 222cc92aca7SAndreas Gohr} 223cc92aca7SAndreas Gohr//Setup VIM: ex: et ts=4 : 224