public function testGetActivityOwners()
 {
     $organization = new Organization();
     $organization->setName('Org');
     $user = new User();
     $user->setUsername('test');
     $emailUser = new EmailUser();
     $emailUser->setOrganization($organization);
     $emailUser->setOwner($user);
     $owners = [$emailUser];
     $emailMock = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\EmailUser')->setMethods(['getFromEmailAddress', 'hasOwner', 'getOwner', 'getOrganization', 'getActivityTargetEntities'])->disableOriginalConstructor()->getMock();
     $emailMock->expects($this->once())->method('getFromEmailAddress')->willReturn($emailMock);
     $emailMock->expects($this->once())->method('getOwner')->willReturn($emailMock);
     $emailMock->expects($this->exactly(2))->method('getOrganization')->willReturn($organization);
     $emailMock->expects($this->exactly(1))->method('getActivityTargetEntities')->willReturn([]);
     $activityListMock = $this->getMockBuilder('Oro\\Bundle\\ActivityListBundle\\Entity\\ActivityList')->disableOriginalConstructor()->getMock();
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $repository = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $this->doctrineRegistryLink->expects($this->once())->method('getService')->willReturn($em);
     $em->expects($this->once())->method('getRepository')->willReturn($repository);
     $repository->expects($this->once())->method('findBy')->willReturn($owners);
     $activityOwnerArray = $this->emailActivityListProvider->getActivityOwners($emailMock, $activityListMock);
     $this->assertCount(1, $activityOwnerArray);
     $owner = $activityOwnerArray[0];
     $this->assertEquals($organization->getName(), $owner->getOrganization()->getName());
     $this->assertEquals($user->getUsername(), $owner->getUser()->getUsername());
 }
Example #2
0
 /**
  * Set email as seen
  *
  * @param EmailUser $entity
  */
 public function setEmailUserSeen(EmailUser $entity)
 {
     if (!$entity->isSeen()) {
         $entity->setSeen(true);
         $this->em->flush();
     }
 }
Example #3
0
 /**
  * Toggle EmailUser thread seen
  *
  * @param EmailUser $entity
  */
 public function toggleEmailUserSeen(EmailUser $entity)
 {
     $seen = !$entity->isSeen();
     $this->setEmailUserSeen($entity, $seen);
     $this->em->persist($entity);
     if ($entity->getEmail()->getThread() && $entity->getOwner()) {
         $threadedEmailUserBuilder = $this->em->getRepository('OroEmailBundle:EmailUser')->getEmailUserByThreadId([$entity->getEmail()->getThread()->getId()], $entity->getOwner());
         $threadedEmailUserList = $threadedEmailUserBuilder->getQuery()->getResult();
         foreach ($threadedEmailUserList as $threadedEmailUser) {
             $this->setEmailUserSeen($threadedEmailUser, $seen);
             $this->em->persist($threadedEmailUser);
         }
     }
     $this->em->flush();
 }
