All Data Structures Functions Variables Pages
CampaignGenerateDueCampaignItemsJobTest.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 $user;
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->user = User::getByUsername('super');
50  Yii::app()->user->userModel = $this->user;
55  }
56 
57  public function testGetDisplayName()
58  {
60  $this->assertEquals('Generate campaign items', $displayName);
61  }
62 
63  public function testGetType()
64  {
66  $this->assertEquals('CampaignGenerateDueCampaignItems', $type);
67  }
68 
69  public function testGetRecommendedRunFrequencyContent()
70  {
71  $recommendedRunFrequency = CampaignGenerateDueCampaignItemsJob::getRecommendedRunFrequencyContent();
72  $this->assertEquals('Every hour', $recommendedRunFrequency);
73  }
74 
75  public function testRunWithoutAnyCampaigns()
76  {
77  $campaigns = Campaign::getAll();
78  $this->assertEmpty($campaigns);
80  $this->assertTrue($job->run());
81  }
82 
87  {
88  $marketingList = MarketingListTestHelper::populateMarketingListByName('marketingList 01');
89  CampaignTestHelper::createCampaign('Processing',
90  'subject',
91  'text Content',
92  'Html Content',
93  null,
94  null,
95  null,
96  Campaign::STATUS_PROCESSING,
97  null,
98  null,
99  $marketingList);
100  CampaignTestHelper::createCampaign('Incomplete',
101  'subject',
102  'text Content',
103  'Html Content',
104  null,
105  null,
106  null,
107  Campaign::STATUS_PAUSED,
108  null,
109  null,
110  $marketingList);
111  CampaignTestHelper::createCampaign('Paused',
112  'subject',
113  'text Content',
114  'Html Content',
115  null,
116  null,
117  null,
118  Campaign::STATUS_PAUSED,
119  null,
120  null,
121  $marketingList);
122  $this->assertEmpty(CampaignItem::getAll());
124  $this->assertTrue($job->run());
125  $this->assertEmpty(CampaignItem::getAll());
126  }
127 
132  {
133  $marketingList = MarketingListTestHelper::populateMarketingListByName('marketingList 02');
134  $tenDaysFromNowTimestamp = time() + 60*60*24*10;
135  $tenDaysFromNowDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($tenDaysFromNowTimestamp);
136  CampaignTestHelper::createCampaign('Active And Non Due',
137  'subject',
138  'text Content',
139  'Html Content',
140  null,
141  null,
142  null,
143  Campaign::STATUS_ACTIVE,
144  $tenDaysFromNowDateTime,
145  null,
146  $marketingList);
147  $this->assertEmpty(CampaignItem::getAll());
149  $this->assertTrue($job->run());
150  $this->assertEmpty(CampaignItem::getAll());
151  }
152 
157  {
158  $marketingList = MarketingListTestHelper::populateMarketingListByName('marketingList 03');
159  CampaignTestHelper::createCampaign('Active, Due But No Members',
160  'subject',
161  'text Content',
162  'Html Content',
163  null,
164  null,
165  null,
166  Campaign::STATUS_ACTIVE,
167  null,
168  null,
169  $marketingList);
170  $this->assertEmpty(CampaignItem::getAll());
172  $this->assertTrue($job->run());
173  $this->assertEmpty(CampaignItem::getAll());
174  }
175 
180  {
181  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 04');
182  $marketingListId = $marketingList->id;
183  $processed = 0;
184  for ($i = 1; $i <= 5; $i++)
185  {
186  $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 0' . $i, $this->user);
187  MarketingListMemberTestHelper::createMarketingListMember($processed, $marketingList, $contact);
188  }
189  $marketingList->forgetAll();
190 
191  $marketingList = MarketingList::getById($marketingListId);
192  $campaign = CampaignTestHelper::createCampaign('Active, Due With Members',
193  'subject',
194  'text Content',
195  'Html Content',
196  null,
197  null,
198  null,
199  Campaign::STATUS_ACTIVE,
200  null,
201  null,
202  $marketingList);
203  // we have to do this to ensure when we retrieve the data status is updated from db.
204  $campaign->forgetAll();
205  $this->assertEmpty(CampaignItem::getAll());
207  $this->assertTrue($job->run());
208  $campaign = Campaign::getByName('Active, Due With Members');
209  $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign[0]->status);
210  $allCampaignItems = CampaignItem::getAll();
211  $this->assertNotEmpty($allCampaignItems);
212  $this->assertCount(5, $allCampaignItems);
213  $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaign[0]->id);
214  $this->assertNotEmpty($campaignItems);
215  $this->assertCount(5, $campaignItems);
216  }
217 
222  {
223  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 05');
224  $marketingListId = $marketingList->id;
225  $processed = 0;
226  for ($i = 6; $i <= 10; $i++)
227  {
228  $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 0' . $i, $this->user);
229  MarketingListMemberTestHelper::createMarketingListMember($processed, $marketingList, $contact);
230  }
231  $marketingList->forgetAll();
232 
233  $marketingList = MarketingList::getById($marketingListId);
234  $nowDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
235  $campaign = CampaignTestHelper::createCampaign('Active, Just Due With Members',
236  'subject',
237  'text Content',
238  'Html Content',
239  null,
240  null,
241  null,
242  Campaign::STATUS_ACTIVE,
243  $nowDateTime,
244  null,
245  $marketingList);
246  // we have to do this to ensure when we retrieve the data status is updated from db.
247  $campaign->forgetAll();
248  $this->assertEmpty(CampaignItem::getAll());
249  // sleep 1 second to ensure we are giving ample time difference between creating the campaign and calling the job
250  sleep(1);
252  $this->assertTrue($job->run());
253  $campaign = Campaign::getByName('Active, just Due With Members');
254  $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign[0]->status);
255  $allCampaignItems = CampaignItem::getAll();
256  $this->assertNotEmpty($allCampaignItems);
257  $this->assertCount(5, $allCampaignItems);
258  $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaign[0]->id);
259  $this->assertNotEmpty($campaignItems);
260  $this->assertCount(5, $campaignItems);
261  }
262 
267  {
268  $data = array(
269  'old' => array(
270  'subject' => 'Old Subject',
271  'fromName' => 'Old From Name',
272  'fromAddress' => 'old@zurmo.com',
273  'textContent' => 'Old Text Content',
274  'htmlContent' => 'Old Html Content'
275  ),
276  'new' => array(
277  'subject' => 'New Subject',
278  'fromName' => 'New From Name',
279  'fromAddress' => 'new@zurmo.com',
280  'textContent' => 'New Text Content',
281  'htmlContent' => 'New Html Content'
282  ),
283  );
284  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 06');
285  $marketingListId = $marketingList->id;
286  $processed = 0;
287  for ($i = 11; $i <= 15; $i++)
288  {
289  $email = new Email();
290  $email->emailAddress = "demo$i@zurmo.com";
291  $contact = ContactTestHelper::createContactByNameForOwner('campaignContact 0' . $i, $this->user);
292  $contact->primaryEmail = $email;
293  $this->assertTrue($contact->save());
294  MarketingListMemberTestHelper::createMarketingListMember($processed, $marketingList, $contact);
295  }
296  $marketingList->forgetAll();
297  $marketingList = MarketingList::getById($marketingListId);
298  $nowDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
299  $campaign = CampaignTestHelper::createCampaign('campaign',
300  $data['old']['subject'],
301  $data['old']['textContent'],
302  $data['old']['htmlContent'],
303  $data['old']['fromName'],
304  $data['old']['fromAddress'],
305  null,
306  Campaign::STATUS_ACTIVE,
307  $nowDateTime,
308  null,
309  $marketingList);
310  $campaignId = $campaign->id;
311  // we have to do this to ensure when we retrieve the data status is updated from db.
312  $campaign->forgetAll();
313  $this->assertEmpty(CampaignItem::getAll());
314  // sleep 1 second to ensure we are giving ample time difference between creating the campaign and calling the job
315  sleep(1);
316  $campaignGenerateDueCampaignItemsJob = new CampaignGenerateDueCampaignItemsJob();
317  $this->assertTrue($campaignGenerateDueCampaignItemsJob->run());
318  $campaign = Campaign::getById($campaignId);
319  $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
320  $allCampaignItems = CampaignItem::getAll();
321  $this->assertNotEmpty($allCampaignItems);
322  $this->assertCount(5, $allCampaignItems);
323  $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
324  $this->assertNotEmpty($campaignItems);
325  $this->assertCount(5, $campaignItems);
326  $campaignQueueMessagesInOutboxJob = new CampaignQueueMessagesInOutboxJob();
327  $this->assertTrue($campaignQueueMessagesInOutboxJob->run());
328  $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
329  $this->assertNotEmpty($campaignItems);
330  $this->assertCount(5, $campaignItems);
331  $oldCampaignItemIds = array();
332  foreach ($campaignItems as $ci)
333  {
334  $this->assertNotEmpty($ci->id);
335  $this->assertEquals(1, $ci->processed);
336  $this->assertNotEmpty($ci->emailMessage->id);
337  $this->assertNotEmpty($ci->emailMessage->sender->id);
338  $this->assertNotEmpty($ci->emailMessage->content->id);
339  $this->assertNotEmpty($ci->contact->id);
340  $this->assertEquals($data['old']['subject'], $ci->emailMessage->subject);
341  $this->assertEquals($data['old']['fromName'], $ci->emailMessage->sender->fromName);
342  $this->assertEquals($data['old']['fromAddress'], $ci->emailMessage->sender->fromAddress);
343  $this->assertContains($data['old']['textContent'], $ci->emailMessage->content->textContent);
344  $this->assertContains($data['old']['htmlContent'], $ci->emailMessage->content->htmlContent);
345  $oldCampaignItemIds[] = $ci->id;
346  }
347  $campaignItems[ count($campaignItems) -1 ]->delete();
348  array_pop($oldCampaignItemIds);
349  $campaign->status = Campaign::STATUS_ACTIVE;
350  foreach ($data['new'] as $property => $value)
351  {
352  $campaign->$property = $value;
353  }
354  $this->assertTrue($campaign->save());
355  $campaign->forgetAll();
356  $campaignGenerateDueCampaignItemsJob = new CampaignGenerateDueCampaignItemsJob();
357  $this->assertTrue($campaignGenerateDueCampaignItemsJob->run());
358  $campaign = Campaign::getById($campaignId);
359  $this->assertEquals(Campaign::STATUS_PROCESSING, $campaign->status);
360  $allCampaignItems = CampaignItem::getAll();
361  $this->assertNotEmpty($allCampaignItems);
362  $this->assertCount(5, $allCampaignItems);
363  $campaignItems = CampaignItem::getByProcessedAndCampaignId(0, $campaignId);
364  $this->assertNotEmpty($campaignItems);
365  $this->assertCount(1, $campaignItems);
366  $campaignQueueMessagesInOutboxJob = new CampaignQueueMessagesInOutboxJob();
367  $this->assertTrue($campaignQueueMessagesInOutboxJob->run());
368  $campaignItems = CampaignItem::getByProcessedAndCampaignId(1, $campaignId);
369  $this->assertNotEmpty($campaignItems);
370  $this->assertCount(5, $campaignItems);
371  $oldCounter = 0;
372  $newCounter = 0;
373  foreach ($campaignItems as $ci)
374  {
375  $this->assertNotEmpty($ci->id);
376  $this->assertEquals(1, $ci->processed);
377  $this->assertNotEmpty($ci->emailMessage->id);
378  $this->assertNotEmpty($ci->emailMessage->sender->id);
379  $this->assertNotEmpty($ci->emailMessage->content->id);
380  $this->assertNotEmpty($ci->contact->id);
381  $dataIndex = (in_array($ci->id, $oldCampaignItemIds))? 'old' : 'new';
382  if ($dataIndex == 'old')
383  {
384  $oldCounter++;
385  }
386  else
387  {
388  $newCounter++;
389  }
390  $this->assertEquals($data[$dataIndex]['subject'], $ci->emailMessage->subject);
391  $this->assertEquals($data[$dataIndex]['fromName'], $ci->emailMessage->sender->fromName);
392  $this->assertEquals($data[$dataIndex]['fromAddress'], $ci->emailMessage->sender->fromAddress);
393  $this->assertContains($data[$dataIndex]['textContent'], $ci->emailMessage->content->textContent);
394  $this->assertContains($data[$dataIndex]['htmlContent'], $ci->emailMessage->content->htmlContent);
395  }
396  $this->assertEquals(count($oldCampaignItemIds), $oldCounter);
397  $this->assertEquals(count($campaignItems) - count($oldCampaignItemIds), $newCounter);
398  }
399  }
400 ?>
Definition: Email.php:37
static getByProcessedAndCampaignId($processed, $campaignId, $pageSize=null)
static getByUsername($username)
Definition: User.php:49
static deleteAll()
static getById($id, $modelClassName=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Generated on Thu Feb 20 2020 07:10:30