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