File size: 8,729 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
<?php

declare(strict_types=1);

namespace Mautic\CampaignBundle\Tests\Model;

use Doctrine\DBAL\Exception;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use Mautic\CampaignBundle\Entity\Campaign;
use Mautic\CampaignBundle\Entity\Event;
use Mautic\CampaignBundle\Entity\Lead as CampaignLead;
use Mautic\CampaignBundle\Model\CampaignModel;
use Mautic\CoreBundle\Entity\IpAddress;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Entity\Stat;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\PageBundle\Entity\Hit;
use Mautic\PageBundle\Entity\Redirect;
use Mautic\PageBundle\Entity\Trackable;

class CampaignModelFunctionalTest extends MauticMysqlTestCase
{
    /**
     * @throws OptimisticLockException
     * @throws ORMException
     * @throws Exception
     */
    public function testGetEmailsCountryStats(): void
    {
        /** @var CampaignModel $model */
        $model = $this->getContainer()->get('mautic.campaign.model.campaign');

        $dateFrom     = new \DateTimeImmutable('2023-07-21');
        $dateTo       = new \DateTimeImmutable('2023-07-24');
        $leadsPayload = [
            [
                'email'   => '[email protected]',
                'country' => 'Spain',
                'read'    => true,
                'click'   => true,
            ],
            [
                'email'   => '[email protected]',
                'country' => 'Spain',
                'read'    => true,
                'click'   => false,
            ],
            [
                'email'   => '[email protected]',
                'country' => 'Spain',
                'read'    => false,
                'click'   => false,
            ],
            [
                'email'   => '[email protected]',
                'country' => '',
                'read'    => true,
                'click'   => true,
            ],
            [
                'email'   => '[email protected]',
                'country' => 'Poland',
                'read'    => true,
                'click'   => false,
            ],
            [
                'email'   => '[email protected]',
                'country' => 'Poland',
                'read'    => true,
                'click'   => true,
            ],
        ];

        // Create test campaign
        $campaign = new Campaign();
        $campaign->setName('Test campaign');
        $this->em->persist($campaign);
        $this->em->flush();

        foreach ($leadsPayload as $key => $values) {
            // Create lead
            $lead = new Lead();
            $lead->setEmail($values['email']);
            $lead->setCountry($values['country']);
            $this->em->persist($lead);

            $leadsArr[] = [
                'lead'    => $lead,
                'read'    => $values['read'],
                'click'   => $values['click'],
            ];

            // Create campaign lead and assign to lead
            $campaignLead = new CampaignLead();
            $campaignLead->setLead($lead);
            $campaignLead->setDateAdded(new \DateTime('2023-07-22'));
            $campaignLead->setManuallyAdded(true);
            $campaignLead->setCampaign($campaign);
            $this->em->persist($campaignLead);
            $this->em->flush();

            $campaign->addLead($key, $campaignLead);
        }

        for ($i = 0; $i < 4; ++$i) {
            // Create email
            $email = new Email();
            $email->setName('Test email '.$i);
            $this->em->persist($email);
            $this->em->flush();

            // Create email events
            $event = new Event();
            $event->setName('Send email '.$i);
            $event->setType('email.send');
            $event->setEventType('action');
            $event->setChannel('email');
            $event->setChannelId($email->getId());
            $event->setCampaign($campaign);
            $this->em->persist($event);
            $this->em->flush();

            // Add events to campaign
            $campaign->addEvent($i, $event);

            // Create campaign email sending statistics
            foreach ($leadsArr as $value) {
                $this->emulateEmailStat($value['lead'], $email, $value['read'], $event->getId());

                if ($value['read'] && $value['click']) {
                    $hits       = rand(1, 5);
                    $uniqueHits = rand(1, $hits);
                    $this->emulateClick($value['lead'], $email, $hits, $uniqueHits);
                }
            }
        }

        $results = $model->getCountryStats($campaign, $dateFrom, $dateTo);

        $this->assertCount(4, $campaign->getEmailSendEvents());
        $this->assertCount(3, $results);
        $this->assertSame([
            'contacts' => [
                [
                    'country'  => '',
                    'contacts' => '1',
                ],
                [
                    'country'  => 'Poland',
                    'contacts' => '2',
                ],
                [
                    'country'  => 'Spain',
                    'contacts' => '3',
                ],
            ],
            'clicked_through_count' => [
                [
                    'clicked_through_count' => '4',
                    'country'               => '',
                ],
                [
                    'clicked_through_count' => '4',
                    'country'               => 'Poland',
                ],
                [
                    'clicked_through_count' => '4',
                    'country'               => 'Spain',
                ],
            ],
            'read_count' => [
                [
                    'read_count'            => '4',
                    'country'               => '',
                ],
                [
                    'read_count'            => '8',
                    'country'               => 'Poland',
                ],
                [
                    'read_count'            => '8',
                    'country'               => 'Spain',
                ],
            ],
        ], $results);
    }

    /**
     * @throws OptimisticLockException
     * @throws ORMException
     */
    public function testGetContextEntity(): void
    {
        /** @var CampaignModel $model */
        $model = $this->getContainer()->get('mautic.campaign.model.campaign');

        $campaign = new Campaign();
        $campaign->setName('Test email');
        $this->em->persist($campaign);
        $this->em->flush();

        $id     = $campaign->getId();
        $result = $model->getEntity($id);

        $this->assertSame($campaign, $result);
    }

    /**
     * @throws OptimisticLockException
     * @throws ORMException
     */
    private function emulateEmailStat(Lead $lead, Email $email, bool $isRead, int $sourceId): void
    {
        $stat = new Stat();
        $stat->setEmailAddress('[email protected]');
        $stat->setLead($lead);
        $stat->setDateSent(new \DateTime('2023-07-22'));
        $stat->setEmail($email);
        $stat->setIsRead($isRead);
        $stat->setSource('campaign.event');
        $stat->setSourceId($sourceId);
        $this->em->persist($stat);
        $this->em->flush();
    }

    /**
     * @throws OptimisticLockException
     * @throws ORMException
     */
    private function emulateClick(Lead $lead, Email $email, int $hits, int $uniqueHits): void
    {
        $ipAddress = new IpAddress();
        $ipAddress->setIpAddress('127.0.0.1');
        $this->em->persist($ipAddress);
        $this->em->flush();

        $redirect = new Redirect();
        $redirect->setRedirectId(uniqid());
        $redirect->setUrl('https://example.com');
        $redirect->setUniqueHits($uniqueHits);
        $redirect->setHits($hits);
        $this->em->persist($redirect);

        $trackable = new Trackable();
        $trackable->setChannelId($email->getId());
        $trackable->setHits($hits);
        $trackable->setChannel('email');
        $trackable->setUniqueHits($uniqueHits);
        $trackable->setRedirect($redirect);
        $this->em->persist($trackable);

        $pageHit = new Hit();
        $pageHit->setRedirect($redirect);
        $pageHit->setIpAddress($ipAddress);
        $pageHit->setEmail($email);
        $pageHit->setLead($lead);
        $pageHit->setDateHit(new \DateTime('2023-07-22'));
        $pageHit->setCode(200);
        $pageHit->setUrl($redirect->getUrl());
        $pageHit->setTrackingId($redirect->getRedirectId());
        $pageHit->setSource('email');
        $pageHit->setSourceId($email->getId());
        $this->em->persist($pageHit);
        $this->em->flush();
    }
}