All Data Structures Functions Variables Pages
MarketingListsUtilTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
37  {
38  protected $super;
39 
40  public static function setUpBeforeClass()
41  {
42  parent::setUpBeforeClass();
43  SecurityTestHelper::createSuperAdmin();
44  }
45 
46  public function setUp()
47  {
48  parent::setUp();
49  $this->super = User::getByUsername('super');
50  Yii::app()->user->userModel = $this->super;
51  }
52 
53  public function testResolveMarketingList()
54  {
55  $existingMarketingList = MarketingListTestHelper::createMarketingListByName('Test List');
56  $count = count(MarketingList::getAll());
57  $resolveSubscribersForm = new MarketingListResolveSubscribersFromCampaignForm();
58  $resolveSubscribersForm->marketingList['id'] = $existingMarketingList->id;
59  $marketingList = MarketingListsUtil::resolveMarketingList($resolveSubscribersForm);
60  $this->assertEquals($existingMarketingList->id, $marketingList->id);
61  $this->assertEquals($count, count(MarketingList::getAll()));
62 
63  // Test case if we provide both marketingListId and newMarketingListName, existing marketing list should be used
64  // However in real case this should never happen, because we have validation, that check if only one option is selected
65  $resolveSubscribersForm->newMarketingListName = "AAA";
66  $marketingList = MarketingListsUtil::resolveMarketingList($resolveSubscribersForm);
67  $this->assertEquals($existingMarketingList->id, $marketingList->id);
68  $this->assertEquals($count, count(MarketingList::getAll()));
69 
70  // Test with invalid marketingListId
71  $resolveSubscribersForm->marketingList['id'] = 9999;
72  $resolveSubscribersForm->newMarketingListName = null;
73  try
74  {
75  $marketingList = MarketingListsUtil::resolveMarketingList($resolveSubscribersForm);
76  $this->fail('Using invalid marketing list id and empty new marketing list name should thrown NotFoundException');
77  }
78  catch (NotFoundException $e)
79  {
80  $this->assertEquals('Invalid selected marketing list or not entered new marketing list name. Please go back and select marketing list!', $e->getMessage());
81  }
82 
83  // Test when new marketing list name is entered
84  $resolveSubscribersForm = new MarketingListResolveSubscribersFromCampaignForm();
85  $resolveSubscribersForm->newMarketingListName = "AAA";
86  $marketingList = MarketingListsUtil::resolveMarketingList($resolveSubscribersForm);
87  $this->assertTrue($marketingList instanceof MarketingList);
88  $this->assertEquals($marketingList->name, $resolveSubscribersForm->newMarketingListName);
89  $this->assertEquals($count + 1, count(MarketingList::getAll()));
90  }
91 
92  public function testGetContactsByResolveSubscribersFormAndCampaignAndOffsetAndPageSize()
93  {
94  $contact1 = ContactTestHelper::createContactByNameForOwner('contact 01', $this->super);
95  $contact2 = ContactTestHelper::createContactByNameForOwner('contact 02', $this->super);
96  $contact3 = ContactTestHelper::createContactByNameForOwner('contact 03', $this->super);
97  $contact4 = ContactTestHelper::createContactByNameForOwner('contact 04', $this->super);
98  $contact5 = ContactTestHelper::createContactByNameForOwner('contact 05', $this->super);
99  $contact6 = ContactTestHelper::createContactByNameForOwner('contact 06', $this->super);
100  $contact7 = ContactTestHelper::createContactByNameForOwner('contact 07', $this->super);
101  $contact8 = ContactTestHelper::createContactByNameForOwner('contact 08', $this->super);
102  $contact9 = ContactTestHelper::createContactByNameForOwner('contact 09', $this->super);
103  $contact10 = ContactTestHelper::createContactByNameForOwner('contact 10', $this->super);
104  $contact11 = ContactTestHelper::createContactByNameForOwner('contact 11', $this->super);
105 
106  $marketingList = MarketingListTestHelper::createMarketingListByName('Test List 2');
107  $marketingList->addNewMember($contact1->id, false, $contact1);
108  $marketingList->addNewMember($contact2->id, false, $contact2);
109  $marketingList->addNewMember($contact3->id, false, $contact3);
110  $marketingList->addNewMember($contact4->id, false, $contact4);
111  $marketingList->addNewMember($contact5->id, false, $contact5);
112  $marketingList->addNewMember($contact6->id, false, $contact6);
113  $marketingList->addNewMember($contact7->id, false, $contact7);
114  $marketingList->addNewMember($contact8->id, false, $contact8);
115  $marketingList->addNewMember($contact9->id, false, $contact9);
116  $marketingList->addNewMember($contact10->id, false, $contact10);
117  $marketingList->addNewMember($contact11->id, false, $contact11);
118  $marketingListId = $marketingList->id;
119  $marketingList->forgetAll();
120  $marketingList = MarketingList::getById($marketingListId);
121 
122  $newMarketingList = MarketingListTestHelper::createMarketingListByName('Test List 3');
123  $this->assertEquals(11, count($marketingList->marketingListMembers));
124  $this->assertEquals(0, count($newMarketingList->marketingListMembers));
125 
126  $campaign = CampaignTestHelper::createCampaign('campaign 01',
127  'subject 01',
128  'text Content 01',
129  'html Content 01',
130  'fromName 01',
131  'fromAddress01@zurmo.com',
132  null,
133  null,
134  null,
135  null,
136  $marketingList);
137  $campaign->status = Campaign::STATUS_PROCESSING;
138  $this->assertTrue($campaign->save());
139  $campaign->status = Campaign::STATUS_COMPLETED;
140  $this->assertTrue($campaign->save());
141 
142  $resolveSubscribersForm = new MarketingListResolveSubscribersFromCampaignForm();
143  $resolveSubscribersForm->marketingList['id'] = $newMarketingList->id; // This is how data are submitted from form
144  $resolveSubscribersForm->retargetClickedEmailRecipients = true;
145  $resolveSubscribersForm->retargetNotClickedEmailRecipients = true;
146  $resolveSubscribersForm->retargetNotViewedEmailRecipients = true;
147  $resolveSubscribersForm->retargetOpenedEmailRecipients = true;
148 
149  // Now add items to list
150  $campaignItem1 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact1);
151  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_CLICK, 1, $campaignItem1, '121.212.122.112');
152 
153  $campaignItem2 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact2);
154  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_OPEN, 1, $campaignItem2, '121.212.122.112');
155 
156  // No Activity for contact3, but it stills should be added to new marketing list, as unopened email
157  $campaignItem3 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact3);
158 
159  $campaignItem4 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact4);
160  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_HARD_BOUNCE, 1, $campaignItem4, '121.212.122.112');
161 
162  $campaignItem5 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact5);
163  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_UNSUBSCRIBE, 1, $campaignItem5, '121.212.122.112');
164 
165  $campaignItem6 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact6);
166  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_SPAM, 1, $campaignItem6, '121.212.122.112');
167 
168  $campaignItem7 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact7);
169  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_OPEN, 1, $campaignItem7, '121.212.122.112');
170  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_CLICK, 1, $campaignItem7, '121.212.122.112');
171  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_OPEN, 1, $campaignItem7, '121.212.122.112');
172  CampaignItemActivityTestHelper::createCampaignItemActivity(CampaignItemActivity::TYPE_CLICK, 1, $campaignItem7, '121.212.122.112');
173 
174  $campaignItem8 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact8);
175  $campaignItem9 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact9);
176  $campaignItem10 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact10);
177  $campaignItem11 = CampaignItemTestHelper::createCampaignItem(1, $campaign, $contact11);
178 
179  $contacts = MarketingListsUtil::getContactsByResolveSubscribersFormAndCampaignAndOffsetAndPageSize($resolveSubscribersForm, $campaign, 0, 3);
180  $this->assertEquals(6, count($contacts));
181  $contactsIds = array();
182  foreach ($contacts as $contact)
183  {
184  $contactsIds[] = $contact->id;
185  }
186  $this->assertTrue(in_array($contact1->id, $contactsIds));
187  $this->assertTrue(in_array($contact2->id, $contactsIds));
188  $this->assertTrue(in_array($contact3->id, $contactsIds));
189  $this->assertTrue(in_array($contact7->id, $contactsIds));
190  $this->assertTrue(in_array($contact8->id, $contactsIds));
191  $this->assertTrue(in_array($contact9->id, $contactsIds));
192 
193  $contacts = MarketingListsUtil::getContactsByResolveSubscribersFormAndCampaignAndOffsetAndPageSize($resolveSubscribersForm, $campaign, 3, 3);
194  $this->assertEquals(3, count($contacts));
195  $contactsIds = array();
196  foreach ($contacts as $contact)
197  {
198  $contactsIds[] = $contact->id;
199  }
200  $this->assertTrue(in_array($contact10->id, $contactsIds));
201  $this->assertTrue(in_array($contact11->id, $contactsIds));
202  $this->assertTrue(in_array($contact9->id, $contactsIds));
203  }
204 
209  {
210  $campaigns = Campaign::getByName('campaign 01');
211  $this->assertEquals(1, count($campaigns));
212  $campaign = $campaigns[0];
213 
214  $resolveSubscribersForm = new MarketingListResolveSubscribersFromCampaignForm();
215  $resolveSubscribersForm->newMarketingListName = 'AAA'; // This is how data are submitted from form
216  $resolveSubscribersForm->retargetClickedEmailRecipients = true;
217  $resolveSubscribersForm->retargetNotClickedEmailRecipients = true;
218  $resolveSubscribersForm->retargetNotViewedEmailRecipients = true;
219  $resolveSubscribersForm->retargetOpenedEmailRecipients = true;
220 
221  $count = MarketingListsUtil::getCountOfContactsByResolveSubscribersFormAndCampaign($resolveSubscribersForm, $campaign);
222  $this->assertEquals(6, $count);
223  }
224 
229  {
230  $campaigns = Campaign::getByName('campaign 01');
231  $this->assertEquals(1, count($campaigns));
232  $campaign = $campaigns[0];
233 
234  $resolveSubscribersForm = new MarketingListResolveSubscribersFromCampaignForm();
235  $resolveSubscribersForm->newMarketingListName = 'AAA'; // This is how data are submitted from form
236  $resolveSubscribersForm->retargetClickedEmailRecipients = true;
237  $resolveSubscribersForm->retargetNotClickedEmailRecipients = true;
238  $resolveSubscribersForm->retargetNotViewedEmailRecipients = true;
239  $resolveSubscribersForm->retargetOpenedEmailRecipients = true;
240 
241  $pageSize = MarketingListsUtil::$pageSize;
242  MarketingListsUtil::$pageSize = 3;
243  $count = MarketingListsUtil::getNumberOfContactPagesByResolveSubscribersFormAndCampaign($resolveSubscribersForm, $campaign);
244  MarketingListsUtil::$pageSize = $pageSize;
245  $this->assertEquals(2, $count);
246  }
247 
252  {
253  $campaigns = Campaign::getByName('campaign 01');
254  $this->assertEquals(1, count($campaigns));
255  $campaign = $campaigns[0];
257  $this->assertEquals('campaign 01' . ' - Retargeting List - ' . DateTimeUtil::getTodaysDate(), $marketingListName);
258  }
259 
260  public function testAddNewSubscribersToMarketingList()
261  {
262  $contact1 = ContactTestHelper::createContactByNameForOwner('contact 01', $this->super);
263  $contact2 = ContactTestHelper::createContactByNameForOwner('contact 02', $this->super);
264  $contact3 = ContactTestHelper::createContactByNameForOwner('contact 03', $this->super);
265  $contacts = array($contact1, $contact2, $contact3);
266 
267  $marketingList = MarketingListTestHelper::createMarketingListByName('Test List 4');
268  $this->assertEquals(0, count($marketingList->marketingListMembers));
269 
270  MarketingListsUtil::addNewSubscribersToMarketingList($marketingList->id, $contacts);
271  $marketingListId = $marketingList->id;
272  $marketingList->forgetAll();
273  $marketingList = MarketingList::getById($marketingListId);
274  $this->assertEquals(3, count($marketingList->marketingListMembers));
275  }
276  }
277 ?>
static getByUsername($username)
Definition: User.php:49
static getTodaysDate()
static resolveMarketingList($resolveSubscribersForm)
static getContactsByResolveSubscribersFormAndCampaignAndOffsetAndPageSize($resolveSubscribersForm, $campaign, $offset, $pageSize)
static addNewSubscribersToMarketingList($marketingListId, &$contacts, $scenario=null)
static getById($id, $modelClassName=null)
static generateRandomNameForCampaignRetargetingList(Campaign $campaign)
static getNumberOfContactPagesByResolveSubscribersFormAndCampaign($resolveSubscribersForm, $campaign)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static getCountOfContactsByResolveSubscribersFormAndCampaign($resolveSubscribersForm, $campaign)
Generated on Sat Feb 22 2020 07:10:31