Commits

Ivica Nedeljkovic committed ea8ba9a

Change logic how detect if email if forwarded, removing test email forward templates

Comments (0)

Files changed (4)

app/protected/modules/emailMessages/components/EmailArchivingHelper.php

          */
         public static function isMessageForwarded(ImapMessage $emailMessage)
         {
-            $isForwrded = false;
-            foreach (self::$validEmailClientForwardSubjectPrefixes as $forwardSubjectPrefix)
+            $isForwarded = false;
+            foreach ($emailMessage->to as $toAddress)
             {
-                if (stristr($emailMessage->subject, $forwardSubjectPrefix))
+                if ($toAddress['email'] == Yii::app()->imap->imapUsername)
                 {
-                    $isForwrded = true;
+                    $isForwarded = true;
+                    break;
                 }
             }
-            return $isForwrded;
+            return $isForwarded;
         }
 
         /**

app/protected/modules/emailMessages/tests/unit/EmailArchivingHelperTest.php

 
         public function testIsMessageForwarded()
         {
+            Yii::app()->imap->imapUsername = 'dropbox@emample.com';
+
             $imapMessage = new ImapMessage();
-            $imapMessage->subject = "";
+            $imapMessage->subject        = "Test subject";
+            $imapMessage->to[0]['email'] = 'dropbox@emample.com';
+            $imapMessage->fromEmail      = 'steve@example.com';
+            $this->assertTrue(EmailArchivingHelper::isMessageForwarded($imapMessage));
+
+            $imapMessage->to[0]['email'] = 'dropbox@emample.com';
+            $imapMessage->to[1]['email'] = 'john@emample.com';
+            $this->assertTrue(EmailArchivingHelper::isMessageForwarded($imapMessage));
+
+            $imapMessage->to[0]['email'] = 'john@emample.com';
+            $imapMessage->to[1]['email'] = 'dropbox@emample.com';
+            $this->assertTrue(EmailArchivingHelper::isMessageForwarded($imapMessage));
+
+            $imapMessage = new ImapMessage();
+            $imapMessage->subject        = "Test subject";
+            $imapMessage->to[0]['email'] = 'john@emample.com';
+            $imapMessage->cc[0]['email'] = 'dropbox@emample.com';
             $this->assertFalse(EmailArchivingHelper::isMessageForwarded($imapMessage));
 
-            $imapMessage->subject = "Test subject";
+            $imapMessage = new ImapMessage();
+            $imapMessage->subject        = "Test subject";
+            $imapMessage->to[0]['email'] = 'john@emample.com';
+            $imapMessage->cc[0]['email'] = 'peter@emample.com';
+            $imapMessage->cc[1]['email'] = 'dropbox@emample.com';
             $this->assertFalse(EmailArchivingHelper::isMessageForwarded($imapMessage));
 
-            $imapMessage->subject = "Forward subject";
+            // Bcc is not visible when email message is sent to dropbox as Bcc
+            $imapMessage = new ImapMessage();
+            $imapMessage->subject        = "Test subject";
+            $imapMessage->to[0]['email'] = 'john@emample.com';
             $this->assertFalse(EmailArchivingHelper::isMessageForwarded($imapMessage));
-
-            $imapMessage->subject = "Fwd: Test subject";
-            $this->assertTrue(EmailArchivingHelper::isMessageForwarded($imapMessage));
-
-            $imapMessage->subject = "FW: Test subject";
-            $this->assertTrue(EmailArchivingHelper::isMessageForwarded($imapMessage));
-
-            $imapMessage->subject = "fw: Test subject";
-            $this->assertTrue(EmailArchivingHelper::isMessageForwarded($imapMessage));
-
-            $imapMessage->subject = "Fw: Test subject";
-            $this->assertTrue(EmailArchivingHelper::isMessageForwarded($imapMessage));
         }
 
-        /**
-        * @depends testIsMessageForwarded
-        */
         public function testResolveEmailSenderFromForwardedEmailMessage()
         {
             $imapMessage = new ImapMessage();
             $imapMessage->subject = "Test subject";
             $imapMessage->fromEmail = "test@example.com";
 
-            // Outlook format
+            // Outlook, Yahoo, Outlook express format
             $imapMessage->textBody = "
 From: John Smith [mailto:john@example.com]
 Sent: 02 March 2012 AM 01:23
             $this->assertEquals('john@example.com', $sender['email']);
             $this->assertEquals('John Smith', $sender['name']);
 
-            //Google format
+            //Google, Thunderbird format
             $imapMessage->textBody = "
 From: John Smith <john@example.com>
 Date: Thu, Apr 19, 2012 at 5:22 PM
         */
         public function testResolveEmailSenderFromEmailMessage()
         {
+            Yii::app()->imap->imapUsername = 'dropbox@emample.com';
+
             $imapMessage = new ImapMessage();
             $imapMessage->subject = "Test subject";
             $imapMessage->fromEmail = "test@example.com";
+            $imapMessage->cc[0]['email'] = 'dropbox@emample.com';
+            $from = EmailArchivingHelper::resolveEmailSenderFromEmailMessage($imapMessage);
+            $this->assertEquals($imapMessage->fromEmail, $from['email']);
 
-            // Outlook format
+            $imapMessage = new ImapMessage();
+            $imapMessage->fromEmail = "test@example.com";
+            $imapMessage->to[0]['email'] = 'dropbox@emample.com';
+
+            // Outlook, Yahoo, Outlook express format
             $imapMessage->textBody = "
 From: John Smith [mailto:john@example.com]
 Sent: 02 March 2012 AM 01:23
 To: 'Steve Tytler' <steve@example.com>, Peter Smith <peter@example.com>
-Cc: 'John Wein' <john@example.com>, Peter Smith <peter@example.com>
+Cc: Peter Smith <peter@example.com>
 Subject: Hello Steve";
 
-            $from = EmailArchivingHelper::resolveEmailSenderFromEmailMessage($imapMessage);
-            $this->assertEquals($imapMessage->fromEmail, $from['email']);
-
             $imapMessage->subject = "FW: Test subject";
             $from = EmailArchivingHelper::resolveEmailSenderFromEmailMessage($imapMessage);
             $this->assertEquals('john@example.com', $from['email']);
             $this->assertEquals('John Smith', $from['name']);
 
-            //Google format
+            //Google, Thunderbird format
             $imapMessage->textBody = "
 From: John Smith <john@example.com>
 Date: Thu, Apr 19, 2012 at 5:22 PM
             $this->assertEquals('john@example.com', $from['email']);
             $this->assertEquals('John Smith', $from['name']);
 
+
             $imapMessage = new ImapMessage();
             $imapMessage->subject = "Fwd: Test subject";
+            $imapMessage->to[0]['email'] = 'dropbox@emample.com';
             $imapMessage->htmlBody = "
 
             -------- Original Message --------
             $this->assertEquals('John Smith', $from['name']);
 
             $imapMessage = new ImapMessage();
+            $imapMessage->to[0]['email'] = 'dropbox@emample.com';
             $imapMessage->subject = "Fwd: Test subject";
             $imapMessage->textBody = "
 -------- Original Message --------
         */
         public function testResolveEmailRecipientsFromEmailMessage()
         {
+            Yii::app()->imap->imapUsername = 'dropbox@emample.com';
             $imapMessage = new ImapMessage();
             $imapMessage->subject = "Test subject";
             $imapMessage->fromEmail = "test@example.com";
             $this->assertEquals($imapMessage->to, $recipients);
 
             $imapMessage->subject = "FW: Test subject";
+            $imapMessage->to[0]['email'] = 'dropbox@emample.com';
             $recipients = EmailArchivingHelper::resolveEmailRecipientsFromEmailMessage($imapMessage);
             $compareData = array(
                                array(

app/protected/modules/emailMessages/tests/unit/EmailArchivingJobTest.php

         }
 
         /**
-         * Test case when user send email to somebody, and to dropbox(via to field)
-         * This shouldn't happen in reality, because recipient will see that message is sent to dropbox folder too
-         */
-        public function testRunCaseOne()
-        {
-            $super = User::getByUsername('super');
-            $user = User::getByUsername('steve');
-            Yii::app()->imap->connect();
-            Yii::app()->imap->expungeMessages();
-
-            // Check if there are no emails in dropbox
-            $job = new EmailArchivingJob();
-            $this->assertTrue($job->run());
-            $this->assertEquals(0, count(EmailMessage::getAll()));
-            $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
-            $this->assertEquals(0, $imapStats->Nmsgs);
-
-            //Now user send email to another user, and to dropbox
-            $pathToFiles = Yii::getPathOfAlias('application.modules.emailMessages.tests.unit.files');
-            $filePath_1    = $pathToFiles . DIRECTORY_SEPARATOR . 'table.csv';
-            $filePath_2    = $pathToFiles . DIRECTORY_SEPARATOR . 'image.png';
-            $filePath_3    = $pathToFiles . DIRECTORY_SEPARATOR . 'text.txt';
-
-            Yii::app()->emailHelper->sendRawEmail("Email from Steve",
-                                                  $user->primaryEmail->emailAddress,
-                                                  array(
-                                                      Yii::app()->params['emailTestAccounts']['testEmailAddress'],
-                                                      Yii::app()->imap->imapUsername
-                                                  ),
-                                                  'Email from Steve',
-                                                  '<strong>Email</strong> from Steve',
-                                                  null,
-                                                  null,
-                                                  array($filePath_1, $filePath_2, $filePath_3)
-            );
-
-            sleep(30);
-
-            $job = new EmailArchivingJob();
-            $this->assertTrue($job->run());
-
-            $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
-            $this->assertEquals(1, $imapStats->Nmsgs);
-            $this->assertEquals(1, count(EmailMessage::getAll()));
-            $emailMessages = EmailMessage::getAll();
-            $emailMessage = $emailMessages[0];
-
-            $this->assertEquals('Email from Steve', $emailMessage->subject);
-            $this->assertEquals('Email from Steve', trim($emailMessage->content->textContent));
-            $this->assertEquals('<strong>Email</strong> from Steve', trim($emailMessage->content->htmlContent));
-            $this->assertEquals($user->primaryEmail->emailAddress, $emailMessage->sender->fromAddress);
-            $this->assertEquals(2, count($emailMessage->recipients));
-            foreach ($emailMessage->recipients as $recipient)
-            {
-                $this->assertTrue(in_array($recipient->toAddress, array(Yii::app()->params['emailTestAccounts']['testEmailAddress'], Yii::app()->imap->imapUsername)));
-                $this->assertEquals(EmailMessageRecipient::TYPE_TO, $recipient->type);
-            }
-
-            $this->assertEquals(3, count($emailMessage->files));
-            foreach ($emailMessage->files as $attachment)
-            {
-                $this->assertTrue(in_array($attachment->name, array('table.csv', 'image.png', 'text.txt')));
-                $this->assertTrue($attachment->size > 0);
-            }
-        }
-
-        /**
         * Test case when user send email to somebody, and cc to dropbox
         * This shouldn't happen in reality, because recipient will see that message is sent to dropbox folder too
         */
-        public function testRunCaseTwo()
+        public function testRunCaseOne()
         {
             $super = User::getByUsername('super');
             Yii::app()->user->userModel = $super;
         * This is best practictice to be used in reality, because other recipients will not see that user
         * bcc-ed email to dropbox
         */
-        public function testRunCaseThree()
+        public function testRunCaseTwo()
         {
             $super = User::getByUsername('super');
             Yii::app()->user->userModel = $super;
         /**
         * Test case when somebody send email to Zurmo user, and user forward it to dropbox
         */
-        public function testRunCaseFour()
+        public function testRunCaseThree()
         {
             $super = User::getByUsername('super');
             Yii::app()->user->userModel = $super;
             $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
             $this->assertEquals(0, $imapStats->Nmsgs);
 
-            //Now user send email to another user, and to dropbox
             $pathToFiles = Yii::getPathOfAlias('application.modules.emailMessages.tests.unit.files');
             $filePath_1    = $pathToFiles . DIRECTORY_SEPARATOR . 'table.csv';
             $filePath_2    = $pathToFiles . DIRECTORY_SEPARATOR . 'text.txt';
 
-            $originalSubject = "Email from John";
-            $originalTextBody   = "Email from John";
-            $originalHtmlBody   = "<strong>Hi Steve,</strong>. This is John. Bye!";
+            $textBody = "
+---------- Forwarded message ----------
+From: Steve <" . Yii::app()->params['emailTestAccounts']['testEmailAddress'] . ">
+Date: Fri, Jun 8, 2012 at 10:16 AM
+Subject: Email from John
+To: Steve <steve@example.com>
 
-            $forwardedEmailClientSubjectPrefixes = EmailClientForwardTemplatesTestHelper::$subjectPrefixes;
-            $forwardedEmailClientBodyPrefixes    = EmailClientForwardTemplatesTestHelper::$bodyPrefixes;
+Hello Steve
+";
 
-            $numberOfEmailMessages = 0;
-            foreach ($forwardedEmailClientSubjectPrefixes as $client => $subjectPrefixes)
+            $htmlBody = "
+---------- Forwarded message ----------
+From: Steve <" . Yii::app()->params['emailTestAccounts']['testEmailAddress'] . ">
+Date: Fri, Jun 8, 2012 at 10:16 AM
+Subject: Email from John
+To: Steve <steve@example.com>
+
+<strong>Hello</strong> Steve
+";
+
+            //Now user forward email to dropbox
+            $subject = "Fwd: Email from John";
+            Yii::app()->emailHelper->sendRawEmail($subject,
+                                                  $user->primaryEmail->emailAddress,
+                                                  array(Yii::app()->imap->imapUsername),
+                                                  $textBody,
+                                                  $htmlBody,
+                                                  null,
+                                                  null,
+                                                  array($filePath_1, $filePath_2)
+            );
+
+            sleep(10);
+            $job = new EmailArchivingJob();
+            $this->assertTrue($job->run());
+
+            $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
+            $this->assertEquals(1, $imapStats->Nmsgs);
+            $this->assertEquals(1, count(EmailMessage::getAll()));
+            $emailMessages = EmailMessage::getAll();
+            $emailMessage = $emailMessages[0];
+
+            $this->assertEquals($subject, $emailMessage->subject);
+            $this->assertTrue(strpos($emailMessage->content->textContent, 'Hello Steve') !== false);
+            $this->assertTrue(strpos($emailMessage->content->htmlContent, '<strong>Hello</strong> Steve') !== false);
+            $this->assertEquals(Yii::app()->params['emailTestAccounts']['testEmailAddress'], $emailMessage->sender->fromAddress);
+            $this->assertEquals($user->primaryEmail->emailAddress, $emailMessage->recipients[0]->toAddress);
+
+            $this->assertEquals(2, count($emailMessage->files));
+            foreach ($emailMessage->files as $attachment)
             {
-                $this->assertTrue(isset($forwardedEmailClientBodyPrefixes[$client]) ||
-                                  !empty($forwardedEmailClientBodyPrefixes[$client])
-                );
-                $bodyPrefixes = $forwardedEmailClientBodyPrefixes[$client];
-                // Test all subject/body prefix combinations
-                foreach ($subjectPrefixes as $subjectPrefix)
-                {
-                    foreach ($bodyPrefixes as $bodyPrefix)
-                    {
-                        $this->assertTrue($subjectPrefix != '');
-                        $this->assertTrue($bodyPrefix    != '');
-                        $bodyPrefix = str_replace("FROM_NAME", "John Smith", $bodyPrefix);
-                        $bodyPrefix = str_replace("FROM_EMAIL", Yii::app()->params['emailTestAccounts']['testEmailAddress'], $bodyPrefix);
-
-                        // Expunge all emails from dropbox
-                        Yii::app()->imap->expungeMessages();
-                        // Check if there are no emails in dropbox
-                        $job = new EmailArchivingJob();
-                        $this->assertTrue($job->run());
-                        $this->assertEquals($numberOfEmailMessages, count(EmailMessage::getAll()));
-                        $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
-                        $this->assertEquals(0, $imapStats->Nmsgs);
-
-                        $subject = $subjectPrefix . " " . $originalSubject;
-                        $textBody = $bodyPrefix . $originalTextBody;
-                        $htmlBody = $bodyPrefix . $originalHtmlBody;
-                        Yii::app()->emailHelper->sendRawEmail($subject,
-                                                              $user->primaryEmail->emailAddress,
-                                                              array(Yii::app()->imap->imapUsername),
-                                                              $textBody,
-                                                              $htmlBody,
-                                                              null, null,
-                                                              array($filePath_1, $filePath_2));
-
-                        sleep(30);
-                        $job = new EmailArchivingJob();
-                        $this->assertTrue($job->run());
-
-                        $imapStats = Yii::app()->imap->getMessageBoxStatsDetailed();
-                        $this->assertEquals(1, $imapStats->Nmsgs);
-                        $numberOfEmailMessages++;
-                        $this->assertEquals($numberOfEmailMessages, count(EmailMessage::getAll()));
-                        $emailMessages = EmailMessage::getAll();
-                        $emailMessage = $emailMessages[$numberOfEmailMessages - 1];
-
-
-                        $this->assertEquals($subject, $emailMessage->subject);
-                        $this->assertEquals(Yii::app()->params['emailTestAccounts']['testEmailAddress'], $emailMessage->sender->fromAddress);
-                        $this->assertEquals($user->primaryEmail->emailAddress, $emailMessage->recipients[0]->toAddress);
-
-                        $this->assertEquals(2, count($emailMessage->files));
-                        foreach ($emailMessage->files as $attachment)
-                        {
-                            $this->assertTrue(in_array($attachment->name, array('table.csv', 'text.txt')));
-                            $this->assertTrue($attachment->size > 0);
-                        }
-                    }
-                }
+                $this->assertTrue(in_array($attachment->name, array('table.csv', 'text.txt')));
+                $this->assertTrue($attachment->size > 0);
             }
         }
 
         * Test case when sender email is not user primary email.
         * In this case system should send email to user.
         */
-        public function testRunCaseFive()
+        public function testRunCaseFour()
         {
             $super = User::getByUsername('super');
             Yii::app()->user->userModel = $super;
         /**
         * Check if only new messages are pulled from dropdown
         */
-        public function testRunCaseSix()
+        public function testRunCaseFive()
         {
             $super = User::getByUsername('super');
             Yii::app()->user->userModel = $super;

app/protected/modules/emailMessages/tests/unit/EmailClientForwardTemplatesTestHelper.php

-<?php
-    /*********************************************************************************
-     * Zurmo is a customer relationship management program developed by
-     * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
-     *
-     * Zurmo is free software; you can redistribute it and/or modify it under
-     * the terms of the GNU General Public License version 3 as published by the
-     * Free Software Foundation with the addition of the following permission added
-     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
-     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
-     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
-     *
-     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
-     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-     * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
-     * details.
-     *
-     * You should have received a copy of the GNU General Public License along with
-     * this program; if not, see http://www.gnu.org/licenses or write to the Free
-     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-     * 02110-1301 USA.
-     *
-     * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
-     * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
-     ********************************************************************************/
-
-    class EmailClientForwardTemplatesTestHelper
-    {
-        public static $subjectPrefixes = array(
-            'Gmail' => array('Fwd:'),
-            'Outlook' => array ('FW:'),
-            'OutlookExpress' => array ('FW:'),
-            'ThunderBird' => array('Fwd:'),
-            'Yahoo' => array ('Fw:'),
-            'iPhone' => array ('Fw:'),   // iPad is exactly the same
-            'OutlookMac' => array ('Fw:'),
-            'AppleMail' => array ('Fw:'),
-        );
-
-        public static $bodyPrefixes = array(
-            'Gmail' => array(<<<EOD
----------- Forwarded message ----------
-From: FROM_NAME <FROM_EMAIL>
-Date: Fri, Jun 8, 2012 at 10:16 AM
-Subject: Email from John
-To: Steve <steve@example.com>
-EOD
-            ),
-            'Outlook' => array(<<<EOD
------Original Message-----
-From: FROM_NAME [mailto:FROM_EMAIL]
-Sent: Thursday, May 03, 2012 8:52 AM
-To: 'Steve'
-Subject: Email from John
-EOD
-            ),
-            'OutlookExpress' => array(<<<EOD
------Original Message-----
-From: FROM_NAME [mailto:FROM_EMAIL]
-Sent: Thursday, May 03, 2012 8:52 AM
-To: 'Steve'
-Subject: Email from John
-EOD
-            ),
-            'ThunderBird' => array(<<<EOD
--------- Original Message --------
-Subject: 	Email from John
-Date: 	Thu, 7 Jun 2012 10:17:07 -0500
-From: 	FROM_NAME <FROM_EMAIL>
-Reply-To: 	<FROM_EMAIL>
-EOD
-            ),
-            'Yahoo' => array(<<<EOD
---- On Fri, 6/8/12, FROM_NAME <FROM_EMAIL> wrote:
-
-From: FROM_NAME <FROM_EMAIL>
-Subject: Email from John
-To: "Steve" <steve@example.com>
-Date: Friday, June 8, 2012, 6:15 AM
-EOD
-            ),
-            'iPhone' => array(<<<EOD
-Begin forwarded message:
-
-From: FROM_NAME <FROM_EMAIL>
-Date: June 13, 2012 7:20:39 PM EDT
-To: Steve <steve@example.com>
-Subject: Re: Email from John
-EOD
-            ),
-           'OutlookMac' => array(<<<EOD
-On 6/13/12 11:18 AM, FROM_NAME <FROM_EMAIL> wrote:
-EOD
-            ),
-           'AppleMail' => array(<<<EOD
-Begin forwarded message:
-
-From: FROM_NAME <FROM_EMAIL>
-Subject: Email from John
-Date: June 13, 2012 11:18:32 AM EDT
-To: Steve <steve@example.com>
-EOD
-            ),
-        );
-    }
-?>