/**
  * @param EmailBodyAdded $event
  */
 public function linkToScope(EmailBodyAdded $event)
 {
     if ($this->securityFacade->getToken() !== null && !$this->securityFacade->isGranted('CREATE', 'entity:' . AttachmentScope::ATTACHMENT)) {
         return;
     }
     $email = $event->getEmail();
     $entities = $this->activityListProvider->getTargetEntities($email);
     foreach ($entities as $entity) {
         if ((bool) $this->configProvider->getConfig(ClassUtils::getClass($entity))->get('auto_link_attachments')) {
             foreach ($email->getEmailBody()->getAttachments() as $attachment) {
                 $this->attachmentManager->linkEmailAttachmentToTargetEntity($attachment, $entity);
             }
         }
     }
 }
Exemple #2
0
 /**
  * Check possibility of reattach
  *
  * @param EmailAttachment $emailAttachment
  * @param object $targetEntity
  * @param string $targetClass
  *
  * @return bool
  */
 public function canReAttach($emailAttachment, $targetEntity)
 {
     if ($this->emailAttachmentManager->validateEmailAttachmentForTargetClass($emailAttachment, ClassUtils::getRealClass($targetEntity))->count() > 0 || $this->emailAttachmentManager->isAttached($emailAttachment, $targetEntity)) {
         return false;
     } else {
         return true;
     }
 }
 /**
  * @dataProvider getTestData
  */
 public function testLinkToScope($config, $managerCalls, $attachmentCalls)
 {
     $attachments = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\EmailAttachment')->disableOriginalConstructor()->getMock();
     $emailBody = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\EmailBody')->disableOriginalConstructor()->getMock();
     $email = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\Email')->disableOriginalConstructor()->getMock();
     $event = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Event\\EmailBodyAdded')->disableOriginalConstructor()->getMock();
     $configInterface = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface')->disableOriginalConstructor()->getMock();
     $this->securityFacade->expects($this->once())->method('getToken')->willReturn(1);
     $this->securityFacade->expects($this->once())->method('isGranted')->willReturn(true);
     $this->activityListProvider->expects($this->once())->method('getTargetEntities')->willReturn([new SomeEntity()]);
     $configInterface->expects($this->once())->method('get')->will($this->returnValue($config));
     $this->configProvider->expects($this->once())->method('getConfig')->will($this->returnValue($configInterface));
     $this->emailAttachmentManager->expects($this->exactly($managerCalls))->method('linkEmailAttachmentToTargetEntity');
     $emailBody->expects($this->exactly($attachmentCalls))->method('getAttachments')->will($this->returnValue([$attachments]));
     $email->expects($this->exactly($attachmentCalls))->method('getEmailBody')->will($this->returnValue($emailBody));
     $event->expects($this->exactly(1))->method('getEmail')->will($this->returnValue($email));
     $this->listener->linkToScope($event);
 }
 public function testLinkEmailAttachmentToTargetEntityNotValid()
 {
     $file = $this->getMockBuilder('Oro\\Bundle\\AttachmentBundle\\Entity\\File')->setMethods(['getFilename'])->getMock();
     $countable = $this->getMockBuilder('Countable')->getMock();
     $countable->expects($this->never())->method('count')->will($this->returnValue(2));
     $this->configFileValidator->expects($this->never())->method('validate')->will($this->returnValue($countable));
     $emailAttachment = $this->getEmailAttachment();
     $this->emailAttachmentManager->method('buildAttachmentInstance')->withAnyParameters()->will($this->returnValue($this->attachment));
     $emailAttachment->expects($this->any())->method('getFile')->will($this->returnValue($file));
     $this->emailAttachmentManager->linkEmailAttachmentToTargetEntity($emailAttachment, new SomeEntity());
 }