public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     SecurityTestHelper::createSuperAdmin();
     $nobody = UserTestHelper::createBasicUser('nobody');
     $nobody->setRight('MarketingListsModule', MarketingListsModule::getAccessRight());
     $saved = $nobody->save();
     static::assertTrue($saved);
     Yii::app()->user->userModel = $nobody;
     //Setup test data owned by the super user.
     $account = AccountTestHelper::createAccountByNameForOwner('nobodyAccount', $nobody);
     $marketingList1 = MarketingListTestHelper::createMarketingListByName('MarketingList1', 'MarketingList Description1');
     $marketingList2 = MarketingListTestHelper::createMarketingListByName('MarketingList2', 'MarketingList Description2');
     for ($i = 0; $i < 17; $i++) {
         if ($i % 2) {
             $unsubscribed = 0;
         } else {
             $unsubscribed = 1;
         }
         $contact1 = ContactTestHelper::createContactWithAccountByNameForOwner('nobodyContact1' . $i, $nobody, $account);
         $contact2 = ContactTestHelper::createContactWithAccountByNameForOwner('nobodyContact2' . $i, $nobody, $account);
         MarketingListMemberTestHelper::createMarketingListMember($unsubscribed, $marketingList1, $contact1);
         MarketingListMemberTestHelper::createMarketingListMember($unsubscribed, $marketingList2, $contact2);
     }
     AllPermissionsOptimizationUtil::rebuild();
 }
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     SecurityTestHelper::createSuperAdmin();
     SecurityTestHelper::createUsers();
     // set up data owned by super
     Yii::app()->user->userModel = User::getByUsername('super');
     $account1 = AccountTestHelper::createAccountByNameForOwner('account1', Yii::app()->user->userModel);
     $contact1 = ContactTestHelper::createContactWithAccountByNameForOwner('contact1', Yii::app()->user->userModel, $account1);
     $contact2 = ContactTestHelper::createContactWithAccountByNameForOwner('contact2', Yii::app()->user->userModel, $account1);
     $contact3 = ContactTestHelper::createContactWithAccountByNameForOwner('contact3', Yii::app()->user->userModel, $account1);
     $marketingList1 = MarketingListTestHelper::createMarketingListByName('MarketingList1');
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList1, $contact1);
     MarketingListMemberTestHelper::createMarketingListMember(1, $marketingList1, $contact2);
     $member1 = MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList1, $contact3);
     static::$superUserMarketingListId = $marketingList1->id;
     static::$superUserMemberId = $member1->id;
     // set up data owned by nobody
     Yii::app()->user->userModel = UserTestHelper::createBasicUser('nobody');
     $account2 = AccountTestHelper::createAccountByNameForOwner('account2', Yii::app()->user->userModel);
     $contact4 = ContactTestHelper::createContactWithAccountByNameForOwner('contact4', Yii::app()->user->userModel, $account2);
     $contact5 = ContactTestHelper::createContactWithAccountByNameForOwner('contact5', Yii::app()->user->userModel, $account2);
     $contact6 = ContactTestHelper::createContactWithAccountByNameForOwner('contact6', Yii::app()->user->userModel, $account2);
     $marketingList2 = MarketingListTestHelper::createMarketingListByName('MarketingList2');
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList2, $contact4);
     $member2 = MarketingListMemberTestHelper::createMarketingListMember(1, $marketingList2, $contact5);
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList2, $contact6);
     static::$regularUserMarketingListId = $marketingList2->id;
     static::$regularUserMemberId = $member2->id;
     ReadPermissionsOptimizationUtil::rebuild();
 }
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     SecurityTestHelper::createSuperAdmin();
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $account = AccountTestHelper::createAccountByNameForOwner('account', $super);
     static::$contact = ContactTestHelper::createContactWithAccountByNameForOwner('contact', $super, $account);
     static::$marketingList = MarketingListTestHelper::createMarketingListByName('marketingList', 'description', 'fromName', '*****@*****.**', true);
     static::$marketingListMember = MarketingListMemberTestHelper::createMarketingListMember(1, static::$marketingList, static::$contact);
     AllPermissionsOptimizationUtil::rebuild();
 }
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     SecurityTestHelper::createSuperAdmin();
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     //Setup test data owned by the super user.
     $account = AccountTestHelper::createAccountByNameForOwner('superAccount', $super);
     $marketingList1 = MarketingListTestHelper::createMarketingListByName('MarketingList1', 'MarketingList Description1');
     $marketingList2 = MarketingListTestHelper::createMarketingListByName('MarketingList2', 'MarketingList Description2');
     for ($i = 0; $i < 17; $i++) {
         if ($i % 2) {
             $unsubscribed = 0;
         } else {
             $unsubscribed = 1;
         }
         $contact1 = ContactTestHelper::createContactWithAccountByNameForOwner('superContact1' . $i, $super, $account);
         $contact2 = ContactTestHelper::createContactWithAccountByNameForOwner('superContact2' . $i, $super, $account);
         MarketingListMemberTestHelper::createMarketingListMember($unsubscribed, $marketingList1, $contact1);
         MarketingListMemberTestHelper::createMarketingListMember($unsubscribed, $marketingList2, $contact2);
         AllPermissionsOptimizationUtil::rebuild();
     }
 }
 /**
  * @depends testRunWithMultipleRecipientsWithAttachments
  */
 public function testRunWithSubscribedAndUnsubscribedMembers()
 {
     $campaignItemsCountBefore = CampaignItem::getCount();
     $job = new CampaignQueueMessagesInOutboxJob();
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 10');
     $marketingListId = $marketingList->id;
     $contacts = array();
     for ($i = 0; $i < 5; $i++) {
         $email = new Email();
         $email->emailAddress = "demo{$i}@zurmo.com";
         $contact = ContactTestHelper::createContactByNameForOwner('contact 0' . ($i + 14), $this->user);
         $contact->primaryEmail = $email;
         $this->assertTrue($contact->save());
         $contacts[] = $contact;
         MarketingListMemberTestHelper::createMarketingListMember($i % 2, $marketingList, $contact);
     }
     $marketingList->forgetAll();
     $marketingList = MarketingList::getById($marketingListId);
     $campaign = CampaignTestHelper::createCampaign('campaign 10', 'subject 10', '[[FIRST^NAME]]', '[[LAST^NAME]]', null, null, null, Campaign::STATUS_PROCESSING, null, 0, $marketingList);
     $this->assertNotNull($campaign);
     $campaignId = $campaign->id;
     foreach ($contacts as $contact) {
         CampaignItemTestHelper::createCampaignItem(0, $campaign, $contact);
     }
     $this->assertTrue($job->run());
     $campaign = Campaign::getById($campaignId);
     $campaignItemsCountExpected = $campaignItemsCountBefore + 5;
     $campaignItemsCountAfter = CampaignItem::getCount();
     $this->assertEquals($campaignItemsCountExpected, $campaignItemsCountAfter);
     $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
     $this->assertCount(5, $campaignItemsProcessed);
     $skippedCount = 0;
     foreach ($campaignItemsProcessed as $campaignItem) {
         $contact = $campaignItem->contact;
         $member = MarketingListMember::getByMarketingListIdContactIdAndUnsubscribed($campaign->marketingList->id, $contact->id, true);
         if ($member) {
             $personId = $campaignItem->contact->getClassId('Person');
             $type = CampaignItemActivity::TYPE_SKIP;
             $activity = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl($type, $campaignItem->id, $personId);
             $this->assertNotNull($activity);
             $this->assertNotEmpty($activity);
             $this->assertCount(1, $activity);
             $skippedCount++;
         } else {
             $this->assertNotEmpty($campaignItem->emailMessage);
             $this->assertContains($contact->firstName, $campaignItem->emailMessage->content->textContent);
             $this->assertContains($contact->lastName, $campaignItem->emailMessage->content->htmlContent);
         }
     }
     $this->assertEquals(2, $skippedCount);
 }
 public function testToggleUnsubscribed()
 {
     $marketingList = MarketingListTestHelper::createMarketingListByName('MarketingList4', 'MarketingList Description4');
     $this->assertNotNull($marketingList);
     $contact = RandomDataUtil::getRandomValueFromArray(Contact::getAll());
     $this->assertNotEmpty($contact);
     $previousUnsubcribedValue = 1;
     $marketingListMember = MarketingListMemberTestHelper::createMarketingListMember($previousUnsubcribedValue, $marketingList, $contact);
     $marketingListMemberId = $marketingListMember->id;
     $this->assertNotNull($marketingListMember);
     $this->setGetArray(array('id' => $marketingListMemberId));
     $content = $this->runControllerWithNoExceptionsAndGetContent('marketingLists/defaultPortlet/toggleUnsubscribed', true);
     $this->assertEmpty($content);
     $marketingListMember = MarketingListMember::getById($marketingListMemberId);
     $newUnsubscribedValue = $marketingListMember->unsubscribed;
     $this->assertNotEquals($previousUnsubcribedValue, $newUnsubscribedValue);
 }
 public function testGetByFolderTypeAndCampaignId()
 {
     $countBefore = EmailMessage::getCount();
     $job = new CampaignQueueMessagesInOutboxJob();
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $contact = ContactTestHelper::createContactByNameForOwner('contact 02', $this->user);
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 02');
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
     $campaign = CampaignTestHelper::createCampaign('campaign 02', 'subject', 'text Content', 'Html Content', null, null, null, Campaign::STATUS_PROCESSING, null, 0, $marketingList);
     $processed = 0;
     CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
     $this->assertTrue($job->run());
     $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
     $this->assertCount(1, $campaignItemsProcessed);
     $countAfter = EmailMessage::getCount();
     $this->assertEquals($countAfter, $countBefore + 1);
     $outboxCount = count(EmailMessage::getByFolderTypeAndCampaignId(EmailFolder::TYPE_OUTBOX, $campaign->id));
     $this->assertEquals(1, $outboxCount);
 }