Example #4
0
 /**
  * @param EmailUser     $entity
  * @param EntityManager $em
  *
  * @return array
  */
 protected function determineOwners(EmailUser $entity, EntityManager $em)
 {
     $ownerIds = [];
     if ($entity->getOwner() !== null) {
         $ownerIds[] = $entity->getOwner()->getId();
     } else {
         $mailbox = $entity->getMailboxOwner();
         if ($mailbox !== null) {
             $authorizedUsers = $mailbox->getAuthorizedUsers();
             foreach ($authorizedUsers as $user) {
                 $ownerIds[] = $user->getId();
             }
             $authorizedRoles = $mailbox->getAuthorizedRoles();
             foreach ($authorizedRoles as $role) {
                 $users = $em->getRepository('OroUserBundle:Role')->getUserQueryBuilder($role)->getQuery()->getResult();
                 foreach ($users as $user) {
                     $ownerIds[] = $user->getId();
                 }
             }
         }
     }
     return array_unique($ownerIds);
 }
 public function testFlush()
 {
     $changesetAnswer = ['seen' => true];
     $user1 = new User();
     $user1->setId(1);
     $user2 = new User();
     $user2->setId(2);
     $emailUser1 = new EmailUser();
     $emailUser1->setOwner($user1);
     $emailUser2 = new EmailUser();
     $emailUser2->setOwner($user2);
     $emailUserArray = [$emailUser1, $emailUser2, $emailUser1];
     $onFlushEventArgs = $this->getMockBuilder('Doctrine\\ORM\\Event\\OnFlushEventArgs')->setMethods(['getEntityManager', 'getUnitOfWork', 'getScheduledEntityInsertions', 'getScheduledEntityUpdates', 'getEntityChangeSet'])->disableOriginalConstructor()->getMock();
     $onFlushEventArgs->expects($this->once())->method('getEntityManager')->will($this->returnValue($onFlushEventArgs));
     $onFlushEventArgs->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($onFlushEventArgs));
     $onFlushEventArgs->expects($this->exactly(3))->method('getEntityChangeSet')->will($this->returnValue($changesetAnswer));
     $onFlushEventArgs->expects($this->once())->method('getScheduledEntityInsertions')->will($this->returnValue($emailUserArray));
     $onFlushEventArgs->expects($this->once())->method('getScheduledEntityUpdates')->will($this->returnValue($emailUserArray));
     $this->processor->expects($this->exactly(1))->method('send')->with([$user1->getId() => ['entity' => $emailUser1, 'new' => 2], $user2->getId() => ['entity' => $emailUser2, 'new' => 1]]);
     $postFlushEventArgs = $this->getMockBuilder('Doctrine\\ORM\\Event\\PostFlushEventArgs')->disableOriginalConstructor()->getMock();
     $this->listener->onFlush($onFlushEventArgs);
     $this->listener->postFlush($postFlushEventArgs);
 }
 /**
  * Create EmailUser entity object
  *
  * @param string               $subject             The email subject
  * @param string $from                              The FROM email address,
  *                                                  for example: john@example.com or "John Smith" <john@example.c4m>
  * @param string|string[]|null $to                  The TO email address(es).
  *                                                  Example of email address see in description of $from parameter
  * @param \DateTime            $sentAt              The date/time when email sent
  * @param \DateTime            $receivedAt          The date/time when email received
  * @param \DateTime            $internalDate        The date/time an email server returned in INTERNALDATE field
  * @param integer $importance                       The email importance flag.
  *                                                  Can be one of *_IMPORTANCE constants of Email class
  * @param string|string[]|null $cc                  The CC email address(es).
  *                                                  Example of email address see in description of $from parameter
  * @param string|string[]|null $bcc                 The BCC email address(es).
  *                                                  Example of email address see in description of $from parameter
  * @param User|null $owner                          Owner of the email
  * @param OrganizationInterface|null $organization
  *
  * @return EmailUser
  *
  * @SuppressWarnings(ExcessiveParameterList)
  */
 public function emailUser($subject, $from, $to, $sentAt, $receivedAt, $internalDate, $importance = Email::NORMAL_IMPORTANCE, $cc = null, $bcc = null, $owner = null, $organization = null)
 {
     $emailUser = new EmailUser();
     $email = $this->email($subject, $from, $to, $sentAt, $internalDate, $importance, $cc, $bcc);
     $emailUser->setReceivedAt($receivedAt);
     $emailUser->setEmail($email);
     if ($owner !== null) {
         $emailUser->setOwner($owner);
     }
     if ($organization !== null) {
         $emailUser->setOrganization($organization);
     } elseif ($owner !== null) {
         $emailUser->setOrganization($owner->getOrganization());
     }
     $this->batch->addEmailUser($emailUser);
     return $emailUser;
 }
