/**
  * @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());
 }
 /**
  * {@inheritdoc}
  */
 public function loadEmailBody(EmailFolder $folder, Email $email, EntityManager $em)
 {
     /** @var UserEmailOrigin $origin */
     $origin = $folder->getOrigin();
     $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()), $this->imapEmailGoogleOauth2Manager->getAccessTokenWithCheckingExpiration($origin));
     $manager = new ImapEmailManager($this->connectorFactory->createImapConnector($config));
     $manager->selectFolder($folder->getFullName());
     $repo = $em->getRepository('OroImapBundle:ImapEmail');
     $query = $repo->createQueryBuilder('e')->select('e.uid')->innerJoin('e.imapFolder', 'if')->where('e.email = ?1 AND if.folder = ?2')->setParameter(1, $email)->setParameter(2, $folder)->getQuery();
     $loadedEmail = $manager->findEmail($query->getSingleScalarResult());
     if (null === $loadedEmail) {
         throw new EmailBodyNotFoundException($email);
     }
     $builder = new EmailBodyBuilder();
     $builder->setEmailBody($loadedEmail->getBody()->getContent(), $loadedEmail->getBody()->getBodyIsText());
     foreach ($loadedEmail->getAttachments() as $attachment) {
         $builder->addEmailAttachment($attachment->getFileName(), $attachment->getContent(), $attachment->getContentType(), $attachment->getContentTransferEncoding(), $attachment->getContentId());
     }
     return $builder->getEmailBody();
 }
 /**
  * {@inheritdoc}
  */
 public function loadEmailBody(Email $email, EntityManager $em)
 {
     /** @var ImapEmailOrigin $origin */
     $origin = $email->getFolder()->getOrigin();
     $config = new ImapConfig($origin->getHost(), $origin->getPort(), $origin->getSsl(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()));
     $manager = new ImapEmailManager($this->connectorFactory->createImapConnector($config));
     $manager->selectFolder($email->getFolder()->getFullName());
     $repo = $em->getRepository('OroImapBundle:ImapEmail');
     $query = $repo->createQueryBuilder('e')->select('e.uid')->where('e.email = ?1')->setParameter(1, $email)->getQuery();
     /** @var ImapEmail $imapEmail */
     $imapEmail = $query->getSingleResult();
     $loadedEmail = $manager->findEmail($imapEmail['uid']);
     if ($loadedEmail === null) {
         throw new \RuntimeException(sprintf('Cannot find a body for "%s" email.', $email->getSubject()));
     }
     $builder = new EmailBodyBuilder();
     $builder->setEmailBody($loadedEmail->getBody()->getContent(), $loadedEmail->getBody()->getBodyIsText());
     foreach ($loadedEmail->getAttachments() as $attachment) {
         $builder->addEmailAttachment($attachment->getFileName(), $attachment->getContent(), $attachment->getContentType(), $attachment->getContentTransferEncoding());
     }
     return $builder->getEmailBody();
 }