Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
CampaignQueueMessagesInOutboxJobTest.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;
51  }
52 
53  public function testGetDisplayName()
54  {
56  $this->assertEquals('Process campaign messages', $displayName);
57  }
58 
59  public function testGetType()
60  {
62  $this->assertEquals('CampaignQueueMessagesInOutbox', $type);
63  }
64 
65  public function testGetRecommendedRunFrequencyContent()
66  {
67  $recommendedRunFrequency = CampaignQueueMessagesInOutboxJob::getRecommendedRunFrequencyContent();
68  $this->assertEquals('Every hour', $recommendedRunFrequency);
69  }
70 
71  public function testRunWithoutAnyItems()
72  {
73  $campaignItems = CampaignItem::getAll();
74  $this->assertEmpty($campaignItems);
76  $this->assertTrue($job->run());
77  }
78 
82  public function testRunWithoutContact()
83  {
85  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 01');
86  $campaign = CampaignTestHelper::createCampaign('campaign 01',
87  'subject',
88  'text Content',
89  'Html Content',
90  null,
91  null,
92  null,
93  Campaign::STATUS_PROCESSING,
94  null,
95  0,
96  $marketingList);
97  $processed = 0;
98  CampaignItemTestHelper::createCampaignItem($processed, $campaign);
99  $this->assertTrue($job->run());
100  $campaignItems = CampaignItem::getAll();
101  $this->assertEmpty($campaignItems);
102  }
103 
108  {
110  $contact = ContactTestHelper::createContactByNameForOwner('contact 01', $this->user);
111  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 02');
112  $campaign = CampaignTestHelper::createCampaign('campaign 02',
113  'subject',
114  'text Content',
115  'Html Content',
116  null,
117  null,
118  null,
119  Campaign::STATUS_PROCESSING,
120  null,
121  null,
122  $marketingList);
123 
124  $processed = 0;
125  CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
126  $this->assertTrue($job->run());
127  $campaignItems = CampaignItem::getAll();
128  $this->assertCount(1, $campaignItems);
129  $this->assertTrue($campaignItems[0]->isSkipped());
130  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
131  $this->assertCount(1, $campaignItemsProcessed);
132  }
133 
138  {
140  $email = new Email();
141  $email->emailAddress = 'demo@zurmo.com';
142  $contact = ContactTestHelper::createContactByNameForOwner('contact 02', $this->user);
143  $contact->primaryEmail = $email;
144  $this->assertTrue($contact->save());
145  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 03');
146  $campaign = CampaignTestHelper::createCampaign('campaign 03',
147  'subject',
148  'text Content',
149  'Html Content',
150  null,
151  null,
152  null,
153  Campaign::STATUS_ACTIVE,
154  null,
155  0,
156  $marketingList);
157 
158  $processed = 0;
159  CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
160  $this->assertTrue($job->run());
161  $campaignItems = CampaignItem::getAll();
162  $this->assertCount(2, $campaignItems);
163  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
164  $this->assertCount(0, $campaignItemsProcessed);
165  }
166 
171  {
173  $email = new Email();
174  $email->emailAddress = 'demo@zurmo.com';
175  $contact = ContactTestHelper::createContactByNameForOwner('contact 02', $this->user);
176  $contact->primaryEmail = $email;
177  $this->assertTrue($contact->save());
178  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 03');
179  $campaign = CampaignTestHelper::createCampaign('campaign 03',
180  'subject',
181  'text Content',
182  'Html Content',
183  null,
184  null,
185  null,
186  Campaign::STATUS_PROCESSING,
187  null,
188  0,
189  $marketingList);
190 
191  $processed = 0;
192  CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
193  $this->assertTrue($job->run());
194  $campaignItems = CampaignItem::getAll();
195  $this->assertCount(3, $campaignItems);
196  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
197  $this->assertCount(1, $campaignItemsProcessed);
198  }
199 
204  {
206  $email = new Email();
207  $email->emailAddress = 'demo@zurmo.com';
208  $contact = ContactTestHelper::createContactByNameForOwner('contact 03', $this->user);
209  $contact->primaryEmail = $email;
210  $this->assertTrue($contact->save());
211  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 04',
212  'description goes here',
213  'fromName',
214  'from@domain.com');
215  $campaign = CampaignTestHelper::createCampaign('campaign 04',
216  'subject',
217  'text Content',
218  'Html Content',
219  null,
220  null,
221  null,
222  Campaign::STATUS_PROCESSING,
223  null,
224  null,
225  $marketingList);
226  $processed = 0;
227  CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
228  $this->assertTrue($job->run());
229  $campaignItems = CampaignItem::getAll();
230  $this->assertCount(4, $campaignItems);
231  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
232  $this->assertCount(1, $campaignItemsProcessed);
233  }
234 
238  public function testRunWithInvalidMergeTags()
239  {
241  $email = new Email();
242  $email->emailAddress = 'demo@zurmo.com';
243  $contact = ContactTestHelper::createContactByNameForOwner('contact 04', $this->user);
244  $contact->primaryEmail = $email;
245  $this->assertTrue($contact->save());
246  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 05',
247  'description goes here',
248  'fromName',
249  'from@domain.com');
250  MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
251  $campaign = CampaignTestHelper::createCampaign('campaign 05',
252  'subject',
253  '[[TEXT^CONTENT]]',
254  '[[HTML^CONTENT]]',
255  null,
256  null,
257  null,
258  Campaign::STATUS_PROCESSING,
259  null,
260  0,
261  $marketingList,
262  false);
263  $processed = 0;
264  $campaignItem = CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
265  $this->assertFalse($job->run());
266  $this->assertEquals('Provided content contains few invalid merge tags.', $job->getErrorMessage());
267  $campaignItems = CampaignItem::getAll();
268  $this->assertCount(5, $campaignItems);
269  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId( 1, $campaign->id);
270  $this->assertCount(0, $campaignItemsProcessed);
271  $this->assertTrue($campaignItem->delete()); // Need to get rid of this so it doesn't interfere with next test.
272  }
273 
277  public function testRunWithValidMergeTags()
278  {
280  $email = new Email();
281  $email->emailAddress = 'demo@zurmo.com';
282  $contact = ContactTestHelper::createContactByNameForOwner('contact 05', $this->user);
283  $contact->primaryEmail = $email;
284  $this->assertTrue($contact->save());
285  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 06',
286  'description goes here',
287  'fromName',
288  'from@domain.com');
289  $campaign = CampaignTestHelper::createCampaign('campaign 06',
290  'subject',
291  '[[FIRST^NAME]]',
292  '[[LAST^NAME]]',
293  null,
294  null,
295  null,
296  Campaign::STATUS_PROCESSING,
297  null,
298  null,
299  $marketingList);
300  $processed = 0;
301  CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
302  $this->assertTrue($job->run());
303  $campaignItems = CampaignItem::getAll();
304  $this->assertCount(5, $campaignItems);
305  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
306  $this->assertCount(1, $campaignItemsProcessed);
307  }
308 
312  public function testRunWithCustomBatchSize()
313  {
314  $unprocessedItems = CampaignItem::getByProcessed(0);
315  $this->assertCount(1, $unprocessedItems);
317  $email = new Email();
318  $email->emailAddress = 'demo@zurmo.com';
319  $contact = ContactTestHelper::createContactByNameForOwner('contact 06', $this->user);
320  $contact->primaryEmail = $email;
321  $this->assertTrue($contact->save());
322  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 07',
323  'description goes here',
324  'fromName',
325  'from@domain.com');
326  MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
327  $campaign = CampaignTestHelper::createCampaign('campaign 07',
328  'subject',
329  '[[FIRST^NAME]]',
330  '[[LAST^NAME]]',
331  null,
332  null,
333  null,
334  Campaign::STATUS_PROCESSING,
335  null,
336  null,
337  $marketingList);
338  for ($i = 0; $i < 10; $i++)
339  {
340  $processed = 0;
341  CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
342  }
343  $unprocessedItems = CampaignItem::getByProcessedAndCampaignId(
344  0,
345  $campaign->id);
346  $this->assertCount(10, $unprocessedItems);
348  Yii::app()->jobQueue->deleteAll();
349  $this->assertCount(0, Yii::app()->jobQueue->getAll());
350  $this->assertTrue($job->run());
351  $unprocessedItems = CampaignItem::getByProcessedAndCampaignId(
352  0,
353  $campaign->id);
354  $this->assertCount(5, $unprocessedItems);
355  $jobs = Yii::app()->jobQueue->getAll();
356  $this->assertCount(2, $jobs);
357  $this->assertEquals('ProcessOutboundEmail', $jobs[0][0]['jobType']);
358  $this->assertEquals('CampaignQueueMessagesInOutbox', $jobs[5][0]['jobType']);
360  $this->assertTrue($job->run());
361  $unprocessedItems = CampaignItem::getByProcessedAndCampaignId(
362  0,
363  $campaign->id);
364  $this->assertCount(2, $unprocessedItems);
365  $jobs = Yii::app()->jobQueue->getAll();
366  $this->assertCount(2, $jobs);
367  $this->assertEquals('ProcessOutboundEmail', $jobs[0][0]['jobType']);
368  $this->assertEquals('CampaignQueueMessagesInOutbox', $jobs[5][0]['jobType']);
370  Yii::app()->jobQueue->deleteAll();
371  $this->assertCount(0, Yii::app()->jobQueue->getAll());
372  $this->assertTrue($job->run());
373  $unprocessedItems = CampaignItem::getByProcessedAndCampaignId(
374  0,
375  $campaign->id);
376  $this->assertCount(0, $unprocessedItems);
377  $jobs = Yii::app()->jobQueue->getAll();
378  $this->assertCount(2, $jobs);
379  $this->assertCount(1, $jobs[5]);
380  $this->assertEquals('CampaignMarkCompleted', $jobs[5][0]['jobType']);
381  }
382 
387  {
388  $unprocessedItems = CampaignItem::getByProcessed(0);
389  $this->assertCount(1, $unprocessedItems);
391  $email = new Email();
392  $email->emailAddress = 'demo@zurmo.com';
393  $email->optOut = true;
394  $contact = ContactTestHelper::createContactByNameForOwner('contact 07', $this->user);
395  $contact->primaryEmail = $email;
396  $this->assertTrue($contact->save());
397  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 08',
398  'description goes here',
399  'fromName',
400  'from@domain.com');
401  $campaign = CampaignTestHelper::createCampaign('campaign 08',
402  'subject',
403  '[[FIRST^NAME]]',
404  '[[LAST^NAME]]',
405  null,
406  null,
407  null,
408  Campaign::STATUS_PROCESSING,
409  null,
410  null,
411  $marketingList);
412  $processed = 0;
413  $campaignItem = CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
414  $unprocessedItems = CampaignItem::getByProcessedAndCampaignId($processed, $campaign->id);
415  $this->assertCount(1, $unprocessedItems);
416  $this->assertTrue($job->run());
417  $unprocessedItems = CampaignItem::getByProcessedAndCampaignId($processed, $campaign->id);
418  $this->assertCount(0, $unprocessedItems);
419  $personId = $contact->getClassId('Person');
420  $activities = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl(
421  CampaignItemActivity::TYPE_SKIP,
422  $campaignItem->id,
423  $personId);
424  $this->assertNotEmpty($activities);
425  $this->assertCount(1, $activities);
426  }
427 
432  {
433  $campaignItemsCountBefore = CampaignItem::getCount();
435  $contacts = array();
436  for ($i = 0; $i < 5; $i++)
437  {
438  $email = new Email();
439  $email->emailAddress = "demo$i@zurmo.com";
440  $contact = ContactTestHelper::createContactByNameForOwner('contact 0' . ($i + 8), $this->user);
441  $contact->primaryEmail = $email;
442  $this->assertTrue($contact->save());
443  $contacts[] = $contact;
444  }
445  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 09',
446  'description goes here',
447  'fromName',
448  'from@domain.com');
449  $campaign = CampaignTestHelper::createCampaign('campaign 09',
450  'subject',
451  '[[FIRST^NAME]]',
452  '[[LAST^NAME]]',
453  null,
454  null,
455  null,
456  Campaign::STATUS_PROCESSING,
457  null,
458  null,
459  $marketingList);
460  $fileNames = array('testImage.png', 'testZip.zip', 'testPDF.pdf');
461  $files = array();
462  foreach ($fileNames as $index => $fileName)
463  {
464  $file = ZurmoTestHelper::createFileModel($fileName);
465  $files[$index]['name'] = $fileName;
466  $files[$index]['type'] = $file->type;
467  $files[$index]['size'] = $file->size;
468  $files[$index]['contents'] = $file->fileContent->content;
469  $campaign->files->add($file);
470  }
471  $this->assertTrue($campaign->save());
472  $processed = 0;
473  foreach ($contacts as $contact)
474  {
475  MarketingListMemberTestHelper::createMarketingListMember(0, $marketingList, $contact);
476  CampaignItemTestHelper::createCampaignItem($processed, $campaign, $contact);
477  }
478  $this->assertTrue($job->run());
479  ForgetAllCacheUtil::forgetAllCaches();
480  $campaignItemsCountExpected = $campaignItemsCountBefore + 5;
481  $campaignItemsCountAfter = CampaignItem::getCount();
482  $this->assertEquals($campaignItemsCountExpected, $campaignItemsCountAfter);
483  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
484  $this->assertCount(5, $campaignItemsProcessed);
485  foreach ($campaignItemsProcessed as $campaignItem)
486  {
487  $emailMessage = $campaignItem->emailMessage;
488  $this->assertNotEmpty($emailMessage->files);
489  $this->assertCount(count($files), $emailMessage->files);
490  foreach ($files as $index => $file)
491  {
492  $this->assertEquals($files[$index]['name'], $emailMessage->files[$index]->name);
493  $this->assertEquals($files[$index]['type'], $emailMessage->files[$index]->type);
494  $this->assertEquals($files[$index]['size'], $emailMessage->files[$index]->size);
495  $this->assertEquals($files[$index]['contents'], $emailMessage->files[$index]->fileContent->content);
496  }
497  }
498  }
499 
504  {
505  $campaignItemsCountBefore = CampaignItem::getCount();
507  $marketingList = MarketingListTestHelper::createMarketingListByName('marketingList 10');
508  $marketingListId = $marketingList->id;
509  $contacts = array();
510  for ($i = 0; $i < 5; $i++)
511  {
512  $email = new Email();
513  $email->emailAddress = "demo$i@zurmo.com";
514  $contact = ContactTestHelper::createContactByNameForOwner('contact 0' . ($i + 14), $this->user);
515  $contact->primaryEmail = $email;
516  $this->assertTrue($contact->save());
517  $contacts[] = $contact;
518  MarketingListMemberTestHelper::createMarketingListMember(($i%2) , $marketingList, $contact);
519  }
520  $marketingList->forgetAll();
521 
522  $marketingList = MarketingList::getById($marketingListId);
523  $campaign = CampaignTestHelper::createCampaign('campaign 10',
524  'subject 10',
525  '[[FIRST^NAME]]',
526  '[[LAST^NAME]]',
527  null,
528  null,
529  null,
530  Campaign::STATUS_PROCESSING,
531  null,
532  0,
533  $marketingList);
534  $this->assertNotNull($campaign);
535  $campaignId = $campaign->id;
536  foreach ($contacts as $contact)
537  {
538  CampaignItemTestHelper::createCampaignItem(0, $campaign, $contact);
539  }
540  $this->assertTrue($job->run());
541  $campaign = Campaign::getById($campaignId);
542  $campaignItemsCountExpected = $campaignItemsCountBefore + 5;
543  $campaignItemsCountAfter = CampaignItem::getCount();
544  $this->assertEquals($campaignItemsCountExpected, $campaignItemsCountAfter);
545  $campaignItemsProcessed = CampaignItem::getByProcessedAndCampaignId(1, $campaign->id);
546  $this->assertCount(5, $campaignItemsProcessed);
547  $skippedCount = 0;
548  foreach ($campaignItemsProcessed as $campaignItem)
549  {
550  $contact = $campaignItem->contact;
551  $member = MarketingListMember::getByMarketingListIdContactIdAndUnsubscribed($campaign->marketingList->id,
552  $contact->id,
553  true);
554  if ($member)
555  {
556  $personId = $campaignItem->contact->getClassId('Person');
557  $type = CampaignItemActivity::TYPE_SKIP;
558  $activity = CampaignItemActivity::getByTypeAndModelIdAndPersonIdAndUrl($type,
559  $campaignItem->id,
560  $personId);
561  $this->assertNotNull($activity);
562  $this->assertNotEmpty($activity);
563  $this->assertCount(1, $activity);
564  $skippedCount++;
565  }
566  else
567  {
568  $this->assertNotEmpty($campaignItem->emailMessage);
569  $this->assertContains($contact->firstName, $campaignItem->emailMessage->content->textContent);
570  $this->assertContains($contact->lastName, $campaignItem->emailMessage->content->htmlContent);
571  }
572  }
573  $this->assertEquals(2, $skippedCount);
574  }
575  }
576 ?>
Definition: Email.php:37
static getByProcessedAndCampaignId($processed, $campaignId, $pageSize=null)
static getByUsername($username)
Definition: User.php:49
static getByProcessed($processed, $pageSize=null)
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getById($id, $modelClassName=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Generated on Tue Jul 7 2020 07:10:31
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.