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
 }
 /**
  * @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.
 }
    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 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 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);
 }