Spaces:
No application file
No application file
File size: 32,468 Bytes
d2897cd |
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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
<?php
declare(strict_types=1);
namespace Mautic\CampaignBundle\Tests\Command;
use Mautic\CampaignBundle\Entity\Campaign;
use Mautic\CampaignBundle\Entity\Lead;
use Mautic\CampaignBundle\Entity\LeadEventLog;
use Mautic\CampaignBundle\Executioner\InactiveExecutioner;
use Mautic\CampaignBundle\Executioner\ScheduledExecutioner;
use Mautic\LeadBundle\Entity\ListLead;
use Mautic\LeadBundle\Helper\SegmentCountCacheHelper;
use PHPUnit\Framework\Assert;
class TriggerCampaignCommandTest extends AbstractCampaignCommand
{
private ?SegmentCountCacheHelper $segmentCountCacheHelper = null;
protected function setUp(): void
{
parent::setUp();
putenv('CAMPAIGN_EXECUTIONER_SCHEDULER_ACKNOWLEDGE_SECONDS=1');
$this->segmentCountCacheHelper = static::getContainer()->get('mautic.helper.segment.count.cache');
}
public function beforeTearDown(): void
{
parent::beforeTearDown();
putenv('CAMPAIGN_EXECUTIONER_SCHEDULER_ACKNOWLEDGE_SECONDS=0');
$this->segmentCountCacheHelper = null;
}
/**
* @throws \Exception
*/
public function testCampaignExecutionForAll(): void
{
// Process in batches of 10 to ensure batching is working as expected
$this->testSymfonyCommand('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')
->executeQuery()
->fetchAllAssociative();
$this->assertCount(0, $stats);
// Wait 6 seconds then execute the campaign again to send scheduled events
static::getContainer()->get(ScheduledExecutioner::class)->setNowTime(new \DateTime('+'.self::CONDITION_SECONDS.' seconds'));
$this->testSymfonyCommand('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')
->executeQuery()
->fetchAllAssociative();
$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(), true));
}
$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 another 6 seconds to go beyond the inaction timeframe
static::getContainer()->get(InactiveExecutioner::class)->setNowTime(new \DateTime('+'.(self::CONDITION_SECONDS * 2).' seconds'));
// Execute the command again to trigger inaction related events
$this->testSymfonyCommand('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(): void
{
$this->testSymfonyCommand('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')
->executeQuery()
->fetchAllAssociative();
$this->assertCount(0, $stats);
// Wait 6 seconds then execute the campaign again to send scheduled events
static::getContainer()->get(ScheduledExecutioner::class)->setNowTime(new \DateTime('+'.self::CONDITION_SECONDS.' seconds'));
$this->testSymfonyCommand('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')
->executeQuery()
->fetchAllAssociative();
$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(), true));
}
$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 6 seconds to go beyond the inaction timeframe
static::getContainer()->get(InactiveExecutioner::class)->setNowTime(new \DateTime('+'.(self::CONDITION_SECONDS * 2).' seconds'));
// Execute the command again to trigger inaction related events
$this->testSymfonyCommand('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(): void
{
$this->testSymfonyCommand('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')
->executeQuery()
->fetchAllAssociative();
$this->assertCount(0, $stats);
// Wait 6 seconds then execute the campaign again to send scheduled events
static::getContainer()->get(ScheduledExecutioner::class)->setNowTime(new \DateTime('+'.self::CONDITION_SECONDS.' seconds'));
$this->testSymfonyCommand('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')
->executeQuery()
->fetchAllAssociative();
$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(), true));
}
$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 6 seconds to go beyond the inaction timeframe
static::getContainer()->get(InactiveExecutioner::class)->setNowTime(new \DateTime('+'.(self::CONDITION_SECONDS * 2).' seconds'));
// Execute the command again to trigger inaction related events
$this->testSymfonyCommand('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']));
}
public function testCampaignActionChangeMembership(): void
{
$campaign1 = $this->createCampaign('Campaign 1');
$campaign2 = $this->createCampaign('Campaign 2');
$lead = $this->createLead('Lead');
$this->createCampaignLead($campaign1, $lead);
$this->createCampaignLead($campaign2, $lead);
$this->em->flush();
$property = ['addTo' => [$campaign2->getId()], 'removeFrom' => ['this']];
$this->createEvent('Event', $campaign1, 'campaign.addremovelead', 'action', $property);
$this->em->flush();
$this->em->clear();
$this->testSymfonyCommand('mautic:campaigns:trigger', ['--campaign-id' => $campaign1->getId(), '--contact-id' => $lead->getId(), '--kickoff-only' => true]);
$campaignLeads = $this->em->getRepository(Lead::class)->findBy(['lead' => $lead], ['campaign' => 'ASC']);
Assert::assertCount(2, $campaignLeads);
Assert::assertSame($campaign1->getId(), $campaignLeads[0]->getCampaign()->getId());
Assert::assertTrue($campaignLeads[0]->getManuallyRemoved());
Assert::assertSame($campaign2->getId(), $campaignLeads[1]->getCampaign()->getId());
Assert::assertFalse($campaignLeads[1]->getManuallyRemoved());
}
public function testCampaignActionAfterChangeMembership(): void
{
$campaign = $this->createCampaign('Campaign 1');
$lead = $this->createLead('Lead');
$this->createCampaignLead($campaign, $lead);
$this->em->flush();
$property = ['removeFrom' => ['this']];
$event1 = $this->createEvent('Event', $campaign, 'campaign.addremovelead', 'action', $property);
$property = ['points' => 1];
$event2 = $this->createEvent('Event', $campaign, 'lead.changepoints', 'action', $property);
$this->em->flush();
$this->em->clear();
$this->testSymfonyCommand('mautic:campaigns:trigger', ['--campaign-id' => $campaign->getId(), '--contact-id' => $lead->getId(), '--kickoff-only' => true]);
$campaignLeads = $this->em->getRepository(Lead::class)->findBy(['lead' => $lead]);
Assert::assertCount(1, $campaignLeads);
Assert::assertSame($campaign->getId(), $campaignLeads[0]->getCampaign()->getId());
Assert::assertTrue($campaignLeads[0]->getManuallyRemoved());
$campaignEventLogs = $this->em->getRepository(LeadEventLog::class)->findBy(['campaign' => $campaign, 'lead' => $lead], ['event' => 'ASC']);
Assert::assertCount(1, $campaignEventLogs);
Assert::assertSame($campaign->getId(), $campaignEventLogs[0]->getCampaign()->getId());
Assert::assertSame($event1->getId(), $campaignEventLogs[0]->getEvent()->getId());
}
public function testCampaignActionBeforeChangeMembership(): void
{
$campaign = $this->createCampaign('Campaign 1');
$lead = $this->createLead('Lead');
$this->createCampaignLead($campaign, $lead);
$this->em->flush();
$property = ['points' => 1];
$event1 = $this->createEvent('Event', $campaign, 'lead.changepoints', 'action', $property);
$property = ['removeFrom' => ['this']];
$event2 = $this->createEvent('Event', $campaign, 'campaign.addremovelead', 'action', $property);
$this->em->flush();
$this->em->clear();
$this->testSymfonyCommand('mautic:campaigns:trigger', ['--campaign-id' => $campaign->getId(), '--contact-id' => $lead->getId(), '--kickoff-only' => true]);
$campaignLeads = $this->em->getRepository(Lead::class)->findBy(['lead' => $lead]);
Assert::assertCount(1, $campaignLeads);
Assert::assertSame($campaign->getId(), $campaignLeads[0]->getCampaign()->getId());
Assert::assertTrue($campaignLeads[0]->getManuallyRemoved());
$campaignEventLogs = $this->em->getRepository(LeadEventLog::class)->findBy(['campaign' => $campaign, 'lead' => $lead], ['event' => 'ASC']);
Assert::assertCount(2, $campaignEventLogs);
Assert::assertSame($campaign->getId(), $campaignEventLogs[0]->getCampaign()->getId());
Assert::assertSame($event1->getId(), $campaignEventLogs[0]->getEvent()->getId());
Assert::assertSame($campaign->getId(), $campaignEventLogs[1]->getCampaign()->getId());
Assert::assertSame($event2->getId(), $campaignEventLogs[1]->getEvent()->getId());
}
public function testCampaignExclusion(): void
{
$campaign1 = $this->createCampaign('Campaign 1');
$campaign2 = $this->createCampaign('Campaign 2');
$lead = $this->createLead('Lead');
$this->createCampaignLead($campaign1, $lead);
$this->createCampaignLead($campaign2, $lead);
$this->em->flush();
$property = ['addTo' => [$campaign2->getId()], 'removeFrom' => ['this']];
$this->createEvent('Event', $campaign1, 'campaign.addremovelead', 'action', $property);
$this->em->flush();
$this->em->clear();
$this->testSymfonyCommand('mautic:campaigns:trigger', ['--exclude' => [$campaign1->getId()], '--contact-id' => $lead->getId(), '--kickoff-only' => true]);
$campaignLeads = $this->em->getRepository(Lead::class)->findBy(['lead' => $lead], ['campaign' => 'ASC']);
Assert::assertCount(2, $campaignLeads);
Assert::assertSame($campaign1->getId(), $campaignLeads[0]->getCampaign()->getId());
Assert::assertFalse($campaignLeads[0]->getManuallyRemoved(), 'Test not executed campaign does not have Contact removed.');
Assert::assertSame($campaign2->getId(), $campaignLeads[1]->getCampaign()->getId());
Assert::assertFalse($campaignLeads[1]->getManuallyRemoved());
Assert::assertFalse($campaignLeads[1]->getManuallyAdded());
}
/**
* @see https://github.com/mautic/mautic/issues/11061
*
* This test will not fail if the infinite loop returns and instead run indefinitelly until a PHPUNIT timeout is reached.
* I couldn't find an easy way to test for an infinite loop. But we'll know if it returns again.
* We'll just spend more time figuring out which test is taking so long.
*/
public function testCampaignInfiniteLoop(): void
{
$campaignMemberRepo = $this->em->getRepository(Lead::class);
$segmentMemberRepo = $this->em->getRepository(ListLead::class);
$campaignRepo = $this->em->getRepository(Campaign::class);
// Clear the campaign and segment members as those are manually_added.
$campaignMemberRepo->deleteEntities($campaignMemberRepo->findAll());
$segmentMemberRepo->deleteEntities($segmentMemberRepo->findAll());
$campaign = $campaignRepo->find(1); // Created in parent::setUp()
\assert($campaign instanceof Campaign);
$campaign->setAllowRestart(true);
$campaignRepo->saveEntity($campaign);
$john = $this->createLead('John');
$jane = $this->createLead('Jane');
$this->createSegmentMember($campaign->getLists()->first(), $john);
$this->createSegmentMember($campaign->getLists()->first(), $jane);
$this->createCampaignLead($campaign, $john);
$this->createCampaignLead($campaign, $jane, true); // Manually removed.
$this->em->flush();
$this->em->detach($campaign);
$this->em->detach($campaignRepo);
$tStart = microtime(true);
$this->testSymfonyCommand('mautic:campaigns:update', ['--campaign-id' => $campaign->getId()]);
$tDiff = microtime(true) - $tStart;
$this->assertLessThan(10, $tDiff, 'The campaign rebuild takes more than 10 seconds, probably an infinite loop.');
}
public function testCampaignExecuteOrderByDateCreatedDesc(): void
{
$oldCampaign = $this->createCampaign('Some old campaign');
$oldCampaign->setDateAdded(new \DateTime('2019-01-03 03:54:25'));
$newCampaign = $this->createCampaign('New campaign');
$newCampaign->setDateAdded(new \DateTime());
$this->em->flush();
$commandTester = $this->testSymfonyCommand('mautic:campaigns:trigger');
$commandTester->assertCommandIsSuccessful();
$lines = preg_split('/\r\n|\r|\n/', $commandTester->getDisplay());
$campaignStartLines = [];
foreach ($lines as $line) {
if (str_starts_with($line, 'Triggering events for campaign')) {
$campaignStartLines[] = $line;
}
}
// check if the new campaign processed first
$this->assertEquals("Triggering events for campaign {$newCampaign->getId()}", $campaignStartLines[0]);
}
/**
* @throws \Exception
*/
public function testSegmentCacheCount(): void
{
// Execute the command again to trigger related events.
$this->testSymfonyCommand('mautic:campaigns:trigger', ['-i' => 1]);
// Segment cache count should be 50.
$count = $this->segmentCountCacheHelper->getSegmentContactCount(1);
self::assertEquals(50, $count);
}
/**
* @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')
->executeQuery()
->fetchAllAssociative();
$tagCounts = [];
foreach ($tags as $tag) {
$tagCounts[$tag['tag']] = (int) $tag['the_count'];
}
return $tagCounts;
}
/**
* @return int
*/
private function getNonActionPathTakenCount(array $logs)
{
$nonActionCount = 0;
foreach ($logs as $log) {
if ((int) $log['non_action_path_taken']) {
++$nonActionCount;
}
}
return $nonActionCount;
}
}
|