Exemplo n.º 8
0
 public function testProcessDueCampaignItemContactUnsubscribed()
 {
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $email->optOut = false;
     $contact = ContactTestHelper::createContactByNameForOwner('contact 17', $this->user);
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 17', 'description', 'CustomFromName', '*****@*****.**');
     MarketingListMemberTestHelper::createMarketingListMember(true, $marketingList, $contact);
     $campaign = CampaignTestHelper::createCampaign('campaign 17', 'subject 17', 'Dear. Sir', 'Dear. Sir', null, null, null, null, null, null, $marketingList);
     $processed = 0;
     $campaignItem = CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
     CampaignItemsUtil::processDueItem($campaignItem);
     $this->assertEquals(1, $campaignItem->processed);
     $personId = $contact->getClassId('Person');
     $activities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(CampaignItemActivity::TYPE_SKIP, $campaignItem->id, $personId);
     $this->assertNotEmpty($activities);
     $this->assertCount(1, $activities);
 }
 /**
  * @depends testSendQueued
  */
 public function testSendQueuedSkipsPausedCampaignMessages()
 {
     EmailMessage::deleteAll();
     $job = new CampaignQueueMessagesInOutboxJob();
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $contact = ContactTestHelper::createContactByNameForOwner('contact', $this->user);
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList');
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
     $campaign = CampaignTestHelper::createCampaign('campaign', 'subject', 'text Content', 'Html Content', null, null, null, Campaign::STATUS_PROCESSING, null, 0, $marketingList);
     $processed = 0;
     CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
     $this->assertTrue($job->run());
     $campaignItems = CampaignItem::getAll();
     $this->assertCount(1, $campaignItems);
     $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
     $this->assertCount(1, $campaignItemsProcessed);
     $campaign->status = Campaign::STATUS_PAUSED;
     $this->assertTrue($campaign->save());
     $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     Yii::app()->emailHelper->sendQueued();
     $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
 }
 /**
  * @depends testRunWithDueActiveCampaignsWithMembers
  */
 public function testRunWithJustDueActiveCampaignsWithMembers()
 {
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 05');
     $marketingListId = $marketingList->id;
     $processed = 0;
     for ($i = 6; $i <= 10; $i++) {
         $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 0' . $i, $this->user);
         MarketingListMemberTestHelper::createMarketingListMember($processed, $marketingList, $contact);
     }
     $marketingList->forgetAll();
     $marketingList = MarketingList::getById($marketingListId);
     $nowDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $campaign = CampaignTestHelper::createCampaign('Active, Just Due With Members', 'subject', 'text Content', 'Html Content', null, null, null, Campaign::STATUS_ACTIVE, $nowDateTime, null, $marketingList);
     // we have to do this to ensure when we retrieve the data status is updated from db.
     $campaign->forgetAll();
     $this->assertEmpty(CampaignItem::getAll());
     // sleep 1 second to ensure we are giving ample time difference between creating the campaign and calling the job
     sleep(1);
     $job = new CampaignGenerateDueCampaignItemsJob();
     $this->assertTrue($job->run());
     $campaign = Campaign::getByName('Active, just Due With Members');
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign[0]->status);
     $allCampaignItems = CampaignItem::getAll();
     $this->assertNotEmpty($allCampaignItems);
     $this->assertCount(5, $allCampaignItems);
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaign[0]->id);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(5, $campaignItems);
 }
 /**
  * @depends testCreateAndGetMarketingListById
  */
 public function testDeleteMarketingList()
 {
     $marketingLists = MarketingList::getAll();
     $this->assertCount(2, $marketingLists);
     $marketingListId = $marketingLists[0]->id;
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingLists[0]);
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingLists[0]);
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingLists[1]);
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingLists[1]);
     AutoresponderTestHelper::createAutoresponder('subject 01', 'text content', 'html content', 1, Autoresponder::OPERATION_SUBSCRIBE, true, $marketingLists[0]);
     AutoresponderTestHelper::createAutoresponder('subject 02', 'text content', 'html content', 1, Autoresponder::OPERATION_SUBSCRIBE, true, $marketingLists[1]);
     $autoresponders = Autoresponder::getByOperationTypeAndMarketingListId(Autoresponder::OPERATION_SUBSCRIBE, $marketingListId);
     $this->assertCount(1, $autoresponders);
     $this->assertEquals(2, Autoresponder::getCount());
     $marketingListMembersCount = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingListId, 0);
     $this->assertEquals(2, $marketingListMembersCount);
     $marketingLists[0]->forgetAll();
     $marketingLists = MarketingList::getAll();
     $marketingLists[0]->delete();
     $marketingLists = MarketingList::getAll();
     $this->assertCount(1, $marketingLists);
     $autoresponders = Autoresponder::getByOperationTypeAndMarketingListId(Autoresponder::OPERATION_SUBSCRIBE, $marketingListId);
     $this->assertCount(0, $autoresponders);
     $this->assertEquals(1, Autoresponder::getCount());
     $marketingListMembersCount = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingListId, 0);
     $this->assertEquals(0, $marketingListMembersCount);
     $this->assertEquals(2, MarketingListMember::getCount());
 }
 /**
  * @depends testManageSubscriptionsActionThrowsNotFoundExceptionForInvalidPersonlId
  */
 public function testManageSubscriptionsAction()
 {
     $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $contact = ContactTestHelper::createContactByNameForOwner('contact 05', Yii::app()->user->userModel);
     $personId = $contact->getClassId('Person');
     $subscribedIds = array();
     for ($index = 1; $index < 5; $index++) {
         $marketingListName = 'marketingList 0' . $index;
         $marketingList = MarketingList::getByName($marketingListName);
         $marketingList = $marketingList[0];
         $unsubscribed = $index % 2;
         if (!$unsubscribed) {
             $subscribedIds[] = $marketingList->id;
         }
         MarketingListMemberTestHelper::createMarketingListMember($unsubscribed, $marketingList, $contact);
     }
     Yii::app()->user->userModel = null;
     $hash = EmailMessageActivityUtil::resolveHashForUnsubscribeAndManageSubscriptionsUrls($personId, $marketingList->id, 1, 'AutoresponderItem', false);
     $this->setGetArray(array('hash' => $hash));
     $content = $this->runControllerWithNoExceptionsAndGetContent($this->manageSubscriptionsUrl);
     $this->assertTrue(strpos($content, '<title>ZurmoCRM - Manage Subscriptions</title>') !== false);
     $this->assertTrue(strpos($content, '<div id="MarketingListsManageSubscriptionsPageView" ' . 'class="ZurmoPageView PageView">') !== false);
     $this->assertTrue(strpos($content, '<div class="GridView">') !== false);
     $this->assertTrue(strpos($content, '<div id="HeaderLinksView">') !== false);
     $this->assertTrue(strpos($content, '<div id="corp-logo">') !== false);
     $this->assertTrue(strpos($content, '/home/default"><img src="') !== false);
     $this->assertTrue(strpos($content, '/themes/default/images/Zurmo_logo.png" alt="Zurmo Logo" ' . 'height="32" width="107" /></a>') !== false);
     $this->assertTrue(strpos($content, '<div id="MarketingListsManageSubscriptionsListView" ' . 'class="MetadataView">') !== false);
     $this->assertTrue(strpos($content, '<div class="wrapper">') !== false);
     $this->assertTrue(strpos($content, '<h1><span class="truncated-title"><span class="ellipsis-content">' . 'My Subscriptions</span></span></h1>') !== false);
     $this->assertTrue(strpos($content, '<div class="wide" ' . 'id="marketingLists-manageSubscriptionsList">') !== false);
     $this->assertTrue(strpos($content, '<colgroup><col style="width:20%" />' . '<col style="width:80%" /></colgroup>') !== false);
     $this->assertTrue(strpos($content, '<td><a class="simple-link marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed"') !== false);
     $this->assertTrue(strpos($content, '/marketingLists/external/optOut?hash=') !== false);
     $this->assertTrue(strpos($content, '>Unsubscribe All/OptOut</a></td>') !== false);
     $this->assertTrue(strpos($content, '<div id="FooterView">') !== false);
     $this->assertTrue(strpos($content, '<a href="http://www.zurmo.com" id="credit-link" ' . 'class="clearfix">') !== false);
     $this->assertTrue(strpos($content, '<span>Copyright &#169; Zurmo Inc., 2013. ' . 'All rights reserved.</span></a>') !== false);
     $this->assertTrue(strpos($content, '<td>marketingList 02</td>') !== false);
     $this->assertTrue(strpos($content, '<td><div class="switch">') !== false);
     $this->assertTrue(strpos($content, '<div class="switch-state clearfix">') !== false);
     $this->assertTrue(strpos($content, '/marketingLists/external/subscribe?hash=') !== false);
     $this->assertTrue(strpos($content, 'id="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_') !== false);
     $this->assertTrue(strpos($content, 'checked="checked" type="radio" name="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_') !== false);
     $this->assertTrue(strpos($content, '<label for="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_') !== false);
     $this->assertTrue(strpos($content, '_0">Subscribe</label></div>') !== false);
     $this->assertTrue(strpos($content, '/marketingLists/external/unsubscribe?hash=') !== false);
     $this->assertTrue(strpos($content, '_1">Unsubcribe</label></div></div></td>') !== false);
     $this->assertTrue(strpos($content, '<td>marketingList 01</td>') !== false);
     $this->assertTrue(strpos($content, '<td>marketingList 03</td>') !== false);
     $this->assertTrue(strpos($content, '<td>marketingList 04</td>') !== false);
     $this->assertEquals(4, substr_count($content, '_0">Subscribe</label></div>'));
     $this->assertEquals(4, substr_count($content, '_1">Unsubcribe</label></div></div></td>'));
     foreach ($subscribedIds as $subscribedId) {
         $this->assertTrue(strpos($content, 'checked="checked" type="radio" name="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_' . $subscribedId) !== false);
     }
 }
 /**
  * @depends testCreateAndGetMarketingListMemberById
  */
 public function testGetByMarketingListIdAndContactId()
 {
     $marketingList = MarketingListTestHelper::createMarketingListByName('test marketing List 07');
     $this->assertNotNull($marketingList);
     $super = User::getByUsername('super');
     $contact = ContactTestHelper::createContactByNameForOwner('contact 02', $super);
     $marketingListMember = MarketingListMember::getByMarketingListIdAndContactId($marketingList->id, $contact->id);
     $this->assertFalse($marketingListMember);
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
     $marketingListMember = MarketingListMember::getByMarketingListIdAndContactId($marketingList->id, $contact->id);
     $this->assertTrue($marketingListMember[0] instanceof MarketingListMember);
 }
    public function generateCampaignItemsForDueCampaigns($count)
    {
        $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList Test', 'description goes here', 'fromName', '*****@*****.**');
        $marketingListId = $marketingList->id;
        $contacts = array();
        $emails = array();
        for ($i = 0; $i < $count; $i++) {
            $emails[$i] = new Email();
            $emails[$i]->emailAddress = "demo{$i}@zurmo.com";
            $account = AccountTestHelper::createAccountByNameForOwner('account ' . $i, $this->user);
            $contact = ContactTestHelper::createContactWithAccountByNameForOwner('contact ' . $i, $this->user, $account);
            $contact->primaryEmail = $emails[$i];
            $this->assertTrue($contact->save());
            $contacts[$i] = $contact;
            MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
        }
        $marketingList->forgetAll();
        $marketingList = MarketingList::getById($marketingListId);
        $content = <<<MTG
[[COMPANY^NAME]]
[[CREATED^DATE^TIME]]
[[DEPARTMENT]]
[[DESCRIPTION]]
[[FIRST^NAME]]
[[GOOGLE^WEB^TRACKING^ID]]
[[INDUSTRY]]
[[JOB^TITLE]]
[[LAST^NAME]]
[[LATEST^ACTIVITY^DATE^TIME]]
[[MOBILE^PHONE]]
[[MODIFIED^DATE^TIME]]
[[OFFICE^FAX]]
[[OFFICE^PHONE]]
[[TITLE]]
[[SOURCE]]
[[STATE]]
[[WEBSITE]]
[[MODEL^URL]]
[[BASE^URL]]
[[APPLICATION^NAME]]
[[CURRENT^YEAR]]
[[LAST^YEAR]]
[[OWNERS^AVATAR^SMALL]]
[[OWNERS^AVATAR^MEDIUM]]
[[OWNERS^AVATAR^LARGE]]
[[OWNERS^EMAIL^SIGNATURE]]
[[UNSUBSCRIBE^URL]]
[[MANAGE^SUBSCRIPTIONS^URL]]
[[PRIMARY^EMAIL__EMAIL^ADDRESS]]
[[PRIMARY^EMAIL__EMAIL^ADDRESS]]
[[SECONDARY^ADDRESS__CITY]]
[[SECONDARY^ADDRESS__COUNTRY]]
[[SECONDARY^ADDRESS__INVALID]]
[[SECONDARY^ADDRESS__LATITUDE]]
[[SECONDARY^ADDRESS__LONGITUDE]]
[[SECONDARY^ADDRESS__POSTAL^CODE]]
[[SECONDARY^ADDRESS__STATE]]
[[SECONDARY^ADDRESS__STREET1]]
[[SECONDARY^ADDRESS__STREET2]]
[[OWNER__DEPARTMENT]]
[[OWNER__FIRST^NAME]]
[[OWNER__IS^ACTIVE]]
[[OWNER__MOBILE^PHONE]]
[[OWNER__LAST^LOGIN^DATE^TIME]]
[[OWNER__LAST^NAME]]
[[CREATED^BY^USER__FIRST^NAME]]
[[CREATED^BY^USER__LAST^NAME]]
[[CREATED^BY^USER__MOBILE^PHONE]]
[[CREATED^BY^USER__TITLE]]
[[CREATED^BY^USER__USERNAME]]
[[ACCOUNT__ANNUAL^REVENUE]]
[[ACCOUNT__INDUSTRY]]
[[ACCOUNT__NAME]]
[[ACCOUNT__WEBSITE]]
[[ACCOUNT__BILLING^ADDRESS__COUNTRY]]
[[ACCOUNT__BILLING^ADDRESS__CITY]]
[[ACCOUNT__OWNER__FIRST^NAME]]
 ' " ` " '
MTG;
        $sendOnDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 600);
        $campaign = CampaignTestHelper::createCampaign('campaign Test', 'subject', $content, $content, null, null, null, Campaign::STATUS_ACTIVE, $sendOnDateTime, null, $marketingList, false);
        $fileNames = array('testImage.png', 'testZip.zip', 'testPDF.pdf');
        $files = array();
        foreach ($fileNames as $index => $fileName) {
            $file = ZurmoTestHelper::createFileModel($fileName);
            $files[$index]['name'] = $fileName;
            $files[$index]['type'] = $file->type;
            $files[$index]['size'] = $file->size;
            $files[$index]['contents'] = $file->fileContent->content;
            $campaign->files->add($file);
        }
        $this->assertTrue($campaign->save(false));
        $campaignId = $campaign->id;
        $campaign->forgetAll();
        $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
        $this->assertEmpty($campaignItems);
        //Process open campaigns.
        Yii::app()->jobQueue->deleteAll();
        AutoresponderOrCampaignBatchSizeConfigUtil::setBatchSize($count);
        Yii::app()->jobQueue->deleteAll();
        $this->assertCount(0, Yii::app()->jobQueue->getAll());
        ForgetAllCacheUtil::forgetAllCaches();
        $job = new CampaignGenerateDueCampaignItemsJob();
        $startedAt = microtime(true);
        $this->assertTrue($job->run());
        $timeTaken = microtime(true) - $startedAt;
        ForgetAllCacheUtil::forgetAllCaches();
        $campaign->forgetAll();
        unset($campaign);
        $campaign = Campaign::getById($campaignId);
        $this->assertNotNull($campaign);
        $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
        $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
        $this->assertNotEmpty($campaignItems);
        $this->assertCount($count, $campaignItems);
        return $timeTaken;
    }
 /**
  * @depends testRunWithJustDueActiveCampaignsWithMembers
  */
 public function testRunJobDoesNotRegenerateExistingItems()
 {
     $data = array('old' => array('subject' => 'Old Subject', 'fromName' => 'Old From Name', 'fromAddress' => '*****@*****.**', 'textContent' => 'Old Text Content', 'htmlContent' => 'Old Html Content'), 'new' => array('subject' => 'New Subject', 'fromName' => 'New From Name', 'fromAddress' => '*****@*****.**', 'textContent' => 'New Text Content', 'htmlContent' => 'New Html Content'));
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 06');
     $marketingListId = $marketingList->id;
     $processed = 0;
     for ($i = 11; $i <= 15; $i++) {
         $email = new Email();
         $email->emailAddress = "demo{$i}@zurmo.com";
         $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 0' . $i, $this->user);
         $contact->primaryEmail = $email;
         $this->assertTrue($contact->save());
         MarketingListMemberTestHelper::createMarketingListMember($processed, $marketingList, $contact);
     }
     $marketingList->forgetAll();
     $marketingList = MarketingList::getById($marketingListId);
     $nowDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $campaign = CampaignTestHelper::createCampaign('campaign', $data['old']['subject'], $data['old']['textContent'], $data['old']['htmlContent'], $data['old']['fromName'], $data['old']['fromAddress'], null, Campaign::STATUS_ACTIVE, $nowDateTime, null, $marketingList);
     $campaignId = $campaign->id;
     // we have to do this to ensure when we retrieve the data status is updated from db.
     $campaign->forgetAll();
     $this->assertEmpty(CampaignItem::getAll());
     // sleep 1 second to ensure we are giving ample time difference between creating the campaign and calling the job
     sleep(1);
     $campaignGenerateDueCampaignItemsJob = new CampaignGenerateDueCampaignItemsJob();
     $this->assertTrue($campaignGenerateDueCampaignItemsJob->run());
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
     $allCampaignItems = CampaignItem::getAll();
     $this->assertNotEmpty($allCampaignItems);
     $this->assertCount(5, $allCampaignItems);
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(5, $campaignItems);
     $campaignQueueMessagesInOutboxJob = new CampaignQueueMessagesInOutboxJob();
     $this->assertTrue($campaignQueueMessagesInOutboxJob->run());
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(5, $campaignItems);
     $oldCampaignItemIds = array();
     foreach ($campaignItems as $ci) {
         $this->assertNotEmpty($ci->id);
         $this->assertEquals(1, $ci->processed);
         $this->assertNotEmpty($ci->emailMessage->id);
         $this->assertNotEmpty($ci->emailMessage->sender->id);
         $this->assertNotEmpty($ci->emailMessage->content->id);
         $this->assertNotEmpty($ci->contact->id);
         $this->assertEquals($data['old']['subject'], $ci->emailMessage->subject);
         $this->assertEquals($data['old']['fromName'], $ci->emailMessage->sender->fromName);
         $this->assertEquals($data['old']['fromAddress'], $ci->emailMessage->sender->fromAddress);
         $this->assertContains($data['old']['textContent'], $ci->emailMessage->content->textContent);
         $this->assertContains($data['old']['htmlContent'], $ci->emailMessage->content->htmlContent);
         $oldCampaignItemIds[] = $ci->id;
     }
     $campaignItems[count($campaignItems) - 1]->delete();
     array_pop($oldCampaignItemIds);
     $campaign->status = Campaign::STATUS_ACTIVE;
     foreach ($data['new'] as $property => $value) {
         $campaign->{$property} = $value;
     }
     $this->assertTrue($campaign->save());
     $campaign->forgetAll();
     $campaignGenerateDueCampaignItemsJob = new CampaignGenerateDueCampaignItemsJob();
     $this->assertTrue($campaignGenerateDueCampaignItemsJob->run());
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
     $allCampaignItems = CampaignItem::getAll();
     $this->assertNotEmpty($allCampaignItems);
     $this->assertCount(5, $allCampaignItems);
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(1, $campaignItems);
     $campaignQueueMessagesInOutboxJob = new CampaignQueueMessagesInOutboxJob();
     $this->assertTrue($campaignQueueMessagesInOutboxJob->run());
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(5, $campaignItems);
     $oldCounter = 0;
     $newCounter = 0;
     foreach ($campaignItems as $ci) {
         $this->assertNotEmpty($ci->id);
         $this->assertEquals(1, $ci->processed);
         $this->assertNotEmpty($ci->emailMessage->id);
         $this->assertNotEmpty($ci->emailMessage->sender->id);
         $this->assertNotEmpty($ci->emailMessage->content->id);
         $this->assertNotEmpty($ci->contact->id);
         $dataIndex = in_array($ci->id, $oldCampaignItemIds) ? 'old' : 'new';
         if ($dataIndex == 'old') {
             $oldCounter++;
         } else {
             $newCounter++;
         }
         $this->assertEquals($data[$dataIndex]['subject'], $ci->emailMessage->subject);
         $this->assertEquals($data[$dataIndex]['fromName'], $ci->emailMessage->sender->fromName);
         $this->assertEquals($data[$dataIndex]['fromAddress'], $ci->emailMessage->sender->fromAddress);
         $this->assertContains($data[$dataIndex]['textContent'], $ci->emailMessage->content->textContent);
         $this->assertContains($data[$dataIndex]['htmlContent'], $ci->emailMessage->content->htmlContent);
     }
     $this->assertEquals(count($oldCampaignItemIds), $oldCounter);
     $this->assertEquals(count($campaignItems) - count($oldCampaignItemIds), $newCounter);
 }
