Skip to content

Commit

Permalink
BAP-13634: Incorrect Email Messages Id and UID usage in IMAP integrat…
Browse files Browse the repository at this point in the history
…ion (#7240)
  • Loading branch information
Krushelnitskiy authored and mccar committed Feb 3, 2017
1 parent 70b6f33 commit c22b7d1
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Oro/Bundle/ImapBundle/Connector/ImapMessageIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ protected function initialize()
$this->iterationMin = 0;
$this->iterationMax = count($this->ids) - 1;
}

$this->imap->clearCacheUniqueId();
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Oro/Bundle/ImapBundle/Mail/Storage/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class Imap extends \Zend\Mail\Storage\Imap
*/
private $ignoreCloseCommand = false;

/**
* It is used to cache response from getUniqueId in getNumberByUniqueId
*
* @var array
*/
private $uniqueIds = [];

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.NPathComplexity)
Expand Down Expand Up @@ -387,6 +394,34 @@ public function close()
parent::close();
}

/**
* {@inheritdoc}
*/
public function getNumberByUniqueId($id)
{
if (!$this->uniqueIds) {
$uniqueIds = $this->getUniqueId();
if (is_array($uniqueIds)) {
$this->uniqueIds = $uniqueIds;
}
}
foreach ($this->uniqueIds as $k => $v) {
if ($v == $id) {
return $k;
}
}

throw new BaseException\InvalidArgumentException('unique id not found');
}

/**
* Clear cache of UniqueId which was created in method getNumberByUniqueId
*/
public function clearCacheUniqueId()
{
$this->uniqueIds = [];
}

/**
* Creates Message object based on the given data
*
Expand Down
78 changes: 78 additions & 0 deletions src/Oro/Bundle/ImapBundle/Tests/Unit/Mail/Storage/ImapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Oro\Bundle\ImapBundle\Tests\Unit\Mail\Storage;

use Zend\Mail\Storage\Exception\InvalidArgumentException;

use Oro\Bundle\ImapBundle\Mail\Storage\Imap;

class ImapTest extends \PHPUnit_Framework_TestCase
{
public function testCacheForGetNumberByUniqueId()
{
$ids = [
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5
];

$protocolImap = $this->getMockBuilder('Oro\Bundle\ImapBundle\Mail\Protocol\Imap')
->disableOriginalConstructor()->getMock();
$protocolImap->expects(self::once())->method('select')->willReturn(['uidvalidity'=>'']);
$protocolImap->expects(self::once())->method('fetch')->willReturn($ids);

$imap = new Imap($protocolImap);

$id = '3';
self::assertEquals(3, $imap->getNumberByUniqueId($id));

$protocolImap->expects(self::never())->method('fetch');
self::assertEquals(3, $imap->getNumberByUniqueId($id));
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage unique id not found
*/
public function testExceptionForGetNumberByUniqueId()
{
$ids = [
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5
];

$protocolImap = $this->getMockBuilder('Oro\Bundle\ImapBundle\Mail\Protocol\Imap')
->disableOriginalConstructor()->getMock();
$protocolImap->expects(self::once())->method('select')->willReturn(['uidvalidity'=>'']);
$protocolImap->expects(self::once())->method('fetch')->willReturn($ids);

$imap = new Imap($protocolImap);

$id = '6';
$imap->getNumberByUniqueId($id);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage unique id not found
*/
public function testCacheForGetNumberByUniqueIdNotArray()
{
$ids = 3;

$protocolImap = $this->getMockBuilder('Oro\Bundle\ImapBundle\Mail\Protocol\Imap')
->disableOriginalConstructor()->getMock();
$protocolImap->expects(self::once())->method('select')->willReturn(['uidvalidity'=>'']);
$protocolImap->expects(self::once())->method('fetch')->willReturn($ids);

$imap = new Imap($protocolImap);

$id = '3';
$imap->getNumberByUniqueId($id);
}
}

0 comments on commit c22b7d1

Please sign in to comment.