/** * @param string $method * * @dataProvider methodsData */ public function testProcessException($method) { $this->request->setMethod($method); $this->model->setFrom('*****@*****.**')->setTo(['*****@*****.**'])->setSubject('testSubject')->setBody('testBody'); $this->form->expects($this->once())->method('setData')->with($this->model); $this->form->expects($this->once())->method('submit')->with($this->request); $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true)); $exception = new \Exception('TEST'); $this->emailProcessor->expects($this->once())->method('process')->with($this->model)->will($this->returnCallback(function () use($exception) { throw $exception; })); $this->logger->expects($this->once())->method('error')->with('Email sending failed.', ['exception' => $exception]); $this->form->expects($this->once())->method('addError')->with($this->isInstanceOf('Symfony\\Component\\Form\\FormError')); $this->assertFalse($this->handler->process($this->model)); }
/** * @dataProvider supportedMethods */ public function testProcessValidData($method) { $this->request->setMethod($method); $this->model->setFrom('*****@*****.**')->setTo(array('*****@*****.**'))->setSubject('testSubject')->setBody('testBody'); $this->form->expects($this->once())->method('setData')->with($this->model); $this->form->expects($this->once())->method('submit')->with($this->request); $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true)); $message = new \Swift_Message(); $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($message)); $this->mailer->expects($this->once())->method('send')->will($this->returnValue(1)); $origin = new InternalEmailOrigin(); $folder = new EmailFolder(); $folder->setType(EmailFolder::SENT); $origin->addFolder($folder); $emailOriginRepo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock(); $emailOriginRepo->expects($this->once())->method('findOneBy')->with(array('name' => InternalEmailOrigin::BAP))->will($this->returnValue($origin)); $this->em->expects($this->once())->method('getRepository')->with('OroEmailBundle:InternalEmailOrigin')->will($this->returnValue($emailOriginRepo)); $this->emailEntityBuilder->expects($this->once())->method('setOrigin')->with($this->identicalTo($origin)); $email = new EmailEntity(); $this->emailEntityBuilder->expects($this->once())->method('email')->with('testSubject', '*****@*****.**', array('*****@*****.**'))->will($this->returnValue($email)); $body = new EmailBody(); $this->emailEntityBuilder->expects($this->once())->method('body')->with('testBody', false, true)->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'); $this->assertTrue($this->handler->process($this->model)); $this->assertNotNull($message); $this->assertEquals(array('*****@*****.**' => null), $message->getFrom()); $this->assertEquals(array('*****@*****.**' => null), $message->getTo()); $this->assertEquals('testSubject', $message->getSubject()); $this->assertEquals('testBody', $message->getBody()); $this->assertTrue($folder === $email->getFolder()); $this->assertTrue($body === $email->getEmailBody()); }
/** * {@inheritdoc} */ protected function executeAction($context) { $emailModel = new Email(); $from = $this->getEmailAddress($context, $this->options['from']); $this->validateAddress($from); $emailModel->setFrom($from); $to = []; foreach ($this->options['to'] as $email) { if ($email) { $address = $this->getEmailAddress($context, $email); $this->validateAddress($address); $to[] = $this->getEmailAddress($context, $address); } } $emailModel->setTo($to); $entity = $this->contextAccessor->getValue($context, $this->options['entity']); $template = $this->contextAccessor->getValue($context, $this->options['template']); $emailTemplate = $this->objectManager->getRepository('OroEmailBundle:EmailTemplate')->findByName($template); if (!$emailTemplate) { $errorMessage = sprintf('Template "%s" not found.', $template); $this->logger->error('Workflow send email action.' . $errorMessage); throw new EntityNotFoundException($errorMessage); } $templateData = $this->renderer->compileMessage($emailTemplate, ['entity' => $entity]); $type = $emailTemplate->getType() == 'txt' ? 'text/plain' : 'text/html'; list($subjectRendered, $templateRendered) = $templateData; $emailModel->setSubject($subjectRendered); $emailModel->setBody($templateRendered); $emailModel->setType($type); $emailUser = $this->emailProcessor->process($emailModel); if (array_key_exists('attribute', $this->options)) { $this->contextAccessor->setValue($context, $this->options['attribute'], $emailUser->getEmail()); } }
/** * {@inheritdoc} */ protected function executeAction($context) { $emailModel = new Email(); $emailModel->setFrom($this->getEmailAddress($context, $this->options['from'])); $to = array(); foreach ($this->options['to'] as $email) { if ($email) { $to[] = $this->getEmailAddress($context, $email); } } $emailModel->setTo($to); $emailModel->setSubject($this->contextAccessor->getValue($context, $this->options['subject'])); $emailModel->setBody($this->contextAccessor->getValue($context, $this->options['body'])); $email = $this->emailProcessor->process($emailModel); if (array_key_exists('attribute', $this->options)) { $this->contextAccessor->setValue($context, $this->options['attribute'], $email); } }
/** * @param int $id * @param string $entity * @param array $from * @param array $to * @param string $subject * @param string $body * * @dataProvider sendDataProvider */ public function testSend($id, $entity, array $from, array $to, $subject, $body, $expects) { $emails = array_keys($from); $this->helper->expects($this->once())->method('getSingleEntityIdentifier')->will($this->returnValue($id)); $this->emailHelper->expects($this->once())->method('buildFullEmailAddress')->will($this->returnValue(sprintf('%s <%s>', reset($emails), reset($from)))); $marketingList = new MarketingList(); $marketingList->setEntity($entity); $template = new EmailTemplate(); $template->setType('html'); $settings = new InternalTransportSettings(); $settings->setTemplate($template); $campaign = new EmailCampaign(); $campaign->setMarketingList($marketingList)->setTransportSettings($settings); $this->renderer->expects($this->once())->method('compileMessage')->will($this->returnValue([$subject, $body])); $emailModel = new Email(); $emailModel->setFrom(sprintf('%s <%s>', reset($emails), reset($from)))->setType($template->getType())->setEntityClass($entity)->setEntityId($id)->setTo($to)->setSubject($subject)->setBody($body); $this->processor->expects($expects)->method('process')->with($this->equalTo($emailModel)); $this->transport->send($campaign, $entity, $from, $to); }
/** * {@inheritdoc} */ protected function executeAction($context) { $type = 'txt'; $emailModel = new Email(); $emailModel->setFrom($this->getEmailAddress($context, $this->options['from'])); $to = []; foreach ($this->options['to'] as $email) { if ($email) { $to[] = $this->getEmailAddress($context, $email); } } $emailModel->setTo($to); $emailModel->setSubject($this->contextAccessor->getValue($context, $this->options['subject'])); $emailModel->setBody($this->contextAccessor->getValue($context, $this->options['body'])); if (array_key_exists('type', $this->options) && in_array($this->options['type'], ['txt', 'html'], true)) { $type = $this->options['type']; } $emailModel->setType($type); $emailUser = $this->emailProcessor->process($emailModel); if (array_key_exists('attribute', $this->options)) { $this->contextAccessor->setValue($context, $this->options['attribute'], $emailUser->getEmail()); } }
/** * @param EmailModel $emailModel */ protected function applyFrom(EmailModel $emailModel) { if (!$emailModel->getFrom()) { if ($this->request->query->has('from')) { $from = $this->request->query->get('from'); if (!empty($from)) { $this->helper->preciseFullEmailAddress($from); } $emailModel->setFrom($from); } else { $user = $this->helper->getUser(); if ($user) { $emailModel->setFrom($this->helper->buildFullEmailAddress($user)); } } } }
/** * Populate a model with initial data. * This method is used to load an initial data from a query string * * @param Email $model */ protected function initModel(Email $model) { if ($this->request->query->has('gridName')) { $model->setGridName($this->request->query->get('gridName')); } if ($this->request->query->has('from')) { $from = $this->request->query->get('from'); if (!empty($from)) { $this->preciseFullEmailAddress($from); } $model->setFrom($from); } else { $user = $this->getUser(); if ($user) { $model->setFrom(EmailUtil::buildFullEmailAddress($user->getEmail(), $this->nameFormatter->format($user))); } } if ($this->request->query->has('to')) { $to = trim($this->request->query->get('to')); if (!empty($to)) { $this->preciseFullEmailAddress($to); } $model->setTo(array($to)); } if ($this->request->query->has('subject')) { $subject = trim($this->request->query->get('subject')); $model->setSubject($subject); } }
/** * Populate a model with initial data. * This method is used to load an initial data from a query string * * @param Email $model */ protected function initModel(Email $model) { if ($this->request->query->has('gridName')) { $model->setGridName($this->request->query->get('gridName')); } if ($this->request->query->has('entityClass')) { $model->setEntityClass($this->entityRoutingHelper->decodeClassName($this->request->query->get('entityClass'))); } if ($this->request->query->has('entityId')) { $model->setEntityId($this->request->query->get('entityId')); } if ($this->request->query->has('from')) { $from = $this->request->query->get('from'); if (!empty($from)) { $this->preciseFullEmailAddress($from); } $model->setFrom($from); } else { $user = $this->getUser(); if ($user) { $model->setFrom($this->emailAddressHelper->buildFullEmailAddress($user->getEmail(), $this->nameFormatter->format($user))); } } if ($this->request->query->has('to')) { $to = trim($this->request->query->get('to')); if (!empty($to)) { $this->preciseFullEmailAddress($to, $model->getEntityClass(), $model->getEntityId()); } $model->setTo(array($to)); } if ($this->request->query->has('subject')) { $subject = trim($this->request->query->get('subject')); $model->setSubject($subject); } }