Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add charset for imap_search or imap_sort #402

Merged
merged 1 commit into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function clearFlag(string $flag, $numbers): bool
*
* @return MessageIteratorInterface
*/
public function getMessages(ConditionInterface $search = null, int $sortCriteria = null, bool $descending = false): MessageIteratorInterface
public function getMessages(ConditionInterface $search = null, int $sortCriteria = null, bool $descending = false, string $charset = 'UTF-8'): MessageIteratorInterface
{
if (null === $search) {
$search = new All();
Expand All @@ -179,9 +179,9 @@ public function getMessages(ConditionInterface $search = null, int $sortCriteria
\imap_errors();

if (null !== $sortCriteria) {
$messageNumbers = \imap_sort($this->resource->getStream(), $sortCriteria, $descending ? 1 : 0, \SE_UID, $query);
$messageNumbers = \imap_sort($this->resource->getStream(), $sortCriteria, $descending ? 1 : 0, \SE_UID, $query, $charset);
} else {
$messageNumbers = \imap_search($this->resource->getStream(), $query, \SE_UID);
$messageNumbers = \imap_search($this->resource->getStream(), $query, \SE_UID, $charset);
}
if (false === $messageNumbers) {
if (false !== \imap_last_error()) {
Expand Down
2 changes: 1 addition & 1 deletion src/MailboxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function clearFlag(string $flag, $numbers): bool;
*
* @return MessageIteratorInterface
*/
public function getMessages(ConditionInterface $search = null, int $sortCriteria = null, bool $descending = false): MessageIteratorInterface;
public function getMessages(ConditionInterface $search = null, int $sortCriteria = null, bool $descending = false, string $charset = 'UTF-8'): MessageIteratorInterface;

/**
* Get message iterator for a sequence.
Expand Down
52 changes: 52 additions & 0 deletions tests/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Ddeboer\Imap\Exception\ReopenMailboxException;
use Ddeboer\Imap\MailboxInterface;
use Ddeboer\Imap\MessageIterator;
use Ddeboer\Imap\MessageIteratorInterface;
use Ddeboer\Imap\Search;

/**
* @covers \Ddeboer\Imap\Exception\AbstractException
Expand Down Expand Up @@ -298,4 +300,54 @@ public function testBulkCopy()
$this->expectException(MessageCopyException::class);
$this->mailbox->copy($messages, $anotherMailbox);
}

public function testSort()
{
$anotherMailbox = $this->createMailbox();
$this->createTestMessage($anotherMailbox, 'B');
$this->createTestMessage($anotherMailbox, 'A');
$this->createTestMessage($anotherMailbox, 'C');

$concatSubjects = function (MessageIteratorInterface $it) {
$subject = '';
foreach ($it as $message) {
$subject .= $message->getSubject();
}

return $subject;
};

$this->assertSame('BAC', $concatSubjects($anotherMailbox->getMessages()));
$this->assertSame('ABC', $concatSubjects($anotherMailbox->getMessages(null, \SORTSUBJECT)));
$this->assertSame('CBA', $concatSubjects($anotherMailbox->getMessages(null, \SORTSUBJECT, true)));
$this->assertSame('B', $concatSubjects($anotherMailbox->getMessages(new Search\Text\Subject('B'), \SORTSUBJECT, true)));
}

public function testGetMessagesWithUtf8Subject()
{
$anotherMailbox = $this->createMailbox();
$this->createTestMessage($anotherMailbox, '1', 'Ж П');
$this->createTestMessage($anotherMailbox, '2', 'Ж б');
$this->createTestMessage($anotherMailbox, '3', 'б П');

$messagesFound = '';
foreach ($anotherMailbox->getMessages(new Search\Text\Body(\mb_convert_encoding('б', 'Windows-1251', 'UTF-8')), null, false, 'Windows-1251') as $message) {
$subject = $message->getSubject();
$this->assertIsString($subject);

$messagesFound .= \substr($subject, 0, 1);
}

$this->assertSame('23', $messagesFound);

$messagesFound = '';
foreach ($anotherMailbox->getMessages(new Search\Text\Body(\mb_convert_encoding('П', 'Windows-1251', 'UTF-8')), \SORTSUBJECT, true, 'Windows-1251') as $message) {
$subject = $message->getSubject();
$this->assertIsString($subject);

$messagesFound .= \substr($subject, 0, 1);
}

$this->assertSame('31', $messagesFound);
}
}
23 changes: 0 additions & 23 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use Ddeboer\Imap\Message\PartInterface;
use Ddeboer\Imap\Message\Transcoder;
use Ddeboer\Imap\MessageInterface;
use Ddeboer\Imap\MessageIteratorInterface;
use Ddeboer\Imap\Search;
use PHPUnit\Framework\Error\Deprecated;
use ReflectionClass;
use Zend\Mail;
Expand Down Expand Up @@ -759,27 +757,6 @@ public function testIssue142()
$this->assertCount(1, $message->getAttachments());
}

public function testSort()
{
$this->createTestMessage($this->mailbox, 'B');
$this->createTestMessage($this->mailbox, 'A');
$this->createTestMessage($this->mailbox, 'C');

$concatSubjects = function (MessageIteratorInterface $it) {
$subject = '';
foreach ($it as $message) {
$subject .= $message->getSubject();
}

return $subject;
};

$this->assertSame('BAC', $concatSubjects($this->mailbox->getMessages()));
$this->assertSame('ABC', $concatSubjects($this->mailbox->getMessages(null, \SORTSUBJECT)));
$this->assertSame('CBA', $concatSubjects($this->mailbox->getMessages(null, \SORTSUBJECT, true)));
$this->assertSame('B', $concatSubjects($this->mailbox->getMessages(new Search\Text\Subject('B'), \SORTSUBJECT, true)));
}

public function testSignedMessage()
{
$fixture = $this->getFixture('pec');
Expand Down