File size: 5,139 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
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Command;

use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Command\DeduplicateCommand;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadField;
use PHPUnit\Framework\Assert;

final class DeduplicateCommandFunctionalTest extends MauticMysqlTestCase
{
    protected $useCleanupRollback = false;

    protected function setUp(): void
    {
        if ('testDeduplicateCommandWithAnotherUniqueFieldAndAnd' === $this->getName()) {
            $this->configParams['contact_unique_identifiers_operator'] = CompositeExpression::TYPE_AND;
        }

        parent::setUp();
    }

    public function testDeduplicateCommandWithUniqueEmail(): void
    {
        $contactRepository = $this->em->getRepository(Lead::class);

        $contactDeduper = static::getContainer()->get('mautic.lead.deduper');

        Assert::assertSame(0, $contactRepository->count([]), 'Some contacts were forgotten to remove from other tests');

        $this->saveContact('[email protected]'); // 1
        $this->saveContact('[email protected]'); // 1
        $this->saveContact('[email protected]'); // 1
        $this->saveContact('[email protected]'); // 1
        $this->saveContact('[email protected]'); // 2
        $this->saveContact('[email protected]'); // 2
        $this->saveContact('[email protected]'); // 3

        $this->em->flush();

        Assert::assertSame(7, $contactRepository->count([]));

        Assert::assertSame(
            2,
            $contactDeduper->countDuplicatedContacts(array_keys($contactDeduper->getUniqueFields('lead'))),
            'The deduper should see and process only 2 duplicated contacts. The third is unique.'
        );

        $output = $this->testSymfonyCommand(DeduplicateCommand::NAME);

        Assert::assertSame(3, $contactRepository->count([]), $output->getDisplay());
    }

    public function testDeduplicateCommandWithAnotherUniqueFieldAndAnd(): void
    {
        $contactRepository = $this->em->getRepository(Lead::class);

        $fieldRepository = $this->em->getRepository(LeadField::class);

        Assert::assertSame(0, $contactRepository->count([]), 'Some contacts were forgotten to remove from other tests');

        $this->saveContact('[email protected]', '111111111'); // 1
        $this->saveContact('[email protected]', '111111111'); // 1
        $this->saveContact('[email protected]', '222222222'); // 2
        $this->saveContact('[email protected]', '222222222'); // 2
        $this->saveContact('[email protected]', '333333333'); // 3
        $this->saveContact('[email protected]', '333333333'); // 3
        $this->saveContact('[email protected]', '4444444444'); // 4
        $this->saveContact('[email protected]', '4444444444'); // 5

        $phoneField = $fieldRepository->findOneBy(['alias' => 'phone']);
        \assert($phoneField instanceof LeadField);
        $phoneField->setIsUniqueIdentifer(true);
        $phoneField->setLabel('Cell phone'); // Testing also field with more words.
        $this->em->persist($phoneField);

        $this->em->flush();

        Assert::assertSame(8, $contactRepository->count([]));

        $output = $this->testSymfonyCommand(DeduplicateCommand::NAME);

        Assert::assertSame(5, $contactRepository->count([]), $output->getDisplay());
    }

    public function testDeduplicateCommandWithAnotherUniqueFieldAndOr(): void
    {
        $contactRepository = $this->em->getRepository(Lead::class);

        $fieldRepository = $this->em->getRepository(LeadField::class);

        Assert::assertSame(0, $contactRepository->count([]), 'Some contacts were forgotten to remove from other tests');

        $this->saveContact('[email protected]', '111111111'); // 1
        $this->saveContact('[email protected]', '111111111'); // 1
        $this->saveContact('[email protected]', '222222222'); // 1
        $this->saveContact('[email protected]', '222222222'); // 1
        $this->saveContact('[email protected]', '333333333'); // 2
        $this->saveContact('[email protected]', '333333333'); // 2
        $this->saveContact('[email protected]', '4444444444'); // 3
        $this->saveContact('[email protected]', '4444444444'); // 3

        $phoneField = $fieldRepository->findOneBy(['alias' => 'phone']);
        \assert($phoneField instanceof LeadField);
        $phoneField->setIsUniqueIdentifer(true);
        $phoneField->setLabel('Cell phone'); // Testing also field with more words.
        $this->em->persist($phoneField);

        $this->em->flush();

        Assert::assertSame(8, $contactRepository->count([]));

        $output = $this->testSymfonyCommand(DeduplicateCommand::NAME);

        Assert::assertSame(3, $contactRepository->count([]), $output->getDisplay());
    }

    private function saveContact(string $email, string $phone = null): Lead
    {
        $contact = new Lead();
        $contact->setEmail($email);
        $contact->setPhone($phone);
        $contact->setDateIdentified(new \DateTime());

        $this->em->persist($contact);

        return $contact;
    }
}