<commit_before><?php

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

use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\EmailBundle\Builder\EmailBodyBuilder;

class EmailBodyBuilderTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var EmailBodyBuilder
     */
    protected $emailBodyBuilder;

    /**
     * @var ConfigManager|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $configManager;

    protected function setUp()
    {
        $this->configManager = $this->getMockBuilder('Oro\Bundle\ConfigBundle\Config\ConfigManager')
            ->disableOriginalConstructor()
            ->getMock();

        $this->emailBodyBuilder = new EmailBodyBuilder($this->configManager);
    }

    public function testCreateEmailBody()
    {
        $this->emailBodyBuilder->setEmailBody('test', true);
        $body = $this->emailBodyBuilder->getEmailBody();
        $this->assertEquals(true, $body->getBodyIsText());
        $this->assertEquals('test', $body->getBodyContent());
    }

    /**
     * @expectedException \LogicException
     */
    public function testGetEmptyEmailBody()
    {
        $this->emailBodyBuilder->getEmailBody();
    }

    /**
     * @expectedException \LogicException
     */
    public function testAddEmailAttachmentWithoutBody()
    {
        $this->emailBodyBuilder->addEmailAttachment(
            'test',
            'content',
            'ct',
            '',
            1,
            1
        );
    }

    /**
     * @param bool $expected
     * @param [] $data
     * @param bool $configSyncEnabled
     * @param int $configSyncMaxSize
     *
     * @dataProvider addAttachmentProvider
     */
    public function testAddAttachment($expected, $data, $configSyncEnabled, $configSyncMaxSize)
    {
        $this->emailBodyBuilder->setEmailBody('test', true);

        $this->configManager->expects($this->at(0))
            ->method('get')
            ->with('oro_email.attachment_sync_enable')
            ->willReturn($configSyncEnabled);
        if ($configSyncEnabled) {
            $this->configManager->expects($this->at(1))
                ->method('get')
                ->with('oro_email.attachment_sync_max_size')
                ->willReturn($configSyncMaxSize);
        }

        $this->emailBodyBuilder->addEmailAttachment(
            'test',
            'content',
            'ct',
            $data['contentTransferEncoding'],
            $data['embeddedContentId'],
            $data['contentSize']
        );

        $body = $this->emailBodyBuilder->getEmailBody();
        $this->assertEquals($expected, $body->getHasAttachments());
    }

    public function addAttachmentProvider()<commit_msg>BAP-9647: Add limit for attachment size withing IMAP/EWS emails<commit_after><?php

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

use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\EmailBundle\Builder\EmailBodyBuilder;

class EmailBodyBuilderTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var EmailBodyBuilder
     */
    protected $emailBodyBuilder;

    /**
     * @var ConfigManager|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $configManager;

    protected function setUp()
    {
        $this->configManager = $this->getMockBuilder('Oro\Bundle\ConfigBundle\Config\ConfigManager')
            ->disableOriginalConstructor()
            ->getMock();

        $this->emailBodyBuilder = new EmailBodyBuilder($this->configManager);
    }

    public function testCreateEmailBody()
    {
        $this->emailBodyBuilder->setEmailBody('test', true);
        $body = $this->emailBodyBuilder->getEmailBody();
        $this->assertEquals(true, $body->getBodyIsText());
        $this->assertEquals('test', $body->getBodyContent());
    }

    /**
     * @expectedException \LogicException
     */
    public function testGetEmptyEmailBody()
    {
        $this->emailBodyBuilder->getEmailBody();
    }

    /**
     * @expectedException \LogicException
     */
    public function testAddEmailAttachmentWithoutBody()
    {
        $this->emailBodyBuilder->addEmailAttachment(
            'test',
            'content',
            'ct',
            '',
            1,
            1
        );
    }

    /**
     * @param bool $expected
     * @param [] $data
     * @param bool $configSyncEnabled
     * @param int $configSyncMaxSize
     *
     * @dataProvider addAttachmentProvider
     */
    public function testAddAttachment($expected, $data, $configSyncEnabled, $configSyncMaxSize)
    {
        $this->emailBodyBuilder->setEmailBody('test', true);

        $this->configManager->expects($this->at(0))
            ->method('get')
            ->with(EmailBodyBuilder::ORO_EMAIL_ATTACHMENT_SYNC_ENABLE)
            ->willReturn($configSyncEnabled);
        if ($configSyncEnabled) {
            $this->configManager->expects($this->at(1))
                ->method('get')
                ->with(EmailBodyBuilder::ORO_EMAIL_ATTACHMENT_SYNC_MAX_SIZE)
                ->willReturn($configSyncMaxSize);
        }

        $this->emailBodyBuilder->addEmailAttachment(
            'test',
            'content',
            'ct',
            $data['contentTransferEncoding'],
            $data['embeddedContentId'],
            $data['contentSize']
        );

        $body = $this->emailBodyBuilder->getEmailBody();
        $this->assertEquals($expected, $body->getHasAttachments());
    }

    public function addAttachmentProvider()
{'fraction_non_alphanumeric': 0.12974452554744526, 'fraction_numerical': 0.0021897810218978104, 'mean_word_length': 2.432060112711334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, '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': '2893928', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1482, 'n_words': 211}