Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/zendframework/zendframework#6775-mbox-custom-EOL…
Browse files Browse the repository at this point in the history
…-support' into develop

Close zendframework/zendframework#6775
  • Loading branch information
Ocramius committed Dec 31, 2014
4 parents 78fe6d0 + 6abd55e + f03d2ba + 6c82601 commit 9e12fce
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/Storage/Mbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class Mbox extends AbstractStorage
*/
protected $messageClass = '\Zend\Mail\Storage\Message\File';

/**
* end of Line for messages
*
* @var string|null
*/
protected $messageEOL;

/**
* Count messages all messages in current box
*
Expand Down Expand Up @@ -105,8 +112,18 @@ public function getMessage($id)
|| is_subclass_of($this->messageClass, '\Zend\Mail\Storage\Message\File')) {
// TODO top/body lines
$messagePos = $this->getPos($id);
return new $this->messageClass(array('file' => $this->fh, 'startPos' => $messagePos['start'],
'endPos' => $messagePos['end']));

$messageClassParams = array(
'file' => $this->fh,
'startPos' => $messagePos['start'],
'endPos' => $messagePos['end']
);

if (isset($this->messageEOL)) {
$messageClassParams['EOL'] = $this->messageEOL;
}

return new $this->messageClass($messageClassParams);
}

$bodyLines = 0; // TODO: need a way to change that
Expand Down Expand Up @@ -181,6 +198,10 @@ public function __construct($params)
throw new Exception\InvalidArgumentException('no valid filename given in params');
}

if (isset($params->messageEOL)) {
$this->messageEOL = (string) $params->messageEOL;
}

$this->openMboxFile($params->filename);
$this->has['top'] = true;
$this->has['uniqueid'] = false;
Expand Down
7 changes: 6 additions & 1 deletion src/Storage/Part/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class File extends Part
* - file filename or open file handler with message content (required)
* - startPos start position of message or part in file (default: current position)
* - endPos end position of message or part in file (default: end of file)
* - EOL end of Line for messages
*
* @param array $params full message with or without headers
* @throws Exception\RuntimeException
Expand Down Expand Up @@ -53,7 +54,11 @@ public function __construct(array $params)
$header .= $line;
}

$this->headers = Headers::fromString($header);
if (isset($params['EOL'])) {
$this->headers = Headers::fromString($header, $params['EOL']);
} else {
$this->headers = Headers::fromString($header);
}

$this->contentPos[0] = ftell($this->fh);
if ($endPos !== null) {
Expand Down
40 changes: 40 additions & 0 deletions test/Storage/MboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MboxTest extends \PHPUnit_Framework_TestCase
{
protected $_mboxOriginalFile;
protected $_mboxFile;
protected $_mboxFileUnix;
protected $_tmpdir;

public function setUp()
Expand Down Expand Up @@ -53,6 +54,10 @@ public function setUp()
public function tearDown()
{
unlink($this->_mboxFile);

if ($this->_mboxFileUnix) {
unlink($this->_mboxFileUnix);
}
}

public function testLoadOk()
Expand Down Expand Up @@ -155,6 +160,17 @@ public function testFetchTopBody()
}
*/

/**
* @group 6775
*/
public function testFetchMessageHeaderUnix()
{
$mail = new Storage\Mbox(array('filename' => $this->getUnixMboxFile(), 'messageEOL' => "\n"));

$subject = $mail->getMessage(1)->subject;
$this->assertEquals('Simple Message', $subject);
}

public function testFetchMessageHeader()
{
$mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
Expand All @@ -172,6 +188,18 @@ public function testFetchMessageBody()
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
}

/**
* @group 6775
*/
public function testFetchMessageBodyUnix()
{
$mail = new Storage\Mbox(array('filename' => $this->getUnixMboxFile(), 'messageEOL' => "\n"));

$content = $mail->getMessage(3)->getContent();
list($content, ) = explode("\n", $content, 2);
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
}

