All Data Structures Functions Variables Pages
WorkflowEmailMessagesUtilTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
38  {
39  protected static $savedWorkflow;
40 
41  public static function setUpBeforeClass()
42  {
43  parent::setUpBeforeClass();
45  $super = User::getByUsername('super');
46  $super->primaryEmail = new Email();
47  $super->primaryEmail->emailAddress = 'super@zurmo.com';
48  $saved = $super->save();
49  assert('$saved'); // Not Coding Standard
50  $savedWorkflow = new SavedWorkflow();
51  $savedWorkflow->name = 'some workflow';
52  $savedWorkflow->description = 'description';
53  $savedWorkflow->moduleClassName = 'moduleClassName';
54  $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
55  $savedWorkflow->type = 'some type';
56  $savedWorkflow->serializedData = serialize(array('something'));
57  $saved = $savedWorkflow->save();
58  assert('$saved'); // Not Coding Standard
59  self::$savedWorkflow = $savedWorkflow;
60  }
61 
62  public static function getDependentTestModelClassNames()
63  {
64  return array('WorkflowModelTestItem');
65  }
66 
67  public function testProcessAfterSaveWhenSendIsInFuture()
68  {
69  $this->assertEquals(0, WorkflowMessageInQueue::getCount());
70  $workflow = new Workflow();
71  $workflow->setId(self::$savedWorkflow->id);
72  $workflow->type = Workflow::TYPE_ON_SAVE;
73  $emailMessageForm = new EmailMessageForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
74  $emailMessageForm->sendAfterDurationInterval = 1;
75  $emailMessageForm->sendAfterDurationType = TimeDurationUtil::DURATION_TYPE_DAY;
76  $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::
77  TYPE_DYNAMIC_TRIGGERED_MODEL_USER,
78  'audienceType' => EmailMessageRecipient::TYPE_TO,
80  DYNAMIC_USER_TYPE_CREATED_BY_USER));
81  $emailMessageForm->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
82  $workflow->addEmailMessage($emailMessageForm);
83  $model = new WorkflowModelTestItem();
84  $model->lastName = 'lastName';
85  $model->string = 'string';
86  $this->assertTrue($model->save());
87  $compareDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + 86400);
88  WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
89  $workflowMessageInQueues = WorkflowMessageInQueue::getAll();
90  $this->assertEquals(1, count($workflowMessageInQueues));
91  $this->assertTrue($workflowMessageInQueues[0]->savedWorkflow->isSame(self::$savedWorkflow));
92  $this->assertTrue($workflowMessageInQueues[0]->triggeredByUser->isSame(Yii::app()->user->userModel));
93  $this->assertEquals($model->getClassId('Item'), $workflowMessageInQueues[0]->modelItem->getClassId('Item'));
94  $this->assertEquals('WorkflowModelTestItem', $workflowMessageInQueues[0]->modelClassName);
95  $this->assertEquals($compareDateTime, $workflowMessageInQueues[0]->processDateTime);
96  $emailMessageData = SavedWorkflowToWorkflowAdapter::
98  $this->assertEquals(serialize($emailMessageData), $workflowMessageInQueues[0]->serializedData);
99  $this->assertTrue($workflowMessageInQueues[0]->delete());
100  }
101 
106  {
107  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
108  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
109 
110  $emailTemplate = new EmailTemplate();
111  $emailTemplate->builtType = EmailTemplate::BUILT_TYPE_PASTED_HTML;
112  $emailTemplate->name = 'the name';
113  $emailTemplate->modelClassName = 'Account';
114  $emailTemplate->textContent = 'some content';
115  $emailTemplate->type = 2;
116  $emailTemplate->subject = 'subject';
117  $saved = $emailTemplate->save();
118  $this->assertTrue($saved);
119  $workflow = new Workflow();
120  $workflow->setId(self::$savedWorkflow->id);
121  $workflow->type = Workflow::TYPE_ON_SAVE;
122  $emailMessageForm = new EmailMessageForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
123  $emailMessageForm->sendAfterDurationInterval = 0;
124  $emailMessageForm->sendAfterDurationType = TimeDurationUtil::DURATION_TYPE_WEEK;
125  $emailMessageForm->emailTemplateId = $emailTemplate->id;
126  $emailMessageForm->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT;
127  $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::
128  TYPE_DYNAMIC_TRIGGERED_BY_USER,
129  'audienceType' => EmailMessageRecipient::TYPE_TO));
130  $emailMessageForm->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
131  $workflow->addEmailMessage($emailMessageForm);
132  $model = new WorkflowModelTestItem();
133  $model->lastName = 'lastName';
134  $model->string = 'string';
135  $this->assertTrue($model->save());
136  WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
137  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
138  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
139  }
140 
141  public function testProcessAfterSaveWhenSendIsImmediateAndToAContactThatIsTheTriggeredModel()
142  {
143  foreach (EmailMessage::getAll() as $emailMessage)
144  {
145  $emailMessage->delete();
146  }
147  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
148  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
149 
150  $emailTemplate = new EmailTemplate();
151  $emailTemplate->builtType = EmailTemplate::BUILT_TYPE_PASTED_HTML;
152  $emailTemplate->name = 'the name';
153  $emailTemplate->modelClassName = 'Account';
154  $emailTemplate->textContent = 'some content';
155  $emailTemplate->type = 2;
156  $emailTemplate->subject = 'subject';
157  $saved = $emailTemplate->save();
158  $this->assertTrue($saved);
159  $workflow = new Workflow();
160  $workflow->setId(self::$savedWorkflow->id);
161  $workflow->type = Workflow::TYPE_ON_SAVE;
162  $emailMessageForm = new EmailMessageForWorkflowForm('Contact', Workflow::TYPE_ON_SAVE);
163  $emailMessageForm->sendAfterDurationInterval = 0;
164  $emailMessageForm->sendAfterDurationType = TimeDurationUtil::DURATION_TYPE_WEEK;
165  $emailMessageForm->emailTemplateId = $emailTemplate->id;
166  $emailMessageForm->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT;
167  $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::
168  TYPE_DYNAMIC_TRIGGERED_MODEL,
169  'audienceType' => EmailMessageRecipient::TYPE_TO));
170  $emailMessageForm->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
171  $workflow->addEmailMessage($emailMessageForm);
172  $model = new Contact();
173  $model->firstName = 'Jason';
174  $model->lastName = 'Blue';
175  $model->state = ContactsUtil::getStartingState();
176  $model->primaryEmail->emailAddress = 'jason@something.com';
177  $this->assertTrue($model->save());
178  WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
179  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
180  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
181 
182  $queuedEmailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
183  $this->assertEquals(1, count($queuedEmailMessages));
184  $this->assertEquals(1, count($queuedEmailMessages[0]->recipients));
185  $this->assertEquals('Jason Blue' , $queuedEmailMessages[0]->recipients[0]->toName);
186  $this->assertEquals('jason@something.com', $queuedEmailMessages[0]->recipients[0]->toAddress);
187  $this->assertEquals($model->id, $queuedEmailMessages[0]->recipients[0]->personsOrAccounts[0]->id);
188  }
189 
190  public function testProcessAfterSaveWhenSendIsImmediateAndToAContactThatIsRelatedToTheTriggeredModel()
191  {
192  foreach (EmailMessage::getAll() as $emailMessage)
193  {
194  $emailMessage->delete();
195  }
196  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
197  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
198 
199  $emailTemplate = new EmailTemplate();
200  $emailTemplate->builtType = EmailTemplate::BUILT_TYPE_PASTED_HTML;
201  $emailTemplate->name = 'the name';
202  $emailTemplate->modelClassName = 'Account';
203  $emailTemplate->textContent = 'some content';
204  $emailTemplate->type = 2;
205  $emailTemplate->subject = 'subject';
206  $saved = $emailTemplate->save();
207  $this->assertTrue($saved);
208  $workflow = new Workflow();
209  $workflow->setId(self::$savedWorkflow->id);
210  $workflow->type = Workflow::TYPE_ON_SAVE;
211  $emailMessageForm = new EmailMessageForWorkflowForm('Account', Workflow::TYPE_ON_SAVE);
212  $emailMessageForm->sendAfterDurationInterval = 0;
213  $emailMessageForm->sendAfterDurationType = TimeDurationUtil::DURATION_TYPE_WEEK;
214  $emailMessageForm->emailTemplateId = $emailTemplate->id;
215  $emailMessageForm->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT;
216  $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::
217  TYPE_DYNAMIC_TRIGGERED_MODEL_RELATION,
218  'audienceType' => EmailMessageRecipient::TYPE_TO,
219  'relation' => 'contacts'));
220  $emailMessageForm->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
221  $workflow->addEmailMessage($emailMessageForm);
222  $model = new Account();
223  $model->name = 'the account';
224  $contact = new Contact();
225  $contact->firstName = 'Jason';
226  $contact->lastName = 'Blue';
227  $contact->state = ContactsUtil::getStartingState();
228  $contact->primaryEmail->emailAddress = 'jason@something.com';
229  $this->assertTrue($contact->save());
230  $contact2 = new Contact();
231  $contact2->firstName = 'Laura';
232  $contact2->lastName = 'Blue';
233  $contact2->state = ContactsUtil::getStartingState();
234  $contact2->primaryEmail->emailAddress = 'laura@something.com';
235  $this->assertTrue($contact2->save());
236  $model->contacts->add($contact);
237  $model->contacts->add($contact2);
238  $this->assertTrue($model->save());
239  WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
240  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
241  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
242 
243  $queuedEmailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
244  $this->assertEquals(1, count($queuedEmailMessages));
245  $this->assertEquals(2, count($queuedEmailMessages[0]->recipients));
246  $this->assertEquals('Jason Blue' , $queuedEmailMessages[0]->recipients[0]->toName);
247  $this->assertEquals('jason@something.com', $queuedEmailMessages[0]->recipients[0]->toAddress);
248  $this->assertEquals($contact->id, $queuedEmailMessages[0]->recipients[0]->personsOrAccounts[0]->id);
249  $this->assertEquals('Laura Blue' , $queuedEmailMessages[0]->recipients[1]->toName);
250  $this->assertEquals('laura@something.com', $queuedEmailMessages[0]->recipients[1]->toAddress);
251  $this->assertEquals($contact2->id, $queuedEmailMessages[0]->recipients[1]->personsOrAccounts[0]->id);
252  }
253 
254  public function testMakeEmailMessageForWorkflowFormByQueueModelAndWorkflow()
255  {
256  $model = ContactTestHelper::createContactByNameForOwner('Jason', Yii::app()->user->userModel);
257  $savedWorkflow = new SavedWorkflow();
258  $savedWorkflow->name = 'some workflow';
259  $savedWorkflow->description = 'description';
260  $savedWorkflow->moduleClassName = 'WorkflowsTestModule';
261  $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
262  $savedWorkflow->type = 'some type';
263  $savedWorkflow->order = 1;
264  $savedWorkflow->serializedData = serialize(array('something'));
265  $savedWorkflow->isActive = true;
266  $saved = $savedWorkflow->save();
267  $this->assertTrue($saved);
268  $emailMessage = null;
269  $emailMessage['emailTemplateId'] = 5;
270  $emailMessage['sendFromType'] = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT;
271  $emailMessage['sendAfterDurationType'] = TimeDurationUtil::DURATION_TYPE_DAY;
272  $emailMessage['sendAfterDurationInterval'] = '44';
274  array(
275  array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_MODEL,
276  'audienceType' => EmailMessageRecipient::TYPE_TO),
277  );
278  $workflowMessageInQueue = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($model, $savedWorkflow, serialize(array($emailMessage)));
279  $workflow = new Workflow();
280  $workflow->setModuleClassName('WorkflowsTestModule');
281  $workflow->setType(Workflow::TYPE_ON_SAVE);
282  $emailMessageForWorkflowForm = WorkflowEmailMessagesUtil::
284  $workflowMessageInQueue, $workflow);
285  $this->assertEquals(5, $emailMessageForWorkflowForm->emailTemplateId);
286  $this->assertEquals(EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT, $emailMessageForWorkflowForm->sendFromType);
287  $this->assertEquals(TimeDurationUtil::DURATION_TYPE_DAY, $emailMessageForWorkflowForm->sendAfterDurationType);
288  $this->assertEquals(44, $emailMessageForWorkflowForm->sendAfterDurationInterval);
289  $this->assertEquals(1, count($emailMessageForWorkflowForm->getEmailMessageRecipients()));
290  }
291  }
292 ?>
Definition: Email.php:37
static processAfterSave(Workflow $workflow, RedBeanModel $model, User $triggeredByUser)
static getByUsername($username)
Definition: User.php:49
static makeArrayFromEmailMessageForWorkflowFormAttributesData(Array $componentFormsData)
static loadStartingData()
static getStartingState()
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static makeEmailMessageForWorkflowFormByQueueModelAndWorkflow(WorkflowMessageInQueue $workflowMessageInQueue, Workflow $workflow)
Generated on Fri May 29 2020 07:10:35