Example #7
0
 /**
  * @dataProvider incomingAndOutgoingProvider
  */
 public function testIsIncomingAndOutgoing(EmailUser $emailUser)
 {
     $this->assertTrue($emailUser->isIncoming());
     $this->assertTrue($emailUser->isOutgoing());
 }
 public function testSync()
 {
     $email = new Email();
     $email->setSubject('Test email');
     $emailBody = new EmailBody();
     $emailUser = new EmailUser();
     $origin = new TestEmailOrigin();
     $folder = new EmailFolder();
     $folder->setOrigin($origin);
     $emailUser->setOrigin($origin);
     $emailUser->addFolder($folder);
     $email->addEmailUser($emailUser);
     $repo = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\Repository\\EmailRepository')->disableOriginalConstructor()->getMock();
     $this->doctrine->expects($this->once())->method('getRepository')->willReturn($repo);
     $runCount = 0;
     $repo->expects($this->exactly(2))->method('getEmailsWithoutBody')->willReturnCallback(function () use(&$runCount, $email) {
         $runCount++;
         return $runCount === 1 ? [$email] : [];
     });
     $loader = $this->getMock('Oro\\Bundle\\EmailBundle\\Provider\\EmailBodyLoaderInterface');
     $this->selector->expects($this->once())->method('select')->with($this->identicalTo($origin))->will($this->returnValue($loader));
     $loader->expects($this->once())->method('loadEmailBody')->with($this->identicalTo($folder), $this->identicalTo($email), $this->identicalTo($this->em))->will($this->returnValue($emailBody));
     $this->em->expects($this->once())->method('persist')->with($this->identicalTo($email));
     $this->em->expects($this->once())->method('flush');
     $this->em->expects($this->once())->method('clear');
     $this->logger->expects($this->exactly(2))->method('notice');
     $this->logger->expects($this->never())->method('warning');
     $this->synchronizer->sync();
     $this->assertSame($emailBody, $email->getEmailBody());
 }
Example #9
0
 public function testBeforeSave()
 {
     $emailUser = new EmailUser();
     $emailUser->beforeSave();
     $this->assertInstanceOf('\\DateTime', $emailUser->getCreatedAt());
 }
