/**
  * @depends testCreateNewActivity
  */
 public function testGetByTypeAndModelIdAndPersonIdAndUrl()
 {
     $type = AutoresponderItemActivity::TYPE_OPEN;
     $url = null;
     $persons = Person::getAll();
     $this->assertNotEmpty($persons);
     $person = $persons[0];
     $autoresponderItems = AutoresponderItem::getAll();
     $this->assertNotEmpty($autoresponderItems);
     $autoresponderItem = $autoresponderItems[0];
     $activities = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl($type, $autoresponderItem->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($autoresponderItem, $activity->autoresponderItem);
     // 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 = AutoresponderItemActivity::TYPE_CLICK;
     $url = 'http://www.zurmo.com';
     $activities = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl($type, $autoresponderItem->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($autoresponderItem, $activity->autoresponderItem);
 }
 /**
  * @depends testResolveQueryStringFromUrlAndCreateOrUpdateActivityThrowsExceptionForInvalidPersonIdForUrlClickTracking
  * @expectedException NotSupportedException
  */
 public function testResolveQueryStringFromUrlAndCreateOrUpdateActivityThrowsExceptionForDuplicateActivityForUrlClickTracking()
 {
     // setup pre-req data
     $autoresponderItemActivities = AutoresponderItemActivity::getByType(AutoresponderItemActivity::TYPE_CLICK);
     $this->assertNotEmpty($autoresponderItemActivities);
     $autoresponderActivity = new AutoresponderItemActivity();
     $autoresponderActivity->quantity = $autoresponderItemActivities[0]->quantity;
     $autoresponderActivity->type = $autoresponderItemActivities[0]->type;
     $autoresponderActivity->autoresponderItem = $autoresponderItemActivities[0]->autoresponderItem;
     $autoresponderActivity->person = $autoresponderItemActivities[0]->person;
     $autoresponderActivity->emailMessageUrl = $autoresponderItemActivities[0]->emailMessageUrl;
     $this->assertTrue($autoresponderActivity->save());
     $modelId = $autoresponderItemActivities[0]->autoresponderItem->id;
     $modelType = get_class($autoresponderItemActivities[0]->autoresponderItem);
     $personId = $autoresponderItemActivities[0]->person->id;
     // get the modelClassName to use for activity object tests
     $className = 'AutoresponderItemActivityUtil';
     $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();
 }
 /**
  * @param int $type
  * @return string
  */
 public static function resolveAutoresponderTypeSubQuery($type)
 {
     assert('is_int($type)');
     $quote = DatabaseCompatibilityUtil::getQuote();
     $where = null;
     $selectDistinct = true;
     $autoresponderItemActivityTableName = AutoresponderItemActivity::getTableName('AutoresponderItemActivity');
     $emailMessageActivityTableName = EmailMessageActivity::getTableName('EmailMessageActivity');
     $selectQueryAdapter = new RedBeanModelSelectQueryAdapter($selectDistinct);
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('AutoresponderItemActivity');
     $selectQueryAdapter->addClauseByQueryString("autoresponder_id");
     $joinTablesAdapter->addFromTableAndGetAliasName($emailMessageActivityTableName, 'emailmessageactivity_id', $autoresponderItemActivityTableName);
     $where = "type = " . $type . " and {$quote}{$autoresponderItemActivityTableName}{$quote}" . ".autoresponderitem_id = autoresponderitem.id";
     $sql = SQLQueryUtil::makeQuery($autoresponderItemActivityTableName, $selectQueryAdapter, $joinTablesAdapter, null, null, $where);
     return $sql;
 }
 /**
  * @depends testRunWithCustomBatchSize
  */
 public function testRunWithContactContainingPrimaryEmailOptedOut()
 {
     $unprocessedItems = AutoresponderItem::getByProcessed(0);
     $this->assertEmpty($unprocessedItems);
     $job = new AutoresponderQueueMessagesInOutboxJob();
     $email = new Email();
     $email->emailAddress = '*****@*****.**';
     $email->optOut = true;
     $contact = ContactTestHelper::createContactByNameForOwner('contact 07', $this->user);
     $contact->primaryEmail = $email;
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 08', 'description goes here', 'fromName', '*****@*****.**');
     MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
     $autoresponder = AutoresponderTestHelper::createAutoresponder('subject 08', '[[FIRST^NAME]]', '[[LAST^NAME]]', 1, Autoresponder::OPERATION_SUBSCRIBE, true, $marketingList);
     $processed = 0;
     $processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - rand(10, 500));
     $autoresponderItem = AutoresponderItemTestHelper::createAutoresponderItem($processed, $processDateTime, $autoresponder, $contact);
     $unprocessedItems = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponder->id);
     $this->assertCount(1, $unprocessedItems);
     $this->assertTrue($job->run());
     $unprocessedItems = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponder->id);
     $this->assertCount(0, $unprocessedItems);
     $personId = $contact->getClassId('Person');
     $activities = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(AutoresponderItemActivity::TYPE_SKIP, $autoresponderItem->id, $personId);
     $this->assertNotEmpty($activities);
     $this->assertCount(1, $activities);
 }
 /**
  * @depends testTrackActionDoesNotComplainForMissingUrlParameterForAutoresponderItem
  */
 public function testTrackActionThrowsRedirectExceptionForUrlParameterForAutoresponderItem()
 {
     $queryStringArray = array('modelId' => static::$autoresponderItemId, 'modelType' => 'AutoresponderItem', '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);
     $autoresponderItemActivity = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(AutoresponderItemActivity::TYPE_CLICK, static::$autoresponderItemId, static::$personId, $queryStringArray['url']);
     $this->assertNotEmpty($autoresponderItemActivity);
     $this->assertCount(1, $autoresponderItemActivity);
     $this->assertEquals(1, $autoresponderItemActivity[0]->quantity);
 }
 /**
  * @depends testProcessDueAutoresponderItemWithAttachments
  */
 public function testProcessDueAutoresponderItemWithOptout()
 {
     $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 07', 'description', 'CustomFromName', '*****@*****.**');
     $autoresponder = AutoresponderTestHelper::createAutoresponder('subject 07', 'Dr. [[FIRST^NAME]] [[LAST^NAME]]', '<b>[[LAST^NAME]]</b>, [[FIRST^NAME]]', 1, Autoresponder::OPERATION_SUBSCRIBE, true, $marketingList);
     $processed = 0;
     $processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $autoresponderItem = AutoresponderItemTestHelper::createAutoresponderItem($processed, $processDateTime, $autoresponder, $contact);
     $this->processDueItem($autoresponderItem);
     $this->assertEquals(1, $autoresponderItem->processed);
     $personId = $contact->getClassId('Person');
     $activities = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(AutoresponderItemActivity::TYPE_SKIP, $autoresponderItem->id, $personId);
     $this->assertNotEmpty($activities);
     $this->assertCount(1, $activities);
 }
 /**
  * @depends testUnsubscribeActionToPublicMarketingListCreatesActivity
  */
 public function testUnsubscribeActionToPrivateMarketingList()
 {
     $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];
     $marketingList->addNewMember($contact->id, false, $contact);
     $member = MarketingListMember::getByMarketingListIdContactIdAndSubscribed($marketingList->id, $contact->id, 0);
     $this->assertNotEmpty($member);
     $this->assertEquals(0, $member[0]->unsubscribed);
     $personId = $contact->getClassId('Person');
     $autoresponder = AutoresponderTestHelper::createAutoresponder('Autoresponder 02', 'textContent', 'htmlContent', 10, Autoresponder::OPERATION_UNSUBSCRIBE, true, $marketingList);
     $this->assertNotEmpty($autoresponder);
     $processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(strtotime('-1 week'));
     $autoresponderItem = AutoresponderItemTestHelper::createAutoresponderItem(1, $processDateTime, $autoresponder, $contact);
     $this->assertNotEmpty($autoresponderItem);
     $autoresponderItemActivities = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(AutoresponderItemActivity::TYPE_UNSUBSCRIBE, $autoresponderItem->id, $personId);
     $this->assertEmpty($autoresponderItemActivities);
     Yii::app()->user->userModel = null;
     $hash = EmailMessageActivityUtil::resolveHashForUnsubscribeAndManageSubscriptionsUrls($personId, $marketingListId, 1, 'AutoresponderItem', false);
     $this->setGetArray(array('hash' => $hash));
     $content = $this->runControllerWithNoExceptionsAndGetContent($this->manageSubscriptionsUrl);
     $this->assertTrue(strpos($content, 'id="marketingListsManageSubscriptionListView-toggleUnsubscribed_' . $marketingListId . '_0" checked="checked" type="radio" name="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_' . $marketingListId) !== false);
     $this->assertTrue(strpos($content, 'id="marketingListsManageSubscriptionListView-toggleUnsubscribed_' . $marketingListId . '_1" type="radio" name="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_' . $marketingListId) !== false);
     @$this->runControllerWithRedirectExceptionAndGetUrl($this->unsubscribeUrl);
     $content = $this->runControllerWithNoExceptionsAndGetContent($this->manageSubscriptionsUrl);
     $this->assertNotEmpty($content);
     $this->assertTrue(strpos($content, 'id="marketingListsManageSubscriptionListView-toggleUnsubscribed_' . $marketingListId . '_0" type="radio" name="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_' . $marketingListId) === false);
     $this->assertTrue(strpos($content, 'id="marketingListsManageSubscriptionListView-toggleUnsubscribed_' . $marketingListId . '_1" checked="checked" type="radio" name="marketingListsManage' . 'SubscriptionListView-toggleUnsubscribed_' . $marketingListId) === false);
     $this->assertTrue(strpos($content, '<td>marketingList 04</td>') === false);
     $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $autoresponderItemActivities = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(AutoresponderItemActivity::TYPE_UNSUBSCRIBE, $autoresponderItem->id, $personId);
     $this->assertEmpty($autoresponderItemActivities);
 }
 /**
  * @depends testRunWithOneBounceMessageWithDesiredCustomHeaders
  */
 public function testRunWithTwoBounceMessagesWithDesiredCustomHeaders()
 {
     $this->skipTestIfMissingSettings();
     $contact = ContactTestHelper::createContactByNameForOwner('contact 02', $this->user);
     $personId = $contact->getClassId('Person');
     $this->assertTrue($contact->save());
     $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 02');
     $autoresponder = AutoresponderTestHelper::createAutoresponder('subject 02', 'text content', 'html content', 1, Autoresponder::OPERATION_SUBSCRIBE, true, $marketingList);
     $processed = 0;
     $processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 200);
     $autoresponderItem = AutoresponderItemTestHelper::createAutoresponderItem($processed, $processDateTime, $autoresponder, $contact);
     $bounce = BounceMessageTestHelper::resolveBounceObject();
     $this->assertTrue($bounce->connect());
     $headers = array('Return-Path' => $bounce->returnPath, 'zurmoItemId' => $autoresponderItem->id, 'zurmoItemClass' => get_class($autoresponderItem), 'zurmoPersonId' => $personId);
     $bounceTestEmailAddress = Yii::app()->params['emailTestAccounts']['bounceTestEmailAddress'];
     Yii::app()->emailHelper->sendRawEmail("1/2 Bounce - Desired Headers " . date(DATE_RFC822), Yii::app()->emailHelper->outboundUsername, $bounceTestEmailAddress, 'Test email body', '<strong>Test</strong> email html body', null, null, null, null, $headers);
     Yii::app()->emailHelper->sendRawEmail("2/2 Bounce - Desired Headers " . date(DATE_RFC822), Yii::app()->emailHelper->outboundUsername, $bounceTestEmailAddress, 'Test email body', '<strong>Test</strong> email html body', null, null, null, null, $headers);
     sleep(60);
     $messages = $bounce->getMessages();
     $this->assertEquals(2, count($messages));
     for ($i = 0; $i < 2; $i++) {
         $this->assertEquals("Mail delivery failed: returning message to sender", $messages[$i]->subject);
         $this->assertContains('Test email body', $messages[$i]->textBody);
         $this->assertContains('zurmoItemId: ' . $autoresponderItem->id, $messages[$i]->textBody);
         $this->assertContains('zurmoItemClass: ' . get_class($autoresponderItem), $messages[$i]->textBody);
         $this->assertContains('zurmoPersonId: ' . $personId, $messages[$i]->textBody);
     }
     $job = new EmailBounceJob();
     $this->assertTrue($job->run());
     $activities = AutoresponderItemActivity::getAll();
     $this->assertNotEmpty($activities);
     $this->assertCount(2, $activities);
     $activities = AutoresponderItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(AutoresponderItemActivity::TYPE_BOUNCE, $autoresponderItem->id, $personId);
     $this->assertNotEmpty($activities);
     $this->assertCount(1, $activities);
     $this->assertEquals(2, $activities[0]->quantity);
     $messages = $bounce->getMessages();
     $this->assertEquals(0, count($messages));
 }
 /**
  * @depends testCreateAndGetAutoresponderById
  */
 public function testDeleteAutoresponder()
 {
     $autoresponders = Autoresponder::getAll();
     $this->assertCount(7, $autoresponders);
     $autoresponderId = $autoresponders[0]->id;
     $processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $contact = ContactTestHelper::createContactByNameForOwner('autoresponderContact', Yii::app()->user->userModel);
     $saved = AutoresponderItem::addNewItem(0, $processDateTime, $contact, $autoresponders[0]);
     $this->assertTrue($saved);
     $autoresponderItems = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponderId);
     //print_r($autoresponderItems[0]->id);
     $this->assertNotEmpty($autoresponderItems);
     $this->assertCount(1, $autoresponderItems);
     $autoresponderItemActivity = new AutoresponderItemActivity();
     $autoresponderItemActivity->type = AutoresponderItemActivity::TYPE_OPEN;
     $autoresponderItemActivity->quantity = 10;
     $autoresponderItemActivity->latestSourceIP = '10.11.12.13';
     $autoresponderItemActivity->autoresponderItem = $autoresponderItems[0];
     $this->assertTrue($autoresponderItemActivity->save());
     //print_r($autoresponderItemActivity->id);
     $autoresponderItemActivities = AutoresponderItemActivity::getAll();
     $this->assertCount(1, $autoresponderItemActivities);
     $autoresponders[0]->delete();
     $autoresponders = Autoresponder::getAll();
     $this->assertEquals(6, count($autoresponders));
     $autoresponderitems = AutoresponderItem::getByProcessedAndAutoresponderId(0, $autoresponderId);
     $this->assertEquals(0, count($autoresponderitems));
     $autoresponderItemActivities = AutoresponderItemActivity::getAll();
     $this->assertCount(0, $autoresponderItemActivities);
 }
 /**
  * @depends testCreateAndGetAutoresponderItemById
  */
 public function testDeleteAutoresponderItem()
 {
     $autoresponderItems = AutoresponderItem::getAll();
     $this->assertCount(37, $autoresponderItems);
     $autoresponderItemActivity = new AutoresponderItemActivity();
     $autoresponderItemActivity->type = AutoresponderItemActivity::TYPE_OPEN;
     $autoresponderItemActivity->quantity = 10;
     $autoresponderItemActivity->latestSourceIP = '10.11.12.13';
     $autoresponderItemActivity->autoresponderItem = $autoresponderItems[0];
     $this->assertTrue($autoresponderItemActivity->save());
     $autoresponderItemActivities = AutoresponderItemActivity::getAll();
     $this->assertCount(1, $autoresponderItemActivities);
     $autoresponderItems[0]->delete();
     $autoresponderItems = AutoresponderItem::getAll();
     $this->assertEquals(36, count($autoresponderItems));
     $autoresponderItemActivities = AutoresponderItemActivity::getAll();
     $this->assertCount(0, $autoresponderItemActivities);
 }