protected function renderUnsubscribedCountContent() { $count = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($this->modelId, true); $message = Zurmo::t('MarketingListsModule', '{count} Unsubscribed', array('{count}' => $count)); $content = ZurmoHtml::tag('strong', array('class' => static::UNSUBSCRIBERS_STATS_CLASS), $message); return $content; }
public function actionCountMembers($marketingListId) { $marketingList = MarketingList::getById((int) $marketingListId); ControllerSecurityUtil::resolveAccessCanCurrentUserReadModel($marketingList); $countArray = array('subscriberCount' => MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingListId, false), 'unsubscriberCount' => MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingListId, true)); echo CJSON::encode($countArray); }
public function setUp() { parent::setUp(); $this->user = User::getByUsername('super'); Yii::app()->user->userModel = $this->user; Campaign::deleteAll(); CampaignItem::deleteAll(); Contact::deleteAll(); MarketingList::deleteAll(); MarketingListMember::deleteAll(); }
/** * @param DemoDataHelper $demoDataHelper */ public function makeAll(&$demoDataHelper) { assert('$demoDataHelper instanceof DemoDataHelper'); assert('$demoDataHelper->isSetRange("Contact")'); assert('$demoDataHelper->isSetRange("MarketingList")'); $members = array(); foreach (MarketingList::getAll() as $marketingList) { foreach (Contact::getAll() as $contact) { $interval = mt_rand(1, 30) * 86400; $member = new MarketingListMember(); $member->setScenario('importModel'); $member->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - $interval); $member->contact = $contact; $member->marketingList = $marketingList; $this->populateModel($member); $saved = $member->unrestrictedSave(); assert('$saved'); $members[] = $member->id; } } $demoDataHelper->setRangeByModelName('MarketingListMember', $members[0], $members[count($members) - 1]); }
/** * @param string $beginDateTime * @param array $searchAttributeData * @return string */ protected static function makeColumnSqlQuery($beginDateTime, $searchAttributeData) { assert('is_string($beginDateTime)'); $quote = DatabaseCompatibilityUtil::getQuote(); $where = null; $selectDistinct = false; $marketingListTableName = MarketingList::getTableName(); $marketingListMemberTableName = MarketingListMember::getTableName(); $createdDateTimeColumnName = MarketingListMember::getColumnNameByAttribute('createdDateTime'); $unsubscribedColumnName = MarketingListMember::getColumnNameByAttribute('unsubscribed'); $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('MarketingList'); $where = RedBeanModelDataProvider::makeWhere('MarketingList', $searchAttributeData, $joinTablesAdapter); MarketingList::resolveReadPermissionsOptimizationToSqlQuery(Yii::app()->user->userModel, $joinTablesAdapter, $where, $selectDistinct); $selectQueryAdapter = new RedBeanModelSelectQueryAdapter($selectDistinct); $newSubscriberSelectPart = "sum(CASE WHEN {$quote}{$marketingListMemberTableName}{$quote}.{$quote}{$createdDateTimeColumnName}" . $quote . " >= '{$beginDateTime}' THEN 1 ELSE 0 END)"; $existingSubscriberSelectPart = "sum(CASE WHEN {$quote}{$marketingListMemberTableName}{$quote}.{$quote}{$createdDateTimeColumnName}" . $quote . " < '{$beginDateTime}' AND " . "{$quote}{$marketingListMemberTableName}{$quote}.{$quote}" . "{$unsubscribedColumnName}{$quote}=0 THEN 1 ELSE 0 END)"; // Not Coding Standard $selectQueryAdapter->addClauseByQueryString($newSubscriberSelectPart, static::NEW_SUBSCRIBERS_COUNT); $selectQueryAdapter->addClauseByQueryString($existingSubscriberSelectPart, static::EXISTING_SUBSCRIBERS_COUNT); $joinTablesAdapter->addLeftTableAndGetAliasName($marketingListMemberTableName, 'id', $marketingListTableName, 'marketinglist_id'); $sql = SQLQueryUtil::makeQuery($marketingListTableName, $selectQueryAdapter, $joinTablesAdapter, null, null, $where); return $sql; }
public static function processDueItem(OwnedModel $item) { assert('is_object($item)'); $itemId = $item->id; $itemClass = get_class($item); assert('$itemClass === "AutoresponderItem" || $itemClass === "CampaignItem"'); $contact = $item->contact; if (empty($contact) || $contact->id < 0) { throw new NotFoundException(); } $ownerModelRelationName = static::resolveItemOwnerModelRelationName($itemClass); $itemOwnerModel = $item->{$ownerModelRelationName}; assert('is_object($itemOwnerModel)'); assert('get_class($itemOwnerModel) === "Autoresponder" || get_class($itemOwnerModel) === "Campaign"'); if ($contact->primaryEmail->optOut || get_class($itemOwnerModel) === "Campaign" && MarketingListMember::getByMarketingListIdContactIdAndUnsubscribed($itemOwnerModel->marketingList->id, $contact->id, true) != false) { $activityClass = $itemClass . 'Activity'; $personId = $contact->getClassId('Person'); $type = $activityClass::TYPE_SKIP; $activityClass::createNewActivity($type, $itemId, $personId); } else { $marketingList = $itemOwnerModel->marketingList; assert('is_object($marketingList)'); assert('get_class($marketingList) === "MarketingList"'); $textContent = $itemOwnerModel->textContent; $htmlContent = null; if ($itemClass == 'CampaignItem' && $itemOwnerModel->supportsRichText || $itemClass == 'AutoresponderItem') { $htmlContent = $itemOwnerModel->htmlContent; } static::resolveContent($textContent, $htmlContent, $contact, $itemOwnerModel->enableTracking, (int) $itemId, $itemClass, (int) $marketingList->id); try { $item->emailMessage = static::resolveEmailMessage($textContent, $htmlContent, $itemOwnerModel, $contact, $marketingList, $itemId, $itemClass); } catch (MissingRecipientsForEmailMessageException $e) { // TODO: @Shoaibi/@Jason: Medium: Do something about it. } } static::markItemAsProcessed($item); }
/** * @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); }
/** * @depends testMassUnsubscribeActionsForSelectedIds */ public function testMassUnsubscribePagesProperlyAndUnsubscribesAllSelected() { // MassUnsubscribe for selected Record Count $listId = self::getModelIdByModelNameAndName('MarketingList', 'MarketingList2'); $this->assertNotEmpty($listId); $list = MarketingList::getById($listId); $this->assertNotEmpty($list); $members = $list->marketingListMembers; $this->assertEquals(17, count($members)); // ensure all members are subscribed first. foreach ($members as $member) { $member->unsubscribed = 0; $member->setScenario(MarketingListMember::SCENARIO_MANUAL_CHANGE); $this->assertTrue($member->unrestrictedSave()); } $unsubscribedCount = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($listId, 1); $this->setGetArray(array('selectAll' => '1', 'MarketingListMembersPortletView_page' => 1, 'id' => $listId)); $this->setPostArray(array('selectedRecordCount' => 17)); // Run Mass Unsubscribe using progress save for page1. $pageSize = Yii::app()->pagination->getForCurrentUserByType('massEditProgressPageSize'); $this->assertEquals(5, $pageSize); $this->runControllerWithExitExceptionAndGetContent('marketingLists/member/massUnsubscribe'); $expectedUnsubscribedCountAfterFirstRequest = $unsubscribedCount + $pageSize; // because the subscribe tests subscribed all $actualUnsubscribedCountAfterFirstRequest = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($listId, 1); $this->assertEquals($expectedUnsubscribedCountAfterFirstRequest, $actualUnsubscribedCountAfterFirstRequest); $this->setGetArray(array('selectAll' => '1', 'MarketingListMember_page' => 2, 'id' => $listId)); $this->setPostArray(array('selectedRecordCount' => 17)); // Run Mass Unsubscribe using progress save for page2. $pageSize = Yii::app()->pagination->getForCurrentUserByType('massEditProgressPageSize'); $this->assertEquals(5, $pageSize); $this->runControllerWithNoExceptionsAndGetContent('marketingLists/member/massUnsubscribeProgress'); $expectedUnsubscribedCountAfterSecondRequest = $expectedUnsubscribedCountAfterFirstRequest + 5; // because the subscribe tests subscribed all $actualUnsubscribedCountAfterSecondRequest = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($listId, 1); $this->assertEquals($expectedUnsubscribedCountAfterSecondRequest, $actualUnsubscribedCountAfterSecondRequest); $this->setGetArray(array('selectAll' => '1', 'MarketingListMember_page' => 3, 'id' => $listId)); $this->setPostArray(array('selectedRecordCount' => 17)); // Run Mass Unsubscribe using progress save for page3. $pageSize = Yii::app()->pagination->getForCurrentUserByType('massEditProgressPageSize'); $this->assertEquals(5, $pageSize); $this->runControllerWithNoExceptionsAndGetContent('marketingLists/member/massUnsubscribeProgress'); $expectedUnsubscribedCountAfterThirdRequest = $expectedUnsubscribedCountAfterSecondRequest + 5; // because the subscribe tests subscribed all $actualUnsubscribedCountAfterThirdRequest = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($listId, 1); $this->assertEquals($expectedUnsubscribedCountAfterThirdRequest, $actualUnsubscribedCountAfterThirdRequest); $this->setGetArray(array('selectAll' => '1', 'MarketingListMember_page' => 4, 'id' => $listId)); $this->setPostArray(array('selectedRecordCount' => 17)); // Run Mass Unsubscribe using progress save for page4. $pageSize = Yii::app()->pagination->getForCurrentUserByType('massEditProgressPageSize'); $this->assertEquals(5, $pageSize); $this->runControllerWithNoExceptionsAndGetContent('marketingLists/member/massUnsubscribeProgress'); $expectedUnsubscribedCountAfterFourthRequest = $expectedUnsubscribedCountAfterThirdRequest + 2; // because the subscribe tests subscribed all $actualUnsubscribedCountAfterFourthRequest = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($listId, 1); $this->assertEquals($expectedUnsubscribedCountAfterFourthRequest, $actualUnsubscribedCountAfterFourthRequest); $subscribedCount = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($listId, 0); $this->assertEquals(0, $subscribedCount); }
protected function resolveMembers($unsubscribed, Contact $contact, $marketingListId, $optOut) { if ($optOut) { $members = MarketingListMember::getByContactIdAndSubscribed($contact->id, $unsubscribed); } else { $members = MarketingListMember::getByMarketingListIdContactIdAndUnsubscribed($marketingListId, $contact->id, $unsubscribed); } if (!is_array($members) && $members !== false) { $members = array($members); } if (empty($members)) { $marketingList = MarketingList::getById(intval($marketingListId)); if ($unsubscribed === 1 && !empty($marketingList) && $marketingList->anyoneCanSubscribe && !$marketingList->memberAlreadyExists($contact->id)) { $members[0] = new MarketingListMember(); $members[0]->contact = $contact; $members[0]->marketingList = $marketingList; } else { return false; } } return $members; }
public function testUnsubscribeContactFromList() { //Empty members from the test marketing list $marketingList = MarketingList::getById(self::$marketingListId); foreach ($marketingList->marketingListMembers as $marketingListMember) { $marketingListMember->delete(); } $marketingList->forget(); $marketingList = MarketingList::getById(self::$marketingListId); $this->assertEquals(0, $marketingList->marketingListMembers->count()); //Subscribe new member $contact = ContactTestHelper::createContactByNameForOwner('jason', Yii::app()->user->userModel); $marketingList->addNewMember($contact->id); $marketingList->forget(); $marketingList = MarketingList::getById(self::$marketingListId); $this->assertEquals(1, $marketingList->marketingListMembers->count()); $this->assertEquals(0, $marketingList->marketingListMembers[0]->unsubscribed); //Try to unsubscribe the contact, it should unsubscribe them $action = new ActionForWorkflowForm('Contact', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_UNSUBSCRIBE_FROM_LIST; $attributes = array('marketingList' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => self::$marketingListId)); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $helper = new WorkflowActionProcessingHelper(88, 'some name', $action, $contact, Yii::app()->user->userModel); $helper->processNonUpdateSelfAction(); $marketingList->forget(); $marketingList = MarketingList::getById(self::$marketingListId); $this->assertEquals(1, $marketingList->marketingListMembers->count()); $this->assertEquals(1, $marketingList->marketingListMembers[0]->unsubscribed); //Subscribe contact from list, then unsubscribe $marketingListMembers = MarketingListMember::getAll(); $this->assertEquals(1, count($marketingListMembers)); $marketingListMembers[0]->unsubscribed = false; $marketingListMembers[0]->unrestrictedSave(); $marketingList->forget(); $marketingList = MarketingList::getById(self::$marketingListId); $this->assertEquals(1, $marketingList->marketingListMembers->count()); $this->assertEquals(0, $marketingList->marketingListMembers[0]->unsubscribed); //Try to unsubscribe the contact, it should unsubscribe them $helper = new WorkflowActionProcessingHelper(88, 'some name', $action, $contact, Yii::app()->user->userModel); $helper->processNonUpdateSelfAction(); $marketingList->forget(); $marketingList = MarketingList::getById(self::$marketingListId); $this->assertEquals(1, $marketingList->marketingListMembers->count()); $this->assertEquals(1, $marketingList->marketingListMembers[0]->unsubscribed); }
protected function processUnsubscribeFromListAction() { $actionAttributes = $this->action->getActionAttributes(); if (count($actionAttributes) > 1 || !isset($actionAttributes['marketingList']) || !$this->triggeredModel instanceof Contact) { throw new NotSupportedException(); } $marketingListId = $actionAttributes['marketingList']->value; $members = MarketingListMember::getByMarketingListIdContactIdAndUnsubscribed($marketingListId, (int) $this->triggeredModel->id, false); if ($members !== false) { $member = $members[0]; $member->unsubscribed = true; if (!$member->unrestrictedSave()) { throw new FailedToSaveModelException(); } } else { try { //Does the marketing list still exist MarketingList::getById((int) $marketingListId); } catch (NotFoundException $e) { $this->logAndNotifyOnMissingMarketingListModel(ActionForWorkflowForm::TYPE_UNSUBSCRIBE_FROM_LIST); } } }
public function actionUserInterface($type = null) { if (!Group::isUserASuperAdministrator(Yii::app()->user->userModel)) { throw new NotSupportedException(); } if ($type == null) { $demoView = new MenuUserInterfaceDemoView(); $view = new ZurmoConfigurationPageView(ZurmoDefaultAdminViewUtil::makeStandardViewForCurrentUser($this, $demoView)); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::STANDARD_VIEW) { $demoView = new StandardUserInterfaceDemoView(); $demoView->message = 'Standard View'; $view = new ZurmoConfigurationPageView(ZurmoDefaultViewUtil::makeStandardViewForCurrentUser($this, $demoView)); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::STANDARD_BREADCRUMBS_VIEW) { $breadCrumbLinks = array('Breadcrumb 1' => array('/zurmo/demo/userInterface'), 'Breadcrumb 2'); $demoView = new StandardUserInterfaceDemoView(); $demoView->message = 'Standard View with BreadCrumbs'; $view = new ZurmoConfigurationPageView(ZurmoDefaultViewUtil::makeViewWithBreadcrumbsForCurrentUser($this, $demoView, $breadCrumbLinks, 'SettingsBreadCrumbView')); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::GRACEFUL_ERROR_VIEW) { $demoView = new StandardUserInterfaceDemoView(); $demoView->message = 'Graceful Error View'; $view = new ZurmoConfigurationPageView(ZurmoDefaultViewUtil::makeErrorViewForCurrentUser($this, $demoView)); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::UNEXPECTED_ERROR_VIEW) { $view = new ErrorPageView('Unexpected error view'); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::AUTHORIZATION_VIEW) { $demoView = new StandardUserInterfaceDemoView(); $demoView->message = 'Authorization View'; $view = new ZurmoConfigurationPageView(ZurmoDefaultViewUtil::makeAuthorizationViewForCurrentUser($this, $demoView)); $view->setCssClasses(array_merge($view->getCssClasses(), array('ZurmoAuthorizationPageView'))); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::CONTACT_FORM_EXTERNAL_VIEW) { $containedView = new ContactExternalEditAndDetailsView('Edit', $this->getId(), $this->getModule()->getId(), new ContactWebFormsModelForm(new Contact()), ContactExternalEditAndDetailsView::getMetadata()); $view = new ContactWebFormsExternalPageView(ZurmoExternalViewUtil::makeExternalViewForCurrentUser($containedView)); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::MARKETING_LISTS_EXTERNAL_PREVIEW_VIEW) { $splashView = new MarketingListsExternalActionsPreviewView(); $view = new MarketingListsExternalActionsPageView($this, $splashView); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::MARKETING_LISTS_MANAGE_SUBSCRIPTIONS_VIEW) { $marketingListMember = MarketingListMember::getSubset(null, 0, 1); $marketingLists = MarketingList::getByUnsubscribedAndAnyoneCanSubscribe($marketingListMember[0]->contact->id); $listView = new MarketingListsManageSubscriptionsListView($this->getId(), $this->getModule()->getId(), $marketingLists, -100, -100, -100, 'notUsed'); $view = new MarketingListsManageSubscriptionsPageView($this, $listView); echo $view->render(); } elseif ($type == MenuUserInterfaceDemoView::MOBILE_HEADER_VIEW) { Yii::app()->userInterface->setSelectedUserInterfaceType(UserInterface::MOBILE); $demoView = new StandardUserInterfaceDemoView(); $demoView->message = 'Standard View'; $view = new ZurmoConfigurationPageView(ZurmoDefaultViewUtil::makeStandardViewForCurrentUser($this, $demoView)); echo $view->render(); } else { throw new NotSupportedException(); } }
protected static function skipMessage(Contact $contact, Item $itemOwnerModel) { return $contact->primaryEmail->optOut || get_class($itemOwnerModel) === "Campaign" && MarketingListMember::getByMarketingListIdContactIdAndUnsubscribed($itemOwnerModel->marketingList->id, $contact->id, true) != false; }
public function addNewMember($contactId, $unsubscribed = false, $contact = null, $scenario = null) { $member = new MarketingListMember(); if (empty($contact)) { $contact = Contact::getById($contactId); } $member->contact = $contact; $member->unsubscribed = $unsubscribed; $member->marketingList = $this; if (isset($scenario)) { $member->setScenario($scenario); } if ($this->memberAlreadyExists($contact->id)) { return false; } return $member->unrestrictedSave(); }
/** * @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()); }
private function createMarketingListMember($contact, $createdDateTime) { $marketingListMember = new MarketingListMember(); $marketingListMember->setScenario('importModel'); $marketingListMember->unsubscribed = false; $marketingListMember->contact = $contact; $marketingListMember->marketingList = $this->marketingList; $marketingListMember->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(strtotime($createdDateTime)); $this->assertTrue($marketingListMember->unrestrictedSave()); return $marketingListMember; }
public function testMassSubscribeActionsForSelectedIds() { $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super'); $contactsCount = Contact::getCount(); $superContactId1 = ContactTestHelper::createContactByNameForOwner('superContact1', $super); $superContactId2 = ContactTestHelper::createContactByNameForOwner('superContact2', $super); $superContactId3 = ContactTestHelper::createContactByNameForOwner('superContact3', $super); $superContactId4 = ContactTestHelper::createContactByNameForOwner('superContact4', $super); $superContactId5 = ContactTestHelper::createContactByNameForOwner('superContact5', $super); $superContactId6 = ContactTestHelper::createContactByNameForOwner('superContact6', $super); $superContactId7 = ContactTestHelper::createContactByNameForOwner('superContact7', $super); $superContactId8 = ContactTestHelper::createContactByNameForOwner('superContact8', $super); $superContactId9 = ContactTestHelper::createContactByNameForOwner('superContact9', $super); $marketingList1 = MarketingListTestHelper::createMarketingListByName('marketingList1'); $marketingList2 = MarketingListTestHelper::createMarketingListByName('marketingList2'); //Load Model MassSubscribe Views. //MassSubscribe view for single selected ids $selectedIds = implode(',', array($superContactId1->id, $superContactId4->id)); // Not Coding Standard $this->setGetArray(array('selectedIds' => $selectedIds, 'selectAll' => '')); $this->resetPostArray(); $content = $this->runControllerWithNoExceptionsAndGetContent('contacts/default/massSubscribe'); $this->assertContains('<strong>2</strong> Contacts selected for subscription', $content); //MassSubscribe view for all result selected ids $this->setGetArray(array('selectAll' => '1')); $this->resetPostArray(); $content = $this->runControllerWithNoExceptionsAndGetContent('contacts/default/massSubscribe'); $this->assertContains('<strong>9</strong> Contacts selected for subscription', $content); //MassSubscribe with no marketingList selected //MassSubscribe for selected ids for paged scenario //MassSubscribe for selected ids for page 1 $pageSize = Yii::app()->pagination->getForCurrentUserByType('massEditProgressPageSize'); $this->assertEquals(5, $pageSize); $selectedIds = implode(',', array($superContactId1->id, $superContactId2->id, $superContactId3->id, $superContactId5->id, $superContactId6->id, $superContactId8->id, $superContactId9->id)); $this->setGetArray(array('selectedIds' => $selectedIds, 'selectAll' => '', 'massSubscribe' => '', 'Contact_page' => 1)); $this->setPostArray(array('selectedRecordCount' => 7, 'MarketingListMember' => array('marketingList' => array('id' => $marketingList1->id)))); $this->runControllerWithExitExceptionAndGetContent('contacts/default/massSubscribe'); $expectedSubscribedCountAfterFirstRequest = $pageSize; $actualSubscribedCountAfterFirstRequest = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingList1->id, 0); $this->assertEquals($expectedSubscribedCountAfterFirstRequest, $actualSubscribedCountAfterFirstRequest); $this->assertEquals(0, MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingList2->id, 0)); //MassSubscribe for selected ids for page 2 $this->setGetArray(array('selectedIds' => $selectedIds, 'selectAll' => '', 'massSubscribe' => '', 'Contact_page' => 2, 'MarketingListMember' => array('marketingList' => array('id' => $marketingList1->id)))); $this->setPostArray(array('selectedRecordCount' => 7, 'MarketingListMember' => array('marketingList' => array('id' => $marketingList1->id)))); $this->runControllerWithNoExceptionsAndGetContent('contacts/default/massSubscribeProgress'); $expectedSubscribedCountAfterSecondRequest = $actualSubscribedCountAfterFirstRequest + (7 - $pageSize); $actualSubscribedCountAfterSecondRequest = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingList1->id, 0); $this->assertEquals($expectedSubscribedCountAfterSecondRequest, $actualSubscribedCountAfterSecondRequest); $this->assertEquals(0, MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingList2->id, 0)); }
/** * @depends testOptOutAction */ public function testSubscribeActionAfterOptOutActionDisableOptOut() { $this->logoutCurrentUserLoginNewUserAndGetByUsername('super'); $marketingList = MarketingList::getByName('marketingList 04'); $this->assertNotEmpty($marketingList); $marketingList = $marketingList[0]; $marketingListId = $marketingList->id; $contact = Contact::getByName('contact 05 contact 05son'); $this->assertNotEmpty($contact); $contact = $contact[0]; $this->assertEquals(1, $contact->primaryEmail->optOut); $personId = $contact->getClassId('Person'); $member = MarketingListMember::getByMarketingListIdContactIdAndSubscribed($marketingList->id, $contact->id, 1); $this->assertNotEmpty($member); Yii::app()->user->userModel = null; $hash = EmailMessageActivityUtil::resolveHashForUnsubscribeAndManageSubscriptionsUrls($personId, $marketingListId, 1, 'AutoresponderItem', false); $this->setGetArray(array('hash' => $hash)); @$this->runControllerWithRedirectExceptionAndGetUrl($this->subscribeUrl); $content = $this->runControllerWithNoExceptionsAndGetContent($this->manageSubscriptionsUrl); $this->assertTrue(strpos($content, '<td>marketingList 01</td>') !== false); $this->assertTrue(strpos($content, '<td>marketingList 03</td>') !== false); $this->assertTrue(strpos($content, 'marketingLists/external/subscribe?hash=') !== false); $this->assertTrue(strpos($content, 'id="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_') !== false); $this->assertTrue(strpos($content, 'id="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_') !== false); $this->assertTrue(strpos($content, 'type="radio" name="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_') !== false); $this->assertTrue(strpos($content, 'id="marketingListsManageSubscriptionListView-toggleUnsubscribed_' . $marketingListId . '_0" checked="checked" type="radio" name="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_' . $marketingListId) !== false); $this->logoutCurrentUserLoginNewUserAndGetByUsername('super'); $contact = Contact::getByName('contact 05 contact 05son'); $this->assertNotEmpty($contact); $contact = $contact[0]; $this->assertEquals(0, $contact->primaryEmail->optOut); }
/** * @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); }
protected function triggerMarketingListMemberMassAction() { $this->triggerMassAction('MarketingListMember', static::getSearchFormClassName(), 'MarketingListMembersPageView', MarketingListMember::getModelLabelByTypeAndLanguage('Plural'), 'MarketingListMembersSearchView', null, false); }
public function skipMessage(Contact $contact, Item $itemOwnerModel) { // TODO: @Shoaibi: Critical0: We could use SQL for getByMarketingListIdContactIdandUnsubscribed to save further performance here. if ($contact->primaryEmail->optOut) { return true; } $marketingListMember = MarketingListMember::getByMarketingListIdAndContactId($itemOwnerModel->marketingList->id, $contact->id); if ($marketingListMember === false) { return true; } else { $marketingListMemberObject = $marketingListMember[0]; $itemOwnerModelClassName = get_class($itemOwnerModel); if ($itemOwnerModelClassName === "Campaign" && $marketingListMemberObject->unsubscribed == true) { return true; } elseif ($itemOwnerModelClassName === "Autoresponder") { if ($itemOwnerModel->operationType == Autoresponder::OPERATION_SUBSCRIBE && $marketingListMemberObject->unsubscribed == true) { return true; } elseif ($itemOwnerModel->operationType == Autoresponder::OPERATION_UNSUBSCRIBE && $marketingListMemberObject->unsubscribed == false) { return true; } } } return false; }
/** * @param $contact * Contact::beforeDelete() resolves associated marketingListMembers, cascades deletion */ public static function resolveMarketingListMembersByContact($contact) { if ($contact->id != null && $contact->id > 0) { $marketingListMembers = MarketingListMember::getByContactId($contact->id); foreach ($marketingListMembers as $marketingListMember) { $marketingListMember->delete(); } } }
public function addNewMember($contactId, $unsubscribed = false, $contact = null) { $member = new MarketingListMember(); if (empty($contact)) { $contact = Contact::getById($contactId); } $member->contact = $contact; $member->unsubscribed = $unsubscribed; $member->marketingList = $this; if ($this->memberAlreadyExists($contact->id)) { return false; } else { $saved = $member->unrestrictedSave(); if ($saved) { return true; } else { throw new FailedToSaveModelException(); } } }
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 testDeleteContactCascadesMarketingListMemmers() { Yii::app()->user->userModel = User::getByUsername('super'); $contactStates = ContactState::getByName('Qualified'); $contact = new Contact(); $contact->owner = Yii::app()->user->userModel; $contact->title->value = 'Mr.'; $contact->firstName = 'Member'; $contact->lastName = 'One'; $contact->state = $contactStates[0]; $this->assertTrue($contact->save()); $contactId = $contact->id; $marketingList = MarketingListTestHelper::createMarketingListByName('Test Marketing List Member Deleted'); $member = MarketingListMemberTestHelper::populateValidMarketingListMember(1, $marketingList, $contact); $this->assertTrue($member->unrestrictedSave()); $this->assertEquals(1, count(MarketingListMember::getByContactId($contactId))); $testContact = new Contact(); $testContact->owner = Yii::app()->user->userModel; $testContact->title->value = 'Mr.'; $testContact->firstName = 'Member'; $testContact->lastName = 'Two'; $testContact->state = $contactStates[0]; $this->assertTrue($testContact->save()); $testContactId = $testContact->id; $member2 = MarketingListMemberTestHelper::populateValidMarketingListMember(1, $marketingList, $testContact); $this->assertTrue($member2->unrestrictedSave()); $this->assertEquals(1, count(MarketingListMember::getByContactId($testContact->id))); $subscribedCount = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingList->id, 1); $this->assertEquals(2, $subscribedCount); $this->assertTrue($contact->delete()); $this->assertEquals(0, count(MarketingListMember::getByContactId($contactId))); $this->assertTrue($testContact->delete()); $this->assertEquals(0, count(MarketingListMember::getByContactId($testContactId))); $subscribedCount = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingList->id, 1); $this->assertEquals(0, $subscribedCount); }
protected static function getSkippedContentForDrillDown(CampaignItem $campaignItem) { $campaignItemActivities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(CampaignItemActivity::TYPE_SKIP, $campaignItem->id, $campaignItem->contact->getClassId('Person'), null, 'latestDateTime'); $content = Zurmo::t('MarketingModule', 'The message was not created.'); if ($campaignItem->contact->primaryEmail->emailAddress == null) { if ($campaignItem->contact->secondaryEmail->emailAddress == null) { $content = Zurmo::t('MarketingModule', 'Contact has no primary email address populated.'); } else { $content = Zurmo::t('MarketingModule', 'The primary email address is not populated. ' . 'The secondary email address is populated, but the primary email address is the one used to send the email message.'); } } elseif (MarketingListMember::getByMarketingListIdContactIdAndUnsubscribed($campaignItem->campaign->marketingList->id, $campaignItem->contact->id, true) != false) { $content = Zurmo::t('MarketingModule', 'The contact is not subscribed to the MarketingListsModuleSingularLabel', LabelUtil::getTranslationParamsForAllModules()); } elseif ($campaignItem->contact->primaryEmail->optOut) { if ($campaignItem->contact->secondaryEmail->optOut) { $content = Zurmo::t('MarketingModule', 'The primary email address is opted out.'); } else { $content = Zurmo::t('MarketingModule', 'The primary email address is opted out. ' . 'The secondary email address is not opted out but the primary email address is the one used to send the email message.'); } } return ZurmoHtml::tag('h4', array(), $content); }
/** * @depends testCreateAndGetMarketingListMemberById */ public function testGetCountByMarketingListIdAndUnsubscribed() { $marketingList = MarketingListTestHelper::createMarketingListByName('test marketing List 05'); $this->assertNotNull($marketingList); $contacts = Contact::getAll(); $this->assertNotEmpty($contacts); $unsubscribedCount = 0; $subscribedCount = 0; foreach ($contacts as $index => $contact) { $unsubcribed = $index % 2; $member = MarketingListMemberTestHelper::populateValidMarketingListMember($unsubcribed, $marketingList, $contact); $this->assertTrue($member->unrestrictedSave()); if ($unsubcribed) { $unsubscribedCount++; } else { $subscribedCount++; } } $calculatedSubscribedCount = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingList->id, 0); $calculatedUnsubscribedCount = MarketingListMember::getCountByMarketingListIdAndUnsubscribed($marketingList->id, 1); $this->assertEquals($subscribedCount, $calculatedSubscribedCount); $this->assertEquals($unsubscribedCount, $calculatedUnsubscribedCount); }
public function testUnsubscribeContactFromListUsingWorkflowAction() { $this->assertEquals(1, static::$marketingList->marketingListMembers->count()); $this->assertEquals(1, MarketingListMember::getCount()); //Try to unsubscribe the contact, this doesn't do anything useful as contact is already unsubscribed $action = new ActionForWorkflowForm('Contact', Workflow::TYPE_ON_SAVE); $action->type = ActionForWorkflowForm::TYPE_UNSUBSCRIBE_FROM_LIST; $attributes = array('marketingList' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => static::$marketingList->id)); $action->setAttributes(array(ActionForWorkflowForm::ACTION_ATTRIBUTES => $attributes)); $helper = new WorkflowActionProcessingHelper(88, 'some name', $action, static::$contact, Yii::app()->user->userModel); $helper->processNonUpdateSelfAction(); $marketingListId = static::$marketingList->id; static::$marketingList->forget(); static::$marketingList = MarketingList::getById($marketingListId); $this->assertEquals(1, static::$marketingList->marketingListMembers->count()); $this->assertEquals(1, static::$marketingList->marketingListMembers[0]->unsubscribed); $this->assertEquals(1, MarketingListMember::getCount()); }
protected function getSubtitle() { return MarketingListMember::getModelLabelByTypeAndLanguage('Plural'); }