Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
EmailArchivingJobTest.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 $userMailer;
40  public static $userImap;
41  public static $emailHelperSendEmailThroughTransport;
42 
43  public static function setUpBeforeClass()
44  {
45  parent::setUpBeforeClass();
46  SecurityTestHelper::createSuperAdmin();
47  self::$emailHelperSendEmailThroughTransport = Yii::app()->emailHelper->sendEmailThroughTransport;
48 
49  if (EmailMessageTestHelper::isSetEmailAccountsTestConfiguration())
50  {
51  UserTestHelper::createBasicUser('aaa');
52  UserTestHelper::createBasicUser('bbb');
53  UserTestHelper::createBasicUser('ccc');
54  UserTestHelper::createBasicUser('ddr');
55  $steve = UserTestHelper::createBasicUser('steve');
56  UserTestHelper::createBasicUser('eeer');
57  UserTestHelper::createBasicUser('ffrr');
58  UserTestHelper::createBasicUser('john');
59 
61  $steve->setRight('ContactsModule', ContactsModule::RIGHT_ACCESS_CONTACTS);
62  $steve->setRight('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS);
63  $steve->save();
64 
65  Yii::app()->imap->imapHost = Yii::app()->params['emailTestAccounts']['dropboxImapSettings']['imapHost'];
66  Yii::app()->imap->imapUsername = Yii::app()->params['emailTestAccounts']['dropboxImapSettings']['imapUsername'];
67  Yii::app()->imap->imapPassword = Yii::app()->params['emailTestAccounts']['dropboxImapSettings']['imapPassword'];
68  Yii::app()->imap->imapPort = Yii::app()->params['emailTestAccounts']['dropboxImapSettings']['imapPort'];
69  Yii::app()->imap->imapSSL = Yii::app()->params['emailTestAccounts']['dropboxImapSettings']['imapSSL'];
70  Yii::app()->imap->imapFolder = Yii::app()->params['emailTestAccounts']['dropboxImapSettings']['imapFolder'];
71  Yii::app()->imap->setInboundSettings();
72  Yii::app()->imap->init();
73 
74  Yii::app()->emailHelper->outboundHost = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundHost'];
75  Yii::app()->emailHelper->outboundPort = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundPort'];
76  Yii::app()->emailHelper->outboundUsername = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundUsername'];
77  Yii::app()->emailHelper->outboundPassword = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundPassword'];
78  Yii::app()->emailHelper->outboundSecurity = Yii::app()->params['emailTestAccounts']['smtpSettings']['outboundSecurity'];
79  Yii::app()->emailHelper->sendEmailThroughTransport = true;
80  Yii::app()->emailHelper->setOutboundSettings();
81 
82  self::$userMailer['outboundHost'] = Yii::app()->params['emailTestAccounts']['userSmtpSettings']['outboundHost'];
83  self::$userMailer['outboundPort'] = Yii::app()->params['emailTestAccounts']['userSmtpSettings']['outboundPort'];
84  self::$userMailer['outboundUsername'] = Yii::app()->params['emailTestAccounts']['userSmtpSettings']['outboundUsername'];
85  self::$userMailer['outboundPassword'] = Yii::app()->params['emailTestAccounts']['userSmtpSettings']['outboundPassword'];
86  self::$userMailer['outboundSecurity'] = Yii::app()->params['emailTestAccounts']['userSmtpSettings']['outboundSecurity'];
87  }
88  }
89 
90  public function setup()
91  {
92  ForgetAllCacheUtil::forgetAllCaches(); //Special situation to deal with email box and ensuring the user cache doesn't get corrupted
93  parent::setup();
94  $super = User::getByUsername('super');
95  Yii::app()->user->userModel = $super;
96 
97  if (EmailMessageTestHelper::isSetEmailAccountsTestConfiguration())
98  {
99  $user = User::getByUsername('steve');
100  $user->primaryEmail->emailAddress = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapUsername'];
101  $this->assertTrue($user->save());
102  }
103  else
104  {
105  $this->markTestSkipped(Zurmo::t('EmailMessagesModule', 'Test email settings are not configured in perInstanceTest.php file.'));
106  }
107  }
108 
109  public static function tearDownAfterClass()
110  {
111  $imap = new ZurmoImap();
112  $imap->imapHost = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapHost'];
113  $imap->imapUsername = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapUsername'];
114  $imap->imapPassword = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapPassword'];
115  $imap->imapPort = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapPort'];
116  $imap->imapSSL = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapSSL'];
117  $imap->imapFolder = Yii::app()->params['emailTestAccounts']['userImapSettings']['imapFolder'];
118  $imap->init();
119  $imap->connect();
120  $imap->deleteMessages(true);
121 
122  Yii::app()->emailHelper->sendEmailThroughTransport = self::$emailHelperSendEmailThroughTransport;
123  parent::tearDownAfterClass();
124  }
125 
130  public function testRunCaseOne()
131  {
132  $super = User::getByUsername('super');
133  Yii::app()->user->userModel = $super;
134  $user = User::getByUsername('steve');
135  Yii::app()->imap->connect();
136  $this->assertEquals(0, Notification::getCountByTypeAndUser('EmailMessageArchivingEmailAddressNotMatching', Yii::app()->user->userModel));
137 
139  Yii::app()->imap->deleteMessages(true);
140 
141  // Check if there are no emails in dropbox
142  $job = new EmailArchivingJob();
143  $this->assertTrue($job->run());
144  $this->assertEquals(0, EmailMessage::getCount());
145  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
146  $this->assertEquals(0, $imapStats->Nmsgs);
147 
148  //Now user send email to another user, and to dropbox
149  $pathToFiles = Yii::getPathOfAlias('application.modules.emailMessages.tests.unit.files');
150  $filePath_1 = $pathToFiles . DIRECTORY_SEPARATOR . 'table.csv';
151  $filePath_2 = $pathToFiles . DIRECTORY_SEPARATOR . 'image.png';
152  $filePath_3 = $pathToFiles . DIRECTORY_SEPARATOR . 'text.txt';
153  Yii::app()->emailHelper->sendRawEmail("Email from Steve",
154  $user->primaryEmail->emailAddress,
155  Yii::app()->params['emailTestAccounts']['testEmailAddress'],
156  'Email from Steve',
157  '<strong>Email</strong> from Steve',
158  array(Yii::app()->imap->imapUsername),
159  null,
160  array($filePath_1, $filePath_2, $filePath_3),
161  self::$userMailer
162  );
163  sleep(60);
164  $job = new EmailArchivingJob();
165  $this->assertTrue($job->run());
166 
167  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
168  $this->assertEquals(0, $imapStats->Nmsgs);
169  $this->assertEquals(3, EmailMessage::getCount());
170  $emailMessages = EmailMessage::getAll();
171 
172  //Message with unmatched notification
173  $emailMessage = $emailMessages[1];
174  $this->assertEquals('Match archived emails', $emailMessage->subject);
175  $this->assertContains('At least one archived email message does not match any records in the system', trim($emailMessage->content->textContent));
176  $this->assertContains('At least one archived email message does not match any records in the system', trim($emailMessage->content->htmlContent));
177 
178  $emailMessage = $emailMessages[2];
179 
180  $this->assertEquals('Email from Steve', $emailMessage->subject);
181  $this->assertEquals('Email from Steve', trim($emailMessage->content->textContent));
182  $this->assertEquals('<!-- zurmo css inline --><strong>Email</strong> from Steve', preg_replace( "/\r|\n/", "", $emailMessage->content->htmlContent));
183  $this->assertEquals($user->primaryEmail->emailAddress, $emailMessage->sender->fromAddress);
184 
185  $this->assertEquals(1, count($emailMessage->recipients));
186  $recipient = $emailMessage->recipients[0];
187  $this->assertEquals($recipient->toAddress, Yii::app()->params['emailTestAccounts']['testEmailAddress']);
188  $this->assertEquals(EmailMessageRecipient::TYPE_TO, $recipient->type);
189 
190  $this->assertEquals(3, count($emailMessage->files));
191  foreach ($emailMessage->files as $attachment)
192  {
193  $this->assertTrue(in_array($attachment->name, array('table.csv', 'image.png', 'text.txt')));
194  $this->assertTrue($attachment->size > 0);
195  }
196  $this->assertEquals(EmailFolder::TYPE_ARCHIVED_UNMATCHED, $emailMessage->folder->type);
197  $this->assertEquals(1, Notification::getCountByTypeAndUser('EmailMessageArchivingEmailAddressNotMatching', $user));
198  }
199 
207  public function testRunCaseTwo()
208  {
209  $super = User::getByUsername('super');
210  Yii::app()->user->userModel = $super;
211  $user = User::getByUsername('steve');
212  Yii::app()->imap->connect();
213  //There is one notification from testRunCaseOne
214  $this->assertEquals(1, Notification::getCountByTypeAndUser('EmailMessageArchivingEmailAddressNotMatching', $user));
215 
217  Yii::app()->imap->deleteMessages(true);
218 
219  // Check if there are no emails in dropbox
220  $job = new EmailArchivingJob();
221  $this->assertTrue($job->run());
222  $this->assertEquals(0, EmailMessage::getCount());
223  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
224  $this->assertEquals(0, $imapStats->Nmsgs);
225 
226  //Now user send email to another user, and to dropbox
227  $pathToFiles = Yii::getPathOfAlias('application.modules.emailMessages.tests.unit.files');
228  $filePath_1 = $pathToFiles . DIRECTORY_SEPARATOR . 'table.csv';
229  $filePath_2 = $pathToFiles . DIRECTORY_SEPARATOR . 'image.png';
230  $filePath_3 = $pathToFiles . DIRECTORY_SEPARATOR . 'text.txt';
231 
232  Yii::app()->emailHelper->sendRawEmail("Email from Steve 2",
233  $user->primaryEmail->emailAddress,
234  Yii::app()->params['emailTestAccounts']['testEmailAddress'],
235  'Email from Steve',
236  '<strong>Email</strong> from Steve',
237  null,
238  array(Yii::app()->imap->imapUsername),
239  array($filePath_1, $filePath_2, $filePath_3),
240  self::$userMailer
241  );
242 
243  sleep(60);
244  $job = new EmailArchivingJob();
245  $this->assertTrue($job->run());
246  sleep(60);
247 
248  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
249  $this->assertEquals(0, $imapStats->Nmsgs);
250  $this->assertEquals(2, EmailMessage::getCount());
251  $emailMessages = EmailMessage::getAll();
252  $emailMessage = $emailMessages[1];
253 
254  $this->assertEquals('Email from Steve 2', $emailMessage->subject);
255  $this->assertEquals('Email from Steve', trim($emailMessage->content->textContent));
256  $this->assertEquals('<!-- zurmo css inline --><strong>Email</strong> from Steve', preg_replace( "/\r|\n/", "", $emailMessage->content->htmlContent));
257  $this->assertEquals($user->primaryEmail->emailAddress, $emailMessage->sender->fromAddress);
258 
259  $this->assertEquals(1, count($emailMessage->recipients));
260  $recipient = $emailMessage->recipients[0];
261  $this->assertEquals($recipient->toAddress, Yii::app()->params['emailTestAccounts']['testEmailAddress']);
262  $this->assertEquals(EmailMessageRecipient::TYPE_TO, $recipient->type);
263 
264  $this->assertEquals(3, count($emailMessage->files));
265  foreach ($emailMessage->files as $attachment)
266  {
267  $this->assertTrue(in_array($attachment->name, array('table.csv', 'image.png', 'text.txt')));
268  $this->assertTrue($attachment->size > 0);
269  }
270  $this->assertEquals(EmailFolder::TYPE_ARCHIVED_UNMATCHED, $emailMessage->folder->type);
271  //No duplicate notification is created
272  $this->assertEquals(1, Notification::getCountByTypeAndUser('EmailMessageArchivingEmailAddressNotMatching', $user));
273  }
274 
280  public function testRunCaseThree()
281  {
282  $super = User::getByUsername('super');
283  Yii::app()->user->userModel = $super;
284  $user = User::getByUsername('steve');
285  Yii::app()->imap->connect();
286 
288  Yii::app()->imap->deleteMessages(true);
289 
290  // Check if there are no emails in dropbox
291  $job = new EmailArchivingJob();
292  $this->assertTrue($job->run());
293  $this->assertEquals(0, EmailMessage::getCount());
294  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
295  $this->assertEquals(0, $imapStats->Nmsgs);
296 
297  $pathToFiles = Yii::getPathOfAlias('application.modules.emailMessages.tests.unit.files');
298  $filePath_1 = $pathToFiles . DIRECTORY_SEPARATOR . 'table.csv';
299  $filePath_2 = $pathToFiles . DIRECTORY_SEPARATOR . 'text.txt';
300 
301  // Begin Not Coding Standard
302  $textBody = "
303 ---------- Forwarded message ----------
304 From: Steve <" . Yii::app()->params['emailTestAccounts']['testEmailAddress'] . ">
305 Date: Fri, Jun 8, 2012 at 10:16 AM
306 Subject: Email from John
307 To: Steve <steve@example.com>
308 
309 Hello Steve
310 ";
311 
312  $htmlBody = "
313 ---------- Forwarded message ----------
314 From: Steve <" . Yii::app()->params['emailTestAccounts']['testEmailAddress'] . ">
315 Date: Fri, Jun 8, 2012 at 10:16 AM
316 Subject: Email from John
317 To: Steve <steve@example.com>
318 
319 <strong>Hello</strong> Steve
320 ";
321  // End Not Coding Standard
322  //Now user forward email to dropbox
323  $subject = "Fwd: Email from John";
324  Yii::app()->emailHelper->sendRawEmail($subject,
325  $user->primaryEmail->emailAddress,
326  array(Yii::app()->imap->imapUsername),
327  $textBody,
328  $htmlBody,
329  null,
330  null,
331  array($filePath_1, $filePath_2),
332  self::$userMailer
333  );
334 
335  sleep(10);
336  $job = new EmailArchivingJob();
337  $this->assertTrue($job->run());
338 
339  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
340  $this->assertEquals(0, $imapStats->Nmsgs);
341  $this->assertEquals(2, EmailMessage::getCount());
342  $emailMessages = EmailMessage::getAll();
343  $emailMessage = $emailMessages[1];
344 
345  $this->assertEquals($subject, $emailMessage->subject);
346  $this->assertContains('Hello Steve', $emailMessage->content->textContent);
347  $this->assertContains('<strong>Hello</strong> Steve', $emailMessage->content->htmlContent);
348  $this->assertEquals(Yii::app()->params['emailTestAccounts']['testEmailAddress'], $emailMessage->sender->fromAddress);
349  $this->assertEquals($user->primaryEmail->emailAddress, $emailMessage->recipients[0]->toAddress);
350 
351  $this->assertEquals(2, count($emailMessage->files));
352  foreach ($emailMessage->files as $attachment)
353  {
354  $this->assertTrue(in_array($attachment->name, array('table.csv', 'text.txt')));
355  $this->assertTrue($attachment->size > 0);
356  }
357  $this->assertEquals(EmailFolder::TYPE_ARCHIVED_UNMATCHED, $emailMessage->folder->type);
358  $this->assertEquals(1, Notification::getCountByTypeAndUser('EmailMessageArchivingEmailAddressNotMatching', $user));
359  }
360 
367  public function testRunCaseFour()
368  {
369  $super = User::getByUsername('super');
370  Yii::app()->user->userModel = $super;
371  $user = User::getByUsername('steve');
372  Yii::app()->imap->connect();
373 
375  Yii::app()->imap->deleteMessages(true);
376 
377  // Check if there are no emails in dropbox
378  $job = new EmailArchivingJob();
379  $this->assertTrue($job->run());
380  $this->assertEquals(0, EmailMessage::getCount());
381  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
382  $this->assertEquals(0, $imapStats->Nmsgs);
383 
384  Yii::app()->emailHelper->sendRawEmail("Email from invalid user",
385  $user->primaryEmail->emailAddress,
386  array(Yii::app()->imap->imapUsername),
387  'Some content here',
388  '<strong>Some</strong> content here',
389  null,
390  null,
391  null,
392  self::$userMailer);
393 
394  // Change user email address.
395  $originalUserAddress = $user->primaryEmail->emailAddress;
396  $user = User::getByUsername('steve');
397  $user->primaryEmail->emailAddress = Yii::app()->params['emailTestAccounts']['testEmailAddress'];
398  $this->assertTrue($user->save());
399 
400  sleep(30);
401  $job = new EmailArchivingJob();
402  $this->assertFalse($job->run());
403  $this->assertContains('Failed to process Message id', $job->getErrorMessage());
404 
405  $this->assertEquals(1, EmailMessage::getCount());
406  $this->assertEquals(1, Notification::getCountByTypeAndUser('EmailMessageOwnerNotExist', $super));
407  $notifications = Notification::getByTypeAndUser('EmailMessageOwnerNotExist', $super);
408  $this->assertContains('Email address does not exist in system', $notifications[0]->notificationMessage->textContent);
409  $this->assertContains('Email address does not exist in system', $notifications[0]->notificationMessage->htmlContent);
410  $this->assertEquals(1, Notification::getCountByTypeAndUser('EmailMessageArchivingEmailAddressNotMatching', $user));
411  }
412 
419  public function testRunCaseFive()
420  {
421  $super = User::getByUsername('super');
422  Yii::app()->user->userModel = $super;
423  $user = User::getByUsername('steve');
424  Yii::app()->imap->connect();
425 
426  ContactTestHelper::createContactByNameForOwner('contact', $user);
427  $contacts = Contact::getByName('contact contactson');
428  $contacts[0]->primaryEmail->emailAddress = Yii::app()->params['emailTestAccounts']['testEmailAddress'];
429  $this->assertTrue($contacts[0]->save());
430 
431  AccountTestHelper::createAccountByNameForOwner('account', $user);
432  $accounts = Account::getByName('account');
433  $accounts[0]->primaryEmail->emailAddress = Yii::app()->params['emailTestAccounts']['testEmailAddress'];
434  $this->assertTrue($accounts[0]->save());
435 
436  $john = User::getByUsername('john');
437  $john->primaryEmail->emailAddress = Yii::app()->params['emailTestAccounts']['testEmailAddress'];
438  $this->assertTrue($john->save());
439 
441  Yii::app()->imap->deleteMessages(true);
442 
443  // Check if there are no emails in dropbox
444  $job = new EmailArchivingJob();
445  $this->assertTrue($job->run());
446  $this->assertEquals(0, EmailMessage::getCount());
447  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
448  $this->assertEquals(0, $imapStats->Nmsgs);
449 
450  //Now user send email to another user, and to dropbox
451  $pathToFiles = Yii::getPathOfAlias('application.modules.emailMessages.tests.unit.files');
452 
453  Yii::app()->emailHelper->sendRawEmail("Email from Steve 3",
454  $user->primaryEmail->emailAddress,
455  array(Yii::app()->params['emailTestAccounts']['testEmailAddress']),
456  'Email from Steve',
457  '<strong>Email</strong> from Steve',
458  null,
459  array(Yii::app()->imap->imapUsername),
460  null,
461  self::$userMailer
462  );
463 
464  sleep(30);
465 
466  $job = new EmailArchivingJob();
467  $this->assertTrue($job->run());
468 
469  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
470  $this->assertEquals(0, $imapStats->Nmsgs);
471  $this->assertEquals(2, EmailMessage::getCount());
472  $emailMessages = EmailMessage::getAll();
473  $emailMessage = $emailMessages[1];
474 
475  $this->assertEquals('Email from Steve 3', $emailMessage->subject);
476  $this->assertEquals('Email from Steve', trim($emailMessage->content->textContent));
477  $this->assertEquals('<!-- zurmo css inline --><strong>Email</strong> from Steve', preg_replace( "/\r|\n/", "", $emailMessage->content->htmlContent));
478  $this->assertEquals($user->primaryEmail->emailAddress, $emailMessage->sender->fromAddress);
479 
480  $this->assertEquals(1, count($emailMessage->recipients));
481  $recipient = $emailMessage->recipients[0];
482  $this->assertCount(3, $recipient->personsOrAccounts);
483  $this->assertEquals($recipient->toAddress, Yii::app()->params['emailTestAccounts']['testEmailAddress']);
484  $this->assertEquals(EmailMessageRecipient::TYPE_TO, $recipient->type);
485  $this->assertEquals(EmailFolder::TYPE_ARCHIVED, $emailMessage->folder->type);
486 
487  $job = new EmailArchivingJob();
488  $this->assertTrue($job->run());
489 
490  $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
491  $this->assertEquals(0, $imapStats->Nmsgs);
492  $this->assertEquals(2, EmailMessage::getCount());
493  $this->assertEquals(1, Notification::getCountByTypeAndUser('EmailMessageArchivingEmailAddressNotMatching', $user));
494  }
495  }
496 ?>
static getByUsername($username)
Definition: User.php:49
static loadStartingData()
static getCountByTypeAndUser($type, User $user)
static deleteAll()
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getByTypeAndUser($type, User $user)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Generated on Thu Aug 6 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.