public function testFailedRemove()
{
$mail = new Storage\Mbox(array('filename' => $this->_mboxFile));
Expand Down Expand Up @@ -295,4 +323,16 @@ public function testShortMbox()
$this->assertEquals($mail->getMessage(2)->subject, 'test2');
$this->assertEquals($mail->getMessage(2)->getContent(), '');
}

/**
* @return string
*/
private function getUnixMboxFile()
{
$this->_mboxFileUnix = $this->_tmpdir . 'INBOX.unix';

copy(__DIR__ . '/../_files/test.mbox/INBOX.unix', $this->_mboxFileUnix);

return $this->_mboxFileUnix;
}
}
114 changes: 114 additions & 0 deletions test/_files/test.mbox/INBOX.unix
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
From next-message@example.com Mon Jan 00 00:00:00 0000
Return-Path: <from@example.com>
Delivered-To: to@example.com
Received: by example.com
id 1; Sun, 30 Apr 2006 19:00:00 +0200 (CEST)
Received: by localhost
id 1; Sun, 30 Apr 2006 19:10:00 +0200 (CEST)
To: to@example.com
Subject: Simple Message
Message-Id: <20060430185000.1@example.com>
Date: Sun, 30 Apr 2006 18:50:00 +0200 (CEST)
From: from@example.com

This is a simple test message
From next-message@example.com Mon Jan 00 00:00:00 0000
To: bar@example.com
Subject: A Really Simple Message
From: foo@example.com

Message

From next-message@example.com Mon Jan 00 00:00:00 0000
To: river@example.com
Subject: To the River
Date: Sun, 01 Jan 1829 00:00:00 +0000
From: poe@example.com
Message-Id: <18290101000000.0000@example.com>
Content-type: text/plain
MIME-version: 1.0
X-Twin: the good
X-Twin: the evil

Fair river! in thy bright, clear flow
Of crystal, wandering water,
Thou art an emblem of the glow
Of beautythe unhidden heart
The playful maziness of art
In old Alberto's daughter;

But when within thy wave she looks
Which glistens then, and trembles
Why, then, the prettiest of brooks
Her worshipper resembles;
For in his heart, as in thy stream,
Her image deeply lies
His heart which trembles at the beam
Of her soul-searching eyes.

From next-message@example.com Mon Jan 00 00:00:00 0000
To: foo@example.com
Subject: multipart
Date: Sun, 01 Jan 2000 00:00:00 +0000
From: crazy@example.com
Content-type: multipart/alternative; boundary="crazy-multipart"
MIME-version: 1.0

multipart message
--crazy-multipart
Content-type: text/plain

The first part
is horizontal

--crazy-multipart
Content-type: text/x-vertical

T s p i v
h e a s e
e c r r
o t t
n i
d c
a
l
--crazy-multipart--

From next-message@example.com Mon Jan 00 00:00:00 0000
To: foo@example.com
Subject: multipart
Date: Sun, 01 Jan 2000 01:00:00 +0000
From: normal@example.com
Content-type: multipart/alternative; boundary="normal-multipart"
MIME-version: 1.0

multipart message
--normal-multipart
Content-type: text/html

<html>
<head><style>
body{background:#008;color:white}
em{color:#f88}
</style></head>
<body>Again a <em>simple</em> message</body>
</html>
--normal-multipart
Content-type: text/plain

Again a simple message
--normal-multipart--
From next-message@example.com Mon Jan 00 00:00:00 0000
To: foo@example.com
Subject: no body
Date: Sun, 01 Jan 2000 01:00:00 +0000
From: short@example.com
From next-message@example.com Mon Jan 00 00:00:00 0000
To: to@example.com
Subject: Dot Test
Date: Sun, 01 Jan 2000 01:00:00 +0000
From: from@example.com

Before the dot
.
is after the dot

0 comments on commit 9e12fce

Please sign in to comment.