-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7629 from laboro/bug/CRM-5529
CRM-5529: Recipient names which contain a comma may be perceived as t…
- Loading branch information
Showing
11 changed files
with
193 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Oro/Bundle/EmailBundle/Form/DataTransformer/EmailAddressRecipientsTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Oro\Bundle\EmailBundle\Form\DataTransformer; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Oro\Bundle\EmailBundle\Provider\EmailRecipientsHelper; | ||
|
||
/** | ||
* Transforms form value between array of full email addresses and string of base64 encoded full email addresses. | ||
*/ | ||
class EmailAddressRecipientsTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transform($value) | ||
{ | ||
if (!is_array($value)) { | ||
return $value; | ||
} | ||
|
||
return EmailRecipientsHelper::prepareFormRecipientIds($value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (is_array($value)) { | ||
return $value; | ||
} | ||
|
||
return EmailRecipientsHelper::extractFormRecipientIds($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
class EmailRecipientsHelper | ||
{ | ||
const ORGANIZATION_PROPERTY = 'organization'; | ||
const EMAIL_IDS_SEPARATOR = ';'; | ||
|
||
/** @var AclHelper */ | ||
protected $aclHelper; | ||
|
@@ -167,9 +168,9 @@ public function createRecipientData(Recipient $recipient) | |
} | ||
|
||
return [ | ||
'id' => $recipient->getId(), | ||
'text' => $recipient->getName(), | ||
'data' => json_encode($data), | ||
'id' => self::prepareFormRecipientIds($recipient->getId()), | ||
'text' => $recipient->getName(), | ||
'data' => json_encode($data), | ||
]; | ||
} | ||
|
||
|
@@ -325,7 +326,7 @@ protected function recipientsFromResult(array $result, $entityClass) | |
foreach ($result as $row) { | ||
$recipient = new CategorizedRecipient( | ||
$row['email'], | ||
sprintf('%s <%s>', $row['name'], $row['email']), | ||
sprintf('"%s" <%s>', $row['name'], $row['email']), | ||
new RecipientEntity( | ||
$entityClass, | ||
$row['entityId'], | ||
|
@@ -351,4 +352,44 @@ protected function getPropertyAccessor() | |
|
||
return $this->propertyAccessor; | ||
} | ||
|
||
/** | ||
* Prepares base64 encoded emails to be used as ids in recipients form for select2 component. | ||
* | ||
* @param array|string $ids | ||
* @return string; | ||
*/ | ||
public static function prepareFormRecipientIds($ids) | ||
{ | ||
if (is_string($ids)) { | ||
return base64_encode($ids); | ||
} | ||
|
||
$ids = array_map("base64_encode", $ids); | ||
|
||
return implode(self::EMAIL_IDS_SEPARATOR, $ids); | ||
} | ||
|
||
/** | ||
* Extracts base64 encoded selected email values, that are used as ids in recipients form for select2 component. | ||
* | ||
* @param array|string $value | ||
* @return array; | ||
*/ | ||
public static function extractFormRecipientIds($value) | ||
{ | ||
if (is_array($value)) { | ||
return $value; | ||
} | ||
/* | ||
* str_getcsv is used to cover the case if emails are pasted directly with ";" separator | ||
* and it protects from ";" used in full email address. (example: "Recipient Name; Name2" <[email protected]>) | ||
*/ | ||
$idsEncoded = str_getcsv($value, self::EMAIL_IDS_SEPARATOR); | ||
$idsDecoded = array_map(function ($idEncoded) { | ||
return base64_decode($idEncoded, true) ? : $idEncoded; | ||
}, $idsEncoded); | ||
|
||
return $idsDecoded; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,7 +114,7 @@ protected function setUp() | |
|
||
public function testPreciseFullEmailAddressIsFullQualifiedName() | ||
{ | ||
$emailAddress = 'Admin <[email protected]>'; | ||
$emailAddress = '"Admin" <[email protected]>'; | ||
|
||
$this->entityRoutingHelper->expects($this->never()) | ||
->method('getEntity'); | ||
|
@@ -131,7 +131,7 @@ public function testPreciseFullEmailAddressIsFullQualifiedName() | |
public function testPreciseFullEmailAddressViaRoutingHelper() | ||
{ | ||
$emailAddress = '[email protected]'; | ||
$expected = 'Admin <[email protected]>'; | ||
$expected = '"Admin" <[email protected]>'; | ||
|
||
$ownerClass = 'Oro\Bundle\UserBundle\Entity\User'; | ||
$ownerId = 1; | ||
|
@@ -183,7 +183,7 @@ public function testPreciseFullEmailAddressViaRoutingHelperWithExcludeCurrentUse | |
public function testPreciseFullEmailAddressViaAddressManager() | ||
{ | ||
$emailAddress = '[email protected]'; | ||
$expected = 'Admin <[email protected]>'; | ||
$expected = '"Admin" <[email protected]>'; | ||
|
||
$ownerClass = 'Oro\Bundle\UserBundle\Entity\User'; | ||
$ownerId = null; | ||
|
@@ -307,19 +307,19 @@ public function preciseFullEmailAddressProvider() | |
{ | ||
return [ | ||
[ | ||
'FirstName LastName <[email protected]>', | ||
'"FirstName LastName" <[email protected]>', | ||
'[email protected]', | ||
null, | ||
null | ||
], | ||
[ | ||
'FirstName LastName <[email protected]>', | ||
'"FirstName LastName" <[email protected]>', | ||
'[email protected]', | ||
'Oro\Bundle\EmailBundle\Tests\Unit\Fixtures\Entity\TestUser', | ||
null | ||
], | ||
[ | ||
'OwnerFirstName OwnerLastName <[email protected]>', | ||
'"OwnerFirstName OwnerLastName" <[email protected]>', | ||
'[email protected]', | ||
'Oro\Bundle\EmailBundle\Tests\Unit\Fixtures\Entity\TestUser', | ||
123 | ||
|
@@ -410,7 +410,7 @@ public function testBuildFullEmailAddress() | |
$user = $this->getMock('Oro\Bundle\UserBundle\Entity\User'); | ||
$email = 'email'; | ||
$format = 'format'; | ||
$expected = 'format <email>'; | ||
$expected = '"format" <email>'; | ||
|
||
$user->expects($this->once()) | ||
->method('getEmail') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,4 +192,56 @@ public function filterRecipientsDataProvider() | |
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider prepareFormRecipientIdsDataProvider | ||
*/ | ||
public function testPrepareFormRecipientIds($ids, $expectedResult) | ||
{ | ||
$this->assertEquals($expectedResult, EmailRecipientsHelper::prepareFormRecipientIds($ids)); | ||
} | ||
|
||
public function prepareFormRecipientIdsDataProvider() | ||
{ | ||
return [ | ||
[ | ||
[ | ||
'"Recipient1 Name; Name2" <[email protected]>', | ||
'"Recipient2 Name, Name2" <[email protected]>' | ||
], | ||
|
||
base64_encode('"Recipient1 Name; Name2" <[email protected]>') . ';' | ||
. base64_encode('"Recipient2 Name, Name2" <[email protected]>') | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider extractFormRecipientIdsDataProvider | ||
*/ | ||
public function testExtractFormRecipientIds($value, $expectedResult) | ||
{ | ||
$this->assertEquals($expectedResult, EmailRecipientsHelper::extractFormRecipientIds($value)); | ||
} | ||
|
||
public function extractFormRecipientIdsDataProvider() | ||
{ | ||
return [ | ||
[ | ||
base64_encode('"Recipient1 Name; Name2" <[email protected]>') . ';' | ||
. base64_encode('"Recipient2 Name, Name2" <[email protected]>'), | ||
[ | ||
'"Recipient1 Name; Name2" <[email protected]>', | ||
'"Recipient2 Name, Name2" <[email protected]>' | ||
] | ||
], | ||
[ | ||
'[email protected];[email protected]', | ||
[ | ||
'[email protected]', | ||
'[email protected]' | ||
] | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,8 +91,8 @@ public static function buildFullEmailAddressProvider() | |
['[email protected]', null, '[email protected]'], | ||
['[email protected]', '', '[email protected]'], | ||
['[email protected]', null, '[email protected]'], | ||
['[email protected]', 'John Smith', 'John Smith <[email protected]>'], | ||
[' [email protected] ', ' John Smith ', 'John Smith <[email protected]>'], | ||
['[email protected]', 'John Smith', '"John Smith" <[email protected]>'], | ||
[' [email protected] ', ' John Smith ', '"John Smith" <[email protected]>'], | ||
]; | ||
} | ||
|
||
|
@@ -126,7 +126,7 @@ public static function extractEmailAddressFirstNameProvider() | |
{ | ||
return [ | ||
['John Smith IV. <[email protected]>', 'John'], | ||
['John Smith <[email protected]>', 'John'], | ||
['"John Smith" <[email protected]>', 'John'], | ||
['John <[email protected]>', 'John'], | ||
['[email protected]', 'john'], | ||
['[email protected]', 'john'], | ||
|
@@ -137,7 +137,7 @@ public static function extractEmailAddressLastNameProvider() | |
{ | ||
return [ | ||
['John Smith IV. <[email protected]>', 'Smith IV.'], | ||
['John Smith <[email protected]>', 'Smith'], | ||
['"John Smith" <[email protected]>', 'Smith'], | ||
['John <[email protected]>', 'example.com'], | ||
['[email protected]', 'smith'], | ||
['[email protected]', 'example.com'], | ||
|
Oops, something went wrong.