File size: 3,928 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
<?php

declare(strict_types=1);

namespace Mautic\PointBundle\Tests\Functional;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Entity\Stat;
use Mautic\EmailBundle\Entity\StatRepository;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;
use Mautic\PointBundle\Entity\Group;
use Mautic\PointBundle\Entity\Point;

class PointActionFunctionalTest extends MauticMysqlTestCase
{
    public function testPointActionReadEmail(): void
    {
        /** @var LeadModel $leadModel */
        $leadModel = static::getContainer()->get('mautic.lead.model.lead');

        $lead  = $this->createLead('john@doe.email');
        $email = $this->createEmail();

        $trackingHash = 'tracking_hash_123';
        $this->createEmailStat($lead, $email, $trackingHash);
        $pointAction = $this->createReadEmailAction(5);
        $this->client->request('GET', '/email/'.$trackingHash.'.gif');

        $lead = $leadModel->getEntity($lead->getId());

        $this->assertEquals($pointAction->getDelta(), $lead->getPoints());
    }

    public function testPointActionWithGroupReadEmail(): void
    {
        /** @var LeadModel $leadModel */
        $leadModel = static::getContainer()->get('mautic.lead.model.lead');

        $lead   = $this->createLead('john@doe.email');
        $email  = $this->createEmail();
        $group  = $this->createGroup('Group A');

        $trackingHash = 'tracking_hash_123';
        $this->createEmailStat($lead, $email, $trackingHash);
        $pointAction = $this->createReadEmailAction(5, $group);
        $this->client->request('GET', '/email/'.$trackingHash.'.gif');
        $this->em->clear(Lead::class);
        $lead        = $leadModel->getEntity($lead->getId());
        $groupScore  = $lead->getGroupScores()->first();

        $this->assertEquals($pointAction->getDelta(), $groupScore->getScore());
        // group point action shouldn't update main contact points
        $this->assertEquals(0, $lead->getPoints());
    }

    private function createReadEmailAction(int $delta, Group $group = null): Point
    {
        $pointAction = new Point();
        $pointAction->setName('Read email action');
        $pointAction->setDelta($delta);
        $pointAction->setType('email.open');
        if ($group) {
            $pointAction->setGroup($group);
        }
        $this->em->persist($pointAction);
        $this->em->flush();

        return $pointAction;
    }

    private function createEmailStat(
        Lead $lead,
        Email $email,
        string $trackingHash
    ): Stat {
        /** @var StatRepository $statRepository */
        $statRepository = static::getContainer()->get('mautic.email.repository.stat');

        $stat = new Stat();
        $stat->setTrackingHash($trackingHash);
        $stat->setEmailAddress($lead->getEmail());
        $stat->setLead($lead);
        $stat->setDateSent(new \DateTime());
        $stat->setEmail($email);
        $statRepository->saveEntity($stat);

        return $stat;
    }

    private function createLead(
        string $email
    ): Lead {
        $lead = new Lead();
        $lead->setEmail($email);
        $this->em->persist($lead);

        return $lead;
    }

    private function createEmail(): Email
    {
        $email = new Email();
        $email->setName('Test email');
        $email->setSubject('Test email subject');
        $email->setEmailType('template');
        $email->setCustomHtml('<h1>Email content</h1><br>{signature}');
        $email->setIsPublished(true);
        $email->setFromAddress('from@api.test');
        $email->setFromName('API Test');
        $this->em->persist($email);

        return $email;
    }

    private function createGroup(
        string $name
    ): Group {
        $group = new Group();
        $group->setName($name);
        $this->em->persist($group);

        return $group;
    }
}