public function setUp()
 {
     parent::setUp();
     $this->user = User::getByUsername('super');
     Yii::app()->user->userModel = $this->user;
     EmailAccount::deleteAll();
     EmailMessage::deleteAll();
     EmailMessageContent::deleteAll();
     EmailMessageSender::deleteAll();
     EmailMessageRecipient::deleteAll();
     EmailMessageSendError::deleteAll();
     FileModel::deleteAll();
 }
 public function setUp()
 {
     parent::setUp();
     $this->user = User::getByUsername('super');
     Yii::app()->user->userModel = $this->user;
     EmailAccount::deleteAll();
     EmailMessage::deleteAll();
     EmailMessageContent::deleteAll();
     EmailMessageSender::deleteAll();
     EmailMessageRecipient::deleteAll();
     EmailMessageSendError::deleteAll();
     FileModel::deleteAll();
     if (!EmailMessageTestHelper::isSetEmailAccountsTestConfiguration()) {
         $this->markTestSkipped('Please fix the test email settings');
     }
 }
 /**
  * A notification email is different than a regular outbound email because it is owned by a super user
  * that is different than the user logged in.  So the sender does not have a 'person'
  */
 public function testCreateEmailMessageThatIsANotification()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $billy = User::getByUsername('billy');
     $this->assertEquals(0, EmailMessage::getCount());
     $emailMessage = new EmailMessage();
     $emailMessage->owner = BaseControlUserConfigUtil::getUserToRunAs();
     $emailMessage->subject = 'My First Email';
     //Set sender, and recipient, and content
     $emailContent = new EmailMessageContent();
     $emailContent->textContent = 'My First Message';
     $emailContent->htmlContent = 'Some fake HTML content';
     $emailMessage->content = $emailContent;
     //Sending from the system, does not have a 'person'.
     $sender = new EmailMessageSender();
     $sender->fromAddress = '*****@*****.**';
     $sender->fromName = 'Zurmo System';
     $emailMessage->sender = $sender;
     //Recipient is billy.
     $recipient = new EmailMessageRecipient();
     $recipient->toAddress = '*****@*****.**';
     $recipient->toName = 'Billy James';
     $recipient->type = EmailMessageRecipient::TYPE_TO;
     $recipient->personsOrAccounts->add($billy);
     $emailMessage->recipients->add($recipient);
     //At this point the message is in no folder
     $this->assertTrue($emailMessage->folder->id < 0);
     $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
     $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_DRAFT);
     //Save, at this point the email should be in the draft folder
     $saved = $emailMessage->save();
     $this->assertTrue($saved);
     $this->assertTrue($emailMessage->folder->id > 0);
     $this->assertEquals(0, EmailMessageSendError::getCount());
     $emailMessageSendError = new EmailMessageSendError();
     $data = array();
     $data['message'] = 'error message';
     $emailMessageSendError->serializedData = serialize($data);
     $emailMessage->folder = EmailFolder::getByBoxAndType($emailMessage->folder->emailBox, EmailFolder::TYPE_OUTBOX_ERROR);
     $emailMessage->error = $emailMessageSendError;
     $saved = $emailMessage->save();
     $this->assertTrue($saved);
     $this->assertEquals(1, EmailMessageSendError::getCount());
     //Now swap the error with a new one
     $emailMessageId = $emailMessage->id;
     $emailMessage->forget();
     $emailMessage = EmailMessage::getById($emailMessageId);
     $emailMessageSendError = new EmailMessageSendError();
     $data = array();
     $data['message'] = 'error message 2';
     $emailMessageSendError->serializedData = serialize($data);
     $emailMessage->error = $emailMessageSendError;
     $saved = $emailMessage->save();
     $this->assertTrue($saved);
     $this->assertEquals(1, EmailMessageSendError::getCount());
 }