/**
  * @param Workflow $workflow
  * @param EmailMessageForWorkflowForm $emailMessage
  * @param RedBeanModel $model
  * @param User $triggeredByUser
  * @throws FailedToSaveModelException
  */
 protected static function processEmailMessageAfterSave(Workflow $workflow, EmailMessageForWorkflowForm $emailMessage, RedBeanModel $model, User $triggeredByUser)
 {
     if ($emailMessage->sendAfterDurationInterval == 0) {
         $helper = new WorkflowEmailMessageProcessingHelper($emailMessage, $model, $triggeredByUser);
         $helper->process();
     } else {
         $emailMessageData = SavedWorkflowToWorkflowAdapter::makeArrayFromEmailMessageForWorkflowFormAttributesData(array($emailMessage));
         $workflowMessageInQueue = new WorkflowMessageInQueue();
         $workflowMessageInQueue->processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($emailMessage->resolveNewTimeStampForDuration(time()));
         $workflowMessageInQueue->savedWorkflow = SavedWorkflow::getById((int) $workflow->getId());
         $workflowMessageInQueue->modelClassName = get_class($model);
         $workflowMessageInQueue->modelItem = $model;
         $workflowMessageInQueue->serializedData = serialize($emailMessageData);
         $workflowMessageInQueue->triggeredByUser = $triggeredByUser;
         $saved = $workflowMessageInQueue->save();
         if (!$saved) {
             throw new FailedToSaveModelException();
         }
     }
 }
 /**
  * To try to show failure of https://www.pivotaltracker.com/story/show/81571830
  * Using trademark symbol to demonstrate merge tag resolution working correctly and it properly printing
  * text after the trademark symbol
  */
 public function testNonAlphaNumericSymbolsAsMergeTagFields()
 {
     $account = AccountTestHelper::createAccountByNameForOwner('Candyland™', Yii::app()->user->userModel);
     $emailTemplate = new EmailTemplate();
     $emailTemplate->builtType = EmailTemplate::BUILT_TYPE_PASTED_HTML;
     $emailTemplate->modelClassName = 'Account';
     $emailTemplate->type = 1;
     $emailTemplate->name = 'some template';
     $emailTemplate->subject = 'some subject [[NAME]] is great';
     $emailTemplate->htmlContent = 'Account HTML: [[NAME]] after the merge tag is resolved';
     $emailTemplate->textContent = 'Account Text: [[NAME]] after the merge tag is resolved';
     $this->assertTrue($emailTemplate->save());
     $message = new EmailMessageForWorkflowForm('Account', Workflow::TYPE_ON_SAVE);
     $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_MODEL_USER, 'audienceType' => EmailMessageRecipient::TYPE_TO, 'dynamicUserType' => DynamicTriggeredModelUserWorkflowEmailMessageRecipientForm::DYNAMIC_USER_TYPE_CREATED_BY_USER));
     $message->emailTemplateId = $emailTemplate->id;
     $message->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_CUSTOM;
     $message->sendFromAddress = '*****@*****.**';
     $message->sendFromName = 'Jason';
     $message->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
     $helper = new WorkflowEmailMessageProcessingHelper($message, $account, Yii::app()->user->userModel);
     $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $helper->process();
     $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
     $this->assertEquals('some subject Candyland™ is great', $emailMessages[0]->subject);
     $this->assertEquals('Account Text: Candyland™ after the merge tag is resolved', $emailMessages[0]->content->textContent);
     $this->assertEquals('Account HTML: Candyland™ after the merge tag is resolved', $emailMessages[0]->content->htmlContent);
 }
 /**
  * @depends testProcessWithCustomSender
  */
 public function testInvalidMergeTagsReturnOriginalContent()
 {
     self::$emailTemplate->subject = 'bad subject [[LASTNAME]]';
     self::$emailTemplate->textContent = 'bad text [[LASTNAME]]';
     self::$emailTemplate->htmlContent = 'bad html [[LASTNAME]]';
     $message = new EmailMessageForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
     $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_MODEL_USER, 'audienceType' => EmailMessageRecipient::TYPE_TO, 'dynamicUserType' => DynamicTriggeredModelUserWorkflowEmailMessageRecipientForm::DYNAMIC_USER_TYPE_CREATED_BY_USER));
     $message->emailTemplateId = self::$emailTemplate->id;
     $message->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_CUSTOM;
     $message->sendFromAddress = '*****@*****.**';
     $message->sendFromName = 'Jason';
     $message->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
     $model = new WorkflowModelTestItem();
     $model->lastName = 'the lastName';
     $model->string = 'the string';
     $model->phone = 'the phone';
     $saved = $model->save();
     $this->assertTrue($saved);
     $helper = new WorkflowEmailMessageProcessingHelper($message, $model, Yii::app()->user->userModel);
     $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $helper->process();
     $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
     $this->assertEquals('bad subject [[LASTNAME]]', $emailMessages[0]->subject);
     $this->assertEquals('bad text [[LASTNAME]]', $emailMessages[0]->content->textContent);
     $this->assertEquals('bad html [[LASTNAME]]', $emailMessages[0]->content->htmlContent);
     $this->assertEquals('Jason', $emailMessages[0]->sender->fromName);
     $this->assertEquals('*****@*****.**', $emailMessages[0]->sender->fromAddress);
     $this->assertEquals(1, $emailMessages[0]->recipients->count());
     $this->assertEquals('*****@*****.**', $emailMessages[0]->recipients[0]->toAddress);
     $emailMessages[0]->delete();
 }