<commit_before><?php

namespace Oro\Bundle\EmailBundle\Tests\Unit\Tools;

use Doctrine\Common\Collections\ArrayCollection;

use Oro\Bundle\EmailBundle\Entity\EmailFolder;
use Oro\Bundle\EmailBundle\Entity\InternalEmailOrigin;
use Oro\Bundle\EmailBundle\Model\FolderType;
use Oro\Bundle\EmailBundle\Tools\EmailAddressHelper;
use Oro\Bundle\EmailBundle\Tools\EmailOriginHelper;
use Oro\Bundle\OrganizationBundle\Entity\Organization;

class EmailOriginHelperTest extends \PHPUnit_Framework_TestCase
{
    /** @var EmailOriginHelper */
    protected $emailOriginHelper;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $em;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $doctrineHelper;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $emailModel;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $emailOwnerProvider;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $securityFacadeLink;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $securityFacade;

    /** @var  EmailAddressHelper|\PHPUnit_Framework_MockObject_MockObject */
    protected $emailAddressHelper;

    protected function setUp()
    {
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
            ->disableOriginalConstructor()
            ->getMock();

        $this->doctrineHelper = $this->getMockBuilder('Oro\Bundle\EntityBundle\ORM\DoctrineHelper')
            ->disableOriginalConstructor()
            ->getMock();

        $this->doctrineHelper->expects(self::any())
            ->method('getEntityManager')
            ->with('OroEmailBundle:Email')
            ->will(self::returnValue($this->em));

        $this->emailModel = $this->getMockBuilder('Oro\Bundle\EmailBundle\Form\Model\Email')
            ->setMethods(['getCampaignOwner'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->emailOwnerProvider = $this->getMockBuilder('Oro\Bundle\EmailBundle\Entity\Provider\EmailOwnerProvider')
            ->disableOriginalConstructor()
            ->getMock();

        $this->securityFacadeLink = $this
            ->getMockBuilder('Oro\Bundle\EntityConfigBundle\DependencyInjection\Utils\ServiceLink')
            ->setMethods(['getService'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->securityFacade = $this->getMockBuilder('Oro\Bundle\SecurityBundle\SecurityFacade')
            ->setMethods(['getLoggedUser', 'getOrganization'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->securityFacadeLink->expects($this->any())
            ->method('getService')
            ->will($this->returnValue($this->securityFacade));

        $this->securityFacade->expects($this->any())
            ->method('getOrganization')
            ->will($this->returnValue($this->getTestOrganization()));

        $this->emailAddressHelper = new EmailAddressHelper();

        $this->emailOriginHelper = new EmailOriginHelper(
            $this->doctrineHelper,
            $this->securityFacadeLink,
            $this->emailOwnerProvider,
            $this->emailAddressHelper
        );
    }

    public function testGetEmailOriginFromSecurity()
    {
        $email = 'test';
        $organization = null;
        $originName = InternalEmailOrigin::BAP;
        $enableUseUserEmailOrigin = true;
        $expectedOrigin = new \stdClass();
        $owner = new \stdClass();

        $this->emailOwnerProvider->expects($this->once())
            ->method('findEmailOwner')
            ->willReturn($owner);
        $entityRepository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->disableOriginalConstructor()
            ->getMock();
        $entityRepository->expects($this->once())
            ->method('findOneBy')
            ->willReturn($expectedOrigin);
        $this->em->expects($this->once())
            ->method('getRepository')
            ->willReturn($entityRepository);

        $origin =
            $this->emailOriginHelper->getEmailOrigin($email, $organization, $originName, $enableUseUserEmailOrigin);

        $this->assertEquals($expectedOrigin, $origin);
    }

    public function testGetEmailOriginCache()
    {
        $email = 'test';
        $organization = null;
        $originName = InternalEmailOrigin::BAP;
        $enableUseUserEmailOrigin = true;
        $expectedOrigin = new \stdClass();
        $owner = new \stdClass();

        $this->emailOwnerProvider->expects($this->exactly(2))
            ->method('findEmailOwner')
            ->willReturn($owner);
        $entityRepository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->disableOriginalConstructor()
            ->getMock();
        $entityRepository->expects($this->at(0))
            ->method('findOneBy')
            ->willReturn($expectedOrigin);
        $entityRepository->expects($this->at(1))
            ->method('findOneBy')
            ->willReturn(null);<commit_msg>BAP-13339: Impossible to send email via System Email Box<commit_after><?php

namespace Oro\Bundle\EmailBundle\Tests\Unit\Tools;

use Doctrine\Common\Collections\ArrayCollection;

use Oro\Bundle\EmailBundle\Entity\EmailFolder;
use Oro\Bundle\EmailBundle\Entity\InternalEmailOrigin;
use Oro\Bundle\EmailBundle\Model\FolderType;
use Oro\Bundle\EmailBundle\Tools\EmailAddressHelper;
use Oro\Bundle\EmailBundle\Tools\EmailOriginHelper;
use Oro\Bundle\OrganizationBundle\Entity\Organization;

class EmailOriginHelperTest extends \PHPUnit_Framework_TestCase
{
    /** @var EmailOriginHelper */
    protected $emailOriginHelper;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $em;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $doctrineHelper;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $emailModel;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $emailOwnerProvider;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $securityFacadeLink;

    /** @var \PHPUnit_Framework_MockObject_MockObject */
    protected $securityFacade;

    /** @var  EmailAddressHelper|\PHPUnit_Framework_MockObject_MockObject */
    protected $emailAddressHelper;

    protected function setUp()
    {
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
            ->disableOriginalConstructor()
            ->getMock();

        $this->doctrineHelper = $this->getMockBuilder('Oro\Bundle\EntityBundle\ORM\DoctrineHelper')
            ->disableOriginalConstructor()
            ->getMock();

        $this->doctrineHelper->expects(self::any())
            ->method('getEntityManager')
            ->with('OroEmailBundle:Email')
            ->will(self::returnValue($this->em));

        $this->emailModel = $this->getMockBuilder('Oro\Bundle\EmailBundle\Form\Model\Email')
            ->setMethods(['getCampaignOwner'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->emailOwnerProvider = $this->getMockBuilder('Oro\Bundle\EmailBundle\Entity\Provider\EmailOwnerProvider')
            ->disableOriginalConstructor()
            ->getMock();

        $this->securityFacadeLink = $this
            ->getMockBuilder('Oro\Bundle\EntityConfigBundle\DependencyInjection\Utils\ServiceLink')
            ->setMethods(['getService'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->securityFacade = $this->getMockBuilder('Oro\Bundle\SecurityBundle\SecurityFacade')
            ->setMethods(['getLoggedUser', 'getOrganization'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->securityFacadeLink->expects($this->any())
            ->method('getService')
            ->will($this->returnValue($this->securityFacade));

        $this->securityFacade->expects($this->any())
            ->method('getOrganization')
            ->will($this->returnValue($this->getTestOrganization()));

        $this->emailAddressHelper = new EmailAddressHelper();

        $this->emailOriginHelper = new EmailOriginHelper(
            $this->doctrineHelper,
            $this->securityFacadeLink,
            $this->emailOwnerProvider,
            $this->emailAddressHelper
        );
    }

    public function testGetEmailOriginFromSecurity()
    {
        $email = 'test';
        $organization = null;
        $originName = InternalEmailOrigin::BAP;
        $enableUseUserEmailOrigin = true;
        $expectedOrigin = new \stdClass();
        $owner = new \stdClass();

        $this->emailOwnerProvider->expects($this->once())
            ->method('findEmailOwners')
            ->willReturn($owner);
        $entityRepository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->disableOriginalConstructor()
            ->getMock();
        $entityRepository->expects($this->once())
            ->method('findOneBy')
            ->willReturn($expectedOrigin);
        $this->em->expects($this->once())
            ->method('getRepository')
            ->willReturn($entityRepository);

        $origin =
            $this->emailOriginHelper->getEmailOrigin($email, $organization, $originName, $enableUseUserEmailOrigin);

        $this->assertEquals($expectedOrigin, $origin);
    }

    public function testGetEmailOriginCache()
    {
        $email = 'test';
        $organization = null;
        $originName = InternalEmailOrigin::BAP;
        $enableUseUserEmailOrigin = true;
        $expectedOrigin = new \stdClass();
        $owner = new \stdClass();

        $this->emailOwnerProvider->expects($this->exactly(2))
            ->method('findEmailOwners')
            ->willReturn($owner);
        $entityRepository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->disableOriginalConstructor()
            ->getMock();
        $entityRepository->expects($this->at(0))
            ->method('findOneBy')
            ->willReturn($expectedOrigin);
        $entityRepository->expects($this->at(1))
            ->method('findOneBy')
            ->willReturn(null);
{'fraction_non_alphanumeric': 0.13584014174623488, 'fraction_numerical': 0.0010827837385569445, 'mean_word_length': 2.836858006042296, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 181, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6542117', 'n_tokens_mistral': 2698, 'n_tokens_neox': 2642, 'n_words': 351}