Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
EmailHelperTest.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  public static $emailHelperSendEmailThroughTransport;
40 
41  protected $user = null;
42 
43  public static function setUpBeforeClass()
44  {
45  parent::setUpBeforeClass();
46  SecurityTestHelper::createSuperAdmin();
47  self::$emailHelperSendEmailThroughTransport = Yii::app()->emailHelper->sendEmailThroughTransport;
48  UserTestHelper::createBasicUser('billy');
49  UserTestHelper::createBasicUser('jane');
50  $someoneSuper = UserTestHelper::createBasicUser('someoneSuper');
51 
52  $group = Group::getByName('Super Administrators');
53  $group->users->add($someoneSuper);
54  $saved = $group->save();
55  assert($saved); // Not Coding Standard
56 
57  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
58 
59  if (EmailMessageTestHelper::isSetEmailAccountsTestConfiguration())
60  {
61  $steve = UserTestHelper::createBasicUser('steve');
62  EmailMessageTestHelper::createEmailAccount($steve);
63 
64  Yii::app()->imap->imapHost = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapHost'];
65  Yii::app()->imap->imapUsername = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapUsername'];
66  Yii::app()->imap->imapPassword = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapPassword'];
67  Yii::app()->imap->imapPort = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapPort'];
68  Yii::app()->imap->imapSSL = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapSSL'];
69  Yii::app()->imap->imapFolder = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapFolder'];
70  Yii::app()->imap->setInboundSettings();
71  Yii::app()->imap->init();
72 
73  self::loadEmailHelperWithTestCredentials();
74  }
75  // Delete item from jobQueue, that is created when new user is created
76  Yii::app()->jobQueue->deleteAll();
77  }
78 
79  public function setUp()
80  {
81  parent::setUp();
82  $this->user = User::getByUsername('super');
83  Yii::app()->user->userModel = $this->user;
84  if (!EmailMessageTestHelper::isSetEmailAccountsTestConfiguration())
85  {
86  $this->markTestSkipped(Zurmo::t('EmailMessagesModule', 'Email test settings are missing.'));
87  }
88  }
89 
90  public function testSend()
91  {
92  $emailMessage = EmailMessageTestHelper::createDraftSystemEmail('a test email', $this->user);
93  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
94  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
95  $this->assertEquals(0, count(Yii::app()->jobQueue->getAll()));
96  Yii::app()->emailHelper->send($emailMessage);
97  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
98  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
99  $queuedJobs = Yii::app()->jobQueue->getAll();
100  $this->assertEquals(1, count($queuedJobs));
101  $this->assertEquals('ProcessOutboundEmail', $queuedJobs[0][0]['jobType']);
102  }
103 
107  public function testSendQueued()
108  {
109  $super = User::getByUsername('super');
110  Yii::app()->user->userModel = $super;
111 
112  //add a message in the outbox_error folder.
113  $emailMessage = EmailMessageTestHelper::createDraftSystemEmail('a test email 2', $this->user);
114  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
115  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_OUTBOX_ERROR);
116  $emailMessage->save();
117 
118  $this->assertEquals(2, Yii::app()->emailHelper->getQueuedCount());
119  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
120  Yii::app()->emailHelper->sendQueued();
121  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
122  $this->assertEquals(2, Yii::app()->emailHelper->getSentCount());
123 
124  //add a message in the outbox folder.
125  $emailMessage = EmailMessageTestHelper::createDraftSystemEmail('a test email 3', $this->user);
126  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
127  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_OUTBOX);
128  $emailMessage->save();
129  //add a message in the outbox_error folder.
130  $emailMessage = EmailMessageTestHelper::createDraftSystemEmail('a test email 4', $this->user);
131  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
132  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_OUTBOX_ERROR);
133  $emailMessage->save();
134  //add a message in the outbox_error folder.
135  $emailMessage = EmailMessageTestHelper::createDraftSystemEmail('a test email 5', $this->user);
136  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
137  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_OUTBOX_ERROR);
138  $emailMessage->save();
139 
140  $this->assertEquals(3, Yii::app()->emailHelper->getQueuedCount());
141  $this->assertEquals(2, Yii::app()->emailHelper->getSentCount());
142  Yii::app()->emailHelper->sendQueued(1);
143  $this->assertEquals(2, Yii::app()->emailHelper->getQueuedCount());
144  $this->assertEquals(3, Yii::app()->emailHelper->getSentCount());
145  Yii::app()->emailHelper->sendQueued(2);
146  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
147  $this->assertEquals(5, Yii::app()->emailHelper->getSentCount());
148  }
149 
153  public function testSendImmediately()
154  {
155  $emailMessage = EmailMessageTestHelper::createDraftSystemEmail('a test email 2', $this->user);
156  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
157  $this->assertEquals(5, Yii::app()->emailHelper->getSentCount());
158  Yii::app()->emailHelper->sendImmediately($emailMessage);
159  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
160  $this->assertEquals(6, Yii::app()->emailHelper->getSentCount());
161  }
162 
166  public function testLoadOutboundSettings()
167  {
168  $emailHelper = new EmailHelper;
169  $emailHelper->outboundHost = null;
170 
171  ZurmoConfigurationUtil::setByModuleName('EmailMessagesModule', 'outboundHost', 'xxx');
172 
173  $emailHelper = new EmailHelper;
174  $emailHelper->outboundHost = 'xxx';
175  }
176 
181  {
182  $billy = User::getByUsername('billy');
183  Yii::app()->user->userModel = $billy;
184  $emailHelper = new EmailHelper;
185 
186  //Load outbound setting when no EmailAccount was created
187  try
188  {
189  $emailHelper->loadOutboundSettingsFromUserEmailAccount($billy);
190  $this->fail();
191  }
192  catch (NotFoundException $e)
193  {
194  $this->addToAssertionCount(1);
195  }
196 
197  //Load outbound setting when EmailAccount useCustomOutboundSettings = false
198  EmailMessageTestHelper::createEmailAccount($billy);
199  $emailHelper->loadOutboundSettingsFromUserEmailAccount($billy);
200  $this->assertEquals('smtp', $emailHelper->outboundType);
201  $this->assertEquals(Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundPort'], $emailHelper->outboundPort);
202  //outboundHost was set on @testLoadOutboundSettingsFromUserEmailAccount
203  $this->assertEquals('xxx', $emailHelper->outboundHost);
204  $this->assertEquals($emailHelper->defaultTestToAddress, $emailHelper->fromAddress);
205  $this->assertEquals(strval($billy), $emailHelper->fromName);
206 
207  //Load outbound setting when EmailAccount useCustomOutboundSettings = true
208  $emailAccount = EmailAccount::getByUserAndName($billy);
209  $emailAccount->useCustomOutboundSettings = EmailMessageUtil::OUTBOUND_PERSONAL_SMTP_SETTINGS;
210  $emailAccount->outboundType = 'xyz';
211  $emailAccount->outboundPort = 55;
212  $emailAccount->outboundHost = 'zurmo.com';
213  $emailAccount->outboundUsername = 'billy';
214  $emailAccount->outboundPassword = 'billypass';
215  $emailAccount->outboundSecurity = 'ssl';
216  $emailAccount->save();
217  $emailHelper->loadOutboundSettingsFromUserEmailAccount($billy);
218  $this->assertEquals('xyz', $emailHelper->outboundType);
219  $this->assertEquals(55, $emailHelper->outboundPort);
220  $this->assertEquals('zurmo.com', $emailHelper->outboundHost);
221  $this->assertEquals('billy', $emailHelper->outboundUsername);
222  $this->assertEquals('billypass', $emailHelper->outboundPassword);
223  $this->assertEquals('ssl', $emailHelper->outboundSecurity);
224  $this->assertEquals($billy->getFullName(), $emailHelper->fromName);
225  $this->assertEquals('user@zurmo.com', $emailHelper->fromAddress);
226  }
227 
231  public function testSendRealEmail()
232  {
233  self::loadEmailHelperWithTestCredentials();
234  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
235  if (EmailMessageTestHelper::isSetEmailAccountsTestConfiguration())
236  {
237  $user = User::getByUsername('steve');
238  $user->primaryEmail->emailAddress = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapUsername'];
239  $this->assertTrue($user->save());
240 
241  Yii::app()->imap->connect();
242  Yii::app()->imap->deleteMessages(true);
243  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
244  $this->assertEquals(0, $imapStats->Nmsgs);
245 
246  $emailMessage = EmailMessageTestHelper::createOutboxEmail($this->user, 'Test email',
247  'Raw content', ',b>html content</b>end.', // Not Coding Standard
248  'Zurmo', Yii::app()->emailHelper->outboundUsername,
249  'Ivica', Yii::app()->params['emailTestAccounts']['userImapSettings']['imapUsername']);
250 
251  $filesIds = array();
252  $fileTxt = ZurmoTestHelper::createFileModel('testNote.txt');
253  $filesIds[] = $fileTxt->id;
254  $filePng = ZurmoTestHelper::createFileModel('testImage.png');
255  $filesIds[] = $filePng->id;
256  $fileZip = ZurmoTestHelper::createFileModel('testZip.zip');
257  $filesIds[] = $fileZip->id;
258  $filePdf = ZurmoTestHelper::createFileModel('testPDF.pdf');
259  $filesIds[] = $filePdf->id;
260  EmailMessageUtil::attachFilesToMessage($filesIds, $emailMessage);
261  $this->assertEquals('4', count($emailMessage->files));
262 
263  Yii::app()->imap->connect();
264  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
265  $this->assertEquals(0, $imapStats->Nmsgs);
266 
267  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
268  $this->assertEquals(6, Yii::app()->emailHelper->getSentCount());
269  Yii::app()->emailHelper->sendQueued();
270  $job = new ProcessOutboundEmailJob();
271  $this->assertTrue($job->run());
272  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
273  $this->assertEquals(7, Yii::app()->emailHelper->getSentCount());
274 
275  sleep(30);
276  Yii::app()->imap->connect();
277  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
278  $this->assertEquals(1, $imapStats->Nmsgs);
279  }
280  }
281 
282  public static function tearDownAfterClass()
283  {
284  $imap = new ZurmoImap();
285  $imap->imapHost = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapHost'];
286  $imap->imapUsername = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapUsername'];
287  $imap->imapPassword = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapPassword'];
288  $imap->imapPort = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapPort'];
289  $imap->imapSSL = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapSSL'];
290  $imap->imapFolder = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapFolder'];
291  $imap->init();
292  $imap->connect();
293  $imap->deleteMessages(true);
294 
295  Yii::app()->emailHelper->sendEmailThroughTransport = self::$emailHelperSendEmailThroughTransport;
296  parent::tearDownAfterClass();
297  }
298 
303  {
304  $super = User::getByUsername('super');
305  Yii::app()->user->userModel = $super;
306 
307  if (EmailMessageTestHelper::isSetEmailAccountsTestConfiguration())
308  {
309  //add a message in the outbox_error folder.
310  $emailMessage = EmailMessageTestHelper::createDraftSystemEmail('a test email 2', $this->user);
311  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
312  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_OUTBOX_ERROR);
313  $emailMessage->sendAttempts = 5;
314  $emailMessage->save();
315 
316  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
317  $this->assertEquals(7, Yii::app()->emailHelper->getSentCount());
318  Yii::app()->emailHelper->sendQueued();
319  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
320  $this->assertEquals(7, Yii::app()->emailHelper->getSentCount());
321  $this->assertTrue($emailMessage->folder->isSame(EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_OUTBOX_FAILURE)));
322  }
323  else
324  {
325  $this->markTestSkipped();
326  }
327  }
328 
329  public function testSendMessagePopulatesEmailAccountSettings()
330  {
331  $jane = User::getByUsername('jane');
332  Yii::app()->user->userModel = $jane;
333  $emailHelper = new EmailHelper;
334  EmailMessageTestHelper::createEmailAccount($jane);
335  $emailAccount = EmailAccount::getByUserAndName($jane);
336  $emailAccount->useCustomOutboundSettings = EmailMessageUtil::OUTBOUND_PERSONAL_SMTP_SETTINGS;
337  $emailAccount->outboundType = 'abc';
338  $emailAccount->outboundPort = 11;
339  $emailAccount->outboundHost = 'dumb.domain';
340  $emailAccount->outboundUsername = 'jane';
341  $emailAccount->outboundPassword = 'janepass';
342  $emailAccount->outboundSecurity = 'ssl';
343  $emailAccount->save();
344 
345  $emailMessage = EmailMessageTestHelper::createOutboxEmail(
346  $jane,
347  'Test email',
348  'Raw content',
349  'Html content',
350  'Zurmo',
351  Yii::app()->emailHelper->outboundUsername,
352  'John Doe',
353  Yii::app()->params['emailTestAccounts']['userImapSettings']['imapUsername']);
354  $emailMessage->account = $emailAccount;
355  $emailMessage->save();
356 
357  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
358  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
359  Yii::app()->emailHelper->sendQueued();
360 
361  $job = new ProcessOutboundEmailJob();
362  $this->assertTrue($job->run());
363  //Since user email account has invalid settings message is not sent
364  $this->assertContains('Connection could not be established with host dumb.domain', strval($emailMessage->error));
365  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
366  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
367  }
368 
369  public function testResolveAndGetDefaultFromAddress()
370  {
371  $content = Yii::app()->emailHelper->resolveAndGetDefaultFromAddress();
372  $this->assertEquals('notification@zurmoalerts.com', $content);
373  }
374 
375  public function testSetDefaultFromAddress()
376  {
377  $content = Yii::app()->emailHelper->resolveAndGetDefaultFromAddress();
378  $this->assertEquals('notification@zurmoalerts.com', $content);
380  $metadata = ZurmoModule::getMetadata();
381  $this->assertEquals('notification@zurmoalerts.com', $metadata['global']['defaultFromAddress']);
382  }
383 
384  public function testResolveAndGetDefaultTestToAddress()
385  {
386  $content = Yii::app()->emailHelper->resolveAndGetDefaultTestToAddress();
387  $this->assertEquals('testJobEmail@zurmoalerts.com', $content);
388  }
389 
390  public function testSetDefaultTestToAddress()
391  {
392  $content = Yii::app()->emailHelper->resolveAndGetDefaultTestToAddress();
393  $this->assertEquals('testJobEmail@zurmoalerts.com', $content);
395  $metadata = ZurmoModule::getMetadata();
396  $this->assertEquals('testJobEmail@zurmoalerts.com', $metadata['global']['defaultTestToAddress']);
397  }
398 
403  {
406  $email = new Email();
407  $email->emailAddress = 'demo@zurmo.com';
408  $contact = ContactTestHelper::createContactByNameForOwner('contact', $this->user);
409  $contact->primaryEmail = $email;
410  $this->assertTrue($contact->save());
411  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList');
412  MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
413  $campaign = CampaignTestHelper::createCampaign('campaign',
414  'subject',
415  'text Content',
416  'Html Content',
417  null,
418  null,
419  null,
420  Campaign::STATUS_PROCESSING,
421  null,
422  0,
423  $marketingList);
424 
425  $processed = 0;
426  CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
427  $this->assertTrue($job->run());
428  $campaignItems = CampaignItem::getAll();
429  $this->assertCount(1, $campaignItems);
430  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
431  $this->assertCount(1, $campaignItemsProcessed);
432  $campaign->status = Campaign::STATUS_PAUSED;
433  $this->assertTrue($campaign->save());
434  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
435  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
436  Yii::app()->emailHelper->sendQueued();
437  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
438  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
439  }
440 
444  protected static function loadEmailHelperWithTestCredentials()
445  {
446  Yii::app()->emailHelper->outboundHost = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundHost'];
447  Yii::app()->emailHelper->outboundPort = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundPort'];
448  Yii::app()->emailHelper->outboundUsername = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundUsername'];
449  Yii::app()->emailHelper->outboundPassword = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundPassword'];
450  Yii::app()->emailHelper->outboundSecurity = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundSecurity'];
451  Yii::app()->emailHelper->sendEmailThroughTransport = true;
452  Yii::app()->emailHelper->setOutboundSettings();
453  }
454  }
455 ?>
Definition: Email.php:37
static getMetadata(User $user=null)
Definition: Module.php:502
static getByProcessedAndCampaignId($processed, $campaignId, $pageSize=null)
static getByUsername($username)
Definition: User.php:49
testTooManySendAttemptsResultingInFailure()
testLoadOutboundSettingsFromUserEmailAccount()
static getByUserAndName(User $user, $name=null)
static setDefaultFromAddress($defaultFromAddress)
static loadEmailHelperWithTestCredentials()
static getByName($name)
Definition: Group.php:57
static deleteAll()
static attachFilesToMessage(Array $filesIds, $emailMessage)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static setDefaultTestToAddress($defaultTestToAddress)
testSendQueuedSkipsPausedCampaignMessages()
loadOutboundSettingsFromUserEmailAccount(User $user, $name=null)
static setByModuleName($moduleName, $key, $value, $cache=true)
Generated on Sun Jan 17 2021 07:10:21
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.