/**
  * @dataProvider findEmailOriginDataProvider
  *
  * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $mailModelExpects
  * @param string                                             $emailOwner
  * @param bool                                               $enableUseUserEmailOrigin
  * @param bool                                               $isOriginsNotEmpty
  * @param \PHPUnit_Framework_MockObject_MockObject           $origin
  * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $emailOriginsTimes
  */
 public function testFindEmailOrigin($mailModelExpects, $emailOwner, $enableUseUserEmailOrigin, $isOriginsNotEmpty, \PHPUnit_Framework_MockObject_MockObject $origin, $emailOriginsTimes)
 {
     $organization = $this->getMock('Oro\\Bundle\\OrganizationBundle\\Entity\\OrganizationInterface');
     $collection = new ArrayCollection([$origin]);
     $originName = 'origin name';
     $campaignOwner = null;
     $origin->expects(self::once())->method('getOrganization')->willReturn($organization);
     if (true === $enableUseUserEmailOrigin && true === $isOriginsNotEmpty) {
         $origin->expects(self::once())->method('isActive')->willReturn($isOriginsNotEmpty);
         $origin->expects(self::once())->method('isSmtpConfigured')->willReturn($isOriginsNotEmpty);
     }
     if ('user' === $emailOwner) {
         $emailOwner = $this->getMockBuilder('Oro\\Bundle\\UserBundle\\Entity\\User')->setMethods(['getEmailOrigins', 'addEmailOrigin'])->disableOriginalConstructor()->getMock();
         $emailOwner->expects($emailOriginsTimes)->method('getEmailOrigins')->willReturn($collection);
         if (false === $enableUseUserEmailOrigin) {
             $emailOwner->expects(self::once())->method('addEmailOrigin');
         }
     } elseif ('emailBox' === $emailOwner) {
         $emailOwner = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\Mailbox')->setMethods(['getOrigin'])->disableOriginalConstructor()->getMock();
         if (true === $isOriginsNotEmpty) {
             $emailOwner->expects($emailOriginsTimes)->method('getOrigin')->willReturn($collection);
         } else {
             $emailOwner->expects($emailOriginsTimes)->method('getOrigin')->willReturn(null);
             $campaignOwner = $this->getMockBuilder('Oro\\Bundle\\UserBundle\\Entity\\User')->setMethods(['getEmailOrigins', 'addEmailOrigin'])->disableOriginalConstructor()->getMock();
             $campaignOwner->expects(self::any())->method('getEmailOrigins')->willReturn($collection);
         }
     }
     $this->emailModel->expects($mailModelExpects)->method('getCampaignOwner')->willReturn($campaignOwner);
     $this->emailOriginHelper->setEmailModel($this->emailModel);
     $origin = $this->emailOriginHelper->findEmailOrigin($emailOwner, $organization, $originName, $enableUseUserEmailOrigin);
     self::assertNotNull($origin);
 }
Exemple #2
0
 /**
  * Find existing email origin entity by email string or create and persist new one.
  *
  * @param string                $email
  * @param OrganizationInterface $organization
  * @param string                $originName
  * @param boolean               $enableUseUserEmailOrigin
  *
  * @return EmailOrigin
  */
 public function getEmailOrigin($email, OrganizationInterface $organization = null, $originName = InternalEmailOrigin::BAP, $enableUseUserEmailOrigin = true)
 {
     $originKey = $originName . $email;
     if (!$organization && $this->securityFacade !== null && $this->securityFacade->getOrganization()) {
         $organization = $this->securityFacade->getOrganization();
     }
     if (!array_key_exists($originKey, $this->origins)) {
         $emailOwner = $this->emailOwnerProvider->findEmailOwner($this->getEntityManager(), $this->emailAddressHelper->extractPureEmailAddress($email));
         $origin = $this->emailOriginHelper->findEmailOrigin($emailOwner, $organization, $originName, $enableUseUserEmailOrigin);
         $this->origins[$originKey] = $origin;
     }
     return $this->origins[$originKey];
 }
Exemple #3
0
 /**
  * @dataProvider messageDataProvider
  * @param array $data
  * @param array $expectedMessageData
  * @param bool  $needConverting
  *
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testProcess($data, $expectedMessageData, $needConverting = false)
 {
     $message = new \Swift_Message();
     $this->emailOriginHelper->expects(self::once())->method('setEmailModel');
     $this->emailOriginHelper->expects(self::once())->method('findEmailOrigin')->will(self::returnValue($this->userEmailOrigin));
     $this->userEmailOrigin->expects($this->once())->method('isSmtpConfigured')->will($this->returnValue(true));
     $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($message));
     $this->mailer->expects($this->once())->method('send')->with($message)->will($this->returnValue(true));
     $emailUser = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\EmailUser')->setMethods(['addFolder', 'getEmail'])->getMock();
     $emailUser->expects($this->once())->method('addFolder');
     $email = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\Email')->disableOriginalConstructor()->getMock();
     $emailUser->expects($this->any())->method('getEmail')->willReturn($email);
     $this->emailEntityBuilder->expects($this->once())->method('emailUser')->with($data['subject'], $data['from'], $data['to'], $this->isInstanceOf('DateTime'), $this->isInstanceOf('DateTime'), $this->isInstanceOf('DateTime'), \Oro\Bundle\EmailBundle\Entity\Email::NORMAL_IMPORTANCE, $data['cc'], $data['bcc'])->willReturn($emailUser);
     $body = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\EmailBody')->disableOriginalConstructor()->getMock();
     $this->emailEntityBuilder->expects($this->once())->method('body')->will($this->returnValue($body));
     $batch = $this->getMock('Oro\\Bundle\\EmailBundle\\Builder\\EmailEntityBatchInterface');
     $this->emailEntityBuilder->expects($this->once())->method('getBatch')->will($this->returnValue($batch));
     $batch->expects($this->once())->method('persist')->with($this->identicalTo($this->em));
     $this->em->expects($this->once())->method('flush');
     $email->expects($this->any())->method('getEmailBody')->willReturn($body);
     if (!empty($data['entityClass']) && !empty($data['entityClass'])) {
         $targetEntity = new TestUser();
         $this->doctrineHelper->expects($this->exactly(0))->method('getEntity')->with($data['entityClass'], $data['entityId'])->will($this->returnValue($targetEntity));
         $this->emailActivityManager->expects($this->exactly(0))->method('addAssociation')->with($this->identicalTo($email), $this->identicalTo($targetEntity));
     }
     $model = $this->createEmailModel($data);
     $this->assertSame($emailUser, $this->emailProcessor->process($model));
     $this->assertEquals($expectedMessageData['from'], [$model->getFrom()]);
     $this->assertEquals($data['cc'], $model->getCc());
     $this->assertEquals($data['bcc'], $model->getBcc());
     $this->assertEquals($expectedMessageData['subject'], $model->getSubject());
     if ($needConverting) {
         $id = $model->getAttachments()->first()->getEmailAttachment()->getEmbeddedContentId();
         $this->assertEquals(sprintf($expectedMessageData['body'], 'cid:' . $id), $message->getBody());
     } else {
         $this->assertEquals($expectedMessageData['body'], $model->getBody());
     }
 }