Skip to content

Commit

Permalink
Added PHPUnit tests for MIME decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Krätzig committed May 1, 2019
1 parent 37b2464 commit a3210dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/PhpImap/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,13 @@ protected function initMailPart(IncomingMail $mail, $partStructure, $partNum, $m
}
}

protected function decodeMimeStr($string, $toCharset = 'utf-8') {
/**
* Decodes a mime string
* @param string $string
* @param string $toEncoding
* @return string Converted string if conversion was successful, or the original string if not
*/
public function decodeMimeStr($string, $toCharset = 'utf-8') {
$newString = '';
foreach(imap_mime_header_decode($string) as $element) {
if(isset($element->text)) {
Expand Down
20 changes: 17 additions & 3 deletions tests/unit/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,29 @@ public function testParsedDateDifferentTimeZones(){
/**
* Test, parsed datetime being emtpy the header date
*/

public function testParsedDateTimeWithEmptyHeaderDate(){

$parsedDt = $this->mailbox->parseDateTime('');
$now = new \DateTime;
$this->assertEquals($parsedDt, $now->format('Y-m-d H:i:s'));

}

/**
* Test, that mime encoding returns correct strings
*/
public function testMimeEncoding() {
$test_strings = array(
'=?iso-8859-1?Q?Sebastian_Kr=E4tzig?= <sebastian.kraetzig@example.com>' => 'Sebastian Krätzig <sebastian.kraetzig@example.com>',
'=?iso-8859-1?Q?Sebastian_Kr=E4tzig?=' => 'Sebastian Krätzig',
'sebastian.kraetzig' => 'sebastian.kraetzig',
'=?US-ASCII?Q?Keith_Moore?= <km@ab.example.edu>' => 'Keith Moore <km@ab.example.edu>',
'=?ISO-8859-1?Q?Max_J=F8rn_Simsen?= <max.joern.s@example.dk>' => 'Max Jørn Simsen <max.joern.s@example.dk>',
'=?ISO-8859-1?Q?Andr=E9?= Muster <andre.muster@vm1.ulg.ac.be>' => 'André Muster <andre.muster@vm1.ulg.ac.be>',
'=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=' => 'If you can read this you understand the example.'
);


foreach($test_strings as $str => $expected) {
$this->assertEquals($this->mailbox->decodeMimeStr($str), $expected);
}
}
}

1 comment on commit a3210dd

@Sebbo94BY
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to #246.

Please sign in to comment.