/
TriggerCampaignCommandTest.php
557 lines (442 loc) · 21.6 KB
/
TriggerCampaignCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
/*
* @copyright 2017 Mautic Contributors. All rights reserved
* @author Mautic, Inc.
*
* @link https://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Mautic\CampaignBundle\Tests\Command;
class TriggerCampaignCommandTest extends AbstractCampaignCommand
{
protected function setUp()
{
parent::setUp();
putenv('CAMPAIGN_EXECUTIONER_SCHEDULER_ACKNOWLEDGE_SECONDS=1');
}
public function tearDown()
{
parent::tearDown();
putenv('CAMPAIGN_EXECUTIONER_SCHEDULER_ACKNOWLEDGE_SECONDS=0');
}
/**
* @throws \Exception
*/
public function testCampaignExecutionForAll()
{
// Process in batches of 10 to ensure batching is working as expected
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '-l' => 10]);
// Let's analyze
$byEvent = $this->getCampaignEventLogs([1, 2, 11, 12, 13, 16]);
$tags = $this->getTagCounts();
// Everyone should have been tagged with CampaignTest and have been sent Campaign Test Email 1
$this->assertCount(50, $byEvent[1]);
$this->assertCount(50, $byEvent[2]);
// Sending Campaign Test Email 1 should be scheduled
foreach ($byEvent[2] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Sending Campaign Test Email 1 was not scheduled for lead ID '.$log['lead_id']);
}
}
// Everyone should have had the Is US condition processed
$this->assertCount(50, $byEvent[11]);
// 42 should have been send down the non-action path (red) of the condition
$nonActionCount = $this->getNonActionPathTakenCount($byEvent[11]);
$this->assertEquals(42, $nonActionCount);
// 8 contacts are from the US and should be labeled with US:Action
$this->assertCount(8, $byEvent[12]);
$this->assertEquals(8, $tags['US:Action']);
// Those tagged with US:Action should also be tagged with ChainedAction by a chained event.
$this->assertCount(8, $byEvent[16]);
$this->assertEquals(8, $tags['ChainedAction']);
// The rest (42) contacts are not from the US and should be labeled with NonUS:Action
$this->assertCount(42, $byEvent[13]);
$this->assertEquals(42, $tags['NonUS:Action']);
// No emails should be sent till after 5 seconds and the command is ran again
$stats = $this->db->createQueryBuilder()
->select('*')
->from($this->prefix.'email_stats', 'stat')
->where('stat.lead_id <= 25')
->execute()
->fetchAll();
$this->assertCount(0, $stats);
// Wait 20 seconds then execute the campaign again to send scheduled events
sleep(20);
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '-l' => 10]);
// Send email 1 should no longer be scheduled
$byEvent = $this->getCampaignEventLogs([2, 4]);
$this->assertCount(50, $byEvent[2]);
foreach ($byEvent[2] as $log) {
if (1 === (int) $log['is_scheduled']) {
$this->fail('Sending Campaign Test Email 1 is still scheduled for lead ID '.$log['lead_id']);
}
}
// The non-action events attached to the decision should have no logs entries
$this->assertCount(0, $byEvent[4]);
// Check that the emails actually sent
$stats = $this->db->createQueryBuilder()
->select('*')
->from($this->prefix.'email_stats', 'stat')
->where('stat.lead_id <= 25')
->execute()
->fetchAll();
$this->assertCount(25, $stats);
// Now let's simulate email opens
foreach ($stats as $stat) {
$this->client->request('GET', '/email/'.$stat['tracking_hash'].'.gif');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(), var_export($this->client->getResponse()->getContent()));
}
$byEvent = $this->getCampaignEventLogs([3, 4, 5, 10, 14, 15]);
// The non-action events attached to the decision should have no logs entries
$this->assertCount(0, $byEvent[4]);
$this->assertCount(0, $byEvent[5]);
$this->assertCount(0, $byEvent[14]);
$this->assertCount(0, $byEvent[15]);
// Those 25 should now have open email decisions logged and the next email sent
$this->assertCount(25, $byEvent[3]);
$this->assertCount(25, $byEvent[10]);
// Wait 20 seconds to go beyond the inaction timeframe
sleep(20);
// Execute the command again to trigger inaction related events
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '-l' => 10]);
// Now we should have 50 email open decisions
$byEvent = $this->getCampaignEventLogs([3, 4, 5, 14, 15]);
$this->assertCount(50, $byEvent[3]);
// 25 should be marked as non_action_path_taken
$nonActionCount = $this->getNonActionPathTakenCount($byEvent[3]);
$this->assertEquals(25, $nonActionCount);
// A condition should be logged as evaluated for each of the 25 contacts
$this->assertCount(25, $byEvent[4]);
$this->assertCount(25, $byEvent[5]);
// Tag EmailNotOpen should all be scheduled for these 25 contacts because the condition's timeframe was shorter and therefore the
// contact was sent down the inaction path
$this->assertCount(25, $byEvent[14]);
$this->assertCount(25, $byEvent[15]);
$utcTimezone = new \DateTimeZone('UTC');
foreach ($byEvent[14] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Tag EmailNotOpen is not scheduled for lead ID '.$log['lead_id']);
}
$scheduledFor = new \DateTime($log['trigger_date'], $utcTimezone);
$diff = $this->eventDate->diff($scheduledFor);
if (2 !== $diff->i) {
$this->fail('Tag EmailNotOpen should be scheduled for around 2 minutes ('.$diff->i.' minutes)');
}
}
foreach ($byEvent[15] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Tag EmailNotOpen Again is not scheduled for lead ID '.$log['lead_id']);
}
$scheduledFor = new \DateTime($log['trigger_date'], $utcTimezone);
$diff = $this->eventDate->diff($scheduledFor);
if (6 !== $diff->i) {
$this->fail('Tag EmailNotOpen Again should be scheduled for around 6 minutes ('.$diff->i.' minutes)');
}
}
$byEvent = $this->getCampaignEventLogs([6, 7, 8, 9]);
$tags = $this->getTagCounts();
// Of those that did not open the email, 6 should be tagged US:NotOpen
$this->assertCount(6, $byEvent[6]);
$this->assertEquals(6, $tags['US:NotOpen']);
// And 19 should be tagged NonUS:NotOpen
$this->assertCount(19, $byEvent[7]);
$this->assertEquals(19, $tags['NonUS:NotOpen']);
// And 4 should be tagged UK:NotOpen
$this->assertCount(4, $byEvent[8]);
$this->assertEquals(4, $tags['UK:NotOpen']);
// And 21 should be tagged NonUK:NotOpen
$this->assertCount(21, $byEvent[9]);
$this->assertEquals(21, $tags['NonUK:NotOpen']);
// No one should be tagged as EmailNotOpen because the actions are still scheduled
$this->assertFalse(isset($tags['EmailNotOpen']));
}
/**
* @throws \Exception
*/
public function testCampaignExecutionForOne()
{
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '--contact-id' => 1]);
// Let's analyze
$byEvent = $this->getCampaignEventLogs([1, 2, 11, 12, 13, 16]);
$tags = $this->getTagCounts();
// Everyone should have been tagged with CampaignTest and have been sent Campaign Test Email 1
$this->assertCount(1, $byEvent[1]);
$this->assertCount(1, $byEvent[2]);
// Sending Campaign Test Email 1 should be scheduled
foreach ($byEvent[2] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Sending Campaign Test Email 1 was not scheduled for lead ID '.$log['lead_id']);
}
}
// Everyone should have had the Is US condition processed
$this->assertCount(1, $byEvent[11]);
// 1 should have been send down the non-action path (red) of the condition
$nonActionCount = $this->getNonActionPathTakenCount($byEvent[11]);
$this->assertEquals(1, $nonActionCount);
// 0 contacts are from the US and should be labeled with US:Action
$this->assertCount(0, $byEvent[12]);
$this->assertTrue(empty($tags['US:Action']));
// None tagged with US:Action, so none should be tagged with ChainedAction by a chained event.
$this->assertCount(0, $byEvent[16]);
$this->assertTrue(empty($tags['ChainedAction']));
// The rest (1) contacts are not from the US and should be labeled with NonUS:Action
$this->assertCount(1, $byEvent[13]);
$this->assertEquals(1, $tags['NonUS:Action']);
// No emails should be sent till after 5 seconds and the command is ran again
$stats = $this->db->createQueryBuilder()
->select('*')
->from($this->prefix.'email_stats', 'stat')
->where('stat.lead_id = 1')
->execute()
->fetchAll();
$this->assertCount(0, $stats);
// Wait 20 seconds then execute the campaign again to send scheduled events
sleep(20);
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '--contact-id' => 1]);
// Send email 1 should no longer be scheduled
$byEvent = $this->getCampaignEventLogs([2, 4]);
$this->assertCount(1, $byEvent[2]);
foreach ($byEvent[2] as $log) {
if (1 === (int) $log['is_scheduled']) {
$this->fail('Sending Campaign Test Email 1 is still scheduled for lead ID '.$log['lead_id']);
}
}
// The non-action events attached to the decision should have no logs entries
$this->assertCount(0, $byEvent[4]);
// Check that the emails actually sent
$stats = $this->db->createQueryBuilder()
->select('*')
->from($this->prefix.'email_stats', 'stat')
->where('stat.lead_id = 1')
->execute()
->fetchAll();
$this->assertCount(1, $stats);
// Now let's simulate email opens
foreach ($stats as $stat) {
$this->client->request('GET', '/email/'.$stat['tracking_hash'].'.gif');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(), var_export($this->client->getResponse()->getContent()));
}
$byEvent = $this->getCampaignEventLogs([3, 4, 5, 10, 14, 15]);
// The non-action events attached to the decision should have no logs entries
$this->assertCount(0, $byEvent[4]);
$this->assertCount(0, $byEvent[5]);
$this->assertCount(0, $byEvent[14]);
$this->assertCount(0, $byEvent[15]);
// The 1 should now have open email decisions logged and the next email sent
$this->assertCount(1, $byEvent[3]);
$this->assertCount(1, $byEvent[10]);
// Wait 20 seconds to go beyond the inaction timeframe
sleep(20);
// Execute the command again to trigger inaction related events
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '--contact-id' => 1]);
// Now we should have 1 email open decisions
$byEvent = $this->getCampaignEventLogs([3, 4, 5, 14, 15]);
$this->assertCount(1, $byEvent[3]);
// 0 should be marked as non_action_path_taken
$nonActionCount = $this->getNonActionPathTakenCount($byEvent[3]);
$this->assertEquals(0, $nonActionCount);
// There should be no inactive events
$this->assertCount(0, $byEvent[4]);
$this->assertCount(0, $byEvent[5]);
$this->assertCount(0, $byEvent[14]);
$this->assertCount(0, $byEvent[15]);
$utcTimezone = new \DateTimeZone('UTC');
foreach ($byEvent[14] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Tag EmailNotOpen is not scheduled for lead ID '.$log['lead_id']);
}
$scheduledFor = new \DateTime($log['trigger_date'], $utcTimezone);
$diff = $this->eventDate->diff($scheduledFor);
if (2 !== $diff->i) {
$this->fail('Tag EmailNotOpen should be scheduled for around 2 minutes ('.$diff->i.' minutes)');
}
}
foreach ($byEvent[15] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Tag EmailNotOpen Again is not scheduled for lead ID '.$log['lead_id']);
}
$scheduledFor = new \DateTime($log['trigger_date'], $utcTimezone);
$diff = $this->eventDate->diff($scheduledFor);
if (6 !== $diff->i) {
$this->fail('Tag EmailNotOpen Again should be scheduled for around 6 minutes ('.$diff->i.' minutes)');
}
}
$byEvent = $this->getCampaignEventLogs([6, 7, 8, 9]);
$tags = $this->getTagCounts();
// Of those that did not open the email, 0 should be tagged US:NotOpen
$this->assertCount(0, $byEvent[6]);
$this->assertTrue(empty($tags['US:NotOpen']));
// And 0 should be tagged NonUS:NotOpen
$this->assertCount(0, $byEvent[7]);
$this->assertTrue(empty($tags['NonUS:NotOpen']));
// And 0 should be tagged UK:NotOpen
$this->assertCount(0, $byEvent[8]);
$this->assertTrue(empty($tags['UK:NotOpen']));
// And 0 should be tagged NonUK:NotOpen
$this->assertCount(0, $byEvent[9]);
$this->assertTrue(empty($tags['NonUK:NotOpen']));
// No one should be tagged as EmailNotOpen because the actions are still scheduled
$this->assertTrue(empty($tags['EmailNotOpen']));
}
public function testCampaignExecutionForSome()
{
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '--contact-ids' => '1,2,3,4,19']);
// Let's analyze
$byEvent = $this->getCampaignEventLogs([1, 2, 11, 12, 13, 16]);
$tags = $this->getTagCounts();
// Everyone should have been tagged with CampaignTest and have been sent Campaign Test Email 1
$this->assertCount(5, $byEvent[1]);
$this->assertCount(5, $byEvent[2]);
// Sending Campaign Test Email 1 should be scheduled
foreach ($byEvent[2] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Sending Campaign Test Email 1 was not scheduled for lead ID '.$log['lead_id']);
}
}
// Everyone should have had the Is US condition processed
$this->assertCount(5, $byEvent[11]);
// 4 should have been send down the non-action path (red) of the condition
$nonActionCount = $this->getNonActionPathTakenCount($byEvent[11]);
$this->assertEquals(4, $nonActionCount);
// 1 contacts are from the US and should be labeled with US:Action
$this->assertCount(1, $byEvent[12]);
$this->assertEquals(1, $tags['US:Action']);
// Those tagged with US:Action should also be tagged with ChainedAction by a chained event.
$this->assertCount(1, $byEvent[16]);
$this->assertEquals(1, $tags['ChainedAction']);
// The rest (4) contacts are not from the US and should be labeled with NonUS:Action
$this->assertCount(4, $byEvent[13]);
$this->assertEquals(4, $tags['NonUS:Action']);
// No emails should be sent till after 5 seconds and the command is ran again
$stats = $this->db->createQueryBuilder()
->select('*')
->from($this->prefix.'email_stats', 'stat')
->where('stat.lead_id <= 2')
->execute()
->fetchAll();
$this->assertCount(0, $stats);
// Wait 20 seconds then execute the campaign again to send scheduled events
sleep(20);
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '--contact-ids' => '1,2,3,4,19']);
// Send email 1 should no longer be scheduled
$byEvent = $this->getCampaignEventLogs([2, 4]);
$this->assertCount(5, $byEvent[2]);
foreach ($byEvent[2] as $log) {
if (1 === (int) $log['is_scheduled']) {
$this->fail('Sending Campaign Test Email 1 is still scheduled for lead ID '.$log['lead_id']);
}
}
// The non-action events attached to the decision should have no logs entries
$this->assertCount(0, $byEvent[4]);
// Check that the emails actually sent
$stats = $this->db->createQueryBuilder()
->select('*')
->from($this->prefix.'email_stats', 'stat')
->where('stat.lead_id <= 2')
->execute()
->fetchAll();
$this->assertCount(2, $stats);
// Now let's simulate email opens
foreach ($stats as $stat) {
$this->client->request('GET', '/email/'.$stat['tracking_hash'].'.gif');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(), var_export($this->client->getResponse()->getContent()));
}
$byEvent = $this->getCampaignEventLogs([3, 4, 5, 10, 14, 15]);
// The non-action events attached to the decision should have no logs entries
$this->assertCount(0, $byEvent[4]);
$this->assertCount(0, $byEvent[5]);
$this->assertCount(0, $byEvent[14]);
$this->assertCount(0, $byEvent[15]);
// Those 25 should now have open email decisions logged and the next email sent
$this->assertCount(2, $byEvent[3]);
$this->assertCount(2, $byEvent[10]);
// Wait 20 seconds to go beyond the inaction timeframe
sleep(20);
// Execute the command again to trigger inaction related events
$this->runCommand('mautic:campaigns:trigger', ['-i' => 1, '--contact-ids' => '1,2,3,4,19']);
// Now we should have 5 email open decisions
$byEvent = $this->getCampaignEventLogs([3, 4, 5, 14, 15]);
$this->assertCount(5, $byEvent[3]);
// 3 should be marked as non_action_path_taken
$nonActionCount = $this->getNonActionPathTakenCount($byEvent[3]);
$this->assertEquals(3, $nonActionCount);
// A condition should be logged as evaluated for each of the 3 contacts
$this->assertCount(3, $byEvent[4]);
$this->assertCount(3, $byEvent[5]);
// Tag EmailNotOpen should all be scheduled for these 3 contacts because the condition's timeframe was shorter and therefore the
// contact was sent down the inaction path
$this->assertCount(3, $byEvent[14]);
$this->assertCount(3, $byEvent[15]);
$utcTimezone = new \DateTimeZone('UTC');
foreach ($byEvent[14] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Tag EmailNotOpen is not scheduled for lead ID '.$log['lead_id']);
}
$scheduledFor = new \DateTime($log['trigger_date'], $utcTimezone);
$diff = $this->eventDate->diff($scheduledFor);
if (2 !== $diff->i) {
$this->fail('Tag EmailNotOpen should be scheduled for around 2 minutes ('.$diff->i.' minutes)');
}
}
foreach ($byEvent[15] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Tag EmailNotOpen Again is not scheduled for lead ID '.$log['lead_id']);
}
$scheduledFor = new \DateTime($log['trigger_date'], $utcTimezone);
$diff = $this->eventDate->diff($scheduledFor);
if (6 !== $diff->i) {
$this->fail('Tag EmailNotOpen Again should be scheduled for around 6 minutes ('.$diff->i.' minutes)');
}
}
$byEvent = $this->getCampaignEventLogs([6, 7, 8, 9]);
$tags = $this->getTagCounts();
// Of those that did not open the email, 1 should be tagged US:NotOpen
$this->assertCount(1, $byEvent[6]);
$this->assertEquals(1, $tags['US:NotOpen']);
// And 2 should be tagged NonUS:NotOpen
$this->assertCount(2, $byEvent[7]);
$this->assertEquals(2, $tags['NonUS:NotOpen']);
// And 2 should be tagged UK:NotOpen
$this->assertCount(2, $byEvent[8]);
$this->assertEquals(2, $tags['UK:NotOpen']);
// And 1 should be tagged NonUK:NotOpen
$this->assertCount(1, $byEvent[9]);
$this->assertEquals(1, $tags['NonUK:NotOpen']);
// No one should be tagged as EmailNotOpen because the actions are still scheduled
$this->assertFalse(isset($tags['EmailNotOpen']));
}
/**
* @return array
*/
private function getTagCounts()
{
$tags = $this->db->createQueryBuilder()
->select('t.tag, count(*) as the_count')
->from($this->prefix.'lead_tags', 't')
->join('t', $this->prefix.'lead_tags_xref', 'l', 't.id = l.tag_id')
->groupBy('t.tag')
->execute()
->fetchAll();
$tagCounts = [];
foreach ($tags as $tag) {
$tagCounts[$tag['tag']] = (int) $tag['the_count'];
}
return $tagCounts;
}
/**
* @param array $logs
*
* @return int
*/
private function getNonActionPathTakenCount(array $logs)
{
$nonActionCount = 0;
foreach ($logs as $log) {
if ((int) $log['non_action_path_taken']) {
++$nonActionCount;
}
}
return $nonActionCount;
}
}