Issues (3627)

Tests/EventListener/LeadSubscriberTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2017 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\LeadBundle\Tests\EventListener;
13
14
use DateTime;
15
use Doctrine\ORM\EntityManager;
16
use Mautic\CoreBundle\Helper\IpLookupHelper;
17
use Mautic\CoreBundle\Model\AuditLogModel;
18
use Mautic\CoreBundle\Tests\CommonMocks;
19
use Mautic\LeadBundle\Entity\Lead;
20
use Mautic\LeadBundle\Entity\LeadEventLog;
21
use Mautic\LeadBundle\Entity\LeadEventLogRepository;
22
use Mautic\LeadBundle\Event\LeadEvent;
23
use Mautic\LeadBundle\Event\LeadTimelineEvent;
24
use Mautic\LeadBundle\EventListener\LeadSubscriber;
25
use Mautic\LeadBundle\Helper\LeadChangeEventDispatcher;
26
use Mautic\LeadBundle\LeadEvents;
27
use Mautic\LeadBundle\Templating\Helper\DncReasonHelper;
28
use PHPUnit\Framework\MockObject\MockObject;
29
use Symfony\Component\EventDispatcher\EventDispatcher;
30
use Symfony\Component\Routing\RouterInterface;
31
use Symfony\Component\Translation\TranslatorInterface;
32
33
class LeadSubscriberTest extends CommonMocks
34
{
35
    /**
36
     * @var IpLookupHelper|MockObject
37
     */
38
    private $ipLookupHelper;
39
40
    /**
41
     * @var AuditLogModel|MockObject
42
     */
43
    private $auditLogModel;
44
45
    /**
46
     * @var LeadChangeEventDispatcher|MockObject
47
     */
48
    private $leadEventDispatcher;
49
50
    /**
51
     * @var DncReasonHelper|MockObject
52
     */
53
    private $dncReasonHelper;
54
55
    /**
56
     * @var EntityManager|MockObject
57
     */
58
    private $entityManager;
59
60
    /**
61
     * @var TranslatorInterface|MockObject
62
     */
63
    private $translator;
64
65
    /**
66
     * @var RouterInterface|MockObject
67
     */
68
    private $router;
69
70
    protected function setUp(): void
71
    {
72
        $this->ipLookupHelper      = $this->createMock(IpLookupHelper::class);
73
        $this->auditLogModel       = $this->createMock(AuditLogModel::class);
74
        $this->leadEventDispatcher = $this->createMock(LeadChangeEventDispatcher::class);
75
        $this->dncReasonHelper     = $this->createMock(DncReasonHelper::class);
76
        $this->entityManager       = $this->createMock(EntityManager::class);
77
        $this->translator          = $this->createMock(TranslatorInterface::class);
78
        $this->router              = $this->createMock(RouterInterface::class);
79
    }
80
81
    public function testOnLeadPostSaveWillNotProcessTheSameLeadTwice()
82
    {
83
        $lead = new Lead();
84
85
        $lead->setId(54);
86
87
        $changes = [
88
            'title' => [
89
                '0' => 'sdf',
90
                '1' => 'Mr.',
91
            ],
92
            'fields' => [
93
                'firstname' => [
94
                    '0' => 'Test',
95
                    '1' => 'John',
96
                ],
97
                'lastname' => [
98
                    '0' => 'test',
99
                    '1' => 'Doe',
100
                ],
101
                'email' => [
102
                    '0' => '[email protected]',
103
                    '1' => '[email protected]',
104
                ],
105
                'mobile' => [
106
                    '0' => '345345',
107
                    '1' => '555555555',
108
                ],
109
            ],
110
            'dateModified' => [
111
                '0' => '2017-08-21T15:50:57+00:00',
112
                '1' => '2017-08-22T08:04:31+00:00',
113
            ],
114
            'dateLastActive' => [
115
                '0' => '2017-08-21T15:50:57+00:00',
116
                '1' => '2017-08-22T08:04:31+00:00',
117
            ],
118
        ];
119
120
        // This method will be called exactly once
121
        // even though the onLeadPostSave was called twice for the same lead
122
        $this->auditLogModel->expects($this->once())
123
            ->method('writeToLog');
124
125
        $subscriber = new LeadSubscriber(
126
            $this->ipLookupHelper,
127
            $this->auditLogModel,
128
            $this->leadEventDispatcher,
129
            $this->dncReasonHelper,
130
            $this->entityManager,
131
            $this->translator,
132
            $this->router
133
        );
134
135
        $leadEvent = $this->getMockBuilder(LeadEvent::class)
136
            ->disableOriginalConstructor()
137
            ->getMock();
138
139
        $leadEvent->expects($this->exactly(2))
140
            ->method('getLead')
141
            ->will($this->returnValue($lead));
142
143
        $leadEvent->expects($this->exactly(2))
144
            ->method('getChanges')
145
            ->will($this->returnValue($changes));
146
147
        $subscriber->onLeadPostSave($leadEvent);
148
        $subscriber->onLeadPostSave($leadEvent);
149
    }
150
151
    /**
152
     * Make sure that an timeline entry is created for a lead
153
     * that was created through the API.
154
     */
155
    public function testAddTimelineApiCreatedEntries()
156
    {
157
        $eventTypeKey  = 'lead.apiadded';
158
        $eventTypeName = 'Added through API';
159
160
        $this->translator->expects($this->once())
161
            ->method('trans')
162
            ->will($this->returnValue($eventTypeName));
163
164
        $lead = new Lead();
165
166
        $leadEventLog = [
167
            'id'         => '1',
168
            'lead_id'    => '1',
169
            'user_id'    => null,
170
            'user_name'  => null,
171
            'bundle'     => 'lead',
172
            'object'     => 'api-single',
173
            'action'     => 'identified_contact',
174
            'object_id'  => null,
175
            'date_added' => new DateTime(),
176
            'properties' => '{"object_description":"Awesome User"}',
177
        ];
178
179
        $logs = [
180
            'total'   => 1,
181
            'results' => [
182
                $leadEventLog,
183
            ],
184
        ];
185
186
        $timelineEvent = [
187
            'event'      => $eventTypeKey,
188
            'eventId'    => $eventTypeKey.$leadEventLog['id'],
189
            'eventType'  => $eventTypeName,
190
            'eventLabel' => $eventTypeName,
191
            'timestamp'  => $leadEventLog['date_added'],
192
            'icon'       => 'fa-cogs',
193
            'extra'      => $leadEventLog,
194
            'contactId'  => $leadEventLog['lead_id'],
195
        ];
196
197
        $leadEvent = new LeadTimelineEvent(
198
            $lead
199
        );
200
201
        $repo = $this->getMockBuilder(LeadEventLogRepository::class)
202
            ->disableOriginalConstructor()
203
            ->getMock();
204
205
        $repo->expects($this->at(0))
206
            ->method('getEvents')
207
            ->with($lead, 'lead', 'api-single', null, $leadEvent->getQueryOptions())
208
            ->will($this->returnValue($logs));
209
210
        $repo->expects($this->at(1))
211
            ->method('getEvents')
212
            ->with($lead, 'lead', 'api-batch', null, $leadEvent->getQueryOptions())
213
            ->will($this->returnValue(['total' => 0, 'results' => []]));
214
215
        $this->entityManager->method('getRepository')
0 ignored issues
show
The method method() does not exist on Doctrine\ORM\EntityManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
        $this->entityManager->/** @scrutinizer ignore-call */ 
216
                              method('getRepository')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
216
            ->with(LeadEventLog::class)
217
            ->willReturn($repo);
218
219
        $subscriber = new LeadSubscriber(
220
            $this->ipLookupHelper,
221
            $this->auditLogModel,
222
            $this->leadEventDispatcher,
223
            $this->dncReasonHelper,
224
            $this->entityManager,
225
            $this->translator,
226
            $this->router,
227
            true
228
        );
229
230
        $dispatcher = new EventDispatcher();
231
        $dispatcher->addSubscriber($subscriber);
232
233
        $dispatcher->dispatch(LeadEvents::TIMELINE_ON_GENERATE, $leadEvent);
234
235
        $this->assertSame([$timelineEvent], $leadEvent->getEvents());
236
    }
237
}
238