|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Mautic\LeadBundle\Tests\Functional; |
| 6 | + |
| 7 | +use Mautic\CoreBundle\Test\MauticMysqlTestCase; |
| 8 | +use Mautic\LeadBundle\Entity\Lead; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
| 10 | + |
| 11 | +final class DncSearchFunctionalTest extends MauticMysqlTestCase |
| 12 | +{ |
| 13 | + protected $useCleanupRollback = false; |
| 14 | + |
| 15 | + private const MESSAGE_EMAIL_DNC_SHOULD_APPEAR_IN_EMAIL_SEARCH = 'Contact with email DNC should appear in dnc:email search'; |
| 16 | + |
| 17 | + public function testDncSearchWithAnyChannel(): void |
| 18 | + { |
| 19 | + $contact1 = $this->createContact('contact1@test.com'); |
| 20 | + $contact2 = $this->createContact('contact2@test.com'); |
| 21 | + $contact3 = $this->createContact('contact3@test.com'); |
| 22 | + |
| 23 | + $this->addDncRecord($contact1->getId(), 'email'); |
| 24 | + $this->addDncRecord($contact2->getId(), 'sms'); |
| 25 | + |
| 26 | + $crawler = $this->client->request(Request::METHOD_GET, '/s/contacts?search=dnc%3Aany'); |
| 27 | + $this->assertResponseIsSuccessful(); |
| 28 | + $responseText = $crawler->text(); |
| 29 | + |
| 30 | + $this->assertStringContainsString($contact1->getEmail(), $responseText, 'Contact with email DNC should appear in dnc:any search'); |
| 31 | + $this->assertStringContainsString($contact2->getEmail(), $responseText, 'Contact with SMS DNC should appear in dnc:any search'); |
| 32 | + $this->assertStringNotContainsString($contact3->getEmail(), $responseText, 'Contact without DNC should not appear in dnc:any search'); |
| 33 | + } |
| 34 | + |
| 35 | + public function testDncSearchWithSpecificChannel(): void |
| 36 | + { |
| 37 | + $contact1 = $this->createContact('email-dnc@test.com'); |
| 38 | + $contact2 = $this->createContact('sms-dnc@test.com'); |
| 39 | + $contact3 = $this->createContact('no-dnc@test.com'); |
| 40 | + |
| 41 | + $this->addDncRecord($contact1->getId(), 'email'); |
| 42 | + $this->addDncRecord($contact2->getId(), 'sms'); |
| 43 | + |
| 44 | + $crawler = $this->client->request(Request::METHOD_GET, '/s/contacts?search=dnc%3Aemail'); |
| 45 | + $this->assertResponseIsSuccessful(); |
| 46 | + $responseText = $crawler->text(); |
| 47 | + |
| 48 | + $this->assertStringContainsString($contact1->getEmail(), $responseText, self::MESSAGE_EMAIL_DNC_SHOULD_APPEAR_IN_EMAIL_SEARCH); |
| 49 | + $this->assertStringNotContainsString($contact2->getEmail(), $responseText, 'Contact with SMS DNC should not appear in dnc:email search'); |
| 50 | + $this->assertStringNotContainsString($contact3->getEmail(), $responseText, 'Contact without DNC should not appear in dnc:email search'); |
| 51 | + |
| 52 | + $crawler = $this->client->request(Request::METHOD_GET, '/s/contacts?search=dnc%3Asms'); |
| 53 | + $this->assertResponseIsSuccessful(); |
| 54 | + $responseText = $crawler->text(); |
| 55 | + |
| 56 | + $this->assertStringNotContainsString($contact1->getEmail(), $responseText, 'Contact with email DNC should not appear in dnc:sms search'); |
| 57 | + $this->assertStringContainsString($contact2->getEmail(), $responseText, 'Contact with SMS DNC should appear in dnc:sms search'); |
| 58 | + $this->assertStringNotContainsString($contact3->getEmail(), $responseText, 'Contact without DNC should not appear in dnc:sms search'); |
| 59 | + } |
| 60 | + |
| 61 | + public function testDncSearchNegation(): void |
| 62 | + { |
| 63 | + $contact1 = $this->createContact('dnc-contact@test.com'); |
| 64 | + $contact2 = $this->createContact('normal-contact@test.com'); |
| 65 | + |
| 66 | + $this->addDncRecord($contact1->getId(), 'email'); |
| 67 | + |
| 68 | + $crawler = $this->client->request(Request::METHOD_GET, '/s/contacts?search=!dnc%3Aany'); |
| 69 | + $this->assertResponseIsSuccessful(); |
| 70 | + $responseText = $crawler->text(); |
| 71 | + |
| 72 | + $this->assertStringNotContainsString($contact1->getEmail(), $responseText, 'Contact with DNC should not appear in negative dnc:any search'); |
| 73 | + $this->assertStringContainsString($contact2->getEmail(), $responseText, 'Contact without DNC should appear in negative dnc:any search'); |
| 74 | + } |
| 75 | + |
| 76 | + public function testDncSearchWithMultipleChannelsOnSameContact(): void |
| 77 | + { |
| 78 | + $contact1 = $this->createContact('multi-dnc@test.com'); |
| 79 | + $contact2 = $this->createContact('single-dnc@test.com'); |
| 80 | + $contact3 = $this->createContact('no-dnc-multiple@test.com'); |
| 81 | + |
| 82 | + $this->addDncRecord($contact1->getId(), 'email'); |
| 83 | + $this->addDncRecord($contact1->getId(), 'sms'); |
| 84 | + |
| 85 | + $this->addDncRecord($contact2->getId(), 'email'); |
| 86 | + |
| 87 | + $crawler = $this->client->request(Request::METHOD_GET, '/s/contacts?search=dnc%3Aany'); |
| 88 | + $this->assertResponseIsSuccessful(); |
| 89 | + $responseText = $crawler->text(); |
| 90 | + |
| 91 | + $this->assertStringContainsString($contact1->getEmail(), $responseText, 'Contact with multiple DNC channels should appear in dnc:any search'); |
| 92 | + $this->assertStringContainsString($contact2->getEmail(), $responseText, 'Contact with single DNC channel should appear in dnc:any search'); |
| 93 | + $this->assertStringNotContainsString($contact3->getEmail(), $responseText, 'Contact without DNC should not appear in dnc:any search'); |
| 94 | + |
| 95 | + $crawler = $this->client->request(Request::METHOD_GET, '/s/contacts?search=dnc%3Aemail'); |
| 96 | + $this->assertResponseIsSuccessful(); |
| 97 | + $responseText = $crawler->text(); |
| 98 | + |
| 99 | + $this->assertStringContainsString($contact1->getEmail(), $responseText, self::MESSAGE_EMAIL_DNC_SHOULD_APPEAR_IN_EMAIL_SEARCH); |
| 100 | + $this->assertStringContainsString($contact2->getEmail(), $responseText, self::MESSAGE_EMAIL_DNC_SHOULD_APPEAR_IN_EMAIL_SEARCH); |
| 101 | + $this->assertStringNotContainsString($contact3->getEmail(), $responseText, 'Contact without email DNC should not appear in dnc:email search'); |
| 102 | + |
| 103 | + $crawler = $this->client->request(Request::METHOD_GET, '/s/contacts?search=dnc%3Asms'); |
| 104 | + $this->assertResponseIsSuccessful(); |
| 105 | + $responseText = $crawler->text(); |
| 106 | + |
| 107 | + $this->assertStringContainsString($contact1->getEmail(), $responseText, 'Contact with SMS DNC should appear in dnc:sms search'); |
| 108 | + $this->assertStringNotContainsString($contact2->getEmail(), $responseText, 'Contact without SMS DNC should not appear in dnc:sms search'); |
| 109 | + $this->assertStringNotContainsString($contact3->getEmail(), $responseText, 'Contact without SMS DNC should not appear in dnc:sms search'); |
| 110 | + } |
| 111 | + |
| 112 | + private function createContact(string $email): Lead |
| 113 | + { |
| 114 | + $contact = new Lead(); |
| 115 | + $contact->setEmail($email); |
| 116 | + $contact->setDateIdentified(new \DateTime()); |
| 117 | + $this->em->persist($contact); |
| 118 | + $this->em->flush(); |
| 119 | + |
| 120 | + return $contact; |
| 121 | + } |
| 122 | + |
| 123 | + private function addDncRecord(int $contactId, string $channel): void |
| 124 | + { |
| 125 | + $this->em->getConnection()->executeStatement( |
| 126 | + 'INSERT INTO '.MAUTIC_TABLE_PREFIX.'lead_donotcontact (lead_id, channel, reason, comments, date_added) VALUES (?, ?, ?, ?, ?)', |
| 127 | + [$contactId, $channel, 1, 'Test DNC', new \DateTime()], |
| 128 | + [\Doctrine\DBAL\Types\Types::INTEGER, \Doctrine\DBAL\Types\Types::STRING, \Doctrine\DBAL\Types\Types::INTEGER, \Doctrine\DBAL\Types\Types::STRING, \Doctrine\DBAL\Types\Types::DATETIME_MUTABLE] |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments