/** * @param int $type * @return string */ protected static function resolveCampaignTypeSubQuery($type) { assert('is_int($type)'); $quote = DatabaseCompatibilityUtil::getQuote(); $where = null; $campaignItemTableName = CampaignItem::getTableName(); $campaignItemActivityTableName = CampaignItemActivity::getTableName(); $emailMessageActivityTableName = EmailMessageActivity::getTableName(); $selectQueryAdapter = new RedBeanModelSelectQueryAdapter(); $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('CampaignItemActivity'); $selectQueryAdapter->addClauseByQueryString("1"); $joinTablesAdapter->addFromTableAndGetAliasName($emailMessageActivityTableName, 'emailmessageactivity_id', $campaignItemActivityTableName); $where = "type = " . $type . " and {$quote}{$campaignItemActivityTableName}{$quote}" . ".campaignitem_id = {$quote}{$campaignItemTableName}{$quote}.id"; $sql = SQLQueryUtil::makeQuery($campaignItemActivityTableName, $selectQueryAdapter, $joinTablesAdapter, null, 1, $where); return $sql; }
public function testGetCountByTypeAndEmailMessageActivity() { // setup pre-req data $contact = ContactTestHelper::createContactByNameForOwner('contact 01', $this->user); $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 01', 'description 01', 'fromName 01', '*****@*****.**'); $campaign = CampaignTestHelper::createCampaign('campaign 01', 'subject 01', 'text Content 01', 'html Content 01', 'fromName 01', '*****@*****.**', null, null, null, null, $marketingList); $processed = 0; $campaignItem = CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact); $modelId = $campaignItem->id; $modelType = get_class($campaignItem); $personId = $contact->getClassId('Person'); $this->assertNotNull($personId); $activityData = array('modelId' => $campaignItem->id, 'modelType' => 'CampaignItem', 'personId' => $personId, 'url' => null, 'type' => CampaignItemActivity::TYPE_OPEN); $activityCreatedOrUpdated = CampaignItemActivityUtil::createOrUpdateActivity($activityData); $emailMessageActivities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(CampaignItemActivity::TYPE_OPEN, $campaignItem->id, $personId, null); $externalMessageActivityCount = ExternalApiEmailMessageActivity::getByTypeAndEmailMessageActivity(CampaignItemActivity::TYPE_OPEN, $emailMessageActivities[0], "sendgrid"); $this->assertEquals(0, $externalMessageActivityCount); $externalApiEmailMessageActivity = new ExternalApiEmailMessageActivity(); $externalApiEmailMessageActivity->emailMessageActivity = $emailMessageActivities[0]; $externalApiEmailMessageActivity->api = 'sendgrid'; $externalApiEmailMessageActivity->type = CampaignItemActivity::TYPE_OPEN; $externalApiEmailMessageActivity->reason = 'Test reason'; $externalApiEmailMessageActivity->emailAddress = '*****@*****.**'; $externalApiEmailMessageActivity->itemClass = 'CampaignItem'; $externalApiEmailMessageActivity->itemId = $campaignItem->id; $this->assertTrue($externalApiEmailMessageActivity->save()); $id = $externalApiEmailMessageActivity->id; $externalMessageActivityCount = ExternalApiEmailMessageActivity::getByTypeAndEmailMessageActivity(CampaignItemActivity::TYPE_OPEN, $emailMessageActivities[0], "sendgrid"); $this->assertEquals(1, $externalMessageActivityCount); $externalApiActivity = ExternalApiEmailMessageActivity::getById($id); $externalApiActivity->reason = 'New reason 1'; $this->assertTrue($externalApiActivity->save()); $externalApiActivity = ExternalApiEmailMessageActivity::getById($id); $this->assertEquals('New reason 1', $externalApiActivity->reason); $activities = ExternalApiEmailMessageActivity::getByEmailAddress('*****@*****.**', "sendgrid", false); $this->assertEquals(1, count($activities)); $this->assertEquals('New reason 1', $activities[0]->reason); }
/** * Resolve content for sendgrid bounce or spam email activity. * @param CampaignItem $campaignItem * @param int $type * @return string */ protected static function resolveContentForSendGridBounceOrSpamEventActivity(CampaignItem $campaignItem, $type) { $typesArray = CampaignItemActivity::getTypesArray(); $campaignItemActivities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl($type, $campaignItem->id, $campaignItem->contact->getClassId('Person'), null, 'latestDateTime'); $content = null; foreach ($campaignItemActivities as $campaignItemActivity) { $externalMessageActivities = ExternalApiEmailMessageActivity::getByEmailMessageActivity($campaignItemActivity, "sendgrid", false); foreach ($externalMessageActivities as $externalMessageActivity) { $content .= '<tr>'; $content .= '<td>' . $typesArray[$externalMessageActivity->type] . '</td>'; $content .= '<td>' . DateTimeUtil::convertDbFormattedDateTimeToLocaleFormattedDisplay($campaignItemActivity->latestDateTime) . '</td>'; $content .= '<td>' . $campaignItemActivity->quantity . '</td>'; $content .= '<td>' . $campaignItemActivity->latestSourceIP . '</td>'; $content .= '<td></td>'; $content .= '</tr>'; } } return $content; }
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 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 testResolveQueryStringFromUrlAndCreateOrUpdateActivityThrowsExceptionForInvalidPersonIdForUrlClickTracking * @expectedException NotSupportedException */ public function testResolveQueryStringFromUrlAndCreateOrUpdateActivityThrowsExceptionForDuplicateActivityForUrlClickTracking() { // setup pre-req data $campaignItemActivities = CampaignItemActivity::getByType(CampaignItemActivity::TYPE_CLICK); $this->assertNotEmpty($campaignItemActivities); $campaignActivity = new CampaignItemActivity(); $campaignActivity->quantity = $campaignItemActivities[0]->quantity; $campaignActivity->type = $campaignItemActivities[0]->type; $campaignActivity->campaignItem = $campaignItemActivities[0]->campaignItem; $campaignActivity->person = $campaignItemActivities[0]->person; $campaignActivity->emailMessageUrl = $campaignItemActivities[0]->emailMessageUrl; $this->assertTrue($campaignActivity->save()); $modelId = $campaignItemActivities[0]->campaignItem->id; $modelType = get_class($campaignItemActivities[0]->campaignItem); $personId = $campaignItemActivities[0]->person->id; // get the modelClassName to use for activity object tests $className = 'CampaignItemActivityUtil'; $resolveModelClassNameByModelTypeFunction = static::getProtectedMethod($className, 'resolveModelClassNameByModelType'); $modelClassName = $resolveModelClassNameByModelTypeFunction->invokeArgs(null, array($modelType)); $type = $modelClassName::TYPE_CLICK; $existingActivities = $modelClassName::getByType($type); $this->assertCount(2, $existingActivities); // get base query string $resolveBaseQueryStringArrayFunction = static::getProtectedMethod('ContentTrackingUtil', 'resolveBaseQueryStringArray'); $queryStringArray = $resolveBaseQueryStringArrayFunction->invokeArgs(null, array($modelId, $modelType, $personId)); $this->assertNotEmpty($queryStringArray); $this->assertCount(3, $queryStringArray); $this->assertArrayHasKey('modelId', $queryStringArray); $this->assertArrayHasKey('modelType', $queryStringArray); $this->assertArrayHasKey('personId', $queryStringArray); $queryStringArray['url'] = 'http://www.zurmo.com'; // get hash for query string and ensure its what we expect it to be. $queryStringArrayHash = StringUtil::resolveHashForQueryStringArray($queryStringArray); $queryStringArrayDecoded = $className::resolveQueryStringArrayForHash($queryStringArrayHash); $this->assertNotEmpty($queryStringArrayDecoded); $this->assertCount(5, $queryStringArrayDecoded); $this->assertArrayHasKey('modelId', $queryStringArrayDecoded); $this->assertArrayHasKey('modelType', $queryStringArrayDecoded); $this->assertArrayHasKey('personId', $queryStringArrayDecoded); $this->assertArrayHasKey('url', $queryStringArrayDecoded); $this->assertArrayHasKey('type', $queryStringArrayDecoded); $this->assertEquals($queryStringArray['modelId'], $queryStringArrayDecoded['modelId']); $this->assertEquals($queryStringArray['modelType'], $queryStringArrayDecoded['modelType']); $this->assertEquals($queryStringArray['personId'], $queryStringArrayDecoded['personId']); $this->assertEquals($queryStringArray['url'], $queryStringArrayDecoded['url']); $this->assertNull($queryStringArrayDecoded['type']); // do the magic, confirm magic worked by checking return value. $_GET['id'] = $queryStringArrayHash; $result = $className::resolveQueryStringFromUrlAndCreateOrUpdateActivity(); }
/** * @depends testHasAtLeastOneUnsubscribeActivity */ public function testHasAtLeastOneBounceActivity() { $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 13'); $campaign = CampaignTestHelper::createCampaign('campaign 12', 'subject 12', 'text 12', 'html 12', null, null, null, null, null, null, $marketingList); $this->assertNotNull($campaign); $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 13', Yii::app()->user->userModel); $campaignItem = CampaignItemTestHelper::createCampaignItem(0, $campaign, $contact); $this->assertNotNull($campaignItem); $this->assertFalse($campaignItem->hasAtLeastOneBounceActivity()); CampaignItemActivity::createNewActivity(CampaignItemActivity::TYPE_OPEN, $campaignItem->id, $contact->getClassId('Person'), null, null, $campaignItem); $this->assertFalse($campaignItem->hasAtLeastOneBounceActivity()); CampaignItemActivity::createNewActivity(CampaignItemActivity::TYPE_BOUNCE, $campaignItem->id, $contact->getClassId('Person'), null, null, $campaignItem); $this->assertTrue($campaignItem->hasAtLeastOneBounceActivity()); CampaignItemActivity::createNewActivity(CampaignItemActivity::TYPE_BOUNCE, $campaignItem->id, $contact->getClassId('Person'), null, null, $campaignItem); $this->assertTrue($campaignItem->hasAtLeastOneBounceActivity()); }
/** * @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); }
/** * @depends testRequiredAttributes */ public function testDeleteCampaign() { $campaigns = Campaign::getAll(); $this->assertEquals(4, count($campaigns)); CampaignItemTestHelper::createCampaignItem(0, $campaigns[0]); $campaignItems = CampaignItem::getAll(); $this->assertCount(1, $campaignItems); $campaignItemActivity = new CampaignItemActivity(); $campaignItemActivity->type = CampaignItemActivity::TYPE_CLICK; $campaignItemActivity->quantity = 1; $campaignItemActivity->campaignItem = $campaignItems[0]; $campaignItemActivity->latestSourceIP = '121.212.122.112'; $this->assertTrue($campaignItemActivity->save()); $emailMessage = EmailMessageTestHelper::createOutboxEmail(Yii::app()->user->userModel, 'subject', 'html', 'text', 'from', '*****@*****.**', 'to', '*****@*****.**'); $campaignItems[0]->emailMessage = $emailMessage; $this->assertTrue($campaignItems[0]->unrestrictedSave()); $this->assertEquals(1, CampaignItemActivity::getCount()); $this->assertEquals(1, EmailMessage::getCount()); $campaigns[0]->delete(); $this->assertEquals(3, Campaign::getCount()); $this->assertEquals(0, CampaignItem::getCount()); $this->assertEquals(0, CampaignItemActivity::getCount()); $this->assertEquals(1, EmailMessage::getCount()); }
/** * @return bool; */ public function hasAtLeastOneEventActivity($eventType) { $count = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl($eventType, $this->id, $this->contact->getClassId('Person'), null, 'latestDateTime', null, true); if ($count > 0) { return true; } return false; }
/** * @depends testCreateNewActivity */ public function testGetByTypeAndModelIdAndPersonIdAndUrl() { $type = CampaignItemActivity::TYPE_OPEN; $url = null; $persons = Person::getAll(); $this->assertNotEmpty($persons); $person = $persons[0]; $campaignItems = CampaignItem::getAll(); $this->assertNotEmpty($campaignItems); $campaignItem = $campaignItems[0]; $activities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl($type, $campaignItem->id, $person->id, $url); $this->assertNotEmpty($activities); $this->assertCount(1, $activities); $activity = $activities[0]; $this->assertEquals($type, $activity->type); $this->assertEquals(1, $activity->quantity); $this->assertEquals($person, $activity->person); $this->assertEquals($campaignItem, $activity->campaignItem); // now try same thing but with a url this time. $contact = Contact::getByName('contact 02 contact 02son'); $personId = $contact[0]->getClassId('Person'); $person = Person::getById($personId); $type = CampaignItemActivity::TYPE_CLICK; $url = 'http://www.zurmo.com'; $activities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl($type, $campaignItem->id, $personId, $url); $this->assertNotEmpty($activities); $this->assertCount(1, $activities); $activity = $activities[0]; $this->assertEquals($type, $activity->type); $this->assertEquals(1, $activity->quantity); $this->assertEquals($person, $activity->person); $this->assertEquals($campaignItem, $activity->campaignItem); }
/** * @return bool; */ public function hasAtLeastOneBounceActivity() { $count = CampaignItemActivity::getChildActivityByTypeAndModelIdAndModelRelationNameAndPersonIdAndUrl(CampaignItemActivity::TYPE_BOUNCE, $this->id, 'campaignItem', $this->contact->getClassId('Person'), null, 'latestDateTime', null, true); if ($count > 0) { return true; } return false; }
/** * @depends testTrackActionDoesNotComplainForMissingUrlParameterForCampaignItem */ public function testTrackActionThrowsRedirectExceptionForUrlParameterForCampaignItem() { $queryStringArray = array('modelId' => static::$campaignItemId, 'modelType' => 'CampaignItem', 'personId' => static::$personId, 'url' => 'http://www.zurmo.com'); $hash = StringUtil::resolveHashForQueryStringArray($queryStringArray); $this->setGetArray(array('id' => $hash)); $url = $this->runControllerWithRedirectExceptionAndGetUrl(static::TRACK_ROUTE); $this->assertEquals($queryStringArray['url'], $url); $campaignItemActivity = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(CampaignItemActivity::TYPE_CLICK, static::$campaignItemId, static::$personId, $queryStringArray['url']); $this->assertNotEmpty($campaignItemActivity); $this->assertCount(1, $campaignItemActivity); $this->assertEquals(1, $campaignItemActivity[0]->quantity); }
protected static function getClickedContentForDrillDown(CampaignItem $campaignItem) { $typesArray = CampaignItemActivity::getTypesArray(); $campaignItemActivities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(CampaignItemActivity::TYPE_CLICK, $campaignItem->id, $campaignItem->contact->getClassId('Person'), null, 'latestDateTime'); $content = null; foreach ($campaignItemActivities as $campaignItemActivity) { $content .= '<tr>'; $content .= '<td>' . $typesArray[CampaignItemActivity::TYPE_CLICK] . '</td>'; $content .= '<td>' . DateTimeUtil::convertDbFormattedDateTimeToLocaleFormattedDisplay($campaignItemActivity->latestDateTime) . '</td>'; $content .= '<td>' . $campaignItemActivity->quantity . '</td>'; $content .= '<td>' . $campaignItemActivity->latestSourceIP . '</td>'; $content .= '<td>' . $campaignItemActivity->emailMessageUrl . '</td>'; $content .= '</tr>'; } return $content; }
/** * @depends testRunWithCustomBatchSize */ public function testRunWithContactContainingPrimaryEmailOptedOut() { $unprocessedItems = CampaignItem::getByProcessed(0); $this->assertCount(1, $unprocessedItems); $job = new CampaignQueueMessagesInOutboxJob(); $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', '*****@*****.**'); $campaign = CampaignTestHelper::createCampaign('campaign 08', 'subject', '[[FIRST^NAME]]', '[[LAST^NAME]]', null, null, null, Campaign::STATUS_PROCESSING, null, null, $marketingList); $processed = 0; $campaignItem = CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact); $unprocessedItems = CampaignItem::getByProcessedAndCampaignId($processed, $campaign->id); $this->assertCount(1, $unprocessedItems); $this->assertTrue($job->run()); $unprocessedItems = CampaignItem::getByProcessedAndCampaignId($processed, $campaign->id); $this->assertCount(0, $unprocessedItems); $personId = $contact->getClassId('Person'); $activities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(CampaignItemActivity::TYPE_SKIP, $campaignItem->id, $personId); $this->assertNotEmpty($activities); $this->assertCount(1, $activities); }
/** * Special method to load a campaign with all types of campaignItemActivity */ public function actionLoadCampaignWithAllItemActivityTypes() { if (!Group::isUserASuperAdministrator(Yii::app()->user->userModel)) { throw new NotSupportedException(); } $emailBox = EmailBoxUtil::getDefaultEmailBoxByUser(Yii::app()->user->userModel); $marketingList = new MarketingList(); $marketingList->name = 'Demo Marketing List'; $marketingList->save(); $campaign = new Campaign(); $campaign->marketingList = $marketingList; $campaign->name = 'Campaign with all campaignItemActivity'; $campaign->subject = 'Demo for all types of campaignItemActivities'; $campaign->status = Campaign::STATUS_COMPLETED; $campaign->sendOnDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $campaign->supportsRichText = true; $campaign->htmlContent = 'Demo content'; $campaign->fromName = 'Zurmo'; $campaign->fromAddress = '*****@*****.**'; $campaign->enableTracking = true; $saved = $campaign->save(); if (!$saved) { throw new FailedToSaveModelException(); } $contacts = Contact::getAll(); //Awaiting queue $contact = $contacts[0]; $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $campaignItem->unrestrictedSave(); //Contact is not subscribed $contact = $contacts[1]; $marketingList->addNewMember($contact->id, true); $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->quantity = 1; $activity->type = CampaignItemActivity::TYPE_SKIP; $activity->save(); $campaignItem->unrestrictedSave(); //Skipped, both primary and secondary are opted out $contact = $contacts[2]; $contact->primaryEmail->emailAddress = $contact->firstName . '*****@*****.**'; $contact->primaryEmail->optOut = true; $contact->secondaryEmail->emailAddress = $contact->firstName . '*****@*****.**'; $contact->secondaryEmail->optOut = true; $contact->save(); $marketingList->addNewMember($contact->id); $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->quantity = 1; $activity->type = CampaignItemActivity::TYPE_SKIP; $activity->save(); $campaignItem->unrestrictedSave(); //Skipped, primary is opted out but secondary is not $contact = $contacts[3]; $contact->primaryEmail->emailAddress = $contact->firstName . '*****@*****.**'; $contact->primaryEmail->optOut = true; $contact->secondaryEmail->emailAddress = $contact->firstName . '*****@*****.**'; $contact->secondaryEmail->optOut = false; $contact->save(); $marketingList->addNewMember($contact->id); $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->quantity = 1; $activity->type = CampaignItemActivity::TYPE_SKIP; $activity->save(); $campaignItem->unrestrictedSave(); //Skipped, primary and secondary not set $contact = $contacts[4]; $contact->primaryEmail = null; $contact->secondaryEmail = null; $contact->save(); $marketingList->addNewMember($contact->id); $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->quantity = 1; $activity->type = CampaignItemActivity::TYPE_SKIP; $activity->save(); $campaignItem->unrestrictedSave(); //Skipped, primary not set but secondary is set $contact = $contacts[5]; $contact->primaryEmail = null; $contact->secondaryEmail->emailAddress = $contact->firstName . '@zurmo.org'; $contact->secondaryEmail->optOut = false; $contact->save(); $marketingList->addNewMember($contact->id); $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->quantity = 1; $activity->type = CampaignItemActivity::TYPE_SKIP; $activity->save(); $campaignItem->unrestrictedSave(); //Queued $contact = $contacts[6]; $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->quantity = 1; $activity->save(); $emailMessage = new EmailMessage(); $emailMessage->setScenario('importModel'); $emailMessage->owner = $contact->owner; $emailMessage->subject = 'Subject'; $emailContent = new EmailMessageContent(); $emailContent->textContent = 'My First Message'; $emailContent->htmlContent = 'Some fake HTML content'; $emailMessage->content = $emailContent; //Sending is current user (super) $sender = new EmailMessageSender(); $sender->fromAddress = '*****@*****.**'; $sender->fromName = 'Zurmo'; $sender->personsOrAccounts->add(Yii::app()->user->userModel); $emailMessage->sender = $sender; //Recipient is BobMessage $recipient = new EmailMessageRecipient(); $recipient->toAddress = '*****@*****.**'; $recipient->toName = strval($contact); $recipient->personsOrAccounts->add($contact); $recipient->type = EmailMessageRecipient::TYPE_TO; $emailMessage->recipients->add($recipient); $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_OUTBOX); $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $emailMessage->save(); if (!$saved) { throw new FailedToSaveModelException(); } $campaignItem->emailMessage = $emailMessage; $campaignItem->unrestrictedSave(); //Queued with error $contact = $contacts[7]; $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->quantity = 1; $activity->save(); $emailMessage = new EmailMessage(); $emailMessage->setScenario('importModel'); $emailMessage->owner = $contact->owner; $emailMessage->subject = 'Subject'; $emailContent = new EmailMessageContent(); $emailContent->textContent = 'My First Message'; $emailContent->htmlContent = 'Some fake HTML content'; $emailMessage->content = $emailContent; //Sending is current user (super) $sender = new EmailMessageSender(); $sender->fromAddress = '*****@*****.**'; $sender->fromName = 'Zurmo'; $sender->personsOrAccounts->add(Yii::app()->user->userModel); $emailMessage->sender = $sender; //Recipient is BobMessage $recipient = new EmailMessageRecipient(); $recipient->toAddress = '*****@*****.**'; $recipient->toName = strval($contact); $recipient->personsOrAccounts->add($contact); $recipient->type = EmailMessageRecipient::TYPE_TO; $emailMessage->recipients->add($recipient); $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_OUTBOX_ERROR); $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $emailMessage->sendAttempts = 2; $emailMessageError = new EmailMessageSendError(); $emailMessageError->serializedData = serialize(array('code' => '0001', 'message' => 'Error Message')); $emailMessage->error = $emailMessageError; $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $emailMessage->save(); if (!$saved) { throw new FailedToSaveModelException(); } $campaignItem->emailMessage = $emailMessage; $campaignItem->unrestrictedSave(); //Failure $contact = $contacts[8]; $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->quantity = 1; $activity->save(); $emailMessage = new EmailMessage(); $emailMessage->setScenario('importModel'); $emailMessage->owner = $contact->owner; $emailMessage->subject = 'Subject'; $emailContent = new EmailMessageContent(); $emailContent->textContent = 'My First Message'; $emailContent->htmlContent = 'Some fake HTML content'; $emailMessage->content = $emailContent; //Sending is current user (super) $sender = new EmailMessageSender(); $sender->fromAddress = '*****@*****.**'; $sender->fromName = 'Zurmo'; $sender->personsOrAccounts->add(Yii::app()->user->userModel); $emailMessage->sender = $sender; //Recipient is BobMessage $recipient = new EmailMessageRecipient(); $recipient->toAddress = '*****@*****.**'; $recipient->toName = strval($contact); $recipient->personsOrAccounts->add($contact); $recipient->type = EmailMessageRecipient::TYPE_TO; $emailMessage->recipients->add($recipient); $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_OUTBOX_FAILURE); $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $emailMessage->sendAttempts = 3; $emailMessageError = new EmailMessageSendError(); $emailMessageError->serializedData = serialize(array('code' => '0001', 'message' => 'Error Message')); $emailMessage->error = $emailMessageError; $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $emailMessage->save(); if (!$saved) { throw new FailedToSaveModelException(); } $campaignItem->emailMessage = $emailMessage; $campaignItem->unrestrictedSave(); //Sent, open, click, bounce $contact = $contacts[9]; $campaignItem = new CampaignItem(); $campaignItem->processed = true; $campaignItem->campaign = $campaign; $campaignItem->contact = $contact; $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->type = CampaignItemActivity::TYPE_CLICK; $activity->quantity = rand(1, 50); $activity->latestDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + rand(100, 1000)); $activity->latestSourceIP = '10.11.12.13'; $activity->save(); $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->type = CampaignItemActivity::TYPE_OPEN; $activity->quantity = rand(1, 50); $activity->latestDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + rand(100, 1000)); $activity->latestSourceIP = '10.11.12.13'; $activity->save(); $activity = new CampaignItemActivity(); $activity->person = $contact; $activity->campaignItem = $campaignItem; $activity->type = CampaignItemActivity::TYPE_BOUNCE; $activity->quantity = rand(1, 50); $activity->latestDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + rand(100, 1000)); $activity->latestSourceIP = '10.11.12.13'; $activity->save(); $emailMessage = new EmailMessage(); $emailMessage->setScenario('importModel'); $emailMessage->owner = $contact->owner; $emailMessage->subject = 'Subject'; $emailContent = new EmailMessageContent(); $emailContent->textContent = 'My First Message'; $emailContent->htmlContent = 'Some fake HTML content'; $emailMessage->content = $emailContent; //Sending is current user (super) $sender = new EmailMessageSender(); $sender->fromAddress = '*****@*****.**'; $sender->fromName = 'Zurmo'; $sender->personsOrAccounts->add(Yii::app()->user->userModel); $emailMessage->sender = $sender; //Recipient is BobMessage $recipient = new EmailMessageRecipient(); $recipient->toAddress = '*****@*****.**'; $recipient->toName = strval($contact); $recipient->personsOrAccounts->add($contact); $recipient->type = EmailMessageRecipient::TYPE_TO; $emailMessage->recipients->add($recipient); $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_SENT); $emailMessage->sentDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $emailMessage->save(); if (!$saved) { throw new FailedToSaveModelException(); } $emailMessage->save(); $campaignItem->emailMessage = $emailMessage; $campaignItem->unrestrictedSave(); }