Exemplo n.º 16
0
 /**
  * @depends testIsSent
  */
 public function testHasFailedToSend()
 {
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 09');
     $campaign = CampaignTestHelper::createCampaign('campaign 08', 'subject 08', 'text 08', 'html 08', null, null, null, null, null, null, $marketingList);
     $this->assertNotNull($campaign);
     $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 09', Yii::app()->user->userModel);
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $campaignItem = CampaignItemTestHelper::createCampaignItem(0, $campaign, $contact);
     $this->assertNotNull($campaignItem);
     $this->assertFalse($campaignItem->hasFailedToSend());
     $this->processDueItem($campaignItem);
     $this->assertFalse($campaignItem->hasFailedToSend());
     // Folder is outbox at the end of processDueItem and hence it fails
     $box = EmailBox::resolveAndGetByName(EmailBox::CAMPAIGNS_NAME);
     $campaignItem->emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_OUTBOX_FAILURE);
     $this->assertTrue($campaignItem->unrestrictedSave());
     $this->assertTrue($campaignItem->hasFailedToSend());
 }
    public function generateAndProcessCampaignItems($count)
    {
        $contacts = array();
        $emails = array();
        for ($i = 0; $i < $count; $i++) {
            $emails[$i] = new Email();
            $emails[$i]->emailAddress = "demo{$i}@zurmo.com";
            $account = AccountTestHelper::createAccountByNameForOwner('account ' . $i, $this->user);
            $contact = ContactTestHelper::createContactWithAccountByNameForOwner('contact ' . $i, $this->user, $account);
            $contact->primaryEmail = $emails[$i];
            $this->assertTrue($contact->save());
            $contacts[$i] = $contact;
        }
        $content = <<<MTG
[[COMPANY^NAME]]
[[CREATED^DATE^TIME]]
[[DEPARTMENT]]
[[DESCRIPTION]]
[[FIRST^NAME]]
[[GOOGLE^WEB^TRACKING^ID]]
[[INDUSTRY]]
[[JOB^TITLE]]
[[LAST^NAME]]
[[LATEST^ACTIVITY^DATE^TIME]]
[[MOBILE^PHONE]]
[[MODIFIED^DATE^TIME]]
[[OFFICE^FAX]]
[[OFFICE^PHONE]]
[[TITLE]]
[[SOURCE]]
[[STATE]]
[[WEBSITE]]
[[MODEL^URL]]
[[BASE^URL]]
[[APPLICATION^NAME]]
[[CURRENT^YEAR]]
[[LAST^YEAR]]
[[OWNERS^AVATAR^SMALL]]
[[OWNERS^AVATAR^MEDIUM]]
[[OWNERS^AVATAR^LARGE]]
[[OWNERS^EMAIL^SIGNATURE]]
[[UNSUBSCRIBE^URL]]
[[MANAGE^SUBSCRIPTIONS^URL]]
[[PRIMARY^EMAIL__EMAIL^ADDRESS]]
[[PRIMARY^EMAIL__EMAIL^ADDRESS]]
[[SECONDARY^ADDRESS__CITY]]
[[SECONDARY^ADDRESS__COUNTRY]]
[[SECONDARY^ADDRESS__INVALID]]
[[SECONDARY^ADDRESS__LATITUDE]]
[[SECONDARY^ADDRESS__LONGITUDE]]
[[SECONDARY^ADDRESS__POSTAL^CODE]]
[[SECONDARY^ADDRESS__STATE]]
[[SECONDARY^ADDRESS__STREET1]]
[[SECONDARY^ADDRESS__STREET2]]
[[OWNER__DEPARTMENT]]
[[OWNER__FIRST^NAME]]
[[OWNER__IS^ACTIVE]]
[[OWNER__MOBILE^PHONE]]
[[OWNER__LAST^LOGIN^DATE^TIME]]
[[OWNER__LAST^NAME]]
[[CREATED^BY^USER__FIRST^NAME]]
[[CREATED^BY^USER__LAST^NAME]]
[[CREATED^BY^USER__MOBILE^PHONE]]
[[CREATED^BY^USER__TITLE]]
[[CREATED^BY^USER__USERNAME]]
[[ACCOUNT__ANNUAL^REVENUE]]
[[ACCOUNT__INDUSTRY]]
[[ACCOUNT__NAME]]
[[ACCOUNT__WEBSITE]]
[[ACCOUNT__BILLING^ADDRESS__COUNTRY]]
[[ACCOUNT__BILLING^ADDRESS__CITY]]
[[ACCOUNT__OWNER__FIRST^NAME]]
 ' " ` " '
MTG;
        $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList Test', 'description goes here', 'fromName', '*****@*****.**');
        $campaign = CampaignTestHelper::createCampaign('campaign Test', 'subject', $content, $content, null, null, null, Campaign::STATUS_PROCESSING, null, null, $marketingList, false);
        $fileNames = array('testImage.png', 'testZip.zip', 'testPDF.pdf');
        $files = array();
        foreach ($fileNames as $index => $fileName) {
            $file = ZurmoTestHelper::createFileModel($fileName);
            $files[$index]['name'] = $fileName;
            $files[$index]['type'] = $file->type;
            $files[$index]['size'] = $file->size;
            $files[$index]['contents'] = $file->fileContent->content;
            $campaign->files->add($file);
        }
        $this->assertTrue($campaign->save(false));
        $processed = 0;
        foreach ($contacts as $contact) {
            MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
            CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
        }
        AutoresponderOrCampaignBatchSizeConfigUtil::setBatchSize($count);
        Yii::app()->jobQueue->deleteAll();
        ForgetAllCacheUtil::forgetAllCaches();
        $job = new CampaignQueueMessagesInOutboxJob();
        $this->assertCount(0, Yii::app()->jobQueue->getAll());
        $startedAt = microtime(true);
        $this->assertTrue($job->run());
        $timeTaken = microtime(true) - $startedAt;
        ForgetAllCacheUtil::forgetAllCaches();
        $campaignItemsCountExpected = $count;
        $campaignItemsCountAfter = CampaignItem::getCount();
        $this->assertEquals($campaignItemsCountExpected, $campaignItemsCountAfter);
        $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
        $this->assertCount($count, $campaignItemsProcessed);
        foreach ($campaignItemsProcessed as $i => $campaignItem) {
            $contact = $contacts[$i];
            $email = $emails[$i];
            $emailMessage = $campaignItem->emailMessage;
            $this->assertEquals($marketingList->owner->id, $emailMessage->owner->id);
            $marketingListPermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($marketingList);
            $emailMessagePermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($emailMessage);
            $this->assertEquals($marketingListPermissions, $emailMessagePermissions);
            $this->assertEquals($campaign->subject, $emailMessage->subject);
            $this->assertNotEquals($campaign->textContent, $emailMessage->content->textContent);
            $this->assertNotEquals($campaign->htmlContent, $emailMessage->content->htmlContent);
            $this->assertEquals(2, substr_count($emailMessage->content->textContent, '/marketingLists/external/'));
            $this->assertContains('/marketingLists/external/', $emailMessage->content->htmlContent);
            $this->assertEquals(2, substr_count($emailMessage->content->htmlContent, '/marketingLists/external/'));
            $this->assertEquals('*****@*****.**', $emailMessage->sender->fromAddress);
            $this->assertEquals('Support Team', $emailMessage->sender->fromName);
            $this->assertEquals(1, $emailMessage->recipients->count());
            $recipients = $emailMessage->recipients;
            $this->assertEquals(strval($contact), $recipients[0]->toName);
            $this->assertEquals($email->emailAddress, $recipients[0]->toAddress);
            $this->assertEquals(EmailMessageRecipient::TYPE_TO, $recipients[0]->type);
            $this->assertNotEmpty($emailMessage->files);
            $this->assertCount(count($files), $emailMessage->files);
            foreach ($campaign->files as $index => $file) {
                $this->assertEquals($file->name, $emailMessage->files[$index]->name);
                $this->assertEquals($file->type, $emailMessage->files[$index]->type);
                $this->assertEquals($file->size, $emailMessage->files[$index]->size);
                //CampaingItem should share the Attachments content from Campaign
                $this->assertEquals($file->fileContent->content, $emailMessage->files[$index]->fileContent->content);
                $this->assertEquals($file->fileContent->id, $emailMessage->files[$index]->fileContent->id);
            }
            $headersArray = array('zurmoItemId' => $campaignItem->id, 'zurmoItemClass' => get_class($campaignItem), 'zurmoPersonId' => $contact->getClassId('Person'));
            $expectedHeaders = serialize($headersArray);
            $this->assertEquals($expectedHeaders, $emailMessage->headers);
        }
        return $timeTaken;
    }
 /**
  * @depends testRunWithCustomBatchSize
  */
 public function testRunWithContactContainingPrimaryEmailOptedOut()
 {
     $unprocessedItems = AutoresponderItem::getByProcessed(0);
     $this->assertEmpty($unprocessedItems);
     $job = new AutoresponderQueueMessagesInOutboxJob();
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $email->optOut = true;
     $contact = ContactTestHelper::createContactByNameForOwner('contact 07', $this->user);
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 08', 'description goes here', 'fromName', '*****@*****.**');
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
     $autoresponder = AutoresponderTestHelper::createAutoresponder('subject 08', '[[FIRST^NAME]]', '[[LAST^NAME]]', 1, Autoresponder::OPERATION_SUBSCRIBE, true, $marketingList);
     $processed = 0;
     $processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - rand(10, 500));
     $autoresponderItem = AutoresponderItemTestHelper::createAutoresponderItem($processed, $processDateTime, $autoresponder, $contact);
     $unprocessedItems = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponder->id);
     $this->assertCount(1, $unprocessedItems);
     $this->assertTrue($job->run());
     $unprocessedItems = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponder->id);
     $this->assertCount(0, $unprocessedItems);
     $personId = $contact->getClassId('Person');
     $activities = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(AutoresponderItemActivity::TYPE_SKIP, $autoresponderItem->id, $personId);
     $this->assertNotEmpty($activities);
     $this->assertCount(1, $activities);
 }
 /**
  * @depends testRunWithDueActiveCampaignsWithMembers
  */
 public function testRunWithDueActiveCampaignsWithCustomBatchSize()
 {
     $this->purgeAllCampaigns();
     $contactIds = array();
     $marketingListIds = array();
     $campaignIds = array();
     for ($index = 6; $index < 9; $index++) {
         $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 0' . $index, $this->user);
         $contactIds[] = $contact->id;
         $contact->forgetAll();
     }
     for ($index = 5; $index < 10; $index++) {
         $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 0' . $index);
         $marketingListId = $marketingList->id;
         $marketingListIds[] = $marketingListId;
         foreach ($contactIds as $contactId) {
             $contact = Contact::getById($contactId);
             $unsubscribed = rand(10, 20) % 2;
             MarketingListMemberTestHelper::createMarketingListMember($unsubscribed, $marketingList, $contact);
         }
         $marketingList->forgetAll();
         $marketingList = MarketingList::getById($marketingListId);
         $campaignSuffix = substr($marketingList->name, -2);
         $campaign = CampaignTestHelper::createCampaign('campaign ' . $campaignSuffix, 'subject ' . $campaignSuffix, 'text ' . $campaignSuffix, 'html ' . $campaignSuffix, null, null, null, null, null, null, $marketingList);
         $this->assertNotNull($campaign);
         $campaignIds[] = $campaign->id;
         $campaign->forgetAll();
     }
     foreach ($campaignIds as $campaignId) {
         $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
         $this->assertEmpty($campaignItems);
     }
     AutoresponderOrCampaignBatchSizeConfigUtil::setBatchSize(10);
     $job = new CampaignGenerateDueCampaignItemsJob();
     $this->assertTrue($job->run());
     foreach ($campaignIds as $index => $campaignId) {
         $campaign = Campaign::getById($campaignId);
         $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
         if ($index === 0) {
             $expectedCount = AutoresponderOrCampaignBatchSizeConfigUtil::getBatchSize();
             $memberCount = count($campaign->marketingList->marketingListMembers);
             if ($memberCount < $expectedCount) {
                 $expectedCount = $memberCount;
             }
             $this->assertNotEmpty($campaignItems);
             $this->assertCount($expectedCount, $campaignItems);
             $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
         } else {
             $this->assertEmpty($campaignItems);
             $this->assertEquals(Campaign::STATUS_ACTIVE, $campaign->status);
         }
     }
     AutoresponderOrCampaignBatchSizeConfigUtil::setBatchSize(null);
     $this->assertTrue($job->run());
     foreach ($campaignIds as $index => $campaignId) {
         $campaign = Campaign::getById($campaignId);
         $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
         if ($index < 2) {
             $expectedCount = AutoresponderOrCampaignBatchSizeConfigUtil::getBatchSize();
             $memberCount = count($campaign->marketingList->marketingListMembers);
             if ($memberCount < $expectedCount) {
                 $expectedCount = $memberCount;
             }
             $this->assertNotEmpty($campaignItems);
             $this->assertCount($expectedCount, $campaignItems);
             $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
         } else {
             $this->assertEmpty($campaignItems);
             $this->assertEquals(Campaign::STATUS_ACTIVE, $campaign->status);
         }
     }
     // TODO: @Shoaibi: Medium: Add tests for the other campaign type.
 }
 /**
  * @depends testCreateAndGetMarketingListMemberById
  */
 public function testBeforeSaveAutoresponderRegister()
 {
     $marketingList = MarketingListTestHelper::createMarketingListByName('test marketing List 06');
     $this->assertNotNull($marketingList);
     $autoresponderSubscribe = AutoresponderTestHelper::createAutoresponder('test autoresponder Subscribe', 'This is text content Subscribe', 'This is <b>html</b> content Subscribe', 10, Autoresponder::OPERATION_SUBSCRIBE, false, $marketingList);
     $this->assertNotNull($autoresponderSubscribe);
     $autoresponderUnsubscribe = AutoresponderTestHelper::createAutoresponder('test autoresponder Unsubscribe', 'This is text content Unsubscribe', 'This is <b>html</b> content Unsubscribe', 20, Autoresponder::OPERATION_UNSUBSCRIBE, true, $marketingList);
     $this->assertNotNull($autoresponderUnsubscribe);
     $member = MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList);
     $autoresponderItemsSubscribe = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponderSubscribe->id);
     $this->assertNotEmpty($autoresponderItemsSubscribe);
     $this->assertCount(1, $autoresponderItemsSubscribe);
     $autoresponderItemsUnsubscribe = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponderUnsubscribe->id);
     $this->assertEmpty($autoresponderItemsUnsubscribe);
     $member->unsubscribed = 1;
     $saved = $member->unrestrictedSave();
     $this->assertTrue($saved);
     $autoresponderItemsSubscribe = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponderSubscribe->id);
     $this->assertNotEmpty($autoresponderItemsSubscribe);
     $this->assertCount(1, $autoresponderItemsSubscribe);
     $autoresponderItemsUnsubscribe = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponderUnsubscribe->id);
     $this->assertNotEmpty($autoresponderItemsUnsubscribe);
     $this->assertCount(1, $autoresponderItemsUnsubscribe);
 }
 public function testRunWithCampaignPausedAfterAllItemsGeneratedAndThenUnpaused()
 {
     // Cleanup
     Campaign::deleteAll();
     $this->assertEquals(0, Campaign::getCount());
     $this->assertEquals(0, CampaignItem::getCount());
     $this->assertEquals(0, CampaignItemActivity::getCount());
     MarketingList::deleteAll();
     $this->assertEquals(0, MarketingList::getCount());
     $this->assertEquals(0, MarketingListMember::getCount());
     EmailMessage::deleteAll();
     $this->assertEquals(0, EmailMessage::getCount());
     $this->assertEquals(0, EmailMessageContent::getCount());
     $this->assertEquals(0, EmailMessageSender::getCount());
     $this->assertEquals(0, EmailMessageRecipient::getCount());
     Contact::deleteAll();
     $this->assertEquals(0, Contact::getCount());
     // setup an email address for contacts
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     // create a marketing list with 5 members
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 05');
     $marketingListId = $marketingList->id;
     for ($i = 1; $i <= 5; $i++) {
         $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 0' . $i, $this->user);
         $contact->primaryEmail = $email;
         $this->assertTrue($contact->save());
         MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
     }
     $marketingList->forgetAll();
     // create a due campaign with that marketing list
     $campaign = CampaignTestHelper::createCampaign('campaign 04', 'subject', 'text Content', 'Html Content', null, null, null, null, null, null, MarketingList::getById($marketingListId));
     $campaignId = $campaign->id;
     /*
      * Run 1:
      * CampaignGenerateDueCampaignItemsJob
      * status == processing
      * items generated but unprocessed
      *
      * CampaignQueueMessagesInOutboxJob
      * status == processing
      * items processed with email messages generated and queued
      *
      * ProcessOutboundEmailJob
      * status == processing
      * email items attempted to be sent
      *
      * Run 2:
      * Pause the campaign
      * items are processed and still present
      *
      * Unpause the campaign
      * status == active
      * items are processed and still present
      *
      * CampaignGenerateDueCampaignItemsJob
      * status == processing
      * ensure all items are present and processed
      *
      * CampaignQueueMessagesInOutboxJob
      * status == processing
      * ensure all items are present and processed
      * ensure all email messages are present with correct folder type
      *
      * ProcessOutboundEmailJob
      * ensure all email messages are present with correct folder type
      * status == processing
      *
      *
      * Run 3:
      * Mark Campaign as Completed
      * status == completed
      */
     // we have to do this to ensure when we retrieve the data status is updated from db.
     $campaign->forgetAll();
     $this->assertEmpty(CampaignItem::getAll());
     // Run 1 starts here
     // Run CampaignGenerateDueCampaignItemsJob
     $job = new CampaignGenerateDueCampaignItemsJob();
     $this->assertTrue($job->run());
     $campaign = Campaign::getById($campaignId);
     // ensure status is processing
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
     // ensure 5 campaign items have been generated
     $this->assertEquals(5, CampaignItem::getCount());
     // ensure all 5 campaign items are unprocessed
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(5, $campaignItems);
     // Run CampaignQueueMessagesInOutboxJob
     $job = new CampaignQueueMessagesInOutboxJob();
     $this->assertTrue($job->run());
     // Ensure campaign status
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
     // ensure all 5 campaign items are processed
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(5, $campaignItems);
     // Ensure 5 new email messages
     $this->assertEquals(5, EmailMessage::getCount());
     $this->assertEquals(5, EmailHelper::getQueuedCount());
     // Run ProcessOutboundEmail
     $job = new ProcessOutboundEmailJob();
     $this->assertTrue($job->run());
     // Ensure all email were sent
     $this->assertEquals(5, EmailMessage::getCount());
     $this->assertEquals(0, EmailHelper::getQueuedCount());
     foreach (EmailMessage::getAll() as $emailMessage) {
         $this->assertEquals($emailMessage->folder->type, EmailFolder::TYPE_SENT);
     }
     // Ensure campaign status
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
     // end of Run 1
     // Run 2 starts here
     // Pause the campaign
     $campaign->status = Campaign::STATUS_PAUSED;
     $this->assertTrue($campaign->save());
     // ensure campaign items are still there
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
     $this->assertCount(5, $campaignItems);
     $this->assertNotEmpty($campaignItems);
     // Unpause campaign
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_PAUSED, $campaign->status);
     $campaign->status = Campaign::STATUS_ACTIVE;
     $this->assertTrue($campaign->save());
     // ensure campaign items are still there
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
     $this->assertCount(5, $campaignItems);
     $this->assertNotEmpty($campaignItems);
     // Ensure status change
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_ACTIVE, $campaign->status);
     // Run CampaignGenerateDueCampaignItemsJob
     $job = new CampaignGenerateDueCampaignItemsJob();
     $this->assertTrue($job->run());
     // Ensure campaign status change
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
     // ensure all 5 (previously created in run1) campaign items are still processed
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(5, $campaignItems);
     // Run CampaignQueueMessagesInOutboxJob
     $job = new CampaignQueueMessagesInOutboxJob();
     $this->assertTrue($job->run());
     // Ensure campaign status
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
     // ensure all 5 (previously created in run1) campaign items are still processed
     $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
     $this->assertNotEmpty($campaignItems);
     $this->assertCount(5, $campaignItems);
     // Ensure all 5 (previously created and sent in run1) emails are sent.
     $this->assertEquals(5, EmailMessage::getCount());
     foreach (EmailMessage::getAll() as $emailMessage) {
         $this->assertEquals($emailMessage->folder->type, EmailFolder::TYPE_SENT);
     }
     // Run ProcessOutboundEmail
     $job = new ProcessOutboundEmailJob();
     $this->assertTrue($job->run());
     // Ensure all 5 (previously created and sent in run1) emails are sent.
     $this->assertEquals(5, EmailMessage::getCount());
     foreach (EmailMessage::getAll() as $emailMessage) {
         $this->assertEquals($emailMessage->folder->type, EmailFolder::TYPE_SENT);
     }
     // Ensure campaign status
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
     // end of Run 2
     // Run 3 starts here
     // Run CampaignMarkCompleted
     $job = new CampaignMarkCompletedJob();
     $this->assertTrue($job->run());
     // Ensure campaign status change
     $campaign = Campaign::getById($campaignId);
     $this->assertEquals(Campaign::STATUS_COMPLETED, $campaign->status);
     // end of Run 3
 }
 public function testSkipMessage()
 {
     // Tests with Autoresponder
     $contact = ContactTestHelper::createContactByNameForOwner('contact 15', $this->user);
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 15');
     $autoresponder = AutoresponderTestHelper::createAutoresponder('subject 15', 'text content', 'html content', 1, Autoresponder::OPERATION_UNSUBSCRIBE, false, $marketingList);
     $autoresponderItemsUtil = new AutoresponderItemsUtil();
     // Test when the contact isn't in the marketing list at all, same as it was deleted
     $this->assertTrue($autoresponderItemsUtil->skipMessage($contact, $autoresponder));
     // Test when the contact is subscribed but the operation is OPERATION_UNSUBSCRIBE
     $marketingListMember = MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
     $this->assertTrue($autoresponderItemsUtil->skipMessage($contact, $autoresponder));
     // Test when the contact is unsubscribed, so skipMessage return false
     $marketingListMember->unsubscribed = true;
     $this->assertTrue($marketingListMember->unrestrictedSave());
     $this->assertFalse($autoresponderItemsUtil->skipMessage($contact, $autoresponder));
     $contact = ContactTestHelper::createContactByNameForOwner('contact 16', $this->user);
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 16');
     $autoresponder = AutoresponderTestHelper::createAutoresponder('subject 16', 'text content', 'html content', 1, Autoresponder::OPERATION_SUBSCRIBE, false, $marketingList);
     // Test when the contact isn't in the marketing list at all, same as it was deleted
     $this->assertTrue($autoresponderItemsUtil->skipMessage($contact, $autoresponder));
     // Test when the contact is unsubscribed but the operation is OPERATION_SUBSCRIBE
     $marketingListMember = MarketingListMemberTestHelper::createMarketingListMember(1, $marketingList, $contact);
     $this->assertTrue($autoresponderItemsUtil->skipMessage($contact, $autoresponder));
     // Test when the contact is subscribed, so skipMessage return false
     $marketingListMember->unsubscribed = false;
     $this->assertTrue($marketingListMember->unrestrictedSave());
     $this->assertFalse($autoresponderItemsUtil->skipMessage($contact, $autoresponder));
     //Tests with Campaign
     $contact = ContactTestHelper::createContactByNameForOwner('contact 17', $this->user);
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 17');
     $campaign = CampaignTestHelper::createCampaign('campaign 01', 'subject 01', 'text 01', 'html 01', null, null, null, null, null, null, $marketingList);
     $this->assertNotNull($campaign);
     // Test when the contact isn't in the marketing list at all, same as it was deleted
     $this->assertTrue($autoresponderItemsUtil->skipMessage($contact, $campaign));
     // Test when the contact is unsubscribed
     $marketingListMember = MarketingListMemberTestHelper::createMarketingListMember(1, $marketingList, $contact);
     $this->assertTrue($autoresponderItemsUtil->skipMessage($contact, $campaign));
     // Test when the contact is subscribed, so skipMessage return false
     $marketingListMember->unsubscribed = false;
     $this->assertTrue($marketingListMember->unrestrictedSave());
     $this->assertFalse($autoresponderItemsUtil->skipMessage($contact, $campaign));
 }