Example #10
0
 /**
  * Select email flag manager by entity EmailUser
  *
  * @param EmailUser $emailUser - EmailUser
  *
  * @return EmailFlagManagerInterface
  */
 protected function selectEmailFlagManager(EmailUser $emailUser)
 {
     $folder = $emailUser->getFolders()->first();
     $origin = $emailUser->getOrigin();
     if (!$origin || !$origin->isActive()) {
         return null;
     }
     $emailFlagManagerLoader = $this->selectorEmailFlagManager->select($origin);
     return $emailFlagManagerLoader->select($folder, $this->em);
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testPersist()
 {
     $origin = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\EmailOrigin')->getMock();
     $origin->expects($this->any())->method('getId')->will($this->returnValue(1));
     $folder = new EmailFolder();
     $folder->setName('Exist');
     $folder->setFullName('Exist');
     $folder->setOrigin($origin);
     $this->batch->addFolder($folder);
     $newFolder = new EmailFolder();
     $newFolder->setName('New');
     $newFolder->setFullName('New');
     $newFolder->setOrigin($origin);
     $this->batch->addFolder($newFolder);
     $dbFolder = new EmailFolder();
     $dbFolder->setName('DbExist');
     $dbFolder->setFullName('DbExist');
     $dbFolder->setOrigin($origin);
     $addr = $this->addrManager->newEmailAddress()->setEmail('Exist');
     $this->batch->addAddress($addr);
     $newAddr = $this->addrManager->newEmailAddress()->setEmail('New');
     $this->batch->addAddress($newAddr);
     $dbAddr = $this->addrManager->newEmailAddress()->setEmail('DbExist');
     $email1 = new Email();
     $email1->setXMessageId('email1');
     $email1->setMessageId('email1');
     $email1->setFromEmailAddress($addr);
     $emailUser1 = new EmailUser();
     $emailUser1->addFolder($folder);
     $emailUser1->setOrigin($origin);
     $email1->addEmailUser($emailUser1);
     $this->addEmailRecipient($email1, $addr);
     $this->addEmailRecipient($email1, $newAddr);
     $this->batch->addEmailUser($emailUser1);
     $email2 = new Email();
     $email2->setXMessageId('email2');
     $email2->setMessageId('email2');
     $email2->setFromEmailAddress($newAddr);
     $emailUser2 = new EmailUser();
     $emailUser2->addFolder($newFolder);
     $emailUser2->setOrigin($origin);
     $email2->addEmailUser($emailUser2);
     $this->addEmailRecipient($email2, $addr);
     $this->addEmailRecipient($email2, $newAddr);
     $this->batch->addEmailUser($emailUser2);
     $email3 = new Email();
     $email3->setXMessageId('email3');
     $email3->setMessageId('some_email');
     $email3->setFromEmailAddress($newAddr);
     $emailUser3 = new EmailUser();
     $emailUser3->addFolder($folder);
     $email3->addEmailUser($emailUser3);
     $this->addEmailRecipient($email3, $addr);
     $this->addEmailRecipient($email3, $newAddr);
     $this->batch->addEmailUser($emailUser3);
     $email4 = new Email();
     $email4->setXMessageId('email4');
     $email4->setMessageId('some_email');
     $email4->setFromEmailAddress($newAddr);
     $emailUser4 = new EmailUser();
     $emailUser4->addFolder($folder);
     $email4->addEmailUser($emailUser4);
     $this->addEmailRecipient($email4, $addr);
     $this->addEmailRecipient($email4, $newAddr);
     $this->batch->addEmailUser($emailUser4);
     $existingEmail = new Email();
     $existingEmail->setXMessageId('existing_email');
     $existingEmail->setMessageId('some_email');
     $existingEmail->setFromEmailAddress($newAddr);
     $emailUser5 = new EmailUser();
     $emailUser5->addFolder($dbFolder);
     $existingEmail->addEmailUser($emailUser5);
     $this->addEmailRecipient($existingEmail, $addr);
     $this->addEmailRecipient($existingEmail, $newAddr);
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $folderRepo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $addrRepo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $emailRepo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $em->expects($this->exactly(3))->method('getRepository')->will($this->returnValueMap(array(array('OroEmailBundle:EmailFolder', $folderRepo), array('Oro\\Bundle\\EmailBundle\\Tests\\Unit\\Entity\\TestFixtures\\TestEmailAddressProxy', $addrRepo), array('OroEmailBundle:Email', $emailRepo))));
     $folderRepo->expects($this->exactly(2))->method('findOneBy')->will($this->returnCallback(function ($c) use(&$dbFolder) {
         return $c['fullName'] === 'Exist' ? $dbFolder : null;
     }));
     $addrRepo->expects($this->exactly(2))->method('findOneBy')->will($this->returnCallback(function ($c) use(&$dbAddr) {
         return $c['email'] === 'Exist' ? $dbAddr : null;
     }));
     $emailRepo->expects($this->once())->method('findBy')->with(array('messageId' => array('email1', 'email2', 'some_email')))->will($this->returnValue([$existingEmail]));
     $owner = $this->getMock('Oro\\Bundle\\EmailBundle\\Entity\\EmailOwnerInterface');
     $this->ownerProvider->expects($this->any())->method('findEmailOwner')->will($this->returnValue($owner));
     $this->batch->persist($em);
     $this->assertCount(1, $email1->getEmailUsers());
     $this->assertCount(1, $email2->getEmailUsers());
     $this->assertTrue($origin === $emailUser1->getOrigin());
     $this->assertTrue($origin === $emailUser2->getOrigin());
     $this->assertSame($newFolder, $emailUser2->getFolders()->first());
     $this->assertSame($dbFolder, $emailUser1->getFolders()->first());
     $this->assertTrue($dbAddr === $email1->getFromEmailAddress());
     $this->assertNull($email1->getFromEmailAddress()->getOwner());
     $this->assertTrue($newAddr === $email2->getFromEmailAddress());
     $this->assertTrue($owner === $email2->getFromEmailAddress()->getOwner());
     $email1Recipients = $email1->getRecipients();
     $this->assertTrue($dbAddr === $email1Recipients[0]->getEmailAddress());
     $this->assertNull($email1Recipients[0]->getEmailAddress()->getOwner());
     $this->assertTrue($newAddr === $email1Recipients[1]->getEmailAddress());
     $this->assertTrue($owner === $email1Recipients[1]->getEmailAddress()->getOwner());
     $email2Recipients = $email2->getRecipients();
     $this->assertTrue($dbAddr === $email2Recipients[0]->getEmailAddress());
     $this->assertNull($email2Recipients[0]->getEmailAddress()->getOwner());
     $this->assertTrue($newAddr === $email2Recipients[1]->getEmailAddress());
     $this->assertTrue($owner === $email2Recipients[1]->getEmailAddress()->getOwner());
     $changes = $this->batch->getChanges();
     $this->assertCount(3, $changes);
     $this->assertSame($folder, $changes[0]['old']);
     $this->assertSame($dbFolder, $changes[0]['new']);
     $this->assertSame($email3, $changes[1]['old']);
     $this->assertSame($existingEmail, $changes[1]['new']);
     $this->assertSame($email4, $changes[2]['old']);
     $this->assertSame($existingEmail, $changes[2]['new']);
 }
 /**
  * @expectedException \Oro\Bundle\EmailBundle\Exception\EmailBodyNotFoundException
  * @expectedExceptionMessage Cannot find a body for "test email" email.
  */
 public function testEnsureEmailBodyCachedNotFound()
 {
     $email = new Email();
     ReflectionUtil::setId($email, 123);
     $email->setSubject('test email');
     $emailBody = new EmailBody();
     $emailUser = new EmailUser();
     $origin = new TestEmailOrigin();
     $folder = new EmailFolder();
     $folder->setOrigin($origin);
     $emailUser->setFolder($folder);
     $email->addEmailUser($emailUser);
     $exception = new EmailBodyNotFoundException($email);
     $loader = $this->getMock('Oro\\Bundle\\EmailBundle\\Provider\\EmailBodyLoaderInterface');
     $this->selector->expects($this->once())->method('select')->with($this->identicalTo($origin))->will($this->returnValue($loader));
     $loader->expects($this->once())->method('loadEmailBody')->will($this->throwException($exception));
     $this->em->expects($this->never())->method('persist');
     $this->em->expects($this->never())->method('flush');
     $this->logger->expects($this->once())->method('notice')->with('Load email body failed. Email id: 123. Error: Cannot find a body for "test email" email.', ['exception' => $exception]);
     $this->logger->expects($this->never())->method('warning');
     $this->manager->ensureEmailBodyCached($email);
     $this->assertSame($emailBody, $email->getEmailBody());
 }
Example #13
0
 /**
  * @param Email $email
  * @param array $folders
  *
  * @return EmailUser[]
  */
 protected function processFolders(Email $email, $folders)
 {
     $apiOrigin = $this->getEmailOrigin();
     $emailUserList = [];
     foreach ($folders as $item) {
         $origin = $item['origin'] ?: $this->getEmailOrigin();
         if ($origin->getId() && $origin->getId() !== $apiOrigin->getId()) {
             continue;
         }
         $folder = $origin->getFolder($item['type'], $item['fullName']);
         if (!$folder) {
             $folder = $this->emailEntityBuilder->folder($item['type'], $item['fullName'], $item['name']);
             $origin->addFolder($folder);
         } else {
             $this->emailEntityBuilder->setFolder($folder);
         }
         $emailUser = new EmailUser();
         $emailUser->setEmail($email);
         $emailUser->setOwner($apiOrigin->getOwner());
         $emailUser->setOrganization($apiOrigin->getOrganization());
         $emailUser->setFolder($folder);
         $emailUserList[] = $emailUser;
     }
     return $emailUserList;
 }