File size: 4,787 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
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Deduplicate;

use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Deduplicate\ContactMerger;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;

final class ContactMergerFunctionalTest extends MauticMysqlTestCase
{
    public function testMergedContactFound(): void
    {
        $model = static::getContainer()->get('mautic.lead.model.lead');
        \assert($model instanceof LeadModel);

        $merger = static::getContainer()->get('mautic.lead.merger');
        \assert($merger instanceof ContactMerger);

        $bob = new Lead();
        $bob->setFirstname('Bob')
            ->setLastname('Smith')
            ->setEmail('[email protected]');
        $model->saveEntity($bob);
        $bobId = $bob->getId();

        $jane = new Lead();
        $jane->setFirstname('Jane')
            ->setLastname('Smith')
            ->setEmail('[email protected]');
        $model->saveEntity($jane);
        $janeId = $jane->getId();

        $merger->merge($jane, $bob);

        // Bob should have been merged into Jane
        $jane = $model->getEntity($janeId);
        $this->assertEquals($janeId, $jane->getId());

        // If Bob is queried, Jane should be returned
        $jane = $model->getEntity($bobId);
        $this->assertEquals($janeId, $jane->getId());

        // Merge Jane into a third contact
        $joey = new Lead();
        $joey->setFirstname('Joey')
            ->setLastname('Smith')
            ->setEmail('[email protected]');
        $model->saveEntity($joey);
        $joeyId = $joey->getId();

        $merger->merge($joey, $jane);

        // Query for Bob which should now return Joey
        $joey = $model->getEntity($bobId);
        $this->assertEquals($joeyId, $joey->getId());

        // If Joey is deleted, querying for Bob or Jane should result in null
        $model->deleteEntity($joey);
        $bob = $model->getEntity($bobId);
        $this->assertNull($bob);
        $jane = $model->getEntity($janeId);
        $this->assertNull($jane);
    }

    public function testMergedContactsPointsAreAccurate(): void
    {
        $model = static::getContainer()->get('mautic.lead.model.lead');
        \assert($model instanceof LeadModel);

        $em = static::getContainer()->get('doctrine.orm.entity_manager');
        \assert($em instanceof EntityManager);

        $merger = static::getContainer()->get('mautic.lead.merger');
        \assert($merger instanceof ContactMerger);

        // Startout Jane with 50 points
        $jane = new Lead();
        $jane->setFirstname('Jane')
            ->setLastname('Smith')
            ->setEmail('[email protected]')
            ->setPoints(50);

        $model->saveEntity($jane);

        $em->detach($jane);
        $jane = $model->getEntity($jane->getId());
        $this->assertEquals(50, $jane->getPoints());
        $janeId = $jane->getId();

        // Jane is currently a visitor on a different device with 3 points
        $visitor = new Lead();
        $visitor->setPoints(3);
        $model->saveEntity($visitor);
        $em->detach($visitor);
        $visitor = $model->getEntity($visitor->getId());
        $this->assertEquals(3, $visitor->getPoints());

        // Jane submits a form or something that identifies her so the visitor should be merged into Jane giving her 53 points
        $jane = $model->getEntity($janeId);
        // Jane should start out with 50 points
        $this->assertEquals(50, $jane->getPoints());
        // Jane should come out of the merge as Jane
        $jane = $merger->merge($jane, $visitor);
        $this->assertEquals($janeId, $jane->getId());
        // Jane should now have 53 points
        $this->assertEquals(53, $jane->getPoints());
        $em->detach($jane);
        $em->detach($visitor);
        // Jane should still have 53 points
        $jane = $model->getEntity($janeId);
        $this->assertEquals(53, $jane->getPoints());

        // Jane is on another device again and gets 3 points
        $visitor2 = new Lead();
        $visitor2->setPoints(3);
        $model->saveEntity($visitor2);
        $em->detach($visitor2);
        $visitor2 = $model->getEntity($visitor2->getId());
        $this->assertEquals(3, $visitor2->getPoints());

        // Jane again identifies herself, gets merged into the new visitor and so should now have a total of 56 points
        $jane = $model->getEntity($janeId);
        $jane = $merger->merge($jane, $visitor2);
        $this->assertEquals($janeId, $jane->getId());
        $em->detach($jane);
        $em->detach($visitor2);
        $jane = $model->getEntity($jane->getId());

        $this->assertEquals(56, $jane->getPoints());
    }
}