/**
  * @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);
             }
         }
     }
 }
 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());
 }