Skip to content

Commit c22b7d1

Browse files
Krushelnitskiymccar
authored andcommitted
BAP-13634: Incorrect Email Messages Id and UID usage in IMAP integration (#7240)
1 parent 70b6f33 commit c22b7d1

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

src/Oro/Bundle/ImapBundle/Connector/ImapMessageIterator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ protected function initialize()
208208
$this->iterationMin = 0;
209209
$this->iterationMax = count($this->ids) - 1;
210210
}
211+
212+
$this->imap->clearCacheUniqueId();
211213
}
212214

213215
/**

src/Oro/Bundle/ImapBundle/Mail/Storage/Imap.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ class Imap extends \Zend\Mail\Storage\Imap
6868
*/
6969
private $ignoreCloseCommand = false;
7070

71+
/**
72+
* It is used to cache response from getUniqueId in getNumberByUniqueId
73+
*
74+
* @var array
75+
*/
76+
private $uniqueIds = [];
77+
7178
/**
7279
* {@inheritdoc}
7380
* @SuppressWarnings(PHPMD.NPathComplexity)
@@ -387,6 +394,34 @@ public function close()
387394
parent::close();
388395
}
389396

397+
/**
398+
* {@inheritdoc}
399+
*/
400+
public function getNumberByUniqueId($id)
401+
{
402+
if (!$this->uniqueIds) {
403+
$uniqueIds = $this->getUniqueId();
404+
if (is_array($uniqueIds)) {
405+
$this->uniqueIds = $uniqueIds;
406+
}
407+
}
408+
foreach ($this->uniqueIds as $k => $v) {
409+
if ($v == $id) {
410+
return $k;
411+
}
412+
}
413+
414+
throw new BaseException\InvalidArgumentException('unique id not found');
415+
}
416+
417+
/**
418+
* Clear cache of UniqueId which was created in method getNumberByUniqueId
419+
*/
420+
public function clearCacheUniqueId()
421+
{
422+
$this->uniqueIds = [];
423+
}
424+
390425
/**
391426
* Creates Message object based on the given data
392427
*
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Oro\Bundle\ImapBundle\Tests\Unit\Mail\Storage;
4+
5+
use Zend\Mail\Storage\Exception\InvalidArgumentException;
6+
7+
use Oro\Bundle\ImapBundle\Mail\Storage\Imap;
8+
9+
class ImapTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testCacheForGetNumberByUniqueId()
12+
{
13+
$ids = [
14+
1 => 1,
15+
2 => 2,
16+
3 => 3,
17+
4 => 4,
18+
5 => 5
19+
];
20+
21+
$protocolImap = $this->getMockBuilder('Oro\Bundle\ImapBundle\Mail\Protocol\Imap')
22+
->disableOriginalConstructor()->getMock();
23+
$protocolImap->expects(self::once())->method('select')->willReturn(['uidvalidity'=>'']);
24+
$protocolImap->expects(self::once())->method('fetch')->willReturn($ids);
25+
26+
$imap = new Imap($protocolImap);
27+
28+
$id = '3';
29+
self::assertEquals(3, $imap->getNumberByUniqueId($id));
30+
31+
$protocolImap->expects(self::never())->method('fetch');
32+
self::assertEquals(3, $imap->getNumberByUniqueId($id));
33+
}
34+
35+
/**
36+
* @expectedException InvalidArgumentException
37+
* @expectedExceptionMessage unique id not found
38+
*/
39+
public function testExceptionForGetNumberByUniqueId()
40+
{
41+
$ids = [
42+
1 => 1,
43+
2 => 2,
44+
3 => 3,
45+
4 => 4,
46+
5 => 5
47+
];
48+
49+
$protocolImap = $this->getMockBuilder('Oro\Bundle\ImapBundle\Mail\Protocol\Imap')
50+
->disableOriginalConstructor()->getMock();
51+
$protocolImap->expects(self::once())->method('select')->willReturn(['uidvalidity'=>'']);
52+
$protocolImap->expects(self::once())->method('fetch')->willReturn($ids);
53+
54+
$imap = new Imap($protocolImap);
55+
56+
$id = '6';
57+
$imap->getNumberByUniqueId($id);
58+
}
59+
60+
/**
61+
* @expectedException InvalidArgumentException
62+
* @expectedExceptionMessage unique id not found
63+
*/
64+
public function testCacheForGetNumberByUniqueIdNotArray()
65+
{
66+
$ids = 3;
67+
68+
$protocolImap = $this->getMockBuilder('Oro\Bundle\ImapBundle\Mail\Protocol\Imap')
69+
->disableOriginalConstructor()->getMock();
70+
$protocolImap->expects(self::once())->method('select')->willReturn(['uidvalidity'=>'']);
71+
$protocolImap->expects(self::once())->method('fetch')->willReturn($ids);
72+
73+
$imap = new Imap($protocolImap);
74+
75+
$id = '3';
76+
$imap->getNumberByUniqueId($id);
77+
}
78+
}

0 commit comments

Comments